Support drawing a node with connection that can be modified by user

This commit is contained in:
2024-02-20 00:18:53 +00:00
parent d73275f04c
commit 10ba3d642d
4 changed files with 55 additions and 30 deletions

View File

@@ -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,

View File

@@ -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,