chore(color): color UI code cleanup - round 3 (#5351)

This commit is contained in:
philmoz
2024-08-01 10:05:52 +10:00
committed by GitHub
parent a362feed91
commit 23df4f63bb
104 changed files with 554 additions and 824 deletions
+36 -19
View File
@@ -90,7 +90,7 @@ void BitmapBuffer::setData(uint16_t *d)
void BitmapBuffer::clear(LcdFlags flags)
{
drawSolidFilledRect(0, 0, _width - offsetX, _height - offsetY, flags);
drawSolidFilledRect(0, 0, _width, _height, flags);
}
void BitmapBuffer::clearClippingRect()
@@ -119,10 +119,36 @@ void BitmapBuffer::getClippingRect(coord_t &xmin, coord_t &xmax, coord_t &ymin,
ymax = this->ymax;
}
void BitmapBuffer::setOffset(coord_t offsetX, coord_t offsetY)
bool BitmapBuffer::applyClippingRect(coord_t& x, coord_t& y, coord_t& w,
coord_t& h) const
{
this->offsetX = offsetX;
this->offsetY = offsetY;
if (h < 0) {
y += h;
h = -h;
}
if (w < 0) {
x += w;
w = -w;
}
if (x >= xmax || y >= ymax) return false;
if (y < ymin) {
h += y - ymin;
y = ymin;
}
if (x < xmin) {
w += x - xmin;
x = xmin;
}
if (y + h > ymax) h = ymax - y;
if (x + w > xmax) w = xmax - x;
return data && h > 0 && w > 0;
}
void BitmapBuffer::drawBitmap(coord_t x, coord_t y, const BitmapBuffer *bmp,
@@ -130,7 +156,6 @@ void BitmapBuffer::drawBitmap(coord_t x, coord_t y, const BitmapBuffer *bmp,
coord_t srch, float scale)
{
if (!data || !bmp) return;
APPLY_OFFSET();
if (x >= xmax || y >= ymax) return;
coord_t bmpw = bmp->width();
@@ -182,7 +207,7 @@ void BitmapBuffer::drawBitmap(coord_t x, coord_t y, const BitmapBuffer *bmp,
}
if (scale == 0) {
if (bmp->getFormat() == BMP_ARGB4444) {
if (bmp->format == BMP_ARGB4444) {
DMACopyAlphaBitmap(data, _width, _height, x, y, bmp->getData(), bmpw,
bmph, srcx, srcy, srcw, srch);
} else {
@@ -211,11 +236,11 @@ void BitmapBuffer::drawBitmap(coord_t x, coord_t y, const BitmapBuffer *bmp,
const pixel_t *q = qstart;
MOVE_PIXEL_RIGHT(q, int(j / scale));
if (bmp->getFormat() == BMP_RGB565) {
if (bmp->format == BMP_RGB565) {
RGB_SPLIT(*q, r, g, b);
drawPixel(p, ARGB_JOIN(0xF, r >> 1, g >> 2, b >> 1));
} else { // bmp->getFormat() == BMP_ARGB4444
} else { // bmp->format == BMP_ARGB4444
drawPixel(p, *q);
}
MOVE_TO_NEXT_RIGHT_PIXEL(p);
@@ -232,9 +257,9 @@ void BitmapBuffer::drawBitmap(coord_t x, coord_t y, const BitmapBuffer *bmp,
const pixel_t *q = qstart;
MOVE_PIXEL_RIGHT(q, int(j / scale));
if (bmp->getFormat() == BMP_RGB565) {
if (bmp->format == BMP_RGB565) {
drawPixel(p, *q);
} else { // bmp->getFormat() == BMP_ARGB4444
} else { // bmp->format == BMP_ARGB4444
ARGB_SPLIT(*q, a, r, g, b);
drawAlphaPixel(p, a, RGB_JOIN(r << 1, g << 2, b << 1));
}
@@ -267,8 +292,6 @@ void BitmapBuffer::drawHorizontalLine(coord_t x, coord_t y, coord_t w,
{
if (opacity == OPACITY_MAX) return;
APPLY_OFFSET();
coord_t h = 1;
if (!applyClippingRect(x, y, w, h)) return;
@@ -320,8 +343,6 @@ void BitmapBuffer::drawVerticalLine(coord_t x, coord_t y, coord_t h,
{
if (opacity == OPACITY_MAX) return;
APPLY_OFFSET();
coord_t w = 1;
if (!applyClippingRect(x, y, w, h)) return;
@@ -385,7 +406,6 @@ void BitmapBuffer::drawFilledRect(coord_t x, coord_t y, coord_t w, coord_t h,
{
if (opacity == OPACITY_MAX) return;
APPLY_OFFSET();
if (!applyClippingRect(x, y, w, h)) return;
if (SOLID != pat) {
@@ -428,7 +448,6 @@ coord_t BitmapBuffer::drawSizedText(coord_t x, coord_t y, const char *s,
uint8_t len, LcdFlags flags)
{
if (!s) return x;
MOVE_OFFSET();
// LVGL does not handle non-null terminated strings
static char buffer[256];
@@ -490,11 +509,9 @@ coord_t BitmapBuffer::drawSizedText(coord_t x, coord_t y, const char *s,
}
#endif
RESTORE_OFFSET();
pos += p.x;
return ((flags & RIGHT) ? orig_pos : pos) - offsetX;
return ((flags & RIGHT) ? orig_pos : pos);
}
// Resize and convert to LVGL image data format with alpha blending to
+39 -109
View File
@@ -27,18 +27,6 @@ constexpr uint8_t SOLID = 0xFF;
constexpr uint8_t DOTTED = 0x55;
constexpr uint8_t STASHED = 0x33;
#define MOVE_OFFSET() \
coord_t offsetX = this->offsetX; \
x += offsetX; \
this->offsetX = 0; \
coord_t offsetY = this->offsetY; \
y += offsetY; \
this->offsetY = 0
#define APPLY_OFFSET() \
x += this->offsetX; \
y += this->offsetY
#define RESTORE_OFFSET() this->offsetX = offsetX, this->offsetY = offsetY
#define MOVE_PIXEL_RIGHT(p, count) p += count
#define MOVE_TO_NEXT_RIGHT_PIXEL(p) MOVE_PIXEL_RIGHT(p, 1)
@@ -73,8 +61,6 @@ class BitmapBuffer
inline void drawPixel(coord_t x, coord_t y, pixel_t value)
{
APPLY_OFFSET();
coord_t w = 1, h = 1;
if (!applyClippingRect(x, y, w, h)) return;
@@ -86,8 +72,6 @@ class BitmapBuffer
inline void drawAlphaPixel(coord_t x, coord_t y, uint8_t opacity,
pixel_t value)
{
APPLY_OFFSET();
coord_t w = 1, h = 1;
if (!applyClippingRect(x, y, w, h)) return;
@@ -118,35 +102,11 @@ class BitmapBuffer
uint8_t pat = SOLID, LcdFlags flags = 0,
uint8_t opacity = 0);
void invertRect(coord_t x, coord_t y, coord_t w, coord_t h,
LcdFlags flags = 0);
void drawFilledTriangle(coord_t x1, coord_t y1, coord_t x2, coord_t y2,
coord_t x3, coord_t y3, LcdFlags flags = 0,
uint8_t opacity = 0);
void drawCircle(coord_t x, coord_t y, coord_t radius, LcdFlags flags = 0, coord_t thickness = 1);
void drawFilledCircle(coord_t x, coord_t y, coord_t radius,
LcdFlags flags = 0);
void drawAnnulusSector(coord_t x, coord_t y, coord_t internalRadius,
coord_t externalRadius, int startAngle, int endAngle,
LcdFlags flags = 0);
void drawBitmapPatternPie(coord_t x0, coord_t y0, const MaskBitmap* img,
LcdFlags flags, int startAngle, int endAngle);
static BitmapBuffer* loadBitmap(const char* filename,
BitmapFormats fmt = BMP_INVALID);
void resizeToLVGL(coord_t w, coord_t h);
MaskBitmap* to8bitMask(size_t* size) const;
void drawBitmapPattern(coord_t x, coord_t y, const MaskBitmap* bmp,
LcdFlags flags, coord_t offset = 0, coord_t width = 0);
coord_t drawSizedText(coord_t x, coord_t y, const char* s, uint8_t len,
LcdFlags flags = 0);
@@ -156,60 +116,61 @@ class BitmapBuffer
return drawSizedText(x, y, s, strlen(s), flags);
}
coord_t drawNumber(coord_t x, coord_t y, int32_t val, LcdFlags flags = 0,
uint8_t len = 0, const char* prefix = nullptr,
const char* suffix = nullptr);
void drawBitmap(coord_t x, coord_t y, const BitmapBuffer* bmp,
coord_t srcx = 0, coord_t srcy = 0, coord_t srcw = 0,
coord_t srch = 0, float scale = 0);
void drawScaledBitmap(const BitmapBuffer* bitmap, coord_t x, coord_t y,
coord_t w, coord_t h);
void drawTextLines(coord_t left, coord_t top, coord_t width, coord_t height,
const char* str, LcdFlags flags);
void drawSource(coord_t x, coord_t y, mixsrc_t idx, LcdFlags flags = 0);
coord_t drawSwitch(coord_t x, coord_t y, int32_t idx, LcdFlags flags = 0);
void drawTimer(coord_t x, coord_t y, int32_t tme, LcdFlags flags = 0);
void drawSensorCustomValue(coord_t x, coord_t y, uint8_t sensor,
int32_t value, LcdFlags flags = 0);
void clearClippingRect();
void setClippingRect(coord_t xmin, coord_t xmax, coord_t ymin, coord_t ymax);
void getClippingRect(coord_t& xmin, coord_t& xmax, coord_t& ymin,
coord_t& ymax);
void setOffset(coord_t offsetX, coord_t offsetY);
inline void clearOffset() { setOffset(0, 0); }
inline void reset()
{
clearOffset();
clearClippingRect();
}
coord_t getOffsetX() const { return offsetX; }
coord_t getOffsetY() const { return offsetY; }
inline uint8_t getFormat() const { return format; }
inline uint16_t width() const { return _width; }
inline uint16_t height() const { return _height; }
inline pixel_t* getData() const { return data; }
uint32_t getDataSize() const { return _width * _height * sizeof(pixel_t); }
// Lua API functions
void setClippingRect(coord_t xmin, coord_t xmax, coord_t ymin, coord_t ymax);
void getClippingRect(coord_t& xmin, coord_t& xmax, coord_t& ymin,
coord_t& ymax);
MaskBitmap* to8bitMask(size_t* size) const;
void invertRect(coord_t x, coord_t y, coord_t w, coord_t h,
LcdFlags flags = 0);
void drawFilledTriangle(coord_t x1, coord_t y1, coord_t x2, coord_t y2,
coord_t x3, coord_t y3, LcdFlags flags = 0,
uint8_t opacity = 0);
void drawCircle(coord_t x, coord_t y, coord_t radius, LcdFlags flags = 0, coord_t thickness = 1);
void drawFilledCircle(coord_t x, coord_t y, coord_t radius,
LcdFlags flags = 0);
void drawAnnulusSector(coord_t x, coord_t y, coord_t internalRadius,
coord_t externalRadius, int startAngle, int endAngle,
LcdFlags flags = 0);
void drawBitmapPatternPie(coord_t x0, coord_t y0, const MaskBitmap* img,
LcdFlags flags, int startAngle, int endAngle);
void drawBitmapPattern(coord_t x, coord_t y, const MaskBitmap* bmp,
LcdFlags flags, coord_t offset = 0, coord_t width = 0);
void drawScaledBitmap(const BitmapBuffer* bitmap, coord_t x, coord_t y,
coord_t w, coord_t h);
coord_t drawNumber(coord_t x, coord_t y, int32_t val, LcdFlags flags = 0,
uint8_t len = 0, const char* prefix = nullptr,
const char* suffix = nullptr);
void drawTextLines(coord_t left, coord_t top, coord_t width, coord_t height,
const char* str, LcdFlags flags);
void drawTimer(coord_t x, coord_t y, int32_t tme, LcdFlags flags = 0);
void drawSource(coord_t x, coord_t y, mixsrc_t idx, LcdFlags flags = 0);
coord_t drawSwitch(coord_t x, coord_t y, int32_t idx, LcdFlags flags = 0);
void drawSensorCustomValue(coord_t x, coord_t y, uint8_t sensor,
int32_t value, LcdFlags flags = 0);
protected:
void drawValueWithUnit(coord_t x, coord_t y, int val, uint8_t unit,
LcdFlags flags = 0);
@@ -223,37 +184,8 @@ class BitmapBuffer
void drawDate(coord_t x, coord_t y, TelemetryItem& telemetryItem,
LcdFlags att);
inline bool applyClippingRect(coord_t& x, coord_t& y, coord_t& w,
coord_t& h) const
{
if (h < 0) {
y += h;
h = -h;
}
if (w < 0) {
x += w;
w = -w;
}
if (x >= xmax || y >= ymax) return false;
if (y < ymin) {
h += y - ymin;
y = ymin;
}
if (x < xmin) {
w += x - xmin;
x = xmin;
}
if (y + h > ymax) h = ymax - y;
if (x + w > xmax) w = xmax - x;
return data && h > 0 && w > 0;
}
bool applyClippingRect(coord_t& x, coord_t& y, coord_t& w,
coord_t& h) const;
inline void drawPixel(pixel_t* p, pixel_t value)
{
@@ -290,8 +222,6 @@ class BitmapBuffer
coord_t xmax;
coord_t ymin = 0;
coord_t ymax;
coord_t offsetX = 0;
coord_t offsetY = 0;
pixel_t* data;
pixel_t* data_end;
@@ -32,8 +32,6 @@
void BitmapBuffer::invertRect(coord_t x, coord_t y, coord_t w, coord_t h,
LcdFlags flags)
{
APPLY_OFFSET();
if (!applyClippingRect(x, y, w, h)) return;
// No 'opacity' here, only 'color'
@@ -333,12 +331,6 @@ bool BitmapBuffer::liangBarskyClipper(coord_t &x1, coord_t &y1, coord_t &x2,
void BitmapBuffer::drawLine(coord_t x1, coord_t y1, coord_t x2, coord_t y2,
uint8_t pat, LcdFlags flags)
{
// Offsets
x1 += offsetX;
y1 += offsetY;
x2 += offsetX;
y2 += offsetY;
if (!liangBarskyClipper(x1, y1, x2, y2)) return;
// TODO; Replace with LVGL line draw - currently does not support dotted line drawing
@@ -493,8 +485,6 @@ void BitmapBuffer::drawFilledTriangle(coord_t x0, coord_t y0, coord_t x1,
void BitmapBuffer::drawFilledCircle(coord_t x, coord_t y, coord_t radius,
LcdFlags flags)
{
APPLY_OFFSET();
lv_draw_rect_dsc_t rect_dsc;
lv_draw_rect_dsc_init(&rect_dsc);
rect_dsc.bg_opa = LV_OPA_COVER;
@@ -527,8 +517,6 @@ void BitmapBuffer::drawFilledCircle(coord_t x, coord_t y, coord_t radius,
void BitmapBuffer::drawCircle(coord_t x, coord_t y, coord_t radius,
LcdFlags flags, coord_t thickness)
{
APPLY_OFFSET();
lv_draw_rect_dsc_t rect_dsc;
lv_draw_rect_dsc_init(&rect_dsc);
rect_dsc.bg_opa = LV_OPA_TRANSP;
@@ -690,8 +678,6 @@ void BitmapBuffer::drawAnnulusSector(coord_t x, coord_t y,
coord_t externalRadius, int startAngle,
int endAngle, LcdFlags flags)
{
APPLY_OFFSET();
if (endAngle == startAngle)
endAngle += 1;
@@ -731,7 +717,7 @@ MaskBitmap *BitmapBuffer::to8bitMask(size_t *size) const
const pixel_t *p = getPixelPtrAbs(0, 0);
int ofs = 0;
if (getFormat() == BMP_ARGB4444) {
if (format == BMP_ARGB4444) {
for (int i = width() * height(); i > 0; i--) {
ARGB_SPLIT(*p, a __attribute__((unused)), r, g, b);
int v = (r + g + b) / 3;
@@ -760,8 +746,6 @@ void BitmapBuffer::drawBitmapPattern(coord_t x, coord_t y, const MaskBitmap *bmp
LcdFlags flags, coord_t offset,
coord_t width)
{
APPLY_OFFSET();
coord_t bmpw = bmp->width;
coord_t bmph = bmp->height;
+2 -3
View File
@@ -54,8 +54,7 @@ Button::Button(Window* parent, const rect_t& rect,
ButtonBase::ButtonBase(Window* parent, const rect_t& rect,
std::function<uint8_t(void)> pressHandler,
LvglCreate objConstruct) :
FormField(parent, rect, 0,
objConstruct ? objConstruct : lv_btn_create),
FormField(parent, rect, objConstruct ? objConstruct : lv_btn_create),
pressHandler(std::move(pressHandler))
{
}
@@ -113,7 +112,7 @@ IconButton::IconButton(Window* parent, EdgeTxIcon icon, coord_t x, coord_t y,
ButtonBase(parent, {x, y, EdgeTxStyles::UI_ELEMENT_HEIGHT, EdgeTxStyles::UI_ELEMENT_HEIGHT}, pressHandler, button_create)
{
padAll(PAD_ZERO);
iconImage = new StaticIcon(this, 0, 0, icon, COLOR_THEME_SECONDARY1);
iconImage = new StaticIcon(this, 0, 0, icon, COLOR_THEME_SECONDARY1_INDEX);
iconImage->center(EdgeTxStyles::UI_ELEMENT_HEIGHT - 4, EdgeTxStyles::UI_ELEMENT_HEIGHT - 4);
}
+1 -1
View File
@@ -77,7 +77,7 @@ static void btn_matrix_event(lv_event_t* e)
}
ButtonMatrix::ButtonMatrix(Window* parent, const rect_t& r) :
FormField(parent, r, 0, btnmatrix_create)
FormField(parent, r, btnmatrix_create)
{
lv_obj_add_flag(lvobj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
+1 -1
View File
@@ -65,7 +65,7 @@ ChoiceBase::ChoiceBase(Window* parent, const rect_t& rect,
std::function<int()> _getValue,
std::function<void(int)> _setValue,
ChoiceType type) :
FormField(parent, rect, 0, choice_create),
FormField(parent, rect, choice_create),
vmin(vmin), vmax(vmax), menuTitle(title),
_getValue(std::move(_getValue)),
_setValue(std::move(_setValue))
+8 -8
View File
@@ -51,7 +51,7 @@ BaseDialog::BaseDialog(Window* parent, const char* title,
etx_solid_bg(content->getLvObj());
lv_obj_center(content->getLvObj());
header = new StaticText(content, {0, 0, LV_PCT(100), 0}, title ? title : "", COLOR_THEME_PRIMARY2);
header = new StaticText(content, {0, 0, LV_PCT(100), 0}, title ? title : "", COLOR_THEME_PRIMARY2_INDEX);
etx_solid_bg(header->getLvObj(), COLOR_THEME_SECONDARY1_INDEX);
header->padAll(PAD_SMALL);
header->show(title != nullptr);
@@ -102,11 +102,11 @@ MessageDialog::MessageDialog(Window* parent, const char* title,
BaseDialog(parent, title, true)
{
messageWidget = new StaticText(form, {0, 0, LV_PCT(100), LV_SIZE_CONTENT},
message, messageFlags);
message, COLOR_THEME_PRIMARY1_INDEX, messageFlags);
if (info) {
infoWidget = new StaticText(form, {0, 0, LV_PCT(100), LV_SIZE_CONTENT},
info, infoFlags);
info, COLOR_THEME_PRIMARY1_INDEX, infoFlags);
}
}
@@ -116,14 +116,14 @@ void MessageDialog::onClicked() { deleteLater(); }
DynamicMessageDialog::DynamicMessageDialog(
Window* parent, const char* title, std::function<std::string()> textHandler,
const char* message, const int lineHeight, const LcdFlags textFlags) :
const char* message, const int lineHeight, LcdColorIndex color, LcdFlags textFlags) :
BaseDialog(parent, title, true)
{
messageWidget = new StaticText(form, {0, 0, LV_PCT(100), LV_SIZE_CONTENT},
message, CENTERED | COLOR_THEME_PRIMARY1);
message, COLOR_THEME_PRIMARY1_INDEX, CENTERED);
infoWidget = new DynamicText(form, {0, 0, LV_PCT(100), LV_SIZE_CONTENT},
textHandler, textFlags);
textHandler, color, textFlags);
}
void DynamicMessageDialog::onClicked() { deleteLater(); }
@@ -139,7 +139,7 @@ ConfirmDialog::ConfirmDialog(Window* parent, const char* title,
cancelHandler(std::move(cancelHandler))
{
if (message) {
new StaticText(form, {0, 0, LV_PCT(100), 0}, message, CENTERED);
new StaticText(form, {0, 0, LV_PCT(100), 0}, message, COLOR_THEME_PRIMARY1_INDEX, CENTERED);
}
auto box = new Window(form, rect_t{});
@@ -185,7 +185,7 @@ LabelDialog::LabelDialog(Window *parent, const char *label, int length, const ch
lv_obj_center(form->getLvObj());
auto hdr = new StaticText(form, {0, 0, LV_PCT(100), 0}, title,
COLOR_THEME_PRIMARY2);
COLOR_THEME_PRIMARY2_INDEX);
etx_solid_bg(hdr->getLvObj(), COLOR_THEME_SECONDARY1_INDEX);
hdr->padAll(PAD_MEDIUM);
+1 -1
View File
@@ -98,7 +98,7 @@ class DynamicMessageDialog : public BaseDialog
std::function<std::string()> textHandler,
const char* message = "",
const int lineHeight = EdgeTxStyles::PAGE_LINE_HEIGHT,
const LcdFlags textFlags = CENTERED);
LcdColorIndex color = COLOR_THEME_PRIMARY1_INDEX, LcdFlags textFlags = CENTERED);
// Attn.: FONT(XXL) is not supported by DynamicMessageDialog
protected:
+1 -2
View File
@@ -39,8 +39,7 @@ void FlexGridLayout::add(Window* w)
}
}
FormField::FormField(Window* parent, const rect_t& rect, LcdFlags textFlags,
LvglCreate objConstruct) :
FormField::FormField(Window* parent, const rect_t& rect, LvglCreate objConstruct) :
Window(parent, rect, objConstruct)
{
setTextFlag(textFlags);
+1 -2
View File
@@ -77,8 +77,7 @@ class FlexGridLayout
class FormField : public Window
{
public:
FormField(Window* parent, const rect_t& rect, LcdFlags textFlags = 0,
LvglCreate objConstruct = nullptr);
FormField(Window* parent, const rect_t& rect, LvglCreate objConstruct = nullptr);
virtual void changeEnd(bool forceChanged = false)
{
+1 -1
View File
@@ -327,7 +327,7 @@ class MenuWindowContent : public Window
setFlexLayout(LV_FLEX_FLOW_COLUMN, PAD_ZERO, MENUS_WIDTH, LV_SIZE_CONTENT);
header = new StaticText(this, {0, 0, LV_PCT(100), 0}, "",
COLOR_THEME_PRIMARY2);
COLOR_THEME_PRIMARY2_INDEX);
etx_solid_bg(header->getLvObj(), COLOR_THEME_SECONDARY1_INDEX);
header->padAll(PAD_SMALL);
header->hide();
+44 -150
View File
@@ -26,21 +26,16 @@
class NumberArea : public FormField
{
public:
NumberArea(Window* parent, const rect_t& rect, int vmin, int vmax,
std::function<int()> getValue,
std::function<void(int)> setValue = nullptr,
LcdFlags textFlags = 0) :
FormField(parent, rect, textFlags, etx_textarea_create),
vmin(vmin),
vmax(vmax),
_getValue(std::move(getValue)),
_setValue(std::move(setValue))
NumberArea(NumberEdit* parent, const rect_t& rect) :
FormField(parent, rect, etx_textarea_create),
numEdit(parent)
{
if (rect.w == 0) setWidth(DEF_W);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
etx_obj_add_style(lvobj, styles->text_align_right, LV_PART_MAIN);
if (parent->getTextFlags() & CENTERED)
etx_obj_add_style(lvobj, styles->text_align_center, LV_PART_MAIN);
else
etx_obj_add_style(lvobj, styles->text_align_right, LV_PART_MAIN);
// Allow encoder acceleration
lv_obj_add_flag(lvobj, LV_OBJ_FLAG_ENCODER_ACCEL);
@@ -71,45 +66,44 @@ class NumberArea : public FormField
event);
if (editMode) {
int value = numEdit->getValue();
switch (event) {
#if defined(HARDWARE_KEYS)
case EVT_ROTARY_RIGHT: {
int value = getValue();
auto step = vstep;
step += (rotaryEncoderGetAccel() * accelFactor) / 8;
auto step = numEdit->step;
step += (rotaryEncoderGetAccel() * numEdit->accelFactor) / 8;
do {
#if defined(USE_HATS_AS_KEYS)
value -= step;
#else
value += step;
#endif
} while (isValueAvailable && !isValueAvailable(value) &&
value <= vmax);
if (value <= vmax) {
setValue(value);
} while (numEdit->isValueAvailable && !numEdit->isValueAvailable(value) &&
value <= numEdit->vmax);
if (value <= numEdit->vmax) {
numEdit->setValue(value);
} else {
setValue(vmax);
numEdit->setValue(numEdit->vmax);
onKeyError();
}
return;
}
case EVT_ROTARY_LEFT: {
int value = getValue();
auto step = vstep;
step += (rotaryEncoderGetAccel() * accelFactor) / 8;
auto step = numEdit->step;
step += (rotaryEncoderGetAccel() * numEdit->accelFactor) / 8;
do {
#if defined(USE_HATS_AS_KEYS)
value += step;
#else
value -= step;
#endif
} while (isValueAvailable && !isValueAvailable(value) &&
value >= vmin);
if (value >= vmin) {
setValue(value);
} while (numEdit->isValueAvailable && !numEdit->isValueAvailable(value) &&
value >= numEdit->vmin);
if (value >= numEdit->vmin) {
numEdit->setValue(value);
} else {
setValue(vmin);
numEdit->setValue(numEdit->vmin);
onKeyError();
}
return;
@@ -117,35 +111,35 @@ class NumberArea : public FormField
#endif
case EVT_VIRTUAL_KEY_PLUS:
setValue(getValue() + vstep);
numEdit->setValue(value + numEdit->step);
break;
case EVT_VIRTUAL_KEY_MINUS:
setValue(getValue() - vstep);
numEdit->setValue(value - numEdit->step);
break;
case EVT_VIRTUAL_KEY_FORWARD:
setValue(getValue() + fastStep * vstep);
numEdit->setValue(value + numEdit->fastStep * numEdit->step);
break;
case EVT_VIRTUAL_KEY_BACKWARD:
setValue(getValue() - fastStep * vstep);
numEdit->setValue(value - numEdit->fastStep * numEdit->step);
break;
case EVT_VIRTUAL_KEY_DEFAULT:
setValue(vdefault);
numEdit->setValue(numEdit->vdefault);
break;
case EVT_VIRTUAL_KEY_MAX:
setValue(vmax);
numEdit->setValue(numEdit->vmax);
break;
case EVT_VIRTUAL_KEY_MIN:
setValue(vmin);
numEdit->setValue(numEdit->vmin);
break;
case EVT_VIRTUAL_KEY_SIGN:
setValue(-getValue());
numEdit->setValue(-value);
break;
}
}
@@ -164,108 +158,21 @@ class NumberArea : public FormField
}
}
void setMin(int value) { vmin = value; }
void setMax(int value) { vmax = value; }
void setDefault(int value) { vdefault = value; }
void setStep(int value) { vstep = value; }
void setFastStep(int value) { fastStep = value; }
void setAccelFactor(int value) { accelFactor = value; }
void setValue(int value)
{
auto newValue = limit(vmin, value, vmax);
if (newValue != currentValue) {
currentValue = newValue;
if (_setValue != nullptr) {
_setValue(currentValue);
}
}
updateDisplay();
}
int32_t getValue() const { return _getValue != nullptr ? _getValue() : 0; }
void setPrefix(std::string value) { prefix = std::move(value); }
void setSuffix(std::string value) { suffix = std::move(value); }
void setZeroText(std::string value) { zeroText = std::move(value); }
void setAvailableHandler(std::function<bool(int)> handler)
{
isValueAvailable = std::move(handler);
}
void setSetValueHandler(std::function<void(int)> handler)
{
_setValue = std::move(handler);
}
void setGetValueHandler(std::function<int()> handler)
{
_getValue = std::move(handler);
}
void setDisplayHandler(std::function<std::string(int value)> function)
{
displayFunction = std::move(function);
}
void setCancelHandler(std::function<void(void)> handler)
{
cancelHandler = std::move(handler);
}
void openKeyboard() { NumberKeyboard::show(this); }
void directEdit() { FormField::onClicked(); }
void update()
{
if (_getValue == nullptr) return;
currentValue = _getValue();
updateDisplay();
if (lvobj != nullptr)
lv_textarea_set_text(lvobj, numEdit->getDisplayVal().c_str());
}
static LAYOUT_VAL(DEF_W, 100, 100)
protected:
int vdefault = 0;
int vmin;
int vmax;
int vstep = 1;
int fastStep = 10;
int accelFactor = 4;
int currentValue;
std::string prefix;
std::string suffix;
std::string zeroText;
std::function<int()> _getValue;
std::function<void(int)> _setValue;
std::function<std::string(int)> displayFunction;
std::function<bool(int)> isValueAvailable;
std::function<void(void)> cancelHandler = nullptr;
void updateDisplay()
{
if (lvobj != nullptr) {
std::string str;
if (displayFunction != nullptr) {
str = displayFunction(currentValue);
} else if (!zeroText.empty() && currentValue == 0) {
str = zeroText;
} else {
str = formatNumberAsString(currentValue, textFlags, 0, prefix.c_str(),
suffix.c_str());
}
lv_textarea_set_text(lvobj, str.c_str());
}
}
NumberEdit* numEdit = nullptr;
void onCancel() override
{
if (cancelHandler)
cancelHandler();
else
FormField::onCancel();
onClicked();
}
static void numberedit_cb(lv_event_t* e)
@@ -304,7 +211,7 @@ NumberEdit::NumberEdit(Window* parent, const rect_t& rect, int vmin, int vmax,
vmin(vmin),
vmax(vmax)
{
if (rect.w == 0) setWidth(NumberArea::DEF_W);
if (rect.w == 0) setWidth(DEF_W);
setTextFlag(textFlags);
@@ -323,32 +230,14 @@ void NumberEdit::openEdit()
edit = new NumberArea(
this,
{-(PAD_MEDIUM + 2), -(PAD_TINY + 2),
lv_obj_get_width(lvobj), lv_obj_get_height(lvobj)},
this->vmin, this->vmax, _getValue, _setValue, textFlags);
lv_obj_get_width(lvobj), lv_obj_get_height(lvobj)});
edit->setChangeHandler([=]() {
update();
if (edit->hasFocus())
lv_group_focus_obj(lvobj);
edit->hide();
});
edit->setCancelHandler([=]() {
edit->onClicked();
});
}
edit->setTextFlag(textFlags);
edit->setSetValueHandler(_setValue);
edit->setGetValueHandler(_getValue);
edit->setAvailableHandler(isValueAvailable);
edit->setDisplayHandler(displayFunction);
edit->setDefault(vdefault);
edit->setMin(vmin);
edit->setMax(vmax);
edit->setStep(step);
edit->setFastStep(fastStep);
edit->setAccelFactor(accelFactor);
edit->setPrefix(prefix);
edit->setSuffix(suffix);
edit->setZeroText(zeroText);
edit->update();
edit->show();
lv_group_focus_obj(edit->getLvObj());
@@ -369,7 +258,7 @@ void NumberEdit::update()
updateDisplay();
}
void NumberEdit::updateDisplay()
std::string NumberEdit::getDisplayVal()
{
std::string str;
if (displayFunction != nullptr) {
@@ -380,7 +269,12 @@ void NumberEdit::updateDisplay()
str = formatNumberAsString(currentValue, textFlags, 0, prefix.c_str(),
suffix.c_str());
}
setText(str);
return str;
}
void NumberEdit::updateDisplay()
{
setText(getDisplayVal());
}
void NumberEdit::setValue(int value)
@@ -393,5 +287,5 @@ void NumberEdit::setValue(int value)
}
}
updateDisplay();
if (edit) edit->setValue(value);
if (edit) edit->update();
}
+6
View File
@@ -91,7 +91,11 @@ class NumberEdit : public TextButton
int32_t getValue() const { return _getValue != nullptr ? _getValue() : 0; }
static LAYOUT_VAL(DEF_W, 100, 100)
protected:
friend class NumberArea;
NumberArea* edit = nullptr;
std::function<int()> _getValue;
std::function<void(int)> _setValue;
@@ -108,6 +112,8 @@ class NumberEdit : public TextButton
std::function<std::string(int)> displayFunction;
std::function<bool(int)> isValueAvailable;
std::string getDisplayVal();
void updateDisplay();
void openEdit();
};
+1 -1
View File
@@ -92,7 +92,7 @@ Slider::Slider(Window* parent, coord_t width, int32_t vmin, int32_t vmax,
padLeft(8);
padRight(8);
slider = (new FormField(this, rect_t{}, 0, slider_create))->getLvObj();
slider = (new FormField(this, rect_t{}, slider_create))->getLvObj();
lv_obj_set_width(slider, lv_pct(100));
lv_obj_add_event_cb(slider, slider_changed_cb, LV_EVENT_VALUE_CHANGED, this);
+6 -6
View File
@@ -26,7 +26,7 @@
//-----------------------------------------------------------------------------
StaticText::StaticText(Window* parent, const rect_t& rect, std::string txt,
LcdFlags textFlags) :
LcdColorIndex color, LcdFlags textFlags) :
Window(parent, rect, lv_label_create), text(std::move(txt))
{
setTextFlag(textFlags);
@@ -34,7 +34,7 @@ StaticText::StaticText(Window* parent, const rect_t& rect, std::string txt,
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
etx_font(lvobj, FONT_INDEX(textFlags));
etx_txt_color(lvobj, indexFromColor(textFlags));
etx_txt_color(lvobj, color);
if (textFlags & CENTERED)
etx_obj_add_style(lvobj, styles->text_align_center, LV_PART_MAIN);
@@ -144,9 +144,9 @@ void DynamicNumber<int16_t>::updateText()
//-----------------------------------------------------------------------------
StaticIcon::StaticIcon(Window* parent, coord_t x, coord_t y, EdgeTxIcon icon,
LcdFlags color) :
LcdColorIndex color) :
Window(parent, rect_t{x, y, 0, 0}, lv_canvas_create),
currentColor(indexFromColor(color))
currentColor(color)
{
setWindowFlag(NO_FOCUS);
@@ -158,9 +158,9 @@ StaticIcon::StaticIcon(Window* parent, coord_t x, coord_t y, EdgeTxIcon icon,
}
StaticIcon::StaticIcon(Window* parent, coord_t x, coord_t y, const char* filename,
LcdFlags color) :
LcdColorIndex color) :
Window(parent, rect_t{x, y, 0, 0}, lv_canvas_create),
currentColor(indexFromColor(color))
currentColor(color)
{
setWindowFlag(NO_FOCUS);
+8 -8
View File
@@ -30,7 +30,7 @@ class StaticText : public Window
{
public:
StaticText(Window *parent, const rect_t &rect, std::string text = "",
LcdFlags textFlags = COLOR_THEME_PRIMARY1);
LcdColorIndex color = COLOR_THEME_PRIMARY1_INDEX, LcdFlags textFlags = 0);
#if defined(DEBUG_WINDOWS)
std::string getName() const override;
@@ -44,7 +44,7 @@ class StaticText : public Window
};
#define Subtitle(parent, text) \
StaticText(parent, rect_t{}, text, COLOR_THEME_PRIMARY1 | FONT(BOLD))
StaticText(parent, rect_t{}, text, COLOR_THEME_PRIMARY1_INDEX, FONT(BOLD))
//-----------------------------------------------------------------------------
@@ -53,8 +53,8 @@ class DynamicText : public StaticText
public:
DynamicText(Window *parent, const rect_t &rect,
std::function<std::string()> textHandler,
LcdFlags textFlags = COLOR_THEME_PRIMARY1) :
StaticText(parent, rect, "", textFlags),
LcdColorIndex color = COLOR_THEME_PRIMARY1_INDEX, LcdFlags textFlags = 0) :
StaticText(parent, rect, "", color, textFlags),
textHandler(std::move(textHandler))
{
}
@@ -75,9 +75,9 @@ class DynamicNumber : public StaticText
public:
DynamicNumber(Window *parent, const rect_t &rect,
std::function<T()> numberHandler,
LcdFlags textFlags = COLOR_THEME_PRIMARY1,
LcdColorIndex color = COLOR_THEME_PRIMARY1_INDEX, LcdFlags textFlags = 0,
const char *prefix = nullptr, const char *suffix = nullptr) :
StaticText(parent, rect, "", textFlags),
StaticText(parent, rect, "", color, textFlags),
numberHandler(std::move(numberHandler)),
prefix(prefix),
suffix(suffix)
@@ -122,9 +122,9 @@ class StaticIcon : public Window
{
public:
StaticIcon(Window *parent, coord_t x, coord_t y, EdgeTxIcon icon,
LcdFlags color);
LcdColorIndex color);
StaticIcon(Window *parent, coord_t x, coord_t y, const char* filename,
LcdFlags color);
LcdColorIndex color);
void deleteLater(bool detach = true, bool trash = true) override;
+2 -6
View File
@@ -31,15 +31,13 @@ class TextArea : public FormField
{
public:
TextArea(Window* parent, const rect_t& rect, char* value, uint8_t length) :
FormField(parent, rect, 0, etx_textarea_create), value(value), length(length)
FormField(parent, rect, etx_textarea_create), value(value), length(length)
{
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
lv_textarea_set_max_length(lvobj, length);
lv_textarea_set_placeholder_text(lvobj, "---");
if (rect.w == 0) setWidth(DEF_W);
update();
}
@@ -67,8 +65,6 @@ class TextArea : public FormField
cancelHandler = std::move(handler);
}
static LAYOUT_VAL(DEF_W, 100, 100)
protected:
char* value;
uint8_t length;
@@ -127,7 +123,7 @@ TextEdit::TextEdit(Window* parent, const rect_t& rect, char* text,
}),
updateHandler(updateHandler), text(text), length(length)
{
if (rect.w == 0) setWidth(TextArea::DEF_W);
if (rect.w == 0) setWidth(DEF_W);
update();
lv_obj_align(label, LV_ALIGN_OUT_LEFT_MID, 0, PAD_TINY);
+1 -1
View File
@@ -77,7 +77,7 @@ static void toggleswitch_event_handler(lv_event_t* e)
ToggleSwitch::ToggleSwitch(Window* parent, const rect_t& rect,
std::function<uint8_t()> getValue,
std::function<void(uint8_t)> setValue) :
FormField(parent, rect, 0, switch_create),
FormField(parent, rect, switch_create),
_getValue(std::move(getValue)),
_setValue(std::move(setValue))
{
+1 -1
View File
@@ -528,7 +528,7 @@ SetupLine::SetupLine(Window* parent, coord_t y, coord_t col2, PaddingSize paddin
createEdit(this, col2, editY);
} else {
setHeight(h);
new StaticText(this, {0, titleY, 0, titleH}, title, COLOR_THEME_PRIMARY1 | FONT(BOLD));
new StaticText(this, {0, titleY, 0, titleH}, title, COLOR_THEME_PRIMARY1_INDEX, FONT(BOLD));
}
}
+1
View File
@@ -67,6 +67,7 @@ class Window
void setTextFlag(LcdFlags flag);
void clearTextFlag(LcdFlags flag);
LcdFlags getTextFlags() const { return textFlags; }
typedef std::function<void()> CloseHandler;
void setCloseHandler(CloseHandler h) { closeHandler = std::move(h); }