New classes for DynamicText and DynamicNumber<T>
This commit is contained in:
+4
-4
@@ -474,7 +474,7 @@ coord_t BitmapBuffer::drawSizedText(coord_t x, coord_t y, const char * s, uint8_
|
||||
coord_t BitmapBuffer::drawNumber(coord_t x, coord_t y, int32_t val, LcdFlags flags, uint8_t len, const char * prefix, const char * suffix)
|
||||
{
|
||||
char str[48+1]; // max=16 for the prefix, 16 chars for the number, 16 chars for the suffix
|
||||
char *s = str+32;
|
||||
char *s = str + 32;
|
||||
*s = '\0';
|
||||
int idx = 0;
|
||||
int mode = MODE(flags);
|
||||
@@ -487,13 +487,13 @@ coord_t BitmapBuffer::drawNumber(coord_t x, coord_t y, int32_t val, LcdFlags fla
|
||||
*--s = '0' + (val % 10);
|
||||
++idx;
|
||||
val /= 10;
|
||||
if (mode!=0 && idx==mode) {
|
||||
if (mode != 0 && idx == mode) {
|
||||
mode = 0;
|
||||
*--s = '.';
|
||||
if (val==0)
|
||||
if (val == 0)
|
||||
*--s = '0';
|
||||
}
|
||||
} while (val!=0 || mode>0 || (mode==MODE(LEADING0) && idx<len));
|
||||
} while (val != 0 || mode > 0 || (mode == MODE(LEADING0) && idx < len));
|
||||
if (neg) *--s = '-';
|
||||
|
||||
// TODO needs check on all string lengths ...
|
||||
|
||||
+2
-2
@@ -28,7 +28,7 @@ constexpr WindowFlags BUTTON_CHECKED = FORM_FLAGS_LAST << 2;
|
||||
constexpr WindowFlags BUTTON_CHECKED_ON_FOCUS = FORM_FLAGS_LAST << 3;
|
||||
constexpr WindowFlags BUTTON_DISABLED = FORM_FLAGS_LAST << 4;
|
||||
|
||||
class Button : public FormField {
|
||||
class Button: public FormField {
|
||||
public:
|
||||
Button(FormGroup * parent, const rect_t & rect, std::function<uint8_t(void)> pressHandler = nullptr, WindowFlags windowFlags = 0):
|
||||
FormField(parent, rect, windowFlags),
|
||||
@@ -104,7 +104,7 @@ class Button : public FormField {
|
||||
void onPress();
|
||||
};
|
||||
|
||||
class TextButton : public Button {
|
||||
class TextButton: public Button {
|
||||
public:
|
||||
TextButton(FormGroup * parent, const rect_t & rect, std::string text, std::function<uint8_t(void)> pressHandler = nullptr, WindowFlags windowFlags = BUTTON_BACKGROUND):
|
||||
Button(parent, rect, pressHandler, windowFlags | OPAQUE),
|
||||
|
||||
+2
-2
@@ -52,7 +52,7 @@ class ModalWindow: public Window
|
||||
return true;
|
||||
}
|
||||
|
||||
bool onTouchEnd(coord_t x, coord_t y)
|
||||
bool onTouchEnd(coord_t x, coord_t y) override
|
||||
{
|
||||
if (!Window::onTouchEnd(x, y)) {
|
||||
deleteLater();
|
||||
@@ -60,7 +60,7 @@ class ModalWindow: public Window
|
||||
return true;
|
||||
}
|
||||
|
||||
bool onTouchSlide(coord_t x, coord_t y, coord_t startX, coord_t startY, coord_t slideX, coord_t slideY)
|
||||
bool onTouchSlide(coord_t x, coord_t y, coord_t startX, coord_t startY, coord_t slideX, coord_t slideY) override
|
||||
{
|
||||
Window::onTouchSlide(x, y, startX, startY, slideX, slideY);
|
||||
return true;
|
||||
|
||||
+1
-1
@@ -22,7 +22,7 @@
|
||||
|
||||
#include "form.h"
|
||||
|
||||
class Slider : public FormField {
|
||||
class Slider: public FormField {
|
||||
public:
|
||||
Slider(Window * parent, const rect_t & rect, int32_t vmin, int32_t vmax, std::function<int32_t()> getValue, std::function<void(int32_t)> setValue):
|
||||
FormField(parent, rect),
|
||||
|
||||
+56
-1
@@ -23,7 +23,7 @@
|
||||
#include "window.h"
|
||||
#include "button.h" // TODO just for BUTTON_BACKGROUND
|
||||
|
||||
class StaticText : public Window {
|
||||
class StaticText: public Window {
|
||||
public:
|
||||
StaticText(Window * parent, const rect_t & rect, std::string text = "", WindowFlags windowFlags = 0, LcdFlags textFlags = 0) :
|
||||
Window(parent, rect, windowFlags, textFlags),
|
||||
@@ -108,4 +108,59 @@ class StaticBitmap: public Window {
|
||||
const BitmapBuffer * bitmap = nullptr;
|
||||
};
|
||||
|
||||
class DynamicText: public StaticText
|
||||
{
|
||||
public:
|
||||
DynamicText(Window * parent, const rect_t & rect, std::function<std::string()> textHandler):
|
||||
StaticText(parent, rect),
|
||||
textHandler(std::move(textHandler))
|
||||
{
|
||||
}
|
||||
|
||||
void checkEvents() override
|
||||
{
|
||||
std::string newText = textHandler();
|
||||
if (newText != text) {
|
||||
text = newText;
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
std::function<std::string()> textHandler;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class DynamicNumber: public Window
|
||||
{
|
||||
public:
|
||||
DynamicNumber(Window * parent, const rect_t & rect, std::function<T()> numberHandler, LcdFlags textFlags = 0, const char * prefix = nullptr, const char * suffix = nullptr):
|
||||
Window(parent, rect, 0, textFlags),
|
||||
numberHandler(std::move(numberHandler)),
|
||||
prefix(prefix),
|
||||
suffix(suffix)
|
||||
{
|
||||
}
|
||||
|
||||
void paint(BitmapBuffer * dc) override
|
||||
{
|
||||
dc->drawNumber(0, FIELD_PADDING_TOP, value, textFlags, 0, prefix, suffix);
|
||||
}
|
||||
|
||||
void checkEvents() override
|
||||
{
|
||||
T newValue = numberHandler();
|
||||
if (value != newValue) {
|
||||
value = newValue;
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
T value = 0;
|
||||
std::function<T()> numberHandler;
|
||||
const char * prefix;
|
||||
const char * suffix;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+1
-1
@@ -357,7 +357,7 @@ class Window {
|
||||
std::function<void()> closeHandler;
|
||||
std::function<void()> focusHandler;
|
||||
|
||||
void attach(Window * window);
|
||||
void attach(Window * window);
|
||||
|
||||
void addChild(Window * window)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user