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
ProgressBar.cpp
1/*
2 * ProgressBar.cpp
3 */
4
6#include "Core/UI_Manager.h"
7#include <math.h>
8
9template <class T>
11{
12 return LocX + _minValueTextWidth + UiManager.ElementPadding + (uint16_t)(((value - _minValue) / (float)(_maxValue - _minValue)) * (ProgressbarWidth - 1));
13}
14
15//------------------------------------------------------------------------------------------------------------------------------------------------
16
17template <class T>
18ProgressBar<T>::ProgressBar(T* valuePointer, T minValue, T maxValue, ProgressBarOrigin_t origin, T tickIncrement, uint16_t progressbarWidth, uint16_t progressbarHeight, uint16_t locX, uint16_t locY): UIElement(locX, locY, UI_INDICATOR)
19{
20 ProgressbarWidth = progressbarWidth;
21 ProgressbarHeight = progressbarHeight;
22 _valuePointer = valuePointer;
23 _minValue = minValue;
24 _maxValue = maxValue;
25 _origin = origin;
26 _tickIncrement = tickIncrement;
27}
28
29template <class T>
30void ProgressBar<T>::Draw(bool redraw)
31{
32 if (Visible)
33 {
35 {
36 _lastDrawnVisible = true;
37 UiManager.Gfx->fillRect(LocX, LocY, Width, Height, UiManager.ColorBackground);
38
40 if (_lastValueDraw > _maxValue) { _lastValueDraw = _maxValue; } // Coerce value to be between _minValue and _maxValue
42
43 // Draw outer border of progress bar
45
46 // Draw inner progress bar
47 uint16_t valueXCoord = xCoordinateFromValue(_lastValueDraw);
48 UiManager.Gfx->fillRect((uint16_t)min(valueXCoord, _originXCoord), LocY + Height - ProgressbarHeight, (uint16_t)abs((int16_t)valueXCoord - (int16_t)_originXCoord), ProgressbarHeight, UiManager.ColorForeground);
49
50 // Draw min and max value texts
51 char buffer[6];
52 itoa(_maxValue, buffer, 10);
53 UiManager.Gfx->setCursor(xCoordinateFromValue(_maxValue) + UiManager.ElementPadding, LocY + Height - UiManager.ElementPadding);
54 UiManager.Gfx->print(buffer);
55 itoa(_minValue, buffer, 10);
56 UiManager.Gfx->setCursor(xCoordinateFromValue(_minValue) - UiManager.ElementPadding - _minValueTextWidth, LocY + Height - UiManager.ElementPadding);
57 UiManager.Gfx->print(buffer);
58
59 if(_tickIncrement > 0) // Use _tickIncrement<=0 to disable ticks
60 {
61 for (T xVal = _minValue; xVal <= _maxValue; xVal+=_tickIncrement)
62 {
63 int xCoord = xCoordinateFromValue(xVal);
64 int tickLength = (((int)xVal) % 10 == 0 ? PROGRESSBAR_LONG_TICK_LENGTH : (((int)xVal) % 5 == 0 ? PROGRESSBAR_MIDDLE_TICK_LENGTH : PROGRESSBAR_SHORT_TICK_LENGTH));
65 UiManager.Gfx->drawFastVLine(xCoord, LocY + Height - ProgressbarHeight - tickLength, tickLength, UiManager.ColorForeground);
66 }
67 }
68 }
69 }
70 else if(!Visible && _lastDrawnVisible) // clear only when the Visible property changes from true to false
71 {
72 _lastDrawnVisible = false;
73 UiManager.Gfx->fillRect(LocX, LocY, Width, Height, UiManager.ColorBackground);
74 }
75}
76
77template <class T>
79{
80 char buffer[6];
81 itoa(_maxValue, buffer, 10);
82 UiManager.Gfx->getTextBounds(buffer, 0, 0, nullptr, nullptr, &_maxValueTextWidth, nullptr);
83 itoa(_minValue, buffer, 10);
84 UiManager.Gfx->getTextBounds(buffer, 0, 0, nullptr, nullptr, &_minValueTextWidth, nullptr);
85
86 Width = _minValueTextWidth + UiManager.ElementPadding + ProgressbarWidth + UiManager.ElementPadding + _maxValueTextWidth;
88}
89
90template <class T>
Containing a class for a progress bar indicator that is showing a horizontal progress bar.
#define PROGRESSBAR_LONG_TICK_LENGTH
Length of the tick lines used at all values dividable by 10.
Definition ProgressBar.h:21
#define PROGRESSBAR_SHORT_TICK_LENGTH
Length of the tick lines used at all values dividable by 1.
Definition ProgressBar.h:23
@ PROGRESSBAR_ORIGIN_RIGHT
Origin is on the right.
Definition ProgressBar.h:18
@ PROGRESSBAR_ORIGIN_LEFT
Origin is on the left.
Definition ProgressBar.h:16
enum ProgressBarOrigin ProgressBarOrigin_t
Available progress bar origin positions.
#define PROGRESSBAR_MIDDLE_TICK_LENGTH
Length of the tick lines used at all values dividable by 5.
Definition ProgressBar.h:22
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_INDICATOR
Indicator elements are only used to show information to the user.
Definition UIElementType.h:15
uint16_t xCoordinateFromValue(T value)
Calculate the X coordinate of the value on the progress bar.
Definition ProgressBar.cpp:10
T _lastValueDraw
This variable is updated from the _valuePointer when the value changed.
Definition ProgressBar.h:34
uint16_t ProgressbarWidth
Drawing width of the ProgressBar (without min and max texts and tick markers)
Definition ProgressBar.h:53
T _tickIncrement
Distance between tick lines drawn above the progress bar.
Definition ProgressBar.h:38
T * _valuePointer
Pointer to the numeric variable that is used to draw this indicator.
Definition ProgressBar.h:33
uint16_t _maxValueTextWidth
Text width of the maximum value text.
Definition ProgressBar.h:42
uint16_t _originXCoord
X coordinate of the origin calculated on layout reconstruction.
Definition ProgressBar.h:39
uint16_t ProgressbarHeight
Drawing height of the ProgressBar (without min and max texts and tick markers)
Definition ProgressBar.h:54
ProgressBar(T *valuePointer, T minValue, T maxValue, ProgressBarOrigin_t origin, T tickIncrement, uint16_t progressbarWidth=80, uint16_t progressbarHeight=10, uint16_t locX=0, uint16_t locY=0)
Constructor of the ProgressBar.
Definition ProgressBar.cpp:18
T _minValue
Minimum value that can be shown by the progress bar.
Definition ProgressBar.h:35
uint16_t _minValueTextWidth
Text width of the minimum value text.
Definition ProgressBar.h:41
virtual void RecalculateDimensions() override
Recalculate the Height and Width of the UIElement.
Definition ProgressBar.cpp:78
virtual void RecalculateLayout() override
Recalculate the UIElement layout (containers update the X- and Y-Location of all their items,...
Definition ProgressBar.cpp:91
virtual void Draw(bool redraw) override
Method used for drawing of the ProgressBar.
Definition ProgressBar.cpp:30
ProgressBarOrigin_t _origin
Origin position of this progress bar.
Definition ProgressBar.h:37
T _maxValue
Maximum value that can be shown by the progress bar.
Definition ProgressBar.h:36
bool _lastDrawnVisible
The Visible value that was last drawn.
Definition UIElement.h:26
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
UIElement(UIElementType type)
Constructor of the UIElement.
Definition UIElement.h:43
uint16_t LocX
X Location of the upper left corner of the UIElement.
Definition UIElement.h:30