Compare commits
2 Commits
373e48216d
...
f2faa56e5f
Author | SHA1 | Date | |
---|---|---|---|
f2faa56e5f | |||
a68d6997a5 |
13
include/ui.h
13
include/ui.h
@ -21,6 +21,8 @@
|
|||||||
typedef enum ui_elem_type ui_elem_type;
|
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;
|
||||||
|
typedef struct ui_elem_colours ui_elem_colours;
|
||||||
|
typedef enum noodle_action noodle_action;
|
||||||
|
|
||||||
enum ui_elem_type {
|
enum ui_elem_type {
|
||||||
UI_ELEM_NODE,
|
UI_ELEM_NODE,
|
||||||
@ -39,12 +41,17 @@ struct ui_elem {
|
|||||||
ui_elem_type type;
|
ui_elem_type type;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct ui_elem_colours ui_elem_colours;
|
|
||||||
struct ui_elem_colours {
|
struct ui_elem_colours {
|
||||||
colour fill;
|
colour fill;
|
||||||
colour border;
|
colour border;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum noodle_action {
|
||||||
|
NOODLE_ACTION_NONE,
|
||||||
|
NOODLE_ACTION_DRAGGING,
|
||||||
|
NOODLE_ACTION_RELEASED,
|
||||||
|
};
|
||||||
|
|
||||||
struct ui_ctx {
|
struct ui_ctx {
|
||||||
u64 count;
|
u64 count;
|
||||||
i64 hovered;
|
i64 hovered;
|
||||||
@ -66,7 +73,7 @@ bool ui_button(const window *wnd, ui_ctx *ctx, rect rect,
|
|||||||
ui_elem_colours colours);
|
ui_elem_colours colours);
|
||||||
bool ui_node(const window *wnd, ui_ctx *ctx, rect rect,
|
bool ui_node(const window *wnd, ui_ctx *ctx, rect rect,
|
||||||
ui_elem_colours colours);
|
ui_elem_colours colours);
|
||||||
bool ui_noodle(const window *wnd, ui_ctx *ctx, line ln, ui_elem_colours colours,
|
noodle_action ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||||
rect parent_node);
|
ui_elem_colours colours, rect parent_node);
|
||||||
|
|
||||||
#endif // !UI_H
|
#endif // !UI_H
|
||||||
|
@ -171,10 +171,32 @@ i32 run_main_loop(void) {
|
|||||||
*ln = line_from_origin(origin, new_angle, DEFAULT_NOODLE_LENGTH);
|
*ln = line_from_origin(origin, new_angle, DEFAULT_NOODLE_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ui_noodle(main_window, &(comp.ctx), *ln, node_elem->colours,
|
switch (ui_noodle(main_window, &(comp.ctx), *ln, node_elem->colours,
|
||||||
node_elem->rec)) {
|
node_elem->rec)) {
|
||||||
|
case NOODLE_ACTION_DRAGGING:
|
||||||
ln->p0.x += comp.ctx.rel_x;
|
ln->p0.x += comp.ctx.rel_x;
|
||||||
ln->p0.y += comp.ctx.rel_y;
|
ln->p0.y += comp.ctx.rel_y;
|
||||||
|
break;
|
||||||
|
case NOODLE_ACTION_RELEASED:
|
||||||
|
for (u64 k = 0; 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};
|
||||||
|
|
||||||
|
ln->p0 = p0;
|
||||||
|
} else {
|
||||||
|
ln->p0 = ln->p1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (delta_multiplier > 0) {
|
if (delta_multiplier > 0) {
|
||||||
|
17
src/ui.c
17
src/ui.c
@ -141,10 +141,10 @@ bool ui_node(const window *wnd, ui_ctx *ctx, rect rect,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ui_noodle(const window *wnd, ui_ctx *ctx, line ln, ui_elem_colours colours,
|
noodle_action ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||||
rect parent_node) {
|
ui_elem_colours colours, rect parent_node) {
|
||||||
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
|
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
|
||||||
return false;
|
return NOODLE_ACTION_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 id = get_id(ctx);
|
u64 id = get_id(ctx);
|
||||||
@ -220,24 +220,25 @@ bool ui_noodle(const window *wnd, ui_ctx *ctx, line ln, ui_elem_colours colours,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != id)) {
|
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != id)) {
|
||||||
return false;
|
return NOODLE_ACTION_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->mouse_up) {
|
if (ctx->mouse_up) {
|
||||||
if (ctx->hovered == ctx->active && ctx->hovered == id) {
|
if (ctx->hovered == ctx->active && ctx->hovered == id) {
|
||||||
ctx->hovered = ctx->active = -1;
|
ctx->hovered = ctx->active = -1;
|
||||||
|
return NOODLE_ACTION_RELEASED;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return NOODLE_ACTION_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->hovered == id && ctx->active == id) {
|
if (ctx->hovered == id && ctx->active == id) {
|
||||||
return true;
|
return NOODLE_ACTION_DRAGGING;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!aabb(bounding_box, ctx->mouse_x, ctx->mouse_y) ||
|
if (!aabb(bounding_box, ctx->mouse_x, ctx->mouse_y) ||
|
||||||
aabb(parent_node, ctx->mouse_x, ctx->mouse_y)) {
|
aabb(parent_node, ctx->mouse_x, ctx->mouse_y)) {
|
||||||
return false;
|
return NOODLE_ACTION_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->hovered = id;
|
ctx->hovered = id;
|
||||||
@ -246,7 +247,7 @@ bool ui_noodle(const window *wnd, ui_ctx *ctx, line ln, ui_elem_colours colours,
|
|||||||
ctx->active = id;
|
ctx->active = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return NOODLE_ACTION_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal u64 get_id(ui_ctx *ctx) {
|
internal u64 get_id(ui_ctx *ctx) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user