From e636accfd5b310165c0d7b5075f89a64583b5f1a Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Sun, 10 Jul 2022 00:21:09 +0200 Subject: [PATCH] Allow children of gui_container be resized anytime So far, their position inside the container was determined when initializing the interface. However, if a child were resized afterwards, the container would not adjust its elements accordingly. Moreover, the implementation for gui_container relied on hacking the children's X/Y coordinates, which could only be done once. Now, two additional members have been added to gui_common so that specific X/Y offset can be determined by the parent, additionally to the traditional rules followed by gui_coords. Despite the extra memory footprint, it now allows containers to set specific X/Y offsets for their children on every game cycle. --- src/gui/inc/gui.h | 2 +- src/gui/src/container.c | 27 ++++++++++++++++++++++++--- src/gui/src/gui.c | 6 +++--- src/menu/src/gamecfg_menu.c | 7 ++++--- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/gui/inc/gui.h b/src/gui/inc/gui.h index 18ed37b..84a6c66 100644 --- a/src/gui/inc/gui.h +++ b/src/gui/inc/gui.h @@ -21,7 +21,7 @@ struct gui_common void (*get_dim)(const struct gui_common *, short *w, short *h); } *cb; - short x, y; + short x, y, xoff, yoff; bool hcentered, vcentered; struct gui_common *parent, *child, *sibling; }; diff --git a/src/gui/src/container.c b/src/gui/src/container.c index f026741..ad3b901 100644 --- a/src/gui/src/container.c +++ b/src/gui/src/container.c @@ -1,5 +1,7 @@ #include #include +#include +#include static void add_child(struct gui_common *const parent, struct gui_common *const child) @@ -18,7 +20,7 @@ static void add_child(struct gui_common *const parent, if (c->w) c->w += c->spacing; - child->x += c->w; + child->xoff = c->w; c->w += w; if (h > c->h) @@ -30,7 +32,7 @@ static void add_child(struct gui_common *const parent, if (c->h) c->h += c->spacing; - child->y += c->h; + child->yoff = c->h; c->h += h; if (w > c->w) @@ -49,12 +51,31 @@ static void get_dim(const struct gui_common *const g, *h = c->h; } +static int update(struct gui_common *const g, + const union peripheral *const p, const struct camera *const cam) +{ + struct gui_container *const c = (struct gui_container *)g; + + c->w = c->h = 0; + + struct gui_common *const child = c->common.child; + + if (child->cb->get_dim) + add_child(&c->common, child); + + for (struct gui_common *s = child->sibling; s; s = s->sibling) + add_child(&c->common, s); + + return 0; +} + void gui_container_init(struct gui_container *const c) { static const struct gui_common_cb cb = { .add_child = add_child, - .get_dim = get_dim + .get_dim = get_dim, + .update = update }; *c = (const struct gui_container) diff --git a/src/gui/src/gui.c b/src/gui/src/gui.c index df00687..f65ed91 100644 --- a/src/gui/src/gui.c +++ b/src/gui/src/gui.c @@ -31,14 +31,14 @@ static void get_centered(const struct gui_common *const g, void gui_coords(const struct gui_common *const g, short *const x, short *const y) { - *x = g->x; - *y = g->y; + *x = g->x + g->xoff; + *y = g->y + g->yoff; const struct gui_common *p = g->parent; if (p) { - short px = p->x, py = p->y; + short px = p->x + p->xoff, py = p->y + p->yoff; if (p->hcentered || p->vcentered) gui_coords(p, &px, &py); diff --git a/src/menu/src/gamecfg_menu.c b/src/menu/src/gamecfg_menu.c index 31d105e..9fc3873 100644 --- a/src/menu/src/gamecfg_menu.c +++ b/src/menu/src/gamecfg_menu.c @@ -20,8 +20,11 @@ static int update(struct menu_common *const c, void *const arg) struct gamecfg_menu *const m = arg; m->bcnt.common.y = screen_h - 40; + m->r.w = screen_w / 2; + m->r.h = screen_h / 2; - if (gui_update(&m->bcnt.common, &c->p, &c->cam)) + if (gui_update(&m->cnt.common, &c->p, &c->cam) + || gui_update(&m->bcnt.common, &c->p, &c->cam)) return -1; return 0; @@ -48,8 +51,6 @@ int menu_gamecfg(struct menu_common *const c) m.cnt.common.vcentered = true; gui_rounded_rect_init(&m.r); - m.r.w = screen_w / 2; - m.r.h = screen_h / 2; gui_add_child(&m.cnt.common, &m.r.common); gui_container_init(&m.bcnt);