Compare commits
No commits in common. "718e7eec2158172e2754519f731a9e410e7e4d07" and "fb1af033081e0120135d14ec8861392eab0f90f7" have entirely different histories.
718e7eec21
...
fb1af03308
@ -35,17 +35,16 @@ struct ui_ctx {
|
||||
u64 count;
|
||||
i64 hovered;
|
||||
i64 active;
|
||||
i32 mouse_x;
|
||||
i32 mouse_y;
|
||||
i32 rel_x;
|
||||
i32 rel_y;
|
||||
i64 mouse_x;
|
||||
i64 mouse_y;
|
||||
i64 rel_x;
|
||||
i64 rel_y;
|
||||
bool mouse_down;
|
||||
bool mouse_up;
|
||||
const window_t *wnd;
|
||||
ui_elem_t elements[MAX_UI_ELEMENTS];
|
||||
};
|
||||
|
||||
void init_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,
|
||||
const SDL_Event *event);
|
||||
|
@ -42,9 +42,6 @@ i32 run_main_loop(void) {
|
||||
}
|
||||
|
||||
compositor_t comp = {0};
|
||||
|
||||
init_ui_ctx(&(comp.ctx));
|
||||
|
||||
comp.rects = (rect_t *)malloc(sizeof(rect_t) * MAX_NODES);
|
||||
comp.nodes = (node_t *)malloc(sizeof(node_t) * MAX_NODES);
|
||||
|
||||
|
30
src/ui.c
30
src/ui.c
@ -2,6 +2,7 @@
|
||||
#include "SDL_events.h"
|
||||
#include "aliases/aliases.h"
|
||||
#include "window.h"
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct ui_elem_colours ui_elem_colours_t;
|
||||
struct ui_elem_colours {
|
||||
@ -27,22 +28,21 @@ bool aabb(const ui_elem_t *elem, i32 x, i32 y) {
|
||||
y > elem->rect.topleft.y && y <= elem->rect.topleft.y + elem->rect.h;
|
||||
}
|
||||
|
||||
void init_ui_ctx(ui_ctx_t *ctx) {
|
||||
*ctx = (ui_ctx_t){0};
|
||||
ctx->hovered = -1;
|
||||
ctx->active = -1;
|
||||
}
|
||||
|
||||
void reset_ui_ctx(ui_ctx_t *ctx) {
|
||||
ctx->count = 0;
|
||||
ctx->mouse_down = false;
|
||||
ctx->mouse_up = false;
|
||||
ctx->rel_x = 0;
|
||||
ctx->rel_y = 0;
|
||||
}
|
||||
|
||||
void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx,
|
||||
const SDL_Event *event) {
|
||||
switch (event->type) {
|
||||
case SDL_MOUSEMOTION:
|
||||
ctx->rel_x = event->motion.xrel;
|
||||
ctx->rel_y = event->motion.yrel;
|
||||
|
||||
if (wnd->id == event->motion.windowID) {
|
||||
ctx->mouse_x = event->motion.x;
|
||||
ctx->mouse_y = event->motion.y;
|
||||
@ -85,7 +85,7 @@ bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) {
|
||||
fill_rect(wnd, rect, colours[UI_ELEM_BUTTON].fill);
|
||||
draw_rect(wnd, rect, colours[UI_ELEM_BUTTON].border);
|
||||
|
||||
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) {
|
||||
if (wnd != ctx->wnd) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -115,26 +115,24 @@ rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) {
|
||||
ui_elem_t elem = (ui_elem_t){
|
||||
.id = (ctx->count)++,
|
||||
.rect = rect,
|
||||
.type = UI_ELEM_NODE,
|
||||
.type = UI_ELEM_BUTTON,
|
||||
};
|
||||
|
||||
fill_rect(wnd, rect, colours[UI_ELEM_NODE].fill);
|
||||
draw_rect(wnd, rect, colours[UI_ELEM_NODE].border);
|
||||
fill_rect(wnd, rect, colours[UI_ELEM_BUTTON].fill);
|
||||
draw_rect(wnd, rect, colours[UI_ELEM_BUTTON].border);
|
||||
|
||||
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) {
|
||||
if (wnd != ctx->wnd) {
|
||||
return rect;
|
||||
}
|
||||
|
||||
if (ctx->mouse_up) {
|
||||
ctx->hovered = ctx->active = -1;
|
||||
ctx->rel_x = ctx->rel_y = 0;
|
||||
return rect;
|
||||
}
|
||||
|
||||
if (ctx->hovered == elem.id && ctx->active == elem.id) {
|
||||
return (rect_t){
|
||||
.topleft.x = ctx->mouse_x + ctx->rel_x,
|
||||
.topleft.y = ctx->mouse_y + ctx->rel_y,
|
||||
.topleft.x = rect.topleft.x + ctx->rel_x,
|
||||
.topleft.y = rect.topleft.y + ctx->rel_y,
|
||||
.w = rect.w,
|
||||
.h = rect.h,
|
||||
};
|
||||
@ -148,8 +146,6 @@ rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return rect;
|
||||
|
Loading…
Reference in New Issue
Block a user