Add touch to Standalone Lua scripts

This commit is contained in:
jfrickmann
2021-11-19 08:01:28 -05:00
parent df153b4b1f
commit ce7b408445
5 changed files with 212 additions and 47 deletions
+86 -29
View File
@@ -21,9 +21,10 @@
#include "standalone_lua.h"
#include "view_main.h"
#include "lua/lua_api.h"
#include "touch.h"
using std::string;
extern BitmapBuffer* luaLcdBuffer;
void LuaPopup::paint(BitmapBuffer* dc, uint8_t type, const char* text, const char* info)
{
@@ -50,6 +51,10 @@ 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
// LUA lcd buffer
uint16_t* lcdGetBackupBuffer();
@@ -103,8 +108,43 @@ void StandaloneLuaWindow::paint(BitmapBuffer* dc)
void StandaloneLuaWindow::checkEvents()
{
Window::checkEvents();
runLua(event);
// Set global LUA LCD buffer
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();
}
}
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!");
deleteLater();
}
// Kill global LUA LCD buffer
luaLcdBuffer = nullptr;
}
#if defined(HARDWARE_KEYS)
@@ -114,6 +154,49 @@ void StandaloneLuaWindow::onEvent(event_t 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)
{
@@ -138,30 +221,4 @@ bool StandaloneLuaWindow::displayPopup(event_t event, uint8_t type, const char*
}
return false;
}
extern BitmapBuffer* luaLcdBuffer;
void StandaloneLuaWindow::runLua(event_t evt)
{
// Set global LUA LCD buffer
luaLcdBuffer = &lcdBuffer;
if (luaState != INTERPRETER_RELOAD_PERMANENT_SCRIPTS) {
if (luaTask(evt, true)) {
#if defined(DEBUG_WINDOWS)
TRACE("# StandaloneLuaWindow::invalidate()");
#endif
invalidate();
}
}
if (luaState == INTERPRETER_RELOAD_PERMANENT_SCRIPTS) {
// Script does not run anymore...
TRACE("LUA standalone script exited: deleting window!");
deleteLater();
}
// Kill global LUA LCD buffer
luaLcdBuffer = nullptr;
}
}
+11
View File
@@ -22,6 +22,7 @@
#pragma once
#include "libopenui.h"
#include "lua/lua_api.h"
struct LuaPopup
{
@@ -52,6 +53,12 @@ public:
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);
@@ -65,4 +72,8 @@ protected:
// run LUA code
void runLua(event_t evt);
event_t event = 0;
#if defined(HARDWARE_TOUCH)
static bool fingerDown;
LuaTouchData touch;
#endif
};