Rename data types and start implementing drawing quads in ui

This commit is contained in:
2024-01-21 03:38:48 +00:00
parent 6df11cfdeb
commit 64f0328966
7 changed files with 251 additions and 180 deletions

155
src/ui.c
View File

@@ -3,25 +3,24 @@
#include "aliases/aliases.h"
#include "window.h"
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 &&
y > elem->rect.topleft.y && y <= elem->rect.topleft.y + elem->rect.h;
bool aabb(rect rect, i32 x, i32 y) {
return x > rect.topleft.x && x <= rect.topleft.x + rect.w &&
y > rect.topleft.y && y <= rect.topleft.y + rect.h;
}
void init_ui_ctx(ui_ctx_t *ctx) {
*ctx = (ui_ctx_t){0};
void init_ui_ctx(ui_ctx *ctx) {
*ctx = (ui_ctx){0};
ctx->hovered = -1;
ctx->active = -1;
}
void reset_ui_ctx(ui_ctx_t *ctx) {
void reset_ui_ctx(ui_ctx *ctx) {
ctx->count = 0;
ctx->mouse_down = false;
ctx->mouse_up = false;
}
void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx,
const SDL_Event *event) {
void handle_ui_events(const window *wnd, ui_ctx *ctx, const SDL_Event *event) {
switch (event->type) {
case SDL_MOUSEMOTION:
if (wnd->id == event->motion.windowID) {
@@ -52,36 +51,32 @@ 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,
ui_elem_colours_t colours) {
bool ui_button(const window *wnd, ui_ctx *ctx, rect rec,
ui_elem_colours colours) {
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
return false;
}
ui_elem_t elem = (ui_elem_t){
.id = (ctx->count)++,
.rect = rect,
.type = UI_ELEM_BUTTON,
};
u64 id = (ctx->count)++;
fill_rect(wnd, rect, colours.fill);
draw_rect(wnd, rect, colours.border);
fill_rect(wnd, rec, colours.fill);
draw_rect(wnd, rec, colours.border);
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) {
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != id)) {
return false;
}
if (!aabb(&elem, ctx->mouse_x, ctx->mouse_y)) {
if (!aabb(rec, ctx->mouse_x, ctx->mouse_y)) {
return false;
}
ctx->hovered = elem.id;
ctx->hovered = id;
if (ctx->mouse_down) {
ctx->active = elem.id;
ctx->active = id;
}
if (ctx->mouse_up && ctx->hovered == elem.id && ctx->active == elem.id) {
if (ctx->mouse_up && ctx->hovered == id && ctx->active == id) {
ctx->hovered = ctx->active = -1;
return true;
}
@@ -89,51 +84,119 @@ bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect,
return false;
}
rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect,
ui_elem_colours_t colours) {
rect ui_node(const window *wnd, ui_ctx *ctx, rect rec,
ui_elem_colours colours) {
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
return (rect_t){0};
return (rect){0};
}
ui_elem_t elem = (ui_elem_t){
.id = (ctx->count)++,
.rect = rect,
.type = UI_ELEM_NODE,
};
u64 id = (ctx->count)++;
fill_rect(wnd, rect, colours.fill);
draw_rect(wnd, rect, colours.border);
fill_rect(wnd, rec, colours.fill);
draw_rect(wnd, rec, colours.border);
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) {
return rect;
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != id)) {
return rec;
}
if (ctx->mouse_up) {
ctx->hovered = ctx->active = -1;
ctx->rel_x = ctx->rel_y = 0;
return rect;
return rec;
}
if (ctx->hovered == elem.id && ctx->active == elem.id) {
return (rect_t){
if (ctx->hovered == id && ctx->active == id) {
return (rect){
.topleft.x = ctx->mouse_x + ctx->rel_x,
.topleft.y = ctx->mouse_y + ctx->rel_y,
.w = rect.w,
.h = rect.h,
.w = rec.w,
.h = rec.h,
};
}
if (!aabb(&elem, ctx->mouse_x, ctx->mouse_y)) {
return rect;
if (!aabb(rec, ctx->mouse_x, ctx->mouse_y)) {
return rec;
}
ctx->hovered = elem.id;
ctx->hovered = id;
if (ctx->mouse_down) {
ctx->active = elem.id;
ctx->rel_x = rect.topleft.x - ctx->mouse_x;
ctx->rel_y = rect.topleft.y - ctx->mouse_y;
ctx->active = id;
ctx->rel_x = rec.topleft.x - ctx->mouse_x;
ctx->rel_y = rec.topleft.y - ctx->mouse_y;
}
return rect;
return rec;
}
quad ui_quad(const window *wnd, ui_ctx *ctx, quad qd, ui_elem_colours colours) {
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
return (quad){0};
}
u64 id = (ctx->count)++;
fill_quad(wnd, qd, colours.fill);
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != id)) {
return qd;
}
if (ctx->mouse_up) {
ctx->hovered = ctx->active = -1;
ctx->rel_x = ctx->rel_y = 0;
return qd;
}
if (ctx->hovered == id && ctx->active == id) {
return (quad){
.p0 = (point){ctx->mouse_x - qd.p0.x + ctx->rel_x,
ctx->mouse_y - qd.p0.y + ctx->rel_y},
.p1 = (point){ctx->mouse_x - qd.p1.x + ctx->rel_x,
ctx->mouse_y - qd.p1.y + ctx->rel_y},
.p2 = (point){ctx->mouse_x - qd.p2.x + ctx->rel_x,
ctx->mouse_y - qd.p2.y + ctx->rel_y},
.p3 = (point){ctx->mouse_x - qd.p3.x + ctx->rel_x,
ctx->mouse_y - qd.p3.y + ctx->rel_y},
};
}
i32 _min_x_1 = qd.p0.x < qd.p1.x ? qd.p0.x : qd.p1.x;
i32 _min_x_2 = qd.p2.x < qd.p3.x ? qd.p2.x : qd.p3.x;
i32 min_x = _min_x_1 < _min_x_2 ? _min_x_1 : _min_x_2;
i32 _max_x_1 = qd.p0.x > qd.p1.x ? qd.p0.x : qd.p1.x;
i32 _max_x_2 = qd.p2.x > qd.p3.x ? qd.p2.x : qd.p3.x;
i32 max_x = _max_x_1 > _max_x_2 ? _max_x_1 : _max_x_2;
i32 _min_y_1 = qd.p0.y < qd.p1.y ? qd.p0.y : qd.p1.y;
i32 _min_y_2 = qd.p2.y < qd.p3.y ? qd.p2.y : qd.p3.y;
i32 min_y = _min_y_1 < _min_y_2 ? _min_y_1 : _min_y_2;
i32 _max_y_1 = qd.p0.y > qd.p1.y ? qd.p0.y : qd.p1.y;
i32 _max_y_2 = qd.p2.y > qd.p3.y ? qd.p2.y : qd.p3.y;
i32 max_y = _max_y_1 > _max_y_2 ? _max_y_1 : _max_y_2;
rect rec = (rect){
.topleft.x = min_x,
.topleft.y = min_y,
.w = max_x - min_x,
.h = max_y - min_y,
};
draw_rect(wnd, rec, (colour){.abgr = 0xff000000});
if (!aabb(rec, ctx->mouse_x, ctx->mouse_y)) {
return qd;
}
ctx->hovered = id;
if (ctx->mouse_down) {
ctx->active = id;
ctx->rel_x = qd.p0.x - ctx->mouse_x;
ctx->rel_y = qd.p0.y - ctx->mouse_y;
}
return qd;
}