From e998b6c287f0af6cefc6284f08eadd0db03f7a8a Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Thu, 8 Dec 2022 15:35:58 +0100 Subject: [PATCH] better data structures and unit handling --- test/main.c | 6 ++---- ugui.c | 57 ++++++++++++++++++++++++++++++----------------------- ugui.h | 27 +++++++++++-------------- 3 files changed, 45 insertions(+), 45 deletions(-) diff --git a/test/main.c b/test/main.c index f1a74ca..daefef0 100644 --- a/test/main.c +++ b/test/main.c @@ -177,9 +177,9 @@ int main(void) ug_ctx_set_unit(ctx, UG_UNIT_MM); ug_container_floating(ctx, "stupid name", - (ug_rect_t){.fx = 0, .fy = 0, .fw = 50, .fh = 50}); + (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_rect_t){.fx = -20, .fy = -10, .fw = 100, .fh = 30}); + (ug_div_t){.x=SIZE_PX(-10), .y=SIZE_PT(-16), .w=SIZE_MM(100), .h=SIZE_PX(15)}); ug_frame_end(ctx); @@ -202,8 +202,6 @@ int main(void) } SDL_RenderPresent(r); - printf("-------------------FRAME DONE--------------------\n"); - } while (event.type != SDL_QUIT); cleanup(); diff --git a/ugui.c b/ugui.c index b4dd03b..fc6ed13 100644 --- a/ugui.c +++ b/ugui.c @@ -104,6 +104,28 @@ static ug_id_t hash(const void *data, unsigned int size) } +int size_to_px(ug_ctx_t *ctx, ug_size_t s) +{ + switch (s.unit) { + case UG_UNIT_MM: return roundf(s.size.f * ctx->ppm); + case UG_UNIT_PT: return roundf(s.size.f * ctx->ppd); + case UG_UNIT_PX: return s.size.i; + default: return 0; + } +} + + +ug_rect_t div_to_rect(ug_ctx_t *ctx, ug_div_t *div) +{ + ug_rect_t r; + r.x = size_to_px(ctx, div->x); + r.y = size_to_px(ctx, div->y); + r.w = size_to_px(ctx, div->w); + r.h = size_to_px(ctx, div->h); + return r; +} + + // update the style cache with the correct sizes in pixels and colors static void update_style_cache(ug_ctx_t *ctx) { @@ -261,21 +283,7 @@ static void update_container(ug_ctx_t *ctx, ug_container_t *cnt) rect = &cnt->rect; rca = &cnt->rca; - // if the container is new it has never been converted to pixels - if (cnt->unit != UG_UNIT_PX) { - float scale = 1.0; - switch (ctx->unit) { - case UG_UNIT_MM: scale = ctx->ppm; break; - case UG_UNIT_PT: scale = ctx->ppd; break; - default: break; - } - rect->x = roundf(rect->fx * scale); - rect->y = roundf(rect->fy * scale); - rect->w = roundf(rect->fw * scale); - rect->h = roundf(rect->fh * scale); - - cnt->unit = UG_UNIT_PX; - } else if (ctx->ppi != ctx->last_ppi) { + if (ctx->ppi != ctx->last_ppi) { // if the scale has been updated than we need to scale the container // as well float scale = ctx->ppi / ctx->last_ppi; @@ -317,11 +325,11 @@ static void update_container(ug_ctx_t *ctx, ug_container_t *cnt) */ const ug_style_t *s = ctx->style_px; - int bl = s->cnt.border.l.size; - int br = s->cnt.border.r.size; - int bt = s->cnt.border.t.size; - int bb = s->cnt.border.b.size; - int hh = s->cnt.titlebar.height.size; + 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; @@ -439,12 +447,12 @@ static void update_container(ug_ctx_t *ctx, ug_container_t *cnt) // a floating container can be placed anywhere and can be resized, acts like a // window inside another window -int ug_container_floating(ug_ctx_t *ctx, const char *name, ug_rect_t rect) +int ug_container_floating(ug_ctx_t *ctx, const char *name, ug_div_t div) { TEST_CTX(ctx); - // TODO: verify rect + // TODO: verify div - ug_id_t id = name ? hash(name, strlen(name)) : hash(&rect, sizeof(ug_rect_t)); + ug_id_t id = name ? hash(name, strlen(name)) : hash(&div, sizeof(ug_div_t)); ug_container_t *cnt = get_container(ctx, id); if (cnt->id) { @@ -452,8 +460,7 @@ int ug_container_floating(ug_ctx_t *ctx, const char *name, ug_rect_t rect) } else { cnt->id = id; cnt->max_size = max_size; - cnt->rect = rect; - cnt->unit = ctx->unit; + cnt->rect = div_to_rect(ctx, &div); cnt->flags = UG_CNT_MOVABLE | UG_CNT_RESIZABLE | UG_CNT_SCROLL_X | UG_CNT_SCROLL_Y ; } diff --git a/ugui.h b/ugui.h index 71c3381..9686cee 100644 --- a/ugui.h +++ b/ugui.h @@ -5,22 +5,18 @@ #define BIT(n) (1 << n) #define RGBA_FORMAT(x) { .a=x&0xff, .b=(x>>8)&0xff, .g=(x>>16)&0xff, .r=(x>>24)&0xff } #define RGB_FORMAT(x) { .a=0xff, .b=x&0xff, .g=(x>>8)&0xff, .r=(x>>16)&0xff } -#define SIZE_PX(x) { .size=x, .unit=UG_UNIT_PX } -#define SIZE_MM(x) { .size=x, .unit=UG_UNIT_MM } -#define SIZE_PT(x) { .size=x, .unit=UG_UNIT_PT } +#define SIZE_PX(x) { .size.i=x, .unit=UG_UNIT_PX } +#define SIZE_MM(x) { .size.f=x, .unit=UG_UNIT_MM } +#define SIZE_PT(x) { .size.f=x, .unit=UG_UNIT_PT } // basic types -typedef unsigned int ug_id_t; -typedef struct { union {int x, w;}; union {int y, h;}; } ug_vec2_t; -typedef struct { unsigned char a, b, g, r; } ug_color_t; -typedef struct { int size, unit; } ug_size_t; - -typedef struct { - union { int x; float fx; }; - union { int y; float fy; }; - union { int w; float fw; }; - union { int h; float fh; }; -} ug_rect_t; +typedef unsigned int ug_id_t; +typedef struct { union {int x, w;}; union {int y, h;}; } ug_vec2_t; +typedef struct { unsigned char a, b, g, r; } ug_color_t; +typedef struct { int x, y, w, h; } ug_rect_t; +typedef struct { union {int i; float f;} size; int unit; } ug_size_t; +// div has information about the phisical dimension +typedef struct { ug_size_t x, y, w, h;} ug_div_t; typedef enum { UG_UNIT_PX = 0, @@ -34,7 +30,6 @@ typedef enum { // the z index of a container is determined by it's position on the stack typedef struct { ug_id_t id; - ug_unit_t unit; ug_rect_t rect; // absolute position rect ug_rect_t rca; @@ -194,7 +189,7 @@ int ug_ctx_set_unit(ug_ctx_t *ctx, ug_unit_t unit); // a floating container can be placed anywhere and can be resized, acts like a // window inside another window -int ug_container_floating(ug_ctx_t *ctx, const char *name, ug_rect_t rect); +int ug_container_floating(ug_ctx_t *ctx, const char *name, ug_div_t div); // like a floating container but cannot be resized 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