Fix moving behaviour

This commit is contained in:
Abdelrahman Said 2024-01-21 19:39:25 +00:00
parent 64f0328966
commit 53d0a4698f
2 changed files with 28 additions and 39 deletions

View File

@ -76,13 +76,6 @@ i32 run_main_loop(void) {
i32 toolbox_button_x = (toolbox->width - BUTTON_WIDTH) / 2; i32 toolbox_button_x = (toolbox->width - BUTTON_WIDTH) / 2;
quad qd = (quad){
.p0 = (point){140, 40},
.p1 = (point){190, 40},
.p2 = (point){170, 200},
.p3 = (point){210, 200},
};
while (comp.running) { while (comp.running) {
while (SDL_PollEvent(&(comp.event))) { while (SDL_PollEvent(&(comp.event))) {
handle_ui_events(&(comp.windows[comp.active_window - 1]), &(comp.ctx), handle_ui_events(&(comp.windows[comp.active_window - 1]), &(comp.ctx),
@ -159,8 +152,6 @@ i32 run_main_loop(void) {
comp.nodes[i].colours); comp.nodes[i].colours);
} }
qd = ui_quad(main_window, &(comp.ctx), qd, op_node_colours);
for (u64 i = 0; i < MAX_WINDOWS; ++i) { for (u64 i = 0; i < MAX_WINDOWS; ++i) {
swap_buffers(&(comp.windows[i])); swap_buffers(&(comp.windows[i]));
} }

View File

@ -3,10 +3,7 @@
#include "aliases/aliases.h" #include "aliases/aliases.h"
#include "window.h" #include "window.h"
bool aabb(rect rect, i32 x, i32 y) { INTERNAL bool aabb(rect rec, 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 *ctx) { void init_ui_ctx(ui_ctx *ctx) {
*ctx = (ui_ctx){0}; *ctx = (ui_ctx){0};
@ -18,6 +15,8 @@ void reset_ui_ctx(ui_ctx *ctx) {
ctx->count = 0; ctx->count = 0;
ctx->mouse_down = false; ctx->mouse_down = false;
ctx->mouse_up = false; ctx->mouse_up = false;
ctx->rel_x = 0;
ctx->rel_y = 0;
} }
void handle_ui_events(const window *wnd, ui_ctx *ctx, const SDL_Event *event) { void handle_ui_events(const window *wnd, ui_ctx *ctx, const SDL_Event *event) {
@ -26,6 +25,8 @@ void handle_ui_events(const window *wnd, ui_ctx *ctx, const SDL_Event *event) {
if (wnd->id == event->motion.windowID) { if (wnd->id == event->motion.windowID) {
ctx->mouse_x = event->motion.x; ctx->mouse_x = event->motion.x;
ctx->mouse_y = event->motion.y; ctx->mouse_y = event->motion.y;
ctx->rel_x += event->motion.xrel;
ctx->rel_y += event->motion.yrel;
ctx->wnd = wnd; ctx->wnd = wnd;
break; break;
@ -36,19 +37,19 @@ void handle_ui_events(const window *wnd, ui_ctx *ctx, const SDL_Event *event) {
ctx->mouse_y = event->button.y; ctx->mouse_y = event->button.y;
ctx->mouse_down = true; ctx->mouse_down = true;
ctx->wnd = wnd; ctx->wnd = wnd;
}
break; break;
}
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
if (wnd->id == event->button.windowID) { if (wnd->id == event->button.windowID) {
ctx->mouse_x = event->button.x; ctx->mouse_x = event->button.x;
ctx->mouse_y = event->button.y; ctx->mouse_y = event->button.y;
ctx->mouse_up = true; ctx->mouse_up = true;
ctx->wnd = wnd; ctx->wnd = wnd;
}
break; break;
} }
}
} }
bool ui_button(const window *wnd, ui_ctx *ctx, rect rec, bool ui_button(const window *wnd, ui_ctx *ctx, rect rec,
@ -106,12 +107,10 @@ rect ui_node(const window *wnd, ui_ctx *ctx, rect rec,
} }
if (ctx->hovered == id && ctx->active == id) { if (ctx->hovered == id && ctx->active == id) {
return (rect){ rec.topleft.x += ctx->rel_x;
.topleft.x = ctx->mouse_x + ctx->rel_x, rec.topleft.y += ctx->rel_y;
.topleft.y = ctx->mouse_y + ctx->rel_y,
.w = rec.w, return rec;
.h = rec.h,
};
} }
if (!aabb(rec, ctx->mouse_x, ctx->mouse_y)) { if (!aabb(rec, ctx->mouse_x, ctx->mouse_y)) {
@ -122,8 +121,6 @@ rect ui_node(const window *wnd, ui_ctx *ctx, rect rec,
if (ctx->mouse_down) { if (ctx->mouse_down) {
ctx->active = id; ctx->active = id;
ctx->rel_x = rec.topleft.x - ctx->mouse_x;
ctx->rel_y = rec.topleft.y - ctx->mouse_y;
} }
return rec; return rec;
@ -149,16 +146,16 @@ quad ui_quad(const window *wnd, ui_ctx *ctx, quad qd, ui_elem_colours colours) {
} }
if (ctx->hovered == id && ctx->active == id) { if (ctx->hovered == id && ctx->active == id) {
return (quad){ qd.p0.x += ctx->rel_x;
.p0 = (point){ctx->mouse_x - qd.p0.x + ctx->rel_x, qd.p0.y += ctx->rel_y;
ctx->mouse_y - qd.p0.y + ctx->rel_y}, qd.p1.x += ctx->rel_x;
.p1 = (point){ctx->mouse_x - qd.p1.x + ctx->rel_x, qd.p1.y += ctx->rel_y;
ctx->mouse_y - qd.p1.y + ctx->rel_y}, qd.p2.x += ctx->rel_x;
.p2 = (point){ctx->mouse_x - qd.p2.x + ctx->rel_x, qd.p2.y += ctx->rel_y;
ctx->mouse_y - qd.p2.y + ctx->rel_y}, qd.p3.x += ctx->rel_x;
.p3 = (point){ctx->mouse_x - qd.p3.x + ctx->rel_x, qd.p3.y += ctx->rel_y;
ctx->mouse_y - qd.p3.y + ctx->rel_y},
}; return qd;
} }
i32 _min_x_1 = qd.p0.x < qd.p1.x ? qd.p0.x : qd.p1.x; i32 _min_x_1 = qd.p0.x < qd.p1.x ? qd.p0.x : qd.p1.x;
@ -184,8 +181,6 @@ quad ui_quad(const window *wnd, ui_ctx *ctx, quad qd, ui_elem_colours colours) {
.h = max_y - min_y, .h = max_y - min_y,
}; };
draw_rect(wnd, rec, (colour){.abgr = 0xff000000});
if (!aabb(rec, ctx->mouse_x, ctx->mouse_y)) { if (!aabb(rec, ctx->mouse_x, ctx->mouse_y)) {
return qd; return qd;
} }
@ -194,9 +189,12 @@ quad ui_quad(const window *wnd, ui_ctx *ctx, quad qd, ui_elem_colours colours) {
if (ctx->mouse_down) { if (ctx->mouse_down) {
ctx->active = id; ctx->active = id;
ctx->rel_x = qd.p0.x - ctx->mouse_x;
ctx->rel_y = qd.p0.y - ctx->mouse_y;
} }
return qd; return qd;
} }
INTERNAL bool aabb(rect rec, i32 x, i32 y) {
return x > rec.topleft.x && x <= rec.topleft.x + rec.w && y > rec.topleft.y &&
y <= rec.topleft.y + rec.h;
}