refactor(color): use LVGL styles and objects for UI, legacy cleanup (#4407)

This commit is contained in:
philmoz
2024-04-05 17:33:14 +10:00
committed by GitHub
parent 5ff5c2ab8e
commit f28c1150ed
392 changed files with 18929 additions and 19492 deletions
+2
View File
@@ -9,6 +9,8 @@ endif()
set(LIBOPENUI_SRC
libopenui_file.cpp
bitmapbuffer.cpp
bitmapbuffer_fileio.cpp
bitmapbuffer_draw_extra.cpp
window.cpp
layer.cpp
form.cpp
File diff suppressed because it is too large Load Diff
+272 -409
View File
@@ -18,431 +18,294 @@
#pragma once
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "libopenui_defines.h"
struct MaskBitmap;
class TelemetryItem;
constexpr uint8_t SOLID = 0xFF;
constexpr uint8_t DOTTED = 0x55;
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_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
#if defined(LCD_VERTICAL_INVERT) && 0
#define MOVE_PIXEL_RIGHT(p, count) p -= count
#else
#define MOVE_PIXEL_RIGHT(p, count) p += count
#endif
#define MOVE_PIXEL_RIGHT(p, count) p += count
#define MOVE_TO_NEXT_RIGHT_PIXEL(p) MOVE_PIXEL_RIGHT(p, 1)
#define MOVE_TO_NEXT_RIGHT_PIXEL(p) MOVE_PIXEL_RIGHT(p, 1)
#define USE_STB
enum BitmapFormats
{
BMP_INVALID = -1,
BMP_8BIT = 0,
BMP_RGB565,
BMP_ARGB4444
};
enum BitmapFormats { BMP_INVALID = -1, BMP_RGB565 = 0, BMP_ARGB4444 };
struct _lv_draw_ctx_t;
typedef _lv_draw_ctx_t lv_draw_ctx_t;
struct _lv_obj_t;
typedef _lv_obj_t lv_obj_t;
template<class T>
class BitmapBufferBase
{
public:
BitmapBufferBase(uint8_t format, uint16_t width, uint16_t height, T * data):
format(format),
_width(width),
_height(height),
xmax(width),
ymax(height),
data(data),
data_end(data + (width * height))
{
}
BitmapBufferBase(uint8_t format, T * data):
format(format),
_width(*((uint16_t*)data)),
_height(*(((uint16_t*)data) + 1)),
xmax(_width),
ymax(_height),
data(data + 2),
data_end(data + 2 + (_width * _height))
{
}
inline void clearClippingRect()
{
xmin = 0;
xmax = _width;
ymin = 0;
ymax = _height;
}
inline void setClippingRect(coord_t xmin, coord_t xmax, coord_t ymin, coord_t ymax)
{
this->xmin = xmin;
this->xmax = xmax;
this->ymin = ymin;
this->ymax = ymax;
}
inline void getClippingRect(coord_t & xmin, coord_t & xmax, coord_t & ymin, coord_t & ymax)
{
xmin = this->xmin;
xmax = this->xmax;
ymin = this->ymin;
ymax = this->ymax;
}
inline void setOffset(coord_t offsetX, coord_t offsetY)
{
this->offsetX = offsetX;
this->offsetY = 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 T * getData() const
{
return data;
}
uint32_t getDataSize() const
{
return _width * _height * sizeof(T);
}
inline const pixel_t * getPixelPtrAbs(coord_t x, coord_t y) const
{
#if defined(LCD_VERTICAL_INVERT) && 0
x = _width - x - 1;
y = _height - y - 1;
#endif
return &data[y * _width + x];
}
protected:
uint8_t format;
coord_t _width;
coord_t _height;
coord_t xmin = 0;
coord_t xmax;
coord_t ymin = 0;
coord_t ymax;
coord_t offsetX = 0;
coord_t offsetY = 0;
T * data;
T * data_end;
};
typedef BitmapBufferBase<const uint16_t> Bitmap;
class RLEBitmap:
public BitmapBufferBase<uint16_t>
{
public:
RLEBitmap(uint8_t format, const uint8_t* rle_data);
~RLEBitmap();
static int decode(uint8_t * dest, unsigned int destSize, const uint8_t * src);
};
class BitmapBuffer: public BitmapBufferBase<pixel_t>
{
private:
bool dataAllocated;
#if defined(DEBUG)
bool leakReported;
#endif
lv_obj_t* canvas = nullptr;
lv_draw_ctx_t* draw_ctx = nullptr;
public:
BitmapBuffer(uint8_t format, uint16_t width, uint16_t height);
BitmapBuffer(uint8_t format, uint16_t width, uint16_t height, uint16_t * data);
~BitmapBuffer();
void setDrawCtx(lv_draw_ctx_t* ctx) { draw_ctx = ctx; }
void setData(uint16_t* d) {
if (!dataAllocated) {
data = d;
data_end = d + (_width * _height);
}
}
inline void setFormat(uint8_t format)
{
this->format = format;
}
inline void clear(LcdFlags flags=0)
{
drawSolidFilledRect(0, 0, _width - offsetX, _height - offsetY, flags);
}
inline const pixel_t * getPixelPtr(coord_t x, coord_t y) const
{
APPLY_OFFSET();
coord_t w = 1, h = 1;
if (!applyClippingRect(x, y, w, h))
return nullptr;
return getPixelPtrAbs(x, y);
}
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;
drawPixelAbs(x, y, value);
}
void drawAlphaPixel(pixel_t * p, uint8_t opacity, uint16_t color);
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;
drawAlphaPixelAbs(x, y, opacity, value);
}
void drawHorizontalLine(coord_t x, coord_t y, coord_t w, uint8_t pat = SOLID, LcdFlags flags = 0, uint8_t opacity = 0);
void drawVerticalLine(coord_t x, coord_t y, coord_t h, uint8_t pat = SOLID, LcdFlags flags = 0, uint8_t opacity = 0);
void drawLine(coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t pat, LcdFlags att);
inline void drawSolidHorizontalLine(coord_t x, coord_t y, coord_t w, LcdFlags flags)
{
drawSolidFilledRect(x, y, w, 1, flags);
}
inline void drawSolidVerticalLine(coord_t x, coord_t y, coord_t h, LcdFlags flags)
{
drawSolidFilledRect(x, y, 1, h, flags);
}
void drawRect(coord_t x, coord_t y, coord_t w, coord_t h, uint8_t thickness = 1, uint8_t pat = SOLID, LcdFlags flags = 0, uint8_t opacity = 0);
inline void drawSolidRect(coord_t x, coord_t y, coord_t w, coord_t h, uint8_t thickness = 1, LcdFlags flags = 0)
{
drawSolidFilledRect(x, y, thickness, h, flags);
drawSolidFilledRect(x+w-thickness, y, thickness, h, flags);
drawSolidFilledRect(x, y, w, thickness, flags);
drawSolidFilledRect(x, y+h-thickness, w, thickness, flags);
}
void drawSolidFilledRect(coord_t x, coord_t y, coord_t w, coord_t h, LcdFlags flags = 0);
void drawFilledRect(coord_t x, coord_t y, coord_t w, coord_t h, 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);
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 uint8_t * img, LcdFlags flags, int startAngle, int endAngle);
static BitmapBuffer * loadBitmap(const char * filename, BitmapFormats fmt = BMP_INVALID);
static BitmapBuffer * loadMask(const char * filename);
static BitmapBuffer * load8bitMask(const uint8_t * lbm);
static BitmapBuffer * load8bitMaskLZ4(const uint8_t * compressed_data);
uint8_t * to8bitMask(size_t* size) const;
void drawMask(coord_t x, coord_t y, const BitmapBuffer * mask, LcdFlags flags, coord_t offsetX = 0, coord_t width = 0);
void drawBitmapPattern(coord_t x, coord_t y, const uint8_t * 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);
coord_t drawText(coord_t x, coord_t y, const char * s, LcdFlags flags = 0)
{
if (!s) return x;
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);
template<class T>
void drawBitmap(coord_t x, coord_t y, const T * bmp, coord_t srcx = 0, coord_t srcy = 0, coord_t srcw = 0, coord_t srch = 0, float scale = 0);
template<class T>
void drawScaledBitmap(const T * bitmap, coord_t x, coord_t y, coord_t w, coord_t h);
protected:
static BitmapBuffer * load_bmp(const char * filename);
static BitmapBuffer * load_stb(const char * filename, BitmapFormats fmt = BMP_INVALID);
static BitmapBuffer * load_stb_buffer(const uint8_t * buffer, int len);
static BitmapBuffer * convert_stb_bitmap(uint8_t * img, int w, int h, int n,
BitmapFormats fmt = BMP_INVALID);
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;
}
template <class T>
void drawBitmapAbs(coord_t x, coord_t y, const T* bmp, coord_t srcx = 0,
coord_t srcy = 0, coord_t srcw = 0, coord_t srch = 0,
float scale = 0);
inline void drawPixel(pixel_t * p, pixel_t value)
{
if (data && (data <= p && p < data_end)) {
*p = value;
}
#if defined(DEBUG)
else if (!leakReported) {
leakReported = true;
TRACE("BitmapBuffer(%p).drawPixel(): buffer overrun, data: %p, written at: %p", this, data, p);
}
#endif
}
inline const pixel_t * getPixelPtrAbs(coord_t x, coord_t y) const
{
#if defined(LCD_VERTICAL_INVERT) && 0
x = _width - x - 1;
y = _height - y - 1;
#endif
return &data[y * _width + x];
}
inline pixel_t * getPixelPtrAbs(coord_t x, coord_t y)
{
#if defined(LCD_VERTICAL_INVERT) && 0
x = _width - x - 1;
y = _height - y - 1;
#endif
return &data[y * _width + x];
}
inline void drawPixelAbs(coord_t x, coord_t y, pixel_t value)
{
pixel_t * p = getPixelPtrAbs(x, y);
drawPixel(p, value);
}
inline void drawAlphaPixelAbs(coord_t x, coord_t y, uint8_t opacity, uint16_t color)
{
pixel_t * p = getPixelPtrAbs(x, y);
drawAlphaPixel(p, opacity, color);
}
void drawHorizontalLineAbs(coord_t x, coord_t y, coord_t w, uint8_t pat, LcdFlags flags, uint8_t opacity);
bool liangBarskyClipper(coord_t& x1, coord_t& y1, coord_t& x2, coord_t& y2);
};
class LZ4Bitmap : public BitmapBuffer
class BitmapBuffer
{
public:
LZ4Bitmap(uint8_t format, const uint8_t* compressed_data);
~LZ4Bitmap();
};
BitmapBuffer(uint8_t format, uint16_t width, uint16_t height);
BitmapBuffer(uint8_t format, uint16_t width, uint16_t height, uint16_t* data);
class LZ4Mask : public BitmapBufferBase<uint8_t>
{
public:
LZ4Mask(const uint8_t* compressed_data);
~LZ4Mask();
~BitmapBuffer();
void setDrawCtx(lv_draw_ctx_t* ctx) { draw_ctx = ctx; }
void setData(uint16_t* d);
void clear(LcdFlags flags = 0);
inline const pixel_t* getPixelPtrAbs(coord_t x, coord_t y) const
{
return &data[y * _width + x];
}
inline pixel_t* getPixelPtrAbs(coord_t x, coord_t y)
{
return &data[y * _width + x];
}
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;
drawPixelAbs(x, y, value);
}
void drawAlphaPixel(pixel_t* p, uint8_t opacity, uint16_t color);
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;
pixel_t* p = getPixelPtrAbs(x, y);
drawAlphaPixel(p, opacity, value);
}
void drawHorizontalLine(coord_t x, coord_t y, coord_t w, uint8_t pat = SOLID,
LcdFlags flags = 0, uint8_t opacity = 0);
void drawVerticalLine(coord_t x, coord_t y, coord_t h, uint8_t pat = SOLID,
LcdFlags flags = 0, uint8_t opacity = 0);
void drawLine(coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t pat,
LcdFlags att);
void drawRect(coord_t x, coord_t y, coord_t w, coord_t h,
uint8_t thickness = 1, uint8_t pat = SOLID, LcdFlags flags = 0,
uint8_t opacity = 0);
void drawSolidRect(coord_t x, coord_t y, coord_t w, coord_t h,
uint8_t thickness = 1, LcdFlags flags = 0);
void drawSolidFilledRect(coord_t x, coord_t y, coord_t w, coord_t h,
LcdFlags flags = 0);
void drawFilledRect(coord_t x, coord_t y, coord_t w, coord_t h,
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, LcdFlags bgColor);
uint8_t* 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);
coord_t drawText(coord_t x, coord_t y, const char* s, LcdFlags flags = 0)
{
if (!s) return x;
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); }
protected:
void drawValueWithUnit(coord_t x, coord_t y, int val, uint8_t unit,
LcdFlags flags = 0);
coord_t drawGPSCoord(coord_t x, coord_t y, int32_t value,
const char* direction, LcdFlags flags,
bool seconds = true);
void drawGPSPosition(coord_t x, coord_t y, int32_t longitude,
int32_t latitude, LcdFlags flags = 0);
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;
}
inline void drawPixel(pixel_t* p, pixel_t value)
{
if (data && (data <= p && p < data_end)) {
*p = value;
}
#if defined(DEBUG)
else if (!leakReported) {
leakReported = true;
TRACE(
"BitmapBuffer(%p).drawPixel(): buffer overrun, data: %p, written at: "
"%p",
this, data, p);
}
#endif
}
inline void drawPixelAbs(coord_t x, coord_t y, pixel_t value)
{
pixel_t* p = getPixelPtrAbs(x, y);
drawPixel(p, value);
}
void drawHorizontalLineAbs(coord_t x, coord_t y, coord_t w, uint8_t pat,
LcdFlags flags, uint8_t opacity);
bool liangBarskyClipper(coord_t& x1, coord_t& y1, coord_t& x2, coord_t& y2);
protected:
uint8_t format;
coord_t _width;
coord_t _height;
coord_t xmin = 0;
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;
private:
bool dataAllocated = false;
#if defined(DEBUG)
bool leakReported;
#endif
lv_obj_t* canvas = nullptr;
lv_draw_ctx_t* draw_ctx = nullptr;
};
// Back buffer to draw
extern BitmapBuffer * lcd;
extern BitmapBuffer* lcd;
// Buffer currently on display
extern BitmapBuffer * lcdFront;
extern BitmapBuffer* lcdFront;
@@ -0,0 +1,755 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* libopenui - https://github.com/opentx/libopenui
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
// #include <math.h>
#include "bitmapbuffer.h"
#include "bitmaps.h"
#include "board.h"
#include "dma2d.h"
#include "opentx.h"
#include "strhelpers.h"
#include "telemetry/telemetry_sensors.h"
#include "translations.h"
// Drawing functions used by Lua API
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'
pixel_t color = COLOR_VAL(flags);
RGB_SPLIT(color, red, green, blue);
pixel_t bg_color = COLOR_VAL(COLOR_THEME_PRIMARY2);
RGB_SPLIT(bg_color, bgRed, bgGreen, bgBlue);
DMAWait();
for (int i = y; i < y + h; i++) {
pixel_t *p = getPixelPtrAbs(x, i);
for (int j = 0; j < w; j++) {
// TODO ASSERT_IN_DISPLAY(p);
RGB_SPLIT(*p, pRed, pGreen, pBlue);
drawPixel(p, RGB_JOIN(bgRed + red - pRed, bgGreen + green - pGreen,
bgBlue + blue - pBlue));
MOVE_TO_NEXT_RIGHT_PIXEL(p);
}
}
}
void BitmapBuffer::drawValueWithUnit(coord_t x, coord_t y, int val,
uint8_t unit, LcdFlags flags)
{
if ((flags & NO_UNIT) || unit == UNIT_RAW) {
drawNumber(x, y, val, flags & (~NO_UNIT));
} else {
drawNumber(x, y, val, flags & (~NO_UNIT), 0, nullptr, STR_VTELEMUNIT[unit]);
}
}
void BitmapBuffer::drawTextLines(coord_t left, coord_t top, coord_t width,
coord_t height, const char *str,
LcdFlags flags)
{
coord_t x = left;
coord_t y = top;
coord_t line = getFontHeightCondensed(flags & 0xFFFF);
coord_t space = getTextWidth(" ", 1, flags);
coord_t word;
const char *nxt = str;
flags &= ~(VCENTERED | CENTERED | RIGHT);
while (true) {
for (bool done = false; !done; nxt++) {
switch (nxt[0]) {
case '-':
case '/':
case ':':
case '(':
case '{':
case '[':
nxt++;
case ' ':
case '\n':
case '\0':
done = true;
}
}
nxt--;
word = getTextWidth(str, nxt - str, flags);
if (x + word > left + width && x > left) {
x = left;
y += line;
}
if (y + line > top + height) return;
drawSizedText(x, y, str, nxt - str, flags);
x += word;
switch (nxt[0]) {
case '\0':
return;
case '\n':
x = left;
y += line;
nxt++;
break;
case ' ':
x += space;
nxt++;
}
str = nxt;
}
}
void BitmapBuffer::drawSource(coord_t x, coord_t y, mixsrc_t idx,
LcdFlags flags)
{
char s[16];
getSourceString(s, idx);
drawText(x, y, s, flags);
}
coord_t BitmapBuffer::drawSwitch(coord_t x, coord_t y, int32_t idx,
LcdFlags flags)
{
return drawText(x, y, getSwitchPositionName(idx), flags);
}
void BitmapBuffer::drawTimer(coord_t x, coord_t y, int32_t tme, LcdFlags flags)
{
char str[LEN_TIMER_STRING];
TimerOptions timerOptions;
timerOptions.options = (flags & TIMEHOUR) != 0 ? SHOW_TIME : SHOW_TIMER;
getTimerString(str, tme, timerOptions);
drawText(x, y, str, flags);
}
coord_t BitmapBuffer::drawGPSCoord(coord_t x, coord_t y, int32_t value,
const char *direction, LcdFlags flags,
bool seconds)
{
std::string s = getGPSCoord(value, direction, seconds);
return drawText(x, y, s.c_str(), flags);
}
void BitmapBuffer::drawGPSPosition(coord_t x, coord_t y, int32_t longitude,
int32_t latitude, LcdFlags flags)
{
if (flags & PREC1) {
drawGPSCoord(x, y, latitude, "NS", flags, true);
drawGPSCoord(x, y + FH, longitude, "EW", flags, true);
} else {
if (flags & RIGHT) {
x = drawGPSCoord(x, y, longitude, "EW", flags, true);
drawGPSCoord(x - 5, y, latitude, "NS", flags, true);
} else {
x = drawGPSCoord(x, y, latitude, "NS", flags, true);
drawGPSCoord(x + 5, y, longitude, "EW", flags, true);
}
}
}
void BitmapBuffer::drawDate(coord_t x, coord_t y, TelemetryItem &telemetryItem,
LcdFlags att)
{
bool doTwoLines = false;
coord_t ox = x;
if (att & FONT(XL)) {
att &= ~FONT_MASK;
doTwoLines = true;
}
std::string s = getTelemDate(telemetryItem);
x = drawText(x, y, s.c_str(), att);
if (doTwoLines) {
y += FH;
x = ox;
} else {
x += 11;
}
s = getTelemTime(telemetryItem);
drawText(x, y, s.c_str(), att);
}
void BitmapBuffer::drawSensorCustomValue(coord_t x, coord_t y, uint8_t sensor,
int32_t value, LcdFlags flags)
{
if (sensor >= MAX_TELEMETRY_SENSORS) {
// Lua luaLcdDrawChannel() can call us with a bad value
return;
}
TelemetryItem &telemetryItem = telemetryItems[sensor];
TelemetrySensor &telemetrySensor = g_model.telemetrySensors[sensor];
if (telemetrySensor.unit == UNIT_DATETIME) {
drawDate(x, y, telemetryItem, flags);
} else if (telemetrySensor.unit == UNIT_GPS) {
drawGPSPosition(x, y, telemetryItem.gps.longitude,
telemetryItem.gps.latitude, flags);
} else if (telemetrySensor.unit == UNIT_TEXT) {
drawSizedText(x, flags & FONT(XL) ? y + 1 : y, telemetryItem.text,
sizeof(telemetryItem.text), flags & ~FONT(XL));
} else {
if (telemetrySensor.prec > 0) {
flags |= (telemetrySensor.prec == 1 ? PREC1 : PREC2);
}
drawValueWithUnit(
x, y, value,
telemetrySensor.unit == UNIT_CELLS ? UNIT_VOLTS : telemetrySensor.unit,
flags);
}
}
void BitmapBuffer::drawScaledBitmap(const BitmapBuffer *bitmap, coord_t x, coord_t y,
coord_t w, coord_t h)
{
if (bitmap) {
float vscale = float(h) / bitmap->height();
float hscale = float(w) / bitmap->width();
float scale = vscale < hscale ? vscale : hscale;
int xshift = (w - (bitmap->width() * scale)) / 2;
int yshift = (h - (bitmap->height() * scale)) / 2;
// TRACE(" BitmapBuffer::drawScaledBitmap()---- scale %f", scale);
drawBitmap(x + xshift, y + yshift, bitmap, 0, 0, 0, 0, scale);
}
}
//
// Liang-barsky clipping algo
//
static float lb_max(float arr[], int n)
{
float m = 0;
for (int i = 0; i < n; ++i)
if (m < arr[i]) m = arr[i];
return m;
}
// this function gives the minimum
static float lb_min(float arr[], int n)
{
float m = 1;
for (int i = 0; i < n; ++i)
if (m > arr[i]) m = arr[i];
return m;
}
bool BitmapBuffer::liangBarskyClipper(coord_t &x1, coord_t &y1, coord_t &x2,
coord_t &y2)
{
// defining variables
float p1 = -(x2 - x1);
float p2 = -p1;
float p3 = -(y2 - y1);
float p4 = -p3;
float q1 = x1 - xmin;
float q2 = xmax - x1;
float q3 = y1 - ymin;
float q4 = ymax - y1;
float posarr[5], negarr[5];
int posind = 1, negind = 1;
posarr[0] = 1;
negarr[0] = 0;
if ((p1 == 0 && q1 < 0) || (p2 == 0 && q2 < 0) || (p3 == 0 && q3 < 0) ||
(p4 == 0 && q4 < 0)) {
return false;
}
if (p1 != 0) {
float r1 = q1 / p1;
float r2 = q2 / p2;
if (p1 < 0) {
negarr[negind++] = r1; // for negative p1, add it to negative array
posarr[posind++] = r2; // and add p2 to positive array
} else {
negarr[negind++] = r2;
posarr[posind++] = r1;
}
}
if (p3 != 0) {
float r3 = q3 / p3;
float r4 = q4 / p4;
if (p3 < 0) {
negarr[negind++] = r3;
posarr[posind++] = r4;
} else {
negarr[negind++] = r4;
posarr[posind++] = r3;
}
}
float xn1, yn1, xn2, yn2;
float rn1, rn2;
rn1 = lb_max(negarr, negind); // maximum of negative array
rn2 = lb_min(posarr, posind); // minimum of positive array
if (rn1 > rn2) { // reject
return false;
}
xn1 = x1 + p2 * rn1;
yn1 = y1 + p4 * rn1; // computing new points
xn2 = x1 + p2 * rn2;
yn2 = y1 + p4 * rn2;
x1 = coord_t(xn1);
y1 = coord_t(yn1);
x2 = coord_t(xn2);
y2 = coord_t(yn2);
return true;
}
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
// except for vertical and horizontal.
// No 'opacity' here, only 'color'
pixel_t color = COLOR_VAL(flags);
int dx = x2 - x1; /* the horizontal distance of the line */
int dy = y2 - y1; /* the vertical distance of the line */
int dxabs = abs(dx);
int dyabs = abs(dy);
int sdx = sgn(dx);
int sdy = sgn(dy);
int x = dyabs >> 1;
int y = dxabs >> 1;
int px = x1;
int py = y1;
DMAWait();
if (dxabs >= dyabs) {
/* the line is more horizontal than vertical */
for (int i = 0; i <= dxabs; i++) {
if ((1 << (px % 8)) & pat) {
drawPixelAbs(px, py, color);
}
y += dyabs;
if (y >= dxabs) {
y -= dxabs;
py += sdy;
}
px += sdx;
}
} else {
/* the line is more vertical than horizontal */
for (int i = 0; i <= dyabs; i++) {
if ((1 << (py % 8)) & pat) {
drawPixelAbs(px, py, color);
}
x += dxabs;
if (x >= dyabs) {
x -= dyabs;
px += sdx;
}
py += sdy;
}
}
}
/*
Notice: BitmapBuffer::drawFilledTriangle use the implementation
made for the Adafruit GFX library, which is licensed under BSD
license.
Software License Agreement (BSD License)
Copyright (c) 2012 Adafruit Industries. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
void BitmapBuffer::drawFilledTriangle(coord_t x0, coord_t y0, coord_t x1,
coord_t y1, coord_t x2, coord_t y2,
LcdFlags flags, uint8_t opacity)
{
coord_t a, b;
#define SWAP(a, b) \
{ \
coord_t tmp = b; \
b = a; \
a = tmp; \
}
if (y0 > y1) {
SWAP(y0, y1);
SWAP(x0, x1);
}
if (y1 > y2) {
SWAP(y2, y1);
SWAP(x2, x1);
}
if (y0 > y1) {
SWAP(y0, y1);
SWAP(x0, x1);
}
if (y0 == y2) { // Handle awkward all-on-same-line case as its own thing
a = b = x0;
if (x1 < a)
a = x1;
else if (x1 > b)
b = x1;
if (x2 < a)
a = x2;
else if (x2 > b)
b = x2;
drawHorizontalLine(a, y0, b - a + 1, SOLID, flags, opacity);
return;
}
coord_t dx01 = x1 - x0, dy01 = y1 - y0, dx02 = x2 - x0, dy02 = y2 - y0,
dx12 = x2 - x1, dy12 = y2 - y1;
coord_t sa = 0, sb = 0;
coord_t last = (y1 == y2) ? y1 : y1 - 1;
coord_t y;
for (y = y0; y <= last; y++) {
a = x0 + sa / dy01;
b = x0 + sb / dy02;
sa += dx01;
sb += dx02;
if (a > b) SWAP(a, b);
drawHorizontalLine(a, y, b - a + 1, SOLID, flags, opacity);
}
sa = dx12 * (y - y1);
sb = dx02 * (y - y0);
for (; y <= y2; y++) {
a = x1 + sa / dy12;
b = x0 + sb / dy02;
sa += dx12;
sb += dx02;
if (a > b) SWAP(a, b);
drawHorizontalLine(a, y, b - a + 1, SOLID, flags, opacity);
}
}
void BitmapBuffer::drawFilledCircle(coord_t x, coord_t y, coord_t radius,
LcdFlags flags)
{
drawCircle(x, y, radius, flags, radius);
}
void BitmapBuffer::drawCircle(coord_t x, coord_t y, coord_t radius,
LcdFlags flags, coord_t thickness)
{
APPLY_OFFSET();
lv_draw_arc_dsc_t arc_dsc;
lv_draw_arc_dsc_init(&arc_dsc);
arc_dsc.width = thickness;
arc_dsc.opa = LV_OPA_COVER;
arc_dsc.color = makeLvColor(flags);
if (draw_ctx) {
lv_point_t p;
p.x = x + draw_ctx->buf_area->x1;
p.y = y + draw_ctx->buf_area->y1;
lv_draw_arc(draw_ctx, &arc_dsc, &p, radius, 0, 360);
}
#if !defined(BOOT)
else if (canvas) {
lv_canvas_draw_arc(canvas, x, y, radius, 0, 360, &arc_dsc);
}
#endif
}
class Slope
{
public:
explicit Slope(int angle)
{
if (angle < 0) angle += 360;
if (angle > 360) angle %= 360;
float radians = float(angle) * (M_PI / 180.0f);
if (angle == 0) {
left = false;
value = 100000;
} else if (angle == 360) {
left = true;
value = 100000;
} else if (angle >= 180) {
left = true;
value = -(cosf(radians) * 100 / sinf(radians));
} else {
left = false;
value = (cosf(radians) * 100 / sinf(radians));
}
}
Slope(bool left, int value) : left(left), value(value) {}
bool isBetween(const Slope &start, const Slope &end) const
{
if (left) {
if (start.left) {
if (end.left)
return end.value > start.value
? (value <= end.value && value >= start.value)
: (value <= end.value || value >= start.value);
else
return value >= start.value;
} else {
if (end.left)
return value <= end.value;
else
return end.value > start.value;
}
} else {
if (start.left) {
if (end.left)
return start.value > end.value;
else
return value >= end.value;
} else {
if (end.left)
return value <= start.value;
else
return end.value < start.value
? (value >= end.value && value <= start.value)
: (value <= start.value || value >= end.value);
}
}
}
Slope &invertVertical()
{
value = -value;
return *this;
}
Slope &invertHorizontal()
{
left = !left;
return *this;
}
protected:
bool left;
int value;
};
void BitmapBuffer::drawBitmapPatternPie(coord_t x, coord_t y,
const MaskBitmap *img, LcdFlags flags,
int startAngle, int endAngle)
{
// TODO: can this converted to LVGL primitives?
if (endAngle == startAngle) {
endAngle += 1;
}
Slope startSlope(startAngle);
Slope endSlope(endAngle);
pixel_t color = COLOR_VAL(flags);
coord_t width = img->width;
coord_t height = img->height;
const uint8_t *q = img->data;
int w2 = width / 2;
int h2 = height / 2;
DMAWait();
for (int y1 = h2 - 1; y1 >= 0; y1--) {
for (int x1 = w2 - 1; x1 >= 0; x1--) {
Slope slope(false, x1 == 0 ? 99000 : y1 * 100 / x1);
if (slope.isBetween(startSlope, endSlope)) {
drawAlphaPixel(x + w2 + x1, y + h2 - y1,
q[(h2 - y1) * width + w2 + x1] >> 4, color);
}
if (slope.invertVertical().isBetween(startSlope, endSlope)) {
drawAlphaPixel(x + w2 + x1, y + h2 + y1,
q[(h2 + y1) * width + w2 + x1] >> 4, color);
}
if (slope.invertHorizontal().isBetween(startSlope, endSlope)) {
drawAlphaPixel(x + w2 - x1, y + h2 + y1,
q[(h2 + y1) * width + w2 - x1] >> 4, color);
}
if (slope.invertVertical().isBetween(startSlope, endSlope)) {
drawAlphaPixel(x + w2 - x1, y + h2 - y1,
q[(h2 - y1) * width + w2 - x1] >> 4, color);
}
}
}
}
void BitmapBuffer::drawAnnulusSector(coord_t x, coord_t y,
coord_t internalRadius,
coord_t externalRadius, int startAngle,
int endAngle, LcdFlags flags)
{
APPLY_OFFSET();
if (endAngle == startAngle)
endAngle += 1;
startAngle -= 90;
if (startAngle < 0) startAngle += 360;
endAngle -= 90;
if (endAngle < 0) endAngle += 360;
lv_draw_arc_dsc_t arc_dsc;
lv_draw_arc_dsc_init(&arc_dsc);
arc_dsc.width = externalRadius - internalRadius;
arc_dsc.opa = LV_OPA_COVER;
arc_dsc.color = makeLvColor(flags);
if (draw_ctx) {
lv_point_t p;
p.x = x + draw_ctx->buf_area->x1;
p.y = y + draw_ctx->buf_area->y1;
lv_draw_arc(draw_ctx, &arc_dsc, &p, externalRadius, startAngle, endAngle);
}
#if !defined(BOOT)
else if (canvas) {
lv_canvas_draw_arc(canvas, x, y, externalRadius, startAngle, endAngle, &arc_dsc);
}
#endif
}
uint8_t *BitmapBuffer::to8bitMask(size_t *size) const
{
*size = width() * height() + 4;
uint8_t *mask = (uint8_t *)malloc(*size);
((uint16_t *)mask)[0] = width();
((uint16_t *)mask)[1] = height();
const pixel_t *p = getPixelPtrAbs(0, 0);
if (getFormat() == BMP_ARGB4444) {
auto ofs = 4u;
for (int i = width() * height(); i > 0; i--) {
ARGB_SPLIT(*p, a __attribute__((unused)), r, g, b);
auto v = ((uint32_t)r + (uint32_t)g + (uint32_t)b) / 3;
mask[ofs++] = ((OPACITY_MAX - v) << 4);
MOVE_TO_NEXT_RIGHT_PIXEL(p);
}
} else { // BMP_RGB565
auto ofs = 4u;
for (int i = width() * height(); i > 0; i--) {
auto v =
((GET_RED(*p) >> 1) + (GET_GREEN(*p) >> 2) + (GET_BLUE(*p) >> 1)) / 3;
mask[ofs++] = ((OPACITY_MAX - v) << 4);
MOVE_TO_NEXT_RIGHT_PIXEL(p);
}
}
return mask;
}
// Apply a mask ('bmp') + color ('flags') on top of current pixels:
//
// drawAlphaPixel(bmp[x][y], pixel(x,y), color)
//
//
//
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;
coord_t srcx = offset;
coord_t srcy = 0;
coord_t srcw = (width != 0 ? width : bmpw);
coord_t srch = bmph;
if (srcx + srcw > bmpw) srcw = bmpw - srcx;
if (srcy + srch > bmph) srch = bmph - srcy;
if (x < xmin) {
srcw += x - xmin;
srcx -= x - xmin;
x = xmin;
}
if (y < ymin) {
srch += y - ymin;
srcy -= y - ymin;
y = ymin;
}
if (x + srcw > xmax) {
srcw = xmax - x;
}
if (y + srch > ymax) {
srch = ymax - y;
}
if (srcw <= 0 || srch <= 0) {
return;
}
DMACopyAlphaMask(data, _width, _height, x, y, bmp->data, bmpw, bmph, srcx,
srcy, srcw, srch, COLOR_VAL(flags));
}
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 s[49];
formatNumberAsString(s, 49, val, flags, len, prefix, suffix);
return drawText(x, y, s, flags);
}
@@ -0,0 +1,285 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* libopenui - https://github.com/opentx/libopenui
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "bitmapbuffer.h"
#include "libopenui_file.h"
#include "opentx_helpers.h"
FIL imgFile __DMA;
// #define TRACE_STB_MALLOC
#if defined(TRACE_STB_MALLOC)
#define STBI_MALLOC(sz) stb_malloc(sz)
#define STBI_REALLOC_SIZED(p, oldsz, newsz) stb_realloc(p, oldsz, newsz)
#define STBI_FREE(p) stb_free(p)
void *stb_malloc(unsigned int size)
{
void *res = malloc(size);
TRACE("malloc %d = %p", size, res);
return res;
}
void stb_free(void *ptr)
{
TRACE("free %p", ptr);
free(ptr);
}
void *stb_realloc(void *ptr, unsigned int oldsz, unsigned int newsz)
{
void *res = realloc(ptr, newsz);
TRACE("realloc %p, %d -> %d = %p", ptr, oldsz, newsz, res);
return res;
}
#endif // #if defined(TRACE_STB_MALLOC)
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#undef __I
#define STBI_ONLY_PNG
#define STBI_ONLY_BMP
#define STBI_ONLY_JPEG
#define STBI_NO_STDIO
#define STBI_NO_LINEAR
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
// fill 'data' with 'size' bytes. return number of bytes actually read
int stbc_read(void *user, char *data, int size)
{
FIL *fp = (FIL *)user;
UINT br = 0;
FRESULT res = f_read(fp, data, size, &br);
if (res == FR_OK) {
return (int)br;
}
return 0;
}
// skip the next 'n' bytes, or 'unget' the last -n bytes if negative
void stbc_skip(void *user, int n)
{
FIL *fp = (FIL *)user;
f_lseek(fp, f_tell(fp) + n);
}
// returns nonzero if we are at end of file/data
int stbc_eof(void *user)
{
FIL *fp = (FIL *)user;
int res = f_eof(fp);
return res;
}
// callbacks for stb-image
const stbi_io_callbacks stbCallbacks = {stbc_read, stbc_skip, stbc_eof};
BitmapBuffer *BitmapBuffer::loadBitmap(const char *filename, BitmapFormats fmt)
{
// TRACE(" BitmapBuffer::load_stb(%s)", filename);
FRESULT result = f_open(&imgFile, filename, FA_OPEN_EXISTING | FA_READ);
if (result != FR_OK) {
return nullptr;
}
int x, y, nn;
stbi_info_from_callbacks(&stbCallbacks, &imgFile, &x, &y, &nn);
f_close(&imgFile);
// TRACE(" BitmapBuffer::load_stb()----Info File %s, %d, %d, %d", filename,
// x, y, nn);
result = f_open(&imgFile, filename, FA_OPEN_EXISTING | FA_READ);
if (result != FR_OK) {
return nullptr;
}
int w, h, n;
unsigned char *img =
stbi_load_from_callbacks(&stbCallbacks, &imgFile, &w, &h, &n, 4);
f_close(&imgFile);
if (!img) {
TRACE("load_stb(%s) failed: %s", filename, stbi_failure_reason());
return nullptr;
}
// convert to RGB565 or ARGB4444 format
BitmapFormats dst_fmt = fmt;
if (dst_fmt == BMP_INVALID) {
dst_fmt = (n == 4 ? BMP_ARGB4444 : BMP_RGB565);
}
BitmapBuffer *bmp = new BitmapBuffer(dst_fmt, w, h);
if (bmp == nullptr) {
TRACE("convert_stn_bitmap: malloc failed");
return nullptr;
}
pixel_t *dest = bmp->getPixelPtrAbs(0, 0);
const uint8_t *p = img;
if (dst_fmt == BMP_ARGB4444) {
for (int row = 0; row < h; ++row) {
for (int col = 0; col < w; ++col) {
*dest = ARGB(p[3], p[0], p[1], p[2]);
MOVE_TO_NEXT_RIGHT_PIXEL(dest);
p += 4;
}
}
} else { // assume 3 bytes, packed in groups of 4
for (int row = 0; row < h; ++row) {
for (int col = 0; col < w; ++col) {
*dest = RGB(p[0], p[1], p[2]);
MOVE_TO_NEXT_RIGHT_PIXEL(dest);
p += 4;
}
}
}
stbi_image_free(img);
return bmp;
}
//-----------------------------------------------------------------------------
static lv_res_t decoder_info(struct _lv_img_decoder_t *decoder, const void *src,
lv_img_header_t *header)
{
LV_UNUSED(decoder); /*Unused*/
lv_img_src_t src_type = lv_img_src_get_type(src); /*Get the source type*/
/*If it's a file...*/
if (src_type == LV_IMG_SRC_FILE) {
const char *fn = ((const char *)src) + 1;
FIL imgFile;
FRESULT result = f_open(&imgFile, fn, FA_OPEN_EXISTING | FA_READ);
if (result == FR_OK) {
int x, y, nn;
stbi_info_from_callbacks(&stbCallbacks, &imgFile, &x, &y, &nn);
f_close(&imgFile);
header->always_zero = 0;
header->cf =
(nn == 4) ? LV_IMG_CF_TRUE_COLOR_ALPHA : LV_IMG_CF_TRUE_COLOR;
header->w = x;
header->h = y;
return LV_RES_OK;
}
}
/*If it's a file in a C array...*/
else if (src_type == LV_IMG_SRC_VARIABLE) {
// Not implemented...
}
return LV_RES_INV;
}
static uint8_t *convert_bitmap(uint8_t *img, int w, int h, int n)
{
uint8_t *bmp = (uint8_t *)lv_mem_alloc(((n == 4) ? 3 : 2) * w * h);
if (bmp == nullptr) {
TRACE("convert_bitmap: lv_mem_alloc failed");
return nullptr;
}
const uint8_t *p = img;
if (n == 4) {
uint8_t *dest = bmp;
for (int row = 0; row < h; ++row) {
for (int col = 0; col < w; ++col) {
uint16_t c = RGB(p[0], p[1], p[2]);
*dest++ = c & 0xFF;
*dest++ = c >> 8;
*dest++ = p[3];
p += 4;
}
}
} else {
pixel_t *dest = (pixel_t *)bmp;
for (int row = 0; row < h; ++row) {
for (int col = 0; col < w; ++col) {
*dest = RGB(p[0], p[1], p[2]);
MOVE_TO_NEXT_RIGHT_PIXEL(dest);
p += 4;
}
}
}
return bmp;
}
static lv_res_t decoder_open(lv_img_decoder_t *decoder,
lv_img_decoder_dsc_t *dsc)
{
LV_UNUSED(decoder); /*Unused*/
/*If it's a file...*/
if (dsc->src_type == LV_IMG_SRC_FILE) {
const char *fn = ((const char *)dsc->src) + 1;
FIL imgFile;
FRESULT result = f_open(&imgFile, fn, FA_OPEN_EXISTING | FA_READ);
if (result == FR_OK) {
int w, h, n;
unsigned char *img =
stbi_load_from_callbacks(&stbCallbacks, &imgFile, &w, &h, &n, 4);
f_close(&imgFile);
if (!img) {
TRACE("decoder_open(%s) failed: %s", fn, stbi_failure_reason());
return LV_RES_INV;
}
dsc->img_data = convert_bitmap(img, w, h, n);
stbi_image_free(img);
return dsc->img_data ? LV_RES_OK : LV_RES_INV;
}
}
/*If it's a file in a C array...*/
else if (dsc->src_type == LV_IMG_SRC_VARIABLE) {
// Not implemented...
}
return LV_RES_INV;
}
static void decoder_close(lv_img_decoder_t *decoder, lv_img_decoder_dsc_t *dsc)
{
LV_UNUSED(decoder); /*Unused*/
if (dsc->img_data) {
lv_mem_free((uint8_t *)dsc->img_data);
dsc->img_data = NULL;
}
}
/**
* Register the STB decoder functions in LVGL
*/
void lv_stb_init(void)
{
lv_img_decoder_t *dec = lv_img_decoder_create();
lv_img_decoder_set_info_cb(dec, decoder_info);
lv_img_decoder_set_open_cb(dec, decoder_open);
lv_img_decoder_set_close_cb(dec, decoder_close);
}
+65 -47
View File
@@ -17,50 +17,70 @@
*/
#include "button.h"
#include "font.h"
#include "theme.h"
static void update_checked_flag(lv_obj_t* obj, WindowFlags flags)
#include "theme.h"
#include "themes/etx_lv_theme.h"
static void button_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj)
{
if (!(flags & BUTTON_CHECKED))
lv_obj_clear_state(obj, LV_STATE_CHECKED);
else
lv_obj_add_state(obj, LV_STATE_CHECKED);
etx_btn_style(obj, LV_PART_MAIN);
}
// Must not be static - inherited by menu_button_class
const lv_obj_class_t button_class = {
.base_class = &lv_btn_class,
.constructor_cb = button_constructor,
.destructor_cb = nullptr,
.user_data = nullptr,
.event_cb = nullptr,
.width_def = LV_SIZE_CONTENT,
.height_def = 32,
.editable = LV_OBJ_CLASS_EDITABLE_INHERIT,
.group_def = LV_OBJ_CLASS_GROUP_DEF_TRUE,
.instance_size = sizeof(lv_btn_t),
};
static lv_obj_t* button_create(lv_obj_t* parent)
{
return etx_create(&button_class, parent);
}
Button::Button(Window* parent, const rect_t& rect,
std::function<uint8_t(void)> pressHandler,
WindowFlags windowFlag, LcdFlags textFlags,
LvglCreate objConstruct) :
FormField(parent, rect, windowFlag, textFlags,
objConstruct ? objConstruct : lv_btn_create),
pressHandler(std::move(pressHandler))
std::function<uint8_t(void)> pressHandler) :
ButtonBase(parent, rect, pressHandler, button_create)
{
lv_obj_add_event_cb(lvobj, Button::long_pressed, LV_EVENT_LONG_PRESSED, nullptr);
update_checked_flag(lvobj, windowFlags);
}
void Button::check(bool checked)
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),
pressHandler(std::move(pressHandler))
{
if (_deleted) return;
if (checked != bool(windowFlags & BUTTON_CHECKED)) {
windowFlags ^= BUTTON_CHECKED;
update_checked_flag(lvobj, windowFlags);
invalidate();
lv_obj_add_event_cb(lvobj, ButtonBase::long_pressed, LV_EVENT_LONG_PRESSED,
nullptr);
}
void ButtonBase::check(bool checked)
{
if (!_deleted) {
if (checked != this->checked()) {
if (checked)
lv_obj_add_state(lvobj, LV_STATE_CHECKED);
else
lv_obj_clear_state(lvobj, LV_STATE_CHECKED);
}
}
}
bool Button::checked() const
bool ButtonBase::checked() const
{
return windowFlags & BUTTON_CHECKED;
return lv_obj_get_state(lvobj) & LV_STATE_CHECKED;
}
void Button::onPress()
{
check(pressHandler && pressHandler());
}
void ButtonBase::onPress() { check(pressHandler && pressHandler()); }
void Button::onLongPress()
void ButtonBase::onLongPress()
{
if (longPressHandler) {
check(longPressHandler());
@@ -69,39 +89,37 @@ void Button::onLongPress()
}
}
void Button::onClicked()
{
onPress();
}
void ButtonBase::onClicked() { onPress(); }
#if defined(HARDWARE_TOUCH)
// this needs to stay so that viewmain doesnt get the touch event
bool Button::onTouchEnd(coord_t x, coord_t y) { return true; }
#endif
void Button::checkEvents()
void ButtonBase::checkEvents()
{
Window::checkEvents();
if (checkHandler) checkHandler();
}
void Button::long_pressed(lv_event_t* e)
void ButtonBase::long_pressed(lv_event_t* e)
{
auto obj = lv_event_get_target(e);
auto btn = (Button*)lv_obj_get_user_data(obj);
auto btn = (ButtonBase*)lv_obj_get_user_data(obj);
if (obj) btn->onLongPress();
}
TextButton::TextButton(Window* parent, const rect_t& rect, std::string text,
std::function<uint8_t(void)> pressHandler,
WindowFlags windowFlags) :
Button(parent, rect, std::move(pressHandler), windowFlags, 0, etx_button_create),
text(std::move(text))
std::function<uint8_t(void)> pressHandler) :
ButtonBase(parent, rect, pressHandler, button_create), text(std::move(text))
{
update_checked_flag(lvobj, windowFlags);
label = lv_label_create(lvobj);
lv_label_set_text(label, this->text.c_str());
lv_obj_center(label);
}
IconButton::IconButton(Window* parent, EdgeTxIcon icon,
std::function<uint8_t(void)> pressHandler) :
ButtonBase(parent, {0, 0, 32, 32}, pressHandler, button_create)
{
padAll(PAD_ZERO);
iconImage = new StaticIcon(this, 0, 0, icon, COLOR_THEME_SECONDARY1);
iconImage->center(28, 28);
}
void IconButton::setIcon(EdgeTxIcon icon) { iconImage->setIcon(icon); }
+46 -36
View File
@@ -19,19 +19,20 @@
#pragma once
#include "form.h"
#include "bitmaps.h"
constexpr WindowFlags BUTTON_CHECKED = WINDOW_FLAGS_LAST << 1u;
class StaticIcon;
class Button : public FormField
// Base button class - default LVGL style unless overridden on create
class ButtonBase : public FormField
{
public:
Button(Window* parent, const rect_t& rect,
std::function<uint8_t(void)> pressHandler = nullptr,
WindowFlags windowFlags = 0, LcdFlags textFlags = 0,
LvglCreate objConstruct = nullptr);
ButtonBase(Window* parent, const rect_t& rect,
std::function<uint8_t(void)> pressHandler = nullptr,
LvglCreate objConstruct = nullptr);
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "Button"; }
std::string getName() const override { return "ButtonBase"; }
#endif
virtual void onPress();
@@ -58,10 +59,6 @@ class Button : public FormField
checkHandler = std::move(handler);
}
#if defined(HARDWARE_TOUCH)
bool onTouchEnd(coord_t x, coord_t y) override;
#endif
protected:
std::function<uint8_t(void)> pressHandler;
std::function<uint8_t(void)> longPressHandler;
@@ -70,36 +67,49 @@ class Button : public FormField
static void long_pressed(lv_event_t* e);
};
class TextButton: public Button
class Button : public ButtonBase
{
public:
TextButton(Window* parent, const rect_t& rect, std::string text,
std::function<uint8_t(void)> pressHandler = nullptr,
WindowFlags windowFlags = OPAQUE);
public:
Button(Window* parent, const rect_t& rect,
std::function<uint8_t(void)> pressHandler = nullptr);
#if defined(DEBUG_WINDOWS)
std::string getName() const override
{
return "TextButton \"" + text + "\"";
}
std::string getName() const override { return "Button"; }
#endif
};
class TextButton : public ButtonBase
{
public:
TextButton(Window* parent, const rect_t& rect, std::string text,
std::function<uint8_t(void)> pressHandler = nullptr);
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "TextButton \"" + text + "\""; }
#endif
void setText(std::string value)
{
if (value != text) {
text = std::move(value);
lv_label_set_text(label, text.c_str());
}
}
void setText(std::string value)
{
if (value != text) {
text = std::move(value);
lv_label_set_text(label, text.c_str());
}
}
void setBgColorHandler(std::function<LcdFlags(void)> handler = nullptr)
{
bgColorHandler = std::move(handler);
}
protected:
lv_obj_t* label = nullptr;
protected:
lv_obj_t * label = nullptr;
std::string text;
};
std::string text;
std::function<LcdFlags(void)> bgColorHandler = nullptr;
class IconButton : public ButtonBase
{
public:
IconButton(Window* parent, EdgeTxIcon icon,
std::function<uint8_t(void)> pressHandler = nullptr);
void setIcon(EdgeTxIcon icon);
protected:
StaticIcon* iconImage = nullptr;
};
+35 -3
View File
@@ -18,9 +18,42 @@
#include "button_matrix.h"
#include <stdlib.h>
#include <string.h>
#include "themes/etx_lv_theme.h"
static void btnmatrix_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj)
{
etx_obj_add_style(obj, styles->rounded, LV_PART_MAIN);
etx_obj_add_style(obj, styles->bg_opacity_20,
LV_PART_MAIN | LV_STATE_FOCUSED);
etx_bg_color(obj, COLOR_THEME_FOCUS_INDEX, LV_PART_MAIN | LV_STATE_FOCUSED);
etx_std_style(obj, LV_PART_ITEMS, PAD_LARGE);
etx_obj_add_style(obj, styles->border_color_focus,
LV_PART_ITEMS | LV_STATE_EDITED);
}
static const lv_obj_class_t btnmatrix_class = {
.base_class = &lv_btnmatrix_class,
.constructor_cb = btnmatrix_constructor,
.destructor_cb = nullptr,
.user_data = nullptr,
.event_cb = nullptr,
.width_def = 0,
.height_def = 0,
.editable = LV_OBJ_CLASS_EDITABLE_INHERIT,
.group_def = LV_OBJ_CLASS_GROUP_DEF_INHERIT,
.instance_size = sizeof(lv_btnmatrix_t),
};
static lv_obj_t* btnmatrix_create(lv_obj_t* parent)
{
return etx_create(&btnmatrix_class, parent);
}
static const char _filler[] = "0";
static const char _newline[] = "\n";
static const char _map_end[] = "";
@@ -44,7 +77,7 @@ static void btn_matrix_event(lv_event_t* e)
}
ButtonMatrix::ButtonMatrix(Window* parent, const rect_t& r) :
FormField(parent, r, 0, 0, etx_btnmatrix_create)
FormField(parent, r, 0, btnmatrix_create)
{
lv_obj_add_flag(lvobj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
@@ -125,7 +158,6 @@ void ButtonMatrix::onClicked()
{
lv_group_focus_obj(lvobj);
setEditMode(true);
invalidate();
}
void ButtonMatrix::setChecked(uint8_t btn_id)
+50 -59
View File
@@ -20,6 +20,32 @@
#include "menu.h"
#include "theme.h"
#include "themes/etx_lv_theme.h"
// Choice
static void choice_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj)
{
etx_std_style(obj, LV_PART_MAIN, PAD_TINY);
lv_obj_set_style_pad_hor(obj, PAD_MEDIUM, LV_PART_MAIN);
}
static const lv_obj_class_t choice_class = {
.base_class = &lv_obj_class,
.constructor_cb = choice_constructor,
.destructor_cb = nullptr,
.user_data = nullptr,
.event_cb = nullptr,
.width_def = LV_SIZE_CONTENT,
.height_def = 32,
.editable = LV_OBJ_CLASS_EDITABLE_INHERIT,
.group_def = LV_OBJ_CLASS_GROUP_DEF_INHERIT,
.instance_size = sizeof(lv_obj_t),
};
static lv_obj_t* choice_create(lv_obj_t* parent)
{
return etx_create(&choice_class, parent);
}
void choice_changed_cb(lv_event_t* e)
{
@@ -37,14 +63,13 @@ void choice_changed_cb(lv_event_t* e)
}
}
ChoiceBase::ChoiceBase(Window* parent, const rect_t& rect, ChoiceType type,
WindowFlags windowFlags) :
FormField(parent, rect, windowFlags, 0, etx_choice_create), type(type)
ChoiceBase::ChoiceBase(Window* parent, const rect_t& rect, ChoiceType type) :
FormField(parent, rect, 0, choice_create), type(type)
{
// lv_obj_set_height(lvobj, LV_SIZE_CONTENT);
// lv_obj_set_width(lvobj, LV_SIZE_CONTENT);
lv_obj_set_layout(lvobj, LV_LAYOUT_FLEX);
lv_obj_set_flex_flow(lvobj, LV_FLEX_FLOW_ROW);
lv_obj_set_flex_align(lvobj, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER,
LV_FLEX_ALIGN_SPACE_AROUND);
lv_obj_add_event_cb(lvobj, choice_changed_cb, LV_EVENT_VALUE_CHANGED, lvobj);
label = lv_label_create(lvobj);
@@ -54,31 +79,27 @@ ChoiceBase::ChoiceBase(Window* parent, const rect_t& rect, ChoiceType type,
lv_group_add_obj(def_group, lvobj);
}
lv_obj_set_style_pad_left(label, FIELD_PADDING_LEFT, LV_PART_MAIN);
lv_obj_set_style_pad_top(label, FIELD_PADDING_TOP, LV_PART_MAIN);
lv_obj_set_style_pad_right(label, CHOICE_LABEL_MARGIN_RIGHT, LV_PART_MAIN);
// lv_obj_set
// add the image
lv_obj_t* img = lv_img_create(lvobj);
lv_img_set_src(
img, type == CHOICE_TYPE_DROPOWN ? LV_SYMBOL_DOWN : LV_SYMBOL_DIRECTORY);
lv_obj_set_align(img, LV_ALIGN_RIGHT_MID);
}
std::string Choice::getLabelText()
{
std::string text;
if (textHandler != nullptr) {
if (_getValue) {
text = textHandler(_getValue());
}
} else if (_getValue) {
if (_getValue) {
int val = _getValue();
val -= vmin;
if (val >= 0 && val < (int)values.size()) {
text = values[val];
if (textHandler) {
text = textHandler(val);
} else {
val -= vmin;
if (val >= 0 && val < (int)values.size()) {
text = values[val];
} else {
text = std::to_string(val);
}
}
}
@@ -87,10 +108,11 @@ std::string Choice::getLabelText()
Choice::Choice(Window* parent, const rect_t& rect, int vmin, int vmax,
std::function<int()> _getValue,
std::function<void(int)> _setValue, WindowFlags windowFlags) :
ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN, windowFlags),
std::function<void(int)> _setValue, const char* title) :
ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN),
vmin(vmin),
vmax(vmax),
menuTitle(title),
_getValue(std::move(_getValue)),
_setValue(std::move(_setValue)),
longPressData({})
@@ -100,10 +122,11 @@ Choice::Choice(Window* parent, const rect_t& rect, int vmin, int vmax,
Choice::Choice(Window* parent, const rect_t& rect, const char* const values[],
int vmin, int vmax, std::function<int()> _getValue,
std::function<void(int)> _setValue, WindowFlags windowFlags) :
ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN, windowFlags),
std::function<void(int)> _setValue, const char* title) :
ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN),
vmin(vmin),
vmax(vmax),
menuTitle(title),
_getValue(std::move(_getValue)),
_setValue(std::move(_setValue)),
longPressData({})
@@ -115,11 +138,12 @@ Choice::Choice(Window* parent, const rect_t& rect, const char* const values[],
Choice::Choice(Window* parent, const rect_t& rect,
std::vector<std::string> values, int vmin, int vmax,
std::function<int()> _getValue,
std::function<void(int)> _setValue, WindowFlags windowFlags) :
ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN, windowFlags),
std::function<void(int)> _setValue, const char* title) :
ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN),
values(std::move(values)),
vmin(vmin),
vmax(vmax),
menuTitle(title),
_getValue(std::move(_getValue)),
_setValue(std::move(_setValue)),
longPressData({})
@@ -127,41 +151,12 @@ Choice::Choice(Window* parent, const rect_t& rect,
lv_event_send(lvobj, LV_EVENT_VALUE_CHANGED, nullptr);
}
Choice::Choice(Window* parent, const rect_t& rect, const char* values, int vmin,
int vmax, std::function<int()> _getValue,
std::function<void(int)> _setValue, WindowFlags windowFlags) :
ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN, windowFlags),
vmin(vmin),
vmax(vmax),
_getValue(std::move(_getValue)),
_setValue(std::move(_setValue)),
longPressData({})
{
if (values) {
uint8_t len = values[0];
const char* value = &values[1];
for (int i = vmin; i <= vmax; i++) {
this->values.emplace_back(
std::string(value, min<uint8_t>(len, strlen(value))));
value += len;
}
}
lv_event_send(lvobj, LV_EVENT_VALUE_CHANGED, nullptr);
}
void Choice::addValue(const char* value)
{
values.emplace_back(value);
vmax += 1;
}
void Choice::addValues(const char* const values[], uint8_t count)
{
this->values.reserve(this->values.size() + count);
for (uint8_t i = 0; i < count; i++) this->values.emplace_back(values[i]);
vmax += count;
}
void Choice::setValues(std::vector<std::string> values)
{
this->values = std::move(values);
@@ -237,11 +232,7 @@ void Choice::openMenu()
setEditMode(true); // this needs to be done first before menu is created.
auto menu = new Menu(this);
if (!menuTitle.empty()) menu->setTitle(menuTitle);
if (beforeDisplayMenuHandler) {
beforeDisplayMenuHandler(menu);
}
if (menuTitle) menu->setTitle(menuTitle);
fillMenu(menu);
+9 -35
View File
@@ -43,8 +43,7 @@ class ChoiceBase : public FormField
{
public:
ChoiceBase(Window *parent, const rect_t &rect,
ChoiceType type = CHOICE_TYPE_DROPOWN,
WindowFlags windowFlags = 0);
ChoiceType type = CHOICE_TYPE_DROPOWN);
protected:
ChoiceType type;
@@ -60,24 +59,18 @@ class Choice : public ChoiceBase
Choice(Window *parent, const rect_t &rect, int vmin, int vmax,
std::function<int()> _getValue,
std::function<void(int)> _setValue = nullptr,
WindowFlags windowFlags = 0);
const char *title = nullptr);
Choice(Window *parent, const rect_t &rect, std::vector<std::string> values,
int vmin, int vmax, std::function<int()> _getValue,
std::function<void(int)> _setValue = nullptr,
WindowFlags windowFlags = 0);
const char *title = nullptr);
Choice(Window *parent, const rect_t &rect, const char *const values[],
int vmin, int vmax, std::function<int()> _getValue,
std::function<void(int)> _setValue = nullptr,
WindowFlags windowFlags = 0);
Choice(Window *parent, const rect_t &rect, const char *values, int vmin,
int vmax, std::function<int()> _getValue,
std::function<void(int)> _setValue = nullptr,
WindowFlags windowFlags = 0);
const char *title = nullptr);
void addValue(const char *value);
void addValues(const char *const values[], uint8_t count);
void setValues(std::vector<std::string> values);
void setValues(const char *const values[]);
@@ -88,16 +81,11 @@ class Choice : public ChoiceBase
void onClicked() override;
void setFillMenuHandler(std::function<void(Menu*, int, int&)> handler)
void setFillMenuHandler(std::function<void(Menu *, int, int &)> handler)
{
fillMenuHandler = std::move(handler);
}
void setBeforeDisplayMenuHandler(std::function<void(Menu *)> handler)
{
beforeDisplayMenuHandler = std::move(handler);
}
void setSetValueHandler(std::function<void(int)> handler)
{
_setValue = std::move(handler);
@@ -148,28 +136,15 @@ class Choice : public ChoiceBase
virtual void setValue(int val);
virtual int getIntValue() const { return _getValue(); }
unsigned getValuesCount() const { return getIndexFromValue(vmax + 1); }
void setTextHandler(std::function<std::string(int)> handler)
{
textHandler = std::move(handler);
lv_event_send(lvobj, LV_EVENT_VALUE_CHANGED, nullptr);
}
void setMenuTitle(std::string value) { menuTitle = std::move(value); }
std::string getMenuTitle() const { return menuTitle; }
void setMin(int value) { vmin = value; }
void setMin(int value)
{
vmin = value;
invalidate();
}
void setMax(int value)
{
vmax = value;
invalidate();
}
void setMax(int value) { vmax = value; }
int getMin() const { return vmin; }
int getMax() const { return vmax; }
@@ -187,13 +162,12 @@ class Choice : public ChoiceBase
std::vector<std::string> values;
int vmin = 0;
int vmax = 0;
std::string menuTitle;
const char *menuTitle = nullptr;
std::function<int()> _getValue;
std::function<void(int)> _setValue;
std::function<bool(int)> isValueAvailable;
std::function<std::string(int)> textHandler;
std::function<void(Menu *, int, int&)> fillMenuHandler;
std::function<void(Menu *)> beforeDisplayMenuHandler;
std::function<void(Menu *, int, int &)> fillMenuHandler;
typedef std::function<bool(int16_t)> FilterFct;
void fillMenu(Menu *menu, const FilterFct &filter = nullptr);
+119 -63
View File
@@ -17,85 +17,69 @@
*/
#include "dialog.h"
#include "mainwindow.h"
#include "theme.h"
#include "progress.h"
#include "theme.h"
#include "themes/etx_lv_theme.h"
DialogWindowContent::DialogWindowContent(Dialog* parent, const rect_t& rect) :
ModalWindowContent(parent, rect),
form(this, rect_t{})
{
form.setFlexLayout();
lv_obj_set_style_max_height(form.getLvObj(), LCD_H * 0.8, 0);
lv_obj_set_style_pad_all(form.getLvObj(), lv_dpx(8), 0);
lv_obj_set_scrollbar_mode(form.getLvObj(), LV_SCROLLBAR_MODE_AUTO);
}
//-----------------------------------------------------------------------------
void DialogWindowContent::setTitle(const std::string& text)
class BaseDialogForm : public Window
{
ModalWindowContent::setTitle(text);
if (title) {
lv_coord_t title_h = lv_obj_get_height(title);
lv_obj_set_y(form.getLvObj(), title_h);
public:
BaseDialogForm(Window* parent, lv_coord_t width) : Window(parent, rect_t{})
{
etx_scrollbar(lvobj);
padAll(PAD_MEDIUM);
setFlexLayout(LV_FLEX_FLOW_COLUMN, PAD_MEDIUM, width, LV_SIZE_CONTENT);
}
protected:
void onClicked() override { Keyboard::hide(false); }
};
BaseDialog::BaseDialog(Window* parent, const char* title,
bool closeIfClickedOutside, lv_coord_t width,
lv_coord_t maxHeight) :
ModalWindow(parent, closeIfClickedOutside)
{
auto content = new Window(this, rect_t{});
content->setWindowFlag(OPAQUE);
content->padAll(PAD_ZERO);
content->setFlexLayout(LV_FLEX_FLOW_COLUMN, PAD_ZERO, width, LV_SIZE_CONTENT);
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);
etx_solid_bg(header->getLvObj(), COLOR_THEME_SECONDARY1_INDEX);
header->padAll(PAD_SMALL);
header->show(title != nullptr);
form = new BaseDialogForm(content, width);
if (maxHeight != LV_SIZE_CONTENT)
lv_obj_set_style_max_height(form->getLvObj(), maxHeight - 32, LV_PART_MAIN);
}
void DialogWindowContent::deleteLater(bool detach, bool trash)
void BaseDialog::setTitle(const char* title)
{
if (_deleted) return;
form.deleteLater(true, false);
ModalWindowContent::deleteLater(detach, trash);
}
void DialogWindowContent::updateSize()
{
lv_obj_center(lvobj);
ModalWindowContent::updateSize();
}
#if defined(DEBUG_WINDOWS)
std::string DialogWindowContent::getName() const
{
return "DialogWindowContent";
}
#endif
Dialog::Dialog(Window* parent, std::string title, const rect_t& rect) :
ModalWindow(parent), content(new DialogWindowContent(this, rect))
{
bringToTop();
if (!title.empty()) content->setTitle(std::move(title));
}
void Dialog::onCancel()
{
deleteLater();
}
void Dialog::onEvent(event_t event)
{
// block key events
(void)event;
header->setText(title);
}
//-----------------------------------------------------------------------------
ProgressDialog::ProgressDialog(Window *parent, std::string title,
ProgressDialog::ProgressDialog(Window* parent, const char* title,
std::function<void()> onClose) :
Dialog(parent, title, rect_t{}),
progress(new Progress(&content->form, rect_t{})),
onClose(std::move(onClose))
BaseDialog(parent, title, false), onClose(std::move(onClose))
{
progress->setHeight(LV_DPI_DEF / 4);
progress = new Progress(form, rect_t{0, 0, LV_PCT(100), 32});
updateProgress(0);
}
content->setWidth(LCD_W * 0.8);
content->updateSize();
auto content_w = lv_obj_get_content_width(content->form.getLvObj());
progress->setWidth(content_w);
// disable canceling dialog
setCloseWhenClickOutside(false);
void ProgressDialog::setTitle(std::string title)
{
header->setText(title);
header->show();
}
void ProgressDialog::updateProgress(int percentage)
@@ -109,3 +93,75 @@ void ProgressDialog::closeDialog()
deleteLater();
onClose();
}
//-----------------------------------------------------------------------------
MessageDialog::MessageDialog(Window* parent, const char* title,
const char* message, const char* info,
LcdFlags messageFlags, LcdFlags infoFlags) :
BaseDialog(parent, title, true)
{
messageWidget = new StaticText(form, {0, 0, LV_PCT(100), LV_SIZE_CONTENT},
message, messageFlags);
if (info) {
infoWidget = new StaticText(form, {0, 0, LV_PCT(100), LV_SIZE_CONTENT},
info, infoFlags);
}
}
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) :
BaseDialog(parent, title, true)
{
messageWidget = new StaticText(form, {0, 0, LV_PCT(100), LV_SIZE_CONTENT},
message, CENTERED | COLOR_THEME_PRIMARY1);
infoWidget = new DynamicText(form, {0, 0, LV_PCT(100), LV_SIZE_CONTENT},
textHandler, textFlags);
}
void DynamicMessageDialog::onClicked() { deleteLater(); }
//-----------------------------------------------------------------------------
ConfirmDialog::ConfirmDialog(Window* parent, const char* title,
const char* message,
std::function<void(void)> confirmHandler,
std::function<void(void)> cancelHandler) :
BaseDialog(parent, title, false),
confirmHandler(std::move(confirmHandler)),
cancelHandler(std::move(cancelHandler))
{
if (message) {
new StaticText(form, {0, 0, LV_PCT(100), 0}, message, CENTERED);
}
auto box = new Window(form, rect_t{});
box->padAll(PAD_TINY);
box->setFlexLayout(LV_FLEX_FLOW_ROW, 40, LV_PCT(100), LV_SIZE_CONTENT);
lv_obj_set_flex_align(box->getLvObj(), LV_FLEX_ALIGN_CENTER,
LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_SPACE_BETWEEN);
new TextButton(box, rect_t{0, 0, 96, 0}, STR_NO, [=]() -> int8_t {
onCancel();
return 0;
});
new TextButton(box, rect_t{0, 0, 96, 0}, STR_YES, [=]() -> int8_t {
this->deleteLater();
this->confirmHandler();
return 0;
});
}
void ConfirmDialog::onCancel()
{
deleteLater();
if (cancelHandler) cancelHandler();
}
+94 -43
View File
@@ -18,61 +18,112 @@
#pragma once
#include "modal_window.h"
#include "form.h"
#include "modal_window.h"
class Dialog;
class DialogWindowContent : public ModalWindowContent
{
friend class Dialog;
public:
DialogWindowContent(Dialog* parent, const rect_t& rect);
void setTitle(const std::string& text) override;
void deleteLater(bool detach = true, bool trash = true) override;
void updateSize() override;
#if defined(DEBUG_WINDOWS)
std::string getName() const override;
#endif
public:
FormWindow form;
};
class Dialog : public ModalWindow
{
public:
Dialog(Window* parent, std::string title, const rect_t& rect);
protected:
DialogWindowContent* content;
void onCancel() override;
void onEvent(event_t event) override;
};
class StaticText;
class DynamicText;
class Progress;
class ProgressDialog : public Dialog
#define DIALOG_DEFAULT_WIDTH ((coord_t)(LCD_W * 0.8))
#define DIALOG_DEFAULT_HEIGHT ((coord_t)(LCD_H * 0.8))
//-----------------------------------------------------------------------------
class BaseDialog : public ModalWindow
{
uint32_t lastUpdate = 0;
Progress *progress;
std::function<void()> onClose;
public:
ProgressDialog(Window *parent, std::string title,
BaseDialog(Window* parent, const char* title, bool closeIfClickedOutside,
lv_coord_t width = DIALOG_DEFAULT_WIDTH,
lv_coord_t maxHeight = DIALOG_DEFAULT_HEIGHT);
void setTitle(const char* title);
protected:
Window* form = nullptr;
StaticText* header = nullptr;
void onCancel() override { deleteLater(); }
void onEvent(event_t event) override {}
};
//-----------------------------------------------------------------------------
class ProgressDialog : public BaseDialog
{
public:
ProgressDialog(Window* parent, const char* title,
std::function<void()> onClose);
void updateProgress(int percentage);
void setTitle(std::string title) { content->setTitle(title); }
void setTitle(std::string title);
void closeDialog();
protected:
uint32_t lastUpdate = 0;
Progress* progress;
std::function<void()> onClose;
// disable keys
void onEvent(event_t) override {}
};
//-----------------------------------------------------------------------------
class MessageDialog : public BaseDialog
{
public:
MessageDialog(Window* parent, const char* title, const char* message,
const char* info = nullptr, LcdFlags messageFlags = CENTERED,
LcdFlags infoFlags = CENTERED);
protected:
StaticText* messageWidget;
StaticText* infoWidget;
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "MessageDialog"; }
#endif
void onClicked() override;
};
//-----------------------------------------------------------------------------
class DynamicMessageDialog : public BaseDialog
{
public:
DynamicMessageDialog(Window* parent, const char* title,
std::function<std::string()> textHandler,
const char* message = "",
const int lineHeight = PAGE_LINE_HEIGHT,
const LcdFlags textFlags = CENTERED);
// Attn.: FONT(XXL) is not supported by DynamicMessageDialog
protected:
StaticText* messageWidget;
DynamicText* infoWidget;
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "DynamicMessageDialog"; }
#endif
void onClicked() override;
};
//-----------------------------------------------------------------------------
class ConfirmDialog : public BaseDialog
{
public:
ConfirmDialog(Window* parent, const char* title, const char* message,
std::function<void(void)> confirmHandler,
std::function<void(void)> cancelHandler = nullptr);
protected:
std::function<void(void)> confirmHandler;
std::function<void(void)> cancelHandler;
void onCancel() override;
};
-14
View File
@@ -23,15 +23,6 @@
#include "libopenui_file.h"
#include "menu.h"
#include "theme.h"
#include "message_dialog.h"
#if !defined(STR_SDCARD)
#define STR_SDCARD "SD"
#endif
#if !defined(STR_NO_FILES_ON_SD)
#define STR_NO_FILES_ON_SD "No files on SD Card!"
#endif
FileChoice::FileChoice(Window *parent, const rect_t &rect, std::string folder,
const char *extension, int maxlen,
@@ -116,11 +107,6 @@ bool FileChoice::openMenu()
menu->select(current);
}
menu->setCloseHandler([=]() {
editMode = false;
// setFocus(SET_FOCUS_DEFAULT);
});
return true;
}
}
-86
View File
@@ -1,86 +0,0 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* libopenui - https://github.com/opentx/libopenui
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include <lvgl/lvgl.h>
class FlexGridLayout
{
const lv_coord_t* col_dsc = nullptr;
const lv_coord_t* row_dsc = nullptr;
lv_coord_t padding = 0;
uint8_t col_pos = 0;
uint8_t col_span = 1;
uint8_t row_pos = 0;
uint8_t row_span = 1;
public:
FlexGridLayout(const lv_coord_t col_dsc[], const lv_coord_t row_dsc[],
lv_coord_t padding = 4) :
col_dsc(col_dsc), row_dsc(row_dsc), padding(padding)
{
}
FlexGridLayout(const FlexGridLayout& g) :
col_dsc(g.col_dsc), row_dsc(g.row_dsc), padding(g.padding)
{
}
void apply(Window* w) { apply(w->getLvObj()); }
void apply(lv_obj_t* obj) {
// padding
lv_obj_set_style_pad_all(obj, padding, LV_PART_MAIN);
lv_obj_set_style_pad_row(obj, padding, LV_PART_MAIN);
lv_obj_set_style_pad_column(obj, padding, LV_PART_MAIN);
// layout
lv_obj_set_layout(obj, LV_LAYOUT_GRID);
if (col_dsc && row_dsc) {
lv_obj_set_grid_dsc_array(obj, col_dsc, row_dsc);
}
}
void add(Window* w) { add(w->getLvObj()); }
void add(lv_obj_t* obj) {
if (col_dsc && row_dsc) {
lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_START, col_pos, col_span,
LV_GRID_ALIGN_CENTER, row_pos, row_span);
}
}
void resetPos() { col_pos = 0; row_pos = 0; }
void nextColumn() { col_pos += col_span; }
void nextRow() { row_pos += row_span; col_pos = 0; }
void nextCell()
{
nextColumn();
if (col_dsc[col_pos] == LV_GRID_TEMPLATE_LAST) {
nextRow();
}
}
void setColSpan(uint8_t span) { col_span = span; }
void setRowSpan(uint8_t span) { row_span = span; }
};
-28
View File
@@ -1,28 +0,0 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* libopenui - https://github.com/opentx/libopenui
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include <lvgl/lvgl.h>
#include "opentx_types.h"
const lv_font_t* getFont(LcdFlags flags);
uint8_t getFontHeight(LcdFlags flags);
uint8_t getFontHeightCondensed(LcdFlags flags);
int getTextWidth(const char * s, int len = 0, LcdFlags flags = 0);
+46 -81
View File
@@ -17,13 +17,45 @@
*/
#include "form.h"
#include "bitmapbuffer.h"
FormField::FormField(Window* parent, const rect_t& rect,
WindowFlags windowFlags, LcdFlags textFlags,
LvglCreate objConstruct) :
Window(parent, rect, windowFlags, textFlags, objConstruct)
#include "themes/etx_lv_theme.h"
void FlexGridLayout::apply(Window* w)
{
w->padAll(padding);
// layout
lv_obj_set_layout(w->getLvObj(), LV_LAYOUT_GRID);
if (col_dsc && row_dsc) {
lv_obj_set_grid_dsc_array(w->getLvObj(), col_dsc, row_dsc);
}
}
void FlexGridLayout::add(Window* w)
{
if (col_dsc && row_dsc) {
lv_obj_set_grid_cell(w->getLvObj(), LV_GRID_ALIGN_START, col_pos, col_span,
LV_GRID_ALIGN_CENTER, row_pos, row_span);
}
}
FormField::FormField(const rect_t& rect, LcdFlags textFlags) :
Window(rect)
{
setTextFlag(textFlags);
}
FormField::FormField(Window* parent, const rect_t& rect, LcdFlags textFlags,
LvglCreate objConstruct) :
Window(parent, rect, objConstruct)
{
setTextFlag(textFlags);
lv_obj_add_flag(lvobj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
}
void FormField::setupLVGL()
{
Window::setupLVGL();
lv_obj_add_flag(lvobj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
}
@@ -35,30 +67,13 @@ void FormField::setEditMode(bool newEditMode)
lv_group_t* grp = (lv_group_t*)lv_obj_get_group(lvobj);
if (grp != nullptr) lv_group_set_editing(grp, editMode);
}
if (editMode && lvobj != nullptr) {
lv_obj_add_state(lvobj, LV_STATE_EDITED);
} else if (lvobj != nullptr) {
lv_obj_clear_state(lvobj, LV_STATE_EDITED);
}
invalidate();
}
void FormField::enable(bool value)
{
if (value) {
lv_obj_clear_state(lvobj, LV_STATE_DISABLED);
} else {
lv_obj_add_state(lvobj, LV_STATE_DISABLED);
}
}
void FormField::onClicked()
{
lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
if(indev_type != LV_INDEV_TYPE_POINTER) {
if (indev_type != LV_INDEV_TYPE_POINTER) {
setEditMode(!editMode);
invalidate();
}
}
@@ -66,7 +81,6 @@ void FormField::onCancel()
{
if (isEditMode()) {
setEditMode(false);
invalidate();
} else {
Window::onCancel();
}
@@ -74,74 +88,25 @@ void FormField::onCancel()
void FormField::deleteLater(bool detach, bool trash)
{
if (isEditMode())
setEditMode(false);
if (isEditMode()) setEditMode(false);
Window::deleteLater(detach, trash);
}
FormWindow::Line::Line(Window* parent, lv_obj_t* obj, FlexGridLayout* layout) :
Window(parent, obj), layout(layout)
{
construct();
}
FormWindow::Line::Line(Window* parent, FlexGridLayout* layout) :
FormLine::FormLine(Window* parent, FlexGridLayout& layout) :
Window(parent, rect_t{}), layout(layout)
{
construct();
}
setWindowFlag(NO_FOCUS);
void FormWindow::Line::construct()
{
// Focus
windowFlags |= NO_FOCUS;
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
if (layout) {
layout->apply(lvobj);
}
layout.resetPos();
layout.apply(this);
lv_obj_set_width(lvobj, lv_pct(100));
lv_obj_set_height(lvobj, LV_SIZE_CONTENT);
}
void FormWindow::Line::setLayout(FlexGridLayout *l)
{
layout = l;
if (layout) layout->apply(lvobj);
}
void FormWindow::Line::addChild(Window* window)
void FormLine::addChild(Window* window)
{
Window::addChild(window);
if (layout) {
layout->add(window->getLvObj());
layout->nextCell();
}
}
FormWindow::FormWindow(Window* parent, const rect_t& rect,
WindowFlags windowflags) :
Window(parent, rect, windowflags, 0, etx_form_window_create)
{
}
void FormWindow::setFlexLayout(lv_flex_flow_t flow, lv_coord_t padding)
{
lv_obj_set_flex_flow(lvobj, flow);
if (_LV_FLEX_COLUMN & flow) {
lv_obj_set_style_pad_row(lvobj, padding, LV_PART_MAIN);
} else {
lv_obj_set_style_pad_column(lvobj, padding, LV_PART_MAIN);
}
lv_obj_set_width(lvobj, lv_pct(100));
lv_obj_set_height(lvobj, LV_SIZE_CONTENT);
}
FormWindow::Line* FormWindow::newLine(FlexGridLayout* layout, lv_coord_t left_padding)
{
if (layout) layout->resetPos();
auto lv_line = window_create(lvobj);
if (left_padding) lv_obj_set_style_pad_left(lv_line, left_padding, 0);
return new Line(this, lv_line, layout);
layout.add(window);
layout.nextCell();
}
+84 -46
View File
@@ -18,65 +18,103 @@
#pragma once
#include "themes/etx_lv_theme.h"
#include "window.h"
#include "flexlayout.h"
class FlexGridLayout
{
public:
FlexGridLayout(const lv_coord_t col_dsc[], const lv_coord_t row_dsc[],
PaddingSize padding = PAD_SMALL) :
col_dsc(col_dsc), row_dsc(row_dsc), padding(padding)
{
}
FlexGridLayout(const FlexGridLayout& g) :
col_dsc(g.col_dsc), row_dsc(g.row_dsc), padding(g.padding)
{
}
void apply(Window* w);
void add(Window* w);
void resetPos()
{
col_pos = 0;
row_pos = 0;
}
void nextColumn() { col_pos += col_span; }
void nextRow()
{
row_pos += row_span;
col_pos = 0;
}
void nextCell()
{
nextColumn();
if (col_dsc[col_pos] == LV_GRID_TEMPLATE_LAST) {
nextRow();
}
}
void setColSpan(uint8_t span) { col_span = span; }
void setRowSpan(uint8_t span) { row_span = span; }
protected:
const lv_coord_t* col_dsc = nullptr;
const lv_coord_t* row_dsc = nullptr;
PaddingSize padding = PAD_ZERO;
uint8_t col_pos = 0;
uint8_t col_span = 1;
uint8_t row_pos = 0;
uint8_t row_span = 1;
};
class FormField : public Window
{
public:
FormField(Window *parent, const rect_t &rect, WindowFlags windowFlags = 0,
LcdFlags textFlags = 0, LvglCreate objConstruct = nullptr);
public:
FormField(const rect_t& rect, LcdFlags textFlags);
FormField(Window* parent, const rect_t& rect, LcdFlags textFlags = 0,
LvglCreate objConstruct = nullptr);
virtual void changeEnd(bool forceChanged = false)
{
if (changeHandler) { changeHandler(); }
void setupLVGL() override;
virtual void changeEnd(bool forceChanged = false)
{
if (changeHandler) {
changeHandler();
}
}
void setChangeHandler(std::function<void()> handler)
{
changeHandler = std::move(handler);
}
void setChangeHandler(std::function<void()> handler)
{
changeHandler = std::move(handler);
}
inline bool isEditMode() const { return editMode; }
virtual void setEditMode(bool newEditMode);
inline bool isEditMode() const { return editMode; }
virtual void setEditMode(bool newEditMode);
void enable(bool value = true);
void disable() { enable(false); }
void onClicked() override;
void onCancel() override;
void deleteLater(bool detach = true, bool trash = true) override;
void onClicked() override;
void onCancel() override;
void deleteLater(bool detach = true, bool trash = true) override;
protected:
bool editMode = false;
bool enabled = true;
std::function<void()> changeHandler = nullptr;
protected:
bool editMode = false;
bool enabled = true;
std::function<void()> changeHandler = nullptr;
};
class FormWindow : public Window
class FormLine : public Window
{
public:
class Line : public Window
{
public:
Line(Window *parent, lv_obj_t *obj, FlexGridLayout *layout = nullptr);
Line(Window *parent, FlexGridLayout *layout = nullptr);
public:
FormLine(Window* parent, FlexGridLayout& layout);
void setLayout(FlexGridLayout *l);
protected:
FlexGridLayout& layout;
protected:
FlexGridLayout *layout;
void addChild(Window *window) override;
void construct();
};
FormWindow(Window *parent, const rect_t &rect, WindowFlags windowflags = 0);
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "FormWindow"; }
#endif
void setFlexLayout(lv_flex_flow_t flow = LV_FLEX_FLOW_COLUMN, lv_coord_t padding = 0);
Line* newLine(FlexGridLayout* layout = nullptr, lv_coord_t left_padding = 0);
void addChild(Window* window) override;
};
+39 -12
View File
@@ -20,10 +20,41 @@
#include "form.h"
#include "mainwindow.h"
#include "themes/etx_lv_theme.h"
Keyboard * Keyboard::activeKeyboard = nullptr;
static void keyboard_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj)
{
etx_solid_bg(obj, COLOR_THEME_SECONDARY3_INDEX);
etx_obj_add_style(obj, styles->pad_tiny, LV_PART_MAIN);
etx_obj_add_style(obj, styles->rounded, LV_PART_MAIN);
static void keyboard_event_cb(lv_event_t * e)
etx_std_style(obj, LV_PART_ITEMS, PAD_SMALL);
etx_txt_color(obj, COLOR_THEME_PRIMARY1_INDEX, LV_PART_ITEMS);
etx_txt_color(obj, COLOR_THEME_PRIMARY2_INDEX, LV_PART_ITEMS | LV_STATE_FOCUSED);
etx_bg_color(obj, COLOR_THEME_FOCUS_INDEX, LV_PART_ITEMS | LV_STATE_EDITED);
}
static const lv_obj_class_t keyboard_class = {
.base_class = &lv_keyboard_class,
.constructor_cb = keyboard_constructor,
.destructor_cb = nullptr,
.user_data = nullptr,
.event_cb = nullptr,
.width_def = 0,
.height_def = 0,
.editable = LV_OBJ_CLASS_EDITABLE_INHERIT,
.group_def = LV_OBJ_CLASS_GROUP_DEF_INHERIT,
.instance_size = sizeof(lv_keyboard_t),
};
static lv_obj_t* keyboard_create(lv_obj_t* parent)
{
return etx_create(&keyboard_class, parent);
}
Keyboard* Keyboard::activeKeyboard = nullptr;
static void keyboard_event_cb(lv_event_t* e)
{
auto code = lv_event_get_code(e);
if (code == LV_EVENT_READY) {
@@ -31,7 +62,7 @@ static void keyboard_event_cb(lv_event_t * e)
} else if (code == LV_EVENT_CANCEL) {
Keyboard::hide(true);
} else if (code == LV_EVENT_KEY) {
int32_t c = *((int32_t *)lv_event_get_param(e));
int32_t c = *((int32_t*)lv_event_get_param(e));
if (c == LV_KEY_ESC) {
Keyboard::hide(false);
}
@@ -50,8 +81,8 @@ static void _assign_lv_group(lv_group_t* g)
}
}
Keyboard::Keyboard(coord_t height) :
Window(MainWindow::instance(), {0, LCD_H - height, LCD_W, height}, OPAQUE)
Keyboard::Keyboard(coord_t height) :
NavWindow(MainWindow::instance(), {0, LCD_H - height, LCD_W, height})
{
lv_obj_set_parent(lvobj, lv_layer_top()); // the keyboard is always on top
@@ -62,7 +93,7 @@ Keyboard::Keyboard(coord_t height) :
auto old_g = lv_group_get_default();
lv_group_set_default(group);
keyboard = etx_keyboard_create(lvobj);
keyboard = keyboard_create(lvobj);
lv_group_set_default(old_g);
lv_obj_add_event_cb(keyboard, keyboard_event_cb, LV_EVENT_ALL, this);
@@ -136,15 +167,12 @@ bool Keyboard::attachKeyboard()
void Keyboard::setField(FormField* newField)
{
if (!attachKeyboard())
return;
if (!attachKeyboard()) return;
lv_obj_t* obj = newField->getLvObj();
if (obj) {
fieldContainer = newField->getFullScreenWindow();
if (fieldContainer) {
attach(fieldContainer);
lv_area_t coords;
@@ -157,13 +185,12 @@ void Keyboard::setField(FormField* newField)
scroll_pos = lv_obj_get_scroll_y(fieldContainer->getLvObj());
lv_obj_scroll_to_view(lvobj, LV_ANIM_OFF);
invalidate();
newField->setEditMode(true);
lv_keyboard_set_textarea(keyboard, obj);
lv_obj_add_event_cb(obj, field_focus_leave, LV_EVENT_DEFOCUSED, nullptr);
_assign_lv_group(group);
field = newField;
fieldGroup = (lv_group_t*)lv_obj_get_group(obj);
}
+1 -1
View File
@@ -22,7 +22,7 @@
class FormField;
class Keyboard : public Window
class Keyboard : public NavWindow
{
public:
explicit Keyboard(coord_t height);
+89 -126
View File
@@ -36,7 +36,7 @@ static const lv_btnmatrix_ctrl_t number_kb_ctrl_map[] = {
static void on_key(lv_event_t* e)
{
lv_obj_t* obj = lv_event_get_target(e);
NumberEdit* edit = (NumberEdit*)lv_event_get_user_data(e);
NumberKeyboard* edit = (NumberKeyboard*)lv_event_get_user_data(e);
if (!obj || !edit) return;
uint16_t btn_id = lv_btnmatrix_get_selected_btn(obj);
@@ -46,139 +46,102 @@ static void on_key(lv_event_t* e)
lv_btnmatrix_get_btn_text(obj, lv_btnmatrix_get_selected_btn(obj));
if (txt == NULL) return;
if (strcmp(txt, "<<") == 0) {
edit->onEvent(EVT_VIRTUAL_KEY_BACKWARD);
} else if (strcmp(txt, "-") == 0) {
edit->onEvent(EVT_VIRTUAL_KEY_MINUS);
} else if (strcmp(txt, "+") == 0) {
edit->onEvent(EVT_VIRTUAL_KEY_PLUS);
} else if (strcmp(txt, ">>") == 0) {
edit->onEvent(EVT_VIRTUAL_KEY_FORWARD);
} else if (strcmp(txt, "MIN") == 0) {
edit->onEvent(EVT_VIRTUAL_KEY_MIN);
} else if (strcmp(txt, "DEF") == 0) {
edit->onEvent(EVT_VIRTUAL_KEY_DEFAULT);
} else if (strcmp(txt, "MAX") == 0) {
edit->onEvent(EVT_VIRTUAL_KEY_MAX);
} else if (strcmp(txt, "+/-") == 0) {
edit->onEvent(EVT_VIRTUAL_KEY_SIGN);
}
edit->handleEvent(txt);
}
void NumberKeyboard::handleEvent(const char* btn)
{
if (strcmp(btn, "<<") == 0)
decLarge();
else if (strcmp(btn, "-") == 0)
decSmall();
else if (strcmp(btn, "+") == 0)
incSmall();
else if (strcmp(btn, ">>") == 0)
incLarge();
else if (strcmp(btn, "MIN") == 0)
setMIN();
else if (strcmp(btn, "DEF") == 0)
setDEF();
else if (strcmp(btn, "MAX") == 0)
setMAX();
else if (strcmp(btn, "+/-") == 0)
changeSign();
}
void NumberKeyboard::decLarge()
{
((NumberEdit*)field)->onEvent(EVT_VIRTUAL_KEY_BACKWARD);
}
void NumberKeyboard::decSmall()
{
((NumberEdit*)field)->onEvent(EVT_VIRTUAL_KEY_MINUS);
}
void NumberKeyboard::incSmall()
{
((NumberEdit*)field)->onEvent(EVT_VIRTUAL_KEY_PLUS);
}
void NumberKeyboard::incLarge()
{
((NumberEdit*)field)->onEvent(EVT_VIRTUAL_KEY_FORWARD);
}
void NumberKeyboard::setMIN()
{
((NumberEdit*)field)->onEvent(EVT_VIRTUAL_KEY_MIN);
}
void NumberKeyboard::setMAX()
{
((NumberEdit*)field)->onEvent(EVT_VIRTUAL_KEY_MAX);
}
void NumberKeyboard::setDEF()
{
((NumberEdit*)field)->onEvent(EVT_VIRTUAL_KEY_DEFAULT);
}
void NumberKeyboard::changeSign()
{
((NumberEdit*)field)->onEvent(EVT_VIRTUAL_KEY_SIGN);
}
#if defined(HARDWARE_KEYS)
void NumberKeyboard::onEvent(event_t event)
{
NumberEdit* edit = (NumberEdit*)field;
#if (defined(KEYS_GPIO_REG_PAGEUP) || defined(USE_HATS_AS_KEYS)) && !defined(PCBX12S)
// Radios with both PGUP and PGDN buttons except X12S
switch (event) {
case EVT_KEY_BREAK(KEY_SYS):
// "<<"
edit->onEvent(EVT_VIRTUAL_KEY_BACKWARD);
break;
case EVT_KEY_LONG(KEY_SYS):
killEvents(event);
// "MIN"
edit->onEvent(EVT_VIRTUAL_KEY_MIN);
break;
case EVT_KEY_BREAK(KEY_MODEL):
// ">>"
edit->onEvent(EVT_VIRTUAL_KEY_FORWARD);
break;
case EVT_KEY_LONG(KEY_MODEL):
killEvents(event);
// "MAX"
edit->onEvent(EVT_VIRTUAL_KEY_MAX);
break;
case EVT_KEY_BREAK(KEY_PAGEDN):
// "+"
edit->onEvent(EVT_VIRTUAL_KEY_PLUS);
break;
case EVT_KEY_BREAK(KEY_PAGEUP):
// "-"
edit->onEvent(EVT_VIRTUAL_KEY_MINUS);
break;
case EVT_KEY_BREAK(KEY_TELE):
// "+/-"
edit->onEvent(EVT_VIRTUAL_KEY_SIGN);
break;
case EVT_KEY_LONG(KEY_TELE):
killEvents(event);
// "DEF"
edit->onEvent(EVT_VIRTUAL_KEY_DEFAULT);
break;
default:
break;
}
#if (defined(KEYS_GPIO_REG_PAGEUP) || defined(USE_HATS_AS_KEYS)) && \
!defined(PCBX12S)
// Radios with both PGUP and PGDN buttons except X12S
void NumberKeyboard::onPressSYS() { decLarge(); }
void NumberKeyboard::onLongPressSYS() { setMIN(); }
void NumberKeyboard::onPressMDL() { incLarge(); }
void NumberKeyboard::onLongPressMDL() { setMAX(); }
void NumberKeyboard::onPressTELE() { changeSign(); }
void NumberKeyboard::onLongPressTELE() { setDEF(); }
void NumberKeyboard::onPressPGUP() { decSmall(); }
void NumberKeyboard::onPressPGDN() { incSmall(); }
void NumberKeyboard::onLongPressPGUP() {}
void NumberKeyboard::onLongPressPGDN() {}
#else
// Radios witb only a single PGUP/DN button or X12S
switch (event) {
case EVT_KEY_BREAK(KEY_SYS):
// "-"
edit->onEvent(EVT_VIRTUAL_KEY_MINUS);
break;
case EVT_KEY_LONG(KEY_SYS):
killEvents(event);
// "MIN"
edit->onEvent(EVT_VIRTUAL_KEY_MIN);
break;
case EVT_KEY_BREAK(KEY_MODEL):
// ">>"
edit->onEvent(EVT_VIRTUAL_KEY_FORWARD);
break;
case EVT_KEY_LONG(KEY_MODEL):
killEvents(event);
// "+/-"
edit->onEvent(EVT_VIRTUAL_KEY_SIGN);
break;
// Radios witb only a single PGUP/DN button or X12S
void NumberKeyboard::onPressSYS() { decSmall(); }
void NumberKeyboard::onLongPressSYS() { setMIN(); }
void NumberKeyboard::onPressMDL() { incLarge(); }
void NumberKeyboard::onLongPressMDL() { changeSign(); }
void NumberKeyboard::onPressTELE() { incSmall(); }
void NumberKeyboard::onLongPressTELE() { setMAX(); }
#if defined(PCBX12S)
case EVT_KEY_BREAK(KEY_PAGEUP):
void NumberKeyboard::onPressPGUP() { decLarge(); }
#else
void NumberKeyboard::onPressPGUP() { setDEF(); }
#endif
case EVT_KEY_BREAK(KEY_PAGEDN):
// "<<"
edit->onEvent(EVT_VIRTUAL_KEY_BACKWARD);
break;
#if defined(PCBX12S)
case EVT_KEY_LONG(KEY_PAGEUP):
void NumberKeyboard::onPressPGDN() { decLarge(); }
void NumberKeyboard::onLongPressPGUP() { setDEF(); }
void NumberKeyboard::onLongPressPGDN() { setDEF(); }
#endif
case EVT_KEY_LONG(KEY_PAGEDN):
killEvents(event);
// "DEF"
edit->onEvent(EVT_VIRTUAL_KEY_DEFAULT);
break;
case EVT_KEY_BREAK(KEY_TELE):
// "+"
edit->onEvent(EVT_VIRTUAL_KEY_PLUS);
break;
case EVT_KEY_LONG(KEY_TELE):
killEvents(event);
// "MAX"
edit->onEvent(EVT_VIRTUAL_KEY_MAX);
break;
default:
break;
}
#endif
}
#endif
NumberKeyboard::NumberKeyboard() : Keyboard(KEYBOARD_HEIGHT)
@@ -205,5 +168,5 @@ void NumberKeyboard::show(NumberEdit* field)
lv_keyboard_set_textarea(kb, nullptr);
lv_obj_remove_event_cb(kb, on_key);
lv_obj_add_event_cb(kb, on_key, LV_EVENT_VALUE_CHANGED, field);
lv_obj_add_event_cb(kb, on_key, LV_EVENT_VALUE_CHANGED, _instance);
}
+21 -1
View File
@@ -34,9 +34,29 @@ class NumberKeyboard : public Keyboard
static void show(NumberEdit* field);
void handleEvent(const char* btn);
protected:
void decLarge();
void decSmall();
void incSmall();
void incLarge();
void setMIN();
void setMAX();
void setDEF();
void changeSign();
#if defined(HARDWARE_KEYS)
void onEvent(event_t event) override;
void onPressSYS() override;
void onLongPressSYS() override;
void onPressMDL() override;
void onLongPressMDL() override;
void onPressTELE() override;
void onLongPressTELE() override;
void onPressPGUP() override;
void onPressPGDN() override;
void onLongPressPGUP() override;
void onLongPressPGDN() override;
#endif
static NumberKeyboard* _instance;
+94 -131
View File
@@ -18,7 +18,6 @@
#include "keyboard_text.h"
#include "font.h"
#include "textedit.h"
constexpr coord_t KEYBOARD_HEIGHT = LCD_H * 2 / 5;
@@ -29,139 +28,103 @@ TextKeyboard::TextKeyboard() : Keyboard(KEYBOARD_HEIGHT) {}
TextKeyboard::~TextKeyboard() { _instance = nullptr; }
#if defined(HARDWARE_KEYS)
void TextKeyboard::onEvent(event_t event)
void TextKeyboard::changeMode()
{
// Change keyboard mode
lv_keyboard_mode_t mode = lv_keyboard_get_mode(keyboard);
mode = (mode + 1) & 3;
lv_keyboard_set_mode(keyboard, mode);
}
void TextKeyboard::backspace()
{
lv_keyboard_t* kb = (lv_keyboard_t*)keyboard;
#if (defined(KEYS_GPIO_REG_PAGEUP) || defined(USE_HATS_AS_KEYS)) && !defined(PCBX12S)
// Radios with both PGUP and PGDN buttons except X12S
switch (event) {
case EVT_KEY_BREAK(KEY_SYS): {
// Change keyboard mode
lv_keyboard_mode_t mode = lv_keyboard_get_mode(keyboard);
mode = (mode + 1) & 3;
lv_keyboard_set_mode(keyboard, mode);
} break;
case EVT_KEY_LONG(KEY_MODEL):
killEvents(event);
// Backspace
lv_textarea_del_char(kb->ta);
break;
case EVT_KEY_BREAK(KEY_PAGEDN):
// Cursor right
lv_textarea_cursor_right(kb->ta);
break;
case EVT_KEY_BREAK(KEY_PAGEUP):
// Cursor left
lv_textarea_cursor_left(kb->ta);
break;
case EVT_KEY_LONG(KEY_PAGEDN): {
killEvents(event);
// Cursor to end
int l = strlen(lv_textarea_get_text(kb->ta));
while (lv_textarea_get_cursor_pos(kb->ta) < l)
lv_textarea_cursor_right(kb->ta);
} break;
case EVT_KEY_LONG(KEY_PAGEUP):
killEvents(event);
// Cursor to start
while (lv_textarea_get_cursor_pos(kb->ta) > 0)
lv_textarea_cursor_left(kb->ta);
break;
case EVT_KEY_BREAK(KEY_TELE): {
// Toggle case
char c = lv_textarea_get_text(kb->ta)[lv_textarea_get_cursor_pos(kb->ta)];
if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))) {
c ^= 0x20;
lv_textarea_del_char_forward(kb->ta);
lv_textarea_add_char(kb->ta, c);
lv_textarea_cursor_left(kb->ta);
}
} break;
case EVT_KEY_LONG(KEY_TELE):
killEvents(event);
// Delete
lv_textarea_del_char_forward(kb->ta);
break;
default:
break;
}
#else
// Radios witb only a single PGUP/DN button or X12S
switch (event) {
case EVT_KEY_BREAK(KEY_SYS):
// Cursor left
lv_textarea_cursor_left(kb->ta);
break;
case EVT_KEY_LONG(KEY_SYS):
killEvents(event);
// Cursor to start
while (lv_textarea_get_cursor_pos(kb->ta) > 0)
lv_textarea_cursor_left(kb->ta);
break;
case EVT_KEY_BREAK(KEY_MODEL): {
// Change keyboard mode
lv_keyboard_mode_t mode = lv_keyboard_get_mode(keyboard);
mode = (mode + 1) & 3;
lv_keyboard_set_mode(keyboard, mode);
} break;
case EVT_KEY_LONG(KEY_MODEL):
killEvents(event);
// Backspace
lv_textarea_del_char(kb->ta);
break;
#if defined(PCBX12S)
case EVT_KEY_BREAK(KEY_PAGEUP):
#endif
case EVT_KEY_BREAK(KEY_PAGEDN): {
// Toggle case
char c = lv_textarea_get_text(kb->ta)[lv_textarea_get_cursor_pos(kb->ta)];
if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))) {
c ^= 0x20;
lv_textarea_del_char_forward(kb->ta);
lv_textarea_add_char(kb->ta, c);
lv_textarea_cursor_left(kb->ta);
}
} break;
case EVT_KEY_LONG(KEY_PAGEDN):
killEvents(event);
// Delete
lv_textarea_del_char_forward(kb->ta);
break;
case EVT_KEY_BREAK(KEY_TELE): {
// Cursor right
lv_textarea_cursor_right(kb->ta);
} break;
case EVT_KEY_LONG(KEY_TELE): {
killEvents(event);
// Cursor to end
int l = strlen(lv_textarea_get_text(kb->ta));
while (lv_textarea_get_cursor_pos(kb->ta) < l)
lv_textarea_cursor_right(kb->ta);
} break;
default:
break;
}
#endif
// Backspace
lv_textarea_del_char(kb->ta);
}
void TextKeyboard::toggleCase()
{
lv_keyboard_t* kb = (lv_keyboard_t*)keyboard;
// Toggle case
char c = lv_textarea_get_text(kb->ta)[lv_textarea_get_cursor_pos(kb->ta)];
if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))) {
c ^= 0x20;
lv_textarea_del_char_forward(kb->ta);
lv_textarea_add_char(kb->ta, c);
lv_textarea_cursor_left(kb->ta);
}
}
void TextKeyboard::deleteChar()
{
lv_keyboard_t* kb = (lv_keyboard_t*)keyboard;
// Delete
lv_textarea_del_char_forward(kb->ta);
}
void TextKeyboard::cursorLeft()
{
lv_keyboard_t* kb = (lv_keyboard_t*)keyboard;
// Cursor left
lv_textarea_cursor_left(kb->ta);
}
void TextKeyboard::cursorRight()
{
lv_keyboard_t* kb = (lv_keyboard_t*)keyboard;
// Cursor right
lv_textarea_cursor_right(kb->ta);
}
void TextKeyboard::cursorStart()
{
lv_keyboard_t* kb = (lv_keyboard_t*)keyboard;
// Cursor to start
while (lv_textarea_get_cursor_pos(kb->ta) > 0)
lv_textarea_cursor_left(kb->ta);
}
void TextKeyboard::cursorEnd()
{
lv_keyboard_t* kb = (lv_keyboard_t*)keyboard;
// Cursor to end
size_t l = strlen(lv_textarea_get_text(kb->ta));
while (lv_textarea_get_cursor_pos(kb->ta) < l)
lv_textarea_cursor_right(kb->ta);
}
#if (defined(KEYS_GPIO_REG_PAGEUP) || defined(USE_HATS_AS_KEYS)) && \
!defined(PCBX12S)
// Radios with both PGUP and PGDN buttons except X12S
void TextKeyboard::onPressSYS() { changeMode(); }
void TextKeyboard::onLongPressSYS() {}
void TextKeyboard::onPressMDL() {}
void TextKeyboard::onLongPressMDL() { backspace(); }
void TextKeyboard::onPressTELE() { toggleCase(); }
void TextKeyboard::onLongPressTELE() { deleteChar(); }
void TextKeyboard::onPressPGUP() { cursorLeft(); }
void TextKeyboard::onPressPGDN() { cursorRight(); }
void TextKeyboard::onLongPressPGUP() { cursorStart(); }
void TextKeyboard::onLongPressPGDN() { cursorEnd(); }
#else
// Radios witb only a single PGUP/DN button or X12S
void TextKeyboard::onPressSYS() { cursorLeft(); }
void TextKeyboard::onLongPressSYS() { cursorStart(); }
void TextKeyboard::onPressMDL() { changeMode(); }
void TextKeyboard::onLongPressMDL() { backspace(); }
void TextKeyboard::onPressTELE() { cursorRight(); }
void TextKeyboard::onLongPressTELE() { cursorEnd(); }
#if defined(PCBX12S)
void TextKeyboard::onPressPGUP() { toggleCase(); }
#else
void TextKeyboard::onPressPGUP() { deleteChar(); }
#endif
void TextKeyboard::onPressPGDN() { toggleCase(); }
void TextKeyboard::onLongPressPGUP() {}
void TextKeyboard::onLongPressPGDN() { deleteChar(); }
#endif
#endif
void TextKeyboard::show(FormField* field)
+19 -1
View File
@@ -49,7 +49,25 @@ class TextKeyboard : public Keyboard
protected:
#if defined(HARDWARE_KEYS)
void onEvent(event_t event) override;
void changeMode();
void cursorLeft();
void cursorRight();
void cursorStart();
void cursorEnd();
void toggleCase();
void deleteChar();
void backspace();
void onPressSYS() override;
void onLongPressSYS() override;
void onPressMDL() override;
void onLongPressMDL() override;
void onPressTELE() override;
void onLongPressTELE() override;
void onPressPGUP() override;
void onPressPGDN() override;
void onLongPressPGUP() override;
void onLongPressPGDN() override;
#endif
static TextKeyboard* _instance;
+1 -1
View File
@@ -78,7 +78,7 @@ Window* Layer::getFirstOpaque()
{
for (auto layer = stack.crbegin(); layer != stack.crend(); layer++) {
auto w = layer->window;
if (w && (w->getWindowFlags() & OPAQUE)) return w;
if (w && w->hasWindowFlag(OPAQUE)) return w;
}
return nullptr;
+10 -50
View File
@@ -24,68 +24,30 @@
#include "keys.h"
#include "opentx_types.h"
enum FontIndex
{
FONT_STD_INDEX,
FONT_BOLD_INDEX,
FONT_XXS_INDEX,
FONT_XS_INDEX,
FONT_L_INDEX,
FONT_XL_INDEX,
FONT_XXL_INDEX,
// this one MUST be last
FONTS_COUNT
};
constexpr uint32_t MENU_HEADER_BUTTON_WIDTH = 33;
constexpr uint32_t MENU_HEADER_BUTTONS_LEFT = 47;
constexpr uint32_t MENUS_TOOLBAR_BUTTON_WIDTH = 32;
constexpr uint32_t MENUS_TOOLBAR_BUTTON_PADDING = 4;
constexpr uint32_t MENU_HEADER_HEIGHT = 45;
constexpr uint32_t MENU_TITLE_TOP = 48;
constexpr uint32_t MENU_TITLE_HEIGHT = 21;
constexpr uint32_t MENU_BODY_TOP = MENU_TITLE_TOP + MENU_TITLE_HEIGHT;
constexpr uint32_t MENU_FOOTER_HEIGHT = 0;
constexpr uint32_t MENU_FOOTER_TOP = LCD_H - MENU_FOOTER_HEIGHT;
constexpr uint32_t MENU_BODY_HEIGHT = MENU_FOOTER_TOP - MENU_BODY_TOP;
constexpr uint32_t MENU_BODY_HEIGHT = LCD_H - MENU_BODY_TOP;
constexpr uint32_t MENUS_MARGIN_LEFT = 6;
constexpr uint32_t MENU_HEADER_BACK_BUTTON_WIDTH = MENU_HEADER_HEIGHT;
constexpr uint32_t MENU_HEADER_BACK_BUTTON_HEIGHT = MENU_HEADER_HEIGHT;
constexpr coord_t PAGE_PADDING = 6;
constexpr uint32_t PAGE_LINE_HEIGHT = 20;
constexpr uint32_t FH = PAGE_LINE_HEIGHT;
constexpr uint32_t NUM_BODY_LINES = MENU_BODY_HEIGHT / PAGE_LINE_HEIGHT;
constexpr uint32_t FIELD_PADDING_LEFT = 3;
constexpr uint32_t FIELD_PADDING_TOP = 2;
constexpr uint32_t CURVE_SIDE_WIDTH = 100;
constexpr uint32_t CURVE_CENTER_X = LCD_W - CURVE_SIDE_WIDTH - 7;
constexpr uint32_t CURVE_CENTER_Y = 151;
constexpr uint32_t CURVE_COORD_WIDTH = 36;
constexpr uint32_t CURVE_COORD_HEIGHT = 17;
constexpr uint32_t DATETIME_SEPARATOR_X = LCD_W - 53;
constexpr uint32_t DATETIME_LINE1 = 6;
constexpr uint32_t DATETIME_MIDDLE = (LCD_W + DATETIME_SEPARATOR_X + 1) / 2;
constexpr uint32_t PAGE_TITLE_TOP = 2;
constexpr uint32_t PAGE_TITLE_LEFT = 50;
constexpr coord_t POPUP_HEADER_HEIGHT = 30;
constexpr coord_t MODEL_SELECT_FOOTER_HEIGHT = 24;
#define BLINK 0
#define TIMEHOUR 0
// Used by Lua API
#define INVERS 0x01u
#define BLINK 0x1000u
#define TIMEHOUR 0x2000u
/* drawText flags */
#define LEFT 0x00u /* align left */
// 0x01u used by Lua in api_colorlcd.h
#define VCENTERED 0x02u /* align center vertically */
#define CENTERED 0x04u /* align center */
#define RIGHT 0x08u /* align right */
@@ -102,14 +64,12 @@ constexpr coord_t MODEL_SELECT_FOOTER_HEIGHT = 24;
/* telemetry flags */
#define NO_UNIT 0x40u
#define FONT_MASK 0x0F00u
#define FONT_INDEX(flags) (((flags) & FONT_MASK) >> 8u)
#define FONT(xx) (unsigned(FONT_ ## xx ## _INDEX) << 8u)
#define LV_OBJ_FLAG_ENCODER_ACCEL LV_OBJ_FLAG_USER_1
constexpr coord_t MENUS_LINE_HEIGHT = 35;
constexpr coord_t MENUS_WIDTH = 200;
#include "libopenui_config.h"
#if LCD_W > LCD_H
constexpr coord_t MENUS_MAX_HEIGHT = (MENUS_LINE_HEIGHT * 7) + 8;
#else
constexpr coord_t MENUS_MAX_HEIGHT = (MENUS_LINE_HEIGHT * 10);
#endif
+3 -2
View File
@@ -16,10 +16,11 @@
* GNU General Public License for more details.
*/
#include <inttypes.h>
#include <string.h>
#include "libopenui_file.h"
#include <inttypes.h>
#include <cinttypes>
const char * getFileExtension(const char * filename, uint8_t size, uint8_t extMaxLen, uint8_t * fnlen, uint8_t * extlen)
{
int len = size;
-1
View File
@@ -20,7 +20,6 @@
#include <string>
#include <cstring>
#include <cinttypes>
#include "ff.h"
constexpr uint8_t LEN_FILE_EXTENSION_MAX = 5; // longest used, including the dot, excluding null term.
+46 -7
View File
@@ -17,16 +17,28 @@
*/
#include "mainwindow.h"
#include "keyboard_base.h"
#include "lvgl/lvgl.h"
#include "board.h"
#include "keyboard_base.h"
#include "layout.h"
#include "themes/etx_lv_theme.h"
MainWindow * MainWindow::_instance = nullptr;
MainWindow* MainWindow::_instance = nullptr;
MainWindow::MainWindow() :
Window(nullptr, {0, 0, LCD_W, LCD_H}, OPAQUE)
MainWindow* MainWindow::instance()
{
if (!_instance) _instance = new MainWindow();
return _instance;
}
MainWindow::MainWindow() : Window(nullptr, {0, 0, LCD_W, LCD_H})
{
setWindowFlag(OPAQUE);
etx_solid_bg(lvobj);
background = lv_canvas_create(lvobj);
lv_obj_center(background);
}
void MainWindow::emptyTrash()
@@ -42,10 +54,12 @@ void MainWindow::run(bool trash)
auto start = ticksNow();
auto opaque = Layer::getFirstOpaque();
if (opaque) opaque->checkEvents();
if (opaque) {
opaque->checkEvents();
}
auto copy = children;
for (auto child: copy) {
for (auto child : copy) {
if (!child->deleted() && child->isBubblePopup()) {
child->checkEvents();
}
@@ -59,3 +73,28 @@ void MainWindow::run(bool trash)
(ticksNow() - start) / SYSTEM_TICKS_1MS);
}
}
void MainWindow::setBackground(const BitmapBuffer* bitmap)
{
lv_canvas_set_buffer(background, bitmap->getData(), bitmap->width(),
bitmap->height(), LV_IMG_CF_TRUE_COLOR);
}
void MainWindow::shutdown()
{
// Called when USB is connected in SD card mode
// Delete main view screens
LayoutFactory::deleteCustomScreens();
// clear layer stack first
for (Window* w = Layer::back(); w; w = Layer::back()) w->deleteLater();
children.clear();
clear();
emptyTrash();
// Re-add background canvas
background = lv_canvas_create(lvobj);
lv_obj_center(background);
}
+8 -14
View File
@@ -18,9 +18,8 @@
#pragma once
#include <utility>
#include "layer.h"
#include "bitmapbuffer.h"
#include "window.h"
class MainWindow: public Window
{
@@ -29,18 +28,7 @@ class MainWindow: public Window
MainWindow();
public:
~MainWindow() override
{
children.clear();
}
static MainWindow * instance()
{
if (!_instance)
_instance = new MainWindow();
return _instance;
}
static MainWindow * instance();
#if defined(TESTS)
static void create()
@@ -62,7 +50,13 @@ class MainWindow: public Window
void run(bool trash=true);
void setBackground(const BitmapBuffer* bitmap);
void shutdown();
protected:
lv_obj_t* background = nullptr;
static MainWindow * _instance;
static void emptyTrash();
};
+358 -274
View File
@@ -20,268 +20,362 @@
#include <lvgl/lvgl.h>
#include "font.h"
#include "menutoolbar.h"
#include "table.h"
#include "theme.h"
#include "themes/etx_lv_theme.h"
void MenuBody::onDrawBegin(uint16_t row, uint16_t col,
lv_obj_draw_part_dsc_t* dsc)
{
lv_canvas_t* icon = (lv_canvas_t*)lines[row].getIcon();
if (!icon) return;
#include <memory>
lv_img_t* img = &icon->img;
lv_coord_t cell_left = lv_obj_get_style_pad_left(lvobj, LV_PART_ITEMS);
dsc->label_dsc->ofs_x = img->w + cell_left;
}
constexpr coord_t MENUS_WIDTH = 200;
void MenuBody::onDrawEnd(uint16_t row, uint16_t col,
lv_obj_draw_part_dsc_t* dsc)
{
if (row >= lines.size()) return;
lv_obj_t* icon = lines[row].getIcon();
if (icon) {
lv_draw_img_dsc_t img_dsc;
lv_draw_img_dsc_init(&img_dsc);
lv_img_dsc_t* img = lv_canvas_get_img(icon);
lv_area_t coords;
lv_coord_t area_h = lv_area_get_height(dsc->draw_area);
lv_coord_t cell_left = lv_obj_get_style_pad_left(lvobj, LV_PART_ITEMS);
coords.x1 = dsc->draw_area->x1 + cell_left;
coords.x2 = coords.x1 + img->header.w - 1;
coords.y1 = dsc->draw_area->y1 + (area_h - img->header.h) / 2;
coords.y2 = coords.y1 + img->header.h - 1;
lv_draw_img(dsc->draw_ctx, &img_dsc, &coords, img);
}
if (lines[row].isChecked != nullptr && lines[row].isChecked()) {
lv_area_t coords;
lv_coord_t area_h = lv_area_get_height(dsc->draw_area);
lv_coord_t cell_right = lv_obj_get_style_pad_right(lvobj, LV_PART_ITEMS);
lv_coord_t font_h = getFontHeight(FONT(STD));
coords.x1 = dsc->draw_area->x2 - cell_right - font_h;
coords.x2 = coords.x1 + font_h;
coords.y1 = dsc->draw_area->y1 + (area_h - font_h) / 2;
coords.y2 = coords.y1 + font_h - 1;
lv_draw_label(dsc->draw_ctx, dsc->label_dsc, &coords, LV_SYMBOL_OK,
nullptr);
}
}
struct lvobj_delete {
constexpr lvobj_delete() = default;
void operator()(lv_obj_t* obj) const { lv_obj_del(obj); }
};
static void _force_editing(lv_group_t* g) { lv_group_set_editing(g, true); }
static void localLongPressHandler(lv_event_t* e)
class MenuBody : public TableField
{
MenuBody* menuBody = (MenuBody*)lv_event_get_user_data(e);
if (menuBody) menuBody->longPress();
}
friend class MenuWindowContent;
friend class Menu;
void MenuBody::longPress()
{
isLongPressed = true;
Menu* menu = getParentMenu();
if (menu) menu->longPress();
}
enum MENU_DIRECTION { DIRECTION_UP = 1, DIRECTION_DOWN = -1 };
MenuBody::MenuBody(Window* parent, const rect_t& rect) :
TableField(parent, rect)
{
// Allow encoder acceleration
lv_obj_add_flag(lvobj, LV_OBJ_FLAG_ENCODER_ACCEL);
// Add scroll bar if needed
lv_obj_set_scrollbar_mode(lvobj, LV_SCROLLBAR_MODE_AUTO);
class MenuLine
{
friend class MenuBody;
setColumnCount(1);
setColumnWidth(0, rect.w);
lv_group_t* g = (lv_group_t*)lv_obj_get_group(lvobj);
if (g) {
setFocusHandler([=](bool focus) {
if (focus) {
lv_group_set_focus_cb(g, _force_editing);
} else {
lv_group_set_focus_cb(g, nullptr);
}
});
lv_group_set_editing(g, true);
}
lv_obj_add_event_cb(lvobj, localLongPressHandler, LV_EVENT_LONG_PRESSED,
this);
}
void MenuBody::addLine(const std::string& text, std::function<void()> onPress,
std::function<bool()> isChecked, bool update)
{
lines.emplace_back(text, std::move(onPress), std::move(isChecked), nullptr);
if (update) {
auto idx = lines.size() - 1;
lv_table_set_cell_value(lvobj, idx, 0, text.c_str());
}
}
void MenuBody::addLine(const uint8_t* icon_mask, const std::string& text,
std::function<void()> onPress,
std::function<bool()> isChecked, bool update)
{
lv_obj_t* canvas = lv_canvas_create(nullptr);
lines.emplace_back(text, std::move(onPress), std::move(isChecked), canvas);
lv_coord_t w = *((uint16_t*)icon_mask);
lv_coord_t h = *(((uint16_t*)icon_mask) + 1);
void* buf = (void*)(icon_mask + 4);
lv_canvas_set_buffer(canvas, buf, w, h, LV_IMG_CF_ALPHA_8BIT);
if (update) {
auto idx = lines.size() - 1;
lv_table_set_cell_value(lvobj, idx, 0, text.c_str());
}
}
void MenuBody::updateLines()
{
setRowCount(lines.size());
for (unsigned int idx = 0; idx < lines.size(); idx++) {
lv_table_set_cell_value(lvobj, idx, 0, lines[idx].text.c_str());
}
}
void MenuBody::removeLines()
{
lines.clear();
setRowCount(0);
selectedIndex = 0;
// reset vertical scroll
lv_obj_scroll_to_y(lvobj, 0, LV_ANIM_OFF);
}
coord_t MenuBody::getContentHeight() { return lv_obj_get_self_height(lvobj); }
void MenuBody::onPress(size_t index)
{
Menu* menu = getParentMenu();
if (index < lines.size()) {
if (menu->multiple) {
if (selectedIndex == (int)index)
lines[index].onPress();
else {
setIndex(index);
lines[index].onPress();
}
} else {
// delete menu first to avoid
// focus issues with onPress()
menu->deleteLater();
lines[index].onPress();
}
}
}
// ensure index is in range and also handle wrapping index
int MenuBody::rangeCheck(int index)
{
if (index < 0)
index = lines.size() - 1;
else if (index > (signed)lines.size() - 1)
index = 0;
return index;
}
void MenuBody::setIndex(int index)
{
if (index < (int)lines.size()) {
if (index == selectedIndex) return;
selectedIndex = index;
lv_obj_invalidate(lvobj);
lv_table_t* table = (lv_table_t*)lvobj;
if (index < 0) {
table->row_act = LV_TABLE_CELL_NONE;
table->col_act = LV_TABLE_CELL_NONE;
return;
public:
MenuLine(const std::string& text, std::function<void()> onPress,
std::function<bool()> isChecked, lv_obj_t* icon) :
text(text),
onPress(std::move(onPress)),
isChecked(std::move(isChecked)),
icon(icon)
{
}
table->row_act = index;
table->col_act = 0;
MenuLine(MenuLine&) = delete;
MenuLine(MenuLine&&) = default;
lv_coord_t h_before = 0;
for (uint16_t i = 0; i < table->row_act; i++) h_before += table->row_h[i];
lv_obj_t* getIcon() { return icon.get(); }
lv_coord_t row_h = table->row_h[table->row_act];
lv_coord_t scroll_y = lv_obj_get_scroll_y(lvobj);
protected:
std::string text;
std::function<void()> onPress;
std::function<bool()> isChecked;
std::unique_ptr<lv_obj_t, lvobj_delete> icon;
};
lv_obj_update_layout(lvobj);
lv_coord_t h = lv_obj_get_height(lvobj);
public:
MenuBody(Window* parent, const rect_t& rect) :
TableField(parent, rect)
{
// Allow encoder acceleration
lv_obj_add_flag(lvobj, LV_OBJ_FLAG_ENCODER_ACCEL);
// Add scroll bar if needed
etx_scrollbar(lvobj);
lv_coord_t diff_y = 0;
if (h_before < scroll_y) {
diff_y = scroll_y - h_before;
} else if (scroll_y + h < h_before + row_h) {
diff_y = scroll_y + h - h_before - row_h;
} else {
return;
setColumnWidth(0, rect.w);
lv_group_t* g = (lv_group_t*)lv_obj_get_group(lvobj);
if (g) {
setFocusHandler([=](bool focus) {
if (focus) {
lv_group_set_focus_cb(g, _force_editing);
} else {
lv_group_set_focus_cb(g, nullptr);
}
});
lv_group_set_editing(g, true);
}
lv_obj_scroll_by_bounded(lvobj, 0, diff_y, LV_ANIM_OFF);
lv_obj_add_event_cb(lvobj, MenuBody::localLongPressHandler,
LV_EVENT_LONG_PRESSED, this);
}
}
void MenuBody::onPress(uint16_t row, uint16_t col)
{
if (!isLongPressed) onPress(row);
isLongPressed = false;
}
void MenuBody::onEvent(event_t event)
{
#if defined(HARDWARE_KEYS)
if (event == EVT_KEY_BREAK(KEY_EXIT)) {
onCancel();
} else {
TableField::onEvent(event);
}
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "MenuBody"; }
#endif
void setIndex(int index)
{
if (index < (int)lines.size()) {
if (index == selectedIndex) return;
selectedIndex = index;
lv_obj_invalidate(lvobj);
lv_table_t* table = (lv_table_t*)lvobj;
if (index < 0) {
table->row_act = LV_TABLE_CELL_NONE;
table->col_act = LV_TABLE_CELL_NONE;
return;
}
table->row_act = index;
table->col_act = 0;
lv_coord_t h_before = 0;
for (uint16_t i = 0; i < table->row_act; i++) h_before += table->row_h[i];
lv_coord_t row_h = table->row_h[table->row_act];
lv_coord_t scroll_y = lv_obj_get_scroll_y(lvobj);
lv_obj_update_layout(lvobj);
lv_coord_t h = lv_obj_get_height(lvobj);
lv_coord_t diff_y = 0;
if (h_before < scroll_y) {
diff_y = scroll_y - h_before;
} else if (scroll_y + h < h_before + row_h) {
diff_y = scroll_y + h - h_before - row_h;
} else {
return;
}
lv_obj_scroll_by_bounded(lvobj, 0, diff_y, LV_ANIM_OFF);
}
}
int selection() const { return selectedIndex; }
int count() const { return lines.size(); }
void onEvent(event_t event) override
{
#if defined(HARDWARE_KEYS)
if (event == EVT_KEY_BREAK(KEY_EXIT)) {
onCancel();
} else {
TableField::onEvent(event);
}
#endif
}
void addLine(const uint8_t* icon_mask, const std::string& text,
std::function<void()> onPress, std::function<bool()> isChecked,
bool update = true)
{
lv_obj_t* canvas = nullptr;
if (icon_mask) {
canvas = lv_canvas_create(nullptr);
lv_coord_t w = *((uint16_t*)icon_mask);
lv_coord_t h = *(((uint16_t*)icon_mask) + 1);
void* buf = (void*)(icon_mask + 4);
lv_canvas_set_buffer(canvas, buf, w, h, LV_IMG_CF_ALPHA_8BIT);
}
lines.emplace_back(text, std::move(onPress), std::move(isChecked), canvas);
if (update) {
auto idx = lines.size() - 1;
lv_table_set_cell_value(lvobj, idx, 0, text.c_str());
}
}
void updateLines()
{
setRowCount(lines.size());
for (unsigned int idx = 0; idx < lines.size(); idx++) {
lv_table_set_cell_value(lvobj, idx, 0, lines[idx].text.c_str());
}
}
void removeLines()
{
lines.clear();
setRowCount(0);
selectedIndex = 0;
// reset vertical scroll
lv_obj_scroll_to_y(lvobj, 0, LV_ANIM_OFF);
}
void onPress(uint16_t row, uint16_t col) override
{
if (!isLongPressed) {
Menu* menu = getParentMenu();
if (row < lines.size()) {
if (menu->multiple) {
if (selectedIndex == (int)row)
lines[row].onPress();
else {
setIndex(row);
lines[row].onPress();
}
} else {
// delete menu first to avoid
// focus issues with onPress()
menu->deleteLater();
lines[row].onPress();
}
}
}
isLongPressed = false;
}
void longPress()
{
isLongPressed = true;
Menu* menu = getParentMenu();
if (menu) menu->longPress();
}
void onDrawBegin(uint16_t row, uint16_t col,
lv_obj_draw_part_dsc_t* dsc) override
{
lv_canvas_t* icon = (lv_canvas_t*)lines[row].getIcon();
if (!icon) return;
lv_img_t* img = &icon->img;
lv_coord_t cell_left = lv_obj_get_style_pad_left(lvobj, LV_PART_ITEMS);
dsc->label_dsc->ofs_x = img->w + cell_left;
}
void onDrawEnd(uint16_t row, uint16_t col,
lv_obj_draw_part_dsc_t* dsc) override
{
if (row >= lines.size()) return;
lv_obj_t* icon = lines[row].getIcon();
if (icon) {
lv_draw_img_dsc_t img_dsc;
lv_draw_img_dsc_init(&img_dsc);
lv_img_dsc_t* img = lv_canvas_get_img(icon);
lv_area_t coords;
lv_coord_t area_h = lv_area_get_height(dsc->draw_area);
lv_coord_t cell_left = lv_obj_get_style_pad_left(lvobj, LV_PART_ITEMS);
coords.x1 = dsc->draw_area->x1 + cell_left;
coords.x2 = coords.x1 + img->header.w - 1;
coords.y1 = dsc->draw_area->y1 + (area_h - img->header.h) / 2;
coords.y2 = coords.y1 + img->header.h - 1;
lv_draw_img(dsc->draw_ctx, &img_dsc, &coords, img);
}
if (lines[row].isChecked != nullptr && lines[row].isChecked()) {
lv_area_t coords;
lv_coord_t area_h = lv_area_get_height(dsc->draw_area);
lv_coord_t cell_right = lv_obj_get_style_pad_right(lvobj, LV_PART_ITEMS);
lv_coord_t font_h = getFontHeight(FONT(STD));
coords.x1 = dsc->draw_area->x2 - cell_right - font_h;
coords.x2 = coords.x1 + font_h;
coords.y1 = dsc->draw_area->y1 + (area_h - font_h) / 2;
coords.y2 = coords.y1 + font_h - 1;
lv_draw_label(dsc->draw_ctx, dsc->label_dsc, &coords, LV_SYMBOL_OK,
nullptr);
}
}
void onSelected(uint16_t row, uint16_t col) override { selectedIndex = row; }
protected:
bool isLongPressed = false;
std::vector<MenuLine> lines;
int selectedIndex = 0;
Menu* getParentMenu() { return static_cast<Menu*>(getParent()->getParent()); }
static void localLongPressHandler(lv_event_t* e)
{
MenuBody* menuBody = (MenuBody*)lv_event_get_user_data(e);
if (menuBody) menuBody->longPress();
}
};
//-----------------------------------------------------------------------------
static void menu_content_constructor(const lv_obj_class_t* class_p,
lv_obj_t* obj)
{
etx_solid_bg(obj);
etx_obj_add_style(obj, styles->outline, LV_PART_MAIN);
etx_obj_add_style(obj, styles->outline_color_normal, LV_PART_MAIN);
}
void MenuBody::onCancel()
static const lv_obj_class_t menu_content_class = {
.base_class = &window_base_class,
.constructor_cb = menu_content_constructor,
.destructor_cb = nullptr,
.user_data = nullptr,
.event_cb = nullptr,
.width_def = 0,
.height_def = 0,
.editable = LV_OBJ_CLASS_EDITABLE_INHERIT,
.group_def = LV_OBJ_CLASS_EDITABLE_INHERIT,
.instance_size = sizeof(lv_obj_t)};
static lv_obj_t* menu_content_create(lv_obj_t* parent)
{
if (_onCancel) _onCancel();
TableField::onCancel();
return etx_create(&menu_content_class, parent);
}
MenuWindowContent::MenuWindowContent(Menu* parent) :
ModalWindowContent(parent, {(LCD_W - MENUS_WIDTH) / 2,
(LCD_H - MENUS_WIDTH) / 2, MENUS_WIDTH, 0}),
body(this, {0, 0, width(), height()})
class MenuWindowContent : public Window
{
}
public:
explicit MenuWindowContent(Menu* parent) :
Window(parent, rect_t{}, menu_content_create)
{
setWindowFlag(OPAQUE);
coord_t MenuWindowContent::getHeaderHeight() const
{
if (title) return lv_obj_get_height(title);
return 0;
}
lv_obj_center(lvobj);
setFlexLayout(LV_FLEX_FLOW_COLUMN, PAD_ZERO, MENUS_WIDTH, LV_SIZE_CONTENT);
void MenuWindowContent::deleteLater(bool detach, bool trash)
{
if (_deleted) return;
body.deleteLater(true, false);
ModalWindowContent::deleteLater(detach, trash);
}
header = new StaticText(this, {0, 0, LV_PCT(100), 0}, "",
COLOR_THEME_PRIMARY2);
etx_solid_bg(header->getLvObj(), COLOR_THEME_SECONDARY1_INDEX);
header->padAll(PAD_SMALL);
header->hide();
body = new MenuBody(this, rect_t{0, 0, MENUS_WIDTH, LV_SIZE_CONTENT});
lv_obj_set_style_max_height(body->getLvObj(), LCD_H * 0.8, LV_PART_MAIN);
}
virtual void setTitle(const std::string& text)
{
header->setText(text);
header->show();
}
void onClicked() override { Keyboard::hide(false); }
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "MenuWindowContent"; }
#endif
void updateLines() { body->updateLines(); }
void removeLines() { body->removeLines(); }
int count() { return body->count(); }
int selection() { return body->selection(); }
void setIndex(int index) { body->setIndex(index); }
void addLine(const uint8_t* icon_mask, const std::string& text,
std::function<void()> onPress, std::function<bool()> isChecked,
bool update = true)
{
body->addLine(icon_mask, text, onPress, isChecked, update);
}
protected:
StaticText* header = nullptr;
MenuBody* body = nullptr;
};
//-----------------------------------------------------------------------------
Menu::Menu(Window* parent, bool multiple) :
ModalWindow(parent, true),
content(new MenuWindowContent(this)),
multiple(multiple)
multiple(multiple),
content(new MenuWindowContent(this))
{
}
@@ -289,37 +383,22 @@ void Menu::setToolbar(MenuToolbar* window)
{
toolbar = window;
updatePosition();
toolbar->setLeft(content->left() - toolbar->width());
toolbar->setTop(content->top());
toolbar->setHeight(content->height());
}
void Menu::setOutline(Window* obj)
{
lv_obj_set_style_outline_width(obj->getLvObj(), 2, LV_PART_MAIN);
lv_obj_set_style_outline_color(
obj->getLvObj(), makeLvColor(COLOR_THEME_SECONDARY2), LV_PART_MAIN);
lv_obj_set_style_outline_opa(obj->getLvObj(), LV_OPA_100, LV_PART_MAIN);
lv_obj_set_style_outline_pad(obj->getLvObj(), 1, LV_PART_MAIN);
}
void Menu::updatePosition()
{
coord_t height = content->body.getContentHeight();
if (toolbar) {
content->setLeft((LCD_W - content->width() + toolbar->width()) / 2);
setOutline(toolbar);
} else {
content->setLeft((LCD_W - content->width()) / 2);
auto headerHeight = content->getHeaderHeight();
auto bodyHeight = min<coord_t>(height, MENUS_MAX_HEIGHT - headerHeight);
content->setTop((LCD_H - headerHeight - bodyHeight) / 2);
content->setHeight(headerHeight + bodyHeight);
content->body.setTop(headerHeight);
content->body.setHeight(bodyHeight);
coord_t cw = lv_obj_get_width(content->getLvObj());
coord_t ch = lv_obj_get_height(content->getLvObj());
coord_t tw = lv_obj_get_width(toolbar->getLvObj());
coord_t th = lv_obj_get_height(toolbar->getLvObj());
lv_obj_align(toolbar->getLvObj(), LV_ALIGN_CENTER, -cw / 2, 0);
lv_obj_align(content->getLvObj(), LV_ALIGN_CENTER, tw / 2, 0);
toolbar->setHeight(max(ch, th));
content->setHeight(max(ch, th));
}
setOutline(content);
}
void Menu::setTitle(std::string text)
@@ -328,46 +407,31 @@ void Menu::setTitle(std::string text)
updatePosition();
}
void Menu::addLine(const std::string& text, std::function<void()> onPress,
std::function<bool()> isChecked)
{
content->body.addLine(text, std::move(onPress), std::move(isChecked));
updatePosition();
}
void Menu::addLine(const uint8_t* icon_mask, const std::string& text,
std::function<void()> onPress,
std::function<bool()> isChecked)
{
content->body.addLine(icon_mask, text, std::move(onPress),
std::move(isChecked));
content->addLine(icon_mask, text, std::move(onPress), std::move(isChecked));
updatePosition();
}
void Menu::addLineBuffered(const std::string& text,
std::function<void()> onPress,
std::function<bool()> isChecked)
{
content->body.addLine(text, std::move(onPress), std::move(isChecked), false);
}
void Menu::addLineBuffered(const uint8_t* icon_mask, const std::string& text,
std::function<void()> onPress,
std::function<bool()> isChecked)
{
content->body.addLine(icon_mask, text, std::move(onPress),
std::move(isChecked), false);
content->addLine(icon_mask, text, std::move(onPress), std::move(isChecked),
false);
}
void Menu::updateLines()
{
content->body.updateLines();
content->updateLines();
updatePosition();
}
void Menu::removeLines()
{
content->body.removeLines();
content->removeLines();
updatePosition();
}
@@ -386,9 +450,29 @@ void Menu::onEvent(event_t event)
#endif
}
void Menu::onCancel() { deleteLater(); }
void Menu::onCancel()
{
if (cancelHandler) cancelHandler();
deleteLater();
}
void Menu::longPress()
{
if (toolbar) toolbar->longPress();
}
void Menu::setCancelHandler(std::function<void()> handler)
{
cancelHandler = std::move(handler);
}
void Menu::setWaitHandler(std::function<void()> handler)
{
waitHandler = std::move(handler);
}
unsigned Menu::count() const { return content->count(); }
int Menu::selection() const { return content->selection(); }
void Menu::select(int index) { content->setIndex(index); }
+19 -147
View File
@@ -18,132 +18,12 @@
#pragma once
#include <functional>
#include <memory>
#include <utility>
#include <vector>
#include "modal_window.h"
#include "table.h"
class Menu;
class MenuWindowContent;
class MenuToolbar;
struct lvobj_delete {
constexpr lvobj_delete() = default;
void operator()(lv_obj_t *obj) const { lv_obj_del(obj); }
};
class MenuBody : public TableField
{
friend class MenuWindowContent;
friend class Menu;
enum MENU_DIRECTION { DIRECTION_UP = 1, DIRECTION_DOWN = -1 };
class MenuLine
{
friend class MenuBody;
public:
MenuLine(const std::string &text, std::function<void()> onPress,
std::function<bool()> isChecked, lv_obj_t *icon) :
text(text),
onPress(std::move(onPress)),
isChecked(std::move(isChecked)),
icon(icon)
{
}
MenuLine(MenuLine &) = delete;
MenuLine(MenuLine &&) = default;
lv_obj_t *getIcon() { return icon.get(); }
protected:
std::string text;
std::function<void()> onPress;
std::function<bool()> isChecked;
std::unique_ptr<lv_obj_t, lvobj_delete> icon;
};
public:
MenuBody(Window *parent, const rect_t &rect);
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "MenuBody"; }
#endif
void setIndex(int index);
int selection() const { return selectedIndex; }
int count() const { return lines.size(); }
void onEvent(event_t event) override;
void onCancel() override;
void addLine(const std::string &text, std::function<void()> onPress,
std::function<bool()> isChecked, bool update = true);
void addLine(const uint8_t *icon_mask, const std::string &text,
std::function<void()> onPress, std::function<bool()> isChecked,
bool update = true);
void updateLines();
void removeLines();
void setCancelHandler(std::function<void()> handler)
{
_onCancel = std::move(handler);
}
coord_t getContentHeight();
void onPress(size_t index);
void longPress();
protected:
bool isLongPressed = false;
void onPress(uint16_t row, uint16_t col) override;
void onDrawBegin(uint16_t row, uint16_t col,
lv_obj_draw_part_dsc_t *dsc) override;
void onDrawEnd(uint16_t row, uint16_t col,
lv_obj_draw_part_dsc_t *dsc) override;
int rangeCheck(int);
std::vector<MenuLine> lines;
int selectedIndex = 0;
std::function<void()> _onCancel;
inline Menu *getParentMenu();
void onSelected(uint16_t row, uint16_t col) override { selectedIndex = row; }
};
class MenuWindowContent : public ModalWindowContent
{
friend class Menu;
public:
explicit MenuWindowContent(Menu *parent);
coord_t getHeaderHeight() const;
void deleteLater(bool detach = true, bool trash = true) override;
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "MenuWindowContent"; }
#endif
protected:
MenuBody body;
};
class Menu : public ModalWindow
{
friend class MenuBody;
@@ -155,45 +35,42 @@ class Menu : public ModalWindow
std::string getName() const override { return "Menu"; }
#endif
void setCancelHandler(std::function<void()> handler)
{
content->body.setCancelHandler(std::move(handler));
}
void setWaitHandler(std::function<void()> handler)
{
waitHandler = std::move(handler);
}
void setCancelHandler(std::function<void()> handler);
void setWaitHandler(std::function<void()> handler);
void setToolbar(MenuToolbar *window);
void setTitle(std::string text);
void addLine(const std::string &text, std::function<void()> onPress,
std::function<bool()> isChecked = nullptr);
void addLine(const uint8_t *icon_mask, const std::string &text,
std::function<void()> onPress,
std::function<bool()> isChecked = nullptr);
void addLineBuffered(const std::string &text, std::function<void()> onPress,
std::function<bool()> isChecked = nullptr);
void addLine(const std::string &text, std::function<void()> onPress,
std::function<bool()> isChecked = nullptr)
{
addLine(nullptr, text, onPress, isChecked);
}
void addLineBuffered(const uint8_t *icon_mask, const std::string &text,
std::function<void()> onPress,
std::function<bool()> isChecked = nullptr);
void updateLines();
void addLineBuffered(const std::string &text, std::function<void()> onPress,
std::function<bool()> isChecked = nullptr)
{
addLineBuffered(nullptr, text, onPress, isChecked);
}
void addSeparator();
void updateLines();
void removeLines();
unsigned count() const { return content->body.count(); }
unsigned count() const;
int selection() const { return content->body.selection(); }
int selection() const;
void select(int index) { content->body.setIndex(index); }
void select(int index);
void onEvent(event_t event) override;
void onCancel() override;
@@ -209,16 +86,11 @@ class Menu : public ModalWindow
void longPress();
protected:
MenuWindowContent *content;
bool multiple;
MenuWindowContent *content;
MenuToolbar *toolbar = nullptr;
std::function<void()> waitHandler;
std::function<void()> cancelHandler;
void updatePosition();
void setOutline(Window *obj);
};
Menu *MenuBody::getParentMenu()
{
return static_cast<Menu *>(getParent()->getParent());
}
+42 -21
View File
@@ -18,9 +18,30 @@
#include "menutoolbar.h"
#include "font.h"
#include "menu.h"
#include "themes/etx_lv_theme.h"
#include "translations.h"
constexpr uint32_t MENUS_TOOLBAR_BUTTON_WIDTH = 32;
static const lv_obj_class_t menu_button_class = {
.base_class = &button_class,
.constructor_cb = nullptr,
.destructor_cb = nullptr,
.user_data = nullptr,
.event_cb = nullptr,
.width_def = 0,
.height_def = 0,
.editable = LV_OBJ_CLASS_EDITABLE_INHERIT,
.group_def = LV_OBJ_CLASS_GROUP_DEF_FALSE,
.instance_size = sizeof(lv_btn_t),
};
static lv_obj_t* menu_button_create(lv_obj_t* parent)
{
return etx_create(&menu_button_class, parent);
}
static void toolbar_btn_defocus(lv_event_t* event)
{
auto obj = lv_event_get_target(event);
@@ -30,7 +51,7 @@ static void toolbar_btn_defocus(lv_event_t* event)
MenuToolbarButton::MenuToolbarButton(Window* parent, const rect_t& rect,
const char* picto) :
Button(parent, rect, nullptr, 0, 0, etx_menu_button_create)
ButtonBase(parent, rect, nullptr, menu_button_create)
{
lv_obj_add_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
lv_obj_add_flag(lvobj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
@@ -43,18 +64,21 @@ MenuToolbarButton::MenuToolbarButton(Window* parent, const rect_t& rect,
}
MenuToolbar::MenuToolbar(Choice* choice, Menu* menu, const int columns) :
Window(menu, {0, (LCD_H - MENUS_MAX_HEIGHT) / 2, 76, MENUS_MAX_HEIGHT},
OPAQUE),
Window(menu, {0, 0, 76, MENUS_MAX_HEIGHT}),
choice(choice),
menu(menu),
filterColumns(columns),
group(lv_group_create())
{
lv_obj_set_style_bg_color(lvobj, makeLvColor(COLOR_THEME_SECONDARY3), 0);
setWindowFlag(OPAQUE);
setWidth((MENUS_TOOLBAR_BUTTON_WIDTH + MENUS_TOOLBAR_BUTTON_PADDING) *
columns +
MENUS_TOOLBAR_BUTTON_PADDING);
padAll(PAD_SMALL);
etx_solid_bg(lvobj);
etx_obj_add_style(lvobj, styles->outline, LV_PART_MAIN);
etx_obj_add_style(lvobj, styles->outline_color_normal, LV_PART_MAIN);
setWidth((MENUS_TOOLBAR_BUTTON_WIDTH + PAD_SMALL) * columns + PAD_SMALL);
addButton(STR_SELECT_MENU_ALL, choice->getMin(), choice->getMax(), nullptr,
nullptr, true);
@@ -93,17 +117,14 @@ rect_t MenuToolbar::getButtonRect(bool wideButton)
{
if (wideButton && (nxtBtnPos % filterColumns))
nxtBtnPos = nxtBtnPos - (nxtBtnPos % filterColumns) + filterColumns;
coord_t x = (nxtBtnPos % filterColumns) *
(MENUS_TOOLBAR_BUTTON_WIDTH + MENUS_TOOLBAR_BUTTON_PADDING) +
MENUS_TOOLBAR_BUTTON_PADDING;
coord_t y = (nxtBtnPos / filterColumns) *
(MENUS_TOOLBAR_BUTTON_WIDTH + MENUS_TOOLBAR_BUTTON_PADDING) +
MENUS_TOOLBAR_BUTTON_PADDING;
coord_t w =
wideButton ? (MENUS_TOOLBAR_BUTTON_WIDTH + MENUS_TOOLBAR_BUTTON_PADDING) *
(filterColumns - 1) +
MENUS_TOOLBAR_BUTTON_WIDTH
: MENUS_TOOLBAR_BUTTON_WIDTH;
coord_t x =
(nxtBtnPos % filterColumns) * (MENUS_TOOLBAR_BUTTON_WIDTH + PAD_SMALL);
coord_t y =
(nxtBtnPos / filterColumns) * (MENUS_TOOLBAR_BUTTON_WIDTH + PAD_SMALL);
coord_t w = wideButton ? (MENUS_TOOLBAR_BUTTON_WIDTH + PAD_SMALL) *
(filterColumns - 1) +
MENUS_TOOLBAR_BUTTON_WIDTH
: MENUS_TOOLBAR_BUTTON_WIDTH;
nxtBtnPos += wideButton ? filterColumns : 1;
return {x, y, w, MENUS_TOOLBAR_BUTTON_WIDTH};
}
@@ -139,8 +160,8 @@ void MenuToolbar::addButton(const char* picto, int16_t filtermin,
const Choice::FilterFct& filterFunc,
const char* title, bool wideButton)
{
int vmin = choice->vmin;
int vmax = choice->vmax;
int vmin = choice->getMin();
int vmax = choice->getMax();
if (vmin > filtermin || vmax < filtermin) return;
+2 -3
View File
@@ -20,11 +20,10 @@
#include "button.h"
#include "choice.h"
#include "menu.h"
class Choice;
class Menu;
class MenuToolbarButton : public Button
class MenuToolbarButton : public ButtonBase
{
public:
MenuToolbarButton(Window* parent, const rect_t& rect, const char* picto);
+35 -31
View File
@@ -17,13 +17,37 @@
*/
#include "modal_window.h"
#include "keyboard_base.h"
#include "font.h"
#include "layer.h"
ModalWindow::ModalWindow(Window * parent, bool closeWhenClickOutside):
Window(parent->getFullScreenWindow(), {0, 0, LCD_W, LCD_H}, 0, 0, etx_modal_create),
closeWhenClickOutside(closeWhenClickOutside)
#include "layer.h"
#include "themes/etx_lv_theme.h"
// Modal overlay style (for dimming background)
void modal_window_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj)
{
etx_obj_add_style(obj, styles->bg_opacity_50, LV_PART_MAIN);
etx_bg_color(obj, COLOR_BLACK_INDEX, LV_PART_MAIN);
}
static const lv_obj_class_t modal_window_class = {
.base_class = &window_base_class,
.constructor_cb = modal_window_constructor,
.destructor_cb = nullptr,
.user_data = nullptr,
.event_cb = nullptr,
.width_def = 0,
.height_def = 0,
.editable = LV_OBJ_CLASS_EDITABLE_INHERIT,
.group_def = LV_OBJ_CLASS_EDITABLE_INHERIT,
.instance_size = sizeof(lv_obj_t)};
static lv_obj_t* modal_create(lv_obj_t* parent)
{
return etx_create(&modal_window_class, parent);
}
ModalWindow::ModalWindow(Window* parent, bool closeWhenClickOutside) :
Window(parent->getFullScreenWindow(), {0, 0, LCD_W, LCD_H}, modal_create),
closeWhenClickOutside(closeWhenClickOutside)
{
Layer::push(this);
}
@@ -38,30 +62,10 @@ void ModalWindow::deleteLater(bool detach, bool trash)
void ModalWindow::onClicked()
{
if (closeWhenClickOutside) {
deleteLater();
// Call onCancel of first child (if one exists)
if (children.size() > 0)
children.front()->onCancel();
else
deleteLater();
}
}
ModalWindowContent::ModalWindowContent(ModalWindow* parent,
const rect_t& rect) :
Window(parent, rect, OPAQUE, 0, etx_modal_content_create)
{
}
void ModalWindowContent::onClicked() { Keyboard::hide(false); }
void ModalWindowContent::setTitle(const std::string& text)
{
if (!title) {
title = etx_modal_title_create(lvobj);
lv_obj_move_to_index(title, 0);
lv_obj_update_layout(lvobj);
}
lv_label_set_text(title, text.c_str());
}
std::string ModalWindowContent::getTitle() const
{
if (!title) return std::string();
return lv_label_get_text(title);
}
-18
View File
@@ -40,21 +40,3 @@ class ModalWindow : public Window
protected:
bool closeWhenClickOutside;
};
class ModalWindowContent : public Window
{
public:
explicit ModalWindowContent(ModalWindow* parent, const rect_t& rect);
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "ModalWindowContent"; }
#endif
virtual void setTitle(const std::string& text);
std::string getTitle() const;
void onClicked() override;
protected:
lv_obj_t* title = nullptr;
};
+80 -21
View File
@@ -17,8 +17,11 @@
*/
#include "numberedit.h"
#include "keyboard_number.h"
#include "audio.h"
#include "keyboard_number.h"
#include "strhelpers.h"
#include "themes/etx_lv_theme.h"
static void numberedit_cb(lv_event_t* e)
{
@@ -27,36 +30,56 @@ static void numberedit_cb(lv_event_t* e)
uint32_t key = lv_event_get_key(e);
switch (key) {
case LV_KEY_LEFT:
numEdit->onEvent(EVT_ROTARY_LEFT);
break;
case LV_KEY_RIGHT:
numEdit->onEvent(EVT_ROTARY_RIGHT);
break;
case LV_KEY_LEFT:
numEdit->onEvent(EVT_ROTARY_LEFT);
break;
case LV_KEY_RIGHT:
numEdit->onEvent(EVT_ROTARY_RIGHT);
break;
}
}
NumberEdit::NumberEdit(Window* parent, const rect_t& rect, int vmin, int vmax,
std::function<int()> getValue,
std::function<void(int)> setValue,
WindowFlags windowFlags, LcdFlags textFlags) :
FormField(parent, rect, windowFlags, textFlags, etx_number_edit_create),
vmin(vmin),
vmax(vmax),
_getValue(std::move(getValue)),
_setValue(std::move(setValue))
std::function<void(int)> setValue, LcdFlags textFlags) :
FormField(rect, textFlags),
vmin(vmin),
vmax(vmax),
_getValue(std::move(getValue)),
_setValue(std::move(setValue))
{
lv_obj_enable_style_refresh(false);
// Workaround for performance issues with lv_textarea - create on top layer
// not this window then reparent to this window after setup finished
this->parent = parent;
lvobj = lv_textarea_create(lv_layer_top());
// Do this first - before any styles are applied, otherwise it is very slow
update();
etx_textarea_style(lvobj);
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);
lv_obj_add_event_cb(lvobj, numberedit_cb, LV_EVENT_KEY, this);
update();
lv_obj_set_parent(lvobj, parent->getLvObj());
setupLVGL();
if (rect.w == 0) setWidth(100);
lv_obj_enable_style_refresh(true);
lv_obj_refresh_style(lvobj, LV_PART_ANY, LV_STYLE_PROP_ANY);
}
void NumberEdit::onEvent(event_t event)
{
TRACE_WINDOWS("%s received event 0x%X", getWindowDebugString().c_str(), event);
TRACE_WINDOWS("%s received event 0x%X", getWindowDebugString().c_str(),
event);
if (editMode) {
switch (event) {
@@ -74,8 +97,7 @@ void NumberEdit::onEvent(event_t event)
} while (isValueAvailable && !isValueAvailable(value) && value <= vmax);
if (value <= vmax) {
setValue(value);
}
else {
} else {
setValue(vmax);
onKeyError();
}
@@ -95,8 +117,7 @@ void NumberEdit::onEvent(event_t event)
} while (isValueAvailable && !isValueAvailable(value) && value >= vmin);
if (value >= vmin) {
setValue(value);
}
else {
} else {
setValue(vmin);
onKeyError();
}
@@ -144,10 +165,48 @@ void NumberEdit::onEvent(event_t event)
void NumberEdit::onClicked()
{
lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
if(indev_type == LV_INDEV_TYPE_POINTER) {
if (indev_type == LV_INDEV_TYPE_POINTER) {
NumberKeyboard::show(this);
return;
}
FormField::onClicked();
}
void NumberEdit::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());
}
}
void NumberEdit::setValue(int value)
{
auto newValue = limit(vmin, value, vmax);
if (newValue != currentValue) {
currentValue = newValue;
if (_setValue != nullptr) {
_setValue(currentValue);
}
}
updateDisplay();
}
void NumberEdit::update()
{
if (_getValue == nullptr) return;
auto newValue = _getValue();
if (newValue != currentValue) {
currentValue = newValue;
}
updateDisplay();
}
+5 -45
View File
@@ -19,8 +19,6 @@
#pragma once
#include "form.h"
#include "strhelpers.h"
#include <string>
class NumberEdit: public FormField
{
@@ -28,7 +26,7 @@ class NumberEdit: public FormField
NumberEdit(Window* parent, const rect_t& rect, int vmin, int vmax,
std::function<int()> getValue,
std::function<void(int)> setValue = nullptr,
WindowFlags windowFlags = 0, LcdFlags textFlags = 0);
LcdFlags textFlags = 0);
#if defined(DEBUG_WINDOWS)
std::string getName() const override
@@ -105,28 +103,7 @@ class NumberEdit: public FormField
return accelFactor;
}
void setValue(int value)
{
auto newValue = limit(vmin, value, vmax);
if (newValue != currentValue) {
currentValue = newValue;
if (_setValue != nullptr) {
_setValue(currentValue);
}
}
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());
}
}
void setValue(int value);
void setPrefix(std::string value)
{
@@ -167,26 +144,7 @@ class NumberEdit: public FormField
update();
}
virtual void update()
{
if (_getValue == nullptr) return;
auto newValue = _getValue();
if (newValue != currentValue) {
currentValue = newValue;
}
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());
}
}
virtual void update();
protected:
int vdefault = 0;
@@ -203,4 +161,6 @@ class NumberEdit: public FormField
std::function<void(int)> _setValue;
std::function<std::string(int)> displayFunction;
std::function<bool(int)> isValueAvailable;
void updateDisplay();
};
+31 -4
View File
@@ -17,12 +17,39 @@
*/
#include "progress.h"
#include "theme.h"
Progress::Progress(Window * parent, const rect_t & rect):
Window(parent, rect)
#include "themes/etx_lv_theme.h"
static void etx_bar_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj)
{
bar = etx_bar_create(lvobj);
etx_solid_bg(obj, COLOR_THEME_SECONDARY2_INDEX);
etx_obj_add_style(obj, styles->rounded, LV_PART_MAIN);
etx_solid_bg(obj, COLOR_THEME_SECONDARY1_INDEX, LV_PART_INDICATOR);
etx_obj_add_style(obj, styles->rounded, LV_PART_INDICATOR);
}
static const lv_obj_class_t bar_class = {
.base_class = &lv_bar_class,
.constructor_cb = etx_bar_constructor,
.destructor_cb = nullptr,
.user_data = nullptr,
.event_cb = nullptr,
.width_def = LV_PCT(100),
.height_def = 32,
.editable = LV_OBJ_CLASS_EDITABLE_INHERIT,
.group_def = LV_OBJ_CLASS_GROUP_DEF_INHERIT,
.instance_size = sizeof(lv_bar_t),
};
static lv_obj_t* bar_create(lv_obj_t* parent)
{
return etx_create(&bar_class, parent);
}
Progress::Progress(Window* parent, const rect_t& rect) : Window(parent, rect)
{
bar = bar_create(lvobj);
lv_bar_set_range(bar, 0, 100);
setValue(0);
}
+9 -11
View File
@@ -20,20 +20,18 @@
#include "window.h"
class Progress: public Window {
public:
Progress(Window * parent, const rect_t & rect);
class Progress : public Window
{
public:
Progress(Window* parent, const rect_t& rect);
#if defined(DEBUG_WINDOWS)
std::string getName() const override
{
return "Progress";
}
std::string getName() const override { return "Progress"; }
#endif
void setValue(int newValue);
void setValue(int newValue);
protected:
int value = -1;
lv_obj_t* bar;
protected:
int value = -1;
lv_obj_t* bar;
};
+91 -17
View File
@@ -18,8 +18,55 @@
#include "slider.h"
#include "LvglWrapper.h"
#include "theme.h"
#include "themes/etx_lv_theme.h"
// Slider
const lv_style_const_prop_t slider_knob_props[] = {
LV_STYLE_CONST_PAD_TOP(9), LV_STYLE_CONST_PAD_BOTTOM(9),
LV_STYLE_CONST_PAD_LEFT(4), LV_STYLE_CONST_PAD_RIGHT(4),
LV_STYLE_CONST_RADIUS(4), LV_STYLE_CONST_BORDER_WIDTH(2),
LV_STYLE_PROP_INV,
};
LV_STYLE_CONST_MULTI_INIT(slider_knob, slider_knob_props);
static void slider_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj)
{
etx_solid_bg(obj, COLOR_THEME_SECONDARY1_INDEX);
etx_obj_add_style(obj, styles->rounded, LV_PART_MAIN);
etx_bg_color(obj, COLOR_THEME_FOCUS_INDEX, LV_PART_MAIN | LV_STATE_FOCUSED);
etx_bg_color(obj, COLOR_THEME_EDIT_INDEX,
LV_PART_MAIN | LV_STATE_FOCUSED | LV_STATE_EDITED);
etx_bg_color(obj, COLOR_THEME_FOCUS_INDEX,
LV_PART_INDICATOR | LV_STATE_FOCUSED);
etx_bg_color(obj, COLOR_THEME_EDIT_INDEX,
LV_PART_INDICATOR | LV_STATE_FOCUSED | LV_STATE_EDITED);
etx_obj_add_style(obj, styles->bg_opacity_cover, LV_PART_KNOB);
etx_obj_add_style(obj, slider_knob, LV_PART_KNOB);
etx_obj_add_style(obj, styles->border_color_dark, LV_PART_KNOB);
etx_obj_add_style(obj, styles->border_color_focus,
LV_PART_KNOB | LV_STATE_FOCUSED);
}
static const lv_obj_class_t slider_class = {
.base_class = &lv_slider_class,
.constructor_cb = slider_constructor,
.destructor_cb = nullptr,
.user_data = nullptr,
.event_cb = nullptr,
.width_def = 0,
.height_def = 8,
.editable = LV_OBJ_CLASS_EDITABLE_INHERIT,
.group_def = LV_OBJ_CLASS_GROUP_DEF_INHERIT,
.instance_size = sizeof(lv_slider_t),
};
static lv_obj_t* slider_create(lv_obj_t* parent)
{
return etx_create(&slider_class, parent);
}
static void slider_changed_cb(lv_event_t* e)
{
@@ -35,7 +82,7 @@ static void slider_changed_cb(lv_event_t* e)
Slider::Slider(Window* parent, coord_t width, int32_t vmin, int32_t vmax,
std::function<int()> getValue,
std::function<void(int)> setValue) :
Window(parent, {0, 0, width, 32}, 0, 0, window_create),
Window(parent, {0, 0, width, 32}, window_create),
vmin(vmin),
vmax(vmax),
_getValue(std::move(getValue)),
@@ -45,15 +92,51 @@ Slider::Slider(Window* parent, coord_t width, int32_t vmin, int32_t vmax,
padLeft(8);
padRight(8);
slider = (new FormField(this, rect_t{}, 0, 0, etx_slider_create))->getLvObj();
slider = (new FormField(this, rect_t{}, 0, 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);
lv_slider_set_range(slider, vmin, vmax);
lv_obj_add_event_cb(lvobj, Slider::on_draw, LV_EVENT_DRAW_MAIN_BEGIN,
nullptr);
int range = vmax - vmin;
if (range < 10) {
tickPts = new lv_obj_t*[range - 1];
for (int n = 1; n < range; n += 1) {
lv_obj_t* p = lv_obj_create(lvobj);
lv_obj_set_size(p, 2, 6);
etx_solid_bg(p, COLOR_THEME_PRIMARY2_INDEX);
tickPts[n - 1] = p;
}
}
update();
}
void Slider::on_draw(lv_event_t* e)
{
auto dlg = (Slider*)lv_obj_get_user_data(lv_event_get_target(e));
if (dlg) dlg->delayedInit();
}
void Slider::delayedInit()
{
if (!loaded) {
loaded = true;
coord_t w = lv_obj_get_width(lvobj) - 16;
coord_t x = -1;
int range = vmax - vmin;
if (range < 10) {
for (int n = 1; n < range; n += 1) {
lv_obj_set_pos(tickPts[n - 1], x + (w * n) / range, 1);
}
}
}
}
void Slider::update()
{
if (_getValue != nullptr) {
@@ -67,19 +150,10 @@ void Slider::update()
}
}
void Slider::paint(BitmapBuffer* dc)
void Slider::deleteLater(bool detach, bool trash)
{
coord_t w = lv_obj_get_width(slider);
coord_t x = (lv_obj_get_width(lvobj) - w) / 2 - 1;
int range = vmax - vmin;
LcdFlags color = lv_obj_has_state(slider, LV_STATE_EDITED) ? COLOR_THEME_EDIT
: lv_obj_has_state(slider, LV_STATE_FOCUSED)
? COLOR_THEME_FOCUS
: COLOR_THEME_SECONDARY1;
if (range < 10) {
for (int n = 1; n < range; n += 1) {
dc->drawSolidRect(x + (w * n) / range, 6, 2, 2, 1, color);
}
if (!deleted()) {
if (tickPts) delete tickPts;
Window::deleteLater(detach, trash);
}
}
}
+9 -3
View File
@@ -18,7 +18,7 @@
#pragma once
#include "form.h"
#include "window.h"
class Slider : public Window
{
@@ -35,14 +35,20 @@ class Slider : public Window
if (_setValue != nullptr) _setValue(limit(vmin, value, vmax));
}
void paint(BitmapBuffer *) override;
void update();
protected:
int vmin;
int vmax;
lv_obj_t* slider;
lv_obj_t** tickPts = nullptr;
bool loaded = false;
std::function<int()> _getValue;
std::function<void(int)> _setValue;
static void on_draw(lv_event_t* e);
void delayedInit();
void deleteLater(bool detach, bool trash) override;
};
+204 -39
View File
@@ -17,35 +17,41 @@
*/
#include "static.h"
#include "font.h"
#include "bitmaps.h"
#include "libopenui/thirdparty/lz4/lz4.h"
#include "sdcard.h"
#include "themes/etx_lv_theme.h"
//-----------------------------------------------------------------------------
StaticText::StaticText(Window* parent, const rect_t& rect, std::string txt,
WindowFlags windowFlags, LcdFlags textFlags) :
Window(parent, rect, windowFlags, textFlags, lv_label_create),
text(std::move(txt))
LcdFlags textFlags) :
Window(parent, rect, lv_label_create), text(std::move(txt))
{
lv_obj_enable_style_refresh(false);
setTextFlag(textFlags);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
lv_obj_set_style_text_font(lvobj, getFont(textFlags), LV_PART_MAIN);
lv_obj_set_style_text_color(lvobj, makeLvColor(textFlags), LV_PART_MAIN);
etx_font(lvobj, FONT_INDEX(textFlags));
etx_txt_color(lvobj, indexFromColor(textFlags));
if (textFlags & CENTERED)
lv_obj_set_style_text_align(lvobj, LV_TEXT_ALIGN_CENTER, 0);
etx_obj_add_style(lvobj, styles->text_align_center, LV_PART_MAIN);
else if (textFlags & RIGHT)
lv_obj_set_style_text_align(lvobj, LV_TEXT_ALIGN_RIGHT, 0);
etx_obj_add_style(lvobj, styles->text_align_right, LV_PART_MAIN);
lv_obj_set_style_grid_cell_x_align(lvobj, LV_GRID_ALIGN_STRETCH, 0);
lv_obj_set_style_grid_cell_x_align(lvobj, LV_GRID_ALIGN_STRETCH,
LV_PART_MAIN);
lv_label_set_text(lvobj, text.c_str());
lv_obj_set_height(lvobj, LV_SIZE_CONTENT);
lv_obj_enable_style_refresh(true);
if (rect.h == 0) lv_obj_set_height(lvobj, LV_SIZE_CONTENT);
lv_obj_refresh_style(lvobj, LV_PART_ANY, LV_STYLE_PROP_ANY);
}
#if defined(DEBUG_WINDOWS)
std::string StaticText::getName() const
{
return "StaticText \"" + text + "\"";
return "StaticText \"" + text.substr(0, 20) + "\"";
}
#endif
@@ -57,30 +63,9 @@ void StaticText::setText(std::string value)
}
}
const std::string& StaticText::getText() const
{
return text;
}
const std::string& StaticText::getText() const { return text; }
void StaticText::setBackgroundColor(LcdFlags color)
{
bgColor = color;
if (!lvobj) return;
lv_color_t c = makeLvColor(bgColor);
lv_obj_set_style_bg_color(lvobj, c, LV_PART_MAIN);
}
void StaticText::setBackgroudOpacity(uint32_t opa)
{
lv_obj_set_style_bg_opa(lvobj, opa, LV_PART_MAIN);
}
void StaticText::setFont(LcdFlags font)
{
lv_obj_set_style_text_font(lvobj, getFont(font), 0);
}
//-----------------------------------------------------------------------------
template <>
void DynamicNumber<uint32_t>::updateText()
@@ -126,7 +111,8 @@ void DynamicNumber<uint16_t>::updateText()
const char* s = suffix ? suffix : "";
if ((textFlags & PREC2) == PREC2) {
lv_label_set_text_fmt(lvobj, "%s%" PRIu16 ".%02" PRIu16 "%s", p,
(uint16_t)(value / 100), (uint16_t)(value % 100), s);
(uint16_t)(value / 100), (uint16_t)(value % 100),
s);
} else if (textFlags & PREC1) {
lv_label_set_text_fmt(lvobj, "%s%" PRIu16 ".%01" PRIu16 "%s", p,
(uint16_t)(value / 10), (uint16_t)(value % 10), s);
@@ -144,12 +130,191 @@ void DynamicNumber<int16_t>::updateText()
const char* s = suffix ? suffix : "";
if ((textFlags & PREC2) == PREC2) {
lv_label_set_text_fmt(lvobj, "%s%" PRId16 ".%02" PRIu16 "%s", p,
(int16_t)(value / 100), (uint16_t)abs(value % 100), s);
(int16_t)(value / 100), (uint16_t)abs(value % 100),
s);
} else if (textFlags & PREC1) {
lv_label_set_text_fmt(lvobj, "%s%" PRId16 ".%01" PRIu16 "%s", p,
(int16_t)(value / 10), (uint16_t)abs(value % 10), s);
(int16_t)(value / 10), (uint16_t)abs(value % 10),
s);
} else {
lv_label_set_text_fmt(lvobj, "%s%" PRId16 "%s", p, value, s);
}
}
}
//-----------------------------------------------------------------------------
StaticIcon::StaticIcon(Window* parent, coord_t x, coord_t y, EdgeTxIcon icon,
LcdFlags color) :
Window(parent, rect_t{x, y, 0, 0}, lv_canvas_create),
currentColor(indexFromColor(color))
{
setWindowFlag(NO_FOCUS);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICKABLE);
setIcon(icon);
etx_img_color(lvobj, currentColor, LV_PART_MAIN);
}
void StaticIcon::setColor(LcdColorIndex color)
{
if (currentColor != color) {
etx_img_color(lvobj, color, LV_PART_MAIN);
currentColor = color;
}
}
void StaticIcon::setIcon(EdgeTxIcon icon)
{
auto mask = getBuiltinIcon(icon);
setSize(mask->width, mask->height);
lv_canvas_set_buffer(lvobj, (void*)mask->data, mask->width, mask->height,
LV_IMG_CF_ALPHA_8BIT);
}
void StaticIcon::center(coord_t w, coord_t h)
{
setPos((w - width()) / 2, (h - height()) / 2);
}
//-----------------------------------------------------------------------------
// Display image from file system using LVGL with LVGL scaling
// - LVGL scaling is slow so don't use this if there are many images
StaticImage::StaticImage(Window* parent, const rect_t& rect,
const char* filename) :
Window(parent, rect)
{
setWindowFlag(NO_FOCUS);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICKABLE);
if (!filename) filename = "";
setSource(filename);
}
void StaticImage::setSource(std::string filename)
{
if (!filename.empty()) {
std::string fullpath = std::string("A" PATH_SEPARATOR) + filename;
if (!image) image = lv_img_create(lvobj);
lv_obj_set_pos(image, 0, 0);
lv_obj_set_size(image, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_obj_center(image);
lv_img_set_src(image, fullpath.c_str());
if (!hasImage()) {
// Failed to load
TRACE("could not load image '%s'", filename.c_str());
lv_obj_del(image);
image = nullptr;
}
setZoom();
} else {
clearSource();
}
}
void StaticImage::clearSource()
{
if (image) lv_obj_del(image);
image = nullptr;
}
bool StaticImage::hasImage() const
{
lv_img_t* img = (lv_img_t*)image;
return img && img->w && img->h;
}
void StaticImage::setZoom()
{
lv_img_t* img = (lv_img_t*)image;
if (img && img->w && img->h) {
uint16_t zw = (width() * 256) / img->w;
uint16_t zh = (height() * 256) / img->h;
lv_img_set_zoom(image, min(zw, zh));
}
}
//-----------------------------------------------------------------------------
// Display image from file system with software scaling
// - uglier but much faster than LVGL scaling
StaticBitmap::StaticBitmap(Window* parent, const rect_t& rect,
const char* filename, LcdFlags bgColor) :
Window(parent, rect)
{
setWindowFlag(NO_FOCUS);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICKABLE);
if (!filename) return;
img = BitmapBuffer::loadBitmap(filename, BMP_ARGB4444);
if (img) {
img->resizeToLVGL(width(), height(), bgColor);
canvas = lv_canvas_create(lvobj);
lv_obj_center(canvas);
lv_canvas_set_buffer(canvas, img->getData(), img->width(), img->height(),
LV_IMG_CF_TRUE_COLOR);
}
}
StaticBitmap::~StaticBitmap()
{
if (img) delete img;
}
bool StaticBitmap::hasImage() const
{
return img && canvas;
}
//-----------------------------------------------------------------------------
StaticLZ4Image::StaticLZ4Image(Window* parent, coord_t x, coord_t y,
const LZ4Bitmap* lz4Bitmap) :
Window(parent, {x, y, lz4Bitmap->width, lz4Bitmap->height},
lv_canvas_create)
{
setWindowFlag(NO_FOCUS);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICKABLE);
// Convert ARGB4444 to LV_IMG_CF_TRUE_COLOR_ALPHA
uint16_t w = lz4Bitmap->width;
uint16_t h = lz4Bitmap->height;
uint32_t pixels = w * h;
uint32_t size = (pixels + 1) & 0xFFFFFFFE;
imgData = (uint8_t*)lv_mem_alloc(size * 3);
uint16_t* decompData = (uint16_t*)(imgData + size);
LZ4_decompress_safe((const char*)lz4Bitmap->data, (char*)decompData,
lz4Bitmap->compressedSize, pixels * sizeof(uint16_t));
uint8_t* dest = imgData;
for (uint32_t i = 0; i < pixels; i += 1) {
uint16_t c = *decompData;
ARGB_SPLIT(c, a, r, g, b);
c = RGB_JOIN(r * 2, g * 4, b * 2);
*dest++ = c & 0xFF;
*dest++ = c >> 8;
*dest++ = (a * 255) / 15;
decompData += 1;
}
lv_canvas_set_buffer(lvobj, imgData, w, h, LV_IMG_CF_TRUE_COLOR_ALPHA);
}
void StaticLZ4Image::deleteLater(bool detach, bool trash)
{
if (!deleted()) {
if (imgData) lv_mem_free(imgData);
imgData = nullptr;
Window::deleteLater(detach, trash);
}
}
+103 -57
View File
@@ -18,76 +18,43 @@
#pragma once
#include "bitmaps.h"
#include "window.h"
class StaticText: public Window
struct LZ4Bitmap;
class BitmapBuffer;
//-----------------------------------------------------------------------------
class StaticText : public Window
{
public:
StaticText(Window *parent, const rect_t &rect, std::string text = "",
WindowFlags windowFlags = 0, LcdFlags textFlags = 0);
public:
StaticText(Window *parent, const rect_t &rect, std::string text = "",
LcdFlags textFlags = COLOR_THEME_PRIMARY1);
#if defined(DEBUG_WINDOWS)
std::string getName() const override;
std::string getName() const override;
#endif
void setText(std::string value);
const std::string& getText() const;
void setText(std::string value);
const std::string &getText() const;
void setBackgroundColor(LcdFlags color);
void setBackgroudOpacity(uint32_t opa);
void setFont(LcdFlags font);
protected:
std::string text;
LcdFlags bgColor = 0;
protected:
std::string text;
};
#define Subtitle(parent, text) StaticText(parent, rect_t{}, text, 0, COLOR_THEME_PRIMARY1 | FONT(BOLD))
#define Subtitle(parent, text) \
StaticText(parent, rect_t{}, text, COLOR_THEME_PRIMARY1 | FONT(BOLD))
class StaticBitmap: public Window
{
public:
StaticBitmap(Window * parent, const rect_t & rect, const BitmapBuffer * mask, LcdFlags color, bool scale = false):
Window(parent, rect),
bitmap(mask),
color(color),
scale(scale)
{
}
#if defined(DEBUG_WINDOWS)
std::string getName() const override
{
return "StaticBitmap";
}
#endif
void paint(BitmapBuffer * dc) override
{
if (bitmap) {
if (color != 0xFFFFFFFF)
dc->drawMask(0, 0, bitmap, color);
else if (scale)
dc->drawScaledBitmap(bitmap, 0, 0, width(), height());
else
dc->drawBitmap((width() - bitmap->width()) / 2, (height() - bitmap->height()) / 2, bitmap);
}
}
protected:
const BitmapBuffer * bitmap = nullptr;
LcdFlags color = 0xFFFFFFFF;
bool scale = false;
};
//-----------------------------------------------------------------------------
class DynamicText : public StaticText
{
public:
DynamicText(Window *parent, const rect_t &rect,
std::function<std::string()> textHandler,
LcdFlags textFlags = 0) :
StaticText(parent, rect, "", 0, textFlags),
LcdFlags textFlags = COLOR_THEME_PRIMARY1) :
StaticText(parent, rect, "", textFlags),
textHandler(std::move(textHandler))
{
}
@@ -107,13 +74,15 @@ class DynamicNumber : public StaticText
{
public:
DynamicNumber(Window *parent, const rect_t &rect,
std::function<T()> numberHandler, LcdFlags textFlags = 0,
std::function<T()> numberHandler,
LcdFlags textFlags = COLOR_THEME_PRIMARY1,
const char *prefix = nullptr, const char *suffix = nullptr) :
StaticText(parent, rect, "", 0, textFlags),
StaticText(parent, rect, "", textFlags),
numberHandler(std::move(numberHandler)),
prefix(prefix),
suffix(suffix)
{
value = this->numberHandler();
updateText();
}
@@ -126,12 +95,14 @@ class DynamicNumber : public StaticText
}
}
void setPrefix(const char * value) {
void setPrefix(const char *value)
{
prefix = value;
updateText();
}
void setSuffix(const char * value) {
void setSuffix(const char *value)
{
suffix = value;
updateText();
}
@@ -144,3 +115,78 @@ class DynamicNumber : public StaticText
void updateText();
};
//-----------------------------------------------------------------------------
class StaticIcon : public Window
{
public:
StaticIcon(Window *parent, coord_t x, coord_t y, EdgeTxIcon icon,
LcdFlags color);
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "StaticIcon"; }
#endif
void setColor(LcdColorIndex color);
void setIcon(EdgeTxIcon icon);
void center(coord_t w, coord_t h);
protected:
LcdColorIndex currentColor;
};
//-----------------------------------------------------------------------------
class StaticImage : public Window
{
public:
StaticImage(Window *parent, const rect_t &rect,
const char *filename = nullptr);
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "StaticImage"; }
#endif
void setZoom();
void setSource(std::string filename);
void clearSource();
bool hasImage() const;
protected:
lv_obj_t *image = nullptr;
};
class StaticBitmap : public Window
{
public:
StaticBitmap(Window *parent, const rect_t &rect,
const char *filename, LcdFlags bgColor);
~StaticBitmap();
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "StaticBitmap"; }
#endif
bool hasImage() const;
protected:
lv_obj_t *canvas = nullptr;
BitmapBuffer *img = nullptr;
};
class StaticLZ4Image : public Window
{
public:
StaticLZ4Image(Window *parent, coord_t x, coord_t y,
const LZ4Bitmap *lz4Bitmap);
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "StaticILZ4mage"; }
#endif
protected:
uint8_t *imgData = nullptr;
void deleteLater(bool detach, bool trash) override;
};
+153 -136
View File
@@ -18,163 +18,185 @@
#include "table.h"
void TableField::event_cb(lv_event_t *e)
#include "themes/etx_lv_theme.h"
static void table_event(const lv_obj_class_t* class_p, lv_event_t* e);
// Table
const lv_style_const_prop_t table_cell_props[] = {
LV_STYLE_CONST_BORDER_WIDTH(1),
LV_STYLE_CONST_BORDER_SIDE(LV_BORDER_SIDE_TOP | LV_BORDER_SIDE_BOTTOM),
LV_STYLE_CONST_PAD_TOP(7), LV_STYLE_CONST_PAD_BOTTOM(7),
LV_STYLE_CONST_PAD_LEFT(4), LV_STYLE_CONST_PAD_RIGHT(4),
LV_STYLE_PROP_INV,
};
LV_STYLE_CONST_MULTI_INIT(table_cell, table_cell_props);
static void table_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj)
{
lv_event_code_t code = lv_event_get_code(e);
if (code == LV_EVENT_VALUE_CHANGED) {
clicked(e);
} else if (code == LV_EVENT_DRAW_PART_BEGIN) {
draw_begin(e);
} else if (code == LV_EVENT_DRAW_PART_END) {
draw_end(e);
}
else if (code == LV_EVENT_DRAW_POST) {
lv_obj_t * obj = lv_event_get_target(e);
bool has_focus = lv_obj_has_state(obj, LV_STATE_FOCUS_KEY);
bool is_edited = lv_group_get_editing((lv_group_t*)lv_obj_get_group(obj));
etx_obj_add_style(obj, styles->pad_zero, LV_PART_MAIN);
if (has_focus && !is_edited) {
lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
etx_scrollbar(obj);
lv_draw_rect_dsc_t rect_dsc;
lv_draw_rect_dsc_init(&rect_dsc);
rect_dsc.bg_opa = LV_OPA_TRANSP;
rect_dsc.bg_img_opa = LV_OPA_TRANSP;
rect_dsc.outline_opa = LV_OPA_TRANSP;
rect_dsc.shadow_opa = LV_OPA_TRANSP;
etx_solid_bg(obj, COLOR_THEME_PRIMARY2_INDEX, LV_PART_ITEMS);
etx_txt_color(obj, COLOR_THEME_PRIMARY1_INDEX, LV_PART_ITEMS);
etx_obj_add_style(obj, table_cell, LV_PART_ITEMS);
etx_obj_add_style(obj, styles->border_color_normal, LV_PART_ITEMS);
rect_dsc.border_color = makeLvColor(COLOR_THEME_FOCUS);
rect_dsc.border_opa = LV_OPA_100;
rect_dsc.border_width = lv_dpx(2);
etx_bg_color(obj, COLOR_THEME_FOCUS_INDEX, LV_PART_ITEMS | LV_STATE_EDITED);
etx_txt_color(obj, COLOR_THEME_PRIMARY2_INDEX, LV_PART_ITEMS | LV_STATE_EDITED);
}
lv_area_t coords;
lv_area_copy(&coords, &obj->coords);
static const lv_obj_class_t table_class = {
.base_class = &lv_table_class,
.constructor_cb = table_constructor,
.destructor_cb = nullptr,
.user_data = nullptr,
.event_cb = table_event,
.width_def = 0,
.height_def = 0,
.editable = LV_OBJ_CLASS_EDITABLE_TRUE,
.group_def = LV_OBJ_CLASS_GROUP_DEF_TRUE,
.instance_size = sizeof(lv_table_t),
};
lv_draw_rect(draw_ctx, &rect_dsc, &coords);
static lv_obj_t* table_create(lv_obj_t* parent)
{
return etx_create(&table_class, parent);
}
static void table_event(const lv_obj_class_t* class_p, lv_event_t* e)
{
lv_obj_t* obj = lv_event_get_target(e);
if (obj) {
TableField* tf = (TableField*)lv_obj_get_user_data(obj);
if (tf) {
lv_event_code_t code = lv_event_get_code(e);
switch (code) {
case LV_EVENT_VALUE_CHANGED: {
uint16_t row;
uint16_t col;
lv_table_get_selected_cell(obj, &row, &col);
if (row != LV_TABLE_CELL_NONE && col != LV_TABLE_CELL_NONE) {
lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
if (indev_type == LV_INDEV_TYPE_ENCODER) {
// Encoder send VALUE_CHANGED when selection changes
tf->adjustScroll();
tf->onSelected(row, col);
} else {
// Otherwise it's a click
if (lv_group_get_editing((lv_group_t*)lv_obj_get_group(obj)) ||
indev_type == LV_INDEV_TYPE_POINTER) {
tf->onPress(row, col);
} else {
tf->onClicked();
}
// Note: VALUE_CHANGED is generated on RELEASED
//
// It must be avoided that CLICKED be generated afterwards
// in case the object has been deleted meanwhile and to
// avoid onClicked() being called.
//
lv_indev_reset(lv_indev_get_act(), nullptr);
}
}
} break;
case LV_EVENT_DRAW_PART_BEGIN: {
lv_obj_draw_part_dsc_t* dsc = lv_event_get_draw_part_dsc(e);
if (dsc->part == LV_PART_ITEMS) {
uint16_t cols = lv_table_get_col_cnt(obj);
if (cols) {
uint16_t row = dsc->id / cols;
uint16_t col = dsc->id % cols;
tf->onDrawBegin(row, col, dsc);
}
}
} break;
case LV_EVENT_DRAW_PART_END: {
lv_obj_draw_part_dsc_t* dsc = lv_event_get_draw_part_dsc(e);
if (dsc->part == LV_PART_ITEMS) {
uint16_t cols = lv_table_get_col_cnt(obj);
if (cols) {
uint16_t row = dsc->id / cols;
uint16_t col = dsc->id % cols;
tf->onDrawEnd(row, col, dsc);
}
}
} break;
case LV_EVENT_DRAW_POST: {
bool has_focus = lv_obj_has_state(obj, LV_STATE_FOCUS_KEY);
bool is_edited =
lv_group_get_editing((lv_group_t*)lv_obj_get_group(obj));
if (has_focus && !is_edited) {
lv_draw_ctx_t* draw_ctx = lv_event_get_draw_ctx(e);
lv_draw_rect_dsc_t rect_dsc;
lv_draw_rect_dsc_init(&rect_dsc);
rect_dsc.bg_opa = LV_OPA_TRANSP;
rect_dsc.bg_img_opa = LV_OPA_TRANSP;
rect_dsc.outline_opa = LV_OPA_TRANSP;
rect_dsc.shadow_opa = LV_OPA_TRANSP;
rect_dsc.border_color = makeLvColor(COLOR_THEME_FOCUS);
rect_dsc.border_opa = LV_OPA_100;
rect_dsc.border_width = lv_dpx(2);
lv_area_t coords;
lv_area_copy(&coords, &obj->coords);
lv_draw_rect(draw_ctx, &rect_dsc, &coords);
}
} break;
case LV_EVENT_RELEASED: {
lv_table_t* table = (lv_table_t*)obj;
/*From lv_table.c: handler for LV_EVENT_RELEASED*/
lv_obj_invalidate(obj);
lv_indev_t* indev = lv_indev_get_act();
lv_obj_t* scroll_obj = lv_indev_get_scroll_obj(indev);
if (table->col_act != LV_TABLE_CELL_NONE &&
table->row_act != LV_TABLE_CELL_NONE && scroll_obj == NULL) {
lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
}
return;
} break;
default:
break;
}
}
}
/*Call the ancestor's event handler*/
lv_obj_event_base(&table_class, e);
}
void TableField::clicked(lv_event_t *e)
TableField::TableField(Window* parent, const rect_t& rect) :
Window(parent, rect, table_create)
{
lv_obj_t* target = lv_event_get_target(e);
if (!target) return;
setWindowFlag(OPAQUE);
TableField* tf = (TableField*)lv_obj_get_user_data(target);
if (!tf) return;
uint16_t row;
uint16_t col;
lv_table_get_selected_cell(target, &row, &col);
if (row == LV_TABLE_CELL_NONE || col == LV_TABLE_CELL_NONE)
return;
lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
if(indev_type == LV_INDEV_TYPE_ENCODER) {
// Encoder send VALUE_CHANGED when selection changes
tf->adjustScroll();
tf->onSelected(row, col);
} else {
// Otherwise it's a click
if (lv_group_get_editing((lv_group_t*)lv_obj_get_group(target))
|| indev_type == LV_INDEV_TYPE_POINTER) {
tf->onPress(row, col);
} else {
tf->onClicked();
}
// Note: VALUE_CHANGED is generated on RELEASED
//
// It must be avoided that CLICKED be generated afterwards
// in case the object has been deleted meanwhile and to
// avoid onClicked() being called.
//
lv_indev_reset(lv_indev_get_act(), nullptr);
}
etx_scrollbar(lvobj);
lv_table_set_col_cnt(lvobj, 1);
}
void TableField::draw_begin(lv_event_t* e)
{
lv_obj_draw_part_dsc_t* dsc = lv_event_get_draw_part_dsc(e);
if (dsc->part != LV_PART_ITEMS) return;
lv_obj_t* table = lv_event_get_target(e);
if (!table) return;
uint16_t cols = lv_table_get_col_cnt(table);
if (cols == 0) return;
TableField* tf = (TableField*)lv_obj_get_user_data(table);
if (!tf) return;
uint16_t row = dsc->id / cols;
uint16_t col = dsc->id % cols;
tf->onDrawBegin(row, col, dsc);
}
void TableField::draw_end(lv_event_t* e)
{
lv_obj_draw_part_dsc_t* dsc = lv_event_get_draw_part_dsc(e);
if (dsc->part != LV_PART_ITEMS) return;
lv_obj_t* table = lv_event_get_target(e);
if (!table) return;
uint16_t cols = lv_table_get_col_cnt(table);
if (cols == 0) return;
TableField* tf = (TableField*)lv_obj_get_user_data(table);
if (!tf) return;
uint16_t row = dsc->id / cols;
uint16_t col = dsc->id % cols;
tf->onDrawEnd(row, col, dsc);
}
TableField::TableField(Window* parent, const rect_t& rect, WindowFlags windowFlags) :
Window(parent, rect, windowFlags, 0, etx_table_create)
{
lv_obj_add_event_cb(lvobj, TableField::event_cb, LV_EVENT_ALL, nullptr);
}
void TableField::setColumnCount(uint16_t cols)
{
lv_table_set_col_cnt(lvobj, cols);
}
uint16_t TableField::getColumnCount() const
{
return lv_table_get_col_cnt(lvobj);
}
void TableField::setRowCount(uint16_t rows)
{
lv_table_set_row_cnt(lvobj, rows);
}
uint16_t TableField::getRowCount() const
{
return lv_table_get_row_cnt(lvobj);
}
uint16_t TableField::getRowCount() const { return lv_table_get_row_cnt(lvobj); }
void TableField::setColumnWidth(uint16_t col, coord_t w)
{
lv_table_set_col_width(lvobj, col, w);
}
coord_t TableField::getColumnWidth(uint16_t col) const
{
return lv_table_get_col_width(lvobj, col);
}
void TableField::select(uint16_t row, uint16_t col, bool force)
{
lv_table_t* table = (lv_table_t*)lvobj;
if (!force && table->row_act == row && table->col_act == row) return;
if (row >= table->row_cnt || col >= table->col_cnt) {
if (row >= table->row_cnt || col >= table->col_cnt) {
table->col_act = LV_TABLE_CELL_NONE;
table->row_act = LV_TABLE_CELL_NONE;
} else {
@@ -218,7 +240,6 @@ void TableField::selectNext(int16_t dir)
if (table->col_act == LV_TABLE_CELL_NONE ||
table->row_act == LV_TABLE_CELL_NONE) {
if (table->col_cnt > 0 && table->row_cnt > 0) {
table->col_act = 0;
table->row_act = 0;
@@ -226,7 +247,6 @@ void TableField::selectNext(int16_t dir)
} else {
table->col_act += dir;
if (table->col_act >= table->col_cnt) {
table->col_act = 0;
table->row_act += dir;
@@ -236,7 +256,6 @@ void TableField::selectNext(int16_t dir)
// wrap around
if (table->col_cnt > 0 && table->row_cnt > 0) {
if (dir < 0) {
table->col_act = table->col_cnt - 1;
table->row_act = table->row_cnt - 1;
@@ -257,11 +276,9 @@ void TableField::onEvent(event_t event)
{
if (event == EVT_ROTARY_RIGHT) {
selectNext(1);
}
else if (event == EVT_ROTARY_LEFT) {
} else if (event == EVT_ROTARY_LEFT) {
selectNext(-1);
}
else {
} else {
Window::onEvent(event);
}
}
}
+7 -16
View File
@@ -18,41 +18,32 @@
#pragma once
#include "form.h"
#include "window.h"
class TableField : public Window
{
public:
TableField(Window* parent, const rect_t& rect,
WindowFlags windowFlags = OPAQUE);
TableField(Window* parent, const rect_t& rect);
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "Table"; }
#endif
void setColumnCount(uint16_t cols);
uint16_t getColumnCount() const;
void setRowCount(uint16_t rows);
uint16_t getRowCount() const;
void setColumnWidth(uint16_t col, coord_t w);
coord_t getColumnWidth(uint16_t col) const;
void select(uint16_t row, uint16_t col, bool force = false);
virtual void onPress(uint16_t row, uint16_t col) {}
protected:
void adjustScroll();
void selectNext(int16_t dir);
void onEvent(event_t event) override;
virtual void onSelected(uint16_t row, uint16_t col) {}
virtual void onDrawBegin(uint16_t row, uint16_t col, lv_obj_draw_part_dsc_t* dsc) {}
virtual void onDrawEnd(uint16_t row, uint16_t col, lv_obj_draw_part_dsc_t* dsc) {}
static void event_cb(lv_event_t *e);
static void clicked(lv_event_t *e);
static void draw_begin(lv_event_t *e);
static void draw_end(lv_event_t *e);
void adjustScroll();
void selectNext(int16_t dir);
protected:
void onEvent(event_t event) override;
};
+40 -12
View File
@@ -17,32 +17,49 @@
*/
#include "textedit.h"
#include "font.h"
#include "keyboard_text.h"
#include "myeeprom.h"
#include "storage/storage.h"
#include "themes/etx_lv_theme.h"
#if defined(HARDWARE_KEYS)
#include "menu.h"
#endif
TextEdit::TextEdit(Window *parent, const rect_t &rect, char *value,
uint8_t length, LcdFlags windowFlags) :
FormField(parent, rect, windowFlags, 0,
etx_text_edit_create),
value(value),
length(length)
TextEdit::TextEdit(Window* parent, const rect_t& rect, char* value,
uint8_t length) :
FormField(rect, 0), value(value), length(length)
{
lv_textarea_set_placeholder_text(lvobj, "---");
lv_textarea_set_max_length(lvobj, length);
lv_obj_enable_style_refresh(false);
// Workaround for performance issues with lv_textarea - create on top layer
// not this window then reparent to this window after setup finished
this->parent = parent;
lvobj = lv_textarea_create(lv_layer_top());
// Do this first - before any styles are applied, otherwise it is very slow
update();
etx_textarea_style(lvobj);
lv_textarea_set_max_length(lvobj, length);
lv_textarea_set_placeholder_text(lvobj, "---");
lv_obj_set_parent(lvobj, parent->getLvObj());
setupLVGL();
if (rect.w == 0) setWidth(100);
lv_obj_enable_style_refresh(true);
lv_obj_refresh_style(lvobj, LV_PART_ANY, LV_STYLE_PROP_ANY);
}
void TextEdit::update()
{
// value may not be null-terminated
std::string txt(value, length);
lv_textarea_set_text(lvobj, txt.c_str());
lv_textarea_set_text(lvobj, txt.c_str());
}
void TextEdit::trim()
@@ -72,7 +89,18 @@ void TextEdit::changeEnd(bool forceChanged)
}
}
void TextEdit::onClicked()
void TextEdit::onClicked() { TextKeyboard::show(this); }
ModelTextEdit::ModelTextEdit(Window* parent, const rect_t& rect, char* value,
uint8_t length) :
TextEdit(parent, rect, value, length)
{
TextKeyboard::show(this);
setChangeHandler([]() { storageDirty(EE_MODEL); });
}
RadioTextEdit::RadioTextEdit(Window* parent, const rect_t& rect, char* value,
uint8_t length) :
TextEdit(parent, rect, value, length)
{
setChangeHandler([]() { storageDirty(EE_GENERAL); });
}
+16 -4
View File
@@ -22,10 +22,8 @@
class TextEdit : public FormField
{
public:
TextEdit(Window* parent, const rect_t& rect, char* value, uint8_t length,
LcdFlags windowFlags = 0);
TextEdit(Window* parent, const rect_t& rect, char* value, uint8_t length);
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "TextEdit"; }
@@ -38,7 +36,7 @@ class TextEdit : public FormField
protected:
static void event_cb(lv_event_t* e);
char* value;
uint8_t length;
@@ -47,3 +45,17 @@ class TextEdit : public FormField
void changeEnd(bool forceChanged = false) override;
void onClicked() override;
};
class ModelTextEdit : public TextEdit
{
public:
ModelTextEdit(Window* parent, const rect_t& rect, char* value,
uint8_t length);
};
class RadioTextEdit : public TextEdit
{
public:
RadioTextEdit(Window* parent, const rect_t& rect, char* value,
uint8_t length);
};
+57 -8
View File
@@ -17,8 +17,55 @@
*/
#include "toggleswitch.h"
#include "theme.h"
#include "theme.h"
#include "themes/etx_lv_theme.h"
// Animation
LV_STYLE_CONST_SINGLE_INIT(anim_fast, LV_STYLE_ANIM_TIME, 120);
// Toggle switch
const lv_style_const_prop_t switch_knob_props[] = {
LV_STYLE_CONST_PAD_TOP(-4), LV_STYLE_CONST_PAD_BOTTOM(-4),
LV_STYLE_CONST_PAD_LEFT(-4), LV_STYLE_CONST_PAD_RIGHT(-4),
LV_STYLE_PROP_INV,
};
LV_STYLE_CONST_MULTI_INIT(switch_knob, switch_knob_props);
static void switch_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj)
{
etx_std_style(obj, LV_PART_MAIN, PAD_ZERO);
etx_obj_add_style(obj, styles->circle, LV_PART_MAIN);
etx_obj_add_style(obj, anim_fast, LV_PART_MAIN);
etx_std_style(obj, LV_PART_INDICATOR, PAD_ZERO);
etx_obj_add_style(obj, styles->circle, LV_PART_INDICATOR);
etx_obj_add_style(obj, styles->bg_opacity_cover, LV_PART_KNOB);
etx_obj_add_style(obj, styles->circle, LV_PART_KNOB);
etx_obj_add_style(obj, switch_knob, LV_PART_KNOB);
etx_bg_color(obj, COLOR_THEME_SECONDARY1_INDEX, LV_PART_KNOB);
etx_obj_add_style(obj, styles->disabled, LV_PART_KNOB | LV_STATE_DISABLED);
}
static const lv_obj_class_t switch_class = {
.base_class = &lv_switch_class,
.constructor_cb = switch_constructor,
.destructor_cb = nullptr,
.user_data = nullptr,
.event_cb = nullptr,
.width_def = 0,
.height_def = 32,
.editable = LV_OBJ_CLASS_EDITABLE_INHERIT,
.group_def = LV_OBJ_CLASS_GROUP_DEF_INHERIT,
.instance_size = sizeof(lv_switch_t),
};
static lv_obj_t* switch_create(lv_obj_t* parent)
{
return etx_create(&switch_class, parent);
}
static void toggleswitch_event_handler(lv_event_t* e)
{
@@ -29,23 +76,25 @@ 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, WindowFlags flags) :
FormField(parent, rect, flags, 0, etx_switch_create),
std::function<uint8_t()> getValue,
std::function<void(uint8_t)> setValue) :
FormField(parent, rect, 0, switch_create),
_getValue(std::move(getValue)),
_setValue(std::move(setValue))
{
update();
lv_obj_add_event_cb(lvobj, toggleswitch_event_handler,
LV_EVENT_VALUE_CHANGED, this);
lv_obj_add_event_cb(lvobj, toggleswitch_event_handler, LV_EVENT_VALUE_CHANGED,
this);
}
void ToggleSwitch::update() const
{
if (!_getValue) return;
if (_getValue()) lv_obj_add_state(lvobj, LV_STATE_CHECKED);
else lv_obj_clear_state(lvobj, LV_STATE_CHECKED);
if (_getValue())
lv_obj_add_state(lvobj, LV_STATE_CHECKED);
else
lv_obj_clear_state(lvobj, LV_STATE_CHECKED);
}
void ToggleSwitch::onClicked()
+1 -6
View File
@@ -18,7 +18,6 @@
#pragma once
#include <utility>
#include "form.h"
class ToggleSwitch : public FormField
@@ -26,7 +25,7 @@ class ToggleSwitch : public FormField
public:
ToggleSwitch(Window* parent, const rect_t& rect,
std::function<uint8_t()> getValue,
std::function<void(uint8_t)> setValue, WindowFlags flags = 0);
std::function<void(uint8_t)> setValue);
#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "ToggleSwitch"; }
@@ -34,9 +33,6 @@ class ToggleSwitch : public FormField
void onClicked() override;
const char* getLabel() const { return label.c_str(); }
void setLabel(std::string newLabel) { label = std::move(newLabel); }
uint8_t getValue() const { return _getValue(); }
void setValue(uint8_t value) { _setValue(value); }
@@ -53,7 +49,6 @@ class ToggleSwitch : public FormField
void update() const;
protected:
std::string label;
std::function<uint8_t()> _getValue;
std::function<void(uint8_t)> _setValue;
};
@@ -1,50 +0,0 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include <lvgl/lvgl.h>
lv_obj_t* window_create(lv_obj_t* parent);
lv_obj_t* etx_form_window_create(lv_obj_t* parent);
lv_obj_t* etx_text_edit_create(lv_obj_t* parent);
lv_obj_t* etx_number_edit_create(lv_obj_t* parent);
lv_obj_t* etx_table_create(lv_obj_t* parent);
lv_obj_t* etx_keyboard_create(lv_obj_t* parent);
lv_obj_t* etx_switch_create(lv_obj_t* parent);
lv_obj_t* etx_slider_create(lv_obj_t* parent);
lv_obj_t* etx_btnmatrix_create(lv_obj_t* parent);
lv_obj_t* etx_button_create(lv_obj_t* parent);
lv_obj_t* etx_quick_button_create(lv_obj_t* parent);
lv_obj_t* etx_menu_button_create(lv_obj_t* parent);
lv_obj_t* etx_choice_create(lv_obj_t* parent);
lv_obj_t* etx_modal_create(lv_obj_t* parent);
lv_obj_t* etx_modal_dialog_create(lv_obj_t* parent);
lv_obj_t* etx_modal_content_create(lv_obj_t* parent);
lv_obj_t* etx_modal_title_create(lv_obj_t* parent);
lv_obj_t* etx_bar_create(lv_obj_t* parent);
lv_obj_t* etx_checkbox_create(lv_obj_t* parent);
lv_obj_t* etx_bubble_popup_create(lv_obj_t* parent);
lv_obj_t* input_mix_line_create(lv_obj_t* parent);
lv_obj_t* input_mix_group_create(lv_obj_t* parent);
+191 -251
View File
@@ -17,195 +17,109 @@
*/
#include "window.h"
#include "touch.h"
#include "mainwindow.h"
#include "button.h"
#include "form.h"
#include "themes/etx_lv_theme.h"
std::list<Window *> Window::trash;
extern lv_obj_t *virtual_kb;
const lv_obj_class_t window_base_class = {
.base_class = &lv_obj_class,
.constructor_cb = nullptr,
.destructor_cb = nullptr,
.user_data = nullptr,
.event_cb = nullptr,
.width_def = LV_DPI_DEF,
.height_def = LV_DPI_DEF,
.editable = LV_OBJ_CLASS_EDITABLE_FALSE,
.group_def = LV_OBJ_CLASS_GROUP_DEF_FALSE,
.instance_size = sizeof(lv_obj_t)};
extern "C" void window_event_cb(lv_event_t * e)
lv_obj_t *window_create(lv_obj_t *parent)
{
return etx_create(&window_base_class, parent);
}
void Window::window_event_cb(lv_event_t *e)
{
lv_obj_t *target = lv_event_get_target(e);
lv_event_code_t code = lv_event_get_code(e);
if (code == LV_EVENT_SCROLL_BEGIN) {
TRACE("SCROLL_BEGIN");
} else if (code == LV_EVENT_SCROLL_END) {
TRACE("SCROLL_END");
}
Window *window = (Window *)lv_obj_get_user_data(target);
if (window) {
if (code == LV_EVENT_DELETE || window->deleted()) return;
Window* window = (Window *)lv_obj_get_user_data(target);
if (!window) return;
switch (code) {
case LV_EVENT_SCROLL: {
// exclude pointer based scrolling (only focus scrolling)
if (!lv_obj_is_scrolling(target)) {
lv_point_t *p = (lv_point_t *)lv_event_get_param(e);
lv_coord_t scroll_bottom = lv_obj_get_scroll_bottom(target);
if(code == LV_EVENT_DELETE || window->deleted()) return;
lv_coord_t scroll_y = lv_obj_get_scroll_y(target);
TRACE("SCROLL[x=%d;y=%d;top=%d;bottom=%d]", p->x, p->y, scroll_y,
scroll_bottom);
if (code == LV_EVENT_DRAW_MAIN) {
lv_draw_ctx_t* draw_ctx = lv_event_get_draw_ctx(e);
lv_area_t a, clipping, obj_coords;
lv_area_copy(&a, draw_ctx->buf_area);
lv_area_copy(&clipping, draw_ctx->clip_area);
lv_obj_get_coords(target, &obj_coords);
auto w = a.x2 - a.x1 + 1;
auto h = a.y2 - a.y1 + 1;
TRACE_WINDOWS("DRAW_MAIN %s", window->getWindowDebugString().c_str());
BitmapBuffer buf = {BMP_RGB565, (uint16_t)w, (uint16_t)h,
(uint16_t *)draw_ctx->buf};
buf.setDrawCtx(draw_ctx);
buf.setOffset(obj_coords.x1 - a.x1, obj_coords.y1 - a.y1);
buf.setClippingRect(clipping.x1 - a.x1, clipping.x2 + 1 - a.x1,
clipping.y1 - a.y1, clipping.y2 + 1 - a.y1);
window->paint(&buf);
}
#if defined(HARDWARE_TOUCH)
else if (code == LV_EVENT_PRESSED || code == LV_EVENT_RELEASED) {
TRACE_WINDOWS("PRESSED: %s", window->getWindowDebugString().c_str());
lv_indev_t *click_source = (lv_indev_t *)lv_event_get_param(e);
lv_area_t obj_coords;
lv_obj_get_coords(target, &obj_coords);
lv_point_t point_act;
lv_indev_get_point(click_source, &point_act);
// Ignore event from keypad
if (point_act.x < 0 || point_act.y < 0) return;
lv_point_t rel_pos;
rel_pos.x = point_act.x - obj_coords.x1;
rel_pos.y = point_act.y - obj_coords.y1;
if (code == LV_EVENT_PRESSED) {
TRACE("PRESSED[%d|%d]", rel_pos.x, rel_pos.y);
window->onTouchStart(rel_pos.x, rel_pos.y);
} else {
lv_point_t vect_act;
lv_indev_get_vect(click_source, &vect_act);
TRACE("RELEASED[%d|%d] vect[%d|%d]", rel_pos.x, rel_pos.y, vect_act.x,
vect_act.y, lv_indev_get_scroll_dir(click_source));
if (vect_act.x != 0 || vect_act.y != 0) return;
window->onTouchEnd(rel_pos.x, rel_pos.y);
if (scroll_y <= 45 && p->y > 0) {
lv_obj_scroll_by(target, 0, scroll_y, LV_ANIM_OFF);
} else if (scroll_bottom <= 16 && p->y < 0) {
lv_obj_scroll_by(target, 0, -scroll_bottom, LV_ANIM_OFF);
}
}
} break;
case LV_EVENT_CLICKED:
TRACE("CLICKED[%p]", window);
window->onClicked();
break;
case LV_EVENT_CANCEL:
TRACE("CANCEL[%p]", window);
window->onCancel();
break;
case LV_EVENT_FOCUSED:
if (window->focusHandler) window->focusHandler(true);
break;
case LV_EVENT_DEFOCUSED:
if (window->focusHandler) window->focusHandler(false);
break;
default:
break;
}
}
#endif
else if (code == LV_EVENT_SCROLL) {
// exclude pointer based scrolling (only focus scrolling)
if (!lv_obj_is_scrolling(target)) {
lv_point_t* p = (lv_point_t*)lv_event_get_param(e);
lv_coord_t scroll_bottom = lv_obj_get_scroll_bottom(target);
lv_coord_t scroll_y = lv_obj_get_scroll_y(target);
TRACE("SCROLL[x=%d;y=%d;top=%d;bottom=%d]", p->x, p->y,
scroll_y, scroll_bottom);
if (scroll_y <= 45 && p->y > 0) {
lv_obj_scroll_by(target, 0, scroll_y, LV_ANIM_OFF);
} else if (scroll_bottom <= 16 && p->y < 0) {
lv_obj_scroll_by(target, 0, -scroll_bottom, LV_ANIM_OFF);
}
}
}
else if (code == LV_EVENT_CLICKED) {
TRACE("CLICKED[%p]", window);
window->onClicked();
}
else if (code == LV_EVENT_CANCEL) {
TRACE("CANCEL[%p]", window);
window->onCancel();
} else if (code == LV_EVENT_FOCUSED) {
if(window->focusHandler)
window->focusHandler(true);
} else if (code == LV_EVENT_DEFOCUSED) {
if(window->focusHandler)
window->focusHandler(false);
}
}
Window::Window(Window *parent, const rect_t &rect, WindowFlags windowFlags,
LcdFlags textFlags, LvglCreate objConstruct) :
rect(rect),
parent(parent),
windowFlags(windowFlags),
textFlags(textFlags)
// Constructor to allow lvobj to be created separately - used by NumberEdit and
// TextEdit
Window::Window(const rect_t &rect) : rect(rect), parent(nullptr)
{
lv_obj_t* lv_parent = nullptr;
if (parent) lv_parent = parent->lvobj;
lvobj = nullptr;
}
if (objConstruct != nullptr && objConstruct != window_create) {
lvobj = objConstruct(lv_parent);
lv_obj_add_event_cb(lvobj, window_event_cb, LV_EVENT_ALL, nullptr);
} else {
lvobj = window_create(lv_parent);
}
Window::Window(Window *parent, const rect_t &rect, LvglCreate objConstruct) :
rect(rect), parent(parent)
{
lv_obj_t *lv_parent = parent ? parent->lvobj : nullptr;
if (objConstruct == nullptr) objConstruct = window_create;
lvobj = objConstruct(lv_parent);
Window::setupLVGL();
}
void Window::setupLVGL()
{
lv_obj_set_user_data(lvobj, this);
lv_obj_add_event_cb(lvobj, Window::window_event_cb, LV_EVENT_ALL, nullptr);
if (rect.x || rect.y)
lv_obj_set_pos(lvobj, rect.x, rect.y);
if (rect.w)
lv_obj_set_width(lvobj, rect.w);
if (rect.h)
lv_obj_set_height(lvobj, rect.h);
if (rect.x || rect.y) lv_obj_set_pos(lvobj, rect.x, rect.y);
if (rect.w) lv_obj_set_width(lvobj, rect.w);
if (rect.h) lv_obj_set_height(lvobj, rect.h);
if (windowFlags & OPAQUE) {
lv_obj_set_style_bg_opa(lvobj, LV_OPA_MAX, LV_PART_MAIN);
}
lv_obj_set_scrollbar_mode(lvobj, LV_SCROLLBAR_MODE_OFF);
// lv_obj_set_scrollbar_mode(lvobj, LV_SCROLLBAR_MODE_OFF);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_SCROLL_ELASTIC);
if (parent) {
parent->addChild(this);
if (!(windowFlags & TRANSPARENT)) {
invalidate();
}
}
// honor the no focus flag of libopenui
if (this->windowFlags & NO_FOCUS) {
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
}
}
Window::Window(Window *parent, lv_obj_t* lvobj)
: rect(nullRect),
parent(parent),
lvobj(lvobj)
{
lv_obj_set_user_data(lvobj, this);
if (windowFlags & OPAQUE) {
lv_obj_set_style_bg_opa(lvobj, LV_OPA_MAX, LV_PART_MAIN);
}
lv_obj_set_scrollbar_mode(lvobj, LV_SCROLLBAR_MODE_OFF);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_SCROLL_ELASTIC);
lv_obj_add_event_cb(lvobj, window_event_cb, LV_EVENT_ALL, this);
if (parent) {
parent->addChild(this);
if (!(windowFlags & TRANSPARENT)) {
invalidate();
}
}
// honor the no focus flag of libopenui
if (this->windowFlags & NO_FOCUS) {
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
}
}
@@ -257,20 +171,19 @@ Window *Window::getFullScreenWindow()
return nullptr;
}
void Window::setWindowFlags(WindowFlags flags)
void Window::setWindowFlag(WindowFlags flag)
{
// TODO: check if LVGL flags need updating...
windowFlags = flags;
windowFlags |= flag;
// honor the no focus flag of libopenui
if (this->windowFlags & NO_FOCUS)
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
}
void Window::setTextFlags(LcdFlags flags)
{
textFlags = flags;
if (!lvobj) return;
void Window::clearWindowFlag(WindowFlags flag) { windowFlags &= ~flag; }
// lv integration for colors
lv_obj_set_style_text_color(lvobj, makeLvColor(textFlags), LV_PART_MAIN);
}
void Window::setTextFlag(LcdFlags flag) { textFlags |= flag; }
void Window::clearTextFlag(LcdFlags flag) { textFlags &= ~flag; }
void Window::attach(Window *newParent)
{
@@ -291,13 +204,16 @@ void Window::detach()
void Window::deleteLater(bool detach, bool trash)
{
if (_deleted)
return;
if (_deleted) return;
TRACE_WINDOWS("Delete %p %s", this, getWindowDebugString().c_str());
_deleted = true;
if (closeHandler) {
closeHandler();
}
if (detach)
this->detach();
else
@@ -307,10 +223,6 @@ void Window::deleteLater(bool detach, bool trash)
Window::trash.push_back(this);
}
if (closeHandler) {
closeHandler();
}
deleteChildren();
if (lvobj != nullptr) {
@@ -323,7 +235,7 @@ void Window::deleteLater(bool detach, bool trash)
void Window::clear()
{
deleteChildren();
if(lvobj != nullptr) {
if (lvobj != nullptr) {
lv_obj_clean(lvobj);
}
invalidate();
@@ -333,7 +245,7 @@ void Window::deleteChildren()
{
// prevent LVGL refocus while mass-deleting
// inhibit_focus = true;
for (auto window: children) {
for (auto window : children) {
window->deleteLater(false);
}
// inhibit_focus = false;
@@ -345,35 +257,23 @@ bool Window::hasFocus() const
return lvobj && lv_obj_has_state(lvobj, LV_STATE_FOCUSED);
}
void Window::padLeft(coord_t pad)
{
lv_obj_set_style_pad_left(lvobj, pad, 0);
}
void Window::padLeft(coord_t pad) { lv_obj_set_style_pad_left(lvobj, pad, 0); }
void Window::padRight(coord_t pad)
{
lv_obj_set_style_pad_right(lvobj, pad, 0);
}
void Window::padTop(coord_t pad)
{
lv_obj_set_style_pad_top(lvobj, pad, 0);
}
void Window::padTop(coord_t pad) { lv_obj_set_style_pad_top(lvobj, pad, 0); }
void Window::padBottom(coord_t pad)
{
lv_obj_set_style_pad_bottom(lvobj, pad, 0);
}
void Window::padAll(coord_t pad)
{
lv_obj_set_style_pad_all(lvobj, pad, 0);
}
void Window::padAll(PaddingSize pad) { etx_padding(lvobj, pad, LV_PART_MAIN); }
void Window::padRow(coord_t pad)
{
lv_obj_set_style_pad_row(lvobj, pad, 0);
}
void Window::padRow(coord_t pad) { lv_obj_set_style_pad_row(lvobj, pad, 0); }
void Window::padColumn(coord_t pad)
{
@@ -382,68 +282,44 @@ void Window::padColumn(coord_t pad)
void Window::bringToTop()
{
attach(parent); // does a detach + attach
attach(parent); // does a detach + attach
if (lvobj && lv_obj_get_parent(lvobj)) lv_obj_move_foreground(lvobj);
}
void Window::checkEvents()
{
auto copy = children;
for (auto child: copy) {
for (auto child : copy) {
if (!child->deleted()) {
child->checkEvents();
}
}
if (windowFlags & REFRESH_ALWAYS) {
invalidate();
}
}
void Window::onEvent(event_t event)
{
TRACE_WINDOWS("%s received event 0x%X", Window::getWindowDebugString("Window").c_str(), event);
if (parent) { parent->onEvent(event); }
TRACE_WINDOWS("%s received event 0x%X",
Window::getWindowDebugString("Window").c_str(), event);
if (parent) {
parent->onEvent(event);
}
}
void Window::onClicked()
{
if (parent && !(windowFlags & OPAQUE)) { parent->onClicked(); }
if (parent && !(windowFlags & OPAQUE)) {
parent->onClicked();
}
}
void Window::onCancel()
{
if (parent) { parent->onCancel(); }
if (parent) {
parent->onCancel();
}
}
#if defined(HARDWARE_TOUCH)
bool Window::onTouchStart(coord_t x, coord_t y)
{
if (parent && !(windowFlags & OPAQUE)) return parent->onTouchStart(x, y);
return true;
}
bool Window::onTouchEnd(coord_t x, coord_t y)
{
if (parent && !(windowFlags & OPAQUE)) return parent->onTouchEnd(x, y);
return true;
}
#endif
void Window::updateSize()
{
lv_obj_set_height(lvobj, LV_SIZE_CONTENT);
lv_obj_update_layout(lvobj);
rect.x = lv_obj_get_x(lvobj);
rect.y = lv_obj_get_y(lvobj);
rect.w = lv_obj_get_width(lvobj);
rect.h = lv_obj_get_height(lvobj);
invalidate();
}
void Window::addChild(Window* window)
void Window::addChild(Window *window)
{
auto lv_parent = lv_obj_get_parent(window->lvobj);
if (lv_parent && (lv_parent != lvobj)) {
@@ -453,17 +329,68 @@ void Window::addChild(Window* window)
children.push_back(window);
}
void Window::removeChild(Window* window)
void Window::removeChild(Window *window)
{
children.remove(window);
invalidate();
}
void Window::invalidate(const rect_t & rect)
void Window::invalidate()
{
if (lvobj) lv_obj_invalidate(lvobj);
}
void Window::setFlexLayout(lv_flex_flow_t flow, lv_coord_t padding,
coord_t width, coord_t height)
{
lv_obj_set_flex_flow(lvobj, flow);
if (_LV_FLEX_COLUMN & flow) {
lv_obj_set_style_pad_row(lvobj, padding, LV_PART_MAIN);
} else {
lv_obj_set_style_pad_column(lvobj, padding, LV_PART_MAIN);
}
lv_obj_set_width(lvobj, width);
lv_obj_set_height(lvobj, height);
}
FormLine *Window::newLine(FlexGridLayout &layout)
{
return new FormLine(this, layout);
}
void Window::show(bool visible)
{
if (lv_obj_has_flag(lvobj, LV_OBJ_FLAG_HIDDEN) == visible) {
if (visible)
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_HIDDEN);
else
lv_obj_add_flag(lvobj, LV_OBJ_FLAG_HIDDEN);
}
}
void Window::enable(bool enabled)
{
if (lv_obj_has_state(lvobj, LV_STATE_DISABLED) == enabled) {
if (enabled)
lv_obj_clear_state(lvobj, LV_STATE_DISABLED);
else
lv_obj_add_state(lvobj, LV_STATE_DISABLED);
}
}
#if defined(HARDWARE_TOUCH)
void Window::addBackButton()
{
new ButtonBase(
this, {0, 0, MENU_HEADER_HEIGHT, MENU_HEADER_HEIGHT},
[=]() -> uint8_t {
onCancel();
return 0;
},
window_create);
}
#endif
void NavWindow::onEvent(event_t event)
{
switch (event) {
@@ -482,7 +409,7 @@ void NavWindow::onEvent(event_t event)
break;
case EVT_KEY_LONG(KEY_SYS):
killEvents(KEY_SYS);
killEvents(event);
onLongPressSYS();
break;
@@ -491,33 +418,46 @@ void NavWindow::onEvent(event_t event)
break;
case EVT_KEY_LONG(KEY_TELE):
killEvents(KEY_TELE);
killEvents(event);
onLongPressTELE();
break;
#if defined(KEYS_GPIO_REG_PAGEUP) || defined(USE_HATS_AS_KEYS)
case EVT_KEY_FIRST(KEY_PAGEDN):
#else
case EVT_KEY_BREAK(KEY_PAGEDN):
#endif
killEvents(event);
onPressPGDN();
break;
// TODO: these need to go away!
// -> board code should map the keys as required
#if defined(KEYS_GPIO_REG_PAGEUP) || defined(USE_HATS_AS_KEYS)
case EVT_KEY_FIRST(KEY_PAGEUP):
// Radios with both PGUP and PGDN buttons
case EVT_KEY_BREAK(KEY_PAGEUP):
onPressPGUP();
break;
case EVT_KEY_LONG(KEY_PAGEDN):
killEvents(event);
onLongPressPGDN();
break;
case EVT_KEY_LONG(KEY_PAGEUP):
killEvents(event);
onLongPressPGUP();
break;
#else
// Radios witb only a single PGUP/DN button
case EVT_KEY_LONG(KEY_PAGEDN):
#endif
killEvents(event);
onPressPGUP();
break;
#endif
#endif
default:
if (bubbleEvents())
Window::onEvent(event);
if (bubbleEvents()) Window::onEvent(event);
}
}
NavWindow::NavWindow(Window *parent, const rect_t &rect,
LvglCreate objConstruct) :
Window(parent, rect, objConstruct)
{
setWindowFlag(OPAQUE);
}
+52 -41
View File
@@ -18,46 +18,44 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <functional>
#include <list>
#include <string>
#include <utility>
#include "LvglWrapper.h"
#include "bitmapbuffer.h"
#include "libopenui_defines.h"
#include "opentx_helpers.h"
#include "widgets/etx_obj_create.h"
typedef uint32_t WindowFlags;
#if !defined(_GNUC_)
#undef OPAQUE
#undef TRANSPARENT
#endif
enum PaddingSize {
PAD_ZERO = 0,
PAD_TINY = 2,
PAD_SMALL = 4,
PAD_MEDIUM = 6,
PAD_LARGE = 8
};
constexpr WindowFlags OPAQUE = 1u << 0u;
constexpr WindowFlags TRANSPARENT = 1u << 1u;
constexpr WindowFlags NO_FOCUS = 1u << 2u;
constexpr WindowFlags REFRESH_ALWAYS = 1u << 3u;
constexpr WindowFlags WINDOW_FLAGS_LAST = REFRESH_ALWAYS;
constexpr WindowFlags NO_FOCUS = 1u << 1u;
typedef lv_obj_t *(*LvglCreate)(lv_obj_t *);
extern "C" void window_event_cb(lv_event_t *e);
class FlexGridLayout;
class FormLine;
class Window
{
friend void window_event_cb(lv_event_t *e);
public:
Window(Window *parent, const rect_t &rect, WindowFlags windowFlags = 0,
LcdFlags textFlags = 0, LvglCreate objConstruct = nullptr);
Window(const rect_t &rect);
Window(Window *parent, const rect_t &rect, LvglCreate objConstruct = nullptr);
Window(Window *parent, lv_obj_t *lvobj);
virtual void setupLVGL();
virtual ~Window();
@@ -72,11 +70,12 @@ class Window
Window *getFullScreenWindow();
WindowFlags getWindowFlags() const { return windowFlags; }
void setWindowFlags(WindowFlags flags);
void setWindowFlag(WindowFlags flag);
void clearWindowFlag(WindowFlags flag);
bool hasWindowFlag(WindowFlags flag) const { return windowFlags & flag; }
LcdFlags getTextFlags() const { return textFlags; }
void setTextFlags(LcdFlags flags);
void setTextFlag(LcdFlags flag);
void clearTextFlag(LcdFlags flag);
typedef std::function<void()> CloseHandler;
void setCloseHandler(CloseHandler h) { closeHandler = std::move(h); }
@@ -111,18 +110,26 @@ class Window
lv_obj_set_height(lvobj, rect.h);
}
void setLeft(coord_t x)
{
rect.x = x;
lv_obj_set_pos(lvobj, rect.x, rect.y);
}
void setTop(coord_t y)
{
rect.y = y;
lv_obj_set_pos(lvobj, rect.x, rect.y);
}
void setSize(coord_t w, coord_t h)
{
rect.w = w;
rect.h = h;
lv_obj_set_size(lvobj, w, h);
}
void setPos(coord_t x, coord_t y)
{
rect.x = x;
rect.y = y;
lv_obj_set_pos(lvobj, x, y);
}
coord_t left() const { return rect.x; }
coord_t right() const { return rect.x + rect.w; }
@@ -141,7 +148,7 @@ class Window
void padRight(coord_t pad);
void padTop(coord_t pad);
void padBottom(coord_t pad);
void padAll(coord_t pad);
void padAll(PaddingSize pad);
void padRow(coord_t pad);
void padColumn(coord_t pad);
@@ -150,9 +157,7 @@ class Window
virtual void onClicked();
virtual void onCancel();
virtual void updateSize();
void invalidate() { invalidate({0, 0, rect.w, rect.h}); }
void invalidate();
void bringToTop();
@@ -164,11 +169,8 @@ class Window
bool deleted() const { return _deleted; }
virtual void paint(BitmapBuffer *) {}
#if defined(HARDWARE_TOUCH)
virtual bool onTouchStart(coord_t x, coord_t y);
virtual bool onTouchEnd(coord_t x, coord_t y);
void addBackButton();
#endif
inline lv_obj_t *getLvObj() { return lvobj; }
@@ -178,6 +180,16 @@ class Window
virtual bool isBubblePopup() { return false; }
void setFlexLayout(lv_flex_flow_t flow = LV_FLEX_FLOW_COLUMN,
lv_coord_t padding = 2, coord_t width = LV_PCT(100),
coord_t height = LV_SIZE_CONTENT);
FormLine *newLine(FlexGridLayout &layout);
virtual void show(bool visible = true);
void hide() { show(false); }
void enable(bool enabled = true);
void disable() { enable(false); }
protected:
static std::list<Window *> trash;
@@ -201,17 +213,14 @@ class Window
virtual void addChild(Window *window);
void removeChild(Window *window);
virtual void invalidate(const rect_t &rect);
static void window_event_cb(lv_event_t *e);
};
class NavWindow : public Window
{
public:
NavWindow(Window *parent, const rect_t &rect, WindowFlags windowFlags = 0,
LcdFlags textFlags = 0, LvglCreate objConstruct = nullptr) :
Window(parent, rect, windowFlags, textFlags, objConstruct)
{
}
NavWindow(Window *parent, const rect_t &rect,
LvglCreate objConstruct = nullptr);
protected:
#if defined(HARDWARE_KEYS)
@@ -223,6 +232,8 @@ class NavWindow : public Window
virtual void onLongPressTELE() {}
virtual void onPressPGUP() {}
virtual void onPressPGDN() {}
virtual void onLongPressPGUP() {}
virtual void onLongPressPGDN() {}
#endif
virtual bool bubbleEvents() { return true; }
void onEvent(event_t event) override;