diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-07-10 00:21:09 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-07-10 00:26:48 +0200 |
| commit | f69dc6f359015e992d181c60cd815cefd6c62aad (patch) | |
| tree | 67e47e8862884f6e66b5bb745ff0b964c93a9124 /src/gui | |
| parent | f318a66f8ccc847bd9940d125936aa87df74fb0f (diff) | |
| download | jancity-f69dc6f359015e992d181c60cd815cefd6c62aad.tar.gz | |
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.
Diffstat (limited to 'src/gui')
| -rw-r--r-- | src/gui/inc/gui.h | 2 | ||||
| -rw-r--r-- | src/gui/src/container.c | 27 | ||||
| -rw-r--r-- | src/gui/src/gui.c | 6 |
3 files changed, 28 insertions, 7 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 <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) 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); |
