Compare commits

..

2 Commits

Author SHA1 Message Date
46c5de8dcb container_body 2022-12-10 17:06:41 +01:00
5d1d5024ec menu bar 2022-12-10 16:51:27 +01:00
3 changed files with 95 additions and 27 deletions

View File

@ -175,6 +175,8 @@ int main(void)
ug_frame_begin(ctx); ug_frame_begin(ctx);
ug_container_menu_bar(ctx, "Menu fichissimo", (ug_size_t)SIZE_PX(24));
ug_container_floating(ctx, "stupid name", 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_div_t){.x=SIZE_PX(0), .y=SIZE_PX(0), .w=SIZE_PX(100), .h=SIZE_MM(75.0)});
@ -189,6 +191,9 @@ int main(void)
ug_container_floating(ctx, "stupid er", ug_container_floating(ctx, "stupid er",
(ug_div_t){.x=SIZE_PX(150), .y=SIZE_PX(-100), .w=SIZE_PX(100), .h=SIZE_MM(75.0)}); (ug_div_t){.x=SIZE_PX(150), .y=SIZE_PX(-100), .w=SIZE_PX(100), .h=SIZE_MM(75.0)});
ug_container_body(ctx, "Main Body");
if (ug_container_body(ctx, "Other Body"))
printf("No space!\n");
ug_frame_end(ctx); ug_frame_end(ctx);

109
ugui.c
View File

@ -188,8 +188,8 @@ ug_ctx_t *ug_ctx_new(void)
memset(ctx, 0, sizeof(ug_ctx_t)); memset(ctx, 0, sizeof(ug_ctx_t));
ctx->style = &default_style; ctx->style = &default_style;
ctx->style_px = &style_cache;\ ctx->style_px = &style_cache;
// NOTE: this fixes a bug where for the first frame the container stack\ // NOTE: this fixes a bug where for the first frame the container stack
// doesn't get sorted, but it is kind of a botch // 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); ug_ctx_set_displayinfo(ctx, DEF_SCALE, DEF_PPI);
@ -323,9 +323,9 @@ static void sort_containers(ug_ctx_t *ctx)
ctx->cnt_stack.sorted = 1; ctx->cnt_stack.sorted = 1;
} }
// update the container dimensions and position according to the context information,
// also handle resizing, moving, ect. if allowed by the container // update the container position in the context area
static void position_container(ug_ctx_t *ctx, ug_container_t *cnt) static int position_container(ug_ctx_t *ctx, ug_container_t *cnt)
{ {
ug_rect_t *rect, *rca; ug_rect_t *rect, *rca;
rect = &cnt->rect; rect = &cnt->rect;
@ -394,6 +394,9 @@ static void position_container(ug_ctx_t *ctx, ug_container_t *cnt)
// if the container is not fixed than it can have positions outside of the // if the container is not fixed than it can have positions outside of the
// main window, thus negative // main window, thus negative
if (!TEST(cnt->flags, UG_CNT_FLOATING)) { if (!TEST(cnt->flags, UG_CNT_FLOATING)) {
// if there is no space left propagate the error
if (!ctx->origin.w || !ctx->origin.h)
return -1;
// <0 -> relative to the right margin // <0 -> relative to the right margin
if (rect->x < 0) rca->x = cx + cw - rca->w + rca->x + 1; if (rect->x < 0) rca->x = cx + cw - rca->w + rca->x + 1;
else rca->x = cx; else rca->x = cx;
@ -402,24 +405,18 @@ static void position_container(ug_ctx_t *ctx, ug_container_t *cnt)
// if the container is fixed than update the available space in // if the container is fixed than update the available space in
// the context // the context
if (rect->y >= 0) { if (rect->w) {
if (rect->x < 0) { ctx->origin.x = rect->x >= 0 ? rca->x + rca->w : ctx->origin.x;
ctx->origin.w = rca->x;
} else {
ctx->origin.x = rca->x + rca->w;
ctx->origin.w -= rca->w; ctx->origin.w -= rca->w;
} } else if (rect->h) {
} ctx->origin.y = rect->y >= 0 ? rca->y + rca->h : ctx->origin.y;
ctx->origin.h -= rca->h;
if (rect->x >= 0) {
if (rect->y < 0) {
ctx->origin.h = rca->y;
} else { } else {
ctx->origin.y = rca->y + rca->h; // width and height zero means fill everything
ctx->origin.y -= rca->h; ctx->origin.w = ctx->origin.h = 0;
}
} }
} }
return 0;
} }
@ -625,7 +622,9 @@ int ug_container_floating(ug_ctx_t *ctx, const char *name, ug_div_t div)
UG_CNT_SCROLL_X | UG_CNT_SCROLL_Y ; UG_CNT_SCROLL_X | UG_CNT_SCROLL_Y ;
} }
position_container(ctx, cnt); // TODO: what about error codes and meaning?
if (position_container(ctx, cnt))
return -1;
handle_container(ctx, cnt); handle_container(ctx, cnt);
return 0; return 0;
@ -678,10 +677,74 @@ int ug_container_sidebar(ug_ctx_t *ctx, const char *name, ug_size_t size, int si
cnt->rect = rect; cnt->rect = rect;
} }
position_container(ctx, cnt); if (position_container(ctx, cnt))
return -1;
handle_container(ctx, cnt); handle_container(ctx, cnt);
// TODO: change available context space to reflect adding a sidebar 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);
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;
}
if (position_container(ctx, cnt))
return -1;
handle_container(ctx, cnt);
return 0;
}
int ug_container_body(ug_ctx_t *ctx, const char *name)
{
TEST_CTX(ctx);
ug_id_t id = 0;
if (name) {
id = hash(name, strlen(name));
} else {
// sorry but body must have a name
return -1;
}
ug_container_t *cnt = get_container(ctx, id);
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;
cnt->rect = (ug_rect_t){0};
}
if (position_container(ctx, cnt))
return -1;
handle_container(ctx, cnt);
return 0; return 0;
} }
@ -753,7 +816,7 @@ int ug_frame_begin(ug_ctx_t *ctx)
ctx->origin.h = ctx->size.h; ctx->origin.h = ctx->size.h;
// update hover index // update hover index
printf("Container Stack:\n"); printf("Container Stack: active %x\n", ctx->active.cnt);
ug_vec2_t v = ctx->mouse.pos; ug_vec2_t v = ctx->mouse.pos;
for (int i = 0; i < ctx->cnt_stack.idx; i++) { for (int i = 0; i < ctx->cnt_stack.idx; i++) {
printf("[%d]: %x\n", i, ctx->cnt_stack.items[i].id); printf("[%d]: %x\n", i, ctx->cnt_stack.items[i].id);

4
ugui.h
View File

@ -213,11 +213,11 @@ 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); 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
// top of the window // 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 // 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); 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 // a body is a container that scales with the window, sits at it's center and cannot
// be resized // be resized, it also fills all the available space
int ug_container_body(ug_ctx_t *ctx, const char *name); int ug_container_body(ug_ctx_t *ctx, const char *name);
// Input functions // Input functions