diff options
| -rw-r--r-- | editor/tile.c | 32 | ||||
| -rw-r--r-- | editor/tile.h | 1 | ||||
| -rw-r--r-- | teensy/teensy_ui.c | 83 | ||||
| -rw-r--r-- | teensy/teensy_ui.h | 17 |
4 files changed, 97 insertions, 36 deletions
diff --git a/editor/tile.c b/editor/tile.c index b40a6a4..7a9a169 100644 --- a/editor/tile.c +++ b/editor/tile.c @@ -3,14 +3,16 @@ #include "editor.h" static -void tile_ui(void) +void tile_editor_win(void) { Tile_Editor *t = &editor.tile; if ( !tyui_begin_window( "Tile Editor", - ty_recti(0, 0, 100, 100), - &t->tile_editor_win + ty_recti(0, 0, 100, SCREEN_HEIGHT), + &t->tile_editor_win, + .flags = TYUI_WIN_NORESIZEY | TYUI_WIN_NOMOVE | TYUI_WIN_NOTITLE | TYUI_WIN_ALWAYS_BELOW, + .min_size = ty_vec2i(100, SCREEN_HEIGHT), ) ) { return; @@ -21,9 +23,31 @@ void tile_ui(void) tyui_end_window(); } +static +void test_win(void) +{ + Tile_Editor *t = &editor.tile; + if ( + !tyui_begin_window( + "Test", + ty_recti(100, 100, 100, 100), + &t->test_win, + .flags = TYUI_WIN_INVISIBLE + ) + ) { + return; + } + + for (int i = 0; i < 10; i++) + tyui_text("test"); + + tyui_end_window(); +} + void tile_proc(void) { - tile_ui(); + tile_editor_win(); + test_win(); } void tile_draw(void) diff --git a/editor/tile.h b/editor/tile.h index 9ede5bd..0886d4c 100644 --- a/editor/tile.h +++ b/editor/tile.h @@ -13,6 +13,7 @@ typedef struct { typedef struct { Map *open_map; tyui_Id tile_editor_win; + tyui_Id test_win; } Tile_Editor; void tile_proc(void); diff --git a/teensy/teensy_ui.c b/teensy/teensy_ui.c index 374d2bf..f518431 100644 --- a/teensy/teensy_ui.c +++ b/teensy/teensy_ui.c @@ -214,6 +214,18 @@ bool is_hovered(ty_Recti rect) } static +ty_Recti win_rect(Window *win) +{ + ty_Recti rect = win->rect; + if (!(win->flags & TYUI_WIN_NOTITLE)) { + rect.y += tyui.style.title_bar_height; + rect.h -= tyui.style.title_bar_height; + } + + return rect; +} + +static Window create_window(ty_Recti rect, uint32_t flags) { Window win = {0}; @@ -247,7 +259,8 @@ void tyui_init(const ty_Font *font) }; uint32_t root_flags = 0; - root_flags |= TYUI_WIN_NORESIZE; + root_flags |= TYUI_WIN_NORESIZEX; + root_flags |= TYUI_WIN_NORESIZEY; root_flags |= TYUI_WIN_NOCLOSE; root_flags |= TYUI_WIN_NOMOVE; root_flags |= TYUI_WIN_INVISIBLE; @@ -335,33 +348,43 @@ bool tyui_begin_window_ex(tyui_Window_Conf conf, bool *closed_ptr) tyui.active = win; win->layout = win->layout_stack; - if (win->flags & TYUI_WIN_INVISIBLE) - goto done; - ty_Vec2i mouse_pos = ty_mouse_pos(); ty_Vec2i md = mouse_delta(); mouse_pos.x -= md.x; mouse_pos.y -= md.y; - bool hovering_resizer = false; + bool resizing_x = false; + bool resizing_y = false; // Grabbing resizer? { + ty_Vec2i win_start = ty_recti_start(win->rect); ty_Vec2i win_end = ty_recti_end(win->rect); - ty_Recti resize_rect = ty_recti( - win_end.x - 20, win_end.y - 20 + TEXT_HEIGHT, - 20, 20 + + ty_Recti resize_rect_right = ty_recti( + win_end.x - 20, win_start.y, + 20, win->rect.h ); - hovering_resizer = - ty_pointi_in_recti(mouse_pos, resize_rect) || - ty_pointi_in_recti(ty_mouse_pos(), resize_rect); + ty_Recti resize_rect_bottom = ty_recti( + win_start.x, win_end.y - 20 + TEXT_HEIGHT, + win->rect.w, 20 + ); - if (win->lmb_down && hovering_resizer) { - win->rect.w += md.x; - win->rect.h += md.y; + resizing_x = + ty_pointi_in_recti(mouse_pos, resize_rect_right) && + (win->flags & TYUI_WIN_NORESIZEX) == 0; + resizing_y = + ty_pointi_in_recti(mouse_pos, resize_rect_bottom) && + (win->flags & TYUI_WIN_NORESIZEY) == 0; + if (win->lmb_down && resizing_x) { + win->rect.w += md.x; win->rect.w = ty_max(win->rect.w, conf.min_size.x); + } + + if (win->lmb_down && resizing_y) { + win->rect.h += md.y; win->rect.h = ty_max(win->rect.h, conf.min_size.y); } } @@ -372,10 +395,10 @@ bool tyui_begin_window_ex(tyui_Window_Conf conf, bool *closed_ptr) // Grabbing title bar? { if ( - win->lmb_down && + win->lmb_down && + (win->flags & TYUI_WIN_NOMOVE) == 0 && (ty_pointi_in_recti(mouse_pos, title_rect) || - ty_pointi_in_recti(ty_mouse_pos(), title_rect)) - ) { + ty_pointi_in_recti(ty_mouse_pos(), title_rect))) { win->rect.x += md.x; win->rect.y += md.y; @@ -389,18 +412,22 @@ bool tyui_begin_window_ex(tyui_Window_Conf conf, bool *closed_ptr) 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 += tyui.style.title_bar_height; - body_rect.h -= tyui.style.title_bar_height; + if (win->flags & TYUI_WIN_INVISIBLE) + goto skip_invisible; + + ty_Recti body_rect = win_rect(win); rect_cmd(body_rect, tyui.style.win_border); rect_cmd(ty_recti_shrink(body_rect, 1), tyui.style.win_bg); - if (hovering_resizer) { + if (resizing_x) { 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, tyui.style.win_title); + } + + if (resizing_y) { ty_Recti bot_bar = ty_recti( 0, win->rect.y + win->rect.h - 1, win->rect.x + win->rect.w, 1 @@ -408,10 +435,13 @@ bool tyui_begin_window_ex(tyui_Window_Conf conf, bool *closed_ptr) rect_cmd(bot_bar, tyui.style.win_title); } +skip_invisible: + tyui_push_layout((float[]){-1, 0}); - layout()->total_height = tyui.style.title_bar_height + tyui.style.padding; + layout()->total_height = tyui.style.padding; + if ((win->flags & TYUI_WIN_NOTITLE) == 0) + layout()->total_height += tyui.style.title_bar_height; -done: return !closed; } @@ -616,11 +646,14 @@ void focus_window(tyui_Id win_id) if (idx < 0) ty_log_err("uh oh"); + Window *win = &tyui.windows[win_id - 1]; + win->focused = true; + if (win->flags & TYUI_WIN_ALWAYS_BELOW) + return; + 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; - - tyui.windows[win_id - 1].focused = true; } static diff --git a/teensy/teensy_ui.h b/teensy/teensy_ui.h index abcf291..c1081d4 100644 --- a/teensy/teensy_ui.h +++ b/teensy/teensy_ui.h @@ -7,19 +7,22 @@ #define TYUI_MAX_LAYOUT_SIZE (16) #define TYUI_DEFAULT_MIN_WINDOW_SIZE (ty_vec2i(32, 32)) -#define TYUI_WIN_NORESIZE (1 << 0) -#define TYUI_WIN_NOCLOSE (1 << 1) -#define TYUI_WIN_NOMOVE (1 << 2) -#define TYUI_WIN_INVISIBLE (1 << 3) +#define TYUI_WIN_NORESIZEX (1 << 0) +#define TYUI_WIN_NORESIZEY (1 << 1) +#define TYUI_WIN_NOCLOSE (1 << 2) +#define TYUI_WIN_NOMOVE (1 << 3) +#define TYUI_WIN_NOTITLE (1 << 4) +#define TYUI_WIN_INVISIBLE (1 << 5) +#define TYUI_WIN_ALWAYS_BELOW (1 << 6) // Marks the beginning of a window, with sane defaults. -#define tyui_begin_window(_title, _rect, _id) \ +#define tyui_begin_window(_title, _rect, _id, ...) \ tyui_begin_window_ex((tyui_Window_Conf){ \ .title = _title, \ .rect = _rect, \ - .min_size = TYUI_DEFAULT_MIN_WINDOW_SIZE, \ .id = _id, \ - .flags = 0, \ + .min_size = TYUI_DEFAULT_MIN_WINDOW_SIZE, \ + __VA_ARGS__ \ }, NULL) // Displays left-aligned, formatted text. |
