From 8d6235221fc8bb78d371667c68ebff2806efe678 Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Mon, 16 Jan 2023 17:40:15 +0100 Subject: [PATCH] cleanup --- def_style.h | 2 +- test/main.c | 2 +- ugui.c | 64 ++++++++++++++++++++++++++--------------------------- ugui.h | 12 ++-------- 4 files changed, 35 insertions(+), 45 deletions(-) diff --git a/def_style.h b/def_style.h index 75ab6f6..386fd1b 100644 --- a/def_style.h +++ b/def_style.h @@ -13,7 +13,7 @@ static const ug_style_t default_style = { .margin = SIZE_PX(3), .border = { .color = RGB_FORMAT(0xf50a00), - .size = SIZE_PX(10), + .size = SIZE_PX(2), }, .title = { .color = { diff --git a/test/main.c b/test/main.c index 1939caf..2e52c66 100644 --- a/test/main.c +++ b/test/main.c @@ -192,7 +192,7 @@ int main(void) //} ug_container_floating(ctx, "stupid name", (ug_div_t){.x=SIZE_PX(0), .y=SIZE_PX(0), .w=SIZE_PX(100), .h=SIZE_MM(75.0)}); - + ug_element_button(ctx, "float", "", (ug_div_t){SQUARE(SIZE_MM(5))}); //ug_container_floating(ctx, "floating windoooooooow", (ug_div_t){.x=SIZE_PX(100), .y=SIZE_PX(0), .w=SIZE_PX(100), .h=SIZE_MM(75.0)}); //ug_container_sidebar(ctx, "Right Sidebar", (ug_size_t)SIZE_PX(300), UG_SIDE_RIGHT); diff --git a/ugui.c b/ugui.c index 5c25d26..c2fbcb4 100644 --- a/ugui.c +++ b/ugui.c @@ -259,7 +259,7 @@ ug_ctx_t *ug_ctx_new(void) ctx->style_px = &style_cache; // NOTE: this fixes a bug where for the first frame the container stack // doesn't get sorted, but it is kind of a botch - ctx->last_active.cnt = 1; + ctx->last_active_cnt = 1; ug_ctx_set_displayinfo(ctx, DEF_SCALE, DEF_PPI); // TODO: allocate stacks @@ -431,7 +431,7 @@ static void sort_containers(ug_ctx_t *ctx) ug_container_t *s = ctx->cnt_stack.items; ug_container_t c = *cnt; - if (ctx->active.cnt != c.id) + if (ctx->active_cnt != c.id) for (; y > 0 && TEST(s[y-1].flags, UG_CNT_FLOATING); y--); if (i >= y) @@ -540,7 +540,7 @@ static int handle_container(ug_ctx_t *ctx, ug_container_t *cnt) { // if we are not the currently active container than definately we are not // being moved or resized - if (ctx->active.cnt != cnt->id) { + if (ctx->active_cnt != cnt->id) { cnt->flags &= ~CNT_STATE_ALL; return 0; } @@ -582,6 +582,24 @@ static int handle_container(ug_ctx_t *ctx, ug_container_t *cnt) } } + // lower diagonal resize + if (TEST(cnt->flags, UG_CNT_RESIZE_BOTTOM) && TEST(cnt->flags, UG_CNT_RESIZE_RIGHT)) { + clip = (ug_rect_t){ + .x = R_MARGIN((*rca), m), + .y = B_MARGIN((*rca), m), + .w = b + m, + .h = b + m, + }; + if (STATEIS(cnt, CNT_STATE_RESIZE_D) || + (STATEIS(cnt, 0) && INTERSECTS(mpos, clip))) { + cnt->flags |= CNT_STATE_RESIZE_D; + rect->h = MAX(10, rect->h + ctx->mouse.delta.y); + rca->h = MAX(10, rca->h + ctx->mouse.delta.y); + rect->w = MAX(10, rect->w + ctx->mouse.delta.x); + rca->w = MAX(10, rca->w + ctx->mouse.delta.x); + } + } + // right border resize if (TEST(cnt->flags, UG_CNT_RESIZE_RIGHT)) { clip = (ug_rect_t){ @@ -662,24 +680,6 @@ static int handle_container(ug_ctx_t *ctx, ug_container_t *cnt) } } - // lower diagonal resize - if (TEST(cnt->flags, UG_CNT_RESIZE_BOTTOM | UG_CNT_RESIZE_RIGHT)) { - clip = (ug_rect_t){ - .x = R_MARGIN((*rca), -m), - .y = B_MARGIN((*rca), m), - .w = b + m, - .h = b + m, - }; - if (STATEIS(cnt, CNT_STATE_RESIZE_D) || - (STATEIS(cnt, 0) && INTERSECTS(mpos, clip))) { - cnt->flags |= CNT_STATE_RESIZE_D; - rect->h = MAX(10, rect->h + ctx->mouse.delta.y); - rca->h = MAX(10, rca->h + ctx->mouse.delta.y); - rect->w = MAX(10, rect->w + ctx->mouse.delta.x); - rca->w = MAX(10, rca->w + ctx->mouse.delta.x); - } - } - // if we were not resized but we are still active it means we are doing // something to the contained elements, as such set state to none if (STATEIS(cnt, 0)) @@ -1003,7 +1003,7 @@ int ug_frame_begin(ug_ctx_t *ctx) ctx->origin.h = ctx->size.h; // update hover index - printf("Container Stack: active %x\n", ctx->active.cnt); + printf("Container Stack: active %x\n", ctx->active_cnt); ug_vec2_t v = ctx->mouse.pos; for (int i = 0; i < ctx->cnt_stack.idx; i++) { ug_container_t *c = &ctx->cnt_stack.items[i]; @@ -1019,19 +1019,19 @@ int ug_frame_begin(ug_ctx_t *ctx) ug_rect_t r = c->rca; EXPAND(r, SZ_INT(ctx->style_px->border.size)); if (INTERSECTS(v, r)) - ctx->hover.cnt = c->id; + ctx->hover_cnt = c->id; } printf("\n"); - if (ctx->last_active.cnt && ctx->active.cnt != ctx->last_active.cnt) + if (ctx->last_active_cnt && ctx->active_cnt != ctx->last_active_cnt) ctx->cnt_stack.sorted = 0; // update active container if (MOUSEDOWN(ctx, UG_BTN_LEFT)) { - ctx->active.cnt = ctx->hover.cnt; + ctx->active_cnt = ctx->hover_cnt; } else if (MOUSEUP(ctx, UG_BTN_LEFT)) { - ctx->last_active.cnt = ctx->active.cnt; - ctx->active.cnt = 0; + ctx->last_active_cnt = ctx->active_cnt; + ctx->active_cnt = 0; } return 0; @@ -1072,10 +1072,8 @@ int ug_frame_end(ug_ctx_t *ctx) ctx->mouse.last_pos = ctx->mouse.pos; // reset hover, it has to be calculated at frame beginning - ctx->hover.cnt_last = ctx->hover.cnt; - ctx->hover.elem_last = ctx->hover.elem; - ctx->hover.cnt = 0; - ctx->hover.elem = 0; + // FIXME: reeeeeally? + //ctx->hover_cnt = 0; ctx->last_ppi = ctx->ppi; ctx->last_ppm = ctx->ppm; @@ -1307,7 +1305,7 @@ int handle_element(ug_ctx_t *ctx, ug_container_t *cnt, ug_element_t *elem) { // if the contaniner is not the current hover then do nothing, think about // floating cont. over bodies - if (ctx->hover.cnt != cnt->id) + if (ctx->hover_cnt != cnt->id) return 0; if (INTERSECTS(ctx->mouse.pos, elem->rca)) { @@ -1340,7 +1338,7 @@ void draw_elements(ug_ctx_t *ctx, ug_container_t *cnt) switch (e->type) { case UG_ELEM_BUTTON: col = cnt->hover_elem == e->id ? s->btn.color.act : s->btn.color.bg; - bcol = cnt->selected_elem == e->id ? s->btn.color.sel : s->btn.color.br; + bcol = cnt->selected_elem == e->id && ctx->hover_cnt == cnt->id ? s->btn.color.sel : s->btn.color.br; txtcol = s->btn.color.fg; ts = SZ_INT(s->btn.font_size); eb = SZ_INT(ctx->style_px->btn.border); diff --git a/ugui.h b/ugui.h index 2a3f06e..f3f1537 100644 --- a/ugui.h +++ b/ugui.h @@ -177,20 +177,12 @@ typedef struct { ug_vec2_t size; ug_rect_t origin; // which context and element we are hovering - struct { - ug_id_t cnt, elem; - ug_id_t cnt_last, elem_last; - } hover; + ug_id_t hover_cnt; // active is updated on mousedown and released on mouseup // the id of the "active" element, active means different things for // different elements, for exaple active for a button means to be pressed, // and for a text box it means to be focused - struct { - ug_id_t cnt, elem; - } active; - struct { - ug_id_t cnt, elem; - } last_active; + ug_id_t active_cnt, last_active_cnt; // id of the selected container, used for layout // NOTE: since the stacks can be relocated with realloc it is better not // to use a pointer here, even tough it would be better for efficiency