comparison libmlk-ui/mlk/ui/theme.c @ 433:862b15c3a3ae

ui: cleanup hierarchy
author David Demelier <markand@malikania.fr>
date Sat, 15 Oct 2022 21:19:25 +0200
parents src/libmlk-ui/ui/theme.c@8f59201dc76b
children 773a082f0b91
comparison
equal deleted inserted replaced
432:38cf60f5a1c4 433:862b15c3a3ae
1 /*
2 * theme.c -- abstract theming
3 *
4 * Copyright (c) 2020-2022 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20 #include <stddef.h>
21 #include <string.h>
22
23 #include <mlk/core/font.h>
24 #include <mlk/core/maths.h>
25 #include <mlk/core/painter.h>
26 #include <mlk/core/panic.h>
27 #include <mlk/core/texture.h>
28 #include <mlk/core/util.h>
29
30 #include <assets/fonts/opensans-light.h>
31 #include <assets/fonts/opensans-medium.h>
32 #include <assets/fonts/opensans-regular.h>
33
34 #include "align.h"
35 #include "button.h"
36 #include "checkbox.h"
37 #include "frame.h"
38 #include "label.h"
39 #include "theme.h"
40
41 #define THEME(t) (t ? t : &default_theme)
42
43 /* Default font catalog. */
44 #define FONT(bin, size, index) \
45 { bin, sizeof (bin), size, &default_theme.fonts[index], {0} }
46
47 /* Default theme. */
48 static struct theme default_theme = {
49 .colors = {
50 [THEME_COLOR_DEBUG] = 0xff0000ff,
51 [THEME_COLOR_NORMAL] = 0xffffffff,
52 [THEME_COLOR_SELECTED] = 0x006554ff,
53 [THEME_COLOR_SHADOW] = 0x000000ff
54 },
55 .padding = 10,
56 .draw_frame = frame_draw_default,
57 .draw_label = label_draw_default,
58 .draw_button = button_draw_default,
59 .draw_checkbox = checkbox_draw_default
60 };
61
62 static struct font_catalog {
63 const unsigned char *data;
64 const size_t datasz;
65 unsigned int size;
66 struct font **dest;
67 struct font font;
68 } default_fonts[] = {
69 FONT(assets_opensans_light, 12, THEME_FONT_DEBUG),
70 FONT(assets_opensans_regular, 14, THEME_FONT_INTERFACE),
71 FONT(assets_opensans_medium, 14, THEME_FONT_IMPORTANT)
72 };
73
74 int
75 theme_init(void)
76 {
77 /* Open all fonts. */
78 for (size_t i = 0; i < UTIL_SIZE(default_fonts); ++i) {
79 struct font_catalog *fc = &default_fonts[i];
80
81 if (font_openmem(&fc->font, fc->data, fc->datasz, fc->size) < 0)
82 goto failed;
83
84 /* Reference this font into the catalog. */
85 *default_fonts[i].dest = &default_fonts[i].font;
86 }
87
88 return 0;
89
90 failed:
91 theme_finish();
92
93 return -1;
94 }
95
96 struct theme *
97 theme_default(void)
98 {
99 return &default_theme;
100 }
101
102 unsigned int
103 theme_padding(const struct theme *t)
104 {
105 return THEME(t)->padding;
106 }
107
108 void
109 theme_shallow(struct theme *dst, const struct theme *src)
110 {
111 assert(dst);
112
113 memcpy(dst, src ? src : &default_theme, sizeof (*src));
114 }
115
116 void
117 theme_draw_frame(const struct theme *t, const struct frame *frame)
118 {
119 assert(frame);
120
121 THEME(t)->draw_frame(THEME(t), frame);
122 }
123
124 void
125 theme_draw_label(const struct theme *t, const struct label *label)
126 {
127 assert(label);
128
129 THEME(t)->draw_label(THEME(t), label);
130 }
131
132 void
133 theme_draw_button(const struct theme *t, const struct button *button)
134 {
135 assert(button);
136
137 THEME(t)->draw_button(THEME(t), button);
138 }
139
140 void
141 theme_draw_checkbox(const struct theme *t, const struct checkbox *cb)
142 {
143 assert(cb);
144
145 THEME(t)->draw_checkbox(THEME(t), cb);
146 }
147
148 void
149 theme_finish(void)
150 {
151 for (size_t i = 0; i < UTIL_SIZE(default_fonts); ++i) {
152 font_finish(&default_fonts[i].font);
153 *default_fonts[i].dest = NULL;
154 }
155 }