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

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

View File

@ -3,7 +3,6 @@
#include "aliases/aliases.h" #include "aliases/aliases.h"
#include "ui.h" #include "ui.h"
#include "window.h"
#define MAX_NODES 1024 #define MAX_NODES 1024
@ -25,7 +24,7 @@ union node_data {
}; };
struct node { struct node {
rect rec; ui_node_elem node;
ui_elem_colours colours; ui_elem_colours colours;
node_type type; node_type type;
node_data data; node_data data;

View File

@ -14,11 +14,11 @@
#define NODE_WIDTH 70 #define NODE_WIDTH 70
#define NODE_HEIGHT 20 #define NODE_HEIGHT 20
typedef enum ui_elemype ui_elemype; typedef enum ui_elem_type ui_elem_type;
typedef struct ui_elem ui_elem; typedef struct ui_elem ui_elem;
typedef struct ui_ctx ui_ctx; typedef struct ui_ctx ui_ctx;
enum ui_elemype { enum ui_elem_type {
UI_ELEM_NODE, UI_ELEM_NODE,
UI_ELEM_BUTTON, UI_ELEM_BUTTON,
@ -28,7 +28,7 @@ enum ui_elemype {
struct ui_elem { struct ui_elem {
u64 id; u64 id;
rect rect; rect rect;
ui_elemype type; ui_elem_type type;
}; };
typedef struct ui_elem_colours ui_elem_colours; typedef struct ui_elem_colours ui_elem_colours;
@ -37,6 +37,12 @@ struct ui_elem_colours {
colour border; colour border;
}; };
typedef struct ui_node_elem ui_node_elem;
struct ui_node_elem {
line noodle;
rect rec;
};
struct ui_ctx { struct ui_ctx {
u64 count; u64 count;
i64 hovered; i64 hovered;
@ -55,9 +61,7 @@ void reset_ui_ctx(ui_ctx *ctx);
void handle_ui_events(const window *wnd, ui_ctx *ctx, const SDL_Event *event); void handle_ui_events(const window *wnd, ui_ctx *ctx, const SDL_Event *event);
bool ui_button(const window *wnd, ui_ctx *ctx, rect rect, bool ui_button(const window *wnd, ui_ctx *ctx, rect rect,
ui_elem_colours colours); ui_elem_colours colours);
rect ui_node(const window *wnd, ui_ctx *ctx, rect rect, ui_node_elem ui_node(const window *wnd, ui_ctx *ctx, ui_node_elem node,
ui_elem_colours colours);
line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
ui_elem_colours colours); ui_elem_colours colours);
#endif // !UI_H #endif // !UI_H

View File

@ -148,7 +148,7 @@ i32 run_main_loop(void) {
} }
for (u64 i = 0; i < comp.count; ++i) { 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].node = ui_node(main_window, &(comp.ctx), comp.nodes[i].node,
comp.nodes[i].colours); comp.nodes[i].colours);
} }
@ -175,6 +175,8 @@ void add_node(compositor *comp, node_type type, node_data data, i32 x, i32 y,
} }
comp->nodes[(comp->count)++] = (node){ comp->nodes[(comp->count)++] = (node){
.node =
(ui_node_elem){
.rec = .rec =
(rect){ (rect){
.topleft.x = x, .topleft.x = x,
@ -182,6 +184,8 @@ void add_node(compositor *comp, node_type type, node_data data, i32 x, i32 y,
.w = NODE_WIDTH, .w = NODE_WIDTH,
.h = NODE_HEIGHT, .h = NODE_HEIGHT,
}, },
.noodle = (line){0},
},
.colours = colours, .colours = colours,
.type = type, .type = type,
.data.path = data.path, .data.path = data.path,

View File

@ -10,6 +10,8 @@
#define NOODLE_HALF_WIDTH 2 #define NOODLE_HALF_WIDTH 2
#define DEFAULT_NOODLE_LENGTH 60 #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 bool aabb(rect rec, i32 x, i32 y);
INTERNAL line line_from_origin(point origin, f64 angle, i32 line_length); 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; return false;
} }
rect ui_node(const window *wnd, ui_ctx *ctx, rect rec, ui_node_elem ui_node(const window *wnd, ui_ctx *ctx, ui_node_elem node,
ui_elem_colours colours) { ui_elem_colours colours) {
if (ctx->count + 1 >= MAX_UI_ELEMENTS) { if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
return (rect){0}; return (ui_node_elem){0};
} }
u64 id = (ctx->count)++; u64 id = (ctx->count)++;
fill_rect(wnd, rec, colours.fill); line ln;
draw_rect(wnd, rec, colours.border); 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)) { if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != id)) {
return rec; return node;
} }
if (ctx->mouse_up) { if (ctx->mouse_up) {
ctx->hovered = ctx->active = -1; ctx->hovered = ctx->active = -1;
ctx->rel_x = ctx->rel_y = 0; ctx->rel_x = ctx->rel_y = 0;
return rec; return node;
} }
if (ctx->hovered == id && ctx->active == id) { if (ctx->hovered == id && ctx->active == id) {
rec.topleft.x += ctx->rel_x; node.rec.topleft.x += ctx->rel_x;
rec.topleft.y += ctx->rel_y; 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)) { if (!aabb(node.rec, ctx->mouse_x, ctx->mouse_y)) {
return rec; return node;
} }
ctx->hovered = id; ctx->hovered = id;
@ -131,7 +149,7 @@ rect ui_node(const window *wnd, ui_ctx *ctx, rect rec,
ctx->active = id; ctx->active = id;
} }
return rec; return node;
} }
line ui_noodle(const window *wnd, ui_ctx *ctx, line ln, line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,