Add different return states for ui_noodle
This commit is contained in:
parent
373e48216d
commit
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
|
||||||
|
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…
Reference in New Issue
Block a user