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.
This commit is contained in:
Xavier Del Campo Romero 2022-07-10 00:21:09 +02:00
parent 08951d4af5
commit e636accfd5
4 changed files with 32 additions and 10 deletions

View File

@ -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;
};

View File

@ -1,5 +1,7 @@
#include <gui/container.h>
#include <gui.h>
#include <camera.h>
#include <peripheral.h>
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)

View File

@ -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);

View File

@ -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);