feat(lua): support for using LVGL objects on colorlcd (#4887)

This commit is contained in:
philmoz
2024-06-13 13:16:07 +10:00
committed by GitHub
parent 22128df1cd
commit b4e33fc439
30 changed files with 2257 additions and 123 deletions
+1
View File
@@ -134,6 +134,7 @@ if(LUA)
lua/lua_widget.cpp
lua/lua_widget_factory.cpp
lua/widgets.cpp
lua/lua_lvgl_widget.cpp
)
endif()
@@ -68,9 +68,6 @@ void Layout::adjustLayout()
// Save settings
decorationSettings = checkSettings;
// Set visible decoration
show();
}
void Layout::show(bool visible)
+1 -2
View File
@@ -142,7 +142,7 @@ class ModelButton : public Button
if (modelLayouts[layout].hasImage) {
if (modelCell->modelBitmap[0]) {
GET_FILENAME(filename, BITMAPS_PATH, modelCell->modelBitmap, "");
auto bitmap = new StaticImage(this, {PAD_TINY, PAD_TINY, w, h}, filename, true);
auto bitmap = new StaticImage(this, {PAD_TINY, PAD_TINY, w, h}, filename, false, true);
lv_obj_move_background(bitmap->getLvObj());
bitmap->show(bitmap->hasImage());
if (bitmap->hasImage()) {
@@ -626,7 +626,6 @@ void ModelLabelsWindow::newModel()
snprintf(path + len, LEN_BUFFER - len, "/%s%s", name.c_str(), SCRIPT_EXT);
if (f_stat(path, 0) == FR_OK) {
luaExec(path);
StandaloneLuaWindow::instance()->attach();
}
#endif
}
@@ -417,7 +417,6 @@ void RadioSdManagerPage::fileAction(const char* path, const char* name,
else if (isExtensionMatching(ext, SCRIPTS_EXT)) {
menu->addLine(STR_EXECUTE_FILE, [=]() {
luaExec(fullpath);
StandaloneLuaWindow::instance()->attach();
});
}
#endif
-2
View File
@@ -102,8 +102,6 @@ static void run_lua_tool(Window* parent, const std::string& path)
f_chdir(toolPath);
luaExec(path.c_str());
auto lua_win = StandaloneLuaWindow::instance();
lua_win->attach();
}
// LUA scripts in TOOLS
+97 -20
View File
@@ -85,8 +85,9 @@ void StandaloneLuaWindow::redraw_cb(lv_event_t* e)
// singleton instance
StandaloneLuaWindow* StandaloneLuaWindow::_instance;
StandaloneLuaWindow::StandaloneLuaWindow() :
Window(nullptr, {0, 0, LCD_W, LCD_H}),
StandaloneLuaWindow::StandaloneLuaWindow(bool useLvgl) :
Window(MainWindow::instance(), {0, 0, LCD_W, LCD_H}),
useLvgl(useLvgl),
lcdBuffer(BMP_RGB565, LCD_W, LCD_H),
popup({50, 73, LCD_W - 100, LCD_H - 146})
{
@@ -94,24 +95,42 @@ StandaloneLuaWindow::StandaloneLuaWindow() :
etx_solid_bg(lvobj);
lcdBuffer.clear();
lcdBuffer.drawText(LCD_W / 2, LCD_H / 2 - 20, STR_LOADING,
FONT(L) | COLOR_THEME_PRIMARY2 | CENTERED);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
if (useLvglLayout()) {
padAll(PAD_ZERO);
etx_scrollbar(lvobj);
lv_obj_t* lbl = lv_label_create(lvobj);
lv_obj_set_pos(lbl, 0, 0);
lv_obj_set_size(lbl, LCD_W, LCD_H);
etx_solid_bg(lbl, COLOR_THEME_PRIMARY1_INDEX);
etx_txt_color(lbl, COLOR_THEME_PRIMARY2_INDEX);
lv_obj_set_style_text_align(lbl, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
lv_obj_set_style_pad_top(lbl, (LCD_H - EdgeTxStyles::PAGE_LINE_HEIGHT) / 2, LV_PART_MAIN);
lv_label_set_text(lbl, STR_LOADING);
luaLvglManager = this;
} else {
lcdBuffer.clear();
lcdBuffer.drawText(LCD_W / 2, LCD_H / 2 - 20, STR_LOADING,
FONT(L) | COLOR_THEME_PRIMARY2 | CENTERED);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
lv_obj_add_event_cb(lvobj, StandaloneLuaWindow::redraw_cb, LV_EVENT_DRAW_MAIN, nullptr);
}
// setup LUA event handler
setupHandler(this);
lv_obj_add_event_cb(lvobj, StandaloneLuaWindow::redraw_cb, LV_EVENT_DRAW_MAIN, nullptr);
attach();
}
void StandaloneLuaWindow::setup(bool useLvgl)
{
_instance = new StandaloneLuaWindow(useLvgl);
}
StandaloneLuaWindow* StandaloneLuaWindow::instance()
{
if (!_instance) {
_instance = new StandaloneLuaWindow();
}
return _instance;
}
@@ -121,13 +140,13 @@ void StandaloneLuaWindow::attach()
// backup previous screen
prevScreen = lv_scr_act();
// and load new one
lv_scr_load(lvobj);
Layer::back()->hide();
Layer::push(this);
lv_group_add_obj(lv_group_get_default(), lvobj);
lv_group_set_editing(lv_group_get_default(), true);
if (!useLvglLayout()) {
lv_group_add_obj(lv_group_get_default(), lvobj);
lv_group_set_editing(lv_group_get_default(), true);
}
}
}
@@ -135,10 +154,12 @@ void StandaloneLuaWindow::deleteLater(bool detach, bool trash)
{
if (_deleted) return;
luaLvglManager = nullptr;
Layer::pop(this);
Layer::back()->show();
if (prevScreen) {
lv_scr_load(prevScreen);
prevScreen = nullptr;
}
@@ -159,8 +180,20 @@ void StandaloneLuaWindow::checkEvents()
if (luaState != INTERPRETER_RELOAD_PERMANENT_SCRIPTS) {
// if LUA finished a full cycle,
// invalidate to display the screen buffer
bool useLvgl = useLvglLayout();
if (luaTask(true)) {
invalidate();
if (useLvgl && !hasError) {
PROTECT_LUA() {
if (!callRefs(lsScripts)) {
luaShowError();
}
} else {
luaShowError();
}
UNPROTECT_LUA();
} else {
invalidate();
}
}
}
@@ -174,7 +207,7 @@ void StandaloneLuaWindow::checkEvents()
luaLcdBuffer = nullptr;
}
void StandaloneLuaWindow::onClicked() { LuaEventHandler::onClicked(); }
void StandaloneLuaWindow::onClicked() { Keyboard::hide(false); LuaEventHandler::onClicked(); }
void StandaloneLuaWindow::onCancel() { LuaEventHandler::onCancel(); }
@@ -210,3 +243,47 @@ bool StandaloneLuaWindow::displayPopup(event_t event, uint8_t type,
return false;
}
void StandaloneLuaWindow::clear()
{
clearRefs(lsScripts);
Window::clear();
}
void StandaloneLuaWindow::luaShowError()
{
hasError = true;
extern void luaError(lua_State *L, uint8_t error);
luaError(lsScripts, SCRIPT_SYNTAX_ERROR);
}
void StandaloneLuaWindow::showError(bool firstCall, const char* title, const char* msg)
{
hasError = true;
if (!errorModal) {
lv_obj_set_scroll_dir(lvobj, LV_DIR_NONE);
errorModal = lv_obj_create(lvobj);
lv_obj_set_pos(errorModal, lv_obj_get_scroll_x(lvobj), lv_obj_get_scroll_y(lvobj));
lv_obj_set_size(errorModal, LCD_W, LCD_H);
etx_bg_color(errorModal, COLOR_BLACK_INDEX);
etx_obj_add_style(errorModal, styles->bg_opacity_75, LV_PART_MAIN);
errorTitle = lv_label_create(errorModal);
lv_obj_set_pos(errorTitle, 50, 30);
lv_obj_set_size(errorTitle, LCD_W - 100, 32);
etx_txt_color(errorTitle, COLOR_THEME_PRIMARY2_INDEX);
etx_solid_bg(errorTitle, COLOR_THEME_SECONDARY1_INDEX);
etx_font(errorTitle, FONT_L_INDEX);
etx_obj_add_style(errorTitle, styles->text_align_center, LV_PART_MAIN);
errorMsg = lv_label_create(errorModal);
lv_obj_set_pos(errorMsg, 50, 62);
lv_obj_set_size(errorMsg, LCD_W - 100, LCD_H - 92);
lv_obj_set_style_pad_all(errorMsg, 4, LV_PART_MAIN);
etx_txt_color(errorMsg, COLOR_THEME_PRIMARY1_INDEX);
etx_solid_bg(errorMsg, COLOR_THEME_SECONDARY3_INDEX);
etx_font(errorMsg, FONT_STD_INDEX);
etx_obj_add_style(errorMsg, styles->text_align_center, LV_PART_MAIN);
}
lv_label_set_text(errorTitle, title);
lv_label_set_text(errorMsg, msg);
}
+19 -2
View File
@@ -33,14 +33,15 @@ struct LuaPopup
void paint(BitmapBuffer* dc, uint8_t type, const char* text, const char* info);
};
class StandaloneLuaWindow : public Window, public LuaEventHandler
class StandaloneLuaWindow : public Window, public LuaEventHandler, public LuaLvglManager
{
static StandaloneLuaWindow* _instance;
explicit StandaloneLuaWindow();
explicit StandaloneLuaWindow(bool useLvgl);
public:
static StandaloneLuaWindow* instance();
static void setup(bool useLvgl);
void attach();
void deleteLater(bool detach = true, bool trash = true) override;
@@ -52,8 +53,24 @@ public:
bool displayPopup(event_t event, uint8_t type, const char* text,
const char* info, bool& result);
Window* getCurrentParent() const override { return tempParent ? tempParent : (Window*)this; }
void clear() override;
bool useLvglLayout() const override { return useLvgl; }
void luaShowError() override;
void showError(bool firstCall, const char* title, const char* msg);
bool isWidget() override { return false; }
protected:
lv_obj_t* prevScreen = nullptr;
lv_obj_t* errorModal = nullptr;
lv_obj_t* errorTitle = nullptr;
lv_obj_t* errorMsg = nullptr;
bool hasError = false;
bool useLvgl = false;
// GFX
BitmapBuffer lcdBuffer;
+1
View File
@@ -75,6 +75,7 @@ class ViewMain : public NavWindow
bool onLongPress() override;
void show(bool visible = true) override;
bool viewIsVisible() const { return isVisible; }
void showTopBarEdgeTxButton();
void hideTopBarEdgeTxButton();
+3 -2
View File
@@ -115,16 +115,17 @@ void Widget::setFullscreen(bool enable)
}
// Enter Fullscreen Mode
else {
ViewMain::instance()->enableWidgetSelect(false);
// ViewMain hidden - re-show this widget
show();
// Set window opaque (inhibits redraw from windows below)
setWindowFlag(OPAQUE);
updateZoneRect(parent->getRect());
updateZoneRect(parent->getRect(), false);
setRect(parent->getRect());
ViewMain::instance()->enableWidgetSelect(false);
bringToTop();
if (!lv_obj_get_group(lvobj)) {
+1 -1
View File
@@ -83,7 +83,7 @@ class Widget : public ButtonBase
virtual void background() {}
// Update widget 'zone' data (for Lua widgets)
virtual void updateZoneRect(rect_t rect) {}
virtual void updateZoneRect(rect_t rect, bool updateUI = true) {}
void enableFocus(bool enable);
+1 -1
View File
@@ -121,7 +121,7 @@ class TimerWidget : public Widget
int val = lastValue;
if (lastStartValue && timerData.showElapsed &&
lastStartValue != lastValue)
(int)lastStartValue != lastValue)
val = (int)lastStartValue - lastValue;
if (isLarge) {