From 6bd5afa6fc0d694b6dedf74d176295f8feb93a9a Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 24 May 2022 16:12:08 +0200 Subject: [PATCH] Added slider --- CMakeLists.txt | 2 ++ src/widgets/slider.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++ src/widgets/slider.h | 31 +++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 src/widgets/slider.cpp create mode 100644 src/widgets/slider.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e3c00e..237bfd9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ set(headers src/widgets/label.h src/widgets/progressbar.h src/widgets/reverseprogressbar.h + src/widgets/slider.h src/widgets/verticalmeter.h src/widgets/vumeter.h ) @@ -79,6 +80,7 @@ set(sources src/widgets/label.cpp src/widgets/progressbar.cpp src/widgets/reverseprogressbar.cpp + src/widgets/slider.cpp src/widgets/verticalmeter.cpp src/widgets/vumeter.cpp ) diff --git a/src/widgets/slider.cpp b/src/widgets/slider.cpp new file mode 100644 index 0000000..ef76a1c --- /dev/null +++ b/src/widgets/slider.cpp @@ -0,0 +1,44 @@ +#include "slider.h" + +// 3rdparty lib includes +#include + +// local includes +#include "tftinstance.h" + +namespace espgui { +Slider::Slider(int x, int y, int width, int height, int min, int max, uint32_t leftColor, uint32_t rightColor, uint32_t lineColor) : + m_x{x}, + m_y{y}, + m_width{width}, + m_height{height}, + m_min{min}, + m_max{max}, + m_leftColor{leftColor}, + m_rightColor{rightColor}, + m_lineColor{lineColor} +{} + +void Slider::start() +{ + m_lastValue = m_x+1; + tft.drawRect(m_x, m_y, m_width, m_height, TFT_WHITE); + tft.fillRect(m_x+1, m_y+1, m_width-2, m_height-2, m_rightColor); +} + +void Slider::redraw(int value) +{ + // slider with 1 pixel white line at position of value (mapped). Left side of line is leftColor, right side is rightColor + 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+1, m_height-2, m_rightColor); + else if (value > m_lastValue) + tft.fillRect(m_lastValue, m_y+1, value-m_lastValue, m_height-2, m_leftColor); + + m_lastValue = value; + + // draw slider line + tft.drawFastVLine(std::min(value, m_x+m_width-2), m_y+1, m_height-2, m_lineColor); +} +} // namespace espgui diff --git a/src/widgets/slider.h b/src/widgets/slider.h new file mode 100644 index 0000000..d64d93b --- /dev/null +++ b/src/widgets/slider.h @@ -0,0 +1,31 @@ +#pragma once + +// system includes +#include + +// 3rdparty lib includes +#include + +namespace espgui { +class Slider +{ +public: + Slider(int x, int y, int width, int height, int min, int max, uint32_t leftColor = color565(180, 180, 0), uint32_t rightColor = TFT_YELLOW, uint32_t lineColor = TFT_BLACK); + + void start(); + void redraw(int value); + +private: + const int m_x; + const int m_y; + const int m_width; + const int m_height; + const int m_min; + const int m_max; + const uint32_t m_leftColor; + const uint32_t m_rightColor; + const uint32_t m_lineColor; + + int m_lastValue{}; +}; +} // namespace espgui