Move the node drawing code to a function

This commit is contained in:
Abdelrahman Said 2024-02-25 21:21:04 +00:00
parent f03dad33a2
commit 29bcfabda9

View File

@ -33,8 +33,9 @@ struct compositor {
ui_ctx ctx;
};
void add_node(compositor *comp, node_type type, node_data data, u64 inputs,
i32 x, i32 y, ui_elem_colours colours);
internal void add_node(compositor *comp, node_type type, node_data data,
u64 inputs, i32 x, i32 y, ui_elem_colours colours);
internal void draw_node_graph(compositor *comp, const window *wnd);
i32 run_main_loop(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
@ -153,102 +154,7 @@ i32 run_main_loop(void) {
}
}
for (u64 i = NODE_START; i <= comp.count; ++i) {
node *node_elem = &(comp.nodes[i]);
f64 angle = 90.0;
f64 angle_delta = 25.0;
i64 delta_multiplier = node_elem->inputs % 2 == 0 ? -1 : 0;
for (u64 j = 0; j < comp.nodes[i].inputs; ++j) {
f64 new_angle = angle + angle_delta * delta_multiplier;
noodle *ndl = &(node_elem->noodles[j]);
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};
ndl->ln = line_from_origin(origin, new_angle, DEFAULT_NOODLE_LENGTH);
}
switch (ui_noodle(main_window, &(comp.ctx), ndl->ln, node_elem->colours,
node_elem->rec)) {
case NOODLE_ACTION_DRAGGING:
ndl->ln.p0.x += comp.ctx.rel_x;
ndl->ln.p0.y += comp.ctx.rel_y;
break;
case NOODLE_ACTION_RELEASED:
for (u64 k = NODE_START; k <= comp.count; ++k) {
if (k == i) {
continue;
}
const node *nd = &(comp.nodes[k]);
if (aabb(nd->rec, comp.ctx.mouse_x, comp.ctx.mouse_y)) {
point p0 = {nd->rec.topleft.x + nd->rec.w / 2,
nd->rec.topleft.y + nd->rec.h / 2};
ndl->ln.p0 = p0;
ndl->connected_node = k;
} else {
ndl->ln.p0 = ndl->ln.p1;
ndl->connected_node = EMPTY_NODE;
}
}
break;
default:
break;
}
if (delta_multiplier > 0) {
angle = new_angle;
}
if (delta_multiplier == 0) {
delta_multiplier = -1;
} else {
delta_multiplier *= -1;
}
}
if (ui_node(main_window, &(comp.ctx), node_elem->rec,
node_elem->colours)) {
node_elem->rec.topleft.x += comp.ctx.rel_x;
node_elem->rec.topleft.y += comp.ctx.rel_y;
for (u64 j = 0; j < node_elem->inputs; ++j) {
noodle *ndl = &(node_elem->noodles[j]);
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;
}
for (u64 j = NODE_START; j <= comp.count; ++j) {
if (j == i) {
continue;
}
node *nd = &(comp.nodes[j]);
if (nd->inputs == 0) {
continue;
}
for (u64 k = 0; k < nd->inputs; ++k) {
noodle *ndl = &(nd->noodles[k]);
if (ndl->connected_node == i) {
ndl->ln.p0.x += comp.ctx.rel_x;
ndl->ln.p0.y += comp.ctx.rel_y;
}
}
}
}
}
draw_node_graph(&comp, main_window);
for (u64 i = 0; i < MAX_WINDOWS; ++i) {
swap_buffers(&(comp.windows[i]));
@ -268,8 +174,8 @@ i32 run_main_loop(void) {
return EXIT_SUCCESS;
}
void add_node(compositor *comp, node_type type, node_data data, u64 inputs,
i32 x, i32 y, ui_elem_colours colours) {
internal void add_node(compositor *comp, node_type type, node_data data,
u64 inputs, i32 x, i32 y, ui_elem_colours colours) {
if (comp->count + 1 >= MAX_NODES) {
return;
}
@ -296,3 +202,101 @@ void add_node(compositor *comp, node_type type, node_data data, u64 inputs,
.noodles = noodles,
};
}
internal void draw_node_graph(compositor *comp, const window *wnd) {
for (u64 i = NODE_START; i <= comp->count; ++i) {
node *node_elem = &(comp->nodes[i]);
f64 angle = 90.0;
f64 angle_delta = 25.0;
i64 delta_multiplier = node_elem->inputs % 2 == 0 ? -1 : 0;
for (u64 j = 0; j < comp->nodes[i].inputs; ++j) {
f64 new_angle = angle + angle_delta * delta_multiplier;
noodle *ndl = &(node_elem->noodles[j]);
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};
ndl->ln = line_from_origin(origin, new_angle, DEFAULT_NOODLE_LENGTH);
}
switch (ui_noodle(wnd, &(comp->ctx), ndl->ln, node_elem->colours,
node_elem->rec)) {
case NOODLE_ACTION_DRAGGING:
ndl->ln.p0.x += comp->ctx.rel_x;
ndl->ln.p0.y += comp->ctx.rel_y;
break;
case NOODLE_ACTION_RELEASED:
for (u64 k = NODE_START; k <= comp->count; ++k) {
if (k == i) {
continue;
}
const node *nd = &(comp->nodes[k]);
if (aabb(nd->rec, comp->ctx.mouse_x, comp->ctx.mouse_y)) {
point p0 = {nd->rec.topleft.x + nd->rec.w / 2,
nd->rec.topleft.y + nd->rec.h / 2};
ndl->ln.p0 = p0;
ndl->connected_node = k;
} else {
ndl->ln.p0 = ndl->ln.p1;
ndl->connected_node = EMPTY_NODE;
}
}
break;
default:
break;
}
if (delta_multiplier > 0) {
angle = new_angle;
}
if (delta_multiplier == 0) {
delta_multiplier = -1;
} else {
delta_multiplier *= -1;
}
}
if (ui_node(wnd, &(comp->ctx), node_elem->rec, node_elem->colours)) {
node_elem->rec.topleft.x += comp->ctx.rel_x;
node_elem->rec.topleft.y += comp->ctx.rel_y;
for (u64 j = 0; j < node_elem->inputs; ++j) {
noodle *ndl = &(node_elem->noodles[j]);
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;
}
for (u64 j = NODE_START; j <= comp->count; ++j) {
if (j == i) {
continue;
}
node *nd = &(comp->nodes[j]);
if (nd->inputs == 0) {
continue;
}
for (u64 k = 0; k < nd->inputs; ++k) {
noodle *ndl = &(nd->noodles[k]);
if (ndl->connected_node == i) {
ndl->ln.p0.x += comp->ctx.rel_x;
ndl->ln.p0.y += comp->ctx.rel_y;
}
}
}
}
}
}