Add support for maintaining connected noodle when parent node moves

This commit is contained in:
Abdelrahman Said 2024-02-25 21:04:10 +00:00
parent f2faa56e5f
commit 8078a72deb
3 changed files with 30 additions and 20 deletions

View File

@ -7,11 +7,13 @@
#define MAX_NODES 1024
#define IO_INPUT_COUNT 0
#define OP_INPUT_COUNT 2
#define EMPTY_NODE 0
typedef i32 (*node_func)(i32 a, i32 b);
typedef enum node_type node_type;
typedef union node_data node_data;
typedef struct node node;
typedef struct noodle noodle;
enum node_type {
NODE_TYPE_IO,
@ -25,13 +27,18 @@ union node_data {
node_func func;
};
struct noodle {
line ln;
u64 connected_node;
};
struct node {
rect rec;
ui_elem_colours colours;
node_type type;
node_data data;
u64 inputs;
line *noodles;
noodle *noodles;
};
#endif // !NODES_H

View File

@ -7,8 +7,6 @@
#include <stdbool.h>
#define MAX_UI_ELEMENTS 8192
#define RESERVED_UI_SLOT 0
#define UI_ELEM_START_INDEX 1
#define BUTTON_WIDTH 100
#define BUTTON_HEIGHT 40

View File

@ -162,20 +162,20 @@ i32 run_main_loop(void) {
for (u64 j = 0; j < comp.nodes[i].inputs; ++j) {
f64 new_angle = angle + angle_delta * delta_multiplier;
line *ln = &(node_elem->noodles[j]);
noodle *ndl = &(node_elem->noodles[j]);
if (ln->p0.x == ln->p1.x && ln->p0.y == ln->p1.y) {
if (ndl->ln.p0.x == ndl->ln.p1.x && ndl->ln.p0.y == ndl->ln.p1.y) {
point origin = {node_elem->rec.topleft.x + node_elem->rec.w / 2,
node_elem->rec.topleft.y + node_elem->rec.h / 2};
*ln = line_from_origin(origin, new_angle, DEFAULT_NOODLE_LENGTH);
ndl->ln = line_from_origin(origin, new_angle, DEFAULT_NOODLE_LENGTH);
}
switch (ui_noodle(main_window, &(comp.ctx), *ln, node_elem->colours,
switch (ui_noodle(main_window, &(comp.ctx), ndl->ln, node_elem->colours,
node_elem->rec)) {
case NOODLE_ACTION_DRAGGING:
ln->p0.x += comp.ctx.rel_x;
ln->p0.y += comp.ctx.rel_y;
ndl->ln.p0.x += comp.ctx.rel_x;
ndl->ln.p0.y += comp.ctx.rel_y;
break;
case NOODLE_ACTION_RELEASED:
for (u64 k = 0; k < comp.count; ++k) {
@ -189,9 +189,11 @@ i32 run_main_loop(void) {
point p0 = {nd->rec.topleft.x + nd->rec.w / 2,
nd->rec.topleft.y + nd->rec.h / 2};
ln->p0 = p0;
ndl->ln.p0 = p0;
ndl->connected_node = k;
} else {
ln->p0 = ln->p1;
ndl->ln.p0 = ndl->ln.p1;
ndl->connected_node = EMPTY_NODE;
}
}
break;
@ -215,13 +217,16 @@ i32 run_main_loop(void) {
node_elem->rec.topleft.x += comp.ctx.rel_x;
node_elem->rec.topleft.y += comp.ctx.rel_y;
for (u64 j = 0; j < comp.nodes[i].inputs; ++j) {
line *ln = &(node_elem->noodles[j]);
for (u64 j = 0; j < node_elem->inputs; ++j) {
noodle *ndl = &(node_elem->noodles[j]);
ln->p0.x += comp.ctx.rel_x;
ln->p0.y += comp.ctx.rel_y;
ln->p1.x += comp.ctx.rel_x;
ln->p1.y += comp.ctx.rel_y;
if (ndl->connected_node == EMPTY_NODE) {
ndl->ln.p0.x += comp.ctx.rel_x;
ndl->ln.p0.y += comp.ctx.rel_y;
}
ndl->ln.p1.x += comp.ctx.rel_x;
ndl->ln.p1.y += comp.ctx.rel_y;
}
}
}
@ -250,8 +255,8 @@ void add_node(compositor *comp, node_type type, node_data data, u64 inputs,
return;
}
u64 alloc_size = inputs * sizeof(line);
line *noodles = mem_arena_alloc(comp->arena, alloc_size);
u64 alloc_size = inputs * sizeof(noodle);
noodle *noodles = mem_arena_alloc(comp->arena, alloc_size);
if (!noodles) {
return;
}
@ -263,7 +268,7 @@ void add_node(compositor *comp, node_type type, node_data data, u64 inputs,
.h = NODE_HEIGHT,
};
comp->nodes[(comp->count)++] = (node){
comp->nodes[++(comp->count)] = (node){
.rec = rec,
.colours = colours,
.type = type,