Added variable-range types for progress bars

This commit is contained in:
CommanderRedYT
2023-01-29 19:57:28 +01:00
parent ea4915e750
commit 6d3002c87b
5 changed files with 154 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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