From 6d3002c87b04130a5541aa9f9a4318b8beec21ee Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Sun, 29 Jan 2023 19:57:28 +0100 Subject: [PATCH] Added variable-range types for progress bars --- CMakeLists.txt | 4 ++ src/widgets/variablerangeprogressbar.cpp | 33 +++++++++++++++ src/widgets/variablerangeprogressbar.h | 42 +++++++++++++++++++ .../variablerangereverseprogressbar.cpp | 33 +++++++++++++++ src/widgets/variablerangereverseprogressbar.h | 42 +++++++++++++++++++ 5 files changed, 154 insertions(+) create mode 100644 src/widgets/variablerangeprogressbar.cpp create mode 100644 src/widgets/variablerangeprogressbar.h create mode 100644 src/widgets/variablerangereverseprogressbar.cpp create mode 100644 src/widgets/variablerangereverseprogressbar.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fa3451a..4da994a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,8 @@ set(headers src/widgets/progressbar.h src/widgets/reverseprogressbar.h src/widgets/slider.h + src/widgets/variablerangeprogressbar.h + src/widgets/variablerangereverseprogressbar.h src/widgets/verticalmeter.h src/widgets/vumeter.h ) @@ -90,6 +92,8 @@ set(sources src/widgets/progressbar.cpp src/widgets/reverseprogressbar.cpp src/widgets/slider.cpp + src/widgets/variablerangeprogressbar.cpp + src/widgets/variablerangereverseprogressbar.cpp src/widgets/verticalmeter.cpp src/widgets/vumeter.cpp ) diff --git a/src/widgets/variablerangeprogressbar.cpp b/src/widgets/variablerangeprogressbar.cpp new file mode 100644 index 0000000..0063e93 --- /dev/null +++ b/src/widgets/variablerangeprogressbar.cpp @@ -0,0 +1,33 @@ +#include "variablerangeprogressbar.h" + +// 3rdparty lib includes +#include + +// local includes +#include "tftinterface.h" +#include "tftcolors.h" + +namespace espgui { +VariableRangeProgressBar::VariableRangeProgressBar(int x, int y, int width, int height, int min, int max, uint32_t color) : + m_x{x}, m_y{y}, m_width{width}, m_height{height}, m_min{min}, m_max{max}, m_color{color} +{ +} + +void VariableRangeProgressBar::start(TftInterface &tft) +{ + m_lastValue = m_x+1; + tft.drawRect(m_x, m_y, m_width, m_height, TFT_WHITE); +} + +void VariableRangeProgressBar::redraw(TftInterface &tft, int value) +{ + value = cpputils::mapValueClamped(value, m_min, m_max, m_x+1, m_x+m_width-1); + + if (value < m_lastValue) + tft.fillRect(value, m_y+1, m_lastValue-value, m_height-2, TFT_BLACK); + else if (value > m_lastValue) + tft.fillRect(m_lastValue, m_y+1, value-m_lastValue, m_height-2, m_color); + + m_lastValue = value; +} +} // namespace espgui diff --git a/src/widgets/variablerangeprogressbar.h b/src/widgets/variablerangeprogressbar.h new file mode 100644 index 0000000..596cd6f --- /dev/null +++ b/src/widgets/variablerangeprogressbar.h @@ -0,0 +1,42 @@ +#pragma once + +// system includes +#include + +// 3rdparty lib includes +#include + +// local includes +#include "tftcolors.h" + +// forward declares +namespace espgui { class TftInterface; } + +namespace espgui { +class VariableRangeProgressBar +{ +public: + VariableRangeProgressBar(int x, int y, int width, int height, int min, int max, uint32_t color = TFT_YELLOW); + + void start(TftInterface &tft); + void redraw(TftInterface &tft, int value); + + void setMin(int min) { m_min = min; } + void setMax(int max) { m_max = max; } + void setMinMax(int min, int max) { m_min = min; m_max = max; } + + int getMin() const { return m_min; } + int getMax() const { return m_max; } + +private: + const int m_x; + const int m_y; + const int m_width; + const int m_height; + int m_min; + int m_max; + const uint32_t m_color; + + int m_lastValue{}; +}; +} // namespace espgui diff --git a/src/widgets/variablerangereverseprogressbar.cpp b/src/widgets/variablerangereverseprogressbar.cpp new file mode 100644 index 0000000..9614ea0 --- /dev/null +++ b/src/widgets/variablerangereverseprogressbar.cpp @@ -0,0 +1,33 @@ +#include "variablerangereverseprogressbar.h" + +// 3rdparty lib includes +#include + +// local includes +#include "tftinterface.h" +#include "tftcolors.h" + +namespace espgui { +VariableRangeReverseProgressBar::VariableRangeReverseProgressBar(int x, int y, int width, int height, int min, int max, uint32_t color) : + m_x{x}, m_y{y}, m_width{width}, m_height{height}, m_min{min}, m_max{max}, m_color{color} +{ +} + +void VariableRangeReverseProgressBar::start(TftInterface &tft) +{ + m_lastValue = m_x+m_width-1; + tft.drawRect(m_x, m_y, m_width, m_height, TFT_WHITE); +} + +void VariableRangeReverseProgressBar::redraw(TftInterface &tft, int value) +{ + value = cpputils::mapValueClamped(value, m_min, m_max, m_x+m_width-1, m_x+1); + + if (value < m_lastValue) + tft.fillRect(value, m_y+1, m_lastValue-value, m_height-2, m_color); + else if (value > m_lastValue) + tft.fillRect(m_lastValue, m_y+1, value-m_lastValue, m_height-2, TFT_BLACK); + + m_lastValue = value; +} +} // namespace espgui diff --git a/src/widgets/variablerangereverseprogressbar.h b/src/widgets/variablerangereverseprogressbar.h new file mode 100644 index 0000000..04b9308 --- /dev/null +++ b/src/widgets/variablerangereverseprogressbar.h @@ -0,0 +1,42 @@ +#pragma once + +// system includes +#include + +// 3rdparty lib includes +#include + +// local includes +#include "tftcolors.h" + +// forward declares +namespace espgui { class TftInterface; } + +namespace espgui { +class VariableRangeReverseProgressBar +{ +public: + VariableRangeReverseProgressBar(int x, int y, int width, int height, int min, int max, uint32_t color=TFT_YELLOW); + + void start(TftInterface &tft); + void redraw(TftInterface &tft, int value); + + void setMin(int min) { m_min = min; } + void setMax(int max) { m_max = max; } + void setMinMax(int min, int max) { m_min = min; m_max = max; } + + int getMin() const { return m_min; } + int getMax() const { return m_max; } + +private: + const int m_x; + const int m_y; + const int m_width; + const int m_height; + int m_min; + int m_max; + const uint32_t m_color; + + int m_lastValue{}; +}; +} // namespace espgui