From c930520cbdedebeb07468511eae3a5b7e4dd3dcc Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Thu, 8 Dec 2022 18:50:30 +0100 Subject: [PATCH] separate functions for updating dimension, handling input and pushing commands --- test/main.c | 5 ++--- ugui.c | 51 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/test/main.c b/test/main.c index aae0719..e2e92a1 100644 --- a/test/main.c +++ b/test/main.c @@ -178,9 +178,8 @@ int main(void) ug_ctx_set_unit(ctx, UG_UNIT_MM); 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_container_floating(ctx, "better name", - (ug_div_t){.x=SIZE_PX(-10), .y=SIZE_PT(-16), .w=SIZE_MM(100), .h=SIZE_PX(15)}); - ug_container_sidebar(ctx, "Sidebar", (ug_size_t)SIZE_PX(300), UG_SIDE_BOTTOM); + + ug_container_sidebar(ctx, "Sidebar", (ug_size_t)SIZE_PX(300), UG_SIDE_RIGHT); ug_frame_end(ctx); // fill background diff --git a/ugui.c b/ugui.c index 9754f0e..484d2c8 100644 --- a/ugui.c +++ b/ugui.c @@ -282,7 +282,7 @@ static ug_container_t *get_container(ug_ctx_t *ctx, ug_id_t id) // update the container dimensions and position according to the context information, // also handle resizing, moving, ect. if allowed by the container -static void update_container(ug_ctx_t *ctx, ug_container_t *cnt) +static void position_container(ug_ctx_t *ctx, ug_container_t *cnt) { ug_rect_t *rect, *rca; rect = &cnt->rect; @@ -344,27 +344,43 @@ static void update_container(ug_ctx_t *ctx, ug_container_t *cnt) // handle relative position // and move to fit borders - if (rect->w == 0) rca->w = cw - br - bl; + if (rect->w == 0) rca->w = cw; else rca->w += bl + br; - if (rect->h == 0) rca->h = ch - bt - bb; + if (rect->h == 0) rca->h = ch; else if (cnt->flags & UG_CNT_MOVABLE) rca->h += hh + 2*bt + bb; else rca->h += bt + bb; // <0 -> relative to the right margin if (rect->x < 0) rca->x = cw - rca->w + rca->x; if (rect->y < 0) rca->y = ch - rca->h + rca->y; +} + +void handle_container(ug_ctx_t *ctx, ug_container_t *cnt) +{ // if we had focus the frame before, then do shit if (ctx->hover.cnt_last != cnt->id) - goto cnt_draw; + return; // mouse pressed handle resize, for simplicity containers can only // be resized from the bottom and right border // TODO: bring selected container to the top of the stack if (!(ctx->mouse.down_mask & UG_BTN_LEFT) || !(cnt->flags & (RESIZEALL | UG_CNT_MOVABLE))) - goto cnt_draw; + return; + ug_rect_t *rect, *rca; + rect = &cnt->rect; + rca = &cnt->rca; + + const ug_style_t *s = ctx->style_px; + int bl = s->cnt.border.l.size.i; + int br = s->cnt.border.r.size.i; + int bt = s->cnt.border.t.size.i; + int bb = s->cnt.border.b.size.i; + int hh = s->cnt.titlebar.height.size.i; + int cw = ctx->size.w; + int ch = ctx->size.h; ug_vec2_t mpos = ctx->mouse.pos; int minx, maxx, miny, maxy; @@ -451,11 +467,21 @@ static void update_container(ug_ctx_t *ctx, ug_container_t *cnt) // container structure // push the appropriate rectangles to the drawing stack +} + + +void draw_container(ug_ctx_t *ctx, ug_container_t *cnt) +{ ug_rect_t draw_rect; - cnt_draw: + const ug_style_t *s = ctx->style_px; + int bl = s->cnt.border.l.size.i; + int br = s->cnt.border.r.size.i; + int bt = s->cnt.border.t.size.i; + int bb = s->cnt.border.b.size.i; + int hh = s->cnt.titlebar.height.size.i; // push outline - draw_rect = *rca; + draw_rect = cnt->rca; push_rect_command(ctx, &draw_rect, s->cnt.border.color); // titlebar @@ -468,7 +494,7 @@ static void update_container(ug_ctx_t *ctx, ug_container_t *cnt) } // push main body - draw_rect = *rca; + draw_rect = cnt->rca; draw_rect.x += bl; draw_rect.y += bt; draw_rect.w -= bl + br; @@ -502,7 +528,10 @@ int ug_container_floating(ug_ctx_t *ctx, const char *name, ug_div_t div) UG_CNT_SCROLL_X | UG_CNT_SCROLL_Y ; } - update_container(ctx, cnt); + // FIXME: floating containers should always stay on top + position_container(ctx, cnt); + handle_container(ctx, cnt); + draw_container(ctx, cnt); return 0; } @@ -554,7 +583,9 @@ int ug_container_sidebar(ug_ctx_t *ctx, const char *name, ug_size_t size, int si cnt->rect = rect; } - update_container(ctx, cnt); + position_container(ctx, cnt); + handle_container(ctx, cnt); + draw_container(ctx, cnt); // TODO: change available context space to reflect adding a sidebar