Added MouseScroll event to input manager

This commit is contained in:
2015-12-12 18:30:50 +01:00
parent 0465fdb16d
commit af14d60493
3 changed files with 45 additions and 8 deletions
+17
View File
@@ -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
+4 -1
View File
@@ -12,6 +12,7 @@
#include "EMousePress.h" #include "EMousePress.h"
#include "EMouseRelease.h" #include "EMouseRelease.h"
#include "EMouseMove.h" #include "EMouseMove.h"
#include "EMouseScroll.h"
#include "ELockMouse.h" #include "ELockMouse.h"
#include "EGamepadAxis.h" #include "EGamepadAxis.h"
#include "EGamepadButton.h" #include "EGamepadButton.h"
@@ -66,8 +67,10 @@ private:
void PublishGamepadAxisIfChanged(int gamepadID, Gamepad::Axis axis); void PublishGamepadAxisIfChanged(int gamepadID, Gamepad::Axis axis);
void PublishGamepadButtonIfChanged(int gamepadID, Gamepad::Button button); void PublishGamepadButtonIfChanged(int gamepadID, Gamepad::Button button);
static std::vector<unsigned int> CharCallbackQueue; static std::vector<unsigned int> GLFWCharCallbackQueue;
static void GLFWCharCallback(GLFWwindow* window, unsigned int c); static void GLFWCharCallback(GLFWwindow* window, unsigned int c);
static std::vector<std::pair<double, double>> GLFWScrollCallbackQueue;
static void GLFWScrollCallback(GLFWwindow* window, double xoffset, double yoffset);
}; };
#endif #endif
+24 -7
View File
@@ -1,6 +1,7 @@
#include "Core/InputManager.h" #include "Core/InputManager.h"
std::vector<unsigned int> InputManager::CharCallbackQueue; std::vector<unsigned int> InputManager::GLFWCharCallbackQueue;
std::vector<std::pair<double, double>> InputManager::GLFWScrollCallbackQueue;
void InputManager::Initialize() void InputManager::Initialize()
{ {
@@ -8,6 +9,7 @@ void InputManager::Initialize()
//m_LastGamepadAxisState = std::array<GamepadAxisState, XUSER_MAX_COUNT>(); //m_LastGamepadAxisState = std::array<GamepadAxisState, XUSER_MAX_COUNT>();
//m_LastGamepadButtonState = std::array<GamepadButtonState, XUSER_MAX_COUNT>(); //m_LastGamepadButtonState = std::array<GamepadButtonState, XUSER_MAX_COUNT>();
glfwSetCharCallback(m_GLFWWindow, &InputManager::GLFWCharCallback); glfwSetCharCallback(m_GLFWWindow, &InputManager::GLFWCharCallback);
glfwSetScrollCallback(m_GLFWWindow, &InputManager::GLFWScrollCallback);
EVENT_SUBSCRIBE_MEMBER(m_ELockMouse, &InputManager::OnLockMouse); EVENT_SUBSCRIBE_MEMBER(m_ELockMouse, &InputManager::OnLockMouse);
EVENT_SUBSCRIBE_MEMBER(m_EUnlockMouse, &InputManager::OnUnlockMouse); 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 // Mouse buttons
for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i) { for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i) {
m_CurrentMouseState[i] = glfwGetMouseButton(m_GLFWWindow, i); m_CurrentMouseState[i] = glfwGetMouseButton(m_GLFWWindow, i);
@@ -76,13 +87,13 @@ void InputManager::Update(double dt)
m_EventBroker->Publish(e); m_EventBroker->Publish(e);
} }
for (unsigned int& c : CharCallbackQueue) { // Mouse scroll
Events::KeyboardChar e; for (auto& pair : GLFWScrollCallbackQueue) {
e.Timestamp = glfwGetTime(); Events::MouseScroll e;
e.Char = c; std::tie(e.DeltaX, e.DeltaY) = pair;
m_EventBroker->Publish(e); m_EventBroker->Publish(e);
} }
CharCallbackQueue.clear(); GLFWScrollCallbackQueue.clear();
// // Lock mouse while holding LMB // // Lock mouse while holding LMB
// if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT]) // 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) 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) bool InputManager::OnLockMouse(const Events::LockMouse &event)