Expose SDL button codes to Lua

Also sanitize GameController mouse up reason and related code.
This commit is contained in:
Tamás Bálint Misius 2022-08-08 09:13:05 +02:00
parent 059697aba0
commit 8763d6e75f
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
5 changed files with 22 additions and 7 deletions

View File

@ -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 && y<YRES && x<XRES && !gameView->GetPlacingSave())
{
@ -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);
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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

View File

@ -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<LuaWindow>::Register(l);