diff --git a/src/Events/BindKey.h b/src/Events/BindKey.h new file mode 100644 index 0000000..a774942 --- /dev/null +++ b/src/Events/BindKey.h @@ -0,0 +1,17 @@ +#ifndef Events_BindKey_h__ +#define Events_BindKey_h__ + +#include "EventBroker.h" + +namespace Events +{ + +struct BindKey : Event +{ + int KeyCode; + std::string Command; +}; + +} + +#endif // Events_BindKey_h__ \ No newline at end of file diff --git a/src/Events/BindMouseButton.h b/src/Events/BindMouseButton.h new file mode 100644 index 0000000..1f46647 --- /dev/null +++ b/src/Events/BindMouseButton.h @@ -0,0 +1,17 @@ +#ifndef Events_BindMouseButton_h__ +#define Events_BindMouseButton_h__ + +#include "EventBroker.h" + +namespace Events +{ + +struct BindMouseButton : Event +{ + int Button; + std::string Command; +}; + +} + +#endif // Events_BindMouseButton_h__ \ No newline at end of file diff --git a/src/Events/InputCommand.h b/src/Events/InputCommand.h new file mode 100644 index 0000000..34a22c9 --- /dev/null +++ b/src/Events/InputCommand.h @@ -0,0 +1,17 @@ +#ifndef Events_InputCommand_h__ +#define Events_InputCommand_h__ + +#include "EventBroker.h" + +namespace Events +{ + +struct InputCommand : Event +{ + unsigned int PlayerID; + std::string Command; +}; + +} + +#endif // Events_InputCommand_h__ \ No newline at end of file diff --git a/src/Events/InputCommandValue.h b/src/Events/InputCommandValue.h new file mode 100644 index 0000000..87e3702 --- /dev/null +++ b/src/Events/InputCommandValue.h @@ -0,0 +1,18 @@ +#ifndef Events_InputCommandValue_h__ +#define Events_InputCommandValue_h__ + +#include "EventBroker.h" +#include "Events/InputCommand.h" + +namespace Events +{ + +template +struct InputCommandValue : public InputCommand +{ + T Value; +}; + +} + +#endif // Events_InputCommandValue_h__ \ No newline at end of file diff --git a/src/InputManager.cpp b/src/InputManager.cpp index 18d8d92..9f09b1c 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -1,12 +1,6 @@ #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; @@ -43,7 +37,7 @@ void InputManager::Update(double dt) if (m_CurrentMouseState[i] != m_LastMouseState[i]) { // Publish mouse button events - if (m_CurrentKeyState[i]) + if (m_CurrentMouseState[i]) { Events::MousePress e; e.Button = i; diff --git a/src/InputManager.h b/src/InputManager.h index d7545e4..5acaaa2 100644 --- a/src/InputManager.h +++ b/src/InputManager.h @@ -13,7 +13,16 @@ class InputManager { public: - InputManager(GLFWwindow* window, std::shared_ptr eventBroker); + InputManager(GLFWwindow* window, std::shared_ptr eventBroker) + : m_GLFWWindow(window) + , m_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) { } void Update(double dt); diff --git a/src/Systems/InputSystem.cpp b/src/Systems/InputSystem.cpp index 70cb927..f8b1d59 100755 --- a/src/Systems/InputSystem.cpp +++ b/src/Systems/InputSystem.cpp @@ -12,6 +12,30 @@ 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); + m_EKeyUp = decltype(m_EKeyUp)(std::bind(&Systems::InputSystem::OnKeyUp, this, std::placeholders::_1)); + EventBroker->Subscribe(m_EKeyUp); + m_EMousePress = decltype(m_EMousePress)(std::bind(&Systems::InputSystem::OnMousePress, this, std::placeholders::_1)); + EventBroker->Subscribe(m_EMousePress); + m_EMouseRelease = decltype(m_EMouseRelease)(std::bind(&Systems::InputSystem::OnMouseRelease, this, std::placeholders::_1)); + EventBroker->Subscribe(m_EMouseRelease); + m_EBindKey = decltype(m_EBindKey)(std::bind(&Systems::InputSystem::OnBindKey, this, std::placeholders::_1)); + EventBroker->Subscribe(m_EBindKey); + m_EBindMouseButton = decltype(m_EBindMouseButton)(std::bind(&Systems::InputSystem::OnBindMouseButton, this, std::placeholders::_1)); + EventBroker->Subscribe(m_EBindMouseButton); + + { + Events::BindKey e; + e.KeyCode = GLFW_KEY_W; + e.Command = "+forward"; + EventBroker->Publish(e); + } + + { + Events::BindMouseButton e; + e.Button = GLFW_MOUSE_BUTTON_1; + e.Command = "+attack"; + EventBroker->Publish(e); + } } void Systems::InputSystem::Update(double dt) @@ -37,5 +61,89 @@ void Systems::InputSystem::Update(double dt) bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event) { + auto bindingIt = m_KeyBindings.find(event.KeyCode); + if (bindingIt != m_KeyBindings.end()) + { + PublishCommand(0, bindingIt->second, false); + } + return true; -} \ No newline at end of file +} + +bool Systems::InputSystem::OnKeyUp(const Events::KeyUp &event) +{ + auto bindingIt = m_KeyBindings.find(event.KeyCode); + if (bindingIt != m_KeyBindings.end()) + { + PublishCommand(0, bindingIt->second, true); + } + + return true; +} + +bool Systems::InputSystem::OnMousePress(const Events::MousePress &event) +{ + auto bindingIt = m_MouseButtonBindings.find(event.Button); + if (bindingIt != m_MouseButtonBindings.end()) + { + PublishCommand(0, bindingIt->second, false); + } + + return true; +} + +bool Systems::InputSystem::OnMouseRelease(const Events::MouseRelease &event) +{ + auto bindingIt = m_MouseButtonBindings.find(event.Button); + if (bindingIt != m_MouseButtonBindings.end()) + { + PublishCommand(0, bindingIt->second, true); + } + + return true; +} + +bool Systems::InputSystem::OnBindKey(const Events::BindKey &event) +{ + if (event.Command.empty()) + { + m_KeyBindings.erase(event.KeyCode); + } + else + { + m_KeyBindings[event.KeyCode] = event.Command; + LOG_DEBUG("Input: Bound key %c to %s", (char)event.KeyCode, event.Command.c_str()); + } + + return true; +} + +bool Systems::InputSystem::OnBindMouseButton(const Events::BindMouseButton &event) +{ + if (event.Command.empty()) + { + m_MouseButtonBindings.erase(event.Button); + } + else + { + m_MouseButtonBindings[event.Button] = event.Command; + LOG_DEBUG("Input: Bound mouse button %i to %s", event.Button, event.Command.c_str()); + } + + return true; +} + +void Systems::InputSystem::PublishCommand(int playerID, std::string command, bool release /*= false*/) +{ + if (release && command.at(0) == '+') + { + command[0] = '-'; + } + + Events::InputCommand e; + e.PlayerID = playerID; + e.Command = command; + EventBroker->Publish(e); + + LOG_DEBUG("Input: Published command %s for player %i", e.Command.c_str(), playerID); +} diff --git a/src/Systems/InputSystem.h b/src/Systems/InputSystem.h index b38bf96..d0766e5 100755 --- a/src/Systems/InputSystem.h +++ b/src/Systems/InputSystem.h @@ -2,12 +2,19 @@ #define InputSystem_h__ #include +#include #include "System.h" #include "Renderer.h" #include "Components/Input.h" #include "Events/KeyUp.h" #include "Events/KeyDown.h" +#include "Events/MousePress.h" +#include "Events/MouseRelease.h" +#include "Events/BindKey.h" +#include "Events/BindMouseButton.h" +#include "Events/InputCommand.h" +#include "Events/InputCommandValue.h" namespace Systems { @@ -27,9 +34,26 @@ public: private: std::shared_ptr m_Renderer; - // Events + // Input binding tables + std::unordered_map m_KeyBindings; // GLFW_KEY... -> command string + std::unordered_map m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string + + // Input events EventRelay m_EKeyDown; bool OnKeyDown(const Events::KeyDown &event); + EventRelay m_EKeyUp; + bool OnKeyUp(const Events::KeyUp &event); + EventRelay m_EMousePress; + bool OnMousePress(const Events::MousePress &event); + EventRelay m_EMouseRelease; + bool OnMouseRelease(const Events::MouseRelease &event); + // Input binding events + EventRelay m_EBindKey; + bool OnBindKey(const Events::BindKey &event); + EventRelay m_EBindMouseButton; + bool OnBindMouseButton(const Events::BindMouseButton &event); + + void PublishCommand(int playerID, std::string command, bool release = false); }; } diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index d894ab5..2b7c917 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -139,6 +139,10 @@ + + + + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 532c16e..4da5b38 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -51,7 +51,9 @@ - + + Input + @@ -253,6 +255,18 @@ Input\Events + + Input\Events + + + Input\Events + + + Input\Events + + + Input\Events +