diff options
| author | iamcheeseman <[email protected]> | 2026-05-21 21:55:17 -0400 |
|---|---|---|
| committer | iamcheeseman <[email protected]> | 2026-05-21 21:55:17 -0400 |
| commit | 29ac539d35c6c0363b59ec3fbb26315563d62308 (patch) | |
| tree | 612bd24c3f8a615860b0651d27510e60fda768ce /teensy/teensy_ui.c | |
| parent | 130f8da1e3a923a28b8bea3ec2954eb95795daf1 (diff) | |
move ui command list to windows
Diffstat (limited to 'teensy/teensy_ui.c')
| -rw-r--r-- | teensy/teensy_ui.c | 109 |
1 files changed, 58 insertions, 51 deletions
diff --git a/teensy/teensy_ui.c b/teensy/teensy_ui.c index df49157..5450502 100644 --- a/teensy/teensy_ui.c +++ b/teensy/teensy_ui.c @@ -46,11 +46,11 @@ typedef struct { typedef struct { const char *title; + Cmd *cmds; + Layout *layout; + Layout layout_stack[MAX_LAYOUTS]; ty_Recti rect; uint32_t flags; - - Layout layout_stack[MAX_LAYOUTS]; - Layout *layout; } Window; typedef struct { @@ -63,7 +63,6 @@ typedef struct { tyui_Style style; const ty_Font *font; const float *layout; - Cmd *cmds; ty_Vec2i prev_mouse_pos; } Ctx; @@ -96,7 +95,7 @@ void rect_cmd(ty_Recti rect, ty_Color color) cmd.rect = rect; cmd.color = color; - ty_list_append(uictx.cmds, cmd); + ty_list_append(window()->cmds, cmd); } static @@ -108,7 +107,7 @@ void text_cmd(const char *text, ty_Recti rect, tyui_Align align) cmd.rect = rect; cmd.align = align; - ty_list_append(uictx.cmds, cmd); + ty_list_append(window()->cmds, cmd); } static @@ -118,7 +117,7 @@ void image_cmd(ty_Image img, ty_Recti rect) cmd.kind = CMD_IMAGE; cmd.img = img; cmd.rect = rect; - ty_list_append(uictx.cmds, cmd); + ty_list_append(window()->cmds, cmd); } static @@ -130,7 +129,7 @@ void clip_cmd(ty_Recti rect) rect = window()->rect; // This is a full screen clip cmd.rect = ty_recti_clamp(rect, window()->rect); - ty_list_append(uictx.cmds, cmd); + ty_list_append(window()->cmds, cmd); } static @@ -222,6 +221,8 @@ Window create_window(const char *title, ty_Recti rect, uint32_t flags) win.title = title; win.rect = rect; win.flags = flags; + win.cmds = ty_list_create(); + ty_list_reserve(win.cmds, 32); return win; } @@ -229,9 +230,6 @@ void tyui_init(const ty_Font *font) { assert(font); - uictx.cmds = ty_list_create(); - ty_list_reserve(uictx.cmds, 256); - uint32_t root_flags = 0; root_flags |= TYUI_WIN_NORESIZE; root_flags |= TYUI_WIN_NOCLOSE; @@ -252,7 +250,8 @@ void tyui_init(const ty_Font *font) void tyui_deinit(void) { - ty_list_free(uictx.cmds); + for (int i = 0; i < uictx.window_count; i++) + ty_list_free(uictx.windows[i].cmds); } void tyui_push_layout(const float *column_widths) @@ -292,45 +291,6 @@ void tyui_pop_layout(void) advance_layout(); } -void tyui_draw(void) -{ - for (size_t i = 0; i < ty_list_len(uictx.cmds); i++) { - Cmd cmd = uictx.cmds[i]; - switch (cmd.kind) { - case CMD_RECT: - ty_draw_rect(cmd.rect, cmd.color); - break; - case CMD_TEXT: { - int text_width = ty_font_width(uictx.font, cmd.text); - ty_Vec2i pos = ty_recti_start(cmd.rect); - switch (cmd.align) { - case TYUI_ALIGN_LEFT: - break; - case TYUI_ALIGN_RIGHT: - pos.x += cmd.rect.w - text_width; - break; - case TYUI_ALIGN_CENTER: - 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); - break; - } - case CMD_IMAGE: - ty_draw_image(cmd.img, ty_recti_start(cmd.rect)); - break; - case CMD_CLIP: - ty_draw_set_clip(cmd.rect); - break; - } - } - ty_draw_set_clip(TY_CLIP_NONE); - ty_list_clear(uictx.cmds); - - uictx.prev_mouse_pos = ty_mouse_pos(); -} - bool tyui_begin_window_ex( const char *title, ty_Recti rect, @@ -475,3 +435,50 @@ bool tyui_button(const char *text) return clicked; } +static +void draw_window(Window *win) +{ + for (size_t i = 0; i < ty_list_len(win->cmds); i++) { + Cmd cmd = win->cmds[i]; + switch (cmd.kind) { + case CMD_RECT: + ty_draw_rect(cmd.rect, cmd.color); + break; + case CMD_TEXT: { + int text_width = ty_font_width(uictx.font, cmd.text); + ty_Vec2i pos = ty_recti_start(cmd.rect); + switch (cmd.align) { + case TYUI_ALIGN_LEFT: + break; + case TYUI_ALIGN_RIGHT: + pos.x += cmd.rect.w - text_width; + break; + case TYUI_ALIGN_CENTER: + 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); + break; + } + case CMD_IMAGE: + ty_draw_image(cmd.img, ty_recti_start(cmd.rect)); + break; + case CMD_CLIP: + ty_draw_set_clip(cmd.rect); + break; + } + } + ty_draw_set_clip(TY_CLIP_NONE); + ty_list_clear(win->cmds); +} + +void tyui_draw(void) +{ + draw_window(&uictx.root); + + for (int i = 0; i < uictx.window_count; i++) + draw_window(&uictx.windows[i]); + + uictx.prev_mouse_pos = ty_mouse_pos(); +} |
