better data structures and unit handling

master
Alessandro Mauri 2 years ago
parent 6d01741426
commit e998b6c287
  1. 6
      test/main.c
  2. 57
      ugui.c
  3. 27
      ugui.h

@ -177,9 +177,9 @@ int main(void)
ug_ctx_set_unit(ctx, UG_UNIT_MM); ug_ctx_set_unit(ctx, UG_UNIT_MM);
ug_container_floating(ctx, "stupid name", 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_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); ug_frame_end(ctx);
@ -202,8 +202,6 @@ int main(void)
} }
SDL_RenderPresent(r); SDL_RenderPresent(r);
printf("-------------------FRAME DONE--------------------\n");
} while (event.type != SDL_QUIT); } while (event.type != SDL_QUIT);
cleanup(); cleanup();

@ -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 // update the style cache with the correct sizes in pixels and colors
static void update_style_cache(ug_ctx_t *ctx) 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; rect = &cnt->rect;
rca = &cnt->rca; rca = &cnt->rca;
// if the container is new it has never been converted to pixels if (ctx->ppi != ctx->last_ppi) {
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 the scale has been updated than we need to scale the container // if the scale has been updated than we need to scale the container
// as well // as well
float scale = ctx->ppi / ctx->last_ppi; 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; const ug_style_t *s = ctx->style_px;
int bl = s->cnt.border.l.size; int bl = s->cnt.border.l.size.i;
int br = s->cnt.border.r.size; int br = s->cnt.border.r.size.i;
int bt = s->cnt.border.t.size; int bt = s->cnt.border.t.size.i;
int bb = s->cnt.border.b.size; int bb = s->cnt.border.b.size.i;
int hh = s->cnt.titlebar.height.size; int hh = s->cnt.titlebar.height.size.i;
int cw = ctx->size.w; int cw = ctx->size.w;
int ch = ctx->size.h; 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 // a floating container can be placed anywhere and can be resized, acts like a
// window inside another window // 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); 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); ug_container_t *cnt = get_container(ctx, id);
if (cnt->id) { if (cnt->id) {
@ -452,8 +460,7 @@ int ug_container_floating(ug_ctx_t *ctx, const char *name, ug_rect_t rect)
} else { } else {
cnt->id = id; cnt->id = id;
cnt->max_size = max_size; cnt->max_size = max_size;
cnt->rect = rect; cnt->rect = div_to_rect(ctx, &div);
cnt->unit = ctx->unit;
cnt->flags = UG_CNT_MOVABLE | UG_CNT_RESIZABLE | cnt->flags = UG_CNT_MOVABLE | UG_CNT_RESIZABLE |
UG_CNT_SCROLL_X | UG_CNT_SCROLL_Y ; UG_CNT_SCROLL_X | UG_CNT_SCROLL_Y ;
} }

@ -5,22 +5,18 @@
#define BIT(n) (1 << n) #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 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 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_PX(x) { .size.i=x, .unit=UG_UNIT_PX }
#define SIZE_MM(x) { .size=x, .unit=UG_UNIT_MM } #define SIZE_MM(x) { .size.f=x, .unit=UG_UNIT_MM }
#define SIZE_PT(x) { .size=x, .unit=UG_UNIT_PT } #define SIZE_PT(x) { .size.f=x, .unit=UG_UNIT_PT }
// basic types // basic types
typedef unsigned int ug_id_t; typedef unsigned int ug_id_t;
typedef struct { union {int x, w;}; union {int y, h;}; } ug_vec2_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 { unsigned char a, b, g, r; } ug_color_t;
typedef struct { int size, unit; } ug_size_t; typedef struct { int x, y, w, h; } ug_rect_t;
typedef struct { union {int i; float f;} size; int unit; } ug_size_t;
typedef struct { // div has information about the phisical dimension
union { int x; float fx; }; typedef struct { ug_size_t x, y, w, h;} ug_div_t;
union { int y; float fy; };
union { int w; float fw; };
union { int h; float fh; };
} ug_rect_t;
typedef enum { typedef enum {
UG_UNIT_PX = 0, 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 // the z index of a container is determined by it's position on the stack
typedef struct { typedef struct {
ug_id_t id; ug_id_t id;
ug_unit_t unit;
ug_rect_t rect; ug_rect_t rect;
// absolute position rect // absolute position rect
ug_rect_t rca; 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 // a floating container can be placed anywhere and can be resized, acts like a
// window inside another window // 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 // like a floating container but cannot be resized
int ug_container_popup(ug_ctx_t *ctx, const char *name, ug_rect_t rect); 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 // a menu bar is a container of fixed height, cannot be resized and sits at the

Loading…
Cancel
Save