Improve implementation of drawing the noodles

This commit is contained in:
Abdelrahman Said 2024-01-21 22:37:09 +00:00
parent f72497e1b7
commit 924ed544aa
3 changed files with 71 additions and 41 deletions

View File

@ -57,6 +57,7 @@ bool ui_button(const window *wnd, ui_ctx *ctx, rect rect,
ui_elem_colours colours);
rect ui_node(const window *wnd, ui_ctx *ctx, rect rect,
ui_elem_colours colours);
quad ui_quad(const window *wnd, ui_ctx *ctx, quad qd, ui_elem_colours colours);
line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
ui_elem_colours colours);
#endif // !UI_H

View File

@ -76,6 +76,11 @@ i32 run_main_loop(void) {
i32 toolbox_button_x = (toolbox->width - BUTTON_WIDTH) / 2;
line ln = (line){
(point){20, 40},
(point){60, 200},
};
while (comp.running) {
while (SDL_PollEvent(&(comp.event))) {
handle_ui_events(&(comp.windows[comp.active_window - 1]), &(comp.ctx),
@ -152,6 +157,8 @@ i32 run_main_loop(void) {
comp.nodes[i].colours);
}
ln = ui_noodle(main_window, &(comp.ctx), ln, op_node_colours);
for (u64 i = 0; i < MAX_WINDOWS; ++i) {
swap_buffers(&(comp.windows[i]));
}

102
src/ui.c
View File

@ -1,7 +1,13 @@
#include "ui.h"
#include "SDL_events.h"
#include "aliases/aliases.h"
#include "math_utils.h"
#include "window.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define NOODLE_WIDTH 2
INTERNAL bool aabb(rect rec, i32 x, i32 y);
@ -126,63 +132,79 @@ rect ui_node(const window *wnd, ui_ctx *ctx, rect rec,
return rec;
}
quad ui_quad(const window *wnd, ui_ctx *ctx, quad qd, ui_elem_colours colours) {
line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
ui_elem_colours colours) {
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
return (quad){0};
return (line){0};
}
u64 id = (ctx->count)++;
fill_quad(wnd, qd, colours.fill);
bool horizontal = ln.p0.y == ln.p1.y;
rect bounding_box = (rect){0};
if (horizontal) {
i32 x = min(ln.p0.x, ln.p1.x);
bounding_box.topleft = (point){x, ln.p0.y};
bounding_box.w = abs(ln.p1.x - ln.p0.x);
bounding_box.h = NOODLE_WIDTH;
fill_rect(wnd, bounding_box, colours.fill);
} else {
quad qd = (quad){
.p0 = ln.p0,
.p1 = (point){ln.p0.x + NOODLE_WIDTH, ln.p0.y},
.p2 = ln.p1,
.p3 = (point){ln.p1.x + NOODLE_WIDTH, ln.p1.y},
};
fill_quad(wnd, qd, colours.fill);
i32 _min_x_1 = min(qd.p0.x, qd.p1.x);
i32 _min_x_2 = min(qd.p2.x, qd.p3.x);
i32 min_x = min(_min_x_1, _min_x_2);
i32 _max_x_1 = max(qd.p0.x, qd.p1.x);
i32 _max_x_2 = max(qd.p2.x, qd.p3.x);
i32 max_x = max(_max_x_1, _max_x_2);
i32 _min_y_1 = min(qd.p0.y, qd.p1.y);
i32 _min_y_2 = min(qd.p2.y, qd.p3.y);
i32 min_y = min(_min_y_1, _min_y_2);
i32 _max_y_1 = max(qd.p0.y, qd.p1.y);
i32 _max_y_2 = max(qd.p2.y, qd.p3.y);
i32 max_y = max(_max_y_1, _max_y_2);
bounding_box = (rect){
.topleft.x = min_x,
.topleft.y = min_y,
.w = max_x - min_x,
.h = max_y - min_y,
};
}
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != id)) {
return qd;
return ln;
}
if (ctx->mouse_up) {
ctx->hovered = ctx->active = -1;
ctx->rel_x = ctx->rel_y = 0;
return qd;
return ln;
}
if (ctx->hovered == id && ctx->active == id) {
qd.p0.x += ctx->rel_x;
qd.p0.y += ctx->rel_y;
qd.p1.x += ctx->rel_x;
qd.p1.y += ctx->rel_y;
qd.p2.x += ctx->rel_x;
qd.p2.y += ctx->rel_y;
qd.p3.x += ctx->rel_x;
qd.p3.y += ctx->rel_y;
ln.p0.x += ctx->rel_x;
ln.p0.y += ctx->rel_y;
return qd;
return ln;
}
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,
};
if (!aabb(rec, ctx->mouse_x, ctx->mouse_y)) {
return qd;
if (!aabb(bounding_box, ctx->mouse_x, ctx->mouse_y)) {
return ln;
}
ctx->hovered = id;
@ -191,7 +213,7 @@ quad ui_quad(const window *wnd, ui_ctx *ctx, quad qd, ui_elem_colours colours) {
ctx->active = id;
}
return qd;
return ln;
}
INTERNAL bool aabb(rect rec, i32 x, i32 y) {