Mercurial > molko
changeset 612:297fa28cac90
ui: simplify back
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 20 Aug 2023 10:48:57 +0200 |
parents | f2aff78644db |
children | f76cada0bbb2 |
files | examples/example-label/example-label.c libmlk-ui/mlk/ui/gridmenu.c libmlk-ui/mlk/ui/label.c libmlk-ui/mlk/ui/label.h libmlk-ui/mlk/ui/ui_p.h |
diffstat | 5 files changed, 111 insertions(+), 194 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/example-label/example-label.c Sun Aug 20 10:47:46 2023 +0200 +++ b/examples/example-label/example-label.c Sun Aug 20 10:48:57 2023 +0200 @@ -31,16 +31,14 @@ #include <mlk/ui/align.h> #include <mlk/ui/label.h> -#include <mlk/ui/style.h> #include <mlk/ui/ui.h> #include <mlk/example/example.h> #include <mlk/example/glower.h> /* Custom delegate/style for glowing one. */ -static struct mlk_style style_glow; -static struct mlk_label_if delegate_glow; -static struct mlk_glower glower = { +static struct mlk_label_style style_glow; +static struct mlk_glower glow = { .start = 0xffce7fff, .end = 0xd58d6bff, .delay = 22 @@ -55,88 +53,69 @@ .label = { .text = "The world is Malikania.", .style = &style_glow, - .iface = &delegate_glow } }, { .align = MLK_ALIGN_TOP_LEFT, .label = { - .text = "Top left", - .style = &mlk_style, - .iface = &mlk_label_if + .text = "Top left" } }, { .align = MLK_ALIGN_TOP, .label = { - .text = "Top", - .style = &mlk_style, - .iface = &mlk_label_if + .text = "Top" } }, { .align = MLK_ALIGN_TOP_RIGHT, .label = { - .text = "Top right", - .style = &mlk_style, - .iface = &mlk_label_if + .text = "Top right" } }, { .align = MLK_ALIGN_RIGHT, .label = { - .text = "Right", - .style = &mlk_style, - .iface = &mlk_label_if + .text = "Right" } }, { .align = MLK_ALIGN_BOTTOM_RIGHT, .label = { - .text = "Bottom right", - .style = &mlk_style, - .iface = &mlk_label_if + .text = "Bottom right" } }, { .align = MLK_ALIGN_BOTTOM, .label = { - .text = "Bottom", - .style = &mlk_style, - .iface = &mlk_label_if + .text = "Bottom" } }, { .align = MLK_ALIGN_BOTTOM_LEFT, .label = { - .text = "Bottom left", - .style = &mlk_style, - .iface = &mlk_label_if + .text = "Bottom left" } }, { .align = MLK_ALIGN_LEFT, .label = { - .text = "Left", - .style = &mlk_style, - .iface = &mlk_label_if + .text = "Left" } } }; static struct mlk_label mouse_label = { - .text = "This one follows your mouse and is not aligned.", - .style = &mlk_style, - .iface = &mlk_label_if + .text = "This one follows your mouse and is not aligned." }; static void -delegate_glow_update(struct mlk_label_if *self, struct mlk_label *label, unsigned int ticks) +style_glow_update(struct mlk_label_style *self, struct mlk_label *label, unsigned int ticks) { - (void)self; + (void)label; - mlk_glower_update(&glower, ticks); - label->style->normal.color.text = glower.color; + mlk_glower_update(&glow, ticks); + self->color = glow.color; } static void @@ -148,13 +127,11 @@ if (mlk_example_init("example-label") < 0) mlk_panic(); - /* Change default style. */ - mlk_style.normal.color.text = 0x005162ff; + /* Change default style for all labels. */ + mlk_label_style->color = 0x005162ff; - /* Copy default label delegate and style and adapt. */ - style_glow = mlk_style; - delegate_glow = mlk_label_if; - delegate_glow.update = delegate_glow_update; + /* Change the glowing style. */ + style_glow.update = style_glow_update; for (size_t i = 0; i < MLK_UTIL_SIZE(table); ++i) { l = &table[i].label; @@ -162,7 +139,7 @@ mlk_align(table[i].align, &l->x, &l->y, w, h, 0, 0, mlk_window.w, mlk_window.h); } - mlk_glower_init(&glower); + mlk_glower_init(&glow); } static void
--- a/libmlk-ui/mlk/ui/gridmenu.c Sun Aug 20 10:47:46 2023 +0200 +++ b/libmlk-ui/mlk/ui/gridmenu.c Sun Aug 20 10:48:57 2023 +0200 @@ -211,17 +211,17 @@ int selected) { (void)self; + (void)selected; - struct mlk_label label; + struct mlk_label label = {}; - mlk_label_init(&label, &mlk_label_if, menu->style); label.x = menu->x + menu->style->normal.geo.padding + (col * menu->eltw) + (col * menu->spacew); label.y = menu->y + menu->style->normal.geo.padding + (row * menu->elth) + (row * menu->spaceh); label.text = item; - label.selected = selected; mlk_label_draw(&label); - mlk_label_finish(&label); + + // TODO: use a different style if selected. } struct mlk_gridmenu_delegate mlk_gridmenu_delegate = {
--- a/libmlk-ui/mlk/ui/label.c Sun Aug 20 10:47:46 2023 +0200 +++ b/libmlk-ui/mlk/ui/label.c Sun Aug 20 10:48:57 2023 +0200 @@ -22,36 +22,25 @@ #include "align.h" #include "label.h" -#include "style.h" #include "ui.h" +#include "ui_p.h" static int -query(struct mlk_label_if *self, - const struct mlk_label *label, +query(struct mlk_label_style *self, + struct mlk_label *label, unsigned int *w, unsigned int *h) { - (void)self; - - return mlk_font_query(label->style->normal.font, label->text, w, h); + return mlk_font_query(MLK__STYLE_FONT(self->font, MLK_UI_FONT_INTERFACE), label->text, w, h); } static void -draw(struct mlk_label_if *self, const struct mlk_label *label) +draw(struct mlk_label_style *self, struct mlk_label *label) { - (void)self; - - const struct mlk_style_attr *attr; - - if (label->selected) - attr = &label->style->selected; - else - attr = &label->style->normal; - mlk_ui_draw_text( MLK_ALIGN_NONE, - attr->font, - attr->color.text, + MLK__STYLE_FONT(self->font, MLK_UI_FONT_INTERFACE), + self->color, label->text, label->x, label->y, @@ -60,64 +49,42 @@ ); } -struct mlk_label_if mlk_label_if = { - .query = query, - .draw = draw +struct mlk_label_style mlk_label_style_dark = { + .color = 0xf5f7faff, + .font = &mlk_ui_fonts[MLK_UI_FONT_INTERFACE], + .query = query, + .draw = draw }; -void -mlk_label_init(struct mlk_label *lbl, - struct mlk_label_if *iface, - struct mlk_style *st) -{ - assert(lbl); +struct mlk_label_style mlk_label_style_light = { + .color = 0x222323ff, + .font = &mlk_ui_fonts[MLK_UI_FONT_INTERFACE], + .query = query, + .draw = draw +}; - lbl->x = 0; - lbl->y = 0; - lbl->text = ""; - lbl->iface = iface ? iface : &mlk_label_if; - lbl->style = st ? st : &mlk_style; -} +struct mlk_label_style *mlk_label_style = &mlk_label_style_light; int -mlk_label_query(const struct mlk_label *lbl, unsigned int *w, unsigned int *h) +mlk_label_query(struct mlk_label *label, unsigned int *w, unsigned int *h) { - assert(lbl); - - if (lbl->iface->query) - return lbl->iface->query(lbl->iface, lbl, w, h); + assert(label); - if (w) - *w = 0; - if (h) - *h = 0; - - return 0; + return MLK__STYLE_CALL(label->style, mlk_label_style, query, label, w, h); } void -mlk_label_update(struct mlk_label *lbl, unsigned int ticks) +mlk_label_update(struct mlk_label *label, unsigned int ticks) { - assert(lbl); + assert(label); - if (lbl->iface->update) - lbl->iface->update(lbl->iface, lbl, ticks); + MLK__STYLE_CALL(label->style, mlk_label_style, update, label, ticks); } void -mlk_label_draw(const struct mlk_label *lbl) +mlk_label_draw(struct mlk_label *label) { - assert(lbl); - - if (lbl->iface->draw) - lbl->iface->draw(lbl->iface, lbl); -} + assert(label); -void -mlk_label_finish(struct mlk_label *lbl) -{ - assert(lbl); - - if (lbl->iface->finish) - lbl->iface->finish(lbl->iface, lbl); + MLK__STYLE_CALL(label->style, mlk_label_style, draw, label); }
--- a/libmlk-ui/mlk/ui/label.h Sun Aug 20 10:47:46 2023 +0200 +++ b/libmlk-ui/mlk/ui/label.h Sun Aug 20 10:48:57 2023 +0200 @@ -24,9 +24,9 @@ * \brief GUI label. */ +struct mlk_font; struct mlk_label; -struct mlk_label_if; -struct mlk_style; +struct mlk_label_style; /** * \struct mlk_label @@ -55,138 +55,103 @@ const char *text; /** - * (read-write) - * - * Tells if label is considered selected. - */ - int selected; - - /** - * (read-write, borrowed) + * (read-write, borrowed, optional) * - * Label style. + * Style to use for drawing this label. */ - struct mlk_style *style; - - /** - * (read-write, borrowed) - * - * Label interface. - */ - struct mlk_label_if *iface; + struct mlk_label_style *style; }; /** - * \struct mlk_label_if - * \brief Label interface. + * \struct mlk_label_style + * \brief Label style. */ -struct mlk_label_if { - /* - * (read-write, borrowed, optional) - * - * Arbitrary user data. - */ - void *data; - +struct mlk_label_style { /** * (read-write) * - * Query required dimensions to draw this label. + * Text color. + */ + unsigned long color; + + /** + * (read-write, borrowed, optional) * - * \param self this interface - * \param lbl the label to query - * \param w the width destination (maybe NULL) - * \param h the height destination (maybe NULL) - * \return 0 on success or -1 on error + * Font to use. */ - int (*query)(struct mlk_label_if *self, - const struct mlk_label *lbl, + struct mlk_font *font; + + /** + * Check the required size for this label. + * + * \param self this style + * \param label the label to query dimensions + * \param w destination width (can be NULL) + * \param h destination height (can be NULL) + */ + int (*query)(struct mlk_label_style *self, + struct mlk_label *label, unsigned int *w, unsigned int *h); /** * (read-write, optional) * - * Update the label. + * Update this label. * - * \param self this delegate - * \param lbl the label to update - * \param ticks number of ticks since last frame + * \param self this style + * \param label the label to update + * \param ticks frame ticks */ - void (*update)(struct mlk_label_if *self, - struct mlk_label *lbl, - unsigned int ticks); + void (*update)(struct mlk_label_style *self, struct mlk_label *label, unsigned int ticks); /** * (read-write, optional) * - * Draw this label. + * Draw the label. * - * \param self this interface - * \param lbl the label to update + * \param self this style + * \param label the label to draw */ - void (*draw)(struct mlk_label_if *self, - const struct mlk_label *lbl); - - /** - * (read-write, optional) - * - * Cleanup data for this label. - * - * \param self this interface - * \param lbl the underlying label - */ - void (*finish)(struct mlk_label_if *self, - struct mlk_label *lbl); + void (*draw)(struct mlk_label_style *self, struct mlk_label *label); }; /** - * \brief Default stateless interface for label. + * \brief Dark default style for label. + */ +extern struct mlk_label_style mlk_label_style_dark; + +/** + * \brief Light default style for label. */ -extern struct mlk_label_if mlk_label_if; +extern struct mlk_label_style mlk_label_style_light; + +/** + * \brief Default style for all labels. + */ +extern struct mlk_label_style *mlk_label_style; #if defined(__cplusplus) extern "C" { #endif /** - * Initialize the label with default values. - * - * This is not required if you use designated initializers. - * - * \pre lbl != NULL - * \param lbl the label to default initialize - * \param dt interface to use (or NULL to use a default) - * \param st style to use (or NULL to use a default) - */ -void -mlk_label_init(struct mlk_label *lbl, - struct mlk_label_if *dt, - struct mlk_style *st); - -/** * Invoke ::mlk_label_if::query. */ int -mlk_label_query(const struct mlk_label *lbl, unsigned int *w, unsigned int *h); +mlk_label_query(struct mlk_label *label, unsigned int *w, unsigned int *h); /** * Invoke ::mlk_label_if::update. */ void -mlk_label_update(struct mlk_label *lbl, unsigned int ticks); +mlk_label_update(struct mlk_label *label, unsigned int ticks); /** * Invoke ::mlk_label_if::draw. */ void -mlk_label_draw(const struct mlk_label *lbl); - -/** - * Invoke ::mlk_label_if::finish. - */ -void -mlk_label_finish(struct mlk_label *lbl); +mlk_label_draw(struct mlk_label *label); #if defined(__cplusplus) }
--- a/libmlk-ui/mlk/ui/ui_p.h Sun Aug 20 10:47:46 2023 +0200 +++ b/libmlk-ui/mlk/ui/ui_p.h Sun Aug 20 10:48:57 2023 +0200 @@ -21,6 +21,13 @@ #include "sysconfig.h" +#define MLK__STYLE_CALL(s, def, f, ...) \ + ((s) && (s)->f) ? (s)->f((s), __VA_ARGS__) : (def)->f ? (def)->f((s) ? (s) : (def), __VA_ARGS__) : 0 + +#define MLK__STYLE_FONT(f, d) \ + f ? f : &mlk_ui_fonts[d] + +// TODO: DEPRECATED #define MLK__DELEGATE_INVOKE(d, def, f, ...) \ do { \ if (d && d->f) \ @@ -29,6 +36,7 @@ def.f(&def, __VA_ARGS__); \ } while (0) +// TODO: DEPRECATED #define MLK__DELEGATE_INVOKE_RET(d, def, f, ...) \ do { \ if (d && d->f) \