Reset noodle to default position if not connected to another node

This commit is contained in:
Abdelrahman Said 2024-02-25 00:46:02 +00:00
parent a86b025f7d
commit 050bb355d0
2 changed files with 34 additions and 13 deletions

View File

@ -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);

View File

@ -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) {