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.c74
1 files changed, 31 insertions, 43 deletions
diff --git a/teensy/teensy_ui.c b/teensy/teensy_ui.c
index 27af0cf..32e4ca5 100644
--- a/teensy/teensy_ui.c
+++ b/teensy/teensy_ui.c
@@ -21,7 +21,7 @@
enum {
CMD_RECT,
CMD_TEXT,
- CMD_TARGET,
+ CMD_CLIP,
CMD_IMAGE,
};
typedef uint8_t Cmd_Kind;
@@ -47,7 +47,6 @@ typedef struct {
typedef struct {
const char *title;
ty_Recti rect;
- ty_Image content;
uint32_t flags;
Layout layout_stack[MAX_LAYOUTS];
@@ -123,19 +122,25 @@ void image_cmd(ty_Image img, ty_Recti rect)
}
static
-void target_cmd(ty_Image target)
+void clip_cmd(ty_Recti rect)
{
Cmd cmd = {};
- cmd.kind = CMD_TARGET;
- cmd.img = target;
+ cmd.kind = CMD_CLIP;
+ if (rect.w < 0 || rect.h < 0)
+ rect = window()->rect; // This is a full screen clip
+
+ cmd.rect = ty_recti_clamp(rect, window()->rect);
ty_list_append(uictx.cmds, cmd);
}
static
void draw_frame(ty_Recti rect, ty_Color bg_col)
{
+ clip_cmd(TY_CLIP_NONE);
rect_cmd(rect, uictx.style.frame);
- rect_cmd(ty_recti_shrink(rect, uictx.style.frame_size), bg_col);
+ ty_Recti content_rect = ty_recti_shrink(rect, uictx.style.frame_size);
+ rect_cmd(content_rect, bg_col);
+ clip_cmd(content_rect);
}
static
@@ -186,6 +191,9 @@ ty_Recti next_rect(int height)
y += outer->total_height;
}
+ x += window()->rect.x;
+ y += window()->rect.y;
+
advance_layout();
return ty_recti(x, y, width, height);
@@ -202,18 +210,9 @@ ty_Vec2i mouse_delta(void)
}
static
-ty_Vec2i mouse_pos(void)
-{
- ty_Vec2i mouse_pos = ty_mouse_pos();
- mouse_pos.x -= window()->rect.x;
- mouse_pos.y -= window()->rect.y;
- return mouse_pos;
-}
-
-static
bool is_hovered(ty_Recti rect)
{
- return ty_pointi_in_recti(mouse_pos(), rect);
+ return ty_pointi_in_recti(ty_mouse_pos(), rect);
}
static
@@ -223,7 +222,6 @@ Window create_window(const char *title, ty_Recti rect, uint32_t flags)
win.title = title;
win.rect = rect;
win.flags = flags;
- win.content = ty_create_image(rect.w, rect.h, NULL);
return win;
}
@@ -255,11 +253,6 @@ void tyui_init(const ty_Font *font)
void tyui_deinit(void)
{
ty_list_free(uictx.cmds);
-
- ty_free_image(uictx.root.content);
- for (int i = 0; i < uictx.window_count; i++) {
- ty_free_image(uictx.windows[i].content);
- }
}
void tyui_push_layout(const float *column_widths)
@@ -327,12 +320,8 @@ void tyui_draw(void)
case CMD_IMAGE:
ty_draw_image(cmd.img, ty_recti_start(cmd.rect));
break;
- case CMD_TARGET:
- // Add a draw rect property to the renderer, which allows me to
- // define a rect that anything drawn will be clipped by.
-
- ty_draw_set_target(cmd.img.data ? &cmd.img : NULL);
- if (cmd.img.data != NULL) ty_draw_clear(TY_COLOR_MAGENTA);
+ case CMD_CLIP:
+ ty_draw_set_clip(cmd.rect);
break;
}
}
@@ -397,12 +386,6 @@ bool tyui_begin_window_ex(
if (ty_button_down(TY_BTN_DB_LMB) && hovering_resizer) {
win->rect.w += md.x;
win->rect.h += md.y;
- ty_free_image(win->content);
- win->content = ty_create_image(
- win->rect.w,
- win->rect.h + title_rect.h,
- NULL
- );
}
// Grabbing title bar?
@@ -418,20 +401,26 @@ bool tyui_begin_window_ex(
title_rect.h = title_bar_height();
}
- target_cmd(win->content);
+ clip_cmd(TY_CLIP_NONE);
- title_rect = ty_recti(0, 0, win->rect.w, title_bar_height());
rect_cmd(title_rect, uictx.style.win_title);
text_cmd(win->title, title_rect, TYUI_ALIGN_LEFT);
ty_Recti body_rect = win->rect;
- body_rect.x = 0;
- body_rect.y = title_bar_height();
+ body_rect.y += title_bar_height();
rect_cmd(body_rect, uictx.style.win_bg);
if (hovering_resizer) {
- rect_cmd(ty_recti(win->rect.w-1, title_bar_height(), 1, win->rect.h), uictx.style.win_title);
- rect_cmd(ty_recti(0, win->rect.h-1+title_bar_height(), win->rect.w, 1), uictx.style.win_title);
+ 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);
+ 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);
}
tyui_push_layout((float[]){-1, 0});
@@ -448,9 +437,7 @@ void tyui_end_window(void)
return;
}
- target_cmd((ty_Image){});
-
- image_cmd(window()->content, window()->rect);
+ clip_cmd(TY_CLIP_NONE);
uictx.active = &uictx.root;
}
@@ -464,6 +451,7 @@ void tyui_text_ex(tyui_Align align, const char *fmt, ...)
ty_Recti rect = next_rect(TEXT_HEIGHT);
+ clip_cmd(rect);
text_cmd(text, rect, align);
}