109 lines
2.2 KiB
C
109 lines
2.2 KiB
C
#include <gui.h>
|
|
|
|
static void get_centered(const struct gui_common *const g,
|
|
short *const x, short *const y)
|
|
{
|
|
if (g->cb && g->cb->get_dim)
|
|
{
|
|
short w, h, pw = 0, ph = 0;
|
|
|
|
if (g->parent)
|
|
{
|
|
if (g->parent->cb && g->parent->cb->get_dim)
|
|
g->parent->cb->get_dim(g->parent, &pw, &ph);
|
|
}
|
|
else
|
|
{
|
|
pw = screen_w;
|
|
ph = screen_h;
|
|
}
|
|
|
|
g->cb->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)
|
|
{
|
|
*x = g->x + g->xoff;
|
|
*y = g->y + g->yoff;
|
|
|
|
const struct gui_common *p = g->parent;
|
|
|
|
if (p)
|
|
{
|
|
short px = p->x + p->xoff, py = p->y + p->yoff;
|
|
|
|
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,
|
|
struct gui_common *const s)
|
|
{
|
|
for (struct gui_common *c = g; c; c = c->sibling)
|
|
if (!c->sibling)
|
|
{
|
|
c->sibling = s;
|
|
break;
|
|
}
|
|
}
|
|
|
|
void gui_add_child(struct gui_common *const p,
|
|
struct gui_common *const c)
|
|
{
|
|
if (p->child)
|
|
gui_add_sibling(p->child, c);
|
|
else
|
|
p->child = c;
|
|
|
|
c->parent = p;
|
|
|
|
if (p->cb && p->cb->add_child)
|
|
p->cb->add_child(p, c);
|
|
}
|
|
|
|
int gui_update(struct gui_common *const g, const union peripheral *const p,
|
|
const struct camera *const c)
|
|
{
|
|
if (g->cb && g->cb->update && g->cb->update(g, p, c))
|
|
return -1;
|
|
|
|
if (g->child && gui_update(g->child, p, c))
|
|
return -1;
|
|
|
|
for (struct gui_common *s = g->sibling; s; s = s->sibling)
|
|
if (gui_update(s, p, c))
|
|
return -1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int gui_render(const struct gui_common *const g)
|
|
{
|
|
if (g->cb && g->cb->render && g->cb->render(g))
|
|
return -1;
|
|
|
|
if (g->child && gui_render(g->child))
|
|
return -1;
|
|
|
|
for (struct gui_common *s = g->sibling; s; s = s->sibling)
|
|
if (gui_render(s))
|
|
return -1;
|
|
|
|
return 0;
|
|
}
|