[lua] Add onwheel and ontouchmagnify events to Dialog:canvas()

This commit is contained in:
David Capello 2022-12-29 15:12:15 -03:00
parent 95a195b99b
commit 1830e5343f
3 changed files with 58 additions and 0 deletions

View File

@ -92,6 +92,18 @@ bool Canvas::onProcessMessage(ui::Message* msg)
break;
}
case ui::kMouseWheelMessage: {
auto mouseMsg = static_cast<ui::MouseMessage*>(msg);
Wheel(mouseMsg);
break;
}
case ui::kTouchMagnifyMessage: {
auto touchMsg = static_cast<ui::TouchMessage*>(msg);
TouchMagnify(touchMsg);
break;
}
}
return ui::Widget::onProcessMessage(msg);
}

View File

@ -12,6 +12,10 @@
#include "ui/cursor_type.h"
#include "ui/widget.h"
namespace ui {
class TouchMessage;
}
namespace app {
namespace script {
@ -35,6 +39,8 @@ public:
obs::signal<void(ui::MouseMessage*)> MouseMove;
obs::signal<void(ui::MouseMessage*)> MouseDown;
obs::signal<void(ui::MouseMessage*)> MouseUp;
obs::signal<void(ui::MouseMessage*)> Wheel;
obs::signal<void(ui::TouchMessage*)> TouchMagnify;
private:
void onInitTheme(ui::InitThemeEvent& ev) override;

View File

@ -926,6 +926,7 @@ int Dialog_canvas(lua_State* L)
}
lua_pop(L, 1);
// Auxiliary callbacks used in Canvas events
auto mouseCallback =
[](lua_State* L, ui::MouseMessage* msg) {
ASSERT(msg->recipient());
@ -940,6 +941,33 @@ int Dialog_canvas(lua_State* L)
lua_pushinteger(L, int(msg->button()));
lua_setfield(L, -2, "button");
lua_pushnumber(L, msg->pressure());
lua_setfield(L, -2, "pressure");
if (msg->type() == kMouseWheelMessage) {
lua_pushnumber(L, msg->wheelDelta().x);
lua_setfield(L, -2, "deltaX");
lua_pushnumber(L, msg->wheelDelta().y);
lua_setfield(L, -2, "deltaY");
}
};
auto touchCallback =
[](lua_State* L, ui::TouchMessage* msg) {
ASSERT(msg->recipient());
if (!msg->recipient())
return;
lua_pushinteger(L, msg->position().x - msg->recipient()->bounds().x);
lua_setfield(L, -2, "x");
lua_pushinteger(L, msg->position().y - msg->recipient()->bounds().y);
lua_setfield(L, -2, "y");
lua_pushnumber(L, msg->magnification());
lua_setfield(L, -2, "magnification");
};
type = lua_getfield(L, 2, "onmousemove");
@ -959,6 +987,18 @@ int Dialog_canvas(lua_State* L)
Dialog_connect_signal(L, 1, widget->MouseUp, mouseCallback);
}
lua_pop(L, 1);
type = lua_getfield(L, 2, "onwheel");
if (type == LUA_TFUNCTION) {
Dialog_connect_signal(L, 1, widget->Wheel, mouseCallback);
}
lua_pop(L, 1);
type = lua_getfield(L, 2, "ontouchmagnify");
if (type == LUA_TFUNCTION) {
Dialog_connect_signal(L, 1, widget->TouchMagnify, touchCallback);
}
lua_pop(L, 1);
}
}