aboutsummaryrefslogtreecommitdiff
path: root/teensy/teensy_ui.c
diff options
context:
space:
mode:
Diffstat (limited to 'teensy/teensy_ui.c')
-rw-r--r--teensy/teensy_ui.c58
1 files changed, 36 insertions, 22 deletions
diff --git a/teensy/teensy_ui.c b/teensy/teensy_ui.c
index 4dd9d5d..5913005 100644
--- a/teensy/teensy_ui.c
+++ b/teensy/teensy_ui.c
@@ -56,11 +56,11 @@ typedef struct {
Layout layout_stack[MAX_LAYOUTS];
ty_Recti rect;
uint32_t flags;
- bool focused;
bool lmb_down;
bool rmb_down;
bool lmb_pressed;
bool rmb_pressed;
+ int scroll;
} Window;
typedef struct {
@@ -77,6 +77,7 @@ typedef struct {
ty_Vec2i prev_mouse_pos;
+ Window *focused_win;
const tyui_Text_Input *focused_text_box;
} Ctx;
@@ -84,6 +85,18 @@ static tyui_Style default_style;
static Ctx tyui;
static
+ty_Recti win_content(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
void rect_cmd(ty_Recti rect, ty_Color color)
{
Cmd cmd = {0};
@@ -123,9 +136,9 @@ void clip_cmd(ty_Recti rect)
cmd.kind = CMD_CLIP;
if (rect.w < 0 || rect.h < 0)
- rect = window()->rect; // This is a full screen clip
+ rect = win_content(window()); // This is a full screen clip
- cmd.rect = ty_recti_clamp(rect, window()->rect);
+ cmd.rect = ty_recti_clamp(rect, win_content(window()));
ty_list_append(&window()->cmds, cmd);
}
@@ -214,18 +227,6 @@ 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};
@@ -263,7 +264,9 @@ void tyui_init(const ty_Font *font)
root_flags |= TYUI_WIN_NORESIZEY;
root_flags |= TYUI_WIN_NOCLOSE;
root_flags |= TYUI_WIN_NOMOVE;
+ root_flags |= TYUI_WIN_NOTITLE;
root_flags |= TYUI_WIN_INVISIBLE;
+ root_flags |= TYUI_WIN_ALWAYS_BELOW;
tyui.root = create_window(
ty_recti(0, 0, ctx.hints.scr_width, ctx.hints.scr_height),
@@ -325,6 +328,8 @@ bool tyui_begin_window_ex(tyui_Window_Conf conf, bool *closed_ptr)
assert(conf.title);
assert(conf.id);
+ tyui.active = &tyui.root;
+
bool closed = false;
if (closed_ptr)
@@ -345,7 +350,6 @@ bool tyui_begin_window_ex(tyui_Window_Conf conf, bool *closed_ptr)
}
Window *win = &tyui.windows[*conf.id - 1];
- tyui.active = win;
win->layout = win->layout_stack;
ty_Vec2i mouse_pos = ty_mouse_pos();
@@ -415,7 +419,7 @@ bool tyui_begin_window_ex(tyui_Window_Conf conf, bool *closed_ptr)
if (win->flags & TYUI_WIN_INVISIBLE)
goto skip_invisible;
- ty_Recti body_rect = win_rect(win);
+ ty_Recti body_rect = win_content(win);
rect_cmd(body_rect, tyui.style.win_border);
rect_cmd(ty_recti_shrink(body_rect, 1), tyui.style.win_bg);
@@ -437,8 +441,19 @@ bool tyui_begin_window_ex(tyui_Window_Conf conf, bool *closed_ptr)
skip_invisible:
+ tyui.active = win;
+ clip_cmd(win_content(win));
+
+ // TODO: Maybe make a distinction between focused and the top-most hovered
+ // window? By forcing the user to focus the window, they must click on it
+ // beforehand, which is bad UX.
+ if (tyui.focused_win == win && is_hovered(win->rect)) {
+ win->scroll += ty_platform_get_scroll() * TYUI_SCROLL;
+ win->scroll = ty_min(win->scroll, 0);
+ }
+
tyui_push_layout((float[]){-1, 0});
- layout()->total_height = tyui.style.padding;
+ layout()->total_height = tyui.style.padding + win->scroll;
if ((win->flags & TYUI_WIN_NOTITLE) == 0)
layout()->total_height += tyui.style.title_bar_height;
@@ -452,9 +467,9 @@ void tyui_end_window(void)
return;
}
- clip_cmd(TY_CLIP_NONE);
-
tyui.active = &tyui.root;
+
+ clip_cmd(window()->rect);
}
void tyui_text_ex(tyui_Align align, const char *fmt, ...)
@@ -647,7 +662,7 @@ void focus_window(tyui_Id win_id)
ty_log_err("uh oh");
Window *win = &tyui.windows[win_id - 1];
- win->focused = true;
+ tyui.focused_win = win;
if (win->flags & TYUI_WIN_ALWAYS_BELOW)
return;
@@ -665,7 +680,6 @@ void update_windows(void)
win->rmb_down = false;
win->lmb_pressed = false;
win->rmb_pressed = false;
- win->focused = false;
}
for (int i = tyui.window_count - 1; i >= 0; i--) {