Mouse locking event

This commit is contained in:
2014-05-17 00:49:23 +02:00
parent bafa8d0a5d
commit 33b14b1859
5 changed files with 58 additions and 13 deletions
+14
View File
@@ -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__
+27 -8
View File
@@ -5,6 +5,9 @@ void InputManager::Initialize()
{
m_LastGamepadAxisState = std::array<GamepadAxisState, XUSER_MAX_COUNT>();
m_LastGamepadButtonState = std::array<GamepadButtonState, XUSER_MAX_COUNT>();
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;
}
+13 -5
View File
@@ -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> 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<EventBroker> m_EventBroker;
std::shared_ptr<::EventBroker> EventBroker;
EventRelay<Events::LockMouse> m_ELockMouse;
bool OnLockMouse(const Events::LockMouse &event);
EventRelay<Events::UnlockMouse> m_EUnlockMouse;
bool OnUnlockMouse(const Events::UnlockMouse &event);
std::array<int, GLFW_KEY_LAST+1> m_CurrentKeyState;
std::array<int, GLFW_KEY_LAST+1> m_LastKeyState;
std::array<int, GLFW_MOUSE_BUTTON_LAST+1> 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);
+1
View File
@@ -161,6 +161,7 @@
<ClInclude Include="..\..\src\Events\InputCommand.h" />
<ClInclude Include="..\..\src\Events\KeyDown.h" />
<ClInclude Include="..\..\src\Events\KeyUp.h" />
<ClInclude Include="..\..\src\Events\LockMouse.h" />
<ClInclude Include="..\..\src\Events\MouseMove.h" />
<ClInclude Include="..\..\src\Events\MousePress.h" />
<ClInclude Include="..\..\src\Events\MouseRelease.h" />
@@ -353,6 +353,9 @@
<Filter>Input\Events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Util\defferedUtil.h" />
<ClInclude Include="..\..\src\Events\LockMouse.h">
<Filter>Input\Events</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\Shaders\Fragment2.glsl">