Input events moved from InputSystem to new InputManager

This commit is contained in:
2014-05-05 07:16:45 +02:00
parent 330a212830
commit 5e7537b903
12 changed files with 236 additions and 103 deletions
+7 -2
View File
@@ -2,8 +2,9 @@
#include <sstream>
#include "EventBroker.h"
#include "GUI/Frame.h"
#include "Renderer.h"
#include "InputManager.h"
#include "GUI/Frame.h"
#include "GameWorld.h"
class Engine
@@ -16,7 +17,9 @@ public:
m_Renderer = std::make_shared<Renderer>();
m_Renderer->Initialize();
m_UIParent = std::make_shared<GUI::Frame>(0, 0, m_Renderer->Width(), m_Renderer->Height());
m_InputManager = std::make_shared<InputManager>(m_Renderer->GetWindow(), m_EventBroker);
m_UIParent = std::make_shared<GUI::Frame>(m_EventBroker);
m_World = std::make_shared<GameWorld>(m_EventBroker, m_Renderer);
m_World->Initialize();
@@ -32,6 +35,7 @@ public:
double dt = currentTime - m_LastTime;
m_LastTime = currentTime;
m_InputManager->Update(dt);
m_World->Update(dt);
m_Renderer->Draw(dt);
@@ -41,6 +45,7 @@ public:
private:
std::shared_ptr<EventBroker> m_EventBroker;
std::shared_ptr<Renderer> m_Renderer;
std::shared_ptr<InputManager> m_InputManager;
std::shared_ptr<GUI::Frame> m_UIParent;
// TODO: This should ultimately live in GameFrame
std::shared_ptr<GameWorld> m_World;
+3 -3
View File
@@ -1,5 +1,5 @@
#ifndef Event_KeyDown_h__
#define Event_KeyDown_h__
#ifndef Events_KeyDown_h__
#define Events_KeyDown_h__
#include "EventBroker.h"
@@ -13,4 +13,4 @@ struct KeyDown : Event
}
#endif // Event_KeyDown_h__
#endif // Events_KeyDown_h__
+3 -3
View File
@@ -1,5 +1,5 @@
#ifndef Event_KeyUp_h__
#define Event_KeyUp_h__
#ifndef Events_KeyUp_h__
#define Events_KeyUp_h__
#include "EventBroker.h"
@@ -13,4 +13,4 @@ struct KeyUp : Event
}
#endif // Event_KeyUp_h__
#endif // Events_KeyUp_h__
+17
View File
@@ -0,0 +1,17 @@
#ifndef Events_MouseMove_h__
#define Events_MouseMove_h__
#include "EventBroker.h"
namespace Events
{
struct MouseMove : Event
{
double X, Y;
double DeltaX, DeltaY;
};
}
#endif // Events_MouseMove_h__
+16
View File
@@ -0,0 +1,16 @@
#ifndef Events_MousePress_h__
#define Events_MousePress_h__
#include "EventBroker.h"
namespace Events
{
struct MousePress : Event
{
int Button;
};
}
#endif // Events_MousePress_h__
+16
View File
@@ -0,0 +1,16 @@
#ifndef Events_MouseRelease_h__
#define Events_MouseRelease_h__
#include "EventBroker.h"
namespace Events
{
struct MouseRelease : Event
{
int Button;
};
}
#endif // Events_MouseRelease_h__
+92
View File
@@ -0,0 +1,92 @@
#include "PrecompiledHeader.h"
#include "InputManager.h"
InputManager::InputManager(GLFWwindow* window, std::shared_ptr<EventBroker> eventBroker) : m_GLFWWindow(window)
, m_EventBroker(eventBroker)
{
}
void InputManager::Update(double dt)
{
m_LastKeyState = m_CurrentKeyState;
m_LastMouseState = m_CurrentMouseState;
m_LastMouseX = m_CurrentMouseX;
m_LastMouseY = m_CurrentMouseY;
// Keyboard input
for (int i = 0; i <= GLFW_KEY_LAST; ++i)
{
m_CurrentKeyState[i] = glfwGetKey(m_GLFWWindow, i);
if (m_CurrentKeyState[i] != m_LastKeyState[i])
{
// Publish key events
if (m_CurrentKeyState[i])
{
Events::KeyDown e;
e.KeyCode = i;
m_EventBroker->Publish<Events::KeyDown>(e);
}
else
{
Events::KeyUp e;
e.KeyCode = i;
m_EventBroker->Publish<Events::KeyUp>(e);
}
}
}
// Mouse buttons
for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i)
{
m_CurrentMouseState[i] = glfwGetMouseButton(m_GLFWWindow, i);
if (m_CurrentMouseState[i] != m_LastMouseState[i])
{
// Publish mouse button events
if (m_CurrentKeyState[i])
{
Events::MousePress e;
e.Button = i;
m_EventBroker->Publish<Events::MousePress>(e);
}
else
{
Events::MouseRelease e;
e.Button = i;
m_EventBroker->Publish<Events::MouseRelease>(e);
}
}
}
// Cursor position
glfwGetCursorPos(m_GLFWWindow, &m_CurrentMouseX, &m_CurrentMouseY);
m_CurrentMouseDeltaX = m_CurrentMouseX - m_LastMouseX;
m_CurrentMouseDeltaY = m_CurrentMouseY - m_LastMouseY;
if (m_CurrentMouseDeltaX != 0 || m_CurrentMouseDeltaY != 0)
{
// Publish mouse move events
Events::MouseMove e;
e.X = m_CurrentMouseX;
e.Y = m_CurrentMouseY;
e.DeltaX = m_CurrentMouseDeltaX;
e.DeltaY = m_CurrentMouseDeltaY;
m_EventBroker->Publish<Events::MouseMove>(e);
}
// // Lock mouse while holding LMB
// if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT])
// {
// m_LastMouseX = m_Renderer->Width() / 2.f; // xpos;
// m_LastMouseY = m_Renderer->Height() / 2.f; // ypos;
// glfwSetCursorPos(m_GLFWWindow, m_LastMouseX, m_LastMouseY);
// }
// // Hide/show cursor with LMB
// if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT] && !m_LastMouseState[GLFW_MOUSE_BUTTON_LEFT])
// {
// glfwSetInputMode(m_GLFWWindow, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
// }
// if (!m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT] && m_LastMouseState[GLFW_MOUSE_BUTTON_LEFT])
// {
// glfwSetInputMode(m_GLFWWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
// }
}
+33
View File
@@ -0,0 +1,33 @@
#ifndef InputManager_h__
#define InputManager_h__
#include <array>
#include "EventBroker.h"
#include "Events/KeyDown.h"
#include "Events/KeyUp.h"
#include "Events/MousePress.h"
#include "Events/MouseRelease.h"
#include "Events/MouseMove.h"
class InputManager
{
public:
InputManager(GLFWwindow* window, std::shared_ptr<EventBroker> eventBroker);
void Update(double dt);
private:
GLFWwindow* m_GLFWWindow;
std::shared_ptr<EventBroker> m_EventBroker;
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;
std::array<int, GLFW_MOUSE_BUTTON_LAST+1> m_LastMouseState;
double m_CurrentMouseX, m_CurrentMouseY;
double m_LastMouseX, m_LastMouseY;
double m_CurrentMouseDeltaX, m_CurrentMouseDeltaY;
};
#endif // InputManager_h__
+27 -88
View File
@@ -7,96 +7,35 @@ void Systems::InputSystem::RegisterComponents(ComponentFactory* cf)
cf->Register("Input", []() { return new Components::Input(); });
}
void Systems::InputSystem::Initialize()
{
// Subscribe to events
m_EKeyDown = decltype(m_EKeyDown)(std::bind(&Systems::InputSystem::OnKeyDown, this, std::placeholders::_1));
EventBroker->Subscribe(m_EKeyDown);
}
void Systems::InputSystem::Update(double dt)
{
m_LastKeyState = m_CurrentKeyState;
m_LastMouseState = m_CurrentMouseState;
// Keyboard input
for (int i = 0; i <= GLFW_KEY_LAST; ++i)
{
m_CurrentKeyState[i] = glfwGetKey(m_Renderer->GetWindow(), i);
if (m_CurrentKeyState[i] != m_LastKeyState[i])
{
// Publish key events
if (m_CurrentKeyState[i])
{
Events::KeyDown e;
e.KeyCode = i;
EventBroker->Publish<Events::KeyDown>(e);
}
else
{
Events::KeyUp e;
e.KeyCode = i;
EventBroker->Publish<Events::KeyUp>(e);
}
}
}
// Mouse buttons
for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i)
{
m_CurrentMouseState[i] = glfwGetMouseButton(m_Renderer->GetWindow(), i);
}
// Cursor position
double xpos, ypos;
glfwGetCursorPos(m_Renderer->GetWindow(), &xpos, &ypos);
m_CurrentMouseDeltaX = xpos - m_LastMouseX;
m_CurrentMouseDeltaY = ypos - m_LastMouseY;
m_LastMouseX = xpos;
m_LastMouseY = ypos;
// Lock mouse while holding LMB
if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT])
{
m_LastMouseX = m_Renderer->Width() / 2.f; // xpos;
m_LastMouseY = m_Renderer->Height() / 2.f; // ypos;
glfwSetCursorPos(m_Renderer->GetWindow(), m_LastMouseX, m_LastMouseY);
}
// Hide/show cursor with LMB
if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT] && !m_LastMouseState[GLFW_MOUSE_BUTTON_LEFT])
{
glfwSetInputMode(m_Renderer->GetWindow(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
if (!m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT] && m_LastMouseState[GLFW_MOUSE_BUTTON_LEFT])
{
glfwSetInputMode(m_Renderer->GetWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
#ifdef DEBUG
// Wireframe
if (m_CurrentKeyState[GLFW_KEY_F1] && !m_LastKeyState[GLFW_KEY_F1])
{
m_Renderer->DrawWireframe(!m_Renderer->DrawWireframe());
}
// Normals
if (m_CurrentKeyState[GLFW_KEY_F2] && !m_LastKeyState[GLFW_KEY_F2])
{
m_Renderer->DrawNormals(!m_Renderer->DrawNormals());
}
// Bounds
if (m_CurrentKeyState[GLFW_KEY_F3] && !m_LastKeyState[GLFW_KEY_F3])
{
m_Renderer->DrawBounds(!m_Renderer->DrawBounds());
}
#endif
// #ifdef DEBUG
// // Wireframe
// if (m_CurrentKeyState[GLFW_KEY_F1] && !m_LastKeyState[GLFW_KEY_F1])
// {
// m_Renderer->DrawWireframe(!m_Renderer->DrawWireframe());
// }
// // Normals
// if (m_CurrentKeyState[GLFW_KEY_F2] && !m_LastKeyState[GLFW_KEY_F2])
// {
// m_Renderer->DrawNormals(!m_Renderer->DrawNormals());
// }
// // Bounds
// if (m_CurrentKeyState[GLFW_KEY_F3] && !m_LastKeyState[GLFW_KEY_F3])
// {
// m_Renderer->DrawBounds(!m_Renderer->DrawBounds());
// }
// #endif
}
void Systems::InputSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event)
{
auto input = m_World->GetComponent<Components::Input>(entity, "Input");
if (input == nullptr)
return;
input->KeyState = m_CurrentKeyState;
input->LastKeyState = m_LastKeyState;
input->MouseState = m_CurrentMouseState;
input->LastMouseState = m_LastMouseState;
input->dX = m_CurrentMouseDeltaX;
input->dY = m_CurrentMouseDeltaY;
}
std::array<int, GLFW_KEY_LAST+1> Systems::InputSystem::m_CurrentKeyState;
std::array<int, GLFW_KEY_LAST+1> Systems::InputSystem::m_LastKeyState;
return true;
}
+4 -7
View File
@@ -20,19 +20,16 @@ public:
, m_Renderer(renderer) { }
void RegisterComponents(ComponentFactory* cf) override;
void Initialize() override;
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
std::shared_ptr<Renderer> m_Renderer;
static std::array<int, GLFW_KEY_LAST+1> m_CurrentKeyState;
static std::array<int, GLFW_KEY_LAST+1> m_LastKeyState;
std::array<int, GLFW_MOUSE_BUTTON_LAST+1> m_CurrentMouseState;
std::array<int, GLFW_MOUSE_BUTTON_LAST+1> m_LastMouseState;
float m_CurrentMouseDeltaX, m_CurrentMouseDeltaY;
float m_LastMouseX, m_LastMouseY;
// Events
EventRelay<Events::KeyDown> m_EKeyDown;
bool OnKeyDown(const Events::KeyDown &event);
};
}