diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-06-29 00:39:06 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-06-29 00:39:06 +0200 |
| commit | cff35e88f5741c2295b85fa6e62f391a69f5b41f (patch) | |
| tree | 73e5abc406e2e70a5d35301c0d6a022cb7cd1951 | |
| parent | 23f24016da8e019de747bbc513202240b0d0ebba (diff) | |
gui: implement {h,v}centered
| -rw-r--r-- | src/gui/inc/gui.h | 3 | ||||
| -rw-r--r-- | src/gui/src/button.c | 10 | ||||
| -rw-r--r-- | src/gui/src/gui.c | 40 | ||||
| -rw-r--r-- | src/gui/src/label.c | 9 | ||||
| -rw-r--r-- | src/gui/src/rounded_rect.c | 11 |
5 files changed, 70 insertions, 3 deletions
diff --git a/src/gui/inc/gui.h b/src/gui/inc/gui.h index ec87f45..6678b1b 100644 --- a/src/gui/inc/gui.h +++ b/src/gui/inc/gui.h @@ -15,8 +15,9 @@ struct gui_common int (*update)(struct gui_common *, const union peripheral *, const struct camera *); int (*render)(const struct gui_common *); + void (*get_dim)(const struct gui_common *, short *w, short *h); short x, y; - bool centered; + bool hcentered, vcentered; struct gui_common *parent, *child, *sibling; }; diff --git a/src/gui/src/button.c b/src/gui/src/button.c index eed744c..552a49c 100644 --- a/src/gui/src/button.c +++ b/src/gui/src/button.c @@ -151,12 +151,22 @@ static int update(struct gui_common *const g, return 0; } +static void get_dim(const struct gui_common *const g, + short *const w, short *const h) +{ + const struct gui_button *const b = (const struct gui_button *)g; + + *w = b->w; + *h = refs[GUI_BUTTON_MID].h; +} + void gui_button_init(struct gui_button *const b) { *b = (const struct gui_button) { .common = { + .get_dim = get_dim, .update = update, .render = render } diff --git a/src/gui/src/gui.c b/src/gui/src/gui.c index 044820a..0a937f2 100644 --- a/src/gui/src/gui.c +++ b/src/gui/src/gui.c @@ -1,5 +1,33 @@ #include <gui.h> +static void get_centered(const struct gui_common *const g, + short *const x, short *const y) +{ + if (g->get_dim) + { + short w, h, pw = 0, ph = 0; + + if (g->parent) + { + if (g->parent->get_dim) + g->parent->get_dim(g->parent, &pw, &ph); + } + else + { + pw = screen_w; + ph = screen_h; + } + + g->get_dim(g, &w, &h); + + if (g->hcentered) + *x += (pw - w) / 2; + + if (g->vcentered) + *y += (ph - h) / 2; + } +} + void gui_coords(const struct gui_common *const g, short *const x, short *const y) { @@ -8,9 +36,17 @@ void gui_coords(const struct gui_common *const g, short *const x, for (const struct gui_common *p = g->parent; p; p = p->parent) { - *x += p->x; - *y += p->y; + short px = p->x, py = p->y; + + if (p->hcentered || p->vcentered) + gui_coords(p, &px, &py); + + *x += px; + *y += py; } + + if (g->hcentered || g->vcentered) + get_centered(g, x, y); } void gui_add_sibling(struct gui_common *const g, diff --git a/src/gui/src/label.c b/src/gui/src/label.c index 2a24d08..51bacd6 100644 --- a/src/gui/src/label.c +++ b/src/gui/src/label.c @@ -13,12 +13,21 @@ static int render(const struct gui_common *const g) return l->text ? font_puts(l->font, x, y, l->text) : -1; } +static void get_dim(const struct gui_common *const g, + short *const w, short *const h) +{ + const struct gui_label *const l = (const struct gui_label *)g; + + font_dim(l->font, l->text, w, h); +} + void gui_label_init(struct gui_label *const l) { *l = (const struct gui_label) { .common = { + .get_dim = get_dim, .render = render } }; diff --git a/src/gui/src/rounded_rect.c b/src/gui/src/rounded_rect.c index 1ad0902..857d273 100644 --- a/src/gui/src/rounded_rect.c +++ b/src/gui/src/rounded_rect.c @@ -212,12 +212,23 @@ static int render(const struct gui_common *const g) return 0; } +static void get_dim(const struct gui_common *const g, short *const w, + short *const h) +{ + const struct gui_rounded_rect *const r = + (const struct gui_rounded_rect *)g; + + *w = r->w; + *h = r->h; +} + void gui_rounded_rect_init(struct gui_rounded_rect *const r) { *r = (const struct gui_rounded_rect) { .common = { + .get_dim = get_dim, .render = render } }; |
