From 5e7537b903cb1d081121c5a8f2ffa03768d37e07 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Mon, 5 May 2014 07:16:45 +0200 Subject: [PATCH] Input events moved from InputSystem to new InputManager --- src/Engine.h | 9 +- src/Events/KeyDown.h | 6 +- src/Events/KeyUp.h | 6 +- src/Events/MouseMove.h | 17 +++ src/Events/MousePress.h | 16 +++ src/Events/MouseRelease.h | 16 +++ src/InputManager.cpp | 92 ++++++++++++++ src/InputManager.h | 33 +++++ src/Systems/InputSystem.cpp | 115 ++++-------------- src/Systems/InputSystem.h | 11 +- vs11/Returngeance/Returngeance.vcxproj | 5 + .../Returngeance/Returngeance.vcxproj.filters | 13 ++ 12 files changed, 236 insertions(+), 103 deletions(-) create mode 100644 src/Events/MouseMove.h create mode 100644 src/Events/MousePress.h create mode 100644 src/Events/MouseRelease.h create mode 100644 src/InputManager.cpp create mode 100644 src/InputManager.h diff --git a/src/Engine.h b/src/Engine.h index 91fed1c..25337c4 100755 --- a/src/Engine.h +++ b/src/Engine.h @@ -2,8 +2,9 @@ #include #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(); m_Renderer->Initialize(); - m_UIParent = std::make_shared(0, 0, m_Renderer->Width(), m_Renderer->Height()); + m_InputManager = std::make_shared(m_Renderer->GetWindow(), m_EventBroker); + + m_UIParent = std::make_shared(m_EventBroker); m_World = std::make_shared(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 m_EventBroker; std::shared_ptr m_Renderer; + std::shared_ptr m_InputManager; std::shared_ptr m_UIParent; // TODO: This should ultimately live in GameFrame std::shared_ptr m_World; diff --git a/src/Events/KeyDown.h b/src/Events/KeyDown.h index f2da381..0864862 100644 --- a/src/Events/KeyDown.h +++ b/src/Events/KeyDown.h @@ -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__ \ No newline at end of file diff --git a/src/Events/KeyUp.h b/src/Events/KeyUp.h index 0770767..9fcbf83 100644 --- a/src/Events/KeyUp.h +++ b/src/Events/KeyUp.h @@ -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__ \ No newline at end of file +#endif // Events_KeyUp_h__ \ No newline at end of file diff --git a/src/Events/MouseMove.h b/src/Events/MouseMove.h new file mode 100644 index 0000000..2d668a1 --- /dev/null +++ b/src/Events/MouseMove.h @@ -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__ \ No newline at end of file diff --git a/src/Events/MousePress.h b/src/Events/MousePress.h new file mode 100644 index 0000000..3116edb --- /dev/null +++ b/src/Events/MousePress.h @@ -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__ \ No newline at end of file diff --git a/src/Events/MouseRelease.h b/src/Events/MouseRelease.h new file mode 100644 index 0000000..7249dcd --- /dev/null +++ b/src/Events/MouseRelease.h @@ -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__ \ No newline at end of file diff --git a/src/InputManager.cpp b/src/InputManager.cpp new file mode 100644 index 0000000..18d8d92 --- /dev/null +++ b/src/InputManager.cpp @@ -0,0 +1,92 @@ +#include "PrecompiledHeader.h" +#include "InputManager.h" + +InputManager::InputManager(GLFWwindow* window, std::shared_ptr 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(e); + } + else + { + Events::KeyUp e; + e.KeyCode = i; + m_EventBroker->Publish(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(e); + } + else + { + Events::MouseRelease e; + e.Button = i; + m_EventBroker->Publish(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(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); + // } +} diff --git a/src/InputManager.h b/src/InputManager.h new file mode 100644 index 0000000..d7545e4 --- /dev/null +++ b/src/InputManager.h @@ -0,0 +1,33 @@ +#ifndef InputManager_h__ +#define InputManager_h__ + +#include + +#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); + + void Update(double dt); + +private: + GLFWwindow* m_GLFWWindow; + std::shared_ptr m_EventBroker; + + std::array m_CurrentKeyState; + std::array m_LastKeyState; + std::array m_CurrentMouseState; + std::array m_LastMouseState; + double m_CurrentMouseX, m_CurrentMouseY; + double m_LastMouseX, m_LastMouseY; + double m_CurrentMouseDeltaX, m_CurrentMouseDeltaY; +}; + +#endif // InputManager_h__ diff --git a/src/Systems/InputSystem.cpp b/src/Systems/InputSystem.cpp index 3f16de9..70cb927 100755 --- a/src/Systems/InputSystem.cpp +++ b/src/Systems/InputSystem.cpp @@ -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(e); - } - else - { - Events::KeyUp e; - e.KeyCode = i; - EventBroker->Publish(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(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 Systems::InputSystem::m_CurrentKeyState; -std::array Systems::InputSystem::m_LastKeyState; + return true; +} \ No newline at end of file diff --git a/src/Systems/InputSystem.h b/src/Systems/InputSystem.h index 6107c2b..b38bf96 100755 --- a/src/Systems/InputSystem.h +++ b/src/Systems/InputSystem.h @@ -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 m_Renderer; - static std::array m_CurrentKeyState; - static std::array m_LastKeyState; - std::array m_CurrentMouseState; - std::array m_LastMouseState; - float m_CurrentMouseDeltaX, m_CurrentMouseDeltaY; - float m_LastMouseX, m_LastMouseY; + // Events + EventRelay m_EKeyDown; + bool OnKeyDown(const Events::KeyDown &event); }; } diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 17327db..d894ab5 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -98,6 +98,7 @@ + @@ -140,11 +141,15 @@ + + + + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index febf3eb..532c16e 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -51,6 +51,7 @@ + @@ -240,6 +241,18 @@ Input\Events + + Input + + + Input\Events + + + Input\Events + + + Input\Events +