Mercurial > molko
comparison libmlk-ui/mlk/ui/gridmenu.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/gridmenu.c@8f59201dc76b |
children | 773a082f0b91 |
comparison
equal
deleted
inserted
replaced
432:38cf60f5a1c4 | 433:862b15c3a3ae |
---|---|
1 /* | |
2 * gridmenu.c -- GUI grid menu | |
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 <math.h> | |
21 #include <string.h> | |
22 | |
23 #include <mlk/core/event.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/trace.h> | |
29 | |
30 #include "frame.h" | |
31 #include "label.h" | |
32 #include "gridmenu.h" | |
33 #include "theme.h" | |
34 | |
35 #define THEME(m) ((m)->theme ? (m)->theme : theme_default()) | |
36 | |
37 struct index { | |
38 unsigned int row; | |
39 unsigned int col; | |
40 }; | |
41 | |
42 static struct index | |
43 get_index(const struct gridmenu *menu) | |
44 { | |
45 return (struct index) { | |
46 .row = menu->selected / menu->ncols, | |
47 .col = menu->selected % menu->ncols | |
48 }; | |
49 } | |
50 | |
51 static void | |
52 geometry(struct gridmenu *menu) | |
53 { | |
54 const struct theme *theme = THEME(menu); | |
55 struct label label = { | |
56 .theme = theme, | |
57 .flags = LABEL_FLAGS_SHADOW | |
58 }; | |
59 unsigned int reqw = 0, reqh = 0, lw, lh; | |
60 | |
61 /* Compute which item has the bigger width/height to create a spacing. */ | |
62 menu->eltw = menu->elth = 0; | |
63 menu->spacew = menu->spaceh = 0; | |
64 | |
65 for (size_t i = 0; i < menu->itemsz; ++i) { | |
66 if (!(label.text = menu->items[i])) | |
67 continue; | |
68 | |
69 | |
70 label_query(&label, &lw, &lh); | |
71 | |
72 menu->eltw = fmax(menu->eltw, lw); | |
73 menu->elth = fmax(menu->elth, lh); | |
74 } | |
75 | |
76 /* Total texture size required to draw items. */ | |
77 reqw = (theme->padding * 2) + (menu->eltw * menu->ncols); | |
78 reqh = (theme->padding * 2) + (menu->elth * menu->nrows); | |
79 | |
80 /* | |
81 * Compute spacing between elements. We remove the padding because it | |
82 * is outside of the elements. | |
83 */ | |
84 if (reqw > menu->w) { | |
85 tracef("gridmenu width is too small: %u < %u", menu->w, reqw); | |
86 menu->spacew = 1; | |
87 } else if (menu->ncols > 1) { | |
88 reqw -= theme->padding * 2; | |
89 menu->spacew = (menu->w - reqw) / menu->ncols; | |
90 } | |
91 | |
92 if (reqh > menu->h) { | |
93 tracef("gridmenu height is too small: %u < %u", menu->h, reqh); | |
94 menu->spaceh = 1; | |
95 } else if (menu->nrows > 1) { | |
96 reqh -= theme->padding * 2; | |
97 menu->spaceh = (menu->h - reqh) / menu->nrows; | |
98 } | |
99 } | |
100 | |
101 static void | |
102 draw_frame(const struct gridmenu *menu) | |
103 { | |
104 const struct frame f = { | |
105 .x = menu->x, | |
106 .y = menu->y, | |
107 .w = menu->w, | |
108 .h = menu->h, | |
109 .theme = menu->theme, | |
110 }; | |
111 | |
112 frame_draw(&f); | |
113 } | |
114 | |
115 static void | |
116 draw_labels(const struct gridmenu *menu) | |
117 { | |
118 size_t pagesz, pagenr, item, c = 0, r = 0; | |
119 struct label label = {0}; | |
120 const struct theme *theme = THEME(menu); | |
121 | |
122 label.theme = theme; | |
123 label.flags = LABEL_FLAGS_SHADOW; | |
124 | |
125 /* | |
126 * Select the first top-left column based on the current selection and | |
127 * the number of rows/columns. | |
128 */ | |
129 pagesz = menu->nrows * menu->ncols; | |
130 pagenr = menu->selected / pagesz; | |
131 | |
132 for (size_t i = 0; i < pagesz; ++i) { | |
133 item = i + pagenr * pagesz; | |
134 | |
135 if (item >= menu->itemsz || !menu->items[item]) | |
136 continue; | |
137 | |
138 label.text = menu->items[item]; | |
139 label.x = menu->x + theme->padding + (c * menu->eltw) + (c * menu->spacew); | |
140 label.y = menu->y + theme->padding + (r * menu->elth) + (r * menu->spaceh); | |
141 | |
142 if (i == menu->selected % pagesz) | |
143 label.flags |= LABEL_FLAGS_SELECTED; | |
144 else | |
145 label.flags &= ~(LABEL_FLAGS_SELECTED); | |
146 | |
147 label_draw(&label); | |
148 | |
149 if (++c >= menu->ncols) { | |
150 ++r; | |
151 c = 0; | |
152 } | |
153 } | |
154 } | |
155 | |
156 static int | |
157 handle_keydown(struct gridmenu *menu, const struct event_key *key) | |
158 { | |
159 assert(key->type == EVENT_KEYDOWN); | |
160 | |
161 const struct index idx = get_index(menu); | |
162 int validate = 0; | |
163 | |
164 switch (key->key) { | |
165 case KEY_UP: | |
166 if (idx.row > 0) | |
167 menu->selected -= menu->ncols; | |
168 break; | |
169 case KEY_RIGHT: | |
170 if (menu->selected + 1U < menu->itemsz) | |
171 menu->selected += 1; | |
172 break; | |
173 case KEY_DOWN: | |
174 if (idx.row + 1U < menu->itemsz / menu->ncols) | |
175 menu->selected += menu->ncols; | |
176 else | |
177 menu->selected = menu->itemsz - 1; | |
178 break; | |
179 case KEY_LEFT: | |
180 if (idx.col > 0) | |
181 menu->selected -= 1; | |
182 break; | |
183 case KEY_ENTER: | |
184 validate = 1; | |
185 break; | |
186 default: | |
187 break; | |
188 } | |
189 | |
190 return validate; | |
191 } | |
192 | |
193 static int | |
194 handle_clickdown(struct gridmenu *menu, const struct event_click *click) | |
195 { | |
196 assert(click->type == EVENT_CLICKDOWN); | |
197 | |
198 const struct theme *theme = THEME(menu); | |
199 size_t pagesz, pagenr, selected, c = 0, r = 0; | |
200 int x, y; | |
201 | |
202 pagesz = menu->nrows * menu->ncols; | |
203 pagenr = menu->selected / pagesz; | |
204 | |
205 for (size_t i = 0; i < pagesz; ++i) { | |
206 x = menu->x + theme->padding + (c * menu->eltw) + (c * menu->spacew); | |
207 y = menu->y + theme->padding + (r * menu->elth) + (r * menu->spaceh); | |
208 | |
209 if (maths_is_boxed(x, y, menu->eltw, menu->elth, click->x, click->y)) { | |
210 selected = c + r * menu->ncols; | |
211 selected += pagesz * pagenr; | |
212 | |
213 if (selected < menu->itemsz) { | |
214 menu->selected = selected; | |
215 return 1; | |
216 } | |
217 } | |
218 | |
219 if (++c >= menu->ncols) { | |
220 ++r; | |
221 c = 0; | |
222 } | |
223 } | |
224 | |
225 return 0; | |
226 } | |
227 | |
228 void | |
229 gridmenu_init(struct gridmenu *menu, | |
230 unsigned int nr, | |
231 unsigned int nc, | |
232 const char * const *items, | |
233 size_t itemsz) | |
234 { | |
235 assert(menu); | |
236 assert(nr); | |
237 assert(nc); | |
238 | |
239 memset(menu, 0, sizeof (*menu)); | |
240 | |
241 menu->nrows = nr; | |
242 menu->ncols = nc; | |
243 menu->items = items; | |
244 menu->itemsz = itemsz; | |
245 } | |
246 | |
247 void | |
248 gridmenu_resize(struct gridmenu *menu, int x, int y, unsigned int w, unsigned int h) | |
249 { | |
250 assert(menu); | |
251 | |
252 menu->x = x; | |
253 menu->y = y; | |
254 menu->w = w; | |
255 menu->h = h; | |
256 | |
257 geometry(menu); | |
258 } | |
259 | |
260 int | |
261 gridmenu_handle(struct gridmenu *menu, const union event *ev) | |
262 { | |
263 assert(menu); | |
264 assert(ev); | |
265 | |
266 switch (ev->type) { | |
267 case EVENT_KEYDOWN: | |
268 return handle_keydown(menu, &ev->key); | |
269 break; | |
270 case EVENT_CLICKDOWN: | |
271 return handle_clickdown(menu, &ev->click); | |
272 break; | |
273 default: | |
274 break; | |
275 } | |
276 | |
277 return 0; | |
278 } | |
279 | |
280 void | |
281 gridmenu_draw(const struct gridmenu *menu) | |
282 { | |
283 assert(menu); | |
284 | |
285 draw_frame(menu); | |
286 draw_labels(menu); | |
287 } |