From 5d1d5024ec65712a4eae0a70fb193e43290c6c6e Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Sat, 10 Dec 2022 16:51:27 +0100 Subject: [PATCH] menu bar --- test/main.c | 2 ++ ugui.c | 95 +++++++++++++++++++++++++++++++++++++++++++---------- ugui.h | 2 +- 3 files changed, 80 insertions(+), 19 deletions(-) diff --git a/test/main.c b/test/main.c index 79758b0..1993ecf 100644 --- a/test/main.c +++ b/test/main.c @@ -175,6 +175,8 @@ int main(void) ug_frame_begin(ctx); + ug_container_menu_bar(ctx, "Menu fichissimo", (ug_size_t)SIZE_PX(24)); + 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)}); diff --git a/ugui.c b/ugui.c index 8adac9c..a324206 100644 --- a/ugui.c +++ b/ugui.c @@ -188,8 +188,8 @@ ug_ctx_t *ug_ctx_new(void) memset(ctx, 0, sizeof(ug_ctx_t)); ctx->style = &default_style; - ctx->style_px = &style_cache;\ - // NOTE: this fixes a bug where for the first frame the container stack\ + 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; ug_ctx_set_displayinfo(ctx, DEF_SCALE, DEF_PPI); @@ -402,23 +402,50 @@ static void position_container(ug_ctx_t *ctx, ug_container_t *cnt) // if the container is fixed than update the available space in // the context - if (rect->y >= 0) { - if (rect->x < 0) { - ctx->origin.w = rca->x; - } else { + if (rect->x >= 0 && rect->y >= 0) { + if (rect->w) { ctx->origin.x = rca->x + rca->w; - ctx->origin.w -= rca->w; - } - } + ctx->origin.w -= rca->w; + } else if (rect->h) { + ctx->origin.y = rca->y + rca->h; + ctx->origin.h -= rca->h; + } else printf("Huh?\n"); - if (rect->x >= 0) { - if (rect->y < 0) { - ctx->origin.h = rca->y; - } else { + } else if (rect->x < 0 && rect->y >= 0) { + if (rect->w) { + ctx->origin.w -= rca->w; + } else if (rect->h) { ctx->origin.y = rca->y + rca->h; - ctx->origin.y -= rca->h; - } - } + ctx->origin.h -= rca->h; + } else printf("Huh?\n"); + + } else if (rect->x >= 0 && rect->y < 0) { + printf("Really? %x\n", cnt->id); + if (rect->w) { + ctx->origin.x = rca->x + rca->w; + ctx->origin.w -= rca->w; + } else if (rect->h) { + ctx->origin.h -= rca->h; + } else printf("Huh?\n"); + } else printf("What?\n"); + +// if (rect->y >= 0) { +// if (rect->x < 0) { +// ctx->origin.w = rca->x; +// } else { +// ctx->origin.x = rca->x + rca->w; +// ctx->origin.w -= rca->w; +// } +// } +// +// if (rect->x >= 0) { +// if (rect->y < 0) { +// ctx->origin.h = rca->y; +// } else { +// ctx->origin.y = rca->y + rca->h; +// ctx->origin.y -= rca->h; +// } +// } } } @@ -680,9 +707,41 @@ int ug_container_sidebar(ug_ctx_t *ctx, const char *name, ug_size_t size, int si position_container(ctx, cnt); handle_container(ctx, cnt); + + return 0; +} + +int ug_container_menu_bar(ug_ctx_t *ctx, const char *name, ug_size_t height) +{ + TEST_CTX(ctx); + + ug_id_t id = 0; + if (name) { + id = hash(name, strlen(name)); + } else { + int blob[2] = { height.size.i, height.unit}; + id = hash(blob, sizeof(blob)); + } + + ug_container_t *cnt = get_container(ctx, id); - // TODO: change available context space to reflect adding a sidebar + if (cnt->id) { + // nothing? maybe we can skip updating all dimensions and stuff + } else { + cnt->id = id; + cnt->max_size = max_size; + cnt->flags = 0; + ug_rect_t rect = { + .x = 0, .y = 0, + .w = 0, .h = size_to_px(ctx, height), + }; + cnt->rect = rect; + } + + position_container(ctx, cnt); + handle_container(ctx, cnt); + printf("origin: x=%d y=%d w=%d h=%d\n", ctx->origin.x, ctx->origin.y, ctx->origin.w, ctx->origin.h); return 0; } @@ -753,7 +812,7 @@ int ug_frame_begin(ug_ctx_t *ctx) ctx->origin.h = ctx->size.h; // update hover index - printf("Container Stack:\n"); + 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++) { printf("[%d]: %x\n", i, ctx->cnt_stack.items[i].id); diff --git a/ugui.h b/ugui.h index c0b2eed..06edf3c 100644 --- a/ugui.h +++ b/ugui.h @@ -213,7 +213,7 @@ int ug_container_floating(ug_ctx_t *ctx, const char *name, ug_div_t div); int ug_container_popup(ug_ctx_t *ctx, const char *name, ug_rect_t rect); // a menu bar is a container of fixed height, cannot be resized and sits at the // top of the window -int ug_container_menu_bar(ug_ctx_t *ctx, const char *name, int height); +int ug_container_menu_bar(ug_ctx_t *ctx, const char *name, ug_size_t height); // a sidebar is a variable size container anchored to one side of the window int ug_container_sidebar(ug_ctx_t *ctx, const char *name, ug_size_t size, int side); // a body is a container that scales with the window, sits at it's center and cannot