From d989dc6f49f0136ea69c10cdbaf3b4cdc8e31b6b Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Fri, 23 Sep 2022 04:15:48 +0200 Subject: [PATCH] gui: Allow elements to be hidden When a GUI element is hidden, no rendering or updating is done to it or its children. This can be useful to define a complex GUI tree structure that changes under specific conditions, without redefining it. --- src/gui/inc/gui.h | 2 +- src/gui/src/container.c | 13 ++++++++----- src/gui/src/gui.c | 35 +++++++++++++++++++++++------------ 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/gui/inc/gui.h b/src/gui/inc/gui.h index 72b2fb0..b24984d 100644 --- a/src/gui/inc/gui.h +++ b/src/gui/inc/gui.h @@ -24,7 +24,7 @@ struct gui_common } *cb; short x, y, xoff, yoff; - bool hcentered, vcentered; + bool hidden, hcentered, vcentered; struct gui_common *parent, *child, *sibling; }; diff --git a/src/gui/src/container.c b/src/gui/src/container.c index e7d4049..a8c04b4 100644 --- a/src/gui/src/container.c +++ b/src/gui/src/container.c @@ -7,7 +7,7 @@ static void add_child(struct gui_common *const parent, struct gui_common *const child) { - if (!child->cb || !child->cb->get_dim) + if (child->hidden || !child->cb || !child->cb->get_dim) return; struct gui_container *const c = (struct gui_container *)parent; @@ -62,11 +62,14 @@ static int update(struct gui_common *const g, struct gui_common *const child = c->common.child; - if (child->cb->get_dim) - add_child(&c->common, child); + if (child && child->cb) + { + 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); + for (struct gui_common *s = child->sibling; s; s = s->sibling) + add_child(&c->common, s); + } return 0; } diff --git a/src/gui/src/gui.c b/src/gui/src/gui.c index 9b79083..199b4a7 100644 --- a/src/gui/src/gui.c +++ b/src/gui/src/gui.c @@ -6,14 +6,15 @@ static void get_centered(const struct gui_common *const g, short *const x, short *const y) { - if (g->cb && g->cb->get_dim) + if (!g->hidden && g->cb && g->cb->get_dim) { short w, h, pw = 0, ph = 0; + const struct gui_common *p = g->parent; - if (g->parent) + if (p) { - if (g->parent->cb && g->parent->cb->get_dim) - g->parent->cb->get_dim(g->parent, &pw, &ph); + if (!p->hidden && p->cb && p->cb->get_dim) + p->cb->get_dim(p, &pw, &ph); } else { @@ -34,6 +35,9 @@ 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) { + if (g->hidden) + return; + *x = g->x + g->xoff; *y = g->y + g->yoff; @@ -79,6 +83,9 @@ bool gui_pressed(const struct gui_common *const g, const union peripheral *const p, const struct camera *cam, const short w, const short h) { + if (g->hidden) + return false; + bool check = false; switch (p->common.type) @@ -105,6 +112,9 @@ bool gui_released(const struct gui_common *const g, const union peripheral *const p, const struct camera *cam, const short w, const short h) { + if (g->hidden) + return false; + bool check = false; switch (p->common.type) @@ -155,13 +165,14 @@ void gui_add_child(struct gui_common *const p, int gui_update(struct gui_common *const g, const union peripheral *const p, const struct camera *const c, struct input *const in) { - if ((g->child && gui_update(g->child, p, c, in)) - || (g->cb && g->cb->update && g->cb->update(g, p, c, in))) + if (!g->hidden + && ((g->child && gui_update(g->child, p, c, in)) + || (g->cb && g->cb->update && g->cb->update(g, p, c, in)))) return -1; for (struct gui_common *s = g->sibling; s; s = s->sibling) - if ((s->child && gui_update(s->child, p, c, in)) - || (s->cb && s->cb->update && s->cb->update(s, p, c, in))) + if (!s->hidden && ((s->child && gui_update(s->child, p, c, in)) + || (s->cb && s->cb->update && s->cb->update(s, p, c, in)))) return -1; return 0; @@ -169,13 +180,13 @@ int gui_update(struct gui_common *const g, const union peripheral *const p, int gui_render(const struct gui_common *const g) { - if ((g->cb && g->cb->render && g->cb->render(g)) - || (g->child && gui_render(g->child))) + if (!g->hidden && ((g->cb && g->cb->render && g->cb->render(g)) + || (g->child && gui_render(g->child)))) return -1; for (struct gui_common *s = g->sibling; s; s = s->sibling) - if ((s->cb && s->cb->render && s->cb->render(s)) - || (s->child && gui_render(s->child))) + if (!s->hidden && ((s->cb && s->cb->render && s->cb->render(s)) + || (s->child && gui_render(s->child)))) return -1; return 0;