-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
lvglui.cpp
1732 lines (1519 loc) · 51.3 KB
/
lvglui.cpp
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
//
// Copyright (c) 2019 plan44.ch / Lukas Zeller, Zurich, Switzerland
//
// Author: Lukas Zeller <[email protected]>
//
// 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 lv_font_t* getFontByName(const string aFontName)
{
if (aFontName=="roboto12")
return &lv_font_roboto_12;
else if (aFontName=="roboto16")
return &lv_font_roboto_16;
else if (aFontName=="roboto22")
return &lv_font_roboto_22;
else if (aFontName=="roboto28")
return &lv_font_roboto_28;
else
return NULL;
}
static lv_layout_t getLayoutByName(const string aLayoutName)
{
if (aLayoutName=="center") return LV_LAYOUT_CENTER;
else if (aLayoutName=="column_left") return LV_LAYOUT_COL_L;
else if (aLayoutName=="column_middle") return LV_LAYOUT_COL_M;
else if (aLayoutName=="column_right") return LV_LAYOUT_COL_R;
else if (aLayoutName=="row_top") return LV_LAYOUT_ROW_T;
else if (aLayoutName=="row_middle") return LV_LAYOUT_ROW_M;
else if (aLayoutName=="row_bottom") return LV_LAYOUT_ROW_B;
else if (aLayoutName=="pretty") return LV_LAYOUT_PRETTY;
else if (aLayoutName=="grid") return LV_LAYOUT_GRID;
else return LV_LAYOUT_OFF;
}
static lv_fit_t getAutoFitByName(const string aAutoFitName)
{
if (aAutoFitName=="tight") return LV_FIT_TIGHT;
else if (aAutoFitName=="flood") return LV_FIT_FLOOD;
else if (aAutoFitName=="fill") return LV_FIT_FILL;
else return LV_FIT_NONE;
}
static lv_style_t* getStyleByName(const string aStyleName)
{
if (aStyleName=="scr")
return &lv_style_scr;
else if (aStyleName=="transp")
return &lv_style_transp;
else if (aStyleName=="scr")
return &lv_style_scr;
else if (aStyleName=="transp")
return &lv_style_transp;
else if (aStyleName=="transp_fit")
return &lv_style_transp_fit;
else if (aStyleName=="transp_tight")
return &lv_style_transp_tight;
else if (aStyleName=="plain")
return &lv_style_plain;
else if (aStyleName=="plain_color")
return &lv_style_plain_color;
else if (aStyleName=="pretty")
return &lv_style_pretty;
else if (aStyleName=="pretty_color")
return &lv_style_pretty_color;
else if (aStyleName=="btn_rel")
return &lv_style_btn_rel;
else if (aStyleName=="btn_pr")
return &lv_style_btn_pr;
else if (aStyleName=="btn_tgl_rel")
return &lv_style_btn_tgl_rel;
else if (aStyleName=="btn_tgl_pr")
return &lv_style_btn_tgl_pr;
else if (aStyleName=="btn_ina")
return &lv_style_btn_ina;
else
return NULL;
}
static const char* getSymbolByName(const string aSymbolName)
{
if (aSymbolName=="audio") return LV_SYMBOL_AUDIO;
else if (aSymbolName=="video") return LV_SYMBOL_VIDEO;
else if (aSymbolName=="list") return LV_SYMBOL_LIST;
else if (aSymbolName=="ok") return LV_SYMBOL_OK;
else if (aSymbolName=="close") return LV_SYMBOL_CLOSE;
else if (aSymbolName=="power") return LV_SYMBOL_POWER;
else if (aSymbolName=="settings") return LV_SYMBOL_SETTINGS;
else if (aSymbolName=="trash") return LV_SYMBOL_TRASH;
else if (aSymbolName=="home") return LV_SYMBOL_HOME;
else if (aSymbolName=="download") return LV_SYMBOL_DOWNLOAD;
else if (aSymbolName=="drive") return LV_SYMBOL_DRIVE;
else if (aSymbolName=="refresh") return LV_SYMBOL_REFRESH;
else if (aSymbolName=="mute") return LV_SYMBOL_MUTE;
else if (aSymbolName=="volume_mid") return LV_SYMBOL_VOLUME_MID;
else if (aSymbolName=="volume_max") return LV_SYMBOL_VOLUME_MAX;
else if (aSymbolName=="image") return LV_SYMBOL_IMAGE;
else if (aSymbolName=="edit") return LV_SYMBOL_EDIT;
else if (aSymbolName=="prev") return LV_SYMBOL_PREV;
else if (aSymbolName=="play") return LV_SYMBOL_PLAY;
else if (aSymbolName=="pause") return LV_SYMBOL_PAUSE;
else if (aSymbolName=="stop") return LV_SYMBOL_STOP;
else if (aSymbolName=="next") return LV_SYMBOL_NEXT;
else if (aSymbolName=="eject") return LV_SYMBOL_EJECT;
else if (aSymbolName=="left") return LV_SYMBOL_LEFT;
else if (aSymbolName=="right") return LV_SYMBOL_RIGHT;
else if (aSymbolName=="plus") return LV_SYMBOL_PLUS;
else if (aSymbolName=="minus") return LV_SYMBOL_MINUS;
else if (aSymbolName=="warning") return LV_SYMBOL_WARNING;
else if (aSymbolName=="shuffle") return LV_SYMBOL_SHUFFLE;
else if (aSymbolName=="up") return LV_SYMBOL_UP;
else if (aSymbolName=="down") return LV_SYMBOL_DOWN;
else if (aSymbolName=="loop") return LV_SYMBOL_LOOP;
else if (aSymbolName=="directory") return LV_SYMBOL_DIRECTORY;
else if (aSymbolName=="upload") return LV_SYMBOL_UPLOAD;
else if (aSymbolName=="call") return LV_SYMBOL_CALL;
else if (aSymbolName=="cut") return LV_SYMBOL_CUT;
else if (aSymbolName=="copy") return LV_SYMBOL_COPY;
else if (aSymbolName=="save") return LV_SYMBOL_SAVE;
else if (aSymbolName=="charge") return LV_SYMBOL_CHARGE;
else if (aSymbolName=="bell") return LV_SYMBOL_BELL;
else if (aSymbolName=="keyboard") return LV_SYMBOL_KEYBOARD;
else if (aSymbolName=="gps") return LV_SYMBOL_GPS;
else if (aSymbolName=="file") return LV_SYMBOL_FILE;
else if (aSymbolName=="wifi") return LV_SYMBOL_WIFI;
else if (aSymbolName=="battery_full") return LV_SYMBOL_BATTERY_FULL;
else if (aSymbolName=="battery_3") return LV_SYMBOL_BATTERY_3;
else if (aSymbolName=="battery_2") return LV_SYMBOL_BATTERY_2;
else if (aSymbolName=="battery_1") return LV_SYMBOL_BATTERY_1;
else if (aSymbolName=="battery_empty") return LV_SYMBOL_BATTERY_EMPTY;
else if (aSymbolName=="bluetooth") return LV_SYMBOL_BLUETOOTH;
else return "";
}
static lv_color_t colorFromWebColor(const string aWebColor)
{
size_t i = 0;
size_t n = aWebColor.size();
if (n>0 && aWebColor[0]=='#') { i++; n--; } // skip optional #
uint32_t h;
int r=0, g=0, b=0;
if (sscanf(aWebColor.c_str()+i, "%x", &h)==1) {
if (n<=4) {
// short form RGB
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
r = (h>>16)&0xFF;
g = (h>>8)&0xFF;
b = (h>>0)&0xFF;
}
}
return lv_color_make(r,g,b);
}
static lv_border_part_t borderPartFromList(const string aBorderParts)
{
const char* p = aBorderParts.c_str();
string part;
lv_border_part_t parts = LV_BORDER_NONE;
while (nextPart(p, part, ',')) {
if (part=="bottom")
parts |= LV_BORDER_BOTTOM;
else if (part=="top")
parts |= LV_BORDER_TOP;
else if (part=="left")
parts |= LV_BORDER_LEFT;
else if (part=="right")
parts |= LV_BORDER_RIGHT;
else if (part=="full")
parts |= LV_BORDER_FULL;
else if (part=="internal")
parts |= LV_BORDER_INTERNAL;
}
return parts;
}
static lv_align_t alignModeByName(const string aAlignMode)
{
const char *p = aAlignMode.c_str();
bool in = true;
bool top = false;
bool mid = false;
bool bottom = false;
bool left = false;
bool right = false;
string tok;
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)
return LV_ALIGN_IN_TOP_LEFT;
if (in && top && mid)
return LV_ALIGN_IN_TOP_MID;
if (in && top && right)
return LV_ALIGN_IN_TOP_RIGHT;
if (in && bottom && left)
return LV_ALIGN_IN_BOTTOM_LEFT;
if (in && bottom && mid)
return LV_ALIGN_IN_BOTTOM_MID;
if (in && bottom && right)
return LV_ALIGN_IN_BOTTOM_RIGHT;
if (in && left && mid)
return LV_ALIGN_IN_LEFT_MID;
if (in && right && mid)
return LV_ALIGN_IN_RIGHT_MID;
if (!in && top && left)
return LV_ALIGN_OUT_TOP_LEFT;
if (!in && top && mid)
return LV_ALIGN_OUT_TOP_MID;
if (!in && top && right)
return LV_ALIGN_OUT_TOP_RIGHT;
if (!in && bottom && left)
return LV_ALIGN_OUT_BOTTOM_LEFT;
if (!in && bottom && mid)
return LV_ALIGN_OUT_BOTTOM_MID;
if (!in && bottom && right)
return LV_ALIGN_OUT_BOTTOM_RIGHT;
if (!in && left && top)
return LV_ALIGN_OUT_LEFT_TOP;
if (!in && left && mid)
return LV_ALIGN_OUT_LEFT_MID;
if (!in && left && bottom)
return LV_ALIGN_OUT_LEFT_BOTTOM;
if (!in && right && top)
return LV_ALIGN_OUT_RIGHT_TOP;
if (!in && right && mid)
return LV_ALIGN_OUT_RIGHT_MID;
if (!in && right && bottom)
return LV_ALIGN_OUT_RIGHT_BOTTOM;
else
return LV_ALIGN_CENTER;
}
static const char *eventName(lv_event_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_DRAG_BEGIN: etxt = "drag_begin"; break;
case LV_EVENT_DRAG_END: etxt = "drag_end"; break;
case LV_EVENT_DRAG_THROW_BEGIN: etxt = "drag_throw"; break; // end of drag with momentum
case LV_EVENT_KEY: etxt = "key"; break;
case LV_EVENT_FOCUSED: etxt = "focused"; break;
case LV_EVENT_DEFOCUSED: etxt = "defocused"; break;
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_APPLY: etxt = "apply"; break; // "Ok", "Apply" or similar specific button has clicked
case LV_EVENT_CANCEL: etxt = "cancel"; break; // "Close", "Cancel" or similar specific button has clicked
case LV_EVENT_DELETE: etxt = "delete"; break; // Object is being deleted
}
return etxt;
}
// MARK: - LvGLUIObject
ErrorPtr LvGLUIObject::configure(JsonObjectPtr aConfig)
{
JsonObjectPtr o;
if (aConfig->get("name", o)) {
name = o->stringValue();
}
return ErrorPtr();
}
// MARK: - LvGLUiTheme
ErrorPtr LvGLUiTheme::configure(JsonObjectPtr aConfig)
{
JsonObjectPtr o;
uint16_t hue = 0;
lv_font_t* font = NULL;
string themeName;
if (aConfig->get("hue", o)) {
hue = o->int32Value();
}
if (aConfig->get("font", o)) {
font = getFontByName(o->stringValue());
}
if (aConfig->get("theme", o)) {
themeName = o->stringValue();
}
// (re-)init theme
if (themeName=="material") {
theme = lv_theme_material_init(hue, font);
}
else if (themeName=="alien") {
theme = lv_theme_alien_init(hue, font);
}
else if (themeName=="mono") {
theme = lv_theme_mono_init(hue, font);
}
else if (themeName=="nemo") {
theme = lv_theme_nemo_init(hue, font);
}
else if (themeName=="night") {
theme = lv_theme_night_init(hue, font);
}
else if (themeName=="zen") {
theme = lv_theme_zen_init(hue, font);
}
else {
theme = lv_theme_default_init(hue, font);
}
return inherited::configure(aConfig);
}
// MARK: - LvGLUiStyle
LvGLUiStyle::LvGLUiStyle(LvGLUi& aLvGLUI) : inherited(aLvGLUI)
{
lv_style_copy(&style, &lv_style_plain); // base on plain by default
}
ErrorPtr LvGLUiStyle::configure(JsonObjectPtr aConfig)
{
JsonObjectPtr o;
if (aConfig->get("template", o)) {
lv_style_t* s = lvglui.namedStyle(o->stringValue());
if (!s) return TextError::err("unknown style '%s' as template", o->stringValue().c_str());
lv_style_copy(&style, s);
}
// set style properties
if (aConfig->get("glass", o)) {
style.glass = o->boolValue();
}
// - body
if (aConfig->get("color", o)) {
style.body.main_color = colorFromWebColor(o->stringValue());
// also set gradient color. Use "main_color" to set main color alone
style.body.grad_color = style.body.main_color;
}
if (aConfig->get("main_color", o)) {
style.body.main_color = colorFromWebColor(o->stringValue());
}
if (aConfig->get("gradient_color", o)) {
style.body.grad_color = colorFromWebColor(o->stringValue());
}
if (aConfig->get("radius", o)) {
if (o->stringValue()=="circle")
style.body.radius = LV_RADIUS_CIRCLE;
else
style.body.radius = (lv_coord_t)o->int32Value();
}
if (aConfig->get("alpha", o)) {
style.body.opa = (lv_opa_t)o->int32Value();
}
// - border
if (aConfig->get("border_color", o)) {
style.body.border.color = colorFromWebColor(o->stringValue());
}
if (aConfig->get("border_width", o)) {
style.body.border.width = (lv_coord_t)o->int32Value();
}
if (aConfig->get("border_alpha", o)) {
style.body.border.opa = (lv_opa_t)o->int32Value();
}
if (aConfig->get("border_parts", o)) {
style.body.border.part = borderPartFromList(o->stringValue());
}
// - shadow
if (aConfig->get("shadow_color", o)) {
style.body.shadow.color = colorFromWebColor(o->stringValue());
}
if (aConfig->get("shadow_width", o)) {
style.body.shadow.width = (lv_coord_t)o->int32Value();
}
if (aConfig->get("shadow_full", o)) {
style.body.shadow.type = o->boolValue() ? LV_SHADOW_FULL : LV_SHADOW_BOTTOM;
}
// - paddings
if (aConfig->get("padding_top", o)) {
style.body.padding.top = (lv_coord_t)o->int32Value();
}
if (aConfig->get("padding_bottom", o)) {
style.body.padding.bottom = (lv_coord_t)o->int32Value();
}
if (aConfig->get("padding_left", o)) {
style.body.padding.left = (lv_coord_t)o->int32Value();
}
if (aConfig->get("padding_right", o)) {
style.body.padding.right = (lv_coord_t)o->int32Value();
}
if (aConfig->get("padding_inner", o)) {
style.body.padding.inner = (lv_coord_t)o->int32Value();
}
// - text
if (aConfig->get("text_color", o)) {
style.text.color = colorFromWebColor(o->stringValue());
}
if (aConfig->get("text_selection_color", o)) {
style.text.sel_color = colorFromWebColor(o->stringValue());
}
if (aConfig->get("font", o)) {
style.text.font = getFontByName(o->stringValue());
}
if (aConfig->get("text_letter_space", o)) {
style.text.letter_space = (lv_coord_t)o->int32Value();
}
if (aConfig->get("text_line_space", o)) {
style.text.line_space = (lv_coord_t)o->int32Value();
}
if (aConfig->get("text_alpha", o)) {
style.text.opa = (lv_opa_t)o->int32Value();
}
// - image
if (aConfig->get("image_color", o)) {
style.image.color = colorFromWebColor(o->stringValue());
}
if (aConfig->get("image_recoloring", o)) {
style.image.intense = (lv_opa_t)o->int32Value();
}
if (aConfig->get("image_alpha", o)) {
style.image.opa = (lv_opa_t)o->int32Value();
}
// - line
if (aConfig->get("line_color", o)) {
style.line.color = colorFromWebColor(o->stringValue());
}
if (aConfig->get("line_width", o)) {
style.line.width = (lv_coord_t)o->int32Value();
}
if (aConfig->get("line_alpha", o)) {
style.line.opa = (lv_opa_t)o->int32Value();
}
if (aConfig->get("line_rounded", o)) {
style.line.rounded = (lv_opa_t)o->boolValue();
}
return inherited::configure(aConfig);
}
// MARK - Element Factory
static LVGLUiElementPtr createElement(LvGLUi& aLvGLUI, JsonObjectPtr aConfig, LvGLUiContainer* aParentP, bool aContainerByDefault)
{
LVGLUiElementPtr elem;
JsonObjectPtr o;
lv_obj_t* tmpl = NULL;
string tn;
if (aConfig->get("type", o)) {
tn = o->stringValue();
}
if (aConfig->get("template", o)) {
// reference an existing named element to copy from (sibling)
LVGLUiElementPtr templateElem = aLvGLUI.namedElement(o->stringValue(), aParentP);
if (templateElem) tmpl = templateElem->element;
}
// now create according to type
if (tn=="panel") {
elem = LVGLUiElementPtr(new LvGLUiPanel(aLvGLUI, aParentP, tmpl));
}
else if (tn=="image") {
elem = LVGLUiElementPtr(new LvGLUiImage(aLvGLUI, aParentP, tmpl));
}
else if (tn=="label") {
elem = LVGLUiElementPtr(new LvGLUiLabel(aLvGLUI, aParentP, tmpl));
}
else if (tn=="button") {
if (aConfig->get("image"))
elem = LVGLUiElementPtr(new LvGLUiImgButton(aLvGLUI, aParentP, tmpl));
else
elem = LVGLUiElementPtr(new LvGLUiButton(aLvGLUI, aParentP, tmpl));
}
else if (tn=="image_button") {
elem = LVGLUiElementPtr(new LvGLUiImgButton(aLvGLUI, aParentP, tmpl));
}
else if (tn=="slider") {
elem = LVGLUiElementPtr(new LvGLUiSlider(aLvGLUI, aParentP, tmpl));
}
else {
if (aContainerByDefault)
elem = LVGLUiElementPtr(new LvGLUiPanel(aLvGLUI, aParentP, tmpl));
else
elem = LVGLUiElementPtr(new LvGLUiPlain(aLvGLUI, aParentP, tmpl));
}
return elem;
}
// MARK: - LVGLUiElement
const void* LVGLUiElement::imgSrc(const string& aSource)
{
if (aSource.empty()) return NULL;
return aSource.c_str();
}
LVGLUiElement::LVGLUiElement(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate) :
inherited(aLvGLUI),
parentP(aParentP),
element(NULL),
handlesEvents(false)
#if ENABLE_LVGLUI_SCRIPT_FUNCS
,onEventScript(scriptbody+regular, "onEvent")
,onRefreshScript(scriptbody+regular, "onRefresh")
#endif
{
}
lv_obj_t* LVGLUiElement::lvParent()
{
return parentP ? parentP->element : NULL;
}
LVGLUiElement::~LVGLUiElement()
{
clear();
}
void LVGLUiElement::clear()
{
if (element) {
lv_obj_del(element); // delete element and all of its children on the lvgl level
element = NULL;
}
}
ErrorPtr LVGLUiElement::configure(JsonObjectPtr aConfig)
{
if (!element) return TextError::err("trying to configure non-existing lv_obj");
JsonObjectPtr o;
LVGLUiElementPtr alignRef;
lv_coord_t align_dx = 0;
lv_coord_t align_dy = 0;
bool align_middle = false;
// common properties
if (aConfig->get("x", o)) {
lv_obj_set_x(element, o->int32Value());
}
if (aConfig->get("y", o)) {
lv_obj_set_y(element, o->int32Value());
}
if (aConfig->get("dx", o)) {
lv_obj_set_width(element, o->int32Value());
}
if (aConfig->get("dy", o)) {
lv_obj_set_height(element, o->int32Value());
}
if (aConfig->get("alignto", o)) {
alignRef = lvglui.namedElement(o->stringValue(), parentP); // sibling
}
if (aConfig->get("align_dx", o)) {
align_dx = o->int32Value();
}
if (aConfig->get("align_dy", o)) {
align_dy = o->int32Value();
}
if (aConfig->get("align_middle", o)) {
align_middle = o->boolValue();
}
if (aConfig->get("align", o)) {
if (align_middle)
lv_obj_align_origo(element, alignRef->element, alignModeByName(o->stringValue()), align_dx, align_dy);
else
lv_obj_align(element, alignRef->element, alignModeByName(o->stringValue()), align_dx, align_dy);
}
if (aConfig->get("style", o)) {
lv_style_t* style = lvglui.namedOrAdHocStyle(o, true);
if (style) lv_obj_set_style(element, style);
}
if (aConfig->get("hidden", o)) {
lv_obj_set_hidden(element, o->boolValue());
}
if (aConfig->get("click", o)) {
lv_obj_set_click(element, o->boolValue());
}
if (aConfig->get("extended_click", o)) {
int ext = o->int32Value();
lv_obj_set_ext_click_area(element, ext, ext, ext, ext);
}
// generic content change
if (aConfig->get("value", o)) {
setValue(o->int32Value(), 0); // w/o animation. Use script function setValue() for animated changes
}
if (aConfig->get("text", o)) {
setText(o->stringValue());
}
// events
#if ENABLE_LVGLUI_SCRIPT_FUNCS
if (aConfig->get("onevent", o)) {
onEventScript.setSource(o->stringValue());
installEventHandler();
}
if (aConfig->get("onrefresh", o)) {
onRefreshScript.setSource(o->stringValue());
installEventHandler();
}
#endif
return inherited::configure(aConfig);
}
static void elementEventCallback(lv_obj_t * obj, lv_event_t event)
{
LVGLUiElement *eventSource = static_cast<LVGLUiElement*>(lv_obj_get_user_data(obj));
if (eventSource) {
eventSource->handleEvent(event);
}
}
void LVGLUiElement::installEventHandler()
{
if (!handlesEvents) {
handlesEvents = true;
// set user data
lv_obj_set_user_data(element, (void *)this);
// set callback
lv_obj_set_event_cb(element, &elementEventCallback);
}
}
void LVGLUiElement::handleEvent(lv_event_t aEvent)
{
#if ENABLE_LVGLUI_SCRIPT_FUNCS
if (aEvent==LV_EVENT_REFRESH && !onRefreshScript.empty()) {
runEventScript(aEvent, onRefreshScript);
}
else if (!onEventScript.empty()) {
runEventScript(aEvent, onEventScript);
}
#endif
}
void LVGLUiElement::setText(const string &aNewText)
{
string out;
size_t pos = 0, sym;
// replace &sym; pseudo-HTML-entities with symbols.
while (true) {
sym = aNewText.find('&',pos);
if (sym==string::npos)
break;
out.append(aNewText.substr(pos, sym-pos));
pos = sym+1;
sym = aNewText.find(';',pos);
if (sym==string::npos) {
out += '&';
continue;
}
string sy = getSymbolByName(aNewText.substr(pos, sym-pos));
if (sy.empty()) {
out += aNewText.substr(pos-1, sym-pos+2);
}
else {
out += sy;
}
pos = sym+1;
}
// append rest
out.append(aNewText.substr(pos));
setTextRaw(out);
}
// MARK: - LvGLUiLayoutContainer
ErrorPtr LvGLUiLayoutContainer::configure(JsonObjectPtr aConfig)
{
JsonObjectPtr o;
// common properties
if (aConfig->get("layout", o)) {
lv_cont_set_layout(element, getLayoutByName(o->stringValue()));
}
if (aConfig->get("fit", o)) {
lv_cont_set_fit(element, getAutoFitByName(o->stringValue()));
}
if (aConfig->get("fit_horizontal", o)) {
lv_cont_set_fit4(element, o->int32Value(), o->int32Value(), lv_cont_get_fit_top(element), lv_cont_get_fit_bottom(element));
}
if (aConfig->get("fit_vertical", o)) {
lv_cont_set_fit4(element, lv_cont_get_fit_left(element), lv_cont_get_fit_right(element), o->int32Value(), o->int32Value());
}
return inherited::configure(aConfig);
}
// MARK: - LVGLUiContainer
void LvGLUiContainer::clear()
{
namedElements.clear();
anonymousElements.clear();
inherited::clear();
}
ErrorPtr LvGLUiContainer::addElements(JsonObjectPtr aElementConfigArray, LvGLUiContainer* aParent, bool aContainerByDefault)
{
ErrorPtr err;
for (int i = 0; i<aElementConfigArray->arrayLength(); ++i) {
JsonObjectPtr elementConfig = aElementConfigArray->arrayGet(i);
LVGLUiElementPtr uielement = createElement(lvglui, elementConfig, aParent, aContainerByDefault);
if (!uielement || !uielement->element) {
err = TextError::err("unknown/invalid element type: %s", elementConfig->c_strValue());
break;
}
err = uielement->configure(elementConfig);
if (Error::notOK(err)) break;
FOCUSLOG("Created Element '%s' from: %s", uielement->getName().c_str(), elementConfig->c_strValue());
// add to named elements if it has a name
if (!uielement->getName().empty()) {
namedElements[uielement->getName()] = uielement;
}
else if (!aParent || uielement->wrapperNeeded()) {
anonymousElements.push_back(uielement);
}
else {
// this element does not need a wrapper, and has a parent which will release this child's memory
// so we just need to make sure disposing of the wrapper will not delete the lv_obj
uielement->element = NULL; // cut lv_obj from the wrapper
}
}
return err;
}
ErrorPtr LvGLUiContainer::configure(JsonObjectPtr aConfig)
{
// configure basics
ErrorPtr err = inherited::configure(aConfig);
if (Error::isOK(err)) {
// check for elements
JsonObjectPtr o;
if (aConfig->get("elements", o)) {
addElements(o, this, false); // normal elements have a parent, and default elements are plain elements
}
}
return err;
}
// MARK: - LvGLUiPlain - simple object with no child layout
LvGLUiPlain::LvGLUiPlain(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate) :
inherited(aLvGLUI, aParentP, aTemplate)
{
element = lv_obj_create(lvParent(), aTemplate);
}
// MARK: - LvGLUiPanel - object with layout features for contained children
LvGLUiPanel::LvGLUiPanel(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate) :
inherited(aLvGLUI, aParentP, aTemplate)
{
element = lv_cont_create(lvParent(), aTemplate);
}
// MARK: - LvGLUiImage
LvGLUiImage::LvGLUiImage(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate) :
inherited(aLvGLUI, aParentP, aTemplate)
{
element = lv_img_create(lvParent(), aTemplate);
}
ErrorPtr LvGLUiImage::configure(JsonObjectPtr aConfig)
{
// configure params
JsonObjectPtr o;
if (aConfig->get("autosize", o)) {
lv_img_set_auto_size(element, o->boolValue());
}
if (aConfig->get("src", o)) {
if (setProp(imgSrc, lvglui.namedImageSource(o->stringValue())))
lv_img_set_src(element, imgSrc.c_str());
}
if (aConfig->get("symbol", o)) {
lv_img_set_src(element, getSymbolByName(o->stringValue()));
}
if (aConfig->get("offset_x", o)) {
lv_img_set_offset_x(element, o->int32Value());
}
if (aConfig->get("offset_y", o)) {
lv_img_set_offset_y(element, o->int32Value());
}
return inherited::configure(aConfig);
}
void LvGLUiImage::setTextRaw(const string &aNewText)
{
string imgTxt = LV_SYMBOL_DUMMY;
imgTxt.append(aNewText);
lv_img_set_src(element, imgTxt.c_str());
}
// MARK: - LvGLUiLabel
LvGLUiLabel::LvGLUiLabel(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate) :
inherited(aLvGLUI, aParentP, aTemplate)
{
element = lv_label_create(lvParent(), aTemplate);
}
ErrorPtr LvGLUiLabel::configure(JsonObjectPtr aConfig)
{
// configure params
JsonObjectPtr o;
if (aConfig->get("longmode", o)) {
string lm = o->stringValue();
if (lm=="expand") lv_label_set_long_mode(element, LV_LABEL_LONG_EXPAND);
else if (lm=="break") lv_label_set_long_mode(element, LV_LABEL_LONG_BREAK);
else if (lm=="dot") lv_label_set_long_mode(element, LV_LABEL_LONG_DOT);
else if (lm=="scroll") lv_label_set_long_mode(element, LV_LABEL_LONG_SROLL);
else if (lm=="circularscroll") lv_label_set_long_mode(element, LV_LABEL_LONG_SROLL_CIRC);
else if (lm=="crop") lv_label_set_long_mode(element, LV_LABEL_LONG_CROP);
}
if (aConfig->get("text_align", o)) {
string ta = o->stringValue();
if (ta=="left") lv_label_set_align(element, LV_LABEL_ALIGN_LEFT);
else if (ta=="center") lv_label_set_align(element, LV_LABEL_ALIGN_CENTER);
else if (ta=="right") lv_label_set_align(element, LV_LABEL_ALIGN_RIGHT);
}
if (aConfig->get("background", o)) {
lv_label_set_body_draw(element, o->boolValue());
}
if (aConfig->get("inline_colors", o)) {
lv_label_set_recolor(element, o->boolValue());
}
return inherited::configure(aConfig);
}
void LvGLUiLabel::setTextRaw(const string &aNewText)
{
lv_label_set_text(element, aNewText.c_str());
}
// MARK: - LvGLUiButton
LvGLUiButton::LvGLUiButton(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate) :
inherited(aLvGLUI, aParentP, aTemplate),
label(NULL)
#if ENABLE_LVGLUI_SCRIPT_FUNCS
,onPressScript(scriptbody+regular, "onPress")
,onReleaseScript(scriptbody+regular, "onRelease")
#endif
{
element = lv_btn_create(lvParent(), aTemplate);
}
static void configureButtonCommon(LvGLUi &aUi, lv_obj_t *btn, JsonObjectPtr aConfig)
{
JsonObjectPtr o;
if (aConfig->get("released_style", o)) {
lv_style_t *style = aUi.namedOrAdHocStyle(o, true);
if (style) lv_btn_set_style(btn, LV_BTN_STYLE_REL, style);
}
if (aConfig->get("pressed_style", o)) {
lv_style_t *style = aUi.namedOrAdHocStyle(o, true);
if (style) lv_btn_set_style(btn, LV_BTN_STYLE_PR, style);
}
if (aConfig->get("on_style", o)) {
lv_style_t *style = aUi.namedOrAdHocStyle(o, true);
if (style) lv_btn_set_style(btn, LV_BTN_STYLE_TGL_PR, style);
}
if (aConfig->get("off_style", o)) {
lv_style_t *style = aUi.namedOrAdHocStyle(o, true);
if (style) lv_btn_set_style(btn, LV_BTN_STYLE_TGL_REL, style);
}
if (aConfig->get("disabled_style", o)) {
lv_style_t *style = aUi.namedOrAdHocStyle(o, true);
if (style) lv_btn_set_style(btn, LV_BTN_STYLE_INA, style);
}
if (aConfig->get("state", o)) {
string st = o->stringValue();
lv_btn_state_t sta = LV_BTN_STATE_REL; // default to released
if (st=="pressed") sta = LV_BTN_STATE_PR;
else if (st=="on") sta = LV_BTN_STATE_TGL_PR;
else if (st=="off") sta = LV_BTN_STATE_TGL_REL;
else if (st=="inactive") sta = LV_BTN_STATE_INA;
lv_btn_set_state(btn, sta);
}
}
ErrorPtr LvGLUiButton::configure(JsonObjectPtr aConfig)
{
// configure params
JsonObjectPtr o;
if (aConfig->get("toggle", o)) {
lv_btn_set_toggle(element, o->boolValue());
}
if (aConfig->get("ink_in", o)) {
lv_btn_set_ink_in_time(element, o->int32Value());
}
if (aConfig->get("ink_wait", o)) {
lv_btn_set_ink_wait_time(element, o->int32Value());
}
if (aConfig->get("ink_out", o)) {
lv_btn_set_ink_out_time(element, o->int32Value());
}
if (aConfig->get("label", o)) {
// convenience for text-labelled buttons
label = lv_label_create(element, NULL);
setText(o->stringValue());
}
// common button+imgBtn properties
configureButtonCommon(lvglui, element, aConfig);
// event handling
#if ENABLE_LVGLUI_SCRIPT_FUNCS
if (aConfig->get("onpress", o)) {
onPressScript.setSource(o->stringValue());
installEventHandler();
}
if (aConfig->get("onrelease", o)) {
onReleaseScript.setSource(o->stringValue());
installEventHandler();
}
#endif
return inherited::configure(aConfig);
}