diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-09-27 17:03:06 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-11-01 16:26:16 +0100 |
| commit | 980858186149651df5543b6fc99a4f7db0cdd089 (patch) | |
| tree | d347200b0a562d84df505097651ad0642f207fdd /src/gui | |
| parent | 39f50e601d395bbd2d78d0147ac530b756da2fff (diff) | |
| download | jancity-980858186149651df5543b6fc99a4f7db0cdd089.tar.gz | |
WIP
Diffstat (limited to 'src/gui')
| -rw-r--r-- | src/gui/inc/gui/rounded_rect.h | 7 | ||||
| -rw-r--r-- | src/gui/src/button_type1.c | 5 | ||||
| -rw-r--r-- | src/gui/src/line_edit.c | 7 | ||||
| -rw-r--r-- | src/gui/src/rounded_rect.c | 58 |
4 files changed, 72 insertions, 5 deletions
diff --git a/src/gui/inc/gui/rounded_rect.h b/src/gui/inc/gui/rounded_rect.h index e1203e4..13242f4 100644 --- a/src/gui/inc/gui/rounded_rect.h +++ b/src/gui/inc/gui/rounded_rect.h @@ -15,11 +15,9 @@ struct gui_rounded_rect { struct gui_common common; unsigned short w, h; + bool adjust; }; -UTIL_STATIC_ASSERT(!offsetof(struct gui_rounded_rect, common), - "unexpected offset for struct gui_rounded_rect"); - void gui_rounded_rect_init(struct gui_rounded_rect *r); enum @@ -36,6 +34,9 @@ enum extern struct sprite gui_rounded_rect_sprites[MAX_GUI_ROUNDED_RECT_SPRITES]; +UTIL_STATIC_ASSERT(!offsetof(struct gui_rounded_rect, common), + "unexpected offset for struct gui_rounded_rect"); + #ifdef __cplusplus } #endif diff --git a/src/gui/src/button_type1.c b/src/gui/src/button_type1.c index 16038b5..f07f2f3 100644 --- a/src/gui/src/button_type1.c +++ b/src/gui/src/button_type1.c @@ -2,6 +2,7 @@ #include <gui/button.h> #include <gui_private.h> #include <gui_button_private.h> +#include <stdio.h> struct sprite gui_button_sprites[MAX_GUI_BUTTON_SPRITES]; @@ -63,7 +64,11 @@ static int render_mid(const struct gui_button *const b, } } else + { + fprintf(stderr, "%s: unexpected negative size for button %p\n", + __func__, (void *)b); return -1; + } return 0; } diff --git a/src/gui/src/line_edit.c b/src/gui/src/line_edit.c index ef37708..df52fd0 100644 --- a/src/gui/src/line_edit.c +++ b/src/gui/src/line_edit.c @@ -130,7 +130,10 @@ static void on_char(const char ch, void *const user) allowed = true; if (allowed && l->i + 1 < l->sz) + { l->text[l->i++] = ch; + l->text[l->i] = '\0'; + } } static void on_erase(void *const user) @@ -196,11 +199,11 @@ void gui_line_edit_init(struct gui_line_edit *const l, char *const buf, }, .text = buf, - .sz = sz + .sz = sz, + .i = strlen(buf) }; gui_label_init(&l->label); - memset(l->text, '\0', l->sz); l->label.common.hcentered = true; l->label.common.vcentered = true; l->label.text = l->text; diff --git a/src/gui/src/rounded_rect.c b/src/gui/src/rounded_rect.c index cd2bbff..b60a05a 100644 --- a/src/gui/src/rounded_rect.c +++ b/src/gui/src/rounded_rect.c @@ -222,11 +222,69 @@ static void get_dim(const struct gui_common *const g, short *const w, *h = r->h; } +static void add_child(struct gui_common *const parent, + struct gui_common *const child) +{ + struct gui_rounded_rect *const r = (struct gui_rounded_rect *)parent; + + if (!r->adjust || child->hidden || !child->cb || !child->cb->get_dim) + return; + + short w, h; + const short ref_w = refs[GUI_ROUNDED_RECT_MID_VERT].w, + ref_h = refs[GUI_ROUNDED_RECT_MID].h, + min_w = ref_w * 2 + refs[GUI_ROUNDED_RECT_MID].w, + min_h = ref_h * 2 + refs[GUI_ROUNDED_RECT_MID_VERT].h; + + child->cb->get_dim(child, &w, &h); + + if (w > r->w - (ref_w * 2)) + r->w = w + (ref_w * 2); + + if (h > r->h - (ref_h * 2)) + r->h = h + (ref_h * 2); + + if (r->w < min_w) + r->w = min_w; + + if (r->h < min_h) + r->h = min_h; + + if (!child->hcentered) + child->xoff = ref_w; + + if (!child->vcentered) + child->yoff = ref_h; +} + +static int update(struct gui_common *const g, + const union peripheral *const p, const struct camera *const cam, + struct input *const in) +{ + struct gui_rounded_rect *const r = (struct gui_rounded_rect *)g; + + if (r->adjust) + { + r->w = r->h = 0; + + struct gui_common *const child = r->common.child; + + if (child && child->cb && child->cb->get_dim) + add_child(&r->common, child); + + for (struct gui_common *s = child->sibling; s; s = s->sibling) + add_child(&r->common, s); + } + + return 0; +} + void gui_rounded_rect_init(struct gui_rounded_rect *const r) { static const struct gui_common_cb cb = { .get_dim = get_dim, + .update = update, .render = render }; |
