-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsms_gui.c
570 lines (538 loc) · 27.5 KB
/
tsms_gui.c
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include "tsms_gui.h"
#include "tsms_display.h"
#include "tsms_list.h"
#include "tsms_lock.h"
#include "tsms_math.h"
#include "tsms_algorithm.h"
#include "tsms_printer.h"
TSMS_GRID_INFO TSMS_GUI_INVALID_GRID = {0, 0, 0, 0, 0, TSMS_STYLE_DISPLAY_BLOCK};
TSMS_INLINE long __tsms_internal_compare_z_index(void * a, void * b) {
pGuiElement element1 = (pGuiElement) a;
pGuiElement element2 = (pGuiElement) b;
if (element1->computedStyle.position != element2->computedStyle.position)
// absolute position should trigger first
return element2->computedStyle.position - element1->computedStyle.position;
return element2->computedStyle.zIndex - element1->computedStyle.zIndex;
}
TSMS_INLINE long __tsms_internal_compare_grid_render_info(void * a, void * b) {
pGuiElement element1 = (pGuiElement) a;
pGuiElement element2 = (pGuiElement) b;
if (element1->requestRender != element2->requestRender)
// true requestRender should be rendered first
return element1->requestRender ? -1 : 1;
if (element1->computedStyle.position != element2->computedStyle.position)
// relative position should be rendered first
return element1->computedStyle.position - element2->computedStyle.position;
if (element1->grid.zIndex != element2->grid.zIndex)
// low zIndex should be rendered first
return element1->grid.zIndex - element2->grid.zIndex;
if (element1->level != element2->level)
return element1->level - element2->level;
return element1->grid.x - element2->grid.x;
}
// compute style and merge all
TSMS_INLINE void __tsms_internal_request_render(pGuiElement element, bool parentFlag) {
TSMS_STYLE style = element->computedStyle;
if (element->children != TSMS_NULL)
for (TSMS_POS i = 0; i < element->children->length; i++)
__tsms_internal_request_render(element->children->list[i], element->requestRender || parentFlag);
if (parentFlag && style.position == TSMS_STYLE_POSITION_RELATIVE)
element->requestRender = false;
}
TSMS_INLINE void __tsms_internal_check_render(pGuiElement element) {
if (element->requestRender)
return;
if (!TSMS_GUI_equalsGrid(element->lastGrid, element->grid)) {
element->requestRender = true;
pGui gui = TSMS_GUI_getGui(element);
for (TSMS_POS i = 0; i < gui->list->length; i++) {
pGuiElement child = gui->list->list[i];
if (child == element)
continue;
TSMS_STYLE childStyle = child->computedStyle;
if (childStyle.zIndex > element->computedStyle.zIndex)
child->requestRender = true;
}
return;
}
if (element->children != TSMS_NULL)
for (TSMS_POS i = 0; i < element->children->length; i++) {
pGuiElement child = element->children->list[i];
__tsms_internal_check_render(child);
}
}
TSMS_INLINE void __tsms_internal_add_render_entity(pGui gui, pGuiElement element) {
TSMS_LIST_add(gui->list, element);
if (element->type == TSMS_GUI_TYPE_TOUCHABLE)
TSMS_LIST_add(gui->touchableList, element);
if (element->children != TSMS_NULL)
for (TSMS_POS i = 0; i < element->children->length; i++)
__tsms_internal_add_render_entity(gui, element->children->list[i]);
}
TSMS_INLINE void __tsms_internal_touch_callback(TSMS_THP touch, uint8_t id, uint16_t x, uint16_t y, uint16_t size, TSMS_TOUCH_STATE state, void * handler) {
pGui gui = handler;
y = gui->display->screen->height - y;
for (TSMS_POS i = 0; i < gui->touchableList->length; i++) {
pGuiTouchableElement element = gui->touchableList->list[i];
double now = TSMS_TIMER_now(TSMS_TIMER_getDefaultTimer());
if (state == TSMS_TOUCH_STATE_RELEASE && TSMS_INT_LIST_contains(element->points, id)) {
TSMS_INT_LIST_removeElement(element->points, id);
if (element->releaseCallback != TSMS_NULL)
element->releaseCallback(element, x, y, element->releaseHandler);
if (element->press == TSMS_PRESS_STATE_PRESS)
element->press = TSMS_PRESS_STATE_PRESS_AND_RELEASE;
else
element->press = 0;
element->lastUpdate = now;
break;
}
else if (TSMS_GUI_inGrid(element->grid, x, y) && TSMS_GUI_isInValidGrid(element)) {
if (element->press == TSMS_PRESS_STATE_PRESS_AND_RELEASE && now - element->lastUpdate > TSMS_GUI_DOUBLE_PRESS_TIME)
element->press = 0;
if (state == TSMS_TOUCH_STATE_PRESS) {
TSMS_INT_LIST_add(element->points, id);
if (element->press == TSMS_PRESS_STATE_PRESS_AND_RELEASE) {
if (element->doublePressCallback != TSMS_NULL)
element->doublePressCallback(element, x, y, element->doublePressHandler);
element->press = TSMS_PRESS_STATE_DOUBLE_PRESS;
}
if (element->press == 0) {
if (element->pressCallback != TSMS_NULL)
element->pressCallback(element, x, y, element->pressHandler);
element->press = TSMS_PRESS_STATE_PRESS;
}
} else if (state == TSMS_TOUCH_STATE_LONG_PRESS) {
if (element->longPressCallback != TSMS_NULL) {
element->longPressCallback(element, x, y, element->longPressHandler);
element->press = TSMS_PRESS_STATE_LONG_PRESS;
}
}
element->lastUpdate = now;
break;
}
}
}
TSMS_INLINE void __tsms_internal_cancel_previous_render(pGuiElement element, bool parentFlag, pLock lock) {
if (element->requestRender || parentFlag) {
TSMS_STYLE style = element->computedStyle;
for (TSMS_POS i = 0; i < element->renderOperations->length; i++) {
pRenderOperation operation = element->renderOperations->list[i];
TSMS_GUI_cancelRenderOperation(element, operation, style, lock);
TSMS_GUI_releaseRenderOperation(operation);
}
TSMS_LIST_clear(element->renderOperations);
}
if (element->children != TSMS_NULL)
for (TSMS_POS i = 0; i < element->children->length; i++)
__tsms_internal_cancel_previous_render(element->children->list[i], element->requestRender || parentFlag, lock);
}
TSMS_INLINE void __tsms_internal_compute_style(pGuiElement element) {
TSMS_STYLE_getStyle(element);
if (element->children != TSMS_NULL)
for (TSMS_POS i = 0; i < element->children->length; i++)
__tsms_internal_compute_style(element->children->list[i]);
}
bool TSMS_GUI_inGrid(TSMS_GRID_INFO grid, uint16_t x, uint16_t y) {
return x >= grid.x && x < grid.x + grid.width && y <= grid.y && y > grid.y - grid.height;
}
void TSMS_GUI_defaultStyleCallback(pMutableStyle style, TSMS_STYLE oldStyle, TSMS_STYLE newStyle, void * handler) {
pGuiElement element = (pGuiElement) handler;
element->requestRender = true;
pGui gui = TSMS_GUI_getGui(element);
if (gui == TSMS_NULL)
return;
uint16_t oldZIndex = element->computedStyle.zIndex == TSMS_STYLE_INHERIT ? 0 : element->computedStyle.zIndex;
__tsms_internal_compute_style(gui);
uint16_t newZIndex = element->computedStyle.zIndex;
uint16_t minZIndex = min(oldZIndex, newZIndex);
if (oldZIndex != newZIndex)
TSMS_ALGORITHM_sort(gui->touchableList, __tsms_internal_compare_z_index);
for (TSMS_POS i = 0; i < gui->list->length; i++) {
pGuiElement child = gui->list->list[i];
if (child == element)
continue;
TSMS_STYLE childStyle = child->computedStyle;
if ((childStyle.zIndex > minZIndex && childStyle.position == TSMS_STYLE_POSITION_ABSOLUTE && element->computedStyle.position == TSMS_STYLE_POSITION_ABSOLUTE) || (element->computedStyle.position == TSMS_STYLE_POSITION_RELATIVE && childStyle.position == TSMS_STYLE_POSITION_ABSOLUTE))
child->requestRender = true;
}
}
pGui TSMS_GUI_getGui(pGuiElement element) {
if (element == TSMS_NULL)
return TSMS_NULL;
if (element->gui != TSMS_NULL)
return element->gui;
return TSMS_GUI_getGui(element->parent);
}
bool TSMS_GUI_isInvalidGrid(TSMS_GRID_INFO grid) {
return grid.width == 0 && grid.displayType == TSMS_STYLE_DISPLAY_BLOCK;
}
bool TSMS_GUI_isInValidGrid(pGuiElement element) {
if (element == TSMS_NULL)
return false;
if (TSMS_GUI_isInvalidGrid(element->grid))
return false;
if (element->parent == TSMS_NULL)
return true;
return TSMS_GUI_isInValidGrid(element->parent);
}
TSMS_GRID_INFO TSMS_GUI_calcGrid(pGuiElement element, TSMS_STYLE style, uint16_t x, uint16_t y, uint16_t boxWidth, uint16_t boxHeight, uint16_t parentWidth, uint16_t parentHeight) {
if (element == TSMS_NULL)
return TSMS_GUI_INVALID_GRID;
if (style.display == TSMS_STYLE_DISPLAY_NONE)
return (TSMS_GRID_INFO) {x, y, 0, 0, 0, TSMS_STYLE_DISPLAY_NONE};
boxWidth += TSMS_STYLE_X_ATTACHMENT(style);
boxHeight += TSMS_STYLE_Y_ATTACHMENT(style);
if (style.position == TSMS_STYLE_POSITION_RELATIVE) {
if (boxWidth > parentWidth || boxHeight > parentHeight)
return TSMS_GUI_INVALID_GRID;
return (TSMS_GRID_INFO) {x, y, boxWidth, boxHeight, style.zIndex, TSMS_STYLE_DISPLAY_BLOCK};
} else {
uint16_t screenWidth = TSMS_GUI_getGui(element)->display->screen->width;
if (boxWidth + x > screenWidth || y - boxHeight < 0)
return TSMS_GUI_INVALID_GRID;
return (TSMS_GRID_INFO) {x, y, boxWidth, boxHeight, style.zIndex, TSMS_STYLE_DISPLAY_BLOCK};
}
}
pGui TSMS_GUI_create(TSMS_DPHP display) {
pGui gui = (pGui) TSMS_malloc(sizeof(tGui));
if (gui == TSMS_NULL)
return TSMS_NULL;
TSMS_STYLE style = TSMS_STYLE_DEFAULT;
style.width = display->screen->width;
style.height = display->screen->height;
TSMS_INIT_GUI_CONTAINER_ELEMENT(gui, TSMS_GUI_TYPE_GUI, TSMS_CONTAINER_preRender, TSMS_CONTAINER_render, style, gui, true)
gui->display = display;
gui->list = TSMS_LIST_create(10);
gui->touchableList = TSMS_LIST_create(10);
gui->lock = TSMS_LOCK_create();
TSMS_TOUCH_setCallback(display->touch, __tsms_internal_touch_callback, gui);
if (gui->style == TSMS_NULL || gui->children == TSMS_NULL || gui->renderOperations == TSMS_NULL || gui->list == TSMS_NULL || gui->touchableList == TSMS_NULL || gui->lock == TSMS_NULL) {
TSMS_GUI_release(gui);
return TSMS_NULL;
}
return gui;
}
TSMS_RESULT TSMS_GUI_release(pGui gui) {
if (gui == TSMS_NULL)
return TSMS_ERROR;
TSMS_TOUCH_setCallback(gui->display->touch, TSMS_NULL, TSMS_NULL);
TSMS_LIST_release(gui->list);
TSMS_LIST_release(gui->touchableList);
TSMS_LOCK_release(gui->lock);
return TSMS_GUI_releaseGuiElement(gui);
}
TSMS_RESULT TSMS_GUI_add(pGuiElement parent, pGuiElement element) {
if (parent == TSMS_NULL)
return TSMS_ERROR;
if (element == TSMS_NULL)
return TSMS_ERROR;
if (parent->children == TSMS_NULL)
return TSMS_ERROR;
pGui gui = TSMS_GUI_getGui(parent);
if (gui != TSMS_NULL) {
TSMS_LOCK_lock(gui->lock);
element->parent = parent;
element->gui = gui;
element->level = parent->level + 1;
TSMS_RESULT result = TSMS_LIST_add(parent->children, element);
TSMS_LIST_clear(gui->list);
TSMS_LOCK_unlock(gui->lock);
return result;
}
element->parent = parent;
element->gui = gui;
element->level = parent->level + 1;
return TSMS_LIST_add(parent->children, element);
}
TSMS_RESULT TSMS_GUI_remove(pGuiElement parent, pGuiElement element) {
if (parent == TSMS_NULL)
return TSMS_ERROR;
if (element == TSMS_NULL)
return TSMS_ERROR;
if (parent->children == TSMS_NULL)
return TSMS_ERROR;
pGui gui = TSMS_GUI_getGui(parent);
if (gui != TSMS_NULL) {
TSMS_LOCK_lock(gui->lock);
TSMS_RESULT result = TSMS_LIST_removeElement(parent->children, element) == -1 ? TSMS_FAIL : TSMS_SUCCESS;
TSMS_LIST_clear(gui->list);
TSMS_LOCK_unlock(gui->lock);
return result;
}
return TSMS_LIST_removeElement(parent->children, element) == -1 ? TSMS_FAIL : TSMS_SUCCESS;
}
TSMS_RESULT TSMS_GUI_draw(pGui gui) {
if (gui == TSMS_NULL)
return TSMS_ERROR;
if (gui->list->length == 0)
__tsms_internal_add_render_entity(gui, gui);
TSMS_LOCK_lock(gui->lock);
gui->preRender(gui, 0, gui->display->screen->height, gui->display->screen->width, gui->display->screen->height);
__tsms_internal_check_render(gui);
__tsms_internal_request_render(gui, false);
pLock lock = TSMS_SEQUENCE_PRIORITY_LOCK_lock(gui->display->screen->lock, TSMS_NULL, 0);
__tsms_internal_cancel_previous_render(gui, false, lock);
TSMS_ALGORITHM_sort(gui->list, __tsms_internal_compare_grid_render_info);
TSMS_SIZE renderRange;
for (renderRange = 0; renderRange < gui->list->length; renderRange++) {
pGuiElement element = gui->list->list[renderRange];
if (!element->requestRender)
break;
}
if (lock != TSMS_NULL) {
for (TSMS_POS i = 0; i < renderRange; i++) {
pGuiElement element = gui->list->list[i];
element->render(element, lock);
}
TSMS_SEQUENCE_PRIORITY_LOCK_unlock(gui->display->screen->lock, lock);
}
TSMS_RESULT result = TSMS_DISPLAY_request(gui->display);
TSMS_LOCK_unlock(gui->lock);
return result;
}
// do not check grid is valid, because it must be valid to call this function
TSMS_RESULT TSMS_GUI_renderStyle(pGuiElement element, TSMS_STYLE style, pLock lock) {
TSMS_COLOR parentColor = element->parent == TSMS_NULL ? *style.backgroundColor : *element->parent->computedStyle.backgroundColor;
TSMS_GRID_INFO grid = element->grid;
if (!TSMS_UTIL_equalsColor(*style.margin.color,parentColor)) {
// render margin
TSMS_SCREEN_fillRectTopLeft(TSMS_GUI_getGui(element)->display->screen, grid.x, grid.y, grid.width,
style.margin.top,
*style.margin.color, lock);
TSMS_LIST_add(element->renderOperations,
TSMS_GUI_createFillRectRenderOperation(grid.x, grid.y, grid.width, style.margin.top));
TSMS_SCREEN_fillRectTopLeft(TSMS_GUI_getGui(element)->display->screen, grid.x,
grid.y - grid.height + style.margin.bottom,
grid.width, style.margin.bottom, *style.margin.color, lock);
TSMS_LIST_add(element->renderOperations,
TSMS_GUI_createFillRectRenderOperation(grid.x, grid.y - grid.height + style.margin.bottom,
grid.width,
style.margin.bottom));
TSMS_SCREEN_fillRectTopLeft(TSMS_GUI_getGui(element)->display->screen, grid.x, grid.y - style.margin.top,
style.margin.left,
grid.height - style.margin.top - style.margin.bottom, *style.margin.color, lock);
TSMS_LIST_add(element->renderOperations,
TSMS_GUI_createFillRectRenderOperation(grid.x, grid.y - style.margin.top, style.margin.left,
grid.height - style.margin.top - style.margin.bottom));
TSMS_SCREEN_fillRectTopLeft(TSMS_GUI_getGui(element)->display->screen, grid.x + grid.width - style.margin.right,
grid.y - style.margin.top, style.margin.right,
grid.height - style.margin.top - style.margin.bottom, *style.margin.color, lock);
TSMS_LIST_add(element->renderOperations,
TSMS_GUI_createFillRectRenderOperation(grid.x + grid.width - style.margin.right,
grid.y - style.margin.top, style.margin.right,
grid.height - style.margin.top - style.margin.bottom));
}
if (!TSMS_UTIL_equalsColor(*style.border.color,parentColor)) {
// render border
TSMS_SCREEN_fillRectTopLeft(TSMS_GUI_getGui(element)->display->screen, grid.x + style.margin.left,
grid.y - style.margin.top, grid.width - style.margin.left - style.margin.right,
style.border.top, *style.border.color, lock);
TSMS_LIST_add(element->renderOperations,
TSMS_GUI_createFillRectRenderOperation(grid.x + style.margin.left, grid.y - style.margin.top,
grid.width - style.margin.left - style.margin.right,
style.border.top));
TSMS_SCREEN_fillRectTopLeft(TSMS_GUI_getGui(element)->display->screen,
grid.x + style.margin.left,
grid.y - grid.height + style.margin.bottom + style.border.bottom,
grid.width - style.margin.left - style.margin.right,
style.border.bottom, *style.border.color, lock);
TSMS_LIST_add(element->renderOperations, TSMS_GUI_createFillRectRenderOperation(grid.x + style.margin.left,
grid.y - grid.height +
style.margin.bottom +
style.border.bottom,
grid.width - style.margin.left -
style.margin.right,
style.border.bottom));
TSMS_SCREEN_fillRectTopLeft(TSMS_GUI_getGui(element)->display->screen,
grid.x + style.margin.left,
grid.y - style.margin.top,
style.border.left,
grid.height - style.margin.top - style.margin.bottom, *style.border.color, lock);
TSMS_LIST_add(element->renderOperations,
TSMS_GUI_createFillRectRenderOperation(grid.x + style.margin.left, grid.y - style.margin.top,
style.border.left,
grid.height - style.margin.top - style.margin.bottom));
TSMS_SCREEN_fillRectTopLeft(TSMS_GUI_getGui(element)->display->screen,
grid.x + grid.width - style.margin.right - style.border.right,
grid.y - style.margin.top,
style.border.right,
grid.height - style.margin.top - style.margin.bottom, *style.border.color, lock);
TSMS_LIST_add(element->renderOperations,
TSMS_GUI_createFillRectRenderOperation(
grid.x + grid.width - style.margin.right - style.border.right,
grid.y - style.margin.top, style.border.right,
grid.height - style.margin.top - style.margin.bottom));
}
if (!TSMS_UTIL_equalsColor(*style.border.color,parentColor)) {
// render padding
TSMS_SCREEN_fillRectTopLeft(TSMS_GUI_getGui(element)->display->screen,
grid.x + style.margin.left + style.border.left,
grid.y - style.margin.top - style.border.top,
grid.width - style.margin.left - style.margin.right - style.border.left -
style.border.right,
style.padding.top, *style.padding.color, lock);
TSMS_LIST_add(element->renderOperations,
TSMS_GUI_createFillRectRenderOperation(grid.x + style.margin.left + style.border.left,
grid.y - style.margin.top - style.border.top,
grid.width - style.margin.left - style.margin.right -
style.border.left - style.border.right,
style.padding.top));
TSMS_SCREEN_fillRectTopLeft(TSMS_GUI_getGui(element)->display->screen,
grid.x + style.margin.left + style.border.left,
grid.y - grid.height + style.margin.bottom + style.border.bottom +
style.padding.bottom,
grid.width - style.margin.left - style.margin.right - style.border.left -
style.border.right,
style.padding.bottom, *style.padding.color, lock);
TSMS_LIST_add(element->renderOperations,
TSMS_GUI_createFillRectRenderOperation(grid.x + style.margin.left + style.border.left,
grid.y - grid.height + style.margin.bottom +
style.border.bottom + style.padding.bottom,
grid.width - style.margin.left - style.margin.right -
style.border.left - style.border.right,
style.padding.bottom));
TSMS_SCREEN_fillRectTopLeft(TSMS_GUI_getGui(element)->display->screen,
grid.x + style.margin.left + style.border.left,
grid.y - style.margin.top - style.border.top,
style.padding.left,
grid.height - style.margin.top - style.margin.bottom - style.border.top -
style.border.bottom,
*style.padding.color, lock);
TSMS_LIST_add(element->renderOperations,
TSMS_GUI_createFillRectRenderOperation(grid.x + style.margin.left + style.border.left,
grid.y - style.margin.top - style.border.top,
style.padding.left,
grid.height - style.margin.top - style.margin.bottom -
style.border.top - style.border.bottom));
TSMS_SCREEN_fillRectTopLeft(TSMS_GUI_getGui(element)->display->screen,
grid.x + grid.width - style.margin.right - style.border.right - style.padding.right,
grid.y - style.margin.top - style.border.top,
style.padding.right,
grid.height - style.margin.top - style.margin.bottom - style.border.top -
style.border.bottom,
*style.padding.color, lock);
TSMS_LIST_add(element->renderOperations, TSMS_GUI_createFillRectRenderOperation(
grid.x + grid.width - style.margin.right - style.border.right - style.padding.right,
grid.y - style.margin.top - style.border.top, style.padding.right,
grid.height - style.margin.top - style.margin.bottom - style.border.top - style.border.bottom));
}
if (!TSMS_UTIL_equalsColor(parentColor, *style.backgroundColor) || style.position == TSMS_STYLE_POSITION_ABSOLUTE) {
// render background color
TSMS_SCREEN_fillRectTopLeft(TSMS_GUI_getGui(element)->display->screen,
grid.x + style.margin.left + style.border.left + style.padding.left,
grid.y - style.margin.top - style.border.top - style.padding.top,
grid.width - style.margin.left - style.margin.right - style.border.left -
style.border.right - style.padding.left - style.padding.right,
grid.height - style.margin.top - style.margin.bottom - style.border.top -
style.border.bottom - style.padding.top - style.padding.bottom,
*style.backgroundColor, lock);
TSMS_LIST_add(element->renderOperations, TSMS_GUI_createFillRectRenderOperation(
grid.x + style.margin.left + style.border.left + style.padding.left,
grid.y - style.margin.top - style.border.top - style.padding.top,
grid.width - style.margin.left - style.margin.right - style.border.left - style.border.right -
style.padding.left - style.padding.right,
grid.height - style.margin.top - style.margin.bottom - style.border.top - style.border.bottom -
style.padding.top - style.padding.bottom));
}
return TSMS_SUCCESS;
}
bool TSMS_GUI_equalsGrid(TSMS_GRID_INFO grid1, TSMS_GRID_INFO grid2) {
return grid1.x == grid2.x && grid1.y == grid2.y && grid1.width == grid2.width && grid1.height == grid2.height;
}
pRenderOperation TSMS_GUI_createCharRenderOperation(uint16_t x, uint16_t y, TSMS_FONT_TYPE fontType, void *font, char c, TSMS_FONT_SIZE size) {
pRenderOperation operation = TSMS_malloc(sizeof(tRenderOperation));
if (operation == TSMS_NULL)
return TSMS_NULL;
operation->type = TSMS_RENDER_OPERATION_TYPE_CHAR;
operation->size = sizeof(uint16_t) + sizeof(uint16_t) + sizeof(TSMS_FONT_TYPE) + sizeof(void *) + sizeof(char) + sizeof(TSMS_FONT_SIZE);
operation->data = TSMS_malloc(operation->size);
if (operation->data == TSMS_NULL) {
free(operation);
return TSMS_NULL;
}
uint16_t offset = 0;
memcpy(operation->data + offset, &x, sizeof(uint16_t));
offset += sizeof(uint16_t);
memcpy(operation->data + offset, &y, sizeof(uint16_t));
offset += sizeof(uint16_t);
memcpy(operation->data + offset, &fontType, sizeof(TSMS_FONT_TYPE));
offset += sizeof(TSMS_FONT_TYPE);
memcpy(operation->data + offset, &font, sizeof(void *));
offset += sizeof(void *);
memcpy(operation->data + offset, &c, sizeof(char));
offset += sizeof(char);
memcpy(operation->data + offset, &size, sizeof(TSMS_FONT_SIZE));
return operation;
}
pRenderOperation TSMS_GUI_createFillRectRenderOperation(uint16_t x, uint16_t y, uint16_t width, uint16_t height) {
pRenderOperation operation = TSMS_malloc(sizeof(tRenderOperation));
if (operation == TSMS_NULL)
return TSMS_NULL;
operation->type = TSMS_RENDER_OPERATION_TYPE_FILL_RECT;
operation->size = sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint16_t);
operation->data = TSMS_malloc(operation->size);
if (operation->data == TSMS_NULL) {
free(operation);
return TSMS_NULL;
}
uint16_t offset = 0;
memcpy(operation->data + offset, &x, sizeof(uint16_t));
offset += sizeof(uint16_t);
memcpy(operation->data + offset, &y, sizeof(uint16_t));
offset += sizeof(uint16_t);
memcpy(operation->data + offset, &width, sizeof(uint16_t));
offset += sizeof(uint16_t);
memcpy(operation->data + offset, &height, sizeof(uint16_t));
return operation;
}
TSMS_RESULT TSMS_GUI_cancelRenderOperation(pGuiElement element, pRenderOperation operation, TSMS_STYLE style, pLock lock) {
if (operation == TSMS_NULL)
return TSMS_ERROR;
uint16_t offset = 0;
TSMS_COLOR parentColor = element->parent == TSMS_NULL ? *style.backgroundColor : *element->parent->computedStyle.backgroundColor;
if (operation->type == TSMS_RENDER_OPERATION_TYPE_CHAR) {
uint16_t x = *((uint16_t *) (operation->data + offset));
offset += sizeof(uint16_t);
uint16_t y = *((uint16_t *) (operation->data + offset));
offset += sizeof(uint16_t);
TSMS_FONT_TYPE fontType = *((TSMS_FONT_TYPE *) (operation->data + offset));
offset += sizeof(TSMS_FONT_TYPE);
void *font = *((void **) (operation->data + offset));
offset += sizeof(void *);
char c = *((char *) (operation->data + offset));
offset += sizeof(char);
TSMS_FONT_SIZE size = *((TSMS_FONT_SIZE *) (operation->data + offset));
TSMS_SCREEN_drawCharTopLeft(TSMS_GUI_getGui(element)->display->screen, x, y, fontType, font, c, *style.backgroundColor, size, lock);
} else if (operation->type == TSMS_RENDER_OPERATION_TYPE_FILL_RECT) {
uint16_t x = *((uint16_t *) (operation->data + offset));
offset += sizeof(uint16_t);
uint16_t y = *((uint16_t *) (operation->data + offset));
offset += sizeof(uint16_t);
uint16_t width = *((uint16_t *) (operation->data + offset));
offset += sizeof(uint16_t);
uint16_t height = *((uint16_t *) (operation->data + offset));
TSMS_SCREEN_fillRectTopLeft(TSMS_GUI_getGui(element)->display->screen, x, y, width, height, parentColor, lock);
}
return TSMS_SUCCESS;
}
TSMS_RESULT TSMS_GUI_releaseRenderOperation(pRenderOperation operation) {
if (operation == TSMS_NULL)
return TSMS_ERROR;
free(operation->data);
free(operation);
return TSMS_SUCCESS;
}
TSMS_RESULT TSMS_GUI_releaseGuiElement(pGuiElement element) {
if (element == TSMS_NULL)
return TSMS_ERROR;
TSMS_MUTABLE_STYLE_release(element->style);
TSMS_LIST_release(element->children);
if (element->renderOperations != TSMS_NULL)
for (TSMS_POS i = 0; i < element->renderOperations->length; i++) {
pRenderOperation operation = element->renderOperations->list[i];
TSMS_GUI_releaseRenderOperation(operation);
}
TSMS_LIST_release(element->renderOperations);
free(element);
return TSMS_SUCCESS;
}