From 10ba3d642da3b35a84ee1f7d7934d9d656a03290 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Tue, 20 Feb 2024 00:18:53 +0000 Subject: [PATCH] Support drawing a node with connection that can be modified by user --- include/nodes.h | 3 +-- include/ui.h | 18 +++++++++++------- src/compositor.c | 20 ++++++++++++-------- src/ui.c | 44 +++++++++++++++++++++++++++++++------------- 4 files changed, 55 insertions(+), 30 deletions(-) diff --git a/include/nodes.h b/include/nodes.h index 349559c..6ac57f4 100644 --- a/include/nodes.h +++ b/include/nodes.h @@ -3,7 +3,6 @@ #include "aliases/aliases.h" #include "ui.h" -#include "window.h" #define MAX_NODES 1024 @@ -25,7 +24,7 @@ union node_data { }; struct node { - rect rec; + ui_node_elem node; ui_elem_colours colours; node_type type; node_data data; diff --git a/include/ui.h b/include/ui.h index 076fcf9..cd05490 100644 --- a/include/ui.h +++ b/include/ui.h @@ -14,11 +14,11 @@ #define NODE_WIDTH 70 #define NODE_HEIGHT 20 -typedef enum ui_elemype ui_elemype; +typedef enum ui_elem_type ui_elem_type; typedef struct ui_elem ui_elem; typedef struct ui_ctx ui_ctx; -enum ui_elemype { +enum ui_elem_type { UI_ELEM_NODE, UI_ELEM_BUTTON, @@ -28,7 +28,7 @@ enum ui_elemype { struct ui_elem { u64 id; rect rect; - ui_elemype type; + ui_elem_type type; }; typedef struct ui_elem_colours ui_elem_colours; @@ -37,6 +37,12 @@ struct ui_elem_colours { colour border; }; +typedef struct ui_node_elem ui_node_elem; +struct ui_node_elem { + line noodle; + rect rec; +}; + struct ui_ctx { u64 count; i64 hovered; @@ -55,9 +61,7 @@ void reset_ui_ctx(ui_ctx *ctx); void handle_ui_events(const window *wnd, ui_ctx *ctx, const SDL_Event *event); bool ui_button(const window *wnd, ui_ctx *ctx, rect rect, ui_elem_colours colours); -rect ui_node(const window *wnd, ui_ctx *ctx, rect rect, - ui_elem_colours colours); -line ui_noodle(const window *wnd, ui_ctx *ctx, line ln, - ui_elem_colours colours); +ui_node_elem ui_node(const window *wnd, ui_ctx *ctx, ui_node_elem node, + ui_elem_colours colours); #endif // !UI_H diff --git a/src/compositor.c b/src/compositor.c index 1294c27..1e83d36 100644 --- a/src/compositor.c +++ b/src/compositor.c @@ -148,8 +148,8 @@ i32 run_main_loop(void) { } for (u64 i = 0; i < comp.count; ++i) { - comp.nodes[i].rec = ui_node(main_window, &(comp.ctx), comp.nodes[i].rec, - comp.nodes[i].colours); + comp.nodes[i].node = ui_node(main_window, &(comp.ctx), comp.nodes[i].node, + comp.nodes[i].colours); } for (u64 i = 0; i < MAX_WINDOWS; ++i) { @@ -175,12 +175,16 @@ void add_node(compositor *comp, node_type type, node_data data, i32 x, i32 y, } comp->nodes[(comp->count)++] = (node){ - .rec = - (rect){ - .topleft.x = x, - .topleft.y = y, - .w = NODE_WIDTH, - .h = NODE_HEIGHT, + .node = + (ui_node_elem){ + .rec = + (rect){ + .topleft.x = x, + .topleft.y = y, + .w = NODE_WIDTH, + .h = NODE_HEIGHT, + }, + .noodle = (line){0}, }, .colours = colours, .type = type, diff --git a/src/ui.c b/src/ui.c index 4af99db..696ffdd 100644 --- a/src/ui.c +++ b/src/ui.c @@ -10,6 +10,8 @@ #define NOODLE_HALF_WIDTH 2 #define DEFAULT_NOODLE_LENGTH 60 +line ui_noodle(const window *wnd, ui_ctx *ctx, line ln, + ui_elem_colours colours); INTERNAL bool aabb(rect rec, i32 x, i32 y); INTERNAL line line_from_origin(point origin, f64 angle, i32 line_length); @@ -93,36 +95,52 @@ bool ui_button(const window *wnd, ui_ctx *ctx, rect rec, return false; } -rect ui_node(const window *wnd, ui_ctx *ctx, rect rec, - ui_elem_colours colours) { +ui_node_elem ui_node(const window *wnd, ui_ctx *ctx, ui_node_elem node, + ui_elem_colours colours) { if (ctx->count + 1 >= MAX_UI_ELEMENTS) { - return (rect){0}; + return (ui_node_elem){0}; } u64 id = (ctx->count)++; - fill_rect(wnd, rec, colours.fill); - draw_rect(wnd, rec, colours.border); + line ln; + if (node.noodle.p0.x == node.noodle.p1.x && + node.noodle.p0.y == node.noodle.p1.y) { + ln = line_from_origin((point){node.rec.topleft.x + node.rec.w / 2, + node.rec.topleft.y + node.rec.h / 2}, + 90.0, DEFAULT_NOODLE_LENGTH); + } else { + ln = node.noodle; + } + + node.noodle = ui_noodle(wnd, ctx, ln, colours); + + fill_rect(wnd, node.rec, colours.fill); + draw_rect(wnd, node.rec, colours.border); if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != id)) { - return rec; + return node; } if (ctx->mouse_up) { ctx->hovered = ctx->active = -1; ctx->rel_x = ctx->rel_y = 0; - return rec; + return node; } if (ctx->hovered == id && ctx->active == id) { - rec.topleft.x += ctx->rel_x; - rec.topleft.y += ctx->rel_y; + node.rec.topleft.x += ctx->rel_x; + node.rec.topleft.y += ctx->rel_y; + node.noodle.p0.x += ctx->rel_x; + node.noodle.p0.y += ctx->rel_y; + node.noodle.p1.x += ctx->rel_x; + node.noodle.p1.y += ctx->rel_y; - return rec; + return node; } - if (!aabb(rec, ctx->mouse_x, ctx->mouse_y)) { - return rec; + if (!aabb(node.rec, ctx->mouse_x, ctx->mouse_y)) { + return node; } ctx->hovered = id; @@ -131,7 +149,7 @@ rect ui_node(const window *wnd, ui_ctx *ctx, rect rec, ctx->active = id; } - return rec; + return node; } line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,