Added slider

This commit is contained in:
CommanderRedYT
2022-05-24 16:12:08 +02:00
parent 0f4b9033dc
commit 6bd5afa6fc
3 changed files with 77 additions and 0 deletions

View File

@ -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
)

44
src/widgets/slider.cpp Normal file
View File

@ -0,0 +1,44 @@
#include "slider.h"
// 3rdparty lib includes
#include <cpputils.h>
// 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

31
src/widgets/slider.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
// system includes
#include <cstdint>
// 3rdparty lib includes
#include <TFT_eSPI.h>
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