gui: implement {h,v}centered

This commit is contained in:
Xavier Del Campo Romero 2022-06-29 00:39:06 +02:00
parent 23f24016da
commit cff35e88f5
5 changed files with 70 additions and 3 deletions

View File

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

View File

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

View File

@ -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,

View File

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

View File

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