Simplify the immediate mode functions and move node

connection logic to the application level
This commit is contained in:
2024-02-25 20:31:11 +00:00
parent bc6bb1afdc
commit 7b7a913c46
3 changed files with 100 additions and 157 deletions

View File

@@ -9,6 +9,7 @@
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_video.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_WINDOWS 2
@@ -153,8 +154,54 @@ i32 run_main_loop(void) {
}
for (u64 i = 0; i < comp.count; ++i) {
comp.nodes[i].node = ui_node(main_window, &(comp.ctx), comp.nodes[i].node,
comp.nodes[i].colours);
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;
line *ln = &(node_elem->noodles[j]);
if (ln->p0.x == ln->p1.x && ln->p0.y == 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);
}
if (ui_noodle(main_window, &(comp.ctx), *ln, node_elem->colours,
node_elem->rec)) {
ln->p0.x += comp.ctx.rel_x;
ln->p0.y += comp.ctx.rel_y;
}
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 < comp.nodes[i].inputs; ++j) {
line *ln = &(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;
}
}
}
for (u64 i = 0; i < MAX_WINDOWS; ++i) {
@@ -181,28 +228,25 @@ void add_node(compositor *comp, node_type type, node_data data, u64 inputs,
return;
}
u64 alloc_size = inputs * sizeof(ui_noodle_elem);
ui_noodle_elem *noodles = mem_arena_alloc(comp->arena, alloc_size);
u64 alloc_size = inputs * sizeof(line);
line *noodles = mem_arena_alloc(comp->arena, alloc_size);
if (!noodles) {
return;
}
ui_node_elem elem = {
.rec =
(rect){
.topleft.x = x,
.topleft.y = y,
.w = NODE_WIDTH,
.h = NODE_HEIGHT,
},
.inputs = inputs,
.noodles = noodles,
rect rec = (rect){
.topleft.x = x,
.topleft.y = y,
.w = NODE_WIDTH,
.h = NODE_HEIGHT,
};
comp->nodes[(comp->count)++] = (node){
.node = elem,
.rec = rec,
.colours = colours,
.type = type,
.data.path = data.path,
.inputs = inputs,
.noodles = noodles,
};
}