Support drawing a node with connection that can be modified by user
This commit is contained in:
parent
d73275f04c
commit
10ba3d642d
@ -3,7 +3,6 @@
|
||||
|
||||
#include "aliases/aliases.h"
|
||||
#include "ui.h"
|
||||
#include "window.h"
|
||||
|
||||
#define MAX_NODES 1024
|
||||
|
||||
@ -25,7 +24,7 @@ union node_data {
|
||||
};
|
||||
|
||||
struct node {
|
||||
rect rec;
|
||||
ui_node_elem node;
|
||||
ui_elem_colours colours;
|
||||
node_type type;
|
||||
node_data data;
|
||||
|
18
include/ui.h
18
include/ui.h
@ -14,11 +14,11 @@
|
||||
#define NODE_WIDTH 70
|
||||
#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_ctx ui_ctx;
|
||||
|
||||
enum ui_elemype {
|
||||
enum ui_elem_type {
|
||||
UI_ELEM_NODE,
|
||||
UI_ELEM_BUTTON,
|
||||
|
||||
@ -28,7 +28,7 @@ enum ui_elemype {
|
||||
struct ui_elem {
|
||||
u64 id;
|
||||
rect rect;
|
||||
ui_elemype type;
|
||||
ui_elem_type type;
|
||||
};
|
||||
|
||||
typedef struct ui_elem_colours ui_elem_colours;
|
||||
@ -37,6 +37,12 @@ struct ui_elem_colours {
|
||||
colour border;
|
||||
};
|
||||
|
||||
typedef struct ui_node_elem ui_node_elem;
|
||||
struct ui_node_elem {
|
||||
line noodle;
|
||||
rect rec;
|
||||
};
|
||||
|
||||
struct ui_ctx {
|
||||
u64 count;
|
||||
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);
|
||||
bool ui_button(const window *wnd, ui_ctx *ctx, rect rect,
|
||||
ui_elem_colours colours);
|
||||
rect ui_node(const window *wnd, ui_ctx *ctx, rect rect,
|
||||
ui_elem_colours colours);
|
||||
line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||
ui_elem_colours colours);
|
||||
ui_node_elem ui_node(const window *wnd, ui_ctx *ctx, ui_node_elem node,
|
||||
ui_elem_colours colours);
|
||||
|
||||
#endif // !UI_H
|
||||
|
@ -148,8 +148,8 @@ i32 run_main_loop(void) {
|
||||
}
|
||||
|
||||
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].colours);
|
||||
comp.nodes[i].node = ui_node(main_window, &(comp.ctx), comp.nodes[i].node,
|
||||
comp.nodes[i].colours);
|
||||
}
|
||||
|
||||
for (u64 i = 0; i < MAX_WINDOWS; ++i) {
|
||||
@ -175,12 +175,16 @@ void add_node(compositor *comp, node_type type, node_data data, i32 x, i32 y,
|
||||
}
|
||||
|
||||
comp->nodes[(comp->count)++] = (node){
|
||||
.rec =
|
||||
(rect){
|
||||
.topleft.x = x,
|
||||
.topleft.y = y,
|
||||
.w = NODE_WIDTH,
|
||||
.h = NODE_HEIGHT,
|
||||
.node =
|
||||
(ui_node_elem){
|
||||
.rec =
|
||||
(rect){
|
||||
.topleft.x = x,
|
||||
.topleft.y = y,
|
||||
.w = NODE_WIDTH,
|
||||
.h = NODE_HEIGHT,
|
||||
},
|
||||
.noodle = (line){0},
|
||||
},
|
||||
.colours = colours,
|
||||
.type = type,
|
||||
|
44
src/ui.c
44
src/ui.c
@ -10,6 +10,8 @@
|
||||
#define NOODLE_HALF_WIDTH 2
|
||||
#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 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;
|
||||
}
|
||||
|
||||
rect ui_node(const window *wnd, ui_ctx *ctx, rect rec,
|
||||
ui_elem_colours colours) {
|
||||
ui_node_elem ui_node(const window *wnd, ui_ctx *ctx, ui_node_elem node,
|
||||
ui_elem_colours colours) {
|
||||
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
|
||||
return (rect){0};
|
||||
return (ui_node_elem){0};
|
||||
}
|
||||
|
||||
u64 id = (ctx->count)++;
|
||||
|
||||
fill_rect(wnd, rec, colours.fill);
|
||||
draw_rect(wnd, rec, colours.border);
|
||||
line ln;
|
||||
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)) {
|
||||
return rec;
|
||||
return node;
|
||||
}
|
||||
|
||||
if (ctx->mouse_up) {
|
||||
ctx->hovered = ctx->active = -1;
|
||||
ctx->rel_x = ctx->rel_y = 0;
|
||||
return rec;
|
||||
return node;
|
||||
}
|
||||
|
||||
if (ctx->hovered == id && ctx->active == id) {
|
||||
rec.topleft.x += ctx->rel_x;
|
||||
rec.topleft.y += ctx->rel_y;
|
||||
node.rec.topleft.x += ctx->rel_x;
|
||||
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)) {
|
||||
return rec;
|
||||
if (!aabb(node.rec, ctx->mouse_x, ctx->mouse_y)) {
|
||||
return node;
|
||||
}
|
||||
|
||||
ctx->hovered = id;
|
||||
@ -131,7 +149,7 @@ rect ui_node(const window *wnd, ui_ctx *ctx, rect rec,
|
||||
ctx->active = id;
|
||||
}
|
||||
|
||||
return rec;
|
||||
return node;
|
||||
}
|
||||
|
||||
line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||
|
Loading…
Reference in New Issue
Block a user