From 8763d6e75f6e0a763026e20939f04fa6340b48c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Mon, 8 Aug 2022 09:13:05 +0200 Subject: [PATCH] Expose SDL button codes to Lua Also sanitize GameController mouse up reason and related code. --- src/gui/game/GameController.cpp | 8 ++++---- src/gui/game/GameController.h | 9 ++++++++- src/gui/game/GameView.cpp | 4 ++-- src/lua/LuaSDLKeys.h | 5 +++++ src/lua/LuaScriptInterface.cpp | 3 +++ 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index 8ae520616..486bc3122 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -544,11 +544,11 @@ bool GameController::MouseDown(int x, int y, unsigned button) return ret; } -bool GameController::MouseUp(int x, int y, unsigned button, char type) +bool GameController::MouseUp(int x, int y, unsigned button, MouseupReason reason) { - MouseUpEvent ev(x, y, button, type); + MouseUpEvent ev(x, y, button, reason); bool ret = commandInterface->HandleEvent(LuaEvents::mouseup, &ev); - if (type) + if (reason != mouseUpNormal) return ret; if (ret && foundSignID != -1 && yGetPlacingSave()) { @@ -764,7 +764,7 @@ void GameController::Tick() void GameController::Blur() { // Tell lua that mouse is up (even if it really isn't) - MouseUp(0, 0, 0, 1); + MouseUp(0, 0, 0, mouseUpBlur); BlurEvent ev; commandInterface->HandleEvent(LuaEvents::blur, &ev); } diff --git a/src/gui/game/GameController.h b/src/gui/game/GameController.h index d9ff1a2ad..cc6609611 100644 --- a/src/gui/game/GameController.h +++ b/src/gui/game/GameController.h @@ -58,6 +58,13 @@ private: void OpenSaveDone(); public: + enum MouseupReason + { + mouseUpNormal, + mouseUpBlur, + mouseUpDrawEnd, + }; + bool HasDone; GameController(); ~GameController(); @@ -68,7 +75,7 @@ public: bool MouseMove(int x, int y, int dx, int dy); bool MouseDown(int x, int y, unsigned button); - bool MouseUp(int x, int y, unsigned button, char type); + bool MouseUp(int x, int y, unsigned button, MouseupReason reason); bool MouseWheel(int x, int y, int d); bool TextInput(String text); bool TextEditing(String text); diff --git a/src/gui/game/GameView.cpp b/src/gui/game/GameView.cpp index 9f707f33e..6c2d95740 100644 --- a/src/gui/game/GameView.cpp +++ b/src/gui/game/GameView.cpp @@ -1067,7 +1067,7 @@ void GameView::OnMouseMove(int x, int y, int dx, int dy) { isMouseDown = false; drawMode = DrawPoints; - c->MouseUp(x, y, 0, 2); + c->MouseUp(x, y, 0, GameController::mouseUpDrawEnd); } } mouseInZoom = newMouseInZoom; @@ -1748,7 +1748,7 @@ void GameView::DoMouseDown(int x, int y, unsigned button) void GameView::DoMouseUp(int x, int y, unsigned button) { - if(c->MouseUp(x, y, button, 0)) + if(c->MouseUp(x, y, button, GameController::mouseUpNormal)) Window::DoMouseUp(x, y, button); } diff --git a/src/lua/LuaSDLKeys.h b/src/lua/LuaSDLKeys.h index 51367e44a..a80aadc9e 100644 --- a/src/lua/LuaSDLKeys.h +++ b/src/lua/LuaSDLKeys.h @@ -510,6 +510,11 @@ static void initLuaSDLKeys(lua_State *l) SETCONST(l, KMOD_SHIFT); SETCONST(l, KMOD_ALT); SETCONST(l, KMOD_GUI); + SETCONST(l, SDL_BUTTON_LEFT); + SETCONST(l, SDL_BUTTON_MIDDLE); + SETCONST(l, SDL_BUTTON_RIGHT); + SETCONST(l, SDL_BUTTON_X1); + SETCONST(l, SDL_BUTTON_X2); } #undef SETCONST diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 74c15e1e8..023570151 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -518,6 +518,9 @@ void LuaScriptInterface::initInterfaceAPI() //Ren shortcut lua_getglobal(l, "interface"); initLuaSDLKeys(l); + lua_pushinteger(l, GameController::mouseUpNormal); lua_setfield(l, -2, "MOUSE_UP_NORMAL"); + lua_pushinteger(l, GameController::mouseUpBlur); lua_setfield(l, -2, "MOUSE_UP_BLUR"); + lua_pushinteger(l, GameController::mouseUpDrawEnd); lua_setfield(l, -2, "MOUSE_UP_DRAW_END"); lua_setglobal(l, "ui"); Luna::Register(l);