1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-05-13 21:41:29 +00:00

Fix in-game actions not clearing because of input bindings only initializing in menu context

This commit is contained in:
uramer 2025-03-04 21:03:15 +01:00
parent 6e9d15f91d
commit 990096ff9b
2 changed files with 103 additions and 81 deletions

View file

@ -148,7 +148,7 @@ namespace MWLua
}; };
} }
sol::table readOnly = LuaUtil::makeReadOnly(api); sol::table readOnlyApi = LuaUtil::makeReadOnly(api);
return context.setTypePackage(readOnly, "openmw_core"); return context.setTypePackage(readOnlyApi, "openmw_core");
} }
} }

View file

@ -38,12 +38,12 @@ namespace MWLua
sol::table initInputPackage(const Context& context) sol::table initInputPackage(const Context& context)
{ {
sol::object cached = context.getTypePackage("openmw_input");
if (cached != sol::nil)
return cached;
sol::state_view lua = context.sol(); sol::state_view lua = context.sol();
{
if (lua["openmw_input"] != sol::nil)
return lua["openmw_input"];
}
context.cachePackage("openmw_input_keyevent", [&lua]() {
sol::usertype<SDL_Keysym> keyEvent = lua.new_usertype<SDL_Keysym>("KeyEvent"); sol::usertype<SDL_Keysym> keyEvent = lua.new_usertype<SDL_Keysym>("KeyEvent");
keyEvent["symbol"] = sol::readonly_property([](const SDL_Keysym& e) { keyEvent["symbol"] = sol::readonly_property([](const SDL_Keysym& e) {
if (e.sym > 0 && e.sym <= 255) if (e.sym > 0 && e.sym <= 255)
@ -52,28 +52,39 @@ namespace MWLua
return std::string(); return std::string();
}); });
keyEvent["code"] = sol::readonly_property([](const SDL_Keysym& e) -> int { return e.scancode; }); keyEvent["code"] = sol::readonly_property([](const SDL_Keysym& e) -> int { return e.scancode; });
keyEvent["withShift"] = sol::readonly_property([](const SDL_Keysym& e) -> bool { return e.mod & KMOD_SHIFT; }); keyEvent["withShift"]
keyEvent["withCtrl"] = sol::readonly_property([](const SDL_Keysym& e) -> bool { return e.mod & KMOD_CTRL; }); = 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["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; }); keyEvent["withSuper"]
= sol::readonly_property([](const SDL_Keysym& e) -> bool { return e.mod & KMOD_GUI; });
return sol::table(lua, sol::create);
});
context.cachePackage("openmw_input_touchpadevent", [&lua]() {
auto touchpadEvent = lua.new_usertype<SDLUtil::TouchEvent>("TouchpadEvent"); auto touchpadEvent = lua.new_usertype<SDLUtil::TouchEvent>("TouchpadEvent");
touchpadEvent["device"] = sol::readonly_property([](const SDLUtil::TouchEvent& e) -> int { return e.mDevice; }); touchpadEvent["device"]
touchpadEvent["finger"] = sol::readonly_property([](const SDLUtil::TouchEvent& e) -> int { return e.mFinger; }); = sol::readonly_property([](const SDLUtil::TouchEvent& e) -> int { return e.mDevice; });
touchpadEvent["finger"]
= sol::readonly_property([](const SDLUtil::TouchEvent& e) -> int { return e.mFinger; });
touchpadEvent["position"] = sol::readonly_property([](const SDLUtil::TouchEvent& e) -> osg::Vec2f { touchpadEvent["position"] = sol::readonly_property([](const SDLUtil::TouchEvent& e) -> osg::Vec2f {
return { e.mX, e.mY }; return { e.mX, e.mY };
}); });
touchpadEvent["pressure"] touchpadEvent["pressure"]
= sol::readonly_property([](const SDLUtil::TouchEvent& e) -> float { return e.mPressure; }); = sol::readonly_property([](const SDLUtil::TouchEvent& e) -> float { return e.mPressure; });
return sol::table(lua, sol::create);
});
context.cachePackage("openmw_input_inputactions", [&lua]() {
auto inputActions = lua.new_usertype<LuaUtil::InputAction::Registry>("InputActions"); auto inputActions = lua.new_usertype<LuaUtil::InputAction::Registry>("InputActions");
inputActions[sol::meta_function::index] inputActions[sol::meta_function::index]
= [](LuaUtil::InputAction::Registry& registry, std::string_view key) { return registry[key]; }; = [](LuaUtil::InputAction::Registry& registry, std::string_view key) { return registry[key]; };
{ {
auto pairs = [](LuaUtil::InputAction::Registry& registry) { auto pairs = [](LuaUtil::InputAction::Registry& registry) {
auto next auto next = [](LuaUtil::InputAction::Registry& registry, std::string_view key)
= [](LuaUtil::InputAction::Registry& registry, -> sol::optional<std::tuple<std::string, LuaUtil::InputAction::Info>> {
std::string_view key) -> sol::optional<std::tuple<std::string, LuaUtil::InputAction::Info>> {
std::optional<std::string> nextKey(registry.nextKey(key)); std::optional<std::string> nextKey(registry.nextKey(key));
if (!nextKey.has_value()) if (!nextKey.has_value())
return sol::nullopt; return sol::nullopt;
@ -84,7 +95,10 @@ namespace MWLua
}; };
inputActions[sol::meta_function::pairs] = pairs; inputActions[sol::meta_function::pairs] = pairs;
} }
return sol::table(lua, sol::create);
});
context.cachePackage("openmw_input_actioninfo", [&lua]() {
auto actionInfo = lua.new_usertype<LuaUtil::InputAction::Info>("ActionInfo"); auto actionInfo = lua.new_usertype<LuaUtil::InputAction::Info>("ActionInfo");
actionInfo["key"] = sol::readonly_property( actionInfo["key"] = sol::readonly_property(
[](const LuaUtil::InputAction::Info& info) -> std::string_view { return info.mKey; }); [](const LuaUtil::InputAction::Info& info) -> std::string_view { return info.mKey; });
@ -94,18 +108,21 @@ namespace MWLua
[](const LuaUtil::InputAction::Info& info) -> std::string_view { return info.mDescription; }); [](const LuaUtil::InputAction::Info& info) -> std::string_view { return info.mDescription; });
actionInfo["l10n"] = sol::readonly_property( actionInfo["l10n"] = sol::readonly_property(
[](const LuaUtil::InputAction::Info& info) -> std::string_view { return info.mL10n; }); [](const LuaUtil::InputAction::Info& info) -> std::string_view { return info.mL10n; });
actionInfo["type"] = sol::readonly_property([](const LuaUtil::InputAction::Info& info) { return info.mType; }); actionInfo["type"]
= sol::readonly_property([](const LuaUtil::InputAction::Info& info) { return info.mType; });
actionInfo["defaultValue"] actionInfo["defaultValue"]
= sol::readonly_property([](const LuaUtil::InputAction::Info& info) { return info.mDefaultValue; }); = sol::readonly_property([](const LuaUtil::InputAction::Info& info) { return info.mDefaultValue; });
return sol::table(lua, sol::create);
});
context.cachePackage("openmw_input_inputtriggers", [&lua]() {
auto inputTriggers = lua.new_usertype<LuaUtil::InputTrigger::Registry>("InputTriggers"); auto inputTriggers = lua.new_usertype<LuaUtil::InputTrigger::Registry>("InputTriggers");
inputTriggers[sol::meta_function::index] inputTriggers[sol::meta_function::index]
= [](LuaUtil::InputTrigger::Registry& registry, std::string_view key) { return registry[key]; }; = [](LuaUtil::InputTrigger::Registry& registry, std::string_view key) { return registry[key]; };
{ {
auto pairs = [](LuaUtil::InputTrigger::Registry& registry) { auto pairs = [](LuaUtil::InputTrigger::Registry& registry) {
auto next auto next = [](LuaUtil::InputTrigger::Registry& registry, std::string_view key)
= [](LuaUtil::InputTrigger::Registry& registry, -> sol::optional<std::tuple<std::string, LuaUtil::InputTrigger::Info>> {
std::string_view key) -> sol::optional<std::tuple<std::string, LuaUtil::InputTrigger::Info>> {
std::optional<std::string> nextKey(registry.nextKey(key)); std::optional<std::string> nextKey(registry.nextKey(key));
if (!nextKey.has_value()) if (!nextKey.has_value())
return sol::nullopt; return sol::nullopt;
@ -116,7 +133,10 @@ namespace MWLua
}; };
inputTriggers[sol::meta_function::pairs] = pairs; inputTriggers[sol::meta_function::pairs] = pairs;
} }
return sol::table(lua, sol::create);
});
context.cachePackage("openmw_input_triggerinfo", [&lua]() {
auto triggerInfo = lua.new_usertype<LuaUtil::InputTrigger::Info>("TriggerInfo"); auto triggerInfo = lua.new_usertype<LuaUtil::InputTrigger::Info>("TriggerInfo");
triggerInfo["key"] = sol::readonly_property( triggerInfo["key"] = sol::readonly_property(
[](const LuaUtil::InputTrigger::Info& info) -> std::string_view { return info.mKey; }); [](const LuaUtil::InputTrigger::Info& info) -> std::string_view { return info.mKey; });
@ -126,6 +146,8 @@ namespace MWLua
[](const LuaUtil::InputTrigger::Info& info) -> std::string_view { return info.mDescription; }); [](const LuaUtil::InputTrigger::Info& info) -> std::string_view { return info.mDescription; });
triggerInfo["l10n"] = sol::readonly_property( triggerInfo["l10n"] = sol::readonly_property(
[](const LuaUtil::InputTrigger::Info& info) -> std::string_view { return info.mL10n; }); [](const LuaUtil::InputTrigger::Info& info) -> std::string_view { return info.mL10n; });
return sol::table(lua, sol::create);
});
MWBase::InputManager* input = MWBase::Environment::get().getInputManager(); MWBase::InputManager* input = MWBase::Environment::get().getInputManager();
sol::table api(lua, sol::create); sol::table api(lua, sol::create);
@ -449,8 +471,8 @@ namespace MWLua
{ "Tab", SDL_SCANCODE_TAB }, { "Tab", SDL_SCANCODE_TAB },
})); }));
lua["openmw_input"] = LuaUtil::makeReadOnly(api); sol::table readOnlyApi = LuaUtil::makeReadOnly(api);
return lua["openmw_input"]; return context.setTypePackage(readOnlyApi, "openmw_input");
} }
} }