From 353409a5bf362342d362c4cb4e59645b280a27b5 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Tue, 16 Jan 2024 23:12:09 +0000 Subject: [PATCH 01/10] Start refactoring the UI code into an immediate mode style --- include/button.h | 16 -------- include/nodes.h | 11 ----- include/ui.h | 43 +++++++++++++++++++- include/window.h | 4 +- src/button.c | 10 ----- src/compositor.c | 84 +++++++++++++++++++++++++++++++++++--- src/nodes.c | 24 ----------- src/ui.c | 104 +++++++++++++++++++++++++++++++++++++++++++++-- src/window.c | 8 ++-- 9 files changed, 227 insertions(+), 77 deletions(-) delete mode 100644 include/button.h delete mode 100644 src/button.c diff --git a/include/button.h b/include/button.h deleted file mode 100644 index b654ab9..0000000 --- a/include/button.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef BUTTON_H -#define BUTTON_H - -#include "window.h" - -#define BUTTON_WIDTH 100 -#define BUTTON_HEIGHT 40 - -typedef struct button button_t; -struct button { - rect_t rect; -}; - -void draw_button(const window_t *wnd, const button_t *button); - -#endif // !BUTTON_H diff --git a/include/nodes.h b/include/nodes.h index 00a4685..e6c9bfe 100644 --- a/include/nodes.h +++ b/include/nodes.h @@ -6,14 +6,6 @@ #define MAX_NODES 1024 -#define NODE_WIDTH 70 -#define NODE_HEIGHT 20 - -#define IO_NODE_FILL_COLOUR ((colour_t){.abgr = 0xff2c84b7}) -#define IO_NODE_BORDER_COLOUR ((colour_t){.abgr = 0xff315c89}) -#define OP_NODE_FILL_COLOUR ((colour_t){.abgr = 0xffad6c3a}) -#define OP_NODE_BORDER_COLOUR ((colour_t){.abgr = 0xff8e4a33}) - typedef i32 (*node_func_t)(i32 a, i32 b); typedef enum node_type node_type_t; typedef union node_data node_data_t; @@ -32,11 +24,8 @@ union node_data { }; struct node { - rect_t rect; node_type_t type; node_data_t data; }; -void draw_node(const window_t *wnd, const node_t *node); - #endif // !NODES_H diff --git a/include/ui.h b/include/ui.h index 6403253..e184fd9 100644 --- a/include/ui.h +++ b/include/ui.h @@ -1,10 +1,51 @@ #ifndef UI_H #define UI_H +#include "SDL_events.h" #include "aliases/aliases.h" #include "window.h" #include -bool aabb(const rect_t *rect, i32 x, i32 y); +#define MAX_UI_ELEMENTS 4096 + +#define BUTTON_WIDTH 100 +#define BUTTON_HEIGHT 40 + +#define NODE_WIDTH 70 +#define NODE_HEIGHT 20 + +typedef enum ui_elem_type ui_elem_type_t; +typedef struct ui_elem ui_elem_t; +typedef struct ui_ctx ui_ctx_t; + +enum ui_elem_type { + UI_ELEM_NODE, + UI_ELEM_BUTTON, + + COUNT_UI_ELEM, +}; + +struct ui_elem { + u64 id; + rect_t rect; + ui_elem_type_t type; +}; + +struct ui_ctx { + u64 count; + i64 hovered; + i64 active; + i64 mouse_x; + i64 mouse_y; + bool mouse_down; + bool mouse_up; + const window_t *wnd; + ui_elem_t elements[MAX_UI_ELEMENTS]; +}; + +void reset_ui_ctx(ui_ctx_t *ctx); +void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx, + const SDL_Event *event); +bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect); #endif // !UI_H diff --git a/include/window.h b/include/window.h index 63ebb16..b1aadd1 100644 --- a/include/window.h +++ b/include/window.h @@ -63,7 +63,7 @@ void draw_point(const window_t *wnd, point_t p, colour_t colour); void draw_line(const window_t *wnd, const line_t *ln, colour_t colour); void draw_triangle(const window_t *wnd, const triangle_t *triangle, colour_t colour); -void draw_rect(const window_t *wnd, const rect_t *rect, colour_t colour); -void fill_rect(const window_t *wnd, const rect_t *rect, colour_t colour); +void draw_rect(const window_t *wnd, rect_t rect, colour_t colour); +void fill_rect(const window_t *wnd, rect_t rect, colour_t colour); #endif // !WINDOW_H diff --git a/src/button.c b/src/button.c deleted file mode 100644 index fea3c07..0000000 --- a/src/button.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "button.h" -#include "window.h" - -#define BUTTON_FILL_COLOUR ((colour_t){.abgr = 0xff89a83c}) -#define BUTTON_BORDER_COLOUR ((colour_t){.abgr = 0xff768432}) - -void draw_button(const window_t *wnd, const button_t *button) { - fill_rect(wnd, &(button->rect), BUTTON_FILL_COLOUR); - draw_rect(wnd, &(button->rect), BUTTON_BORDER_COLOUR); -} diff --git a/src/compositor.c b/src/compositor.c index c508bf4..ac9104f 100644 --- a/src/compositor.c +++ b/src/compositor.c @@ -1,5 +1,4 @@ #include "aliases/aliases.h" -#include "button.h" #include "nodes.h" #include "ops.h" #include "ui.h" @@ -9,6 +8,7 @@ #include #include #include +#include #include #define MAX_WINDOWS 2 @@ -29,9 +29,7 @@ struct compositor { u64 count; node_t *nodes; bool move_node; - button_t buttons[COUNT_COMP_OPS]; - i64 button_hovered; - i64 button_clicked; + ui_ctx_t ctx; }; void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x, @@ -65,6 +63,76 @@ i32 run_main_loop(void) { colour_t bg_colour = {.abgr = 0xffffffff}; + while (comp.running) { + while (SDL_PollEvent(&(comp.event))) { + for (u64 i = 0; i < MAX_WINDOWS; ++i) { + handle_ui_events(&(comp.windows[i]), &(comp.ctx), &(comp.event)); + } + + switch (comp.event.type) { + case SDL_QUIT: + comp.running = false; + break; + case SDL_WINDOWEVENT: + switch (comp.event.window.event) { + case SDL_WINDOWEVENT_CLOSE: + comp.running = false; + break; + case SDL_WINDOWEVENT_ENTER: { + u32 id = comp.event.window.windowID; + window_t *wnd = NULL; + + for (u64 i = 0; i < MAX_WINDOWS; ++i) { + window_t *window = &(comp.windows[i]); + + if (id == window->id) { + wnd = window; + break; + } + } + + if (!wnd) { + break; + } + + SDL_RaiseWindow(wnd->window); + + break; + } + } + + break; + } + } + + for (u64 i = 0; i < MAX_WINDOWS; ++i) { + clear_window(&(comp.windows[i]), bg_colour); + } + + if (button(main_window, &(comp.ctx), + (rect_t){.topleft.x = 10, + .topleft.y = 10, + .w = BUTTON_WIDTH, + .h = BUTTON_HEIGHT})) { + printf("Button 1 pressed\n"); + } + + if (button(main_window, &(comp.ctx), + (rect_t){.topleft.x = 50, + .topleft.y = 50, + .w = BUTTON_WIDTH, + .h = BUTTON_HEIGHT})) { + printf("Button 2 pressed\n"); + } + + for (u64 i = 0; i < MAX_WINDOWS; ++i) { + swap_buffers(&(comp.windows[i])); + } + + reset_ui_ctx(&(comp.ctx)); + } + +#if 0 i32 button_x = (toolbox->width - BUTTON_WIDTH) / 2; for (u64 i = 0; i < COUNT_COMP_OPS; ++i) { comp.buttons[i] = (button_t){.rect = (rect_t){ @@ -212,15 +280,18 @@ i32 run_main_loop(void) { swap_buffers(&(comp.windows[i])); } } +#endif - cleanup_window(toolbox); - cleanup_window(main_window); + for (u64 i = 0; i < MAX_WINDOWS; ++i) { + cleanup_window(&(comp.windows[i])); + } SDL_Quit(); return EXIT_SUCCESS; } +#if 0 void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x, i32 y) { if (comp->count + 1 >= MAX_NODES) { @@ -239,3 +310,4 @@ void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x, .data.path = data.path, }; } +#endif diff --git a/src/nodes.c b/src/nodes.c index 0e708b5..7b3179b 100644 --- a/src/nodes.c +++ b/src/nodes.c @@ -2,27 +2,3 @@ #include "aliases/aliases.h" #include "window.h" #include - -typedef struct node_colours node_colours_t; -struct node_colours { - colour_t fill; - colour_t border; -}; - -INTERNAL node_colours_t colours[COUNT_NODE_TYPES] = { - [NODE_TYPE_IO] = - (node_colours_t){ - .fill = IO_NODE_FILL_COLOUR, - .border = IO_NODE_BORDER_COLOUR, - }, - [NODE_TYPE_OP] = - (node_colours_t){ - .fill = OP_NODE_FILL_COLOUR, - .border = OP_NODE_BORDER_COLOUR, - }, -}; - -void draw_node(const window_t *wnd, const node_t *node) { - fill_rect(wnd, &(node->rect), colours[node->type].fill); - draw_rect(wnd, &(node->rect), colours[node->type].border); -} diff --git a/src/ui.c b/src/ui.c index a5a71ba..b915d31 100644 --- a/src/ui.c +++ b/src/ui.c @@ -1,6 +1,104 @@ #include "ui.h" +#include "SDL_events.h" +#include "aliases/aliases.h" +#include "window.h" +#include -bool aabb(const rect_t *rect, i32 x, i32 y) { - return x > rect->topleft.x && x <= rect->topleft.x + rect->w && - y > rect->topleft.y && y <= rect->topleft.y + rect->h; +typedef struct ui_elem_colours ui_elem_colours_t; +struct ui_elem_colours { + colour_t fill; + colour_t border; +}; + +INTERNAL ui_elem_colours_t colours[COUNT_UI_ELEM] = { + [UI_ELEM_NODE] = + (ui_elem_colours_t){ + .fill = (colour_t){.abgr = 0xff2c84b7}, + .border = (colour_t){.abgr = 0xff315c89}, + }, + [UI_ELEM_BUTTON] = + (ui_elem_colours_t){ + .fill = (colour_t){.abgr = 0xff89a83c}, + .border = (colour_t){.abgr = 0xff768432}, + }, +}; + +bool aabb(const ui_elem_t *elem, i32 x, i32 y) { + return x > elem->rect.topleft.x && x <= elem->rect.topleft.x + elem->rect.w && + y > elem->rect.topleft.y && y <= elem->rect.topleft.y + elem->rect.h; +} + +void reset_ui_ctx(ui_ctx_t *ctx) { + ctx->count = 0; + ctx->mouse_down = false; + ctx->mouse_up = false; +} + +void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx, + const SDL_Event *event) { + switch (event->type) { + case SDL_MOUSEMOTION: + if (wnd->id == event->motion.windowID) { + ctx->mouse_x = event->motion.x; + ctx->mouse_y = event->motion.y; + ctx->wnd = wnd; + + break; + } + + case SDL_MOUSEBUTTONDOWN: + if (wnd->id == event->button.windowID) { + ctx->mouse_x = event->button.x; + ctx->mouse_y = event->button.y; + ctx->mouse_down = true; + ctx->wnd = wnd; + } + + break; + case SDL_MOUSEBUTTONUP: + if (wnd->id == event->button.windowID) { + ctx->mouse_x = event->button.x; + ctx->mouse_y = event->button.y; + ctx->mouse_up = true; + ctx->wnd = wnd; + } + + break; + } +} + +bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) { + if (ctx->count + 1 >= MAX_UI_ELEMENTS) { + return false; + } + + ui_elem_t elem = (ui_elem_t){ + .id = (ctx->count)++, + .rect = rect, + .type = UI_ELEM_BUTTON, + }; + + fill_rect(wnd, rect, colours[UI_ELEM_BUTTON].fill); + draw_rect(wnd, rect, colours[UI_ELEM_BUTTON].border); + + if (wnd != ctx->wnd) { + return false; + } + + if (!aabb(&elem, ctx->mouse_x, ctx->mouse_y)) { + return false; + } + + ctx->hovered = elem.id; + + if (ctx->mouse_down) { + ctx->active = elem.id; + } + + if (ctx->mouse_up && ctx->hovered == elem.id && ctx->active == elem.id) { + ctx->hovered = ctx->active = -1; + return true; + } + + return false; } diff --git a/src/window.c b/src/window.c index 5602fb0..de0c873 100644 --- a/src/window.c +++ b/src/window.c @@ -90,18 +90,18 @@ void draw_triangle(const window_t *wnd, const triangle_t *triangle, draw_line(wnd, &ln2, colour); } -void draw_rect(const window_t *wnd, const rect_t *rect, colour_t colour) { +void draw_rect(const window_t *wnd, rect_t rect, colour_t colour) { set_colour(wnd, colour); - SDL_Rect dst = {rect->topleft.x, rect->topleft.y, rect->w, rect->h}; + SDL_Rect dst = {rect.topleft.x, rect.topleft.y, rect.w, rect.h}; SDL_RenderDrawRect(wnd->renderer, &dst); } -void fill_rect(const window_t *wnd, const rect_t *rect, colour_t colour) { +void fill_rect(const window_t *wnd, rect_t rect, colour_t colour) { set_colour(wnd, colour); - SDL_Rect dst = {rect->topleft.x, rect->topleft.y, rect->w, rect->h}; + SDL_Rect dst = {rect.topleft.x, rect.topleft.y, rect.w, rect.h}; SDL_RenderFillRect(wnd->renderer, &dst); } -- 2.39.5 From fb1af033081e0120135d14ec8861392eab0f90f7 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Wed, 17 Jan 2024 22:37:27 +0000 Subject: [PATCH 02/10] Start testing adding an immediate-mode node element --- include/ui.h | 3 +++ src/compositor.c | 11 +++++++++++ src/ui.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/include/ui.h b/include/ui.h index e184fd9..82a629d 100644 --- a/include/ui.h +++ b/include/ui.h @@ -37,6 +37,8 @@ struct ui_ctx { i64 active; i64 mouse_x; i64 mouse_y; + i64 rel_x; + i64 rel_y; bool mouse_down; bool mouse_up; const window_t *wnd; @@ -47,5 +49,6 @@ void reset_ui_ctx(ui_ctx_t *ctx); void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx, const SDL_Event *event); bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect); +rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect); #endif // !UI_H diff --git a/src/compositor.c b/src/compositor.c index ac9104f..1c1f7c1 100644 --- a/src/compositor.c +++ b/src/compositor.c @@ -27,6 +27,7 @@ struct compositor { u64 last_clicked_mouse_y; i64 node_hovered; u64 count; + rect_t *rects; node_t *nodes; bool move_node; ui_ctx_t ctx; @@ -41,6 +42,7 @@ i32 run_main_loop(void) { } compositor_t comp = {0}; + comp.rects = (rect_t *)malloc(sizeof(rect_t) * MAX_NODES); comp.nodes = (node_t *)malloc(sizeof(node_t) * MAX_NODES); window_t *main_window = &(comp.windows[0]); @@ -63,6 +65,13 @@ i32 run_main_loop(void) { colour_t bg_colour = {.abgr = 0xffffffff}; + comp.rects[0] = (rect_t){ + .topleft.x = 150, + .topleft.y = 50, + .w = NODE_WIDTH, + .h = NODE_HEIGHT, + }; + while (comp.running) { while (SDL_PollEvent(&(comp.event))) { for (u64 i = 0; i < MAX_WINDOWS; ++i) { @@ -125,6 +134,8 @@ i32 run_main_loop(void) { printf("Button 2 pressed\n"); } + comp.rects[0] = node(main_window, &(comp.ctx), comp.rects[0]); + for (u64 i = 0; i < MAX_WINDOWS; ++i) { swap_buffers(&(comp.windows[i])); } diff --git a/src/ui.c b/src/ui.c index b915d31..aa21aca 100644 --- a/src/ui.c +++ b/src/ui.c @@ -32,12 +32,17 @@ 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; @@ -45,7 +50,6 @@ void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx, break; } - case SDL_MOUSEBUTTONDOWN: if (wnd->id == event->button.windowID) { ctx->mouse_x = event->button.x; @@ -102,3 +106,47 @@ bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) { return false; } + +rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) { + if (ctx->count + 1 >= MAX_UI_ELEMENTS) { + return (rect_t){0}; + } + + ui_elem_t elem = (ui_elem_t){ + .id = (ctx->count)++, + .rect = rect, + .type = UI_ELEM_BUTTON, + }; + + fill_rect(wnd, rect, colours[UI_ELEM_BUTTON].fill); + draw_rect(wnd, rect, colours[UI_ELEM_BUTTON].border); + + if (wnd != ctx->wnd) { + return rect; + } + + if (ctx->mouse_up) { + ctx->hovered = ctx->active = -1; + } + + 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, + .w = rect.w, + .h = rect.h, + }; + } + + if (!aabb(&elem, ctx->mouse_x, ctx->mouse_y)) { + return rect; + } + + ctx->hovered = elem.id; + + if (ctx->mouse_down) { + ctx->active = elem.id; + } + + return rect; +} -- 2.39.5 From c5b263e94812baea81d6bcfe3f1f0fc81b292631 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Wed, 17 Jan 2024 23:28:31 +0000 Subject: [PATCH 03/10] 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; -- 2.39.5 From 718e7eec2158172e2754519f731a9e410e7e4d07 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Wed, 17 Jan 2024 23:28:53 +0000 Subject: [PATCH 04/10] Initialise UI context --- src/compositor.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/compositor.c b/src/compositor.c index 1c1f7c1..33cefa0 100644 --- a/src/compositor.c +++ b/src/compositor.c @@ -42,6 +42,9 @@ i32 run_main_loop(void) { } compositor_t comp = {0}; + + init_ui_ctx(&(comp.ctx)); + comp.rects = (rect_t *)malloc(sizeof(rect_t) * MAX_NODES); comp.nodes = (node_t *)malloc(sizeof(node_t) * MAX_NODES); -- 2.39.5 From aac10f24d1d62d98cd620ae83930a4ba08a1b2a1 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Wed, 17 Jan 2024 23:40:02 +0000 Subject: [PATCH 05/10] Only handle events from the active window --- src/compositor.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/compositor.c b/src/compositor.c index 33cefa0..5dc258f 100644 --- a/src/compositor.c +++ b/src/compositor.c @@ -19,6 +19,7 @@ typedef struct compositor compositor_t; struct compositor { window_t windows[MAX_WINDOWS]; + u32 active_window; SDL_Event event; bool running; u64 mouse_x; @@ -77,9 +78,8 @@ i32 run_main_loop(void) { while (comp.running) { while (SDL_PollEvent(&(comp.event))) { - for (u64 i = 0; i < MAX_WINDOWS; ++i) { - handle_ui_events(&(comp.windows[i]), &(comp.ctx), &(comp.event)); - } + handle_ui_events(&(comp.windows[comp.active_window - 1]), &(comp.ctx), + &(comp.event)); switch (comp.event.type) { case SDL_QUIT: @@ -98,6 +98,7 @@ i32 run_main_loop(void) { window_t *window = &(comp.windows[i]); if (id == window->id) { + comp.active_window = id; wnd = window; break; } -- 2.39.5 From f45fb04dff757e95916fb1ab8ef4399390c62481 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Wed, 17 Jan 2024 23:41:25 +0000 Subject: [PATCH 06/10] Remove testing code --- src/compositor.c | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/src/compositor.c b/src/compositor.c index 5dc258f..05c85f4 100644 --- a/src/compositor.c +++ b/src/compositor.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #define MAX_WINDOWS 2 @@ -28,7 +27,6 @@ struct compositor { u64 last_clicked_mouse_y; i64 node_hovered; u64 count; - rect_t *rects; node_t *nodes; bool move_node; ui_ctx_t ctx; @@ -46,7 +44,6 @@ i32 run_main_loop(void) { init_ui_ctx(&(comp.ctx)); - comp.rects = (rect_t *)malloc(sizeof(rect_t) * MAX_NODES); comp.nodes = (node_t *)malloc(sizeof(node_t) * MAX_NODES); window_t *main_window = &(comp.windows[0]); @@ -69,13 +66,6 @@ i32 run_main_loop(void) { colour_t bg_colour = {.abgr = 0xffffffff}; - comp.rects[0] = (rect_t){ - .topleft.x = 150, - .topleft.y = 50, - .w = NODE_WIDTH, - .h = NODE_HEIGHT, - }; - while (comp.running) { while (SDL_PollEvent(&(comp.event))) { handle_ui_events(&(comp.windows[comp.active_window - 1]), &(comp.ctx), @@ -122,24 +112,6 @@ i32 run_main_loop(void) { clear_window(&(comp.windows[i]), bg_colour); } - if (button(main_window, &(comp.ctx), - (rect_t){.topleft.x = 10, - .topleft.y = 10, - .w = BUTTON_WIDTH, - .h = BUTTON_HEIGHT})) { - printf("Button 1 pressed\n"); - } - - if (button(main_window, &(comp.ctx), - (rect_t){.topleft.x = 50, - .topleft.y = 50, - .w = BUTTON_WIDTH, - .h = BUTTON_HEIGHT})) { - printf("Button 2 pressed\n"); - } - - comp.rects[0] = node(main_window, &(comp.ctx), comp.rects[0]); - for (u64 i = 0; i < MAX_WINDOWS; ++i) { swap_buffers(&(comp.windows[i])); } -- 2.39.5 From 10149fbdbf4cf6285a6dab39bf50c863a3888456 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sat, 20 Jan 2024 22:17:13 +0000 Subject: [PATCH 07/10] Remove predefined colours and allow user to pass colour as param --- include/ui.h | 12 ++++++++++-- src/ui.c | 33 ++++++++------------------------- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/include/ui.h b/include/ui.h index 26883d0..7acbd55 100644 --- a/include/ui.h +++ b/include/ui.h @@ -31,6 +31,12 @@ struct ui_elem { ui_elem_type_t type; }; +typedef struct ui_elem_colours ui_elem_colours_t; +struct ui_elem_colours { + colour_t fill; + colour_t border; +}; + struct ui_ctx { u64 count; i64 hovered; @@ -49,7 +55,9 @@ 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); -bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect); -rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect); +bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect, + ui_elem_colours_t colours); +rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect, + ui_elem_colours_t colours); #endif // !UI_H diff --git a/src/ui.c b/src/ui.c index 4061c17..0953838 100644 --- a/src/ui.c +++ b/src/ui.c @@ -3,25 +3,6 @@ #include "aliases/aliases.h" #include "window.h" -typedef struct ui_elem_colours ui_elem_colours_t; -struct ui_elem_colours { - colour_t fill; - colour_t border; -}; - -INTERNAL ui_elem_colours_t colours[COUNT_UI_ELEM] = { - [UI_ELEM_NODE] = - (ui_elem_colours_t){ - .fill = (colour_t){.abgr = 0xff2c84b7}, - .border = (colour_t){.abgr = 0xff315c89}, - }, - [UI_ELEM_BUTTON] = - (ui_elem_colours_t){ - .fill = (colour_t){.abgr = 0xff89a83c}, - .border = (colour_t){.abgr = 0xff768432}, - }, -}; - bool aabb(const ui_elem_t *elem, i32 x, i32 y) { return x > elem->rect.topleft.x && x <= elem->rect.topleft.x + elem->rect.w && y > elem->rect.topleft.y && y <= elem->rect.topleft.y + elem->rect.h; @@ -71,7 +52,8 @@ void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx, } } -bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) { +bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect, + ui_elem_colours_t colours) { if (ctx->count + 1 >= MAX_UI_ELEMENTS) { return false; } @@ -82,8 +64,8 @@ bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) { .type = UI_ELEM_BUTTON, }; - fill_rect(wnd, rect, colours[UI_ELEM_BUTTON].fill); - draw_rect(wnd, rect, colours[UI_ELEM_BUTTON].border); + fill_rect(wnd, rect, colours.fill); + draw_rect(wnd, rect, colours.border); if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) { return false; @@ -107,7 +89,8 @@ bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) { return false; } -rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) { +rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect, + ui_elem_colours_t colours) { if (ctx->count + 1 >= MAX_UI_ELEMENTS) { return (rect_t){0}; } @@ -118,8 +101,8 @@ rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) { .type = UI_ELEM_NODE, }; - fill_rect(wnd, rect, colours[UI_ELEM_NODE].fill); - draw_rect(wnd, rect, colours[UI_ELEM_NODE].border); + fill_rect(wnd, rect, colours.fill); + draw_rect(wnd, rect, colours.border); if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) { return rect; -- 2.39.5 From 79b6bc35f0cc1dddda5ed57a994ed06beeb7e0d5 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sat, 20 Jan 2024 22:17:39 +0000 Subject: [PATCH 08/10] Delete nodes.c --- src/nodes.c | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 src/nodes.c diff --git a/src/nodes.c b/src/nodes.c deleted file mode 100644 index 7b3179b..0000000 --- a/src/nodes.c +++ /dev/null @@ -1,4 +0,0 @@ -#include "nodes.h" -#include "aliases/aliases.h" -#include "window.h" -#include -- 2.39.5 From 93b79aa643c5babbc9ceec183d82fdeb8facf691 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sat, 20 Jan 2024 22:17:54 +0000 Subject: [PATCH 09/10] Add rect and colours to node struct --- include/nodes.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/nodes.h b/include/nodes.h index e6c9bfe..b07a592 100644 --- a/include/nodes.h +++ b/include/nodes.h @@ -2,6 +2,7 @@ #define NODES_H #include "aliases/aliases.h" +#include "ui.h" #include "window.h" #define MAX_NODES 1024 @@ -24,6 +25,8 @@ union node_data { }; struct node { + rect_t rect; + ui_elem_colours_t colours; node_type_t type; node_data_t data; }; -- 2.39.5 From e71c144db635b25f31c86172fa899c4d4776c504 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sat, 20 Jan 2024 22:18:40 +0000 Subject: [PATCH 10/10] Finalise implementing compositor behaviour using immediate mode UI --- src/compositor.c | 205 +++++++++++------------------------------------ 1 file changed, 47 insertions(+), 158 deletions(-) diff --git a/src/compositor.c b/src/compositor.c index 05c85f4..8324c2d 100644 --- a/src/compositor.c +++ b/src/compositor.c @@ -21,10 +21,6 @@ struct compositor { u32 active_window; SDL_Event event; bool running; - u64 mouse_x; - u64 mouse_y; - u64 last_clicked_mouse_x; - u64 last_clicked_mouse_y; i64 node_hovered; u64 count; node_t *nodes; @@ -33,7 +29,7 @@ struct compositor { }; void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x, - i32 y); + i32 y, ui_elem_colours_t colours); i32 run_main_loop(void) { if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { @@ -65,6 +61,20 @@ i32 run_main_loop(void) { SDL_EventState(SDL_DROPFILE, SDL_ENABLE); colour_t bg_colour = {.abgr = 0xffffffff}; + ui_elem_colours_t button_colours = (ui_elem_colours_t){ + .fill = (colour_t){.abgr = 0xff89a83c}, + .border = (colour_t){.abgr = 0xff768432}, + }; + ui_elem_colours_t io_node_colours = (ui_elem_colours_t){ + .fill = (colour_t){.abgr = 0xff2c84b7}, + .border = (colour_t){.abgr = 0xff315c89}, + }; + ui_elem_colours_t op_node_colours = (ui_elem_colours_t){ + .fill = (colour_t){.abgr = 0xffad6c3a}, + .border = (colour_t){.abgr = 0xff8e4a33}, + }; + + i32 toolbox_button_x = (toolbox->width - BUTTON_WIDTH) / 2; while (comp.running) { while (SDL_PollEvent(&(comp.event))) { @@ -105,6 +115,15 @@ i32 run_main_loop(void) { } break; + case SDL_DROPFILE: + if (comp.event.drop.windowID == main_window->id) { + node_data_t data = (node_data_t){.path = comp.event.drop.file}; + + add_node(&comp, NODE_TYPE_IO, data, comp.ctx.mouse_x, + comp.ctx.mouse_y, io_node_colours); + + break; + } } } @@ -112,6 +131,27 @@ i32 run_main_loop(void) { clear_window(&(comp.windows[i]), bg_colour); } + for (u64 i = 0; i < COUNT_COMP_OPS; ++i) { + rect_t rect = { + .topleft.x = toolbox_button_x, + .topleft.y = i * (BUTTON_HEIGHT + 20) + 30, + .w = BUTTON_WIDTH, + .h = BUTTON_HEIGHT, + }; + + if (button(toolbox, &(comp.ctx), rect, button_colours)) { + node_data_t data = (node_data_t){.func = ops[i]}; + + add_node(&comp, NODE_TYPE_OP, data, comp.ctx.mouse_x, comp.ctx.mouse_y, + op_node_colours); + } + } + + for (u64 i = 0; i < comp.count; ++i) { + comp.nodes[i].rect = node(main_window, &(comp.ctx), comp.nodes[i].rect, + comp.nodes[i].colours); + } + for (u64 i = 0; i < MAX_WINDOWS; ++i) { swap_buffers(&(comp.windows[i])); } @@ -119,156 +159,6 @@ i32 run_main_loop(void) { reset_ui_ctx(&(comp.ctx)); } -#if 0 - i32 button_x = (toolbox->width - BUTTON_WIDTH) / 2; - for (u64 i = 0; i < COUNT_COMP_OPS; ++i) { - comp.buttons[i] = (button_t){.rect = (rect_t){ - .topleft.x = button_x, - .topleft.y = i * (BUTTON_HEIGHT + 20) + 30, - .w = BUTTON_WIDTH, - .h = BUTTON_HEIGHT, - }}; - } - - while (comp.running) { - while (SDL_PollEvent(&(comp.event))) { - switch (comp.event.type) { - case SDL_QUIT: - comp.running = false; - break; - case SDL_WINDOWEVENT: - switch (comp.event.window.event) { - case SDL_WINDOWEVENT_CLOSE: - comp.running = false; - break; - case SDL_WINDOWEVENT_ENTER: { - u32 id = comp.event.window.windowID; - window_t *wnd = NULL; - - for (u64 i = 0; i < MAX_WINDOWS; ++i) { - window_t *window = &(comp.windows[i]); - - if (id == window->id) { - wnd = window; - break; - } - } - - if (!wnd) { - break; - } - - SDL_RaiseWindow(wnd->window); - - break; - } - } - - break; - case SDL_MOUSEBUTTONDOWN: - if (comp.event.button.windowID == main_window->id) { - if (comp.node_hovered != -1) { - comp.move_node = true; - } - - comp.last_clicked_mouse_x = comp.event.button.x; - comp.last_clicked_mouse_y = comp.event.button.y; - - break; - } else if (comp.event.button.windowID == toolbox->id) { - if (comp.button_hovered != -1) { - comp.button_clicked = comp.button_hovered; - } - - break; - } - case SDL_MOUSEBUTTONUP: - comp.move_node = false; - - if (comp.event.button.windowID == toolbox->id) { - if (comp.button_hovered >= 0 && - comp.button_hovered == comp.button_clicked) { - add_node(&comp, NODE_TYPE_OP, - (node_data_t){.func = ops[comp.button_hovered]}, - comp.last_clicked_mouse_x, comp.last_clicked_mouse_y); - } - - comp.button_clicked = -1; - } - - break; - case SDL_MOUSEMOTION: - comp.button_hovered = -1; - - if (comp.event.motion.windowID == main_window->id) { - comp.mouse_x = comp.event.motion.x; - comp.mouse_y = comp.event.motion.y; - - if (comp.move_node) { - i32 dx = comp.event.motion.xrel; - i32 dy = comp.event.motion.yrel; - - node_t *node = &(comp.nodes[comp.node_hovered]); - - node->rect.topleft.x += dx; - node->rect.topleft.y += dy; - } else { - comp.node_hovered = -1; - - for (u64 i = comp.count - 1; i >= 0; --i) { - rect_t *rect = &(comp.nodes[i].rect); - - if (aabb(rect, comp.mouse_x, comp.mouse_y)) { - comp.node_hovered = i; - break; - } - } - } - - break; - } else if (comp.event.motion.windowID == toolbox->id) { - for (u64 i = COUNT_COMP_OPS - 1; i >= 0; --i) { - rect_t *rect = &(comp.buttons[i].rect); - - if (aabb(rect, comp.event.motion.x, comp.event.motion.y)) { - comp.button_hovered = i; - break; - } - } - - break; - } - case SDL_DROPFILE: - if (comp.event.drop.windowID == main_window->id) { - node_data_t data = (node_data_t){.path = comp.event.drop.file}; - - add_node(&comp, NODE_TYPE_IO, data, comp.mouse_x, comp.mouse_y); - - break; - } - } - } - - for (u64 i = 0; i < MAX_WINDOWS; ++i) { - clear_window(&(comp.windows[i]), bg_colour); - } - - for (u64 i = 0; i < comp.count; ++i) { - node_t *node = &(comp.nodes[i]); - draw_node(main_window, node); - } - - for (u64 i = 0; i < COUNT_COMP_OPS; ++i) { - button_t *button = &(comp.buttons[i]); - draw_button(toolbox, button); - } - - for (u64 i = 0; i < MAX_WINDOWS; ++i) { - swap_buffers(&(comp.windows[i])); - } - } -#endif - for (u64 i = 0; i < MAX_WINDOWS; ++i) { cleanup_window(&(comp.windows[i])); } @@ -278,9 +168,8 @@ i32 run_main_loop(void) { return EXIT_SUCCESS; } -#if 0 void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x, - i32 y) { + i32 y, ui_elem_colours_t colours) { if (comp->count + 1 >= MAX_NODES) { return; } @@ -293,8 +182,8 @@ void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x, .w = NODE_WIDTH, .h = NODE_HEIGHT, }, + .colours = colours, .type = type, .data.path = data.path, }; } -#endif -- 2.39.5