~ruther/CTU-FEE-B0B35APO-Semestral-project

ref: 61727753cc0b74adb9be0fcdb4accfe47a1af0f3 CTU-FEE-B0B35APO-Semestral-project/lib-gui/include/gui.h -rw-r--r-- 9.0 KiB
61727753 — František Boháček feat: add list container selected index, padding and colors 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#ifndef __GUI_H__
#define __GUI_H__

#include "display_utils.h"
#include "font.h"
#include "input.h"
#include "logger.h"
#include "mzapo_pheripherals.h"

typedef struct renderer_t renderer_t;
typedef struct component_t component_t;
typedef struct gui_t gui_t;
typedef struct container_t container_t;

typedef void (*render_function)(container_t *container, component_t *component,
                                gui_t *gui);
typedef void (*update_function)(container_t *container, component_t *component,
                                gui_t *gui);

struct component_t {
  int16_t x;
  int16_t y;

  uint16_t width;
  uint16_t height;

  render_function render;
  update_function update;

  bool focusable;
  bool focused;

  void *state;
};

typedef enum {
  CONT_TABLE,
  CONT_GROUP,
  CONT_ONE,
} container_type_t;

/**
 * @brief Summary
 * @details Description
 * @param[inout] state
 * @param[in] index Index of the item to render
 * @param[out] renderer
 * @param[in] beg_x Begin x coordinate
 * @param[in] beg_y Begin y coordinate
 * @return Whether item was rendered
 */
typedef bool (*render_item)(void *state, uint32_t index, renderer_t *renderer, int16_t beg_x, int16_t beg_y, display_pixel_t foreground);

typedef struct {
  void *state;
  render_item render_item_fn;
  render_item render_header_fn;
  uint32_t items_count;
  uint32_t selected_index;
  uint16_t item_height;

  int32_t scroll_x;
  int32_t scroll_y;

  display_pixel_t regular_foreground;
  display_pixel_t regular_background;

  display_pixel_t selected_foreground;
  display_pixel_t selected_background;

  uint16_t item_padding;
} list_container_t;

typedef struct {
  component_t *components;
  uint16_t size;
  uint16_t count;
} group_container_t;

typedef struct {
  component_t component;
  bool set;
} one_container_t;

typedef union {
  list_container_t list;
  group_container_t group;
  one_container_t one;
} container_inner_t;

struct container_t {
  container_type_t type;
  container_inner_t inner;

  bool focusable;
  bool focused;

  int16_t x;
  int16_t y;

  uint16_t width;
  uint16_t height;
};

typedef struct {
  container_t *containers;
  uint16_t containers_size;
  uint16_t containers_count;
} window_t;

struct gui_t {
  window_t *active_window;
  commands_t *commands;
  renderer_t *renderer;
  mzapo_pheripherals_t *pheripherals;
  logger_t *logger;

  size2d_t size;
};

/**
 * @brief Create gui state
 *
 * @param logger
 * @param commands
 * @param renderer
 * @param pheripherals
 * @return gui_t
 */
gui_t gui_create(logger_t *logger, commands_t *commands, renderer_t *renderer,
                 mzapo_pheripherals_t *pheripherals);

/**
 * @brief Render all elements in gui
 *
 * @param gui
 */
void gui_render(gui_t *gui);

/**
 * @brief Update all elements in gui
 *
 * @param gui
 */
void gui_update(gui_t *gui);

/**
 * @brief Set gui active window
 *
 * @param gui
 * @param window
 */
void gui_set_active_window(gui_t *gui, window_t *window);

// gui_window.c
/**
 * @brief Create new gui window
 *
 * @param containers array of containers of static size
 * @param size maximal number of the containers
 * @return window_t
 */
window_t gui_window_create(container_t *containers, uint16_t size);

/**
 * @brief Add container to window
 *
 * @param window
 * @param container
 * @return container_t*
 */
container_t *gui_window_add_container(window_t *window, container_t container);

// gui_container.c
/**
 * @brief Render all elements inside container
 *
 * @param gui
 * @param container
 */
void gui_container_render(gui_t *gui, container_t *container);

/**
 * @brief Update all elements inside container
 *
 * @param gui
 * @param container
 */
void gui_container_update(gui_t *gui, container_t *container);

// gui_component.c
/**
 * @brief Create gui component
 *
 * @param x begin x coord
 * @param y begin y coord
 * @param w width
 * @param h height
 * @param render
 * @param update
 * @return component_t
 */
component_t gui_component_create(int16_t x, int16_t y, uint16_t w, uint16_t h,
                                 render_function render,
                                 update_function update);

/**
 * @brief Check whether component is within screen so it may be rendered
 *
 * @param gui
 * @param container
 * @param component
 * @return true
 * @return false
 */
bool gui_is_component_visible(gui_t *gui, container_t *container,
                              component_t *component);

/**
 * @brief Render gui component
 *
 * @param gui
 * @param container
 * @param component
 */
void gui_component_render(gui_t *gui, container_t *container,
                          component_t *component);

/**
 * @brief Update gui component
 *
 * @param gui
 * @param container
 * @param component
 */
void gui_component_update(gui_t *gui, container_t *container,
                          component_t *component);

/**
 * @brief Get absolute position of component
 *
 * @param container
 * @param component
 * @return coords_t
 */
coords_t gui_component_get_absolute_position(container_t *container,
                                             component_t *component);

/**
 * @brief Get position on screen
 *
 * @param container
 * @param component
 * @return coords_t
 */
coords_t gui_component_get_screen_position(container_t *container,
                                           component_t *component);

// gui_one_container.c
/**
 * @brief Create ONE container that holds only one component
 *
 * @param x
 * @param y
 * @return container_t
 */
container_t gui_one_container_create(int16_t x, int16_t y);

/**
 * @brief Set ONE container component
 *
 * @param container
 * @param component
 * @return component_t* set component
 */
component_t *gui_one_container_set_component(container_t *container,
                                             component_t component);

/**
 * @brief Get ONE container component if it is set
 *
 * @param container
 * @return component_t*
 */
component_t *gui_one_container_get_component(container_t *container);

/**
 * @brief Render ONE container
 *
 * @param gui
 * @param container
 */
void gui_one_container_render(gui_t *gui, container_t *container);

/**
 * @brief Update ONE container
 *
 * @param gui
 * @param container
 */
void gui_one_container_update(gui_t *gui, container_t *container);

// gui_group_container.c
/**
 * @brief Create GROUP container holding n components
 *
 * @param x
 * @param y
 * @param components array of components of fixed size
 * @param components_size size of components
 * @return container_t
 */
container_t gui_group_container_create(int16_t x, int16_t y,
                                       component_t *components,
                                       uint16_t components_size);

/**
 * @brief Add component to GROUP container
 *
 * @param container
 * @param component
 * @return component_t*
 * @return NULL if container is full
 */
component_t *gui_group_container_add_component(container_t *container,
                                               component_t component);

void gui_group_container_render(gui_t *gui, container_t *container);
void gui_group_container_update(gui_t *gui, container_t *container);

// list_container.c

/**
 * @brief Create list container
 *
 * @param[inout] state Custom state of items to pass to render function
 * @param[in] items_count Count of items to show
 * @param[in] item_height The height of item
 * @param[in] render_it Function to render item
 * @param[in] render_header Function to render header
 * @return Container with list data
 */
container_t gui_list_container_create(void *state, uint32_t items_count,
                                      uint16_t item_height,
                                      render_item render_it,
                                      render_item render_header);

/**
 * @brief Get current selected index based on scroll
 *
 * @param[inout] container
 * @return Current selected index
 */
uint32_t gui_list_get_selected_index(container_t *container);

/**
 * @brief Scroll list container by x, y
 * @details Increase scroll coordinates by x, y so different view arrea is shown
 * @param[inout] container
 * @param[in] x x coordinate
 * @param[in] y y coordinate
 */
void gui_list_scroll(container_t *container, int16_t x, int16_t y);

/**
 * @brief List container set new state
 *
 * @param[inout] container
 * @param[out] state Custom state of items
 * @param[in] items_count The count of items
 * @return Whether the state was set correctly
 */
bool gui_list_container_set_state(container_t *container, void *state,
                                  uint32_t items_count);

/**
 * @brief List container set new item height
 *
 * @param[inout] container
 * @param[in] item_height The height of item
 * @return Whether the item was set correctly
 */
bool gui_list_container_set_item_height(container_t *container,
                                        uint16_t item_height);

bool gui_list_container_set_render_function(container_t *container,
                                            render_item render_it,
                                            render_item render_header);

void gui_list_container_render(gui_t *gui, container_t *container);
void gui_list_container_update(gui_t *gui, container_t *container);

#endif // __GUI_H__
Do not follow this link