-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
lvglui.hpp
514 lines (380 loc) · 17.1 KB
/
lvglui.hpp
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
//
// 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/>.
//
#ifndef __p44utils__lvglui__
#define __p44utils__lvglui__
#include "lvgl.hpp"
#if ENABLE_LVGL
#include "jsonobject.hpp"
#if ENABLE_P44SCRIPT && !defined(ENABLE_LVGLUI_SCRIPT_FUNCS)
#define ENABLE_LVGLUI_SCRIPT_FUNCS 1
#endif
#if ENABLE_LVGLUI_SCRIPT_FUNCS && !ENABLE_P44SCRIPT
#error "ENABLE_P44SCRIPT required when ENABLE_LVGLUI_SCRIPT_FUNCS is set"
#endif
#ifndef LVGLUI_LEGCACY_FUNCTIONS
#define LVGLUI_LEGCACY_FUNCTIONS 0
#endif
#if ENABLE_LVGLUI_SCRIPT_FUNCS
#include "p44script.hpp"
#endif
using namespace std;
namespace p44 {
class LvGLUi;
/// base class for any configurable object
class LvGLUIObject : public P44Obj
{
protected:
LvGLUi& lvglui;
string name;
public:
LvGLUIObject(LvGLUi& aLvGLUI) : lvglui(aLvGLUI) {};
const string& getName() { return name; };
LvGLUi& getLvGLUi() { return lvglui; };
/// configure this object from json
/// @param aConfig JSON object containing configuration propertyname/values
virtual ErrorPtr configure(JsonObjectPtr aConfig);
};
/// a initialized theme (base theme + hue + font)
class LvGLUiTheme : public LvGLUIObject
{
typedef LvGLUIObject inherited;
public:
lv_theme_t* theme;
LvGLUiTheme(LvGLUi& aLvGLUI) : inherited(aLvGLUI), theme(NULL) {};
/// configure this object from json
/// @param aConfig JSON object containing configuration propertyname/values
virtual ErrorPtr configure(JsonObjectPtr aConfig) P44_OVERRIDE;
};
typedef boost::intrusive_ptr<LvGLUiTheme> LvGLUiThemePtr;
typedef std::map<string, LvGLUiThemePtr> ThemeMap;
/// customized style
class LvGLUiStyle : public LvGLUIObject
{
typedef LvGLUIObject inherited;
public:
lv_style_t style; ///< the LGVL style
LvGLUiStyle(LvGLUi& aLvGLUI);
/// configure this object from json
/// @param aConfig JSON object containing configuration propertyname/values
virtual ErrorPtr configure(JsonObjectPtr aConfig) P44_OVERRIDE;
};
typedef boost::intrusive_ptr<LvGLUiStyle> LvGLUiStylePtr;
typedef std::map<string, LvGLUiStylePtr> StyleMap;
typedef std::list<LvGLUiStylePtr> StyleList;
// MARK: - element and container base classes
class LVGLUiElement;
class LvGLUiContainer;
typedef boost::intrusive_ptr<LVGLUiElement> LVGLUiElementPtr;
typedef boost::intrusive_ptr<LvGLUiContainer> LVGLUiContainerPtr;
/// abstract base class for visible UI elements, wrapping a lv_obj
class LVGLUiElement : public LvGLUIObject
{
friend class LvGLUi;
typedef LvGLUIObject inherited;
#if ENABLE_LVGLUI_SCRIPT_FUNCS
P44Script::ScriptSource onEventScript; ///< script executed to process otherwise unhandled lvgl events on this element
P44Script::ScriptSource onRefreshScript; ///< script executed to specifically process "refresh" event
#endif
bool handlesEvents;
public:
lv_obj_t* element;
LvGLUiContainer* parentP;
LVGLUiElement(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate);
virtual ~LVGLUiElement();
lv_obj_t* lvParent();
/// set a variable representing a property and return true if value has changed
/// @param aTargetValue the variable to update
/// @param aNewValue the new value
/// @note returns true if variable is actually changed
template<typename T> bool setProp(T &aTargetValue, T aNewValue)
{
if (aTargetValue!=aNewValue) {
aTargetValue = aNewValue;
return true; // changed value
}
return false; // not changed value
};
/// configure this object from json
/// @param aConfig JSON object containing configuration propertyname/values
/// @param aParent parent object, or NULL if none
virtual ErrorPtr configure(JsonObjectPtr aConfig) P44_OVERRIDE;
/// let object handle an event
virtual void handleEvent(lv_event_t aEvent);
/// clear this element (and all of its named and unnamed children)
virtual void clear();
/// @return true if the wrapper object must be kept around (e.g. because it needs to handle events)
virtual bool wrapperNeeded() { return handlesEvents || !getName().empty(); }; // simple objects need the wrapper only if they handle events or can be referenced by name
/// @param aValue the value to set to the element (depends on element type)
/// @param aAnimationTime if set>0, the value change will be animated
virtual void setValue(int16_t aValue, uint16_t aAnimationTimeMs = 0) { /* NOP in base class */ }
/// set a new text for an element
void setText(const string &aNewText);
/// get value
/// @return current value of the control
virtual int16_t getValue() { return 0; /* no value in base class */ }
/// run event script
#if ENABLE_LVGLUI_SCRIPT_FUNCS
void runEventScript(lv_event_t aEvent, P44Script::ScriptSource& aScriptCode);
void scriptDone();
#endif
protected:
void installEventHandler();
virtual void setTextRaw(const string &aNewText) { /* NOP in base class */ }
static const void* imgSrc(const string& aSource);
};
typedef std::map<string, LVGLUiElementPtr> ElementMap;
typedef std::list<LVGLUiElementPtr> ElementList;
/// abstract class for lv_cont and similar objects with layout features
class LvGLUiLayoutContainer : public LVGLUiElement
{
typedef LVGLUiElement inherited;
public:
LvGLUiLayoutContainer(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate) : inherited(aLvGLUI, aParentP, aTemplate) {};
virtual ErrorPtr configure(JsonObjectPtr aConfig) P44_OVERRIDE;
};
/// abstract for a UI element that can create contained child objects from config
class LvGLUiContainer : public LvGLUiLayoutContainer
{
typedef LvGLUiLayoutContainer inherited;
friend class LvGLUi;
ElementMap namedElements; ///< the contained elements that have a name because the need to be referencable
ElementList anonymousElements; ///< the contained elements that need to be around after configuration because they are actionable
public:
LvGLUiContainer(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate) : inherited(aLvGLUI, aParentP, aTemplate) {};
/// configure this object from json
/// @param aConfig JSON object containing configuration propertyname/values
/// @param aParent parent object, or NULL if none
virtual ErrorPtr configure(JsonObjectPtr aConfig) P44_OVERRIDE;
/// clear this element (and all of its named and unnamed children)
virtual void clear() P44_OVERRIDE;
/// @return true if the wrapper object must be kept around (e.g. because it needs to handle events)
virtual bool wrapperNeeded() P44_OVERRIDE { return true; }; // containers always needs wrapper
protected:
ErrorPtr addElements(JsonObjectPtr aElementConfigArray, LvGLUiContainer* aParent, bool aContainerByDefault);
};
// MARK: - specific UI elements
class LvGLUiPlain : public LVGLUiElement
{
typedef LVGLUiElement inherited;
public:
LvGLUiPlain(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate);
};
class LvGLUiPanel : public LvGLUiContainer
{
typedef LvGLUiContainer inherited;
public:
LvGLUiPanel(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate);
};
class LvGLUiImage : public LVGLUiElement
{
typedef LVGLUiElement inherited;
string imgSrc;
public:
LvGLUiImage(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate);
virtual ErrorPtr configure(JsonObjectPtr aConfig) P44_OVERRIDE;
virtual void setTextRaw(const string &aNewText) P44_OVERRIDE;
virtual bool wrapperNeeded() P44_OVERRIDE { return true; }; // wrapper stores the image source, must be kept
};
class LvGLUiLabel : public LVGLUiElement
{
typedef LVGLUiElement inherited;
public:
LvGLUiLabel(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate);
virtual ErrorPtr configure(JsonObjectPtr aConfig) P44_OVERRIDE;
virtual void setTextRaw(const string &aNewText) P44_OVERRIDE;
};
class LvGLUiButton : public LvGLUiLayoutContainer
{
typedef LvGLUiLayoutContainer inherited;
#if ENABLE_LVGLUI_SCRIPT_FUNCS
P44Script::ScriptSource onPressScript;
P44Script::ScriptSource onReleaseScript;
#endif
lv_obj_t *label;
public:
LvGLUiButton(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate);
virtual ErrorPtr configure(JsonObjectPtr aConfig) P44_OVERRIDE;
protected:
virtual void handleEvent(lv_event_t aEvent) P44_OVERRIDE;
virtual void setTextRaw(const string &aNewText) P44_OVERRIDE;
};
class LvGLUiImgButton : public LVGLUiElement
{
typedef LVGLUiElement inherited;
#if ENABLE_LVGLUI_SCRIPT_FUNCS
P44Script::ScriptSource onPressScript;
P44Script::ScriptSource onReleaseScript;
#endif
string relImgSrc;
string prImgSrc;
string tglPrImgSrc;
string tglRelImgSrc;
string inaImgSrc;
bool imgsAssigned;
public:
LvGLUiImgButton(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate);
virtual ErrorPtr configure(JsonObjectPtr aConfig) P44_OVERRIDE;
virtual bool wrapperNeeded() P44_OVERRIDE { return true; }; // wrapper stores the image sources, must be kept
protected:
virtual void handleEvent(lv_event_t aEvent) P44_OVERRIDE;
static const void *imgBtnSrc(const string& aSource);
};
class LvGLUiBarBase : public LVGLUiElement
{
typedef LVGLUiElement inherited;
public:
LvGLUiBarBase(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate) : inherited(aLvGLUI, aParentP, aTemplate) {};
virtual ErrorPtr configure(JsonObjectPtr aConfig) P44_OVERRIDE;
protected:
virtual void setValue(int16_t aValue, uint16_t aAnimationTimeMs = 0) P44_OVERRIDE;
};
class LvGLUiBar : public LvGLUiBarBase
{
typedef LvGLUiBarBase inherited;
public:
LvGLUiBar(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate);
virtual int16_t getValue() P44_OVERRIDE { return lv_bar_get_value(element); }
};
class LvGLUiSlider : public LvGLUiBarBase
{
typedef LvGLUiBarBase inherited;
#if ENABLE_LVGLUI_SCRIPT_FUNCS
P44Script::ScriptSource onChangeScript;
P44Script::ScriptSource onReleaseScript;
#endif
public:
LvGLUiSlider(LvGLUi& aLvGLUI, LvGLUiContainer* aParentP, lv_obj_t *aTemplate);
virtual ErrorPtr configure(JsonObjectPtr aConfig) P44_OVERRIDE;
protected:
virtual void handleEvent(lv_event_t aEvent) P44_OVERRIDE;
virtual int16_t getValue() P44_OVERRIDE { return lv_slider_get_value(element); }
};
// MARK: - LvGLUi
class LvGLUi : public LvGLUiContainer
{
typedef LvGLUiContainer inherited;
lv_disp_t *display; ///< the display this gui appears on
StyleMap styles; ///< custom styles
StyleList adhocStyles; ///< keeps ad hoc styles
ThemeMap themes; ///< initialized themes (basic theme + hue + font)
bool mDataPathResources; ///< look for resources also in data path
string mResourcePrefix; ///< prefix for resource loading
#if ENABLE_LVGLUI_SCRIPT_FUNCS
P44Script::ScriptMainContextPtr mScriptMainContext;
P44Script::ScriptObjPtr mRepresentingObj;
P44Script::ScriptSource activityTimeoutScript;
P44Script::ScriptSource activationScript;
#endif
protected:
virtual void clear() P44_OVERRIDE;
public:
LvGLUi();
#if ENABLE_LVGLUI_SCRIPT_FUNCS
/// set main context for all lvgl object level script executions in
/// @param aScriptMainContext main context to execute lvgl object level scripts (sequentially among each other)
void setScriptMainContext(P44Script::ScriptMainContextPtr aScriptMainContext);
/// get the main script context (or create it on demand if not set by setScriptMainContext()
/// @return the context that runs all lvgl ui event handlers (queued, one by one)
P44Script::ScriptMainContextPtr getScriptMainContext();
/// @return a singleton script object, representing this lvgl ui instance
P44Script::ScriptObjPtr representingScriptObj();
/// report activation / timeout of UI
/// @note actual mechanism to detect UI usage or inactivity must be implemented on app level
/// This method is only to call respective scripts
/// @param aActivated if set, this is an UI activation, otherwise a UI timeout
void uiActivation(bool aActivated);
#endif // ENABLE_LVGLUI_SCRIPT_FUNCS
/// initialize for use with a specified display
/// @param aDisplay the display to use
void initForDisplay(lv_disp_t* aDisplay);
/// clear current UI and set new config
/// @param aConfig the new config for the UI
ErrorPtr setConfig(JsonObjectPtr aConfig);
/// can be used to re-configure UI later (e.g. to add more screens) without clearing existing UI hierarchy
/// @param aConfig configuration to apply to the global lvgl container, e.g. new screen or style
/// @return ok or error
virtual ErrorPtr configure(JsonObjectPtr aConfig) P44_OVERRIDE;
/// get named theme (from themes defined in config)
/// @param aThemeName the name of the theme
/// @return specified theme or NULL if not found
lv_theme_t* namedTheme(const string aThemeName);
/// get named style (custom as defined in config or built-in)
/// @param aStyleName the name of the style
/// @return specified style or NULL if not found
lv_style_t* namedStyle(const string aStyleName);
/// get named style from styles list or create ad-hoc style from definition
/// @param aStyleNameOrDefinition single string with the name of an existing style, or object defining an ad-hoc style
/// @param aDefaultToPlain if true, and style does not exist or cannot be defined ad-hoc, return the plain style instead of NULL
/// @return specified existig or ad-hoc style, NULL (or plain if aDefaultToPlain is set) if specified style cannot be returned
lv_style_t* namedOrAdHocStyle(JsonObjectPtr aStyleNameOrDefinition, bool aDefaultToPlain);
/// get image file path, will possibly look up in different places (resources, data)
/// @param aImageSpec a path or filename specifying an image
/// @return absolute path to existing image file, or empty string if none of the possible places contain the file
virtual string imagePath(const string aImageSpec);
/// get image source specification by name
/// @param aImageSpec a path specifying an image
/// @note names containing dots will be considered file paths. Other texts are considered symbol names.
/// fallback is a text image label.
/// @return image specification (file path or symbol)
string namedImageSource(const string& aImageSpec);
/// @param aElementPath dot separated absolute path beginning at root container, or dot-prefixed relative path
/// (.elem = one of my subelements, ..elem=a sibling (element in my parent's container), ...=grandparent, etc.)
/// @param aOrigin the origin for relative paths
/// @return requested element or NULL if none found
LVGLUiElementPtr namedElement(string aElementPath, LVGLUiElementPtr aOrigin);
/// load named screen and call its onrefreshscript
/// @param aScreenName the name of the screen to load
void loadScreen(const string aScreenName);
/// set resource loading options
/// @param aFromDataPath if set, non-absolute resource (image) file names are first looked up in datapath
/// @param aPrefix if not empty and image spec does not start with "./", this is prepended to the image spec
/// in both data and resource paths
void setResourceLoadOptions(bool aFromDataPath, const string aPrefix);
};
#if ENABLE_LVGLUI_SCRIPT_FUNCS
namespace P44Script {
/// represents a object of a LvGLUI object hierarchy
class LVGLUiElementObj : public StructuredLookupObject
{
friend class p44::LvGLUi;
typedef P44Script::StructuredLookupObject inherited;
LVGLUiElementPtr mElement;
public:
LVGLUiElementObj(LVGLUiElementPtr aElement);
virtual string getAnnotation() const P44_OVERRIDE { return "lvglObj"; };
LVGLUiElementPtr element() { return mElement; }
};
#if LVGLUI_LEGCACY_FUNCTIONS
/// represents the global objects related to lvglui
class LvGLUiLookup : public BuiltInMemberLookup
{
typedef BuiltInMemberLookup inherited;
LvGLUi& mLvGLUi;
public:
LvGLUi* lvglui() { return &mLvGLUi; };
LvGLUiLookup(LvGLUi &aLvGLUi);
};
#endif // LVGLUI_LEGCACY_FUNCTIONS
}
#endif // ENABLE_LVGLUI_SCRIPT_FUNCS
} // namespace p44
#endif // ENABLE_LVGL
#endif /* __p44utils__lvglui__ */