From 29bcfabda93982f455ff50dd9c21086e79c35d17 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 25 Feb 2024 21:21:04 +0000 Subject: [PATCH] Move the node drawing code to a function --- src/compositor.c | 204 ++++++++++++++++++++++++----------------------- 1 file changed, 104 insertions(+), 100 deletions(-) diff --git a/src/compositor.c b/src/compositor.c index 06d7af5..4cf23ac 100644 --- a/src/compositor.c +++ b/src/compositor.c @@ -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; + } + } + } + } + } +}