From 33b14b1859f1262dab5e633c002d0542d4cfdd81 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sat, 17 May 2014 00:49:23 +0200 Subject: [PATCH] Mouse locking event --- src/Events/LockMouse.h | 14 ++++++++ src/InputManager.cpp | 35 ++++++++++++++----- src/InputManager.h | 18 +++++++--- vs11/Returngeance/Returngeance.vcxproj | 1 + .../Returngeance/Returngeance.vcxproj.filters | 3 ++ 5 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 src/Events/LockMouse.h diff --git a/src/Events/LockMouse.h b/src/Events/LockMouse.h new file mode 100644 index 0000000..b045c55 --- /dev/null +++ b/src/Events/LockMouse.h @@ -0,0 +1,14 @@ +#ifndef Events_LockMouse_h__ +#define Events_LockMouse_h__ + +#include "EventBroker.h" + +namespace Events +{ + +struct LockMouse : Event { }; +struct UnlockMouse : Event { }; + +} + +#endif // Events_LockMouse_h__ \ No newline at end of file diff --git a/src/InputManager.cpp b/src/InputManager.cpp index fc225ec..edbb736 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -5,6 +5,9 @@ void InputManager::Initialize() { m_LastGamepadAxisState = std::array(); m_LastGamepadButtonState = std::array(); + + EVENT_SUBSCRIBE_MEMBER(m_ELockMouse, &InputManager::OnLockMouse); + EVENT_SUBSCRIBE_MEMBER(m_EUnlockMouse, &InputManager::OnUnlockMouse); } void InputManager::Update(double dt) @@ -20,13 +23,13 @@ void InputManager::Update(double dt) { Events::KeyDown e; e.KeyCode = i; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } else { Events::KeyUp e; e.KeyCode = i; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } } } @@ -42,13 +45,13 @@ void InputManager::Update(double dt) { Events::MousePress e; e.Button = i; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } else { Events::MouseRelease e; e.Button = i; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } } } @@ -65,7 +68,7 @@ void InputManager::Update(double dt) e.Y = m_CurrentMouseY; e.DeltaX = m_CurrentMouseDeltaX; e.DeltaY = m_CurrentMouseDeltaY; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } // // Lock mouse while holding LMB @@ -156,7 +159,7 @@ void InputManager::PublishGamepadAxisIfChanged(int gamepadID, Gamepad::Axis axis e.GamepadID = gamepadID; e.Axis = axis; e.Value = currentValue; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } } @@ -171,14 +174,30 @@ void InputManager::PublishGamepadButtonIfChanged(int gamepadID, Gamepad::Button Events::GamepadButtonDown e; e.GamepadID = gamepadID; e.Button = button; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } else { Events::GamepadButtonUp e; e.GamepadID = gamepadID; e.Button = button; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } } } + +bool InputManager::OnLockMouse(const Events::LockMouse &event) +{ + m_MouseLocked = true; + glfwSetInputMode(m_GLFWWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + return true; +} + +bool InputManager::OnUnlockMouse(const Events::UnlockMouse &event) +{ + m_MouseLocked = false; + glfwSetInputMode(m_GLFWWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + + return true; +} \ No newline at end of file diff --git a/src/InputManager.h b/src/InputManager.h index 41d77e7..591923e 100644 --- a/src/InputManager.h +++ b/src/InputManager.h @@ -11,22 +11,24 @@ #include "Events/MousePress.h" #include "Events/MouseRelease.h" #include "Events/MouseMove.h" +#include "Events/LockMouse.h" #include "Events/GamepadAxis.h" #include "Events/GamepadButton.h" class InputManager { public: - InputManager(GLFWwindow* window, std::shared_ptr eventBroker) + InputManager(GLFWwindow* window, std::shared_ptr<::EventBroker> eventBroker) : m_GLFWWindow(window) - , m_EventBroker(eventBroker) + , EventBroker(eventBroker) , m_CurrentKeyState() , m_LastKeyState() , m_CurrentMouseState() , m_LastMouseState() , m_CurrentMouseX(0), m_CurrentMouseY(0) , m_LastMouseX(0), m_LastMouseY(0) - , m_CurrentMouseDeltaX(0), m_CurrentMouseDeltaY(0) + , m_CurrentMouseDeltaX(0), m_CurrentMouseDeltaY(0) + , m_MouseLocked(false) { Initialize(); } void Initialize(); @@ -35,8 +37,13 @@ public: private: GLFWwindow* m_GLFWWindow; - std::shared_ptr m_EventBroker; - + std::shared_ptr<::EventBroker> EventBroker; + + EventRelay m_ELockMouse; + bool OnLockMouse(const Events::LockMouse &event); + EventRelay m_EUnlockMouse; + bool OnUnlockMouse(const Events::UnlockMouse &event); + std::array m_CurrentKeyState; std::array m_LastKeyState; std::array m_CurrentMouseState; @@ -51,6 +58,7 @@ private: double m_CurrentMouseX, m_CurrentMouseY; double m_LastMouseX, m_LastMouseY; double m_CurrentMouseDeltaX, m_CurrentMouseDeltaY; + bool m_MouseLocked; void PublishGamepadAxisIfChanged(int gamepadID, Gamepad::Axis axis); void PublishGamepadButtonIfChanged(int gamepadID, Gamepad::Button button); diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 5226852..9849fc0 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -161,6 +161,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index f0252d5..49cc263 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -353,6 +353,9 @@ Input\Events + + Input\Events +