resize the context when adding fixed containers

master
Alessandro Mauri 2 years ago
parent 288bd38b42
commit cdf3f3f855
  1. 13
      test/main.c
  2. 55
      ugui.c
  3. 4
      ugui.h

@ -175,14 +175,17 @@ int main(void)
ug_frame_begin(ctx); ug_frame_begin(ctx);
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)});
//
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_sidebar(ctx, "Right Sidebar", (ug_size_t)SIZE_PX(300), UG_SIDE_RIGHT); ug_container_sidebar(ctx, "Right Sidebar", (ug_size_t)SIZE_PX(300), UG_SIDE_RIGHT);
ug_container_sidebar(ctx, "Left Sidebar", (ug_size_t)SIZE_PX(200), UG_SIDE_LEFT);
ug_container_sidebar(ctx, "Bottom Sidebar", (ug_size_t)SIZE_MM(10), UG_SIDE_BOTTOM); ug_container_sidebar(ctx, "Bottom Sidebar", (ug_size_t)SIZE_MM(10), UG_SIDE_BOTTOM);
ug_container_sidebar(ctx, "Top Sidebar", (ug_size_t)SIZE_MM(40), UG_SIDE_TOP);
ug_frame_end(ctx); ug_frame_end(ctx);
// fill background // fill background

@ -342,8 +342,10 @@ static void position_container(ug_ctx_t *ctx, ug_container_t *cnt)
int bt = s->cnt.border.t.size.i; int bt = s->cnt.border.t.size.i;
int bb = s->cnt.border.b.size.i; int bb = s->cnt.border.b.size.i;
int hh = s->cnt.titlebar.height.size.i; int hh = s->cnt.titlebar.height.size.i;
int cw = ctx->size.w; int cx = ctx->origin.x;
int ch = ctx->size.h; int cy = ctx->origin.y;
int cw = ctx->origin.w;
int ch = ctx->origin.h;
// handle relative sizes // handle relative sizes
if (rect->w == 0) rca->w = cw; if (rect->w == 0) rca->w = cw;
@ -352,21 +354,46 @@ static void position_container(ug_ctx_t *ctx, ug_container_t *cnt)
else if (TEST(cnt->flags, UG_CNT_MOVABLE)) rca->h += hh + 2*bt + bb; else if (TEST(cnt->flags, UG_CNT_MOVABLE)) rca->h += hh + 2*bt + bb;
else rca->h += bt + bb; else rca->h += bt + bb;
// 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_MOVABLE)) { if (!TEST(cnt->flags, UG_CNT_MOVABLE)) {
printf("origin: x=%d y=%d w=%d h=%d\t", ctx->origin.x, ctx->origin.y, ctx->origin.w, ctx->origin.h);
// <0 -> relative to the right margin // <0 -> relative to the right margin
if (rect->x <= 0) rca->x = cw - rca->w + rca->x; if (rect->x < 0) rca->x = cx + cw - rca->w + rca->x + 1;
if (rect->y <= 0) rca->y = ch - rca->h + rca->y; else rca->x = cx;
if (rect->y < 0) rca->y = cy + ch - rca->h + rca->y + 1;
else rca->y = cy;
// 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 {
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;
}
}
printf("new origin: x=%d y=%d w=%d h=%d\n", ctx->origin.x, ctx->origin.y, ctx->origin.w, ctx->origin.h);
} }
} }
void handle_container(ug_ctx_t *ctx, ug_container_t *cnt) void handle_container(ug_ctx_t *ctx, ug_container_t *cnt)
{ {
if (ctx->active.cnt != cnt->id) { // if we are not the currently active container than definately we are not
// if we are not the active container than definately we are not
// being moved or resized // being moved or resized
if (ctx->active.cnt != cnt->id) {
cnt->flags &= ~CNT_STATE_ALL; cnt->flags &= ~CNT_STATE_ALL;
return; return;
} }
@ -392,8 +419,6 @@ void handle_container(ug_ctx_t *ctx, ug_container_t *cnt)
ug_vec2_t mpos = ctx->mouse.pos; ug_vec2_t mpos = ctx->mouse.pos;
int minx, maxx, miny, maxy; int minx, maxx, miny, maxy;
printf("state: %x\n", (cnt->flags & CNT_STATE_ALL)>>16);
// handle movable windows // handle movable windows
if (TEST(cnt->flags, UG_CNT_MOVABLE)) { if (TEST(cnt->flags, UG_CNT_MOVABLE)) {
minx = rca->x + bl; minx = rca->x + bl;
@ -593,7 +618,7 @@ int ug_container_sidebar(ug_ctx_t *ctx, const char *name, ug_size_t size, int si
case UG_SIDE_BOTTOM: case UG_SIDE_BOTTOM:
cnt->flags |= UG_CNT_RESIZE_TOP; cnt->flags |= UG_CNT_RESIZE_TOP;
// FIXME: we do not support relative zero position yet // FIXME: we do not support relative zero position yet
rect.y = 0; rect.y = -1;
rect.h = size_to_px(ctx, size); rect.h = size_to_px(ctx, size);
break; break;
case UG_SIDE_TOP: case UG_SIDE_TOP:
@ -602,7 +627,7 @@ int ug_container_sidebar(ug_ctx_t *ctx, const char *name, ug_size_t size, int si
break; break;
case UG_SIDE_RIGHT: case UG_SIDE_RIGHT:
cnt->flags |= UG_CNT_RESIZE_LEFT; cnt->flags |= UG_CNT_RESIZE_LEFT;
rect.x = 0; rect.x = -1;
rect.w = size_to_px(ctx, size); rect.w = size_to_px(ctx, size);
break; break;
case UG_SIDE_LEFT: case UG_SIDE_LEFT:
@ -683,6 +708,12 @@ int ug_frame_begin(ug_ctx_t *ctx)
// clear command stack // clear command stack
RESET_STACK(ctx->cmd_stack); RESET_STACK(ctx->cmd_stack);
// update the origin
ctx->origin.x = 0;
ctx->origin.y = 0;
ctx->origin.w = ctx->size.w;
ctx->origin.h = ctx->size.h;
// update hover index // update hover index
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++) {
@ -696,9 +727,11 @@ int ug_frame_begin(ug_ctx_t *ctx)
if (MOUSEDOWN(ctx, UG_BTN_LEFT)) { if (MOUSEDOWN(ctx, UG_BTN_LEFT)) {
ctx->active.cnt = ctx->hover.cnt; ctx->active.cnt = ctx->hover.cnt;
} else if (MOUSEUP(ctx, UG_BTN_LEFT)) { } else if (MOUSEUP(ctx, UG_BTN_LEFT)) {
ctx->last_active.cnt = ctx->active.cnt;
ctx->active.cnt = 0; ctx->active.cnt = 0;
} }
printf("active: %x\n", ctx->active.cnt);
//printf("active (%x) last active (%x)\n",ctx->active.cnt, ctx->last_active.cnt);
return 0; return 0;
} }

@ -151,6 +151,7 @@ typedef struct {
// containers need to know how big the "main container" is so that all // containers need to know how big the "main container" is so that all
// the relative positioning work // the relative positioning work
ug_vec2_t size; ug_vec2_t size;
ug_rect_t origin;
// which context and element we are hovering // which context and element we are hovering
struct { struct {
ug_id_t cnt, elem; ug_id_t cnt, elem;
@ -163,6 +164,9 @@ typedef struct {
struct { struct {
ug_id_t cnt, elem; ug_id_t cnt, elem;
} active; } active;
struct {
ug_id_t cnt, elem;
} last_active;
// count the frames for fun // count the frames for fun
unsigned long int frame; unsigned long int frame;
// mouse data // mouse data

Loading…
Cancel
Save