From 30474b70df67aa57e5b6bde07350ada014d7e5eb Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 11 Dec 2015 18:03:48 +0100 Subject: [PATCH] MouseInputHandler --- include/Engine/Input/InputProxy.h | 3 +- include/Engine/Input/MouseInputHandler.h | 5 ++ .../Rendering/DebugCameraInputController.h | 48 +++++++++++++++++++ src/Engine/Input/InputProxy.cpp | 21 ++++++++ src/Engine/Input/MouseInputHandler.cpp | 34 ++++++++----- src/Engine/Rendering/Renderer.cpp | 31 +++--------- src/Game/Game.cpp | 1 + 7 files changed, 104 insertions(+), 39 deletions(-) create mode 100644 include/Engine/Rendering/DebugCameraInputController.h diff --git a/include/Engine/Input/InputProxy.h b/include/Engine/Input/InputProxy.h index 43c4940c..c7817c22 100644 --- a/include/Engine/Input/InputProxy.h +++ b/include/Engine/Input/InputProxy.h @@ -20,6 +20,7 @@ public: void Process(); template void AddHandler(); + void Publish(const Events::InputCommand& e); protected: EventBroker* m_EventBroker; @@ -27,7 +28,7 @@ protected: std::map> m_CommandHandlers; // Represents every unique command (has of PlayerID & Command) and all values reported for that command this frame - //std::map, std::vector> m_CommandQueue; + std::map, std::vector> m_CommandQueue; std::map m_CurrentCommandValues; std::map m_LastCommandValues; diff --git a/include/Engine/Input/MouseInputHandler.h b/include/Engine/Input/MouseInputHandler.h index 39889755..b9193a2d 100644 --- a/include/Engine/Input/MouseInputHandler.h +++ b/include/Engine/Input/MouseInputHandler.h @@ -13,12 +13,15 @@ public: MouseInputHandler(EventBroker* eventBroker, InputProxy* inputProxy); bool BindOrigin(std::string origin, std::string command, float value) override; + virtual float GetCommandValue(std::string command) 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 + std::unordered_map m_CommandValues; + std::unordered_map m_ContinuousCommandValues; EventRelay m_EMousePress; bool OnMousePress(const Events::MousePress& e); @@ -28,6 +31,8 @@ private: bool OnMouseMove(const Events::MouseMove& e); bool hasOrigin(std::string origin); + + }; #endif diff --git a/include/Engine/Rendering/DebugCameraInputController.h b/include/Engine/Rendering/DebugCameraInputController.h new file mode 100644 index 00000000..551ae0f2 --- /dev/null +++ b/include/Engine/Rendering/DebugCameraInputController.h @@ -0,0 +1,48 @@ +#include "../Input/FirstPersonInputController.h" + +template +class DebugCameraInputController : public FirstPersonInputController +{ +public: + DebugCameraInputController(EventBroker* eventBroker, unsigned int playerID) + : FirstPersonInputController(eventBroker, playerID) + { } + + const glm::vec3 Position() const { return m_Position; } + void SetBaseSpeed(float speed) { m_BaseSpeed = speed; } + + virtual bool OnCommand(const Events::InputCommand& e) override + { + if (e.Command == "PrimaryFire") { + if (e.Value > 0) { + LockMouse(); + } else { + UnlockMouse(); + } + return false; + } + + if (e.Command == "Right") { + float value = std::max(-1.f, std::min(e.Value, 1.f)); + m_Velocity.x = value; + } + if (e.Command == "Forward") { + float value = std::max(-1.f, std::min(e.Value, 1.f)); + m_Velocity.z = -value; + } + + return FirstPersonInputController::OnCommand(e); + } + + void Update(double dt) + { + if (glm::length2(m_Velocity) > 0) { + m_Position += m_Orientation * (glm::normalize(m_Velocity) * m_BaseSpeed); + } + } + +protected: + glm::vec3 m_Position = glm::vec3(0, 0, 0); + glm::vec3 m_Velocity = glm::vec3(0, 0, 0); + float m_BaseSpeed = 0.1f; +}; \ No newline at end of file diff --git a/src/Engine/Input/InputProxy.cpp b/src/Engine/Input/InputProxy.cpp index 3fe29b7d..c3e4d669 100644 --- a/src/Engine/Input/InputProxy.cpp +++ b/src/Engine/Input/InputProxy.cpp @@ -66,6 +66,27 @@ void InputProxy::Process() m_LastCommandValues[command] = currentValue; } } + + // Accumulate the input values of all unique commands published by input handlers + for (auto& pair : m_CommandQueue) { + Events::InputCommand e; + e.PlayerID = pair.first.first; + e.Command = pair.first.second; + e.Value = 0; + for (auto& value : pair.second) { + e.Value += value; + } + //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); + } + m_CommandQueue.clear(); +} + +void InputProxy::Publish(const Events::InputCommand& e) +{ + auto key = std::make_pair(e.PlayerID, e.Command); + m_CommandQueue[key].push_back(e.Value); } bool InputProxy::OnBindOrigin(const Events::BindOrigin& e) diff --git a/src/Engine/Input/MouseInputHandler.cpp b/src/Engine/Input/MouseInputHandler.cpp index 2e9c8137..5996b43d 100644 --- a/src/Engine/Input/MouseInputHandler.cpp +++ b/src/Engine/Input/MouseInputHandler.cpp @@ -50,6 +50,16 @@ bool MouseInputHandler::BindOrigin(std::string origin, std::string command, floa return false; } +float MouseInputHandler::GetCommandValue(std::string command) +{ + auto it = m_CommandValues.find(command); + if (it != m_CommandValues.end()) { + return m_CommandValues[command]; + } else { + return 0.f; + } +} + bool MouseInputHandler::OnMousePress(const Events::MousePress& e) { auto it = m_Bindings.find(e.Button); @@ -57,10 +67,10 @@ bool MouseInputHandler::OnMousePress(const Events::MousePress& e) return false; } - Events::InputCommand ic; - ic.PlayerID = 0; - std::tie(ic.Command, ic.Value) = it->second; - m_InputProxy->Publish(ic); + std::string command; + float value; + std::tie(command, value) = it->second; + m_CommandValues[command] += value; return true; } @@ -72,11 +82,10 @@ bool MouseInputHandler::OnMouseRelease(const Events::MouseRelease& e) return false; } - Events::InputCommand ic; - ic.PlayerID = 0; - std::tie(ic.Command, std::ignore) = it->second; - ic.Value = 0; - m_InputProxy->Publish(ic); + std::string command; + float value; + std::tie(command, value) = it->second; + m_CommandValues[command] -= value; return true; } @@ -87,7 +96,7 @@ bool MouseInputHandler::OnMouseMove(const Events::MouseMove& e) auto it = m_Axes.find('X'); if (it != m_Axes.end()) { Events::InputCommand ic; - ic.PlayerID = 0; + ic.PlayerID = -1; std::tie(ic.Command, ic.Value) = it->second; ic.Value *= e.DeltaX; m_InputProxy->Publish(ic); @@ -98,7 +107,7 @@ bool MouseInputHandler::OnMouseMove(const Events::MouseMove& e) auto it = m_Axes.find('Y'); if (it != m_Axes.end()) { Events::InputCommand ic; - ic.PlayerID = 0; + ic.PlayerID = -1; std::tie(ic.Command, ic.Value) = it->second; ic.Value *= e.DeltaY; m_InputProxy->Publish(ic); @@ -117,5 +126,4 @@ bool MouseInputHandler::hasOrigin(std::string origin) return false; } return true; -} - +} \ No newline at end of file diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index 6f4235d2..3bc1d9da 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -1,4 +1,5 @@ #include "Rendering/Renderer.h" +#include "Rendering/DebugCameraInputController.h" void Renderer::Initialize() { @@ -83,6 +84,8 @@ void Renderer::InitializeShaders() void Renderer::InputUpdate(double dt) { + static DebugCameraInputController firstPersonInputController(m_EventBroker, -1); + glm::vec3 m_Position = m_Camera->Position(); if (glfwGetKey(m_Window, GLFW_KEY_O) == GLFW_PRESS) { @@ -112,37 +115,15 @@ void Renderer::InputUpdate(double dt) m_CameraMoveSpeed = 0.5f; } - - static double mousePosX, mousePosY; - glfwGetCursorPos(m_Window, &mousePosX, &mousePosY); - - if (glfwGetKey(m_Window, GLFW_KEY_SPACE) == GLFW_PRESS) { - - - double deltaX, deltaY; - deltaX = mousePosX - (float)Resolution().Width / 2; - deltaY = mousePosY - (float)Resolution().Height / 2; - - float rotationY = -deltaY / 300.f; - float rotationX = -deltaX / 300.f; - glm::quat orientation = m_Camera->Orientation(); - - - orientation = orientation * glm::angleAxis(rotationY, glm::vec3(1, 0, 0)); - orientation = glm::angleAxis(rotationX, glm::vec3(0, 1, 0)) * orientation; - - m_Camera->SetOrientation(orientation); - - glfwSetCursorPos(m_Window, Resolution().Width / 2, Resolution().Height / 2); - } - m_Camera->SetPosition(m_Position); + firstPersonInputController.Update(dt); + m_Camera->SetOrientation(firstPersonInputController.Orientation()); + m_Camera->SetPosition(firstPersonInputController.Position()); } void Renderer::Update(double dt) { m_EventBroker->Process(); InputUpdate(dt); - } void Renderer::Draw(RenderQueueCollection& rq) diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index b7391bac..026857d2 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -71,6 +71,7 @@ void Game::Tick() m_LastTime = currentTime; // Handle input in a weird looking but responsive way + m_EventBroker->Process(); m_EventBroker->Swap(); m_InputManager->Update(dt); m_EventBroker->Swap();