From cc7dbabd191c8e64ec420fe11cb1c8cab212934a Mon Sep 17 00:00:00 2001 From: Petr Mikheev Date: Fri, 9 Jul 2021 20:18:23 +0200 Subject: [PATCH] Change argument of `onKeyPress` --- apps/openmw/mwlua/luabindings.cpp | 20 ++++++++++++++++++++ apps/openmw/mwlua/luabindings.hpp | 2 ++ apps/openmw/mwlua/luamanagerimp.cpp | 5 +++-- apps/openmw/mwlua/playerscripts.hpp | 4 +++- components/lua/scriptscontainer.hpp | 2 +- 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/apps/openmw/mwlua/luabindings.cpp b/apps/openmw/mwlua/luabindings.cpp index bc50062546..5ed5378c0a 100644 --- a/apps/openmw/mwlua/luabindings.cpp +++ b/apps/openmw/mwlua/luabindings.cpp @@ -1,5 +1,7 @@ #include "luabindings.hpp" +#include + #include #include @@ -8,6 +10,12 @@ #include "eventqueue.hpp" #include "worldview.hpp" +namespace sol +{ + template <> + struct is_automagical : std::false_type {}; +} + namespace MWLua { @@ -160,5 +168,17 @@ namespace MWLua return context.mLua->makeReadOnly(res); } + void initInputBindings(const Context& context) + { + sol::usertype keyEvent = context.mLua->sol().new_usertype("KeyEvent"); + keyEvent["symbol"] = sol::readonly_property([](const SDL_Keysym& e) { return std::string(1, static_cast(e.sym)); }); + keyEvent["code"] = sol::readonly_property([](const SDL_Keysym& e) -> int { return e.sym; }); + keyEvent["modifiers"] = sol::readonly_property([](const SDL_Keysym& e) -> int { return e.mod; }); + keyEvent["withShift"] = sol::readonly_property([](const SDL_Keysym& e) -> bool { return e.mod & KMOD_SHIFT; }); + keyEvent["withCtrl"] = sol::readonly_property([](const SDL_Keysym& e) -> bool { return e.mod & KMOD_CTRL; }); + keyEvent["withAlt"] = sol::readonly_property([](const SDL_Keysym& e) -> bool { return e.mod & KMOD_ALT; }); + keyEvent["withSuper"] = sol::readonly_property([](const SDL_Keysym& e) -> bool { return e.mod & KMOD_GUI; }); + } + } diff --git a/apps/openmw/mwlua/luabindings.hpp b/apps/openmw/mwlua/luabindings.hpp index ab83ce995f..8be96763a5 100644 --- a/apps/openmw/mwlua/luabindings.hpp +++ b/apps/openmw/mwlua/luabindings.hpp @@ -26,6 +26,8 @@ namespace MWLua sol::table initFieldGroup(const Context&, const QueryFieldGroup&); + void initInputBindings(const Context&); + // Implemented in objectbindings.cpp void initObjectBindingsForLocalScripts(const Context&); void initObjectBindingsForGlobalScripts(const Context&); diff --git a/apps/openmw/mwlua/luamanagerimp.cpp b/apps/openmw/mwlua/luamanagerimp.cpp index 604dde8495..bd07c191f9 100644 --- a/apps/openmw/mwlua/luamanagerimp.cpp +++ b/apps/openmw/mwlua/luamanagerimp.cpp @@ -48,6 +48,7 @@ namespace MWLua initObjectBindingsForLocalScripts(localContext); initCellBindingsForLocalScripts(localContext); LocalScripts::initializeSelfPackage(localContext); + initInputBindings(localContext); mLua.addCommonPackage("openmw.async", getAsyncPackageInitializer(context)); mLua.addCommonPackage("openmw.util", LuaUtil::initUtilPackage(mLua.sol())); @@ -153,8 +154,8 @@ namespace MWLua // Engine handlers in local scripts if (mPlayerScripts) { - for (const SDL_Keysym key : mKeyPressEvents) - mPlayerScripts->keyPress(key.sym, key.mod); + for (const SDL_Keysym& key : mKeyPressEvents) + mPlayerScripts->keyPress(key); } mKeyPressEvents.clear(); diff --git a/apps/openmw/mwlua/playerscripts.hpp b/apps/openmw/mwlua/playerscripts.hpp index 9a08f917e7..72e064bb9b 100644 --- a/apps/openmw/mwlua/playerscripts.hpp +++ b/apps/openmw/mwlua/playerscripts.hpp @@ -1,6 +1,8 @@ #ifndef MWLUA_PLAYERSCRIPTS_H #define MWLUA_PLAYERSCRIPTS_H +#include + #include "localscripts.hpp" namespace MWLua @@ -14,7 +16,7 @@ namespace MWLua registerEngineHandlers({&mKeyPressHandlers}); } - void keyPress(int sym, int mod) { callEngineHandlers(mKeyPressHandlers, sym, mod); } + void keyPress(const SDL_Keysym& key) { callEngineHandlers(mKeyPressHandlers, key); } private: EngineHandlerList mKeyPressHandlers{"onKeyPress"}; diff --git a/components/lua/scriptscontainer.hpp b/components/lua/scriptscontainer.hpp index a5cb7d3e58..da36109851 100644 --- a/components/lua/scriptscontainer.hpp +++ b/components/lua/scriptscontainer.hpp @@ -163,6 +163,7 @@ namespace LuaUtil void registerEngineHandlers(std::initializer_list handlers); const std::string mNamePrefix; + LuaUtil::LuaState& mLua; private: struct Script @@ -190,7 +191,6 @@ namespace LuaUtil void updateTimerQueue(std::vector& timerQueue, double time); static void insertTimer(std::vector& timerQueue, Timer&& t); - LuaUtil::LuaState& mLua; const UserdataSerializer* mSerializer = nullptr; std::map API;