gui: reuse callback data
There is no need to allocate memory for these callbacks for each single GUI element. Instead, a single, statically-allocated instance can be shared among all GUI elements of a given type.
This commit is contained in:
parent
a71ce37929
commit
b0eb562b9b
|
@ -12,11 +12,15 @@ extern "C"
|
|||
|
||||
struct gui_common
|
||||
{
|
||||
void (*add_child)(struct gui_common *parent, struct gui_common *child);
|
||||
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);
|
||||
const struct gui_common_cb
|
||||
{
|
||||
void (*add_child)(struct gui_common *parent, struct gui_common *child);
|
||||
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);
|
||||
} *cb;
|
||||
|
||||
short x, y;
|
||||
bool hcentered, vcentered;
|
||||
struct gui_common *parent, *child, *sibling;
|
||||
|
|
|
@ -74,11 +74,16 @@ static int render(const struct gui_common *const g)
|
|||
|
||||
void gui_bar_init(struct gui_bar *const b)
|
||||
{
|
||||
static const struct gui_common_cb cb =
|
||||
{
|
||||
.render = render
|
||||
};
|
||||
|
||||
*b = (const struct gui_bar)
|
||||
{
|
||||
.common =
|
||||
{
|
||||
.render = render
|
||||
.cb = &cb
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -164,13 +164,18 @@ static void get_dim(const struct gui_common *const g,
|
|||
|
||||
void gui_button_init(struct gui_button *const b)
|
||||
{
|
||||
static const struct gui_common_cb cb =
|
||||
{
|
||||
.get_dim = get_dim,
|
||||
.update = update,
|
||||
.render = render
|
||||
};
|
||||
|
||||
*b = (const struct gui_button)
|
||||
{
|
||||
.common =
|
||||
{
|
||||
.get_dim = get_dim,
|
||||
.update = update,
|
||||
.render = render
|
||||
.cb = &cb
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -4,13 +4,13 @@
|
|||
static void add_child(struct gui_common *const parent,
|
||||
struct gui_common *const child)
|
||||
{
|
||||
if (!child->get_dim)
|
||||
if (!child->cb || !child->cb->get_dim)
|
||||
return;
|
||||
|
||||
struct gui_container *const c = (struct gui_container *)parent;
|
||||
short w, h;
|
||||
|
||||
child->get_dim(child, &w, &h);
|
||||
child->cb->get_dim(child, &w, &h);
|
||||
|
||||
switch (c->mode)
|
||||
{
|
||||
|
@ -45,12 +45,17 @@ static void get_dim(const struct gui_common *const g,
|
|||
|
||||
void gui_container_init(struct gui_container *const c)
|
||||
{
|
||||
static const struct gui_common_cb cb =
|
||||
{
|
||||
.add_child = add_child,
|
||||
.get_dim = get_dim
|
||||
};
|
||||
|
||||
*c = (const struct gui_container)
|
||||
{
|
||||
.common =
|
||||
{
|
||||
.add_child = add_child,
|
||||
.get_dim = get_dim
|
||||
.cb = &cb
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -3,14 +3,14 @@
|
|||
static void get_centered(const struct gui_common *const g,
|
||||
short *const x, short *const y)
|
||||
{
|
||||
if (g->get_dim)
|
||||
if (g->cb && g->cb->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);
|
||||
if (g->parent->cb && g->parent->cb->get_dim)
|
||||
g->parent->cb->get_dim(g->parent, &pw, &ph);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -18,7 +18,7 @@ static void get_centered(const struct gui_common *const g,
|
|||
ph = screen_h;
|
||||
}
|
||||
|
||||
g->get_dim(g, &w, &h);
|
||||
g->cb->get_dim(g, &w, &h);
|
||||
|
||||
if (g->hcentered)
|
||||
*x += (pw - w) / 2;
|
||||
|
@ -72,14 +72,14 @@ void gui_add_child(struct gui_common *const p,
|
|||
|
||||
c->parent = p;
|
||||
|
||||
if (p->add_child)
|
||||
p->add_child(p, c);
|
||||
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->update && g->update(g, p, c))
|
||||
if (g->cb && g->cb->update && g->cb->update(g, p, c))
|
||||
return -1;
|
||||
|
||||
if (g->child && gui_update(g->child, p, c))
|
||||
|
@ -94,7 +94,7 @@ int gui_update(struct gui_common *const g, const union peripheral *const p,
|
|||
|
||||
int gui_render(const struct gui_common *const g)
|
||||
{
|
||||
if (g->render && g->render(g))
|
||||
if (g->cb && g->cb->render && g->cb->render(g))
|
||||
return -1;
|
||||
|
||||
if (g->child && gui_render(g->child))
|
||||
|
|
|
@ -23,12 +23,17 @@ static void get_dim(const struct gui_common *const g,
|
|||
|
||||
void gui_label_init(struct gui_label *const l)
|
||||
{
|
||||
static const struct gui_common_cb cb =
|
||||
{
|
||||
.get_dim = get_dim,
|
||||
.render = render
|
||||
};
|
||||
|
||||
*l = (const struct gui_label)
|
||||
{
|
||||
.common =
|
||||
{
|
||||
.get_dim = get_dim,
|
||||
.render = render
|
||||
.cb = &cb
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -83,11 +83,16 @@ static int render(const struct gui_common *const g)
|
|||
|
||||
void gui_progress_bar_init(struct gui_progress_bar *const pb)
|
||||
{
|
||||
static const struct gui_common_cb cb =
|
||||
{
|
||||
.render = render
|
||||
};
|
||||
|
||||
*pb = (const struct gui_progress_bar)
|
||||
{
|
||||
.common =
|
||||
{
|
||||
.render = render
|
||||
.cb = &cb
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -224,12 +224,17 @@ static void get_dim(const struct gui_common *const g, short *const w,
|
|||
|
||||
void gui_rounded_rect_init(struct gui_rounded_rect *const r)
|
||||
{
|
||||
static const struct gui_common_cb cb =
|
||||
{
|
||||
.get_dim = get_dim,
|
||||
.render = render
|
||||
};
|
||||
|
||||
*r = (const struct gui_rounded_rect)
|
||||
{
|
||||
.common =
|
||||
{
|
||||
.get_dim = get_dim,
|
||||
.render = render
|
||||
.cb = &cb
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue