UI_Lib  a1366e08a59cc549a65fa26081e6409aa12f26d5
This is a user interface library for graphical LCDs. It offers many different controls and indicators that can be nested depending on the element types.
Loading...
Searching...
No Matches
EnumControl.cpp
1/*
2 * EnumControl.cpp
3 */
4
6#include "Core/UI_Manager.h"
7
8template <class T>
9EnumControl<T>::EnumControl(T* valuePointer, const char** enumNames, uint8_t numEnumValues, void* controlContext, void(*onValueChanged)(void* controlContext), uint16_t locX, uint16_t locY) : EnumIndicator<T>(valuePointer, enumNames, numEnumValues, locX, locY)
10{
11 this->Type = UI_CONTROL;
12 _controlContext = controlContext;
13 _onValueChanged = onValueChanged;
14}
15
16template <class T>
17void EnumControl<T>::Draw(bool redraw)
18{
19 if (this->Visible)
20 {
21 redraw = redraw || (this->_lastValueDraw != *this->_valuePointer) || (this->IsInEditMode != _lastDrawnEditMode) || !this->_lastDrawnVisible;
22 if(redraw)
23 {
24 this->_lastDrawnVisible = true;
26 UiManager.Gfx->fillRect(this->LocX, this->LocY, this->Width, this->Height, UiManager.ColorBackground);
27
28 if (this->IsInEditMode)
29 {
30 UiManager.Gfx->fillRect(this->LocX + UiManager.ElementMargin, this->LocY + UiManager.ElementMargin, this->Width - 2 * UiManager.ElementMargin, this->Height - 2 * UiManager.ElementMargin, UiManager.ColorForeground);
31 UiManager.Gfx->setTextColor(UiManager.ColorForegroundEditMode);
32 }
33 else
34 {
35 UiManager.Gfx->drawFastHLine(this->LocX + UiManager.ElementMargin + 1, this->LocY + this->Height - UiManager.ElementMargin - UiManager.ElementPadding, this->Width - 2 * UiManager.ElementMargin - 2, UiManager.ColorForeground);
36 }
37
39
40 if(this->IsInEditMode)
41 {
42 // Reset text color back to default foreground
43 UiManager.Gfx->setTextColor(UiManager.ColorForeground);
44 }
45 }
46 }
47 else if(!this->Visible && this->_lastDrawnVisible) // clear only when the Visible property changes from true to false
48 {
49 this->_lastDrawnVisible = false;
50 UiManager.Gfx->fillRect(this->LocX, this->LocY, this->Width, this->Height, UiManager.ColorBackground);
51 }
52}
53
54template <class T>
56{
57 switch (key)
58 {
59 case KEYUP:
60 return NextValue();
61 case KEYDOWN:
62 return PreviousValue();
63 case KEYOK:
65 return true;
66 default:
67 return false;
68 }
69}
70
71template <class T>
72bool EnumControl<T>::TouchInput(uint16_t x, uint16_t y, TouchTypes touchType)
73{
74 if(this->HitTest(x, y))
75 {
76 switch (touchType)
77 {
78 case TOUCH_NORMAL:
79 {
80 bool touchWasInRightHalf = (x >= (this->LocX + this->Width / 2));
81 if(touchWasInRightHalf)
82 {
83 NextValue();
84 }
85 else
86 {
88 }
89 return true;
90 }
91 case TOUCH_LONG:
92 {
94 return true;
95 }
96 default: break;
97 }
98 }
99 return false;
100}
101
102template <class T>
104{
105 if (this->IsInEditMode)
106 {
107 if (*this->_valuePointer > 0) { (*((int*)this->_valuePointer))--; }
108 else if(*this->_valuePointer == 0) { (*this->_valuePointer) = (T)(this->_numEnumValues - 1); }
109
111 return true;
112 }
113 return false;
114}
115
116template <class T>
118{
119 if (this->IsInEditMode)
120 {
121 if(*this->_valuePointer < (this->_numEnumValues - 1)) { (*((int*)this->_valuePointer))++; }
122 else if(*this->_valuePointer >= (this->_numEnumValues - 1)) { (*this->_valuePointer) = (T)0; }
123
125 return true;
126 }
127 return false;
128}
129
130template <class T>
132{
133 UiManager.UpdateIsInEditModeElement(this, !this->IsInEditMode);
134}
Containing a class for a enum control that is showing a enumeration variable value and offers the pos...
@ KEYOK
OK (Enter) key.
Definition Keys.h:36
@ KEYDOWN
Down arrow key.
Definition Keys.h:35
@ KEYUP
Up arrow key.
Definition Keys.h:34
enum Keys Keys_t
Available input keys.
TouchTypes
Available touch types.
Definition TouchTypes.h:13
@ TOUCH_LONG
Long touch.
Definition TouchTypes.h:15
@ TOUCH_NORMAL
Normal touch.
Definition TouchTypes.h:14
Containing a class that is used to handle the drawing and key handling of all UI_Elements.
UI_Manager UiManager
Access object for the singleton instance of the UI_Manager.
Definition UI_Manager.cpp:7
@ UI_CONTROL
Control elements are used to show informations and offer the possibility to change some values.
Definition UIElementType.h:16
virtual void Draw(bool redraw) override
Method used for drawing of the EnumControl.
Definition EnumControl.cpp:17
virtual bool TouchInput(uint16_t x, uint16_t y, TouchTypes touchType) override
Process a touch input at the given point (x, y)
Definition EnumControl.cpp:72
void * _controlContext
Context pointer that is returned with the _onValueChanged function pointer.
Definition EnumControl.h:19
void(* _onValueChanged)(void *controlContext)
Function pointer for _onValueChanged event.
Definition EnumControl.h:20
bool PreviousValue()
Set the value of the controlled enumeration variable to the previous enum value.
Definition EnumControl.cpp:103
EnumControl(T *valuePointer, const char **enumNames, uint8_t numEnumValues, void *controlContext=NULL, void(*onValueChanged)(void *controlContext)=NULL, uint16_t locX=0, uint16_t locY=0)
Constructor of the EnumControl.
Definition EnumControl.cpp:9
void ToggleEditMode()
Toggle the control between display and edit mode.
Definition EnumControl.cpp:131
bool NextValue()
Set the value of the controlled enumeration variable to the next enum value.
Definition EnumControl.cpp:117
virtual bool KeyInput(Keys_t key) override
Process the given key.
Definition EnumControl.cpp:55
bool _lastDrawnEditMode
The EditMode that was last drawn.
Definition EnumControl.h:21
T * _valuePointer
Pointer to the enumeration variable that is shown by this indicator.
Definition EnumIndicator.h:21
EnumIndicator(T *valuePointer, const char **enumNames, uint8_t numEnumValues, uint16_t locX=0, uint16_t locY=0)
Constructor of the EnumIndicator.
Definition EnumIndicator.cpp:9
uint8_t _numEnumValues
Number of enumeration values.
Definition EnumIndicator.h:20
T _lastValueDraw
Last drawn enum value.
Definition EnumIndicator.h:22
virtual void Draw(bool redraw) override
Method used for drawing of the EnumIndicator.
Definition EnumIndicator.cpp:17
bool _lastDrawnVisible
The Visible value that was last drawn.
Definition UIElement.h:26
bool IsInEditMode
Is the UIElement in edit mode?
Definition UIElement.h:35
UIElementType Type
Element type (control, indicator, container)
Definition UIElement.h:29
bool HitTest(uint16_t x, uint16_t y)
Check if the given point (x, y) is inside this UIElement.
Definition UIElement.h:109
uint16_t Height
Drawing height of the UIElement.
Definition UIElement.h:33
uint16_t LocY
Y Location of the upper left corner of the UIElement.
Definition UIElement.h:31
bool Visible
The UIElement is only drawn if the visibility is set to true.
Definition UIElement.h:34
uint16_t Width
Drawing width of the UIElement.
Definition UIElement.h:32
uint16_t LocX
X Location of the upper left corner of the UIElement.
Definition UIElement.h:30