From c68dee214ee20b11ad03a7b4a50b15c7dc156246 Mon Sep 17 00:00:00 2001 From: uramer Date: Sat, 10 Feb 2024 22:53:29 +0100 Subject: [PATCH] Mouse input engine handlers --- apps/openmw/mwbase/luamanager.hpp | 11 ++++++++++- apps/openmw/mwinput/mousemanager.cpp | 11 +++++++++++ apps/openmw/mwlua/inputprocessor.hpp | 15 ++++++++++++++- .../reference/lua-scripting/engine_handlers.rst | 9 +++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/apps/openmw/mwbase/luamanager.hpp b/apps/openmw/mwbase/luamanager.hpp index e865756408..69693d47a2 100644 --- a/apps/openmw/mwbase/luamanager.hpp +++ b/apps/openmw/mwbase/luamanager.hpp @@ -81,6 +81,12 @@ namespace MWBase struct InputEvent { + struct WheelChange + { + int x; + int y; + }; + enum { KeyPressed, @@ -91,8 +97,11 @@ namespace MWBase TouchPressed, TouchReleased, TouchMoved, + MouseButtonPressed, + MouseButtonReleased, + MouseWheel, } mType; - std::variant mValue; + std::variant mValue; }; virtual void inputEvent(const InputEvent& event) = 0; diff --git a/apps/openmw/mwinput/mousemanager.cpp b/apps/openmw/mwinput/mousemanager.cpp index 55b50b91ae..91ccd4e0a7 100644 --- a/apps/openmw/mwinput/mousemanager.cpp +++ b/apps/openmw/mwinput/mousemanager.cpp @@ -10,6 +10,7 @@ #include "../mwbase/environment.hpp" #include "../mwbase/inputmanager.hpp" +#include "../mwbase/luamanager.hpp" #include "../mwbase/windowmanager.hpp" #include "../mwbase/world.hpp" @@ -118,6 +119,8 @@ namespace MWInput mBindingsManager->setPlayerControlsEnabled(!guiMode); mBindingsManager->mouseReleased(arg, id); + MWBase::Environment::get().getLuaManager()->inputEvent( + { MWBase::LuaManager::InputEvent::MouseButtonReleased, arg.button }); } } @@ -125,7 +128,11 @@ namespace MWInput { MWBase::InputManager* input = MWBase::Environment::get().getInputManager(); if (mBindingsManager->isDetectingBindingState() || !input->controlsDisabled()) + { mBindingsManager->mouseWheelMoved(arg); + MWBase::Environment::get().getLuaManager()->inputEvent({ MWBase::LuaManager::InputEvent::MouseWheel, + MWBase::LuaManager::InputEvent::WheelChange{ arg.x, arg.y } }); + } input->setJoystickLastUsed(false); } @@ -161,7 +168,11 @@ namespace MWInput const MWGui::SettingsWindow* settingsWindow = MWBase::Environment::get().getWindowManager()->getSettingsWindow(); if ((!settingsWindow || !settingsWindow->isVisible()) && !input->controlsDisabled()) + { mBindingsManager->mousePressed(arg, id); + MWBase::Environment::get().getLuaManager()->inputEvent( + { MWBase::LuaManager::InputEvent::MouseButtonPressed, arg.button }); + } } void MouseManager::updateCursorMode() diff --git a/apps/openmw/mwlua/inputprocessor.hpp b/apps/openmw/mwlua/inputprocessor.hpp index e005183098..dcd19ae8cd 100644 --- a/apps/openmw/mwlua/inputprocessor.hpp +++ b/apps/openmw/mwlua/inputprocessor.hpp @@ -18,7 +18,7 @@ namespace MWLua { mScriptsContainer->registerEngineHandlers({ &mKeyPressHandlers, &mKeyReleaseHandlers, &mControllerButtonPressHandlers, &mControllerButtonReleaseHandlers, &mActionHandlers, &mTouchpadPressed, - &mTouchpadReleased, &mTouchpadMoved }); + &mTouchpadReleased, &mTouchpadMoved, &mMouseButtonPress, &mMouseButtonRelease, &mMouseWheel }); } void processInputEvent(const MWBase::LuaManager::InputEvent& event) @@ -53,6 +53,16 @@ namespace MWLua case InputEvent::TouchMoved: mScriptsContainer->callEngineHandlers(mTouchpadMoved, std::get(event.mValue)); break; + case InputEvent::MouseButtonPressed: + mScriptsContainer->callEngineHandlers(mMouseButtonPress, std::get(event.mValue)); + break; + case InputEvent::MouseButtonReleased: + mScriptsContainer->callEngineHandlers(mMouseButtonRelease, std::get(event.mValue)); + break; + case InputEvent::MouseWheel: + auto wheelEvent = std::get(event.mValue); + mScriptsContainer->callEngineHandlers(mMouseWheel, wheelEvent.y, wheelEvent.x); + break; } } @@ -66,6 +76,9 @@ namespace MWLua typename Container::EngineHandlerList mTouchpadPressed{ "onTouchPress" }; typename Container::EngineHandlerList mTouchpadReleased{ "onTouchRelease" }; typename Container::EngineHandlerList mTouchpadMoved{ "onTouchMove" }; + typename Container::EngineHandlerList mMouseButtonPress{ "onMouseButtonPress" }; + typename Container::EngineHandlerList mMouseButtonRelease{ "onMouseButtonRelease" }; + typename Container::EngineHandlerList mMouseWheel{ "onMouseWheel" }; }; } diff --git a/docs/source/reference/lua-scripting/engine_handlers.rst b/docs/source/reference/lua-scripting/engine_handlers.rst index 754a63b314..2b5e99e6ae 100644 --- a/docs/source/reference/lua-scripting/engine_handlers.rst +++ b/docs/source/reference/lua-scripting/engine_handlers.rst @@ -124,6 +124,15 @@ Engine handler is a function defined by a script, that can be called by the engi * - onTouchMove(touchEvent) - | A finger moved on a touch device. | `Touch event `_. + * - onMouseButtonPress(button) + - | A mouse button was pressed + | Button id + * - onMouseButtonRelease(button) + - | A mouse button was released + | Button id + * - onMouseWheel(vertical, horizontal) + - | Mouse wheel was scrolled + | vertical and horizontal mouse wheel change * - | onConsoleCommand( | mode, command, selectedObject) - | User entered `command` in in-game console. Called if either