Refactor UI code into an immediate mode style #1
12
include/ui.h
12
include/ui.h
@ -31,6 +31,12 @@ struct ui_elem {
|
|||||||
ui_elem_type_t type;
|
ui_elem_type_t type;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef struct ui_elem_colours ui_elem_colours_t;
|
||||||
|
struct ui_elem_colours {
|
||||||
|
colour_t fill;
|
||||||
|
colour_t border;
|
||||||
|
};
|
||||||
|
|
||||||
struct ui_ctx {
|
struct ui_ctx {
|
||||||
u64 count;
|
u64 count;
|
||||||
i64 hovered;
|
i64 hovered;
|
||||||
@ -49,7 +55,9 @@ void init_ui_ctx(ui_ctx_t *ctx);
|
|||||||
void reset_ui_ctx(ui_ctx_t *ctx);
|
void reset_ui_ctx(ui_ctx_t *ctx);
|
||||||
void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx,
|
void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx,
|
||||||
const SDL_Event *event);
|
const SDL_Event *event);
|
||||||
bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect);
|
bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect,
|
||||||
rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect);
|
ui_elem_colours_t colours);
|
||||||
|
rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect,
|
||||||
|
ui_elem_colours_t colours);
|
||||||
|
|
||||||
#endif // !UI_H
|
#endif // !UI_H
|
||||||
|
33
src/ui.c
33
src/ui.c
@ -3,25 +3,6 @@
|
|||||||
#include "aliases/aliases.h"
|
#include "aliases/aliases.h"
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
|
|
||||||
typedef struct ui_elem_colours ui_elem_colours_t;
|
|
||||||
struct ui_elem_colours {
|
|
||||||
colour_t fill;
|
|
||||||
colour_t border;
|
|
||||||
};
|
|
||||||
|
|
||||||
INTERNAL ui_elem_colours_t colours[COUNT_UI_ELEM] = {
|
|
||||||
[UI_ELEM_NODE] =
|
|
||||||
(ui_elem_colours_t){
|
|
||||||
.fill = (colour_t){.abgr = 0xff2c84b7},
|
|
||||||
.border = (colour_t){.abgr = 0xff315c89},
|
|
||||||
},
|
|
||||||
[UI_ELEM_BUTTON] =
|
|
||||||
(ui_elem_colours_t){
|
|
||||||
.fill = (colour_t){.abgr = 0xff89a83c},
|
|
||||||
.border = (colour_t){.abgr = 0xff768432},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
bool aabb(const ui_elem_t *elem, i32 x, i32 y) {
|
bool aabb(const ui_elem_t *elem, i32 x, i32 y) {
|
||||||
return x > elem->rect.topleft.x && x <= elem->rect.topleft.x + elem->rect.w &&
|
return x > elem->rect.topleft.x && x <= elem->rect.topleft.x + elem->rect.w &&
|
||||||
y > elem->rect.topleft.y && y <= elem->rect.topleft.y + elem->rect.h;
|
y > elem->rect.topleft.y && y <= elem->rect.topleft.y + elem->rect.h;
|
||||||
@ -71,7 +52,8 @@ void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) {
|
bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect,
|
||||||
|
ui_elem_colours_t colours) {
|
||||||
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
|
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -82,8 +64,8 @@ bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) {
|
|||||||
.type = UI_ELEM_BUTTON,
|
.type = UI_ELEM_BUTTON,
|
||||||
};
|
};
|
||||||
|
|
||||||
fill_rect(wnd, rect, colours[UI_ELEM_BUTTON].fill);
|
fill_rect(wnd, rect, colours.fill);
|
||||||
draw_rect(wnd, rect, colours[UI_ELEM_BUTTON].border);
|
draw_rect(wnd, rect, colours.border);
|
||||||
|
|
||||||
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) {
|
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) {
|
||||||
return false;
|
return false;
|
||||||
@ -107,7 +89,8 @@ bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) {
|
rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect,
|
||||||
|
ui_elem_colours_t colours) {
|
||||||
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
|
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
|
||||||
return (rect_t){0};
|
return (rect_t){0};
|
||||||
}
|
}
|
||||||
@ -118,8 +101,8 @@ rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) {
|
|||||||
.type = UI_ELEM_NODE,
|
.type = UI_ELEM_NODE,
|
||||||
};
|
};
|
||||||
|
|
||||||
fill_rect(wnd, rect, colours[UI_ELEM_NODE].fill);
|
fill_rect(wnd, rect, colours.fill);
|
||||||
draw_rect(wnd, rect, colours[UI_ELEM_NODE].border);
|
draw_rect(wnd, rect, colours.border);
|
||||||
|
|
||||||
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) {
|
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) {
|
||||||
return rect;
|
return rect;
|
||||||
|
Loading…
Reference in New Issue
Block a user