Support for touch/key events in LUA widgets
This commit is contained in:
committed by
Raphael Coeffic
parent
93797a3e14
commit
a3424d5bc0
@@ -104,7 +104,11 @@ endmacro(add_gui_src)
|
||||
|
||||
if(LUA)
|
||||
add_gui_src(standalone_lua.cpp)
|
||||
set(SRC ${SRC} lua/widgets.cpp)
|
||||
set(SRC ${SRC}
|
||||
lua/lua_widget.cpp
|
||||
lua/lua_widget_factory.cpp
|
||||
lua/widgets.cpp
|
||||
)
|
||||
endif()
|
||||
|
||||
if(LUA_MIXER)
|
||||
|
||||
@@ -69,7 +69,12 @@ static bool evt_to_indev_data(event_t evt, lv_indev_data_t *data)
|
||||
case KEY_EXIT:
|
||||
if (evt != EVT_KEY_LONG(KEY_EXIT)) {
|
||||
data->key = LV_KEY_ESC;
|
||||
} else { return false; }
|
||||
} else {
|
||||
// prevent subsequent RELEASE event
|
||||
lv_indev_wait_release(lv_indev_get_act());
|
||||
data->state = LV_INDEV_STATE_PRESSED;
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
@@ -185,7 +185,6 @@ struct ToolButton : public TextButton {
|
||||
void RadioToolsPage::rebuild(FormWindow * window)
|
||||
{
|
||||
window->clear();
|
||||
// Window::clearFocus();
|
||||
|
||||
std::list<ToolEntry> tools;
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
#include "standalone_lua.h"
|
||||
#include "view_main.h"
|
||||
#include "touch.h"
|
||||
// #include "touch.h"
|
||||
|
||||
using std::string;
|
||||
extern BitmapBuffer* luaLcdBuffer;
|
||||
@@ -51,15 +51,13 @@ void LuaPopup::paint(BitmapBuffer* dc, uint8_t type, const char* text, const cha
|
||||
// singleton instance
|
||||
StandaloneLuaWindow* StandaloneLuaWindow::_instance;
|
||||
|
||||
#if defined(HARDWARE_TOUCH)
|
||||
bool StandaloneLuaWindow::fingerDown = false;
|
||||
#endif
|
||||
|
||||
StandaloneLuaWindow::StandaloneLuaWindow() :
|
||||
Window(nullptr, {0, 0, LCD_W, LCD_H}, OPAQUE),
|
||||
lcdBuffer(BMP_RGB565, LCD_W, LCD_H),
|
||||
popup({50, 73, LCD_W - 100, LCD_H - 146})
|
||||
{
|
||||
// setup LUA event handler
|
||||
setupHandler(this);
|
||||
}
|
||||
|
||||
StandaloneLuaWindow* StandaloneLuaWindow::instance()
|
||||
@@ -82,8 +80,10 @@ void StandaloneLuaWindow::attach()
|
||||
lv_scr_load(lvobj);
|
||||
|
||||
Layer::push(this);
|
||||
|
||||
lv_group_add_obj(lv_group_get_default(), lvobj);
|
||||
lv_group_set_editing(lv_group_get_default(), true);
|
||||
}
|
||||
// setFocus();
|
||||
}
|
||||
|
||||
void StandaloneLuaWindow::deleteLater(bool detach, bool trash)
|
||||
@@ -119,30 +119,11 @@ void StandaloneLuaWindow::checkEvents()
|
||||
luaLcdBuffer = &lcdBuffer;
|
||||
|
||||
if (luaState != INTERPRETER_RELOAD_PERMANENT_SCRIPTS) {
|
||||
#if defined(HARDWARE_TOUCH)
|
||||
// Stick touch in the same slot of touches as luaTask sticks evt in events
|
||||
if (IS_TOUCH_EVENT(event)) {
|
||||
for (int i = 0; i < EVENT_BUFFER_SIZE; i++) {
|
||||
if (events[i] == 0) {
|
||||
touches[i] = touch;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (luaTask(event, true)) {
|
||||
#if defined(DEBUG_WINDOWS)
|
||||
TRACE("# StandaloneLuaWindow::invalidate()");
|
||||
#endif
|
||||
invalidate();
|
||||
}
|
||||
// if LUA finished a full cycle,
|
||||
// invalidate to display the screen buffer
|
||||
if (luaTask(0, true)) { invalidate(); }
|
||||
}
|
||||
|
||||
event = 0;
|
||||
#if defined(HARDWARE_TOUCH)
|
||||
memclear(&touch, sizeof(touch));
|
||||
#endif
|
||||
|
||||
if (luaState == INTERPRETER_RELOAD_PERMANENT_SCRIPTS) {
|
||||
// Script does not run anymore...
|
||||
TRACE("LUA standalone script exited: deleting window!");
|
||||
@@ -153,61 +134,20 @@ void StandaloneLuaWindow::checkEvents()
|
||||
luaLcdBuffer = nullptr;
|
||||
}
|
||||
|
||||
void StandaloneLuaWindow::onClicked()
|
||||
{
|
||||
LuaEventHandler::onClicked();
|
||||
}
|
||||
|
||||
void StandaloneLuaWindow::onCancel()
|
||||
{
|
||||
// do nothing: wait for long press to kill
|
||||
// the script
|
||||
LuaEventHandler::onCancel();
|
||||
}
|
||||
|
||||
#if defined(HARDWARE_KEYS)
|
||||
void StandaloneLuaWindow::onEvent(event_t evt)
|
||||
{
|
||||
event = evt;
|
||||
LuaEventHandler::onEvent(evt);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(HARDWARE_TOUCH)
|
||||
bool StandaloneLuaWindow::onTouchStart(coord_t x, coord_t y)
|
||||
{
|
||||
TRACE_WINDOWS("StandaloneLuaWindow received touch start (%d) x=%d;y=%d", hasFocus(), x, y);
|
||||
// Only one EVT_TOUCH_FIRST at a time
|
||||
if (!fingerDown) {
|
||||
event = EVT_TOUCH_FIRST;
|
||||
touch.touchX = x;
|
||||
touch.touchY = y;
|
||||
fingerDown = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool StandaloneLuaWindow::onTouchEnd(coord_t x, coord_t y)
|
||||
{
|
||||
TRACE_WINDOWS("StandaloneLuaWindow received touch end (%d) x=%d;y=%d", hasFocus(), x, y);
|
||||
if (touchState.tapCount > 0) {
|
||||
event = EVT_TOUCH_TAP;
|
||||
touch.tapCount = touchState.tapCount;
|
||||
} else
|
||||
event = EVT_TOUCH_BREAK;
|
||||
touch.touchX = x;
|
||||
touch.touchY = y;
|
||||
fingerDown = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool StandaloneLuaWindow::onTouchSlide(coord_t x, coord_t y, coord_t startX, coord_t startY, coord_t slideX, coord_t slideY)
|
||||
{
|
||||
TRACE_WINDOWS("StandaloneLuaWindow touch slide");
|
||||
event = EVT_TOUCH_SLIDE;
|
||||
touch.touchX = x;
|
||||
touch.touchY = y;
|
||||
touch.startX = startX;
|
||||
touch.startY = startY;
|
||||
touch.slideX = slideX;
|
||||
touch.slideY = slideY;
|
||||
fingerDown = false;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
bool StandaloneLuaWindow::displayPopup(event_t event, uint8_t type, const char* text,
|
||||
const char* info, bool& result)
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#if defined(LUA)
|
||||
#include "libopenui.h"
|
||||
#include "lua/lua_api.h"
|
||||
#include "lua/lua_widget.h"
|
||||
|
||||
struct LuaPopup
|
||||
{
|
||||
@@ -32,7 +33,7 @@ struct LuaPopup
|
||||
void paint(BitmapBuffer* dc, uint8_t type, const char* text, const char* info);
|
||||
};
|
||||
|
||||
class StandaloneLuaWindow : public Window
|
||||
class StandaloneLuaWindow : public Window, public LuaEventHandler
|
||||
{
|
||||
static StandaloneLuaWindow* _instance;
|
||||
|
||||
@@ -43,24 +44,11 @@ public:
|
||||
|
||||
void attach();
|
||||
void deleteLater(bool detach = true, bool trash = true) override;
|
||||
void paint(BitmapBuffer* dc) override;
|
||||
void checkEvents() override;
|
||||
void onCancel() override;
|
||||
|
||||
#if defined(DEBUG_WINDOWS)
|
||||
std::string getName() const override { return "StandaloneLuaWindow"; }
|
||||
#endif
|
||||
|
||||
#if defined(HARDWARE_KEYS)
|
||||
void onEvent(event_t evt) override;
|
||||
#endif
|
||||
|
||||
#if defined(HARDWARE_TOUCH)
|
||||
bool onTouchStart(coord_t x, coord_t y) override;
|
||||
bool onTouchEnd(coord_t x, coord_t y) override;
|
||||
bool onTouchSlide(coord_t x, coord_t y, coord_t startX, coord_t startY, coord_t slideX, coord_t slideY) override;
|
||||
#endif
|
||||
|
||||
bool displayPopup(event_t event, uint8_t type, const char* text,
|
||||
const char* info, bool& result);
|
||||
|
||||
@@ -75,10 +63,11 @@ protected:
|
||||
|
||||
// run LUA code
|
||||
void runLua(event_t evt);
|
||||
event_t event = 0;
|
||||
#if defined(HARDWARE_TOUCH)
|
||||
static bool fingerDown;
|
||||
LuaTouchData touch;
|
||||
#endif
|
||||
|
||||
void paint(BitmapBuffer* dc) override;
|
||||
void onEvent(event_t evt) override;
|
||||
void checkEvents() override;
|
||||
void onClicked() override;
|
||||
void onCancel() override;
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -30,8 +30,6 @@
|
||||
#include "touch.h"
|
||||
#endif
|
||||
|
||||
// constexpr uint32_t WIDGET_FOCUS_TIMEOUT = 10*1000; // 10 seconds
|
||||
|
||||
static void openWidgetMenu(Widget * parent)
|
||||
{
|
||||
Menu *menu = new Menu(parent);
|
||||
@@ -64,17 +62,6 @@ Widget::Widget(const WidgetFactory* factory, Window* parent,
|
||||
});
|
||||
}
|
||||
|
||||
void Widget::checkEvents()
|
||||
{
|
||||
Button::checkEvents();
|
||||
|
||||
// Give the focus back to ViewMain after WIDGET_FOCUS_TIMEOUT milliseconds
|
||||
// if (!fullscreen && hasFocus() && (RTOS_GET_MS() - focusGainedTS >= WIDGET_FOCUS_TIMEOUT)) {
|
||||
// ViewMain::instance()->setFocus();
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
void Widget::paint(BitmapBuffer * dc)
|
||||
{
|
||||
if (fullscreen) {
|
||||
@@ -86,12 +73,6 @@ void Widget::paint(BitmapBuffer * dc)
|
||||
refresh(dc);
|
||||
|
||||
if (hasFocus() && !fullscreen) {
|
||||
|
||||
// // Blink from haft-time before expiring (5s)
|
||||
// if ((RTOS_GET_MS() - focusGainedTS >= WIDGET_FOCUS_TIMEOUT / 2)
|
||||
// && !FAST_BLINK_ON_PHASE) {
|
||||
// return;
|
||||
// }
|
||||
dc->drawRect(0, 0, width(), height(), 2, STASHED, COLOR_THEME_FOCUS);
|
||||
}
|
||||
}
|
||||
@@ -99,21 +80,7 @@ void Widget::paint(BitmapBuffer * dc)
|
||||
#if defined(HARDWARE_KEYS)
|
||||
void Widget::onEvent(event_t event)
|
||||
{
|
||||
TRACE("### event = 0x%x ###", event);
|
||||
if (!fullscreen) {
|
||||
if (event == EVT_KEY_BREAK(KEY_EXIT)) {
|
||||
// [EXIT] -> exit focus mode (if not fullscreen)
|
||||
// killEvents(event);
|
||||
// ViewMain::instance()->setFocus();
|
||||
return;
|
||||
}
|
||||
// Forward the rest to the parent class
|
||||
Button::onEvent(event);
|
||||
}
|
||||
// In fullscreen mode, we react only to that one key:
|
||||
// [RTN / EXIT LONG] -> exit fullscreen mode
|
||||
else if (EVT_KEY_LONG(KEY_EXIT) == event) {
|
||||
// killEvents(event);
|
||||
if (fullscreen && (EVT_KEY_LONG(KEY_EXIT) == event)) {
|
||||
setFullscreen(false);
|
||||
}
|
||||
}
|
||||
@@ -148,6 +115,11 @@ void Widget::setFullscreen(bool enable)
|
||||
// re-enable scroll chaining (sliding main view)
|
||||
lv_obj_add_flag(lvobj, LV_OBJ_FLAG_SCROLL_CHAIN_HOR);
|
||||
lv_obj_add_flag(lvobj, LV_OBJ_FLAG_SCROLL_CHAIN_VER);
|
||||
|
||||
// exit editing mode
|
||||
lv_group_set_editing(lv_group_get_default(), false);
|
||||
|
||||
onFullscreen(enable);
|
||||
}
|
||||
// Enter Fullscreen Mode
|
||||
else {
|
||||
@@ -167,9 +139,19 @@ void Widget::setFullscreen(bool enable)
|
||||
// disable scroll chaining (sliding main view)
|
||||
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_SCROLL_CHAIN_HOR);
|
||||
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_SCROLL_CHAIN_VER);
|
||||
|
||||
// set group in editing mode (keys LEFT / RIGHT)
|
||||
lv_group_set_editing(lv_group_get_default(), true);
|
||||
|
||||
onFullscreen(enable);
|
||||
}
|
||||
}
|
||||
|
||||
void Widget::onLongPress()
|
||||
{
|
||||
if (!fullscreen) Button::onLongPress();
|
||||
}
|
||||
|
||||
std::list<const WidgetFactory *> & getRegisteredWidgets()
|
||||
{
|
||||
static std::list<const WidgetFactory *> widgets;
|
||||
|
||||
@@ -83,7 +83,6 @@ class Widget : public Button
|
||||
#endif
|
||||
|
||||
// Window interface
|
||||
void checkEvents() override;
|
||||
#if defined(HARDWARE_KEYS)
|
||||
void onEvent(event_t event) override;
|
||||
#endif
|
||||
@@ -108,6 +107,9 @@ class Widget : public Button
|
||||
PersistentData * persistentData;
|
||||
uint32_t focusGainedTS = 0;
|
||||
bool fullscreen = false;
|
||||
|
||||
void onLongPress() override;
|
||||
virtual void onFullscreen(bool enable) {}
|
||||
};
|
||||
|
||||
void registerWidget(const WidgetFactory * factory);
|
||||
|
||||
Reference in New Issue
Block a user