-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlvglui.cpp
More file actions
2745 lines (2411 loc) · 87.1 KB
/
lvglui.cpp
File metadata and controls
2745 lines (2411 loc) · 87.1 KB
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Copyright (c) 2019-2025 plan44.ch / Lukas Zeller, Zurich, Switzerland
//
// Author: Lukas Zeller <luz@plan44.ch>
//
// This file is part of p44utils.
//
// p44utils is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// p44utils is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with p44utils. If not, see <http://www.gnu.org/licenses/>.
//
// File scope debugging options
// - Set ALWAYS_DEBUG to 1 to enable DBGLOG output even in non-DEBUG builds of this file
#define ALWAYS_DEBUG 0
// - set FOCUSLOGLEVEL to non-zero log level (usually, 5,6, or 7==LOG_DEBUG) to get focus (extensive logging) for this file
// Note: must be before including "logger.hpp" (or anything that includes "logger.hpp")
#define FOCUSLOGLEVEL 7
#include "lvglui.hpp"
#include "application.hpp"
using namespace p44;
#if ENABLE_LVGL
#if ENABLE_LVGLUI_SCRIPT_FUNCS
using namespace P44Script;
#endif
// MARK: - static utilities
static const char* getSymbolByName(const string aSymbolName)
{
if (aSymbolName=="audio") return LV_SYMBOL_AUDIO;
if (aSymbolName=="video") return LV_SYMBOL_VIDEO;
if (aSymbolName=="list") return LV_SYMBOL_LIST;
if (aSymbolName=="ok") return LV_SYMBOL_OK;
if (aSymbolName=="close") return LV_SYMBOL_CLOSE;
if (aSymbolName=="power") return LV_SYMBOL_POWER;
if (aSymbolName=="settings") return LV_SYMBOL_SETTINGS;
if (aSymbolName=="trash") return LV_SYMBOL_TRASH;
if (aSymbolName=="home") return LV_SYMBOL_HOME;
if (aSymbolName=="download") return LV_SYMBOL_DOWNLOAD;
if (aSymbolName=="drive") return LV_SYMBOL_DRIVE;
if (aSymbolName=="refresh") return LV_SYMBOL_REFRESH;
if (aSymbolName=="mute") return LV_SYMBOL_MUTE;
if (aSymbolName=="volume_mid") return LV_SYMBOL_VOLUME_MID;
if (aSymbolName=="volume_max") return LV_SYMBOL_VOLUME_MAX;
if (aSymbolName=="image") return LV_SYMBOL_IMAGE;
if (aSymbolName=="edit") return LV_SYMBOL_EDIT;
if (aSymbolName=="prev") return LV_SYMBOL_PREV;
if (aSymbolName=="play") return LV_SYMBOL_PLAY;
if (aSymbolName=="pause") return LV_SYMBOL_PAUSE;
if (aSymbolName=="stop") return LV_SYMBOL_STOP;
if (aSymbolName=="next") return LV_SYMBOL_NEXT;
if (aSymbolName=="eject") return LV_SYMBOL_EJECT;
if (aSymbolName=="left") return LV_SYMBOL_LEFT;
if (aSymbolName=="right") return LV_SYMBOL_RIGHT;
if (aSymbolName=="plus") return LV_SYMBOL_PLUS;
if (aSymbolName=="minus") return LV_SYMBOL_MINUS;
if (aSymbolName=="eye_open") return LV_SYMBOL_EYE_OPEN;
if (aSymbolName=="eye_close") return LV_SYMBOL_EYE_CLOSE;
if (aSymbolName=="warning") return LV_SYMBOL_WARNING;
if (aSymbolName=="shuffle") return LV_SYMBOL_SHUFFLE;
if (aSymbolName=="up") return LV_SYMBOL_UP;
if (aSymbolName=="down") return LV_SYMBOL_DOWN;
if (aSymbolName=="loop") return LV_SYMBOL_LOOP;
if (aSymbolName=="directory") return LV_SYMBOL_DIRECTORY;
if (aSymbolName=="upload") return LV_SYMBOL_UPLOAD;
if (aSymbolName=="call") return LV_SYMBOL_CALL;
if (aSymbolName=="cut") return LV_SYMBOL_CUT;
if (aSymbolName=="copy") return LV_SYMBOL_COPY;
if (aSymbolName=="save") return LV_SYMBOL_SAVE;
if (aSymbolName=="charge") return LV_SYMBOL_CHARGE;
if (aSymbolName=="paste") return LV_SYMBOL_PASTE;
if (aSymbolName=="bell") return LV_SYMBOL_BELL;
if (aSymbolName=="keyboard") return LV_SYMBOL_KEYBOARD;
if (aSymbolName=="gps") return LV_SYMBOL_GPS;
if (aSymbolName=="file") return LV_SYMBOL_FILE;
if (aSymbolName=="wifi") return LV_SYMBOL_WIFI;
if (aSymbolName=="battery_full") return LV_SYMBOL_BATTERY_FULL;
if (aSymbolName=="battery_3") return LV_SYMBOL_BATTERY_3;
if (aSymbolName=="battery_2") return LV_SYMBOL_BATTERY_2;
if (aSymbolName=="battery_1") return LV_SYMBOL_BATTERY_1;
if (aSymbolName=="battery_empty") return LV_SYMBOL_BATTERY_EMPTY;
if (aSymbolName=="usb") return LV_SYMBOL_USB;
if (aSymbolName=="bluetooth") return LV_SYMBOL_BLUETOOTH;
if (aSymbolName=="backspace") return LV_SYMBOL_BACKSPACE;
if (aSymbolName=="sd_card") return LV_SYMBOL_SD_CARD;
if (aSymbolName=="new_line") return LV_SYMBOL_NEW_LINE;
return "";
}
static lv_state_t getStateByName(const string aState)
{
// states
if (aState=="checked") return LV_STATE_CHECKED;
if (aState=="focused") return LV_STATE_FOCUSED;
if (aState=="keyfocused") return LV_STATE_FOCUS_KEY;
if (aState=="edited") return LV_STATE_EDITED;
if (aState=="hovered") return LV_STATE_HOVERED;
if (aState=="pressed") return LV_STATE_PRESSED;
if (aState=="scrolled") return LV_STATE_SCROLLED;
if (aState=="disabled") return LV_STATE_DISABLED;
if (aState=="user1") return LV_STATE_USER_1;
if (aState=="user2") return LV_STATE_USER_2;
if (aState=="user3") return LV_STATE_USER_3;
if (aState=="user4") return LV_STATE_USER_4;
return LV_STATE_DEFAULT;
}
static bool getSelectorByList(const string aStateList, lv_style_selector_t &aSelector)
{
const char* p = aStateList.c_str();
string tok;
lv_state_t state = LV_STATE_DEFAULT;
lv_part_t part = LV_PART_MAIN;
while (nextPart(p, tok, '|')) {
// states?
lv_state_t nextstate = getStateByName(tok);
if (nextstate!=LV_STATE_DEFAULT) {
state |= nextstate;
}
else {
// part?
if (tok=="main") part = LV_PART_MAIN;
else if (tok=="scrollbar") part = LV_PART_SCROLLBAR;
else if (tok=="indicator") part = LV_PART_INDICATOR;
else if (tok=="knob") part = LV_PART_KNOB;
else if (tok=="selected") part = LV_PART_SELECTED;
else if (tok=="items") part = LV_PART_ITEMS;
else if (tok=="cursor") part = LV_PART_CURSOR;
else if (tok=="any") part = LV_PART_ANY;
else return false;
}
}
aSelector = part | state;
return true;
}
static void getStateChangesByList(const string aStatesList, uint16_t& aStatesToSet, uint16_t& aStatesToClear)
{
const char* p = aStatesList.c_str();
string part;
aStatesToSet = 0;
aStatesToClear = 0;
while (nextPart(p, part, ',')) {
bool del = false;
if (part[0]=='-') { del = true; part.erase(0,1); }
else if (part[0]=='+') { part.erase(0,1); }
lv_state_t state = getStateByName(part);
if (del) aStatesToSet |= state;
else aStatesToClear |= state;
}
}
static void getFlagChangesByList(const string aFlagsList, uint32_t& aFlagsToSet, uint32_t& aFlagsToClear)
{
const char* p = aFlagsList.c_str();
string part;
aFlagsToSet = 0;
aFlagsToClear = 0;
while (nextPart(p, part, ',')) {
bool del = false;
uint32_t flag = 0;
if (part[0]=='-') { del = true; part.erase(0,1); }
else if (part[0]=='+') { part.erase(0,1); }
if (part=="hidden") flag = LV_OBJ_FLAG_HIDDEN;
else if (part=="clickable") flag = LV_OBJ_FLAG_CLICKABLE;
else if (part=="focusable") flag = LV_OBJ_FLAG_CLICK_FOCUSABLE;
else if (part=="checkable") flag = LV_OBJ_FLAG_CHECKABLE;
else if (part=="scrollable") flag = LV_OBJ_FLAG_SCROLLABLE;
else if (part=="elastic") flag = LV_OBJ_FLAG_SCROLL_ELASTIC;
else if (part=="momentum") flag = LV_OBJ_FLAG_SCROLL_MOMENTUM;
else if (part=="scroll_one") flag = LV_OBJ_FLAG_SCROLL_ONE;
else if (part=="scroll_chain_x") flag = LV_OBJ_FLAG_SCROLL_CHAIN_HOR;
else if (part=="scroll_chain_y") flag = LV_OBJ_FLAG_SCROLL_CHAIN_VER;
else if (part=="scroll_chain") flag = LV_OBJ_FLAG_SCROLL_CHAIN;
else if (part=="scroll_onfocus") flag = LV_OBJ_FLAG_SCROLL_ON_FOCUS;
else if (part=="scroll_witharrow") flag = LV_OBJ_FLAG_SCROLL_WITH_ARROW;
else if (part=="snappable") flag = LV_OBJ_FLAG_SNAPPABLE;
else if (part=="presslock") flag = LV_OBJ_FLAG_PRESS_LOCK;
else if (part=="eventbubble") flag = LV_OBJ_FLAG_EVENT_BUBBLE;
else if (part=="gesturebubble") flag = LV_OBJ_FLAG_GESTURE_BUBBLE;
else if (part=="hit_test") flag = LV_OBJ_FLAG_ADV_HITTEST;
else if (part=="ignore_layout") flag = LV_OBJ_FLAG_IGNORE_LAYOUT;
else if (part=="floating") flag = LV_OBJ_FLAG_FLOATING;
else if (part=="drawtaskevents") flag = LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS;
else if (part=="overflowvisible") flag = LV_OBJ_FLAG_OVERFLOW_VISIBLE;
else if (part=="newtrack") flag = LV_OBJ_FLAG_FLEX_IN_NEW_TRACK;
else if (part=="layout1") flag = LV_OBJ_FLAG_LAYOUT_1;
else if (part=="layout2") flag = LV_OBJ_FLAG_LAYOUT_2;
else if (part=="widget1") flag = LV_OBJ_FLAG_WIDGET_1;
else if (part=="widget2") flag = LV_OBJ_FLAG_WIDGET_2;
else if (part=="user1") flag = LV_OBJ_FLAG_USER_1;
else if (part=="user2") flag = LV_OBJ_FLAG_USER_2;
else if (part=="user3") flag = LV_OBJ_FLAG_USER_3;
else if (part=="user4") flag = LV_OBJ_FLAG_USER_4;
if (del) aFlagsToClear |= flag;
else aFlagsToSet |= flag;
}
}
// MARK: style properties
static ErrorPtr intPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
aStyleValue.num = aJsonValue->int32Value();
return ErrorPtr();
}
static ErrorPtr coordPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
// strings might be values in other units than pixels
lv_coord_t coord = 0;
if (aJsonValue->isType(json_type_string)) {
string s = aJsonValue->stringValue();
int32_t v;
char unit = 0;
if (sscanf(s.c_str(), "%d%c", &v, &unit)>=1) {
if (unit=='%') coord = lv_pct(v);
else coord = v; // no other recognized units so far
}
}
else {
return intPropValue(aJsonValue, aStyleValue, aLvglUI);
}
aStyleValue.num = coord;
return ErrorPtr();
}
static ErrorPtr radiusPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
if (aJsonValue->stringValue()!="circle") return coordPropValue(aJsonValue, aStyleValue, aLvglUI);
aStyleValue.num = LV_RADIUS_CIRCLE;
return ErrorPtr();
}
static ErrorPtr scalePropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
aStyleValue.num = aJsonValue->doubleValue()*256;
return ErrorPtr();
}
static ErrorPtr anglePropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
aStyleValue.num = aJsonValue->doubleValue()*10;
return ErrorPtr();
}
static ErrorPtr per255PropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
aStyleValue.num = aJsonValue->doubleValue()*2.55; // 0..100 -> 0..255
return ErrorPtr();
}
static ErrorPtr boolPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
aStyleValue.num = aJsonValue->boolValue();
return ErrorPtr();
}
static ErrorPtr alignPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
const string alignMode = aJsonValue->stringValue();
const char *p = alignMode.c_str();
bool in = true; // default to in
bool top = false;
bool mid = false;
bool bottom = false;
bool left = false;
bool right = false;
string tok;
lv_align_t align;
if (alignMode=="center") align = LV_ALIGN_CENTER; // special case not combinable from the elements below
else {
while (nextPart(p, tok, ',')) {
if (tok=="top") top = true;
else if (tok=="mid") mid = true;
else if (tok=="bottom") bottom = true;
else if (tok=="left") left = true;
else if (tok=="right") right = true;
else if (tok=="in") in = true;
else if (tok=="out") in = false;
}
if (in && top && left) align = LV_ALIGN_TOP_LEFT;
else if (in && top && mid) align = LV_ALIGN_TOP_MID;
else if (in && top && right) align = LV_ALIGN_TOP_RIGHT;
else if (in && bottom && left) align = LV_ALIGN_BOTTOM_LEFT;
else if (in && bottom && mid) align = LV_ALIGN_BOTTOM_MID;
else if (in && bottom && right) align = LV_ALIGN_BOTTOM_RIGHT;
else if (in && left && mid) align = LV_ALIGN_LEFT_MID;
else if (in && right && mid) align = LV_ALIGN_RIGHT_MID;
else if (!in && top && left) align = LV_ALIGN_OUT_TOP_LEFT;
else if (!in && top && mid) align = LV_ALIGN_OUT_TOP_MID;
else if (!in && top && right) align = LV_ALIGN_OUT_TOP_RIGHT;
else if (!in && bottom && left) align = LV_ALIGN_OUT_BOTTOM_LEFT;
else if (!in && bottom && mid) align = LV_ALIGN_OUT_BOTTOM_MID;
else if (!in && bottom && right) align = LV_ALIGN_OUT_BOTTOM_RIGHT;
else if (!in && left && top) align = LV_ALIGN_OUT_LEFT_TOP;
else if (!in && left && mid) align = LV_ALIGN_OUT_LEFT_MID;
else if (!in && left && bottom) align = LV_ALIGN_OUT_LEFT_BOTTOM;
else if (!in && right && top) align = LV_ALIGN_OUT_RIGHT_TOP;
else if (!in && right && mid) align = LV_ALIGN_OUT_RIGHT_MID;
else if (!in && right && bottom) align = LV_ALIGN_OUT_RIGHT_BOTTOM;
else return TextError::err("unknown align mode '%s'", alignMode.c_str());
}
aStyleValue.num = align;
return ErrorPtr();
}
static ErrorPtr fontPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
const string fontName = aJsonValue->stringValue();
const lv_font_t* font = nullptr;
if (false) {}
#if LV_FONT_MONTSERRAT_8
else if (fontName=="montserrat8") font = &lv_font_montserrat_8;
#endif
#if LV_FONT_MONTSERRAT_10
else if (fontName=="montserrat10") font = &lv_font_montserrat_10;
#endif
#if LV_FONT_MONTSERRAT_12
else if (fontName=="montserrat12") font = &lv_font_montserrat_12;
#endif
#if LV_FONT_MONTSERRAT_14
else if (fontName=="montserrat14") font = &lv_font_montserrat_14;
#endif
#if LV_FONT_MONTSERRAT_16
else if (fontName=="montserrat16") font = &lv_font_montserrat_16;
#endif
#if LV_FONT_MONTSERRAT_18
else if (fontName=="montserrat18") font = &lv_font_montserrat_18;
#endif
#if LV_FONT_MONTSERRAT_20
else if (fontName=="montserrat20") font = &lv_font_montserrat_20;
#endif
#if LV_FONT_MONTSERRAT_22
else if (fontName=="montserrat22") font = &lv_font_montserrat_22;
#endif
#if LV_FONT_MONTSERRAT_24
else if (fontName=="montserrat24") font = &lv_font_montserrat_24;
#endif
#if LV_FONT_MONTSERRAT_26
else if (fontName=="montserrat26") font = &lv_font_montserrat_26;
#endif
#if LV_FONT_MONTSERRAT_28
else if (fontName=="montserrat28") font = &lv_font_montserrat_28;
#endif
#if LV_FONT_MONTSERRAT_30
else if (fontName=="montserrat30") font = &lv_font_montserrat_30;
#endif
#if LV_FONT_MONTSERRAT_32
else if (fontName=="montserrat32") font = &lv_font_montserrat_32;
#endif
#if LV_FONT_MONTSERRAT_34
else if (fontName=="montserrat34") font = &lv_font_montserrat_34;
#endif
#if LV_FONT_MONTSERRAT_36
else if (fontName=="montserrat36") font = &lv_font_montserrat_36;
#endif
#if LV_FONT_MONTSERRAT_38
else if (fontName=="montserrat38") font = &lv_font_montserrat_38;
#endif
#if LV_FONT_MONTSERRAT_40
else if (fontName=="montserrat40") font = &lv_font_montserrat_40;
#endif
#if LV_FONT_MONTSERRAT_42
else if (fontName=="montserrat42") font = &lv_font_montserrat_42;
#endif
#if LV_FONT_MONTSERRAT_44
else if (fontName=="montserrat44") font = &lv_font_montserrat_44;
#endif
#if LV_FONT_MONTSERRAT_46
else if (fontName=="montserrat46") font = &lv_font_montserrat_46;
#endif
#if LV_FONT_MONTSERRAT_48
else if (fontName=="montserrat48") font = &lv_font_montserrat_48;
#endif
else {
return TextError::err("unknown font '%s'", fontName.c_str());
}
aStyleValue.ptr = font;
return ErrorPtr();
}
static lv_palette_t getPaletteEntryFromColorSpec(const string aPaletteColor)
{
if (aPaletteColor=="red") return LV_PALETTE_RED;
if (aPaletteColor=="pink") return LV_PALETTE_PINK;
if (aPaletteColor=="purple") return LV_PALETTE_PURPLE;
if (aPaletteColor=="deeppurple") return LV_PALETTE_DEEP_PURPLE;
if (aPaletteColor=="red") return LV_PALETTE_INDIGO;
if (aPaletteColor=="blue") return LV_PALETTE_BLUE;
if (aPaletteColor=="lightblue") return LV_PALETTE_LIGHT_BLUE;
if (aPaletteColor=="cyan") return LV_PALETTE_CYAN;
if (aPaletteColor=="teal") return LV_PALETTE_TEAL;
if (aPaletteColor=="green") return LV_PALETTE_GREEN;
if (aPaletteColor=="lightgreen") return LV_PALETTE_LIGHT_GREEN;
if (aPaletteColor=="lime") return LV_PALETTE_LIME;
if (aPaletteColor=="yellow") return LV_PALETTE_YELLOW;
if (aPaletteColor=="amber") return LV_PALETTE_AMBER;
if (aPaletteColor=="orange") return LV_PALETTE_ORANGE;
if (aPaletteColor=="deeporange") return LV_PALETTE_DEEP_ORANGE;
if (aPaletteColor=="brown") return LV_PALETTE_BROWN;
if (aPaletteColor=="bluegrey" || aPaletteColor=="bluegray") return LV_PALETTE_BLUE_GREY;
if (aPaletteColor=="grey" || aPaletteColor=="gray") return LV_PALETTE_GREY;
return LV_PALETTE_NONE;
}
static ErrorPtr colorPropValue(JsonObjectPtr aJsonValue, bool& aHasColor, lv_style_value_t& aColorValue, bool& aHasOpacity, lv_style_value_t& aOpaValue)
{
const string colorSpec = aJsonValue->stringValue();
int r = 0, g = 0, b = 0;
lv_opa_t a;
aHasOpacity = false;
aHasColor = true;
size_t n = colorSpec.size();
lv_color_t color;
do {
if (n>0) {
if (colorSpec[0]!='#') {
// check palette
// syntax <palettecolorname>[+<lighten_level>|-<darken_level>]
// - find adjustment
size_t i = colorSpec.find_first_of("+-");
int32_t adj = 0;
if (i!=string::npos) {
sscanf(colorSpec.c_str()+i, "%d", &adj);
}
else {
i = colorSpec.size();
}
// - find color
string colname = colorSpec.substr(0, i);
if (colname=="black") { color = lv_color_black(); break; }
else if (colname=="white") { color = lv_color_white(); break; }
else if (colname=="transparent") {
aHasOpacity = true;
a = 0;
color = lv_color_black();
break;
}
lv_palette_t pi = getPaletteEntryFromColorSpec(colname);
if (pi!=LV_PALETTE_NONE) {
if (adj==0) color = lv_palette_main(pi);
else if (adj>0) color = lv_palette_lighten(pi, adj);
else color =lv_palette_darken(pi, -adj);
break;
}
return TextError::err("unknown color '%s'", colorSpec.c_str());
}
else {
// web color
// syntax: #rrggbb or #rgb or #aarrggbb or #argb
n--; // string size without # !
uint32_t h;
if (sscanf(colorSpec.c_str()+1, "%x", &h)==1) {
if (n==1) {
// alpha-only single digit
a = h&0xF; a |= a<<4;
aHasOpacity = true;
aHasColor = false;
}
else if (n==2) {
// alpha-only double digit
a = h&0xFF;
aHasOpacity = true;
aHasColor = false;
}
else if (n<=4) {
// short form RGB or ARGB
if (n==4) { aHasOpacity = true; a = (h>>12)&0xF; a |= a<<4; }
r = (h>>8)&0xF; r |= r<<4;
g = (h>>4)&0xF; g |= g<<4;
b = (h>>0)&0xF; b |= b<<4;
}
else {
// long form RRGGBB or AARRGGBB
a = 255;
if (n==8) { aHasOpacity = true; a = (h>>24)&0xFF; }
r = (h>>16)&0xFF;
g = (h>>8)&0xFF;
b = (h>>0)&0xFF;
}
color = lv_color_make(r,g,b);
}
else {
return TextError::err("unknown color '%s'", colorSpec.c_str());
}
}
}
} while(false);
if (aHasOpacity) aOpaValue.num = a;
aColorValue.color = color;
return ErrorPtr();
}
static ErrorPtr colorPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
lv_style_value_t opa;
bool hasOpa, hasColor;
ErrorPtr err = colorPropValue(aJsonValue, hasColor, aStyleValue, hasOpa, opa);
if (Error::isOK(err)) {
if (!hasColor) aStyleValue.color = lv_color_black();
}
return err;
}
static ErrorPtr gradientDirPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
const string gradSpec = aJsonValue->stringValue();
lv_grad_dir_t grad;
if (gradSpec=="vertical") grad = LV_GRAD_DIR_VER;
else if (gradSpec=="horizontal") grad = LV_GRAD_DIR_HOR;
else if (gradSpec=="linear") grad = LV_GRAD_DIR_LINEAR;
else if (gradSpec=="radial") grad = LV_GRAD_DIR_RADIAL;
else if (gradSpec=="conical") grad = LV_GRAD_DIR_CONICAL;
else if (gradSpec=="none") grad = LV_GRAD_DIR_NONE;
else {
return TextError::err("unknown gradient direction '%s'", gradSpec.c_str());
}
aStyleValue.num = grad;
return ErrorPtr();
}
static ErrorPtr borderSidesPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
const string s = aJsonValue->stringValue();
const char* p = s.c_str();
string part;
uint8_t sides = LV_BORDER_SIDE_NONE;
while (nextPart(p, part, ',')) {
if (part=="bottom") sides |= LV_BORDER_SIDE_BOTTOM;
else if (part=="top") sides |= LV_BORDER_SIDE_TOP;
else if (part=="left") sides |= LV_BORDER_SIDE_LEFT;
else if (part=="right") sides |= LV_BORDER_SIDE_RIGHT;
else if (part=="all") sides |= LV_BORDER_SIDE_FULL;
else if (part=="internal") sides |= LV_BORDER_SIDE_INTERNAL;
else return TextError::err("unknown border part '%s'", part.c_str());
}
aStyleValue.num = sides;
return ErrorPtr();
}
static ErrorPtr textDecorPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
const string s = aJsonValue->stringValue();
const char* p = s.c_str();
string part;
uint8_t decor = LV_TEXT_DECOR_NONE;
while (nextPart(p, part, ',')) {
if (part=="underline") decor |= LV_TEXT_DECOR_UNDERLINE;
else if (part=="strikethrough") decor |= LV_TEXT_DECOR_STRIKETHROUGH;
else return TextError::err("unknown text decor '%s'", part.c_str());
}
aStyleValue.num = decor;
return ErrorPtr();
}
static ErrorPtr textAlignPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
const string textAlign = aJsonValue->stringValue();
lv_text_align_t align;
if (textAlign=="left") align = LV_TEXT_ALIGN_LEFT;
else if (textAlign=="center") align = LV_TEXT_ALIGN_CENTER;
else if (textAlign=="right") align = LV_TEXT_ALIGN_RIGHT;
else if (textAlign=="auto") align = LV_TEXT_ALIGN_AUTO;
else return TextError::err("unknown text alignment '%s'", textAlign.c_str());
aStyleValue.num = align;
return ErrorPtr();
}
static ErrorPtr blendModePropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
const string blendMode = aJsonValue->stringValue();
lv_blend_mode_t blend;
if (blendMode=="additive") blend = LV_BLEND_MODE_ADDITIVE;
else if (blendMode=="subtractive") blend = LV_BLEND_MODE_SUBTRACTIVE;
else if (blendMode=="multiply") blend = LV_BLEND_MODE_MULTIPLY;
else if (blendMode=="normal") blend = LV_BLEND_MODE_NORMAL;
else return TextError::err("unknown blend mode '%s'", blendMode.c_str());
aStyleValue.num = blend;
return ErrorPtr();
}
static ErrorPtr layoutPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
const string layoutName = aJsonValue->stringValue();
lv_layout_t layout;
if (layoutName=="flex") layout = LV_LAYOUT_FLEX;
else if (layoutName=="grid") layout = LV_LAYOUT_GRID;
else if (layoutName=="none") layout = LV_LAYOUT_NONE;
else return TextError::err("unknown layout '%s'", layoutName.c_str());
aStyleValue.num = layout;
return ErrorPtr();
}
static ErrorPtr flexAlignPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
const string flexAlign = aJsonValue->stringValue();
lv_flex_align_t align;
if (flexAlign=="start") align = LV_FLEX_ALIGN_START;
else if (flexAlign=="end") align = LV_FLEX_ALIGN_END;
else if (flexAlign=="evenly") align = LV_FLEX_ALIGN_SPACE_EVENLY;
else if (flexAlign=="around") align = LV_FLEX_ALIGN_SPACE_AROUND;
else if (flexAlign=="between") align = LV_FLEX_ALIGN_SPACE_BETWEEN;
else if (flexAlign=="center") align = LV_FLEX_ALIGN_CENTER;
else return TextError::err("unknown flex align '%s'", flexAlign.c_str());
aStyleValue.num = align;
return ErrorPtr();
}
static ErrorPtr flexFlowPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
const string flexFlow = aJsonValue->stringValue();
const char* p = flexFlow.c_str();
string part;
uint8_t flow = LV_FLEX_FLOW_ROW;
while (nextPart(p, part, ',')) {
if (part=="column") flow |= LV_FLEX_COLUMN;
else if (part=="wrap") flow |= LV_FLEX_WRAP;
else if (part=="reverse") flow |= LV_FLEX_REVERSE;
else if (part=="row");
else return TextError::err("unknown flex flow '%s'", flexFlow.c_str());
}
aStyleValue.num = flow;
return ErrorPtr();
}
static ErrorPtr gridAlignPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
const string gridAlign = aJsonValue->stringValue();
lv_grid_align_t align;
if (gridAlign=="start") align = LV_GRID_ALIGN_START;
else if (gridAlign=="end") align = LV_GRID_ALIGN_END;
else if (gridAlign=="evenly") align = LV_GRID_ALIGN_SPACE_EVENLY;
else if (gridAlign=="around") align = LV_GRID_ALIGN_SPACE_AROUND;
else if (gridAlign=="between") align = LV_GRID_ALIGN_SPACE_BETWEEN;
else if (gridAlign=="center") align = LV_GRID_ALIGN_CENTER;
else return TextError::err("unknown grid align '%s'", gridAlign.c_str());
aStyleValue.num = align;
return ErrorPtr();
}
static ErrorPtr gridTemplateArrayPropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
int n = aJsonValue->arrayLength();
int32_t* aGridTemplateP = nullptr;
if (n>0) {
aGridTemplateP = new int32_t[n+1]; // plus terminator
for (int i = 0; i<n; i++) {
aGridTemplateP[i] = aJsonValue->arrayGet(i)->int32Value();
}
aGridTemplateP[n] = LV_GRID_TEMPLATE_LAST;
}
aStyleValue.ptr = aGridTemplateP;
return ErrorPtr();
}
static ErrorPtr imagePropValue(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI)
{
ErrorPtr err;
string imgsrc = aLvglUI.namedImageSource(aJsonValue->stringValue(), true, err);
if (Error::isOK(err)) {
// FIXME: this will never get freed, so redefining styles can leak memory
aStyleValue.ptr = malloc(imgsrc.size()+1);
strncpy((char *)aStyleValue.ptr, imgsrc.c_str(), imgsrc.size()+1);
}
return err;
}
typedef ErrorPtr (*PropValueConverter)(JsonObjectPtr aJsonValue, lv_style_value_t& aStyleValue, LvGLUi& aLvglUI);
typedef struct {
const char* propname;
lv_style_prop_t propid;
lv_style_prop_t opa_propid;
PropValueConverter propconv;
} PropDef;
enum {
P44_STYLE_PAD_ALL=LV_STYLE_LAST_BUILT_IN_PROP+1,
P44_STYLE_MARGIN_ALL=LV_STYLE_LAST_BUILT_IN_PROP+2
};
static const PropDef propDefs[] = {
{ "x", LV_STYLE_X, 0, coordPropValue },
{ "y", LV_STYLE_Y, 0, coordPropValue },
{ "dx", LV_STYLE_WIDTH, 0, coordPropValue },
{ "min_dx", LV_STYLE_MIN_WIDTH, 0, coordPropValue },
{ "max_dx", LV_STYLE_MAX_WIDTH, 0, coordPropValue },
{ "dy", LV_STYLE_HEIGHT, 0, coordPropValue },
{ "min_dy", LV_STYLE_MIN_HEIGHT, 0, coordPropValue },
{ "max_dy", LV_STYLE_MAX_HEIGHT, 0, coordPropValue },
{ "length", LV_STYLE_LENGTH, 0, coordPropValue },
{ "align", LV_STYLE_LENGTH, 0, alignPropValue },
{ "transform_dx", LV_STYLE_TRANSFORM_WIDTH, 0, coordPropValue },
{ "transform_dy", LV_STYLE_TRANSFORM_HEIGHT, 0, coordPropValue },
{ "translate_x", LV_STYLE_TRANSLATE_X, 0, coordPropValue },
{ "translate_y", LV_STYLE_TRANSLATE_Y, 0, coordPropValue },
{ "scale_x", LV_STYLE_TRANSFORM_SCALE_X, 0, scalePropValue },
{ "scale_y", LV_STYLE_TRANSFORM_SCALE_Y, 0, scalePropValue },
{ "rotation", LV_STYLE_TRANSFORM_ROTATION, 0, anglePropValue },
{ "pivot_x", LV_STYLE_TRANSFORM_PIVOT_X, 0, coordPropValue },
{ "pivot_y", LV_STYLE_TRANSFORM_PIVOT_Y, 0, coordPropValue },
{ "skew_x", LV_STYLE_TRANSFORM_SKEW_X, 0, anglePropValue },
{ "skew_y", LV_STYLE_TRANSFORM_SKEW_Y, 0, anglePropValue },
{ "padding", P44_STYLE_PAD_ALL, 0, coordPropValue },
{ "padding_top", LV_STYLE_PAD_TOP, 0, coordPropValue },
{ "padding_bottom", LV_STYLE_PAD_BOTTOM, 0, coordPropValue },
{ "padding_left", LV_STYLE_PAD_LEFT, 0, coordPropValue },
{ "padding_right", LV_STYLE_PAD_RIGHT, 0, coordPropValue },
{ "padding_row", LV_STYLE_PAD_ROW, 0, coordPropValue },
{ "padding_column", LV_STYLE_PAD_COLUMN, 0, coordPropValue },
{ "margin", P44_STYLE_MARGIN_ALL, 0, coordPropValue },
{ "margin_top", LV_STYLE_MARGIN_TOP, 0, coordPropValue },
{ "margin_bottom", LV_STYLE_MARGIN_BOTTOM, 0, coordPropValue },
{ "margin_left", LV_STYLE_MARGIN_LEFT, 0, coordPropValue },
{ "margin_right", LV_STYLE_MARGIN_RIGHT, 0, coordPropValue },
{ "color", LV_STYLE_BG_COLOR, LV_STYLE_BG_OPA, nullptr },
{ "color_main", LV_STYLE_BG_COLOR, LV_STYLE_BG_MAIN_OPA, nullptr },
{ "gradient_color", LV_STYLE_BG_GRAD_COLOR, LV_STYLE_BG_GRAD_OPA, nullptr },
{ "gradient_dir", LV_STYLE_BG_GRAD_DIR, 0, gradientDirPropValue },
{ "gradient_start", LV_STYLE_BG_MAIN_STOP, 0, per255PropValue },
{ "gradient_stop", LV_STYLE_BG_GRAD_STOP, 0, per255PropValue },
{ "bg_image", LV_STYLE_BG_IMAGE_SRC, 0, imagePropValue },
{ "bg_recoloring", LV_STYLE_BG_IMAGE_RECOLOR, LV_STYLE_BG_IMAGE_RECOLOR_OPA, nullptr },
{ "bg_tiled", LV_STYLE_BG_IMAGE_TILED, 0, boolPropValue },
{ "border_color", LV_STYLE_BORDER_COLOR, LV_STYLE_BORDER_OPA, nullptr },
{ "border_width", LV_STYLE_BORDER_WIDTH, 0, coordPropValue },
{ "border_sides", LV_STYLE_BORDER_SIDE, 0, borderSidesPropValue },
{ "border_post", LV_STYLE_BORDER_POST, 0, boolPropValue },
{ "outline_color", LV_STYLE_OUTLINE_COLOR, LV_STYLE_OUTLINE_OPA, nullptr },
{ "outline_width", LV_STYLE_OUTLINE_WIDTH, 0, coordPropValue },
{ "outline_pad", LV_STYLE_OUTLINE_PAD, 0, coordPropValue },
{ "shadow_color", LV_STYLE_SHADOW_COLOR, LV_STYLE_SHADOW_OPA, nullptr },
{ "shadow_width", LV_STYLE_SHADOW_WIDTH, 0, coordPropValue },
{ "shadow_dx", LV_STYLE_SHADOW_OFFSET_X, 0, coordPropValue },
{ "shadow_dy", LV_STYLE_SHADOW_OFFSET_Y, 0, coordPropValue },
{ "shadow_spread", LV_STYLE_SHADOW_OFFSET_Y, 0, coordPropValue },
{ "image_alpha", LV_STYLE_IMAGE_OPA, 0, intPropValue },
{ "image_recoloring", LV_STYLE_IMAGE_RECOLOR, LV_STYLE_IMAGE_RECOLOR_OPA, nullptr },
{ "line_color", LV_STYLE_LINE_COLOR, LV_STYLE_LINE_OPA, nullptr },
{ "line_width", LV_STYLE_LINE_WIDTH, 0, coordPropValue },
{ "line_dash", LV_STYLE_LINE_DASH_WIDTH, 0, coordPropValue },
{ "line_gap", LV_STYLE_LINE_DASH_GAP, 0, coordPropValue },
{ "line_rounded", LV_STYLE_LINE_ROUNDED, 0, boolPropValue },
{ "arc_color", LV_STYLE_ARC_COLOR, LV_STYLE_ARC_OPA, nullptr },
{ "arc_width", LV_STYLE_ARC_WIDTH, 0, coordPropValue },
{ "arc_rounded", LV_STYLE_ARC_ROUNDED, 0, boolPropValue },
{ "arc_image", LV_STYLE_ARC_IMAGE_SRC, 0, imagePropValue },
{ "text_color", LV_STYLE_TEXT_COLOR, LV_STYLE_TEXT_OPA, nullptr },
{ "font", LV_STYLE_TEXT_FONT, 0, fontPropValue },
{ "text_letter_space", LV_STYLE_TEXT_LETTER_SPACE, 0, coordPropValue },
{ "text_line_space", LV_STYLE_TEXT_LINE_SPACE, 0, coordPropValue },
{ "text_decor", LV_STYLE_TEXT_DECOR, 0, textDecorPropValue },
{ "text_align", LV_STYLE_TEXT_ALIGN, 0, textAlignPropValue },
{ "radius", LV_STYLE_RADIUS, 0, radiusPropValue },
{ "clip_corner", LV_STYLE_CLIP_CORNER, 0, boolPropValue },
{ "alpha", LV_STYLE_OPA, 0, intPropValue },
{ "alpha_layered", LV_STYLE_OPA_LAYERED, 0, intPropValue },
// TODO: implement
// - color_filter_dsc/color_filter_opa
// - anim/anim_duration
// - transition
{ "blend_mode", LV_STYLE_BLEND_MODE, 0, blendModePropValue },
{ "layout", LV_STYLE_LAYOUT, 0, layoutPropValue },
{ "flex_flow", LV_STYLE_FLEX_FLOW, 0, flexFlowPropValue },
{ "flex_main_place", LV_STYLE_FLEX_MAIN_PLACE, 0, flexAlignPropValue },
{ "flex_cross_place", LV_STYLE_FLEX_CROSS_PLACE, 0, flexAlignPropValue },
{ "flex_track_place", LV_STYLE_FLEX_TRACK_PLACE, 0, flexAlignPropValue },
{ "flex_grow", LV_STYLE_FLEX_GROW, 0, coordPropValue }, // TODO: unit unclear!!
{ "grid_columns", LV_STYLE_GRID_COLUMN_DSC_ARRAY, 0, gridTemplateArrayPropValue },
{ "grid_column_align", LV_STYLE_GRID_COLUMN_ALIGN, 0, gridAlignPropValue },
{ "grid_rows", LV_STYLE_GRID_ROW_DSC_ARRAY, 0, gridTemplateArrayPropValue },
{ "grid_row_align", LV_STYLE_GRID_ROW_ALIGN, 0, gridAlignPropValue },
{ "grid_x", LV_STYLE_GRID_CELL_COLUMN_POS, 0, coordPropValue },
{ "grid_x_align", LV_STYLE_GRID_CELL_X_ALIGN, 0, gridAlignPropValue },
{ "grid_dx", LV_STYLE_GRID_CELL_COLUMN_SPAN, 0, coordPropValue },
{ "grid_y", LV_STYLE_GRID_CELL_ROW_POS, 0, coordPropValue },
{ "grid_y_align", LV_STYLE_GRID_CELL_Y_ALIGN, 0, gridAlignPropValue },
{ "grid_dy", LV_STYLE_GRID_CELL_ROW_SPAN, 0, coordPropValue },
// Terminator
{ nullptr, 0 }
};
static const PropDef* getPropDefFromName(const string aStylePropName)
{
const PropDef* propDef = propDefs;
while (propDef->propname) {
if (uequals(aStylePropName, propDef->propname)) {
return propDef;
}
propDef++;
}
return nullptr;
}
static lv_event_code_t getEventCodeFromName(const string aEventName)
{
if (aEventName=="press") return LV_EVENT_PRESSED;
if (aEventName=="pressing") return LV_EVENT_PRESSING;
if (aEventName=="lost") return LV_EVENT_PRESS_LOST;
if (aEventName=="shortclick") return LV_EVENT_SHORT_CLICKED;
if (aEventName=="longpress") return LV_EVENT_LONG_PRESSED;
if (aEventName=="longpress_repeat") return LV_EVENT_LONG_PRESSED_REPEAT;
if (aEventName=="click") return LV_EVENT_CLICKED;
if (aEventName=="release") return LV_EVENT_RELEASED;
if (aEventName=="scroll_begin") return LV_EVENT_SCROLL_BEGIN;
if (aEventName=="scroll_throw") return LV_EVENT_SCROLL_THROW_BEGIN;
if (aEventName=="scroll_end") return LV_EVENT_SCROLL_END;
if (aEventName=="scroll") return LV_EVENT_SCROLL;
if (aEventName=="gesture") return LV_EVENT_GESTURE;
if (aEventName=="key") return LV_EVENT_KEY;
if (aEventName=="focus") return LV_EVENT_FOCUSED;
if (aEventName=="defocus") return LV_EVENT_DEFOCUSED;
if (aEventName=="leave") return LV_EVENT_LEAVE;
if (aEventName=="hover") return LV_EVENT_HOVER_OVER;
if (aEventName=="endhover") return LV_EVENT_HOVER_LEAVE;
if (aEventName=="change") return LV_EVENT_VALUE_CHANGED;
if (aEventName=="insert") return LV_EVENT_INSERT;
if (aEventName=="refresh") return LV_EVENT_REFRESH;
if (aEventName=="ready") return LV_EVENT_READY;
if (aEventName=="cancel") return LV_EVENT_CANCEL;
if (aEventName=="create") return LV_EVENT_CREATE;
if (aEventName=="delete") return LV_EVENT_DELETE;
if (aEventName=="load") return LV_EVENT_SCREEN_LOADED;
if (aEventName=="unload") return LV_EVENT_SCREEN_UNLOADED;
if (aEventName=="event") return LV_EVENT_ALL; // all
return LV_EVENT_LAST; // invalid
}
static const char *eventName(lv_event_code_t aEvent)
{
const char *etxt = "";
switch(aEvent) {
case LV_EVENT_PRESSED: etxt = "pressed"; break; // The object has been pressed
case LV_EVENT_PRESSING: etxt = "pressing"; break; // The object is being pressed (sent continuously while pressing)
case LV_EVENT_PRESS_LOST: etxt = "lost"; break; // Still pressing but slid from the objects
case LV_EVENT_SHORT_CLICKED: etxt = "shortclick"; break; // Released before lLV_INDEV_LONG_PRESS_TIME. Not called if dragged.
case LV_EVENT_LONG_PRESSED: etxt = "longpress"; break; // Pressing for LV_INDEV_LONG_PRESS_TIME time. Not called if dragged.
case LV_EVENT_LONG_PRESSED_REPEAT: etxt = "longpress_repeat"; break; // Called after LV_INDEV_LONG_PRESS_TIME in every LV_INDEV_LONG_PRESS_REP_TIME ms. Not called if dragged.
case LV_EVENT_CLICKED: etxt = "click"; break; // Called on release if not dragged (regardless to long press)
case LV_EVENT_RELEASED: etxt = "released"; break; // Called in every case when the object has been released even if it was dragged
case LV_EVENT_SCROLL_BEGIN: etxt = "scroll_begin"; break;
case LV_EVENT_SCROLL_THROW_BEGIN: etxt = "scroll_throw"; break; // end of scroll with momentum
case LV_EVENT_SCROLL_END: etxt = "scroll_end"; break;
case LV_EVENT_SCROLL: etxt = "scroll"; break;
case LV_EVENT_GESTURE: etxt = "gesture"; break;
case LV_EVENT_KEY: etxt = "key"; break;
case LV_EVENT_FOCUSED: etxt = "focused"; break;
case LV_EVENT_DEFOCUSED: etxt = "defocused"; break;
case LV_EVENT_LEAVE: etxt = "leave"; break; // defocused but still selected
// LV_EVENT_HIT_TEST, /**< Perform advanced hit-testing*/
// LV_EVENT_INDEV_RESET, /**< Indev has been reset*/
case LV_EVENT_HOVER_OVER: etxt = "hover"; break; // Indev hover over object
case LV_EVENT_HOVER_LEAVE: etxt = "endhover"; break; // Indev hover leave object
// left out all drawing events
case LV_EVENT_VALUE_CHANGED: etxt = "changed"; break; // The object's value has changed (i.e. slider moved)
case LV_EVENT_INSERT: etxt = "insert"; break;
case LV_EVENT_REFRESH: etxt = "refresh"; break;
case LV_EVENT_READY: etxt = "ready"; break;
case LV_EVENT_CANCEL: etxt = "cancel"; break; // "Close", "Cancel" or similar specific button has clicked
// only some important ones for our context
case LV_EVENT_CREATE: etxt = "create"; break; // Object is being created
case LV_EVENT_DELETE: etxt = "delete"; break; // Object is being deleted
case LV_EVENT_SCREEN_LOADED: etxt = "loaded"; break; // A screen was loaded
case LV_EVENT_SCREEN_UNLOADED: etxt = "unloaded"; break; // A screen was unloaded
default: etxt = "OTHER"; break;
}
return etxt;
}
// MARK: - LvGLUIObject
ErrorPtr LvGLUIObject::configure(JsonObjectPtr aConfig)
{
ErrorPtr err;
// iterate
aConfig->resetKeyIteration();
string key;
JsonObjectPtr o;
while (aConfig->nextKeyValue(key, o)) {
if (key.substr(0,2)=="on") {
// event handler
lv_event_code_t eventcode = getEventCodeFromName(key.substr(2));
if (eventcode!=LV_EVENT_LAST) {
// valid on<event>
err = setEventHandler(eventcode, o);
if (Error::notOK(err)) break;
// successfully handled
continue;
}
}
// not an event handler
err = setProperty(key, o);
if (Error::notOK(err)) break;
}
return err;
}
ErrorPtr LvGLUIObject::setProperty(const string& aName, JsonObjectPtr aValue)
{
ErrorPtr err;
if (aName=="name") {
mName = aValue->stringValue();
}
else {
err = TextError::err("unknown property '%s'", aName.c_str());
}
return err;
}
ErrorPtr LvGLUIObject::setEventHandler(lv_event_code_t aEventCode, JsonObjectPtr aHandler)
{
ErrorPtr err;
#if ENABLE_LVGLUI_SCRIPT_FUNCS
err = TextError::err("object does not support handler for event '%s'", eventName(aEventCode));
#else
err = TextError::err("p44script events not supported");
#endif
return err;
}
// MARK: - LvGLUiTheme
ErrorPtr LvGLUiTheme::configure(JsonObjectPtr aConfig)
{
ErrorPtr err;
JsonObjectPtr o;
const lv_font_t* font = lv_font_default();
lv_color_t primary = lv_color_hex3(0x03A); // TODO: adjust for a nice default
lv_color_t secondary = lv_color_hex3(0x015);
bool isDark = false;
if (aConfig->get("name", o)) {
mName = o->stringValue();
lv_style_value_t val;
if (aConfig->get("primary", o)) {
err = colorPropValue(o, val, mLvglui);
if (Error::notOK(err)) return err;
primary = val.color;
}