aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriamcheeseman <[email protected]>2026-05-23 12:19:03 -0400
committeriamcheeseman <[email protected]>2026-05-23 12:19:03 -0400
commitfc3ad3fec91ba54414cc5cb126d61803611056f3 (patch)
treea8d73c97b4bb9ed974423cc93e17535a35abe2cd
parent8d9195df408915847fedfd595ea73ce24357788c (diff)
rename `uictx` to `tyui`
-rw-r--r--teensy/teensy_ui.c163
1 files changed, 80 insertions, 83 deletions
diff --git a/teensy/teensy_ui.c b/teensy/teensy_ui.c
index d055802..6a156bb 100644
--- a/teensy/teensy_ui.c
+++ b/teensy/teensy_ui.c
@@ -12,13 +12,10 @@
#define MAX_LAYOUTS (16)
#define MAX_WINDOWS (255)
-#define window() (uictx.active)
+#define window() (tyui.active)
#define layout() (window()->layout - 1)
#define push_layout() (window()->layout++)
#define pop_layout() (--window()->layout)
-#define padding() (uictx.style.padding)
-#define control_padding() (uictx.style.control_padding)
-#define title_bar_height() (uictx.style.title_bar_height)
enum {
CMD_RECT,
@@ -98,7 +95,7 @@ tyui_Style default_style = {
};
static
-Ctx uictx;
+Ctx tyui;
static
void rect_cmd(ty_Recti rect, ty_Color color)
@@ -150,8 +147,8 @@ static
void draw_frame(ty_Recti rect, ty_Color bg_col)
{
clip_cmd(TY_CLIP_NONE);
- rect_cmd(rect, uictx.style.frame);
- ty_Recti content_rect = ty_recti_shrink(rect, uictx.style.frame_size);
+ rect_cmd(rect, tyui.style.frame);
+ ty_Recti content_rect = ty_recti_shrink(rect, tyui.style.frame_size);
rect_cmd(content_rect, bg_col);
clip_cmd(content_rect);
}
@@ -164,12 +161,12 @@ void advance_layout(void)
if (++l->idx < l->columns)
return;
- l->total_height += l->max_height + padding();
+ l->total_height += l->max_height + tyui.style.padding;
l->idx = 0;
l->max_height = 0;
if (l == window()->layout_stack)
- l->x = padding();
+ l->x = tyui.style.padding;
else
l->x = 0;
}
@@ -181,7 +178,7 @@ int get_column_width(const Layout *l)
if (width < 0)
return width + l->width - l->x + 1;
if (width < 1)
- return (l->width - padding() * 2) * width;
+ return (l->width - tyui.style.padding * 2) * width;
return width;
}
@@ -207,7 +204,7 @@ ty_Recti next_rect(int height)
width = ty_max(width, 0);
height = ty_max(height, 0);
- l->x += width + padding();
+ l->x += width + tyui.style.padding;
advance_layout();
@@ -219,8 +216,8 @@ ty_Vec2i mouse_delta(void)
{
ty_Vec2i cur = ty_mouse_pos();
return ty_vec2i(
- cur.x - uictx.prev_mouse_pos.x,
- cur.y - uictx.prev_mouse_pos.y
+ cur.x - tyui.prev_mouse_pos.x,
+ cur.y - tyui.prev_mouse_pos.y
);
}
@@ -251,19 +248,19 @@ void tyui_init(const ty_Font *font)
root_flags |= TYUI_WIN_NOMOVE;
root_flags |= TYUI_WIN_INVISIBLE;
- uictx.root = create_window(ty_recti(0, 0, 256, 256), root_flags);
+ tyui.root = create_window(ty_recti(0, 0, 256, 256), root_flags);
- uictx.style = default_style;
- uictx.font = font;
+ tyui.style = default_style;
+ tyui.font = font;
- uictx.active = &uictx.root;
+ tyui.active = &tyui.root;
}
void tyui_deinit(void)
{
- ty_list_free(uictx.root.cmds);
- for (int i = 0; i < uictx.window_count; i++)
- ty_list_free(uictx.windows[i].cmds);
+ ty_list_free(tyui.root.cmds);
+ for (int i = 0; i < tyui.window_count; i++)
+ ty_list_free(tyui.windows[i].cmds);
}
void tyui_push_layout(const float *column_widths)
@@ -291,15 +288,15 @@ void tyui_push_layout(const float *column_widths)
else
new->width = window()->rect.w;
- new->width -= padding();
+ new->width -= tyui.style.padding;
}
void tyui_pop_layout(void)
{
Layout *old = pop_layout();
Layout *top = layout();
- top->max_height = ty_max(top->max_height, old->total_height - padding());
- top->x += old->width + padding();
+ top->max_height = ty_max(top->max_height, old->total_height - tyui.style.padding);
+ top->x += old->width + tyui.style.padding;
advance_layout();
}
@@ -313,7 +310,7 @@ bool tyui_begin_window_ex(tyui_Window_Conf conf, bool *closed_ptr)
if (closed_ptr)
closed = *closed_ptr;
- if (window() != &uictx.root) {
+ if (window() != &tyui.root) {
ty_log_err("cannot create window within another window");
return false;
}
@@ -322,13 +319,13 @@ bool tyui_begin_window_ex(tyui_Window_Conf conf, bool *closed_ptr)
return !closed;
if (*conf.id == 0) {
- *conf.id = ++uictx.window_count;
- uictx.windows[*conf.id - 1] = create_window(conf.rect, conf.flags);
- uictx.window_order[uictx.window_count - 1] = *conf.id;
+ *conf.id = ++tyui.window_count;
+ tyui.windows[*conf.id - 1] = create_window(conf.rect, conf.flags);
+ tyui.window_order[tyui.window_count - 1] = *conf.id;
}
- Window *win = &uictx.windows[*conf.id - 1];
- uictx.active = win;
+ Window *win = &tyui.windows[*conf.id - 1];
+ tyui.active = win;
win->layout = win->layout_stack;
if (win->flags & TYUI_WIN_INVISIBLE)
@@ -363,7 +360,7 @@ bool tyui_begin_window_ex(tyui_Window_Conf conf, bool *closed_ptr)
}
ty_Recti title_rect = win->rect;
- title_rect.h = title_bar_height();
+ title_rect.h = tyui.style.title_bar_height;
// Grabbing title bar?
{
@@ -376,36 +373,36 @@ bool tyui_begin_window_ex(tyui_Window_Conf conf, bool *closed_ptr)
win->rect.y += md.y;
title_rect = win->rect;
- title_rect.h = title_bar_height();
+ title_rect.h = tyui.style.title_bar_height;
}
}
clip_cmd(TY_CLIP_NONE);
- rect_cmd(title_rect, uictx.style.win_title);
+ rect_cmd(title_rect, tyui.style.win_title);
text_cmd(conf.title, ty_recti_shrink(title_rect, 1), TYUI_ALIGN_LEFT);
ty_Recti body_rect = win->rect;
- body_rect.y += title_bar_height();
- body_rect.h -= title_bar_height();
- rect_cmd(body_rect, uictx.style.win_border);
- rect_cmd(ty_recti_shrink(body_rect, 1), uictx.style.win_bg);
+ body_rect.y += tyui.style.title_bar_height;
+ body_rect.h -= tyui.style.title_bar_height;
+ rect_cmd(body_rect, tyui.style.win_border);
+ rect_cmd(ty_recti_shrink(body_rect, 1), tyui.style.win_bg);
if (hovering_resizer) {
ty_Recti right_bar = ty_recti(
win->rect.x + win->rect.w - 1, 0,
1, win->rect.y + win->rect.h
);
- rect_cmd(right_bar, uictx.style.win_title);
+ rect_cmd(right_bar, tyui.style.win_title);
ty_Recti bot_bar = ty_recti(
0, win->rect.y + win->rect.h - 1,
win->rect.x + win->rect.w, 1
);
- rect_cmd(bot_bar, uictx.style.win_title);
+ rect_cmd(bot_bar, tyui.style.win_title);
}
tyui_push_layout((float[]){-1, 0});
- layout()->total_height = title_bar_height() + padding();
+ layout()->total_height = tyui.style.title_bar_height + tyui.style.padding;
done:
return !closed;
@@ -413,14 +410,14 @@ done:
void tyui_end_window(void)
{
- if (window() == &uictx.root) {
+ if (window() == &tyui.root) {
ty_log_err("ending window with no window open");
return;
}
clip_cmd(TY_CLIP_NONE);
- uictx.active = &uictx.root;
+ tyui.active = &tyui.root;
}
void tyui_text_ex(tyui_Align align, const char *fmt, ...)
@@ -438,19 +435,19 @@ void tyui_text_ex(tyui_Align align, const char *fmt, ...)
bool tyui_button(const char *text)
{
- ty_Recti rect = next_rect(TEXT_HEIGHT + control_padding() * 2);
- ty_Color bg_col = uictx.style.bg_normal;
+ ty_Recti rect = next_rect(TEXT_HEIGHT + tyui.style.control_padding * 2);
+ ty_Color bg_col = tyui.style.bg_normal;
bool clicked = false;
if (is_hovered(rect)) {
clicked = window()->lmb_pressed;
bg_col = window()->lmb_down
- ? uictx.style.bg_pressed
- : uictx.style.bg_hover;
+ ? tyui.style.bg_pressed
+ : tyui.style.bg_hover;
}
draw_frame(rect, bg_col);
- text_cmd(text, ty_recti_shrink(rect, control_padding()), TYUI_ALIGN_CENTER);
+ text_cmd(text, ty_recti_shrink(rect, tyui.style.control_padding), TYUI_ALIGN_CENTER);
return clicked;
}
@@ -464,21 +461,21 @@ void tyui_slider_ex(
) {
assert(value_ptr);
- const int grabber_size = uictx.style.grabber_size;
+ const int grabber_size = tyui.style.grabber_size;
- ty_Recti rect = next_rect(TEXT_HEIGHT + control_padding() * 2);
- ty_Color bg_col = uictx.style.bg_normal;
- ty_Color fg_col = uictx.style.fg_normal;
+ ty_Recti rect = next_rect(TEXT_HEIGHT + tyui.style.control_padding * 2);
+ ty_Color bg_col = tyui.style.bg_normal;
+ ty_Color fg_col = tyui.style.fg_normal;
bool clicked = false;
if (is_hovered(rect)) {
clicked = window()->lmb_down;
if (window()->lmb_down) {
- bg_col = uictx.style.bg_pressed;
- fg_col = uictx.style.fg_pressed;
+ bg_col = tyui.style.bg_pressed;
+ fg_col = tyui.style.fg_pressed;
} else {
- bg_col = uictx.style.bg_hover;
- fg_col = uictx.style.fg_hover;
+ bg_col = tyui.style.bg_hover;
+ fg_col = tyui.style.fg_hover;
}
}
@@ -494,8 +491,8 @@ void tyui_slider_ex(
draw_frame(rect, bg_col);
if (show_value) {
text_cmd(
- ty_format(uictx.style.slider_fmt, (int)round(value)),
- ty_recti_shrink(rect, control_padding()),
+ ty_format(tyui.style.slider_fmt, (int)round(value)),
+ ty_recti_shrink(rect, tyui.style.control_padding),
value_align
);
}
@@ -519,19 +516,19 @@ void tyui_text_input(tyui_Text_Input *input)
{
// ASSUMES ASCII
- ty_Recti rect = next_rect(TEXT_HEIGHT + control_padding() * 2);
+ ty_Recti rect = next_rect(TEXT_HEIGHT + tyui.style.control_padding * 2);
size_t len = strlen(input->chars);
uint32_t c;
- bool focused = input == uictx.focused_text_box;
+ bool focused = input == tyui.focused_text_box;
bool hovered = is_hovered(rect);
if (!focused && hovered && window()->lmb_pressed) {
- uictx.focused_text_box = input;
+ tyui.focused_text_box = input;
focused = true;
} else if (focused && !hovered && window()->lmb_pressed) {
- uictx.focused_text_box = NULL;
+ tyui.focused_text_box = NULL;
focused = false;
}
@@ -545,10 +542,10 @@ void tyui_text_input(tyui_Text_Input *input)
input->chars[len - 1] = '\0';
}
- draw_frame(rect, uictx.style.bg_normal);
+ draw_frame(rect, tyui.style.bg_normal);
- ty_Recti text_rect = ty_recti_shrink(rect, control_padding());
- int text_width = ty_font_width(uictx.font, input->chars);
+ ty_Recti text_rect = ty_recti_shrink(rect, tyui.style.control_padding);
+ int text_width = ty_font_width(tyui.font, input->chars);
text_rect.w = fmin(text_rect.w, text_width);
text_cmd(
input->chars,
@@ -558,7 +555,7 @@ void tyui_text_input(tyui_Text_Input *input)
if (focused && (int)(ty_get_time() * 2) % 2 == 0) {
text_rect.x += fmin(text_width, text_rect.w - 1);
text_rect.w = 1;
- rect_cmd(text_rect, uictx.style.fg_normal);
+ rect_cmd(text_rect, tyui.style.fg_normal);
}
}
@@ -572,7 +569,7 @@ void draw_window(Window *win)
ty_draw_rect(cmd.rect, cmd.color);
break;
case CMD_TEXT: {
- int text_width = ty_font_width(uictx.font, cmd.text);
+ int text_width = ty_font_width(tyui.font, cmd.text);
ty_Vec2i pos = ty_recti_start(cmd.rect);
switch (cmd.align) {
case TYUI_ALIGN_LEFT:
@@ -584,8 +581,8 @@ void draw_window(Window *win)
pos.x += (cmd.rect.w - text_width) / 2;
break;
}
- pos.y += (cmd.rect.h - ty_font_height(uictx.font)) / 2;
- ty_draw_text(uictx.font, pos, cmd.text);
+ pos.y += (cmd.rect.h - ty_font_height(tyui.font)) / 2;
+ ty_draw_text(tyui.font, pos, cmd.text);
break;
}
case CMD_IMAGE:
@@ -603,27 +600,27 @@ void draw_window(Window *win)
static
void focus_window(tyui_Id win_id)
{
- int idx = uictx.window_count - 1;
+ int idx = tyui.window_count - 1;
for (; idx >= 0; idx--) {
- if (uictx.window_order[idx] == win_id)
+ if (tyui.window_order[idx] == win_id)
break;
}
if (idx < 0)
ty_log_err("uh oh");
- for (int i = idx; i < uictx.window_count - 1; i++)
- uictx.window_order[i] = uictx.window_order[i + 1];
- uictx.window_order[uictx.window_count - 1] = win_id;
+ for (int i = idx; i < tyui.window_count - 1; i++)
+ tyui.window_order[i] = tyui.window_order[i + 1];
+ tyui.window_order[tyui.window_count - 1] = win_id;
- uictx.windows[win_id - 1].focused = true;
+ tyui.windows[win_id - 1].focused = true;
}
static
void update_windows(void)
{
- for (int i = 0; i < uictx.window_count; i++) {
- Window *win = &uictx.windows[i];
+ for (int i = 0; i < tyui.window_count; i++) {
+ Window *win = &tyui.windows[i];
win->lmb_down = false;
win->rmb_down = false;
win->lmb_pressed = false;
@@ -631,9 +628,9 @@ void update_windows(void)
win->focused = false;
}
- for (int i = uictx.window_count - 1; i >= 0; i--) {
- tyui_Id win_id = uictx.window_order[i];
- Window *win = &uictx.windows[win_id - 1];
+ for (int i = tyui.window_count - 1; i >= 0; i--) {
+ tyui_Id win_id = tyui.window_order[i];
+ Window *win = &tyui.windows[win_id - 1];
if (!is_hovered(win->rect))
continue;
@@ -651,13 +648,13 @@ void update_windows(void)
void tyui_draw(void)
{
- draw_window(&uictx.root);
+ draw_window(&tyui.root);
- for (int i = 0; i < uictx.window_count; i++) {
- tyui_Id win_id = uictx.window_order[i];
- draw_window(&uictx.windows[win_id - 1]);
+ for (int i = 0; i < tyui.window_count; i++) {
+ tyui_Id win_id = tyui.window_order[i];
+ draw_window(&tyui.windows[win_id - 1]);
}
- uictx.prev_mouse_pos = ty_mouse_pos();
+ tyui.prev_mouse_pos = ty_mouse_pos();
update_windows();
}