Add functionality to draw nodes with user-defined inputs count
This commit is contained in:
61
src/ui.c
61
src/ui.c
@@ -10,8 +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 line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||
ui_elem_colours colours, rect node);
|
||||
internal bool aabb(rect rec, i32 x, i32 y);
|
||||
internal line line_from_origin(point origin, f64 angle, i32 line_length);
|
||||
|
||||
@@ -103,17 +103,36 @@ ui_node_elem ui_node(const window *wnd, ui_ctx *ctx, ui_node_elem node,
|
||||
|
||||
u64 id = (ctx->count)++;
|
||||
|
||||
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;
|
||||
}
|
||||
line ln = {0};
|
||||
f64 angle = 90.0;
|
||||
f64 angle_delta = 25.0;
|
||||
i64 delta_multiplier = node.inputs % 2 == 0 ? -1 : 0;
|
||||
|
||||
node.noodle = ui_noodle(wnd, ctx, ln, colours);
|
||||
for (u64 i = 0; i < node.inputs; ++i) {
|
||||
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);
|
||||
}
|
||||
|
||||
fill_rect(wnd, node.rec, colours.fill);
|
||||
draw_rect(wnd, node.rec, colours.border);
|
||||
@@ -131,10 +150,13 @@ ui_node_elem ui_node(const window *wnd, ui_ctx *ctx, ui_node_elem node,
|
||||
if (ctx->hovered == id && ctx->active == id) {
|
||||
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;
|
||||
|
||||
for (u64 i = 0; i < node.inputs; ++i) {
|
||||
node.noodles[i].p0.x += ctx->rel_x;
|
||||
node.noodles[i].p0.y += ctx->rel_y;
|
||||
node.noodles[i].p1.x += ctx->rel_x;
|
||||
node.noodles[i].p1.y += ctx->rel_y;
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
@@ -152,8 +174,8 @@ ui_node_elem ui_node(const window *wnd, ui_ctx *ctx, ui_node_elem node,
|
||||
return node;
|
||||
}
|
||||
|
||||
line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||
ui_elem_colours colours) {
|
||||
internal line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||
ui_elem_colours colours, rect node) {
|
||||
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
|
||||
return (line){0};
|
||||
}
|
||||
@@ -242,7 +264,8 @@ line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||
return ln;
|
||||
}
|
||||
|
||||
if (!aabb(bounding_box, ctx->mouse_x, ctx->mouse_y)) {
|
||||
if (!aabb(bounding_box, ctx->mouse_x, ctx->mouse_y) ||
|
||||
aabb(node, ctx->mouse_x, ctx->mouse_y)) {
|
||||
return ln;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user