From af14d60493f8136ce241e66b2601f78209fcebf9 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sat, 12 Dec 2015 18:30:50 +0100 Subject: [PATCH] Added MouseScroll event to input manager --- include/Engine/Core/EMouseScroll.h | 17 ++++++++++++++++ include/Engine/Core/InputManager.h | 5 ++++- src/Engine/Core/InputManager.cpp | 31 +++++++++++++++++++++++------- 3 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 include/Engine/Core/EMouseScroll.h diff --git a/include/Engine/Core/EMouseScroll.h b/include/Engine/Core/EMouseScroll.h new file mode 100644 index 00000000..8b2826a7 --- /dev/null +++ b/include/Engine/Core/EMouseScroll.h @@ -0,0 +1,17 @@ +#ifndef Events_MouseScroll_h__ +#define Events_MouseScroll_h__ + +#include "EventBroker.h" + +namespace Events +{ + +struct MouseScroll : Event +{ + double DeltaX; + double DeltaY; +}; + +} + +#endif diff --git a/include/Engine/Core/InputManager.h b/include/Engine/Core/InputManager.h index 063e691e..0d53169e 100644 --- a/include/Engine/Core/InputManager.h +++ b/include/Engine/Core/InputManager.h @@ -12,6 +12,7 @@ #include "EMousePress.h" #include "EMouseRelease.h" #include "EMouseMove.h" +#include "EMouseScroll.h" #include "ELockMouse.h" #include "EGamepadAxis.h" #include "EGamepadButton.h" @@ -66,8 +67,10 @@ private: void PublishGamepadAxisIfChanged(int gamepadID, Gamepad::Axis axis); void PublishGamepadButtonIfChanged(int gamepadID, Gamepad::Button button); - static std::vector CharCallbackQueue; + static std::vector GLFWCharCallbackQueue; static void GLFWCharCallback(GLFWwindow* window, unsigned int c); + static std::vector> GLFWScrollCallbackQueue; + static void GLFWScrollCallback(GLFWwindow* window, double xoffset, double yoffset); }; #endif diff --git a/src/Engine/Core/InputManager.cpp b/src/Engine/Core/InputManager.cpp index 4fd2e2a5..cb901cb8 100644 --- a/src/Engine/Core/InputManager.cpp +++ b/src/Engine/Core/InputManager.cpp @@ -1,6 +1,7 @@ #include "Core/InputManager.h" -std::vector InputManager::CharCallbackQueue; +std::vector InputManager::GLFWCharCallbackQueue; +std::vector> InputManager::GLFWScrollCallbackQueue; void InputManager::Initialize() { @@ -8,6 +9,7 @@ void InputManager::Initialize() //m_LastGamepadAxisState = std::array(); //m_LastGamepadButtonState = std::array(); glfwSetCharCallback(m_GLFWWindow, &InputManager::GLFWCharCallback); + glfwSetScrollCallback(m_GLFWWindow, &InputManager::GLFWScrollCallback); EVENT_SUBSCRIBE_MEMBER(m_ELockMouse, &InputManager::OnLockMouse); EVENT_SUBSCRIBE_MEMBER(m_EUnlockMouse, &InputManager::OnUnlockMouse); @@ -39,6 +41,15 @@ void InputManager::Update(double dt) } } + // Keyboard text input + for (unsigned int& c : GLFWCharCallbackQueue) { + Events::KeyboardChar e; + e.Timestamp = glfwGetTime(); + e.Char = c; + m_EventBroker->Publish(e); + } + GLFWCharCallbackQueue.clear(); + // Mouse buttons for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i) { m_CurrentMouseState[i] = glfwGetMouseButton(m_GLFWWindow, i); @@ -76,13 +87,13 @@ void InputManager::Update(double dt) m_EventBroker->Publish(e); } - for (unsigned int& c : CharCallbackQueue) { - Events::KeyboardChar e; - e.Timestamp = glfwGetTime(); - e.Char = c; + // Mouse scroll + for (auto& pair : GLFWScrollCallbackQueue) { + Events::MouseScroll e; + std::tie(e.DeltaX, e.DeltaY) = pair; m_EventBroker->Publish(e); } - CharCallbackQueue.clear(); + GLFWScrollCallbackQueue.clear(); // // Lock mouse while holding LMB // if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT]) @@ -209,7 +220,13 @@ void InputManager::PublishGamepadButtonIfChanged(int gamepadID, Gamepad::Button void InputManager::GLFWCharCallback(GLFWwindow* window, unsigned int c) { - CharCallbackQueue.push_back(c); + GLFWCharCallbackQueue.push_back(c); +} + + +void InputManager::GLFWScrollCallback(GLFWwindow* window, double xoffset, double yoffset) +{ + GLFWScrollCallbackQueue.push_back(std::make_pair(xoffset, yoffset)); } bool InputManager::OnLockMouse(const Events::LockMouse &event)