Compare commits

...

2 Commits

3 changed files with 43 additions and 13 deletions

View File

@ -21,6 +21,8 @@
typedef enum ui_elem_type ui_elem_type;
typedef struct ui_elem ui_elem;
typedef struct ui_ctx ui_ctx;
typedef struct ui_elem_colours ui_elem_colours;
typedef enum noodle_action noodle_action;
enum ui_elem_type {
UI_ELEM_NODE,
@ -39,12 +41,17 @@ struct ui_elem {
ui_elem_type type;
};
typedef struct ui_elem_colours ui_elem_colours;
struct ui_elem_colours {
colour fill;
colour border;
};
enum noodle_action {
NOODLE_ACTION_NONE,
NOODLE_ACTION_DRAGGING,
NOODLE_ACTION_RELEASED,
};
struct ui_ctx {
u64 count;
i64 hovered;
@ -66,7 +73,7 @@ bool ui_button(const window *wnd, ui_ctx *ctx, rect rect,
ui_elem_colours colours);
bool ui_node(const window *wnd, ui_ctx *ctx, rect rect,
ui_elem_colours colours);
bool ui_noodle(const window *wnd, ui_ctx *ctx, line ln, ui_elem_colours colours,
rect parent_node);
noodle_action ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
ui_elem_colours colours, rect parent_node);
#endif // !UI_H

View File

@ -171,10 +171,32 @@ i32 run_main_loop(void) {
*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)) {
case NOODLE_ACTION_DRAGGING:
ln->p0.x += comp.ctx.rel_x;
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) {

View File

@ -141,10 +141,10 @@ bool ui_node(const window *wnd, ui_ctx *ctx, rect rect,
return false;
}
bool ui_noodle(const window *wnd, ui_ctx *ctx, line ln, ui_elem_colours colours,
rect parent_node) {
noodle_action ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
ui_elem_colours colours, rect parent_node) {
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
return false;
return NOODLE_ACTION_NONE;
}
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)) {
return false;
return NOODLE_ACTION_NONE;
}
if (ctx->mouse_up) {
if (ctx->hovered == ctx->active && ctx->hovered == id) {
ctx->hovered = ctx->active = -1;
return NOODLE_ACTION_RELEASED;
}
return true;
return NOODLE_ACTION_NONE;
}
if (ctx->hovered == id && ctx->active == id) {
return true;
return NOODLE_ACTION_DRAGGING;
}
if (!aabb(bounding_box, ctx->mouse_x, ctx->mouse_y) ||
aabb(parent_node, ctx->mouse_x, ctx->mouse_y)) {
return false;
return NOODLE_ACTION_NONE;
}
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;
}
return false;
return NOODLE_ACTION_NONE;
}
internal u64 get_id(ui_ctx *ctx) {