diff options
Diffstat (limited to 'teensy/teensy_ui.c')
| -rw-r--r-- | teensy/teensy_ui.c | 66 |
1 files changed, 60 insertions, 6 deletions
diff --git a/teensy/teensy_ui.c b/teensy/teensy_ui.c index b68b7ab..cfd78c3 100644 --- a/teensy/teensy_ui.c +++ b/teensy/teensy_ui.c @@ -9,6 +9,7 @@ #define TEXT_HEIGHT (8) #define MAX_LAYOUTS (16) +#define MAX_WINDOWS (255) #define window() (uictx.active) #define layout() (window()->layout - 1) @@ -51,12 +52,16 @@ typedef struct { Layout layout_stack[MAX_LAYOUTS]; ty_Recti rect; uint32_t flags; + bool focused; + bool has_lmb; + bool has_rmb; } Window; typedef struct { Window *active; Window root; - Window windows[255]; + Window windows[MAX_WINDOWS]; + int window_order[MAX_WINDOWS]; int window_count; int layout_idx; int layout_size; @@ -316,6 +321,7 @@ bool tyui_begin_window_ex( if (*id == 0) { *id = ++uictx.window_count; uictx.windows[*id - 1] = create_window(title, rect, flags); + uictx.window_order[uictx.window_count - 1] = *id; } Window *win = &uictx.windows[*id - 1]; @@ -344,7 +350,7 @@ bool tyui_begin_window_ex( ty_pointi_in_recti(mouse_pos, resize_rect) || ty_pointi_in_recti(ty_mouse_pos(), resize_rect); - if (ty_button_down(TY_BTN_DB_LMB) && hovering_resizer) { + if (win->has_lmb && hovering_resizer) { win->rect.w += md.x; win->rect.h += md.y; } @@ -356,7 +362,7 @@ bool tyui_begin_window_ex( // Grabbing title bar? { if ( - ty_button_down(TY_BTN_DB_LMB) && + win->has_lmb && (ty_pointi_in_recti(mouse_pos, title_rect) || ty_pointi_in_recti(ty_mouse_pos(), title_rect)) ) { @@ -429,7 +435,7 @@ bool tyui_button(const char *text) bool clicked = false; if (is_hovered(rect)) { - clicked = ty_button_pressed(TY_BTN_DB_LMB); + clicked = window()->has_lmb; bg_col = ty_button_down(TY_BTN_DB_LMB) ? uictx.style.bg_pressed : uictx.style.bg_hover; @@ -479,12 +485,60 @@ void draw_window(Window *win) ty_list_clear(win->cmds); } +static +void focus_window(tyui_Id win_id) +{ + int idx = uictx.window_count - 1; + for (; idx >= 0; idx--) { + if (uictx.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; + + uictx.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]; + win->has_lmb = false; + win->has_rmb = false; + 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]; + if (!is_hovered(win->rect)) + continue; + + win->has_lmb = ty_button_down(TY_BTN_DB_LMB); + win->has_rmb = ty_button_down(TY_BTN_DB_RMB); + + if (win->has_lmb || win->has_rmb) + focus_window(win_id); + + break; + } +} + void tyui_draw(void) { draw_window(&uictx.root); - for (int i = 0; i < uictx.window_count; i++) - draw_window(&uictx.windows[i]); + for (int i = 0; i < uictx.window_count; i++) { + tyui_Id win_id = uictx.window_order[i]; + draw_window(&uictx.windows[win_id - 1]); + } uictx.prev_mouse_pos = ty_mouse_pos(); + update_windows(); } |
