From 050bb355d05b6fff91cdcf233f2f90821408afb2 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 25 Feb 2024 00:46:02 +0000 Subject: [PATCH] Reset noodle to default position if not connected to another node --- include/ui.h | 2 ++ src/ui.c | 45 ++++++++++++++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/include/ui.h b/include/ui.h index ba6be45..b537e0d 100644 --- a/include/ui.h +++ b/include/ui.h @@ -20,6 +20,7 @@ typedef struct ui_ctx ui_ctx; enum ui_elem_type { UI_ELEM_NODE, + UI_ELEM_NOODLE, UI_ELEM_BUTTON, COUNT_UI_ELEM, @@ -55,6 +56,7 @@ struct ui_ctx { bool mouse_down; bool mouse_up; const window *wnd; + ui_elem elements[MAX_UI_ELEMENTS]; }; void init_ui_ctx(ui_ctx *ctx); diff --git a/src/ui.c b/src/ui.c index 9b6b500..9390f65 100644 --- a/src/ui.c +++ b/src/ui.c @@ -69,6 +69,11 @@ bool ui_button(const window *wnd, ui_ctx *ctx, rect rec, } u64 id = (ctx->count)++; + ctx->elements[id] = (ui_elem){ + .id = id, + .rect = rec, + .type = UI_ELEM_BUTTON, + }; fill_rect(wnd, rec, colours.fill); draw_rect(wnd, rec, colours.border); @@ -102,6 +107,11 @@ ui_node_elem ui_node(const window *wnd, ui_ctx *ctx, ui_node_elem node, } u64 id = (ctx->count)++; + ctx->elements[id] = (ui_elem){ + .id = id, + .rect = node.rec, + .type = UI_ELEM_NODE, + }; line ln = {0}; f64 angle = 90.0; @@ -109,29 +119,29 @@ ui_node_elem ui_node(const window *wnd, ui_ctx *ctx, ui_node_elem node, i64 delta_multiplier = node.inputs % 2 == 0 ? -1 : 0; for (u64 i = 0; i < node.inputs; ++i) { + f64 new_angle = angle + angle_delta * delta_multiplier; + if (node.noodles[i].p0.x == node.noodles[i].p1.x && node.noodles[i].p0.y == node.noodles[i].p1.y) { point origin = {node.rec.topleft.x + node.rec.w / 2, node.rec.topleft.y + node.rec.h / 2}; - f64 new_angle = angle + angle_delta * delta_multiplier; - ln = line_from_origin(origin, new_angle, DEFAULT_NOODLE_LENGTH); - - if (delta_multiplier > 0) { - angle = new_angle; - } - - if (delta_multiplier == 0) { - delta_multiplier = -1; - } else { - delta_multiplier *= -1; - } } else { ln = node.noodles[i]; } node.noodles[i] = ui_noodle(wnd, ctx, ln, colours, node.rec); + + if (delta_multiplier > 0) { + angle = new_angle; + } + + if (delta_multiplier == 0) { + delta_multiplier = -1; + } else { + delta_multiplier *= -1; + } } fill_rect(wnd, node.rec, colours.fill); @@ -181,6 +191,11 @@ internal line ui_noodle(const window *wnd, ui_ctx *ctx, line ln, } u64 id = (ctx->count)++; + ctx->elements[id] = (ui_elem){ + .id = id, + .rect = (rect){0}, + .type = UI_ELEM_NOODLE, + }; bool horizontal = ln.p0.y == ln.p1.y; @@ -254,7 +269,11 @@ internal line ui_noodle(const window *wnd, ui_ctx *ctx, line ln, if (ctx->mouse_up) { ctx->hovered = ctx->active = -1; ctx->rel_x = ctx->rel_y = 0; - return ln; + return (line){ + .p0 = ln.p0, + .p1 = ln.p0, + }; + // return ln; } if (ctx->hovered == id && ctx->active == id) {