From c5b263e94812baea81d6bcfe3f1f0fc81b292631 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Wed, 17 Jan 2024 23:28:31 +0000 Subject: [PATCH] Finish implementing node element and add init_ui_ctx function --- include/ui.h | 9 +++++---- src/ui.c | 30 +++++++++++++++++------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/include/ui.h b/include/ui.h index 82a629d..26883d0 100644 --- a/include/ui.h +++ b/include/ui.h @@ -35,16 +35,17 @@ struct ui_ctx { u64 count; i64 hovered; i64 active; - i64 mouse_x; - i64 mouse_y; - i64 rel_x; - i64 rel_y; + i32 mouse_x; + i32 mouse_y; + i32 rel_x; + i32 rel_y; bool mouse_down; bool mouse_up; const window_t *wnd; ui_elem_t elements[MAX_UI_ELEMENTS]; }; +void init_ui_ctx(ui_ctx_t *ctx); void reset_ui_ctx(ui_ctx_t *ctx); void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx, const SDL_Event *event); diff --git a/src/ui.c b/src/ui.c index aa21aca..4061c17 100644 --- a/src/ui.c +++ b/src/ui.c @@ -2,7 +2,6 @@ #include "SDL_events.h" #include "aliases/aliases.h" #include "window.h" -#include typedef struct ui_elem_colours ui_elem_colours_t; struct ui_elem_colours { @@ -28,21 +27,22 @@ bool aabb(const ui_elem_t *elem, i32 x, i32 y) { y > elem->rect.topleft.y && y <= elem->rect.topleft.y + elem->rect.h; } +void init_ui_ctx(ui_ctx_t *ctx) { + *ctx = (ui_ctx_t){0}; + ctx->hovered = -1; + ctx->active = -1; +} + void reset_ui_ctx(ui_ctx_t *ctx) { ctx->count = 0; ctx->mouse_down = false; ctx->mouse_up = false; - ctx->rel_x = 0; - ctx->rel_y = 0; } void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx, const SDL_Event *event) { switch (event->type) { case SDL_MOUSEMOTION: - ctx->rel_x = event->motion.xrel; - ctx->rel_y = event->motion.yrel; - if (wnd->id == event->motion.windowID) { ctx->mouse_x = event->motion.x; ctx->mouse_y = event->motion.y; @@ -85,7 +85,7 @@ bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) { fill_rect(wnd, rect, colours[UI_ELEM_BUTTON].fill); draw_rect(wnd, rect, colours[UI_ELEM_BUTTON].border); - if (wnd != ctx->wnd) { + if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) { return false; } @@ -115,24 +115,26 @@ rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) { ui_elem_t elem = (ui_elem_t){ .id = (ctx->count)++, .rect = rect, - .type = UI_ELEM_BUTTON, + .type = UI_ELEM_NODE, }; - fill_rect(wnd, rect, colours[UI_ELEM_BUTTON].fill); - draw_rect(wnd, rect, colours[UI_ELEM_BUTTON].border); + fill_rect(wnd, rect, colours[UI_ELEM_NODE].fill); + draw_rect(wnd, rect, colours[UI_ELEM_NODE].border); - if (wnd != ctx->wnd) { + if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) { return rect; } if (ctx->mouse_up) { ctx->hovered = ctx->active = -1; + ctx->rel_x = ctx->rel_y = 0; + return rect; } if (ctx->hovered == elem.id && ctx->active == elem.id) { return (rect_t){ - .topleft.x = rect.topleft.x + ctx->rel_x, - .topleft.y = rect.topleft.y + ctx->rel_y, + .topleft.x = ctx->mouse_x + ctx->rel_x, + .topleft.y = ctx->mouse_y + ctx->rel_y, .w = rect.w, .h = rect.h, }; @@ -146,6 +148,8 @@ rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) { if (ctx->mouse_down) { ctx->active = elem.id; + ctx->rel_x = rect.topleft.x - ctx->mouse_x; + ctx->rel_y = rect.topleft.y - ctx->mouse_y; } return rect;