Change argument of `onKeyPress`

dont-compose-content
Petr Mikheev 4 years ago
parent b1a6441c23
commit cc7dbabd19

@ -1,5 +1,7 @@
#include "luabindings.hpp"
#include <SDL_events.h>
#include <components/lua/luastate.hpp>
#include <components/queries/luabindings.hpp>
@ -8,6 +10,12 @@
#include "eventqueue.hpp"
#include "worldview.hpp"
namespace sol
{
template <>
struct is_automagical<SDL_Keysym> : std::false_type {};
}
namespace MWLua
{
@ -160,5 +168,17 @@ namespace MWLua
return context.mLua->makeReadOnly(res);
}
void initInputBindings(const Context& context)
{
sol::usertype<SDL_Keysym> keyEvent = context.mLua->sol().new_usertype<SDL_Keysym>("KeyEvent");
keyEvent["symbol"] = sol::readonly_property([](const SDL_Keysym& e) { return std::string(1, static_cast<char>(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; });
}
}

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

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

@ -1,6 +1,8 @@
#ifndef MWLUA_PLAYERSCRIPTS_H
#define MWLUA_PLAYERSCRIPTS_H
#include <SDL_events.h>
#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"};

@ -163,6 +163,7 @@ namespace LuaUtil
void registerEngineHandlers(std::initializer_list<EngineHandlerList*> handlers);
const std::string mNamePrefix;
LuaUtil::LuaState& mLua;
private:
struct Script
@ -190,7 +191,6 @@ namespace LuaUtil
void updateTimerQueue(std::vector<Timer>& timerQueue, double time);
static void insertTimer(std::vector<Timer>& timerQueue, Timer&& t);
LuaUtil::LuaState& mLua;
const UserdataSerializer* mSerializer = nullptr;
std::map<std::string, sol::object> API;

Loading…
Cancel
Save