From 013f911ae262f71e6679f9aa6282aabd023e63ca Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 11 Dec 2015 14:49:43 +0100 Subject: [PATCH] Added MouseInputHandler and firstPersonInputController to aid in creation of first person movement. --- include/Engine/Core/InputController.h | 11 +- .../Engine/Input/FirstPersonInputController.h | 40 ++++++ include/Engine/Input/MouseInputHandler.h | 33 +++++ include/Game/Game.h | 1 + resources/DefaultInput.ini | 4 + src/Engine/Input/InputProxy.cpp | 2 +- src/Engine/Input/MouseInputHandler.cpp | 121 ++++++++++++++++++ src/Game/Game.cpp | 1 + 8 files changed, 205 insertions(+), 8 deletions(-) create mode 100644 include/Engine/Input/FirstPersonInputController.h create mode 100644 include/Engine/Input/MouseInputHandler.h create mode 100644 src/Engine/Input/MouseInputHandler.cpp diff --git a/include/Engine/Core/InputController.h b/include/Engine/Core/InputController.h index b91fb448..b87d1eff 100644 --- a/include/Engine/Core/InputController.h +++ b/include/Engine/Core/InputController.h @@ -11,25 +11,22 @@ template class InputController { public: - InputController(std::shared_ptr eventBroker) - : EventBroker(eventBroker) + InputController(EventBroker* eventBroker) + : m_EventBroker(eventBroker) { Initialize(); } virtual void Initialize() { EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &InputController::OnCommand); - EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &InputController::OnMouseMove); } - virtual bool OnCommand(const Events::InputCommand &event) { return false; } - virtual bool OnMouseMove(const Events::MouseMove &event) { return false; } + virtual bool OnCommand(const Events::InputCommand& e) { return false; } protected: - std::shared_ptr EventBroker; + EventBroker* m_EventBroker; private: EventRelay m_EInputCommand; - EventRelay m_EMouseMove; }; #endif diff --git a/include/Engine/Input/FirstPersonInputController.h b/include/Engine/Input/FirstPersonInputController.h new file mode 100644 index 00000000..e8b832b5 --- /dev/null +++ b/include/Engine/Input/FirstPersonInputController.h @@ -0,0 +1,40 @@ +#ifndef FirstPersonInputController_h__ +#define FirstPersonInputController_h__ + +#include "../GLM.h" +#include "../Core/InputController.h" + +template +class FirstPersonInputController : public InputController +{ +public: + FirstPersonInputController(EventBroker* eventBroker, unsigned int playerID) + : InputController(eventBroker) + , m_PlayerID(playerID) + { } + + const glm::quat Orientation() const { return m_Orientation; } + + virtual bool OnCommand(const Events::InputCommand& e) override + { + if (m_PlayerID != e.PlayerID) { + return false; + } + + if (e.Command == "Pitch") { + float val = glm::radians(e.Value); + m_Orientation = m_Orientation * glm::angleAxis(-val, glm::vec3(1, 0, 0)); + } + + if (e.Command == "Yaw") { + float val = glm::radians(e.Value); + m_Orientation = glm::angleAxis(-val, glm::vec3(0, 1, 0)) * m_Orientation; + } + } + +private: + const unsigned int m_PlayerID; + glm::quat m_Orientation; +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Input/MouseInputHandler.h b/include/Engine/Input/MouseInputHandler.h new file mode 100644 index 00000000..39889755 --- /dev/null +++ b/include/Engine/Input/MouseInputHandler.h @@ -0,0 +1,33 @@ +#ifndef MouseInputHandler_h__ +#define MouseInputHandler_h__ + +#include +#include "InputHandler.h" +#include "Core/EMousePress.h" +#include "Core/EMouseRelease.h" +#include "Core/EMouseMove.h" + +class MouseInputHandler : public InputHandler +{ +public: + MouseInputHandler(EventBroker* eventBroker, InputProxy* inputProxy); + + bool BindOrigin(std::string origin, std::string command, float value) override; + +private: + std::unordered_map m_OriginCodes; + std::unordered_map m_OriginAxes; + std::unordered_map> m_Bindings; // GLFW_MOUSE_BUTTON... -> command string & value + std::unordered_map> m_Axes; // Axis -> command string & value + + EventRelay m_EMousePress; + bool OnMousePress(const Events::MousePress& e); + EventRelay m_EMouseRelease; + bool OnMouseRelease(const Events::MouseRelease& e); + EventRelay m_EMouseMove; + bool OnMouseMove(const Events::MouseMove& e); + + bool hasOrigin(std::string origin); +}; + +#endif diff --git a/include/Game/Game.h b/include/Game/Game.h index fbd4b017..57941e55 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -11,6 +11,7 @@ #include "Rendering/RenderQueueFactory.h" #include "Input/InputProxy.h" #include "Input/KeyboardInputHandler.h" +#include "Input/MouseInputHandler.h" #include "Core/EKeyDown.h" #include "Core/EntityXMLFile.h" #include "Core/SystemPipeline.h" diff --git a/resources/DefaultInput.ini b/resources/DefaultInput.ini index bc012c52..b6bbd20f 100644 --- a/resources/DefaultInput.ini +++ b/resources/DefaultInput.ini @@ -1,3 +1,7 @@ +[Mouse] +Sensitivity=0.5 +InvertPitch=false + [Bindings] W=Forward A=Left diff --git a/src/Engine/Input/InputProxy.cpp b/src/Engine/Input/InputProxy.cpp index 65f3f7b4..430cb250 100644 --- a/src/Engine/Input/InputProxy.cpp +++ b/src/Engine/Input/InputProxy.cpp @@ -46,7 +46,7 @@ void InputProxy::Process() for (auto& value : pair.second) { e.Value += value; } - e.Value = std::max(-1.f, std::min(e.Value, 1.f)); + //e.Value = std::max(-1.f, std::min(e.Value, 1.f)); m_EventBroker->Publish(e); LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); } diff --git a/src/Engine/Input/MouseInputHandler.cpp b/src/Engine/Input/MouseInputHandler.cpp new file mode 100644 index 00000000..2e9c8137 --- /dev/null +++ b/src/Engine/Input/MouseInputHandler.cpp @@ -0,0 +1,121 @@ +#include "Input/MouseInputHandler.h" + +MouseInputHandler::MouseInputHandler(EventBroker* eventBroker, InputProxy* inputProxy) + : InputHandler(eventBroker, inputProxy) +{ + EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &MouseInputHandler::OnMousePress); + EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &MouseInputHandler::OnMouseRelease); + EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &MouseInputHandler::OnMouseMove); + + m_OriginCodes["Mouse1"] = GLFW_MOUSE_BUTTON_1; + m_OriginCodes["MouseLeft"] = GLFW_MOUSE_BUTTON_LEFT; + m_OriginCodes["Mouse2"] = GLFW_MOUSE_BUTTON_2; + m_OriginCodes["MouseRight"] = GLFW_MOUSE_BUTTON_RIGHT; + m_OriginCodes["Mouse3"] = GLFW_MOUSE_BUTTON_3; + m_OriginCodes["MouseMiddle"] = GLFW_MOUSE_BUTTON_MIDDLE; + m_OriginCodes["Mouse4"] = GLFW_MOUSE_BUTTON_4; + m_OriginCodes["Mouse5"] = GLFW_MOUSE_BUTTON_5; + m_OriginCodes["Mouse6"] = GLFW_MOUSE_BUTTON_6; + m_OriginCodes["Mouse7"] = GLFW_MOUSE_BUTTON_7; + m_OriginCodes["Mouse8"] = GLFW_MOUSE_BUTTON_8; + + m_OriginAxes["MouseX"] = 'X'; + m_OriginAxes["MouseY"] = 'Y'; +} + +bool MouseInputHandler::BindOrigin(std::string origin, std::string command, float value) +{ + auto originCode = m_OriginCodes.find(origin); + if (originCode != m_OriginCodes.end()) { + int code = originCode->second; + m_Bindings[code] = std::make_tuple(command, value); + return true; + } + + auto originAxis = m_OriginAxes.find(origin); + if (originAxis != m_OriginAxes.end()) { + char axis = originAxis->second; + float multiplier = 1.f; + // Sensitivity + multiplier *= ResourceManager::Load("Input.ini")->Get("Mouse.Sensitivity", 1.f); + if (axis == 'Y') { + if (ResourceManager::Load("Input.ini")->Get("Mouse.InvertPitch", false)) { + multiplier *= -1.f; + } + } + m_Axes[axis] = std::make_tuple(command, value * multiplier); + return true; + } + + return false; +} + +bool MouseInputHandler::OnMousePress(const Events::MousePress& e) +{ + auto it = m_Bindings.find(e.Button); + if (it == m_Bindings.end()) { + return false; + } + + Events::InputCommand ic; + ic.PlayerID = 0; + std::tie(ic.Command, ic.Value) = it->second; + m_InputProxy->Publish(ic); + + return true; +} + +bool MouseInputHandler::OnMouseRelease(const Events::MouseRelease& e) +{ + auto it = m_Bindings.find(e.Button); + if (it == m_Bindings.end()) { + return false; + } + + Events::InputCommand ic; + ic.PlayerID = 0; + std::tie(ic.Command, std::ignore) = it->second; + ic.Value = 0; + m_InputProxy->Publish(ic); + + return true; +} + +bool MouseInputHandler::OnMouseMove(const Events::MouseMove& e) +{ + if (std::abs(e.DeltaX) > 0) { + auto it = m_Axes.find('X'); + if (it != m_Axes.end()) { + Events::InputCommand ic; + ic.PlayerID = 0; + std::tie(ic.Command, ic.Value) = it->second; + ic.Value *= e.DeltaX; + m_InputProxy->Publish(ic); + } + } + + if (std::abs(e.DeltaY) > 0) { + auto it = m_Axes.find('Y'); + if (it != m_Axes.end()) { + Events::InputCommand ic; + ic.PlayerID = 0; + std::tie(ic.Command, ic.Value) = it->second; + ic.Value *= e.DeltaY; + m_InputProxy->Publish(ic); + } + } + + return true; +} + +bool MouseInputHandler::hasOrigin(std::string origin) +{ + if (m_OriginCodes.find(origin) == m_OriginCodes.end()) { + return false; + } + if (m_OriginAxes.find(origin) == m_OriginAxes.end()) { + return false; + } + return true; +} + diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 5ae9295a..be988529 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -31,6 +31,7 @@ Game::Game(int argc, char* argv[]) m_InputManager = new InputManager(m_Renderer->Window(), m_EventBroker); m_InputProxy = new InputProxy(m_EventBroker); m_InputProxy->AddHandler(); + m_InputProxy->AddHandler(); m_InputProxy->LoadBindings("Input.ini"); // Create the root level GUI frame