From bffc97f31029b501fd49a7189228a7526d56bf9a Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 11 Dec 2015 00:37:48 +0100 Subject: [PATCH 1/8] Groundwork for input command proxy --- include/Engine/Input/EBindOrigin.h | 22 ++ include/Engine/Input/EInputCommand.h | 2 +- include/Engine/Input/InputProxy.h | 146 +++++++++++++ include/Engine/Input/InputSystem.h | 78 ------- include/Engine/Input/KeyboardInputHandler.h | 67 ++++++ include/Game/Game.h | 3 + src/Engine/CMakeLists.txt | 2 +- src/Engine/Input/InputProxy.cpp | 221 ++++++++++++++++++++ src/Engine/Input/InputSystem.cpp | 221 -------------------- src/Game/Game.cpp | 8 + 10 files changed, 469 insertions(+), 301 deletions(-) create mode 100644 include/Engine/Input/EBindOrigin.h create mode 100644 include/Engine/Input/InputProxy.h delete mode 100644 include/Engine/Input/InputSystem.h create mode 100644 include/Engine/Input/KeyboardInputHandler.h create mode 100644 src/Engine/Input/InputProxy.cpp delete mode 100644 src/Engine/Input/InputSystem.cpp diff --git a/include/Engine/Input/EBindOrigin.h b/include/Engine/Input/EBindOrigin.h new file mode 100644 index 00000000..e39a6023 --- /dev/null +++ b/include/Engine/Input/EBindOrigin.h @@ -0,0 +1,22 @@ +#ifndef Events_BindOrigin_h__ +#define Events_BindOrigin_h__ + +#include "Core/EventBroker.h" + +namespace Events +{ + +/** Called to bind an input origin to an input command. */ +struct BindOrigin : Event +{ + /** The input origin to bind. */ + std::string Origin; + /** The command to send. */ + std::string Command; + /** The value to send for positive stimulation. */ + float Value = 1.f; +}; + +} + +#endif diff --git a/include/Engine/Input/EInputCommand.h b/include/Engine/Input/EInputCommand.h index 9c190a1e..9ec897d3 100644 --- a/include/Engine/Input/EInputCommand.h +++ b/include/Engine/Input/EInputCommand.h @@ -13,7 +13,7 @@ struct InputCommand : Event /** The command that was sent. */ std::string Command; /** The value of the command. */ - float Value; + float Value = 0; }; } diff --git a/include/Engine/Input/InputProxy.h b/include/Engine/Input/InputProxy.h new file mode 100644 index 00000000..1b0e213e --- /dev/null +++ b/include/Engine/Input/InputProxy.h @@ -0,0 +1,146 @@ +#ifndef InputSystem_h__ +#define InputSystem_h__ + +#include +#include + +#include + +#include "Core/EKeyUp.h" +#include "Core/EKeyDown.h" +#include "Core/EMousePress.h" +#include "Core/EMouseRelease.h" +#include "Core/EGamepadAxis.h" +#include "Core/EGamepadButton.h" +#include "Core/Util/EnumClassHash.h" +#include "EBindKey.h" +#include "EBindMouseButton.h" +#include "EBindGamepadAxis.h" +#include "EBindGamepadButton.h" +#include "EInputCommand.h" +#include "EBindOrigin.h" + +class InputProxy; + +class InputHandler +{ +public: + InputHandler(EventBroker* eventBroker, InputProxy* inputProxy) + : m_EventBroker(eventBroker) + , m_InputProxy(inputProxy) + { } + + virtual void Update(double dt) { } + virtual bool BindOrigin(std::string origin, std::string command, float value) = 0; + +protected: + EventBroker* m_EventBroker; + InputProxy* m_InputProxy; +}; + +class InputProxy +{ +public: + InputProxy(EventBroker* eventBroker) + : m_EventBroker(eventBroker) + { + EVENT_SUBSCRIBE_MEMBER(m_EBindOrigin, &InputProxy::OnBindOrigin); + } + + void Update(double dt) + { + m_EventBroker->Process(); + m_EventBroker->Process(); + for (auto& handler : m_Handlers) { + handler->Update(dt); + } + } + + void Process() + { + // Accumulate the input values of all unique commands published by input handlers + for (auto& pair : m_CommandQueue) { + Events::InputCommand e; + e.PlayerID = pair.first.first; + e.Command = pair.first.second; + e.Value = 0; + for (auto& value : pair.second) { + e.Value += value; + } + e.Value = std::max(-1.f, std::min(e.Value, 1.f)); + m_EventBroker->Publish(e); + LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); + } + m_CommandQueue.clear(); + } + + template + void AddHandler() + { + m_Handlers.push_back(new T(m_EventBroker, this)); + } + + void Publish(const Events::InputCommand& e) + { + auto key = std::make_pair(e.PlayerID, e.Command); + m_CommandQueue[key].push_back(e.Value); + } + +protected: + EventBroker* m_EventBroker; + std::vector m_Handlers; + // Represents every unique command (has of PlayerID & Command) and all values reported for that command + std::map, std::vector> m_CommandQueue; + + EventRelay m_EBindOrigin; + bool OnBindOrigin(const Events::BindOrigin& e) + { + bool originBound = false; + for (auto& handler : m_Handlers) { + bool result = handler->BindOrigin(e.Origin, e.Command, e.Value); + if (result) { + if (originBound) { + LOG_WARNING("Multiple handlers responded to binding input origin \"%s\"!", e.Origin.c_str()); + } + originBound = true; + } + } + + if (!originBound) { + LOG_ERROR("No input handler responded to binding input origin \"%s\"!", e.Origin.c_str()); + } + + return originBound; + } + //std::unordered_map> m_CommandMouseButtonValues; // command string -> mouse button value for command + //std::unordered_map> m_CommandGamepadAxisValues; // command string -> gamepad axis value for command + //std::unordered_map> m_CommandGamepadButtonValues; // command string -> gamepad button value for command + //// Input binding tables + //std::unordered_multimap> m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string + //std::unordered_multimap, EnumClassHash> m_GamepadAxisBindings; // Gamepad::Axis -> command string & value + //std::unordered_multimap, EnumClassHash> m_GamepadButtonBindings; // Gamepad::Button -> command string + + //// Input events + //EventRelay m_EMousePress; + //bool OnMousePress(const Events::MousePress &event); + //EventRelay m_EMouseRelease; + //bool OnMouseRelease(const Events::MouseRelease &event); + //EventRelay m_EGamepadAxis; + //bool OnGamepadAxis(const Events::GamepadAxis &event); + //EventRelay m_EGamepadButtonDown; + //bool OnGamepadButtonDown(const Events::GamepadButtonDown &event); + //EventRelay m_EGamepadButtonUp; + //bool OnGamepadButtonUp(const Events::GamepadButtonUp &event); + //// Input binding events + //EventRelay m_EBindMouseButton; + //bool OnBindMouseButton(const Events::BindMouseButton &event); + //EventRelay m_EBindGamepadAxis; + //bool OnBindGamepadAxis(const Events::BindGamepadAxis &event); + //EventRelay m_EBindGamepadButton; + //bool OnBindGamepadButton(const Events::BindGamepadButton &event); + + //float GetCommandTotalValue(std::string command); + //void PublishCommand(int playerID, std::string command, float value); +}; + +#endif diff --git a/include/Engine/Input/InputSystem.h b/include/Engine/Input/InputSystem.h deleted file mode 100644 index 2023880b..00000000 --- a/include/Engine/Input/InputSystem.h +++ /dev/null @@ -1,78 +0,0 @@ -#ifndef InputSystem_h__ -#define InputSystem_h__ - -#include -#include - -#include "Core/System.h" -#include "Core/EKeyUp.h" -#include "Core/EKeyDown.h" -#include "Core/EMousePress.h" -#include "Core/EMouseRelease.h" -#include "Core/EGamepadAxis.h" -#include "Core/EGamepadButton.h" -#include "Core/Util/EnumClassHash.h" -#include "EBindKey.h" -#include "EBindMouseButton.h" -#include "EBindGamepadAxis.h" -#include "EBindGamepadButton.h" -#include "EInputCommand.h" - -namespace Systems -{ - -class InputSystem : public System -{ -public: - InputSystem(World* world, std::shared_ptr eventBroker) - : System(world, eventBroker) - { } - - void RegisterComponents(ComponentFactory* cf) override; - void Initialize() override; - - void Update(double dt) override; - -private: - std::unordered_map> m_CommandKeyboardValues; // command string -> keyboard key value for command - std::unordered_map> m_CommandMouseButtonValues; // command string -> mouse button value for command - std::unordered_map> m_CommandGamepadAxisValues; // command string -> gamepad axis value for command - std::unordered_map> m_CommandGamepadButtonValues; // command string -> gamepad button value for command - // Input binding tables - std::unordered_multimap> m_KeyBindings; // GLFW_KEY... -> command string & value - std::unordered_multimap> m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string - std::unordered_multimap, EnumClassHash> m_GamepadAxisBindings; // Gamepad::Axis -> command string & value - std::unordered_multimap, EnumClassHash> m_GamepadButtonBindings; // Gamepad::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); - EventRelay m_EGamepadAxis; - bool OnGamepadAxis(const Events::GamepadAxis &event); - EventRelay m_EGamepadButtonDown; - bool OnGamepadButtonDown(const Events::GamepadButtonDown &event); - EventRelay m_EGamepadButtonUp; - bool OnGamepadButtonUp(const Events::GamepadButtonUp &event); - // Input binding events - EventRelay m_EBindKey; - bool OnBindKey(const Events::BindKey &event); - EventRelay m_EBindMouseButton; - bool OnBindMouseButton(const Events::BindMouseButton &event); - EventRelay m_EBindGamepadAxis; - bool OnBindGamepadAxis(const Events::BindGamepadAxis &event); - EventRelay m_EBindGamepadButton; - bool OnBindGamepadButton(const Events::BindGamepadButton &event); - - float GetCommandTotalValue(std::string command); - void PublishCommand(int playerID, std::string command, float value); -}; - -} - -#endif diff --git a/include/Engine/Input/KeyboardInputHandler.h b/include/Engine/Input/KeyboardInputHandler.h new file mode 100644 index 00000000..1ffaa067 --- /dev/null +++ b/include/Engine/Input/KeyboardInputHandler.h @@ -0,0 +1,67 @@ +#include +#include "InputProxy.h" +#include "Core/EKeyDown.h" +#include "Core/EKeyUp.h" + +class KeyboardInputHandler : public InputHandler +{ +public: + KeyboardInputHandler(EventBroker* eventBroker, InputProxy* inputProxy) + : InputHandler(eventBroker, inputProxy) + { + EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &KeyboardInputHandler::OnKeyDown); + EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &KeyboardInputHandler::OnKeyUp); + + m_OriginKeyCodes["R"] = GLFW_KEY_R; + } + + bool BindOrigin(std::string origin, std::string command, float value) override + { + auto originIt = m_OriginKeyCodes.find(origin); + if (originIt == m_OriginKeyCodes.end()) { + return false; + } + + int keyCode = originIt->second; + m_KeyBindings[keyCode] = std::make_tuple(command, value); + return true; + } + +private: + std::unordered_map m_OriginKeyCodes; + std::unordered_map> m_KeyBindings; // GLFW_KEY... -> command string & value + + EventRelay m_EKeyDown; + bool OnKeyDown(const Events::KeyDown& e) + { + auto it = m_KeyBindings.find(e.KeyCode); + if (it == m_KeyBindings.end()) { + return false; + } + + Events::InputCommand ic; + ic.PlayerID = 0; + std::tie(ic.Command, ic.Value) = it->second; + m_InputProxy->Publish(ic); + + return true; + } + + EventRelay m_EKeyUp; + bool OnKeyUp(const Events::KeyUp& e) + { + auto it = m_KeyBindings.find(e.KeyCode); + if (it == m_KeyBindings.end()) { + return false; + } + + Events::InputCommand ic; + ic.PlayerID = 0; + std::tie(ic.Command, std::ignore) = it->second; + ic.Value = 0; + m_InputProxy->Publish(ic); + + return true; + } + +}; \ No newline at end of file diff --git a/include/Game/Game.h b/include/Game/Game.h index bc66d62f..04d14be0 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -9,6 +9,8 @@ #include "GUI/Frame.h" #include "Core/World.h" #include "Rendering/RenderQueueFactory.h" +#include "Input/InputProxy.h" +#include "Input/KeyboardInputHandler.h" #include "Core/EKeyDown.h" #include "Core/EntityXMLFile.h" #include "Core/SystemPipeline.h" @@ -30,6 +32,7 @@ private: EventBroker* m_EventBroker; IRenderer* m_Renderer; InputManager* m_InputManager; + InputProxy* m_InputProxy; GUI::Frame* m_FrameStack; World* m_World; SystemPipeline* m_SystemPipeline; diff --git a/src/Engine/CMakeLists.txt b/src/Engine/CMakeLists.txt index de24b9cf..97ed3e68 100644 --- a/src/Engine/CMakeLists.txt +++ b/src/Engine/CMakeLists.txt @@ -73,7 +73,7 @@ source_group(GUI FILES ${SOURCE_FILES_GUI}) set(SOURCE_FILES ${SOURCE_FILES_Core} ${SOURCE_FILES_Core_Util} - #${SOURCE_FILES_Input} + ${SOURCE_FILES_Input} ${SOURCE_FILES_Network} ${SOURCE_FILES_GUI} ${SOURCE_FILES_Rendering} diff --git a/src/Engine/Input/InputProxy.cpp b/src/Engine/Input/InputProxy.cpp new file mode 100644 index 00000000..3a6626df --- /dev/null +++ b/src/Engine/Input/InputProxy.cpp @@ -0,0 +1,221 @@ +#include "Input/InputProxy.h" +#include "Core/World.h" + +//InputProxy::InputProxy(EventBroker* eventBroker) +// : m_EventBroker(eventBroker) +//{ +// // Subscribe to events +// EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &InputProxy::OnMousePress); +// EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &InputProxy::OnMouseRelease); +// EVENT_SUBSCRIBE_MEMBER(m_EGamepadAxis, &InputProxy::OnGamepadAxis); +// EVENT_SUBSCRIBE_MEMBER(m_EGamepadButtonDown, &InputProxy::OnGamepadButtonDown); +// EVENT_SUBSCRIBE_MEMBER(m_EGamepadButtonUp, &InputProxy::OnGamepadButtonUp); +// EVENT_SUBSCRIBE_MEMBER(m_EBindKey, &InputProxy::OnBindKey); +// EVENT_SUBSCRIBE_MEMBER(m_EBindMouseButton, &InputProxy::OnBindMouseButton); +// EVENT_SUBSCRIBE_MEMBER(m_EBindGamepadAxis, &InputProxy::OnBindGamepadAxis); +// EVENT_SUBSCRIBE_MEMBER(m_EBindGamepadButton, &InputProxy::OnBindGamepadButton); +// +// SteamController()->Init(); +// +//} +// +//void InputProxy::Update(double dt) +//{ +// std::array controllers; +// int numControllers = SteamController()->GetConnectedControllers(controllers.data()); +// +// ControllerDigitalActionHandle_t debug_reload_handle = SteamController()->GetDigitalActionHandle("debug_reload"); +// SteamController()-> +//} +// +//bool InputProxy::OnKeyDown(const Events::KeyDown &event) +//{ +// auto range = m_KeyBindings.equal_range(event.KeyCode); +// for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { +// std::string command; +// float value; +// std::tie(command, value) = bindingIt->second; +// m_CommandKeyboardValues[command][event.KeyCode] = value; +// PublishCommand(1, command, GetCommandTotalValue(command)); +// } +// +// return true; +//} +// +//bool InputProxy::OnKeyUp(const Events::KeyUp &event) +//{ +// auto range = m_KeyBindings.equal_range(event.KeyCode); +// for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { +// std::string command; +// float value; +// std::tie(command, value) = bindingIt->second; +// m_CommandKeyboardValues[command][event.KeyCode] = 0; +// PublishCommand(1, command, GetCommandTotalValue(command));; +// } +// +// return true; +//} +// +//bool InputProxy::OnMousePress(const Events::MousePress &event) +//{ +// auto range = m_MouseButtonBindings.equal_range(event.Button); +// for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { +// std::string command; +// float value; +// std::tie(command, value) = bindingIt->second; +// m_CommandMouseButtonValues[command][event.Button] = value; +// PublishCommand(1, command, GetCommandTotalValue(command)); +// } +// +// return true; +//} +// +//bool InputProxy::OnMouseRelease(const Events::MouseRelease &event) +//{ +// auto range = m_MouseButtonBindings.equal_range(event.Button); +// for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { +// std::string command; +// float value; +// std::tie(command, value) = bindingIt->second; +// m_CommandMouseButtonValues[command][event.Button] = 0; +// PublishCommand(1, command, GetCommandTotalValue(command)); +// } +// +// return true; +//} +// +//bool InputProxy::OnGamepadAxis(const Events::GamepadAxis &event) +//{ +// auto range = m_GamepadAxisBindings.equal_range(event.Axis); +// for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { +// std::string command; +// float value; +// std::tie(command, value) = bindingIt->second; +// m_CommandGamepadAxisValues[command][event.Axis] = event.Value * value; +// PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); +// } +// +// return true; +//} +// +//bool InputProxy::OnGamepadButtonDown(const Events::GamepadButtonDown &event) +//{ +// auto range = m_GamepadButtonBindings.equal_range(event.Button); +// for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { +// std::string command; +// float value; +// std::tie(command, value) = bindingIt->second; +// m_CommandGamepadButtonValues[command][event.Button] = value; +// PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); +// } +// +// return true; +//} +// +//bool InputProxy::OnGamepadButtonUp(const Events::GamepadButtonUp &event) +//{ +// auto range = m_GamepadButtonBindings.equal_range(event.Button); +// for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { +// std::string command; +// float value; +// std::tie(command, value) = bindingIt->second; +// m_CommandGamepadButtonValues[command][event.Button] = 0; +// PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); +// } +// +// return true; +//} +// +//bool InputProxy::OnBindKey(const Events::BindKey &event) +//{ +// if (event.Command.empty()) { +// return false; +// } +// +// m_KeyBindings.insert(std::make_pair(event.KeyCode, std::make_tuple(event.Command, event.Value))); +// LOG_DEBUG("Input: Bound key %i to %s", event.KeyCode, event.Command.c_str()); +// +// return true; +//} +// +//bool InputProxy::OnBindMouseButton(const Events::BindMouseButton &event) +//{ +// if (event.Command.empty()) { +// return false; +// } +// +// m_MouseButtonBindings.insert(std::make_pair(event.Button, std::make_tuple(event.Command, event.Value))); +// LOG_DEBUG("Input: Bound mouse button %i to %s", event.Button, event.Command.c_str()); +// +// return true; +//} +// +//bool InputProxy::OnBindGamepadAxis(const Events::BindGamepadAxis &event) +//{ +// if (event.Command.empty()) { +// return false; +// } +// +// m_GamepadAxisBindings.insert(std::make_pair(event.Axis, std::make_tuple(event.Command, event.Value))); +// LOG_DEBUG("Input: Bound gamepad axis %i to %s", event.Axis, event.Command.c_str()); +// +// return true; +//} +// +//bool InputProxy::OnBindGamepadButton(const Events::BindGamepadButton &event) +//{ +// if (event.Command.empty()) { +// return false; +// } +// +// m_GamepadButtonBindings.insert(std::make_pair(event.Button, std::make_tuple(event.Command, event.Value))); +// LOG_DEBUG("Input: Bound gamepad axis %i to %s", event.Button, event.Command.c_str()); +// +// return true; +//} +// +//float InputProxy::GetCommandTotalValue(std::string command) +//{ +// float value = 0.f; +// +// auto keyboardIt = m_CommandKeyboardValues.find(command); +// if (keyboardIt != m_CommandKeyboardValues.end()) { +// for (auto &key : keyboardIt->second) { +// value += key.second; +// } +// } +// +// auto mouseButtonIt = m_CommandMouseButtonValues.find(command); +// if (mouseButtonIt != m_CommandMouseButtonValues.end()) { +// for (auto &button : mouseButtonIt->second) { +// value += button.second; +// } +// } +// +// auto gamepadAxisIt = m_CommandGamepadAxisValues.find(command); +// if (gamepadAxisIt != m_CommandGamepadAxisValues.end()) { +// for (auto &axis : gamepadAxisIt->second) { +// value += axis.second; +// } +// } +// +// auto gamepadButtonIt = m_CommandGamepadButtonValues.find(command); +// if (gamepadButtonIt != m_CommandGamepadButtonValues.end()) { +// for (auto &button : gamepadButtonIt->second) { +// value += button.second; +// } +// } +// +// return std::max(-1.f, std::min(value, 1.f)); +//} +// +//void InputProxy::PublishCommand(int playerID, std::string command, float value) +//{ +// Events::InputCommand e; +// e.PlayerID = playerID; +// e.Command = command; +// e.Value = value; +// m_EventBroker->Publish(e); +// +// LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, playerID); +//} diff --git a/src/Engine/Input/InputSystem.cpp b/src/Engine/Input/InputSystem.cpp deleted file mode 100644 index d11cd73d..00000000 --- a/src/Engine/Input/InputSystem.cpp +++ /dev/null @@ -1,221 +0,0 @@ -#include "PrecompiledHeader.h" -#include "Input/InputSystem.h" -#include "Core/World.h" - -void Systems::InputSystem::RegisterComponents(ComponentFactory* cf) -{ - -} - -void Systems::InputSystem::Initialize() -{ - // Subscribe to events - EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Systems::InputSystem::OnKeyDown); - EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Systems::InputSystem::OnKeyUp); - EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &Systems::InputSystem::OnMousePress); - EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &Systems::InputSystem::OnMouseRelease); - EVENT_SUBSCRIBE_MEMBER(m_EGamepadAxis, &Systems::InputSystem::OnGamepadAxis); - EVENT_SUBSCRIBE_MEMBER(m_EGamepadButtonDown, &Systems::InputSystem::OnGamepadButtonDown); - EVENT_SUBSCRIBE_MEMBER(m_EGamepadButtonUp, &Systems::InputSystem::OnGamepadButtonUp); - EVENT_SUBSCRIBE_MEMBER(m_EBindKey, &Systems::InputSystem::OnBindKey); - EVENT_SUBSCRIBE_MEMBER(m_EBindMouseButton, &Systems::InputSystem::OnBindMouseButton); - EVENT_SUBSCRIBE_MEMBER(m_EBindGamepadAxis, &Systems::InputSystem::OnBindGamepadAxis); - EVENT_SUBSCRIBE_MEMBER(m_EBindGamepadButton, &Systems::InputSystem::OnBindGamepadButton); -} - -void Systems::InputSystem::Update(double dt) -{ - -} - -bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event) -{ - auto range = m_KeyBindings.equal_range(event.KeyCode); - for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { - std::string command; - float value; - std::tie(command, value) = bindingIt->second; - m_CommandKeyboardValues[command][event.KeyCode] = value; - PublishCommand(1, command, GetCommandTotalValue(command)); - } - - return true; -} - -bool Systems::InputSystem::OnKeyUp(const Events::KeyUp &event) -{ - auto range = m_KeyBindings.equal_range(event.KeyCode); - for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { - std::string command; - float value; - std::tie(command, value) = bindingIt->second; - m_CommandKeyboardValues[command][event.KeyCode] = 0; - PublishCommand(1, command, GetCommandTotalValue(command));; - } - - return true; -} - -bool Systems::InputSystem::OnMousePress(const Events::MousePress &event) -{ - auto range = m_MouseButtonBindings.equal_range(event.Button); - for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { - std::string command; - float value; - std::tie(command, value) = bindingIt->second; - m_CommandMouseButtonValues[command][event.Button] = value; - PublishCommand(1, command, GetCommandTotalValue(command)); - } - - return true; -} - -bool Systems::InputSystem::OnMouseRelease(const Events::MouseRelease &event) -{ - auto range = m_MouseButtonBindings.equal_range(event.Button); - for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { - std::string command; - float value; - std::tie(command, value) = bindingIt->second; - m_CommandMouseButtonValues[command][event.Button] = 0; - PublishCommand(1, command, GetCommandTotalValue(command)); - } - - return true; -} - -bool Systems::InputSystem::OnGamepadAxis(const Events::GamepadAxis &event) -{ - auto range = m_GamepadAxisBindings.equal_range(event.Axis); - for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { - std::string command; - float value; - std::tie(command, value) = bindingIt->second; - m_CommandGamepadAxisValues[command][event.Axis] = event.Value * value; - PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); - } - - return true; -} - -bool Systems::InputSystem::OnGamepadButtonDown(const Events::GamepadButtonDown &event) -{ - auto range = m_GamepadButtonBindings.equal_range(event.Button); - for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { - std::string command; - float value; - std::tie(command, value) = bindingIt->second; - m_CommandGamepadButtonValues[command][event.Button] = value; - PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); - } - - return true; -} - -bool Systems::InputSystem::OnGamepadButtonUp(const Events::GamepadButtonUp &event) -{ - auto range = m_GamepadButtonBindings.equal_range(event.Button); - for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { - std::string command; - float value; - std::tie(command, value) = bindingIt->second; - m_CommandGamepadButtonValues[command][event.Button] = 0; - PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); - } - - return true; -} - -bool Systems::InputSystem::OnBindKey(const Events::BindKey &event) -{ - if (event.Command.empty()) { - return false; - } - - m_KeyBindings.insert(std::make_pair(event.KeyCode, std::make_tuple(event.Command, event.Value))); - LOG_DEBUG("Input: Bound key %i to %s", event.KeyCode, event.Command.c_str()); - - return true; -} - -bool Systems::InputSystem::OnBindMouseButton(const Events::BindMouseButton &event) -{ - if (event.Command.empty()) { - return false; - } - - m_MouseButtonBindings.insert(std::make_pair(event.Button, std::make_tuple(event.Command, event.Value))); - LOG_DEBUG("Input: Bound mouse button %i to %s", event.Button, event.Command.c_str()); - - return true; -} - -bool Systems::InputSystem::OnBindGamepadAxis(const Events::BindGamepadAxis &event) -{ - if (event.Command.empty()) { - return false; - } - - m_GamepadAxisBindings.insert(std::make_pair(event.Axis, std::make_tuple(event.Command, event.Value))); - LOG_DEBUG("Input: Bound gamepad axis %i to %s", event.Axis, event.Command.c_str()); - - return true; -} - -bool Systems::InputSystem::OnBindGamepadButton(const Events::BindGamepadButton &event) -{ - if (event.Command.empty()) { - return false; - } - - m_GamepadButtonBindings.insert(std::make_pair(event.Button, std::make_tuple(event.Command, event.Value))); - LOG_DEBUG("Input: Bound gamepad axis %i to %s", event.Button, event.Command.c_str()); - - return true; -} - -float Systems::InputSystem::GetCommandTotalValue(std::string command) -{ - float value = 0.f; - - auto keyboardIt = m_CommandKeyboardValues.find(command); - if (keyboardIt != m_CommandKeyboardValues.end()) { - for (auto &key : keyboardIt->second) { - value += key.second; - } - } - - auto mouseButtonIt = m_CommandMouseButtonValues.find(command); - if (mouseButtonIt != m_CommandMouseButtonValues.end()) { - for (auto &button : mouseButtonIt->second) { - value += button.second; - } - } - - auto gamepadAxisIt = m_CommandGamepadAxisValues.find(command); - if (gamepadAxisIt != m_CommandGamepadAxisValues.end()) { - for (auto &axis : gamepadAxisIt->second) { - value += axis.second; - } - } - - auto gamepadButtonIt = m_CommandGamepadButtonValues.find(command); - if (gamepadButtonIt != m_CommandGamepadButtonValues.end()) { - for (auto &button : gamepadButtonIt->second) { - value += button.second; - } - } - - return std::max(-1.f, std::min(value, 1.f)); -} - -void Systems::InputSystem::PublishCommand(int playerID, std::string command, float value) -{ - Events::InputCommand e; - e.PlayerID = playerID; - e.Command = command; - e.Value = value; - EventBroker->Publish(e); - - LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, playerID); -} diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index fa12bbc6..5451289e 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -29,6 +29,9 @@ Game::Game(int argc, char* argv[]) // Create input manager m_InputManager = new InputManager(m_Renderer->Window(), m_EventBroker); + m_InputProxy = new InputProxy(m_EventBroker); + m_InputProxy->AddHandler(); + m_InputProxy->AddHandler(); // Create the root level GUI frame m_FrameStack = new GUI::Frame(m_EventBroker); @@ -66,9 +69,14 @@ void Game::Tick() double dt = currentTime - m_LastTime; m_LastTime = currentTime; + // Handle input in a weird looking but responsive way m_EventBroker->Swap(); m_InputManager->Update(dt); m_EventBroker->Swap(); + m_InputProxy->Update(dt); + m_EventBroker->Swap(); + m_InputProxy->Process(); + m_EventBroker->Swap(); // Iterate through systems and update world! m_SystemPipeline->Update(m_World, dt); From 784a04b8ada504e080c5ec1b2b2caaa5d589a3a8 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 11 Dec 2015 10:58:32 +0100 Subject: [PATCH 2/8] Fixed bug in config file overriding. Previously it was silently overwriting the whole default config and relied on hardcoded values for defaults. --- src/Engine/Core/ConfigFile.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Engine/Core/ConfigFile.cpp b/src/Engine/Core/ConfigFile.cpp index 004ebc43..00ab4993 100644 --- a/src/Engine/Core/ConfigFile.cpp +++ b/src/Engine/Core/ConfigFile.cpp @@ -24,8 +24,11 @@ ConfigFile::ConfigFile(std::string path) if (boost::filesystem::exists(m_Path)) { try { boost::property_tree::ini_parser::read_ini(m_Path.string(), m_PTreeOverrides); - for (auto& node : m_PTreeOverrides) { - m_PTreeMerged.put_child(node.first, node.second); + for (auto& topLevelNode : m_PTreeOverrides) { + auto& mergedTopLevelNode = m_PTreeMerged.find(topLevelNode.first); + for (auto& childOverrideNode : topLevelNode.second) { + mergedTopLevelNode->second.put_child(childOverrideNode.first, childOverrideNode.second); + } } } catch (boost::property_tree::ptree_error& e) { LOG_ERROR("Failed to parse \"%s\":\n%s", m_Path.filename().string().c_str(), e.what()); From 5d46a10a4e35b3d9f273336f3a9d2bf336796ddd Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 11 Dec 2015 10:58:59 +0100 Subject: [PATCH 3/8] Added GetAll function to config file that returns a list of all top level keys in a config file --- include/Engine/Core/ConfigFile.h | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/include/Engine/Core/ConfigFile.h b/include/Engine/Core/ConfigFile.h index 28c34bd3..54bbbad4 100644 --- a/include/Engine/Core/ConfigFile.h +++ b/include/Engine/Core/ConfigFile.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "../Common.h" #include "ResourceManager.h" @@ -19,12 +20,14 @@ private: public: template T Get(std::string key, T defaultValue); + template + std::vector> GetAll(std::string key); template void Set(std::string key, T value); void SaveToDisk(); - private: +private: boost::filesystem::path m_Path; boost::property_tree::ptree m_PTreeDefaults; boost::property_tree::ptree m_PTreeOverrides; @@ -37,6 +40,21 @@ T ConfigFile::Get(std::string key, T defaultValue) return m_PTreeMerged.get(key, defaultValue); } +template +std::vector> ConfigFile::GetAll(std::string key) +{ + std::vector> out; + auto parent = m_PTreeMerged.find(key); + if (parent == m_PTreeMerged.not_found()) { + return out; + } + for (auto& child : parent->second) { + T value = boost::lexical_cast(child.second.data()); + out.push_back(std::make_pair(child.first, value)); + } + return out; +} + template void ConfigFile::Set(std::string key, T value) { From 35624008e74159d1ec3bd4cf5fc5d3bbf6af2a33 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 11 Dec 2015 11:39:20 +0100 Subject: [PATCH 4/8] InputProxy with a KeyboardInputHandler to translate keyboard keys to input commands. Input origins are bound to commands through the BindOrigin event, or through loading them from a config file. --- include/Engine/Input/EBindGamepadAxis.h | 24 -- include/Engine/Input/EBindGamepadButton.h | 26 -- include/Engine/Input/EBindKey.h | 25 -- include/Engine/Input/EBindMouseButton.h | 25 -- include/Engine/Input/InputHandler.h | 24 ++ include/Engine/Input/InputProxy.h | 149 ++-------- include/Engine/Input/KeyboardInputHandler.h | 61 +--- include/Game/Game.h | 8 +- resources/DefaultInput.ini | 9 + src/Engine/Input/InputProxy.cpp | 297 +++++--------------- src/Engine/Input/KeyboardInputHandler.cpp | 172 ++++++++++++ src/Game/Game.cpp | 16 +- tools/deploy.bat | 1 + 13 files changed, 328 insertions(+), 509 deletions(-) delete mode 100644 include/Engine/Input/EBindGamepadAxis.h delete mode 100644 include/Engine/Input/EBindGamepadButton.h delete mode 100644 include/Engine/Input/EBindKey.h delete mode 100644 include/Engine/Input/EBindMouseButton.h create mode 100644 include/Engine/Input/InputHandler.h create mode 100644 resources/DefaultInput.ini create mode 100644 src/Engine/Input/KeyboardInputHandler.cpp diff --git a/include/Engine/Input/EBindGamepadAxis.h b/include/Engine/Input/EBindGamepadAxis.h deleted file mode 100644 index 1f2665ad..00000000 --- a/include/Engine/Input/EBindGamepadAxis.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef Events_BindGamepadAxis_h__ -#define Events_BindGamepadAxis_h__ - -#include "Core/EventBroker.h" -#include "Core/EGamepadAxis.h" - -namespace Events -{ - -/** Called to bind a gamepad axis to an input command. */ -struct BindGamepadAxis : Event -{ - /** The axis to bind. */ - Gamepad::Axis Axis; - /** The command to send. */ - std::string Command; - /** The value to send for positive stimulation. - - Multiplied by the 0-1 clamped value of the axis. - */ - float Value; -}; - -} diff --git a/include/Engine/Input/EBindGamepadButton.h b/include/Engine/Input/EBindGamepadButton.h deleted file mode 100644 index 47dddf31..00000000 --- a/include/Engine/Input/EBindGamepadButton.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef Events_BindGamepadButton_h__ -#define Events_BindGamepadButton_h__ - -#include "Core/EventBroker.h" -#include "Core/EGamepadButton.h" - -namespace Events -{ - -/** Called to bind a gamepad button to an input command. */ -struct BindGamepadButton : Event -{ - /** The gamepad button to bind. */ - Gamepad::Button Button; - /** The command to send. */ - std::string Command; - /** The value to send for positive stimulation. - - Multiplied by the 0-1 clamped value of the button. - */ - float Value; -}; - -} - -#endif diff --git a/include/Engine/Input/EBindKey.h b/include/Engine/Input/EBindKey.h deleted file mode 100644 index fb1c199a..00000000 --- a/include/Engine/Input/EBindKey.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef Events_BindKey_h__ -#define Events_BindKey_h__ - -#include "Core/EventBroker.h" - -namespace Events -{ - -/** Called to bind a keyboard key to an input command. */ -struct BindKey : Event -{ - /** The GLFW key code to bind. */ - int KeyCode; - /** The command to send. */ - std::string Command; - /** The value to send for positive stimulation. - - Multiplied by the 0-1 clamped value of the key. - */ - float Value; -}; - -} - -#endif diff --git a/include/Engine/Input/EBindMouseButton.h b/include/Engine/Input/EBindMouseButton.h deleted file mode 100644 index 78b5dfa3..00000000 --- a/include/Engine/Input/EBindMouseButton.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef Events_BindMouseButton_h__ -#define Events_BindMouseButton_h__ - -#include "Core/EventBroker.h" - -namespace Events -{ - -/** Called to bind a mouse button to an input command. */ -struct BindMouseButton : Event -{ - /** The GLFW mouse button code to bind. */ - int Button; - /** The command to send. */ - std::string Command; - /** The value to send for positive stimulation. - - Multiplied by the 0-1 clamped value of the button. - */ - float Value; -}; - -} - -#endif diff --git a/include/Engine/Input/InputHandler.h b/include/Engine/Input/InputHandler.h new file mode 100644 index 00000000..c8ecb3af --- /dev/null +++ b/include/Engine/Input/InputHandler.h @@ -0,0 +1,24 @@ +#ifndef InputHandler_h__ +#define InputHandler_h__ + +#include "../Common.h" +#include "../Core/EventBroker.h" +#include "InputProxy.h" + +class InputHandler +{ +public: + InputHandler(EventBroker* eventBroker, InputProxy* inputProxy) + : m_EventBroker(eventBroker) + , m_InputProxy(inputProxy) + { } + + virtual void Update(double dt) { } + virtual bool BindOrigin(std::string origin, std::string command, float value) = 0; + +protected: + EventBroker* m_EventBroker; + InputProxy* m_InputProxy; +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Input/InputProxy.h b/include/Engine/Input/InputProxy.h index 1b0e213e..d3ee57d9 100644 --- a/include/Engine/Input/InputProxy.h +++ b/include/Engine/Input/InputProxy.h @@ -1,146 +1,41 @@ -#ifndef InputSystem_h__ -#define InputSystem_h__ +#ifndef InputProxy_h__ +#define InputProxy_h__ -#include -#include - -#include - -#include "Core/EKeyUp.h" -#include "Core/EKeyDown.h" -#include "Core/EMousePress.h" -#include "Core/EMouseRelease.h" -#include "Core/EGamepadAxis.h" -#include "Core/EGamepadButton.h" -#include "Core/Util/EnumClassHash.h" -#include "EBindKey.h" -#include "EBindMouseButton.h" -#include "EBindGamepadAxis.h" -#include "EBindGamepadButton.h" +#include "../Common.h" +#include "../Core/ResourceManager.h" +#include "../Core/ConfigFile.h" #include "EInputCommand.h" #include "EBindOrigin.h" -class InputProxy; - -class InputHandler -{ -public: - InputHandler(EventBroker* eventBroker, InputProxy* inputProxy) - : m_EventBroker(eventBroker) - , m_InputProxy(inputProxy) - { } - - virtual void Update(double dt) { } - virtual bool BindOrigin(std::string origin, std::string command, float value) = 0; - -protected: - EventBroker* m_EventBroker; - InputProxy* m_InputProxy; -}; +class InputHandler; class InputProxy { public: - InputProxy(EventBroker* eventBroker) - : m_EventBroker(eventBroker) - { - EVENT_SUBSCRIBE_MEMBER(m_EBindOrigin, &InputProxy::OnBindOrigin); - } - - void Update(double dt) - { - m_EventBroker->Process(); - m_EventBroker->Process(); - for (auto& handler : m_Handlers) { - handler->Update(dt); - } - } - - void Process() - { - // Accumulate the input values of all unique commands published by input handlers - for (auto& pair : m_CommandQueue) { - Events::InputCommand e; - e.PlayerID = pair.first.first; - e.Command = pair.first.second; - e.Value = 0; - for (auto& value : pair.second) { - e.Value += value; - } - e.Value = std::max(-1.f, std::min(e.Value, 1.f)); - m_EventBroker->Publish(e); - LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); - } - m_CommandQueue.clear(); - } - + InputProxy(EventBroker* eventBroker); + ~InputProxy(); + + void LoadBindings(std::string file); + void Update(double dt); + void Process(); template - void AddHandler() - { - m_Handlers.push_back(new T(m_EventBroker, this)); - } - - void Publish(const Events::InputCommand& e) - { - auto key = std::make_pair(e.PlayerID, e.Command); - m_CommandQueue[key].push_back(e.Value); - } + void AddHandler(); + void Publish(const Events::InputCommand& e); protected: EventBroker* m_EventBroker; std::vector m_Handlers; - // Represents every unique command (has of PlayerID & Command) and all values reported for that command + // Represents every unique command (has of PlayerID & Command) and all values reported for that command this frame std::map, std::vector> m_CommandQueue; EventRelay m_EBindOrigin; - bool OnBindOrigin(const Events::BindOrigin& e) - { - bool originBound = false; - for (auto& handler : m_Handlers) { - bool result = handler->BindOrigin(e.Origin, e.Command, e.Value); - if (result) { - if (originBound) { - LOG_WARNING("Multiple handlers responded to binding input origin \"%s\"!", e.Origin.c_str()); - } - originBound = true; - } - } - - if (!originBound) { - LOG_ERROR("No input handler responded to binding input origin \"%s\"!", e.Origin.c_str()); - } - - return originBound; - } - //std::unordered_map> m_CommandMouseButtonValues; // command string -> mouse button value for command - //std::unordered_map> m_CommandGamepadAxisValues; // command string -> gamepad axis value for command - //std::unordered_map> m_CommandGamepadButtonValues; // command string -> gamepad button value for command - //// Input binding tables - //std::unordered_multimap> m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string - //std::unordered_multimap, EnumClassHash> m_GamepadAxisBindings; // Gamepad::Axis -> command string & value - //std::unordered_multimap, EnumClassHash> m_GamepadButtonBindings; // Gamepad::Button -> command string - - //// Input events - //EventRelay m_EMousePress; - //bool OnMousePress(const Events::MousePress &event); - //EventRelay m_EMouseRelease; - //bool OnMouseRelease(const Events::MouseRelease &event); - //EventRelay m_EGamepadAxis; - //bool OnGamepadAxis(const Events::GamepadAxis &event); - //EventRelay m_EGamepadButtonDown; - //bool OnGamepadButtonDown(const Events::GamepadButtonDown &event); - //EventRelay m_EGamepadButtonUp; - //bool OnGamepadButtonUp(const Events::GamepadButtonUp &event); - //// Input binding events - //EventRelay m_EBindMouseButton; - //bool OnBindMouseButton(const Events::BindMouseButton &event); - //EventRelay m_EBindGamepadAxis; - //bool OnBindGamepadAxis(const Events::BindGamepadAxis &event); - //EventRelay m_EBindGamepadButton; - //bool OnBindGamepadButton(const Events::BindGamepadButton &event); - - //float GetCommandTotalValue(std::string command); - //void PublishCommand(int playerID, std::string command, float value); + bool OnBindOrigin(const Events::BindOrigin& e); }; +template +void InputProxy::AddHandler() +{ + m_Handlers.push_back(new T(m_EventBroker, this)); +} + #endif diff --git a/include/Engine/Input/KeyboardInputHandler.h b/include/Engine/Input/KeyboardInputHandler.h index 1ffaa067..53b08f7a 100644 --- a/include/Engine/Input/KeyboardInputHandler.h +++ b/include/Engine/Input/KeyboardInputHandler.h @@ -1,67 +1,26 @@ +#ifndef KeyboardInputHandler_h__ +#define KeyboardInputHandler_h__ + #include -#include "InputProxy.h" +#include "InputHandler.h" #include "Core/EKeyDown.h" #include "Core/EKeyUp.h" class KeyboardInputHandler : public InputHandler { public: - KeyboardInputHandler(EventBroker* eventBroker, InputProxy* inputProxy) - : InputHandler(eventBroker, inputProxy) - { - EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &KeyboardInputHandler::OnKeyDown); - EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &KeyboardInputHandler::OnKeyUp); + KeyboardInputHandler(EventBroker* eventBroker, InputProxy* inputProxy); - m_OriginKeyCodes["R"] = GLFW_KEY_R; - } - - bool BindOrigin(std::string origin, std::string command, float value) override - { - auto originIt = m_OriginKeyCodes.find(origin); - if (originIt == m_OriginKeyCodes.end()) { - return false; - } - - int keyCode = originIt->second; - m_KeyBindings[keyCode] = std::make_tuple(command, value); - return true; - } + bool BindOrigin(std::string origin, std::string command, float value) override; private: std::unordered_map m_OriginKeyCodes; std::unordered_map> m_KeyBindings; // GLFW_KEY... -> command string & value EventRelay m_EKeyDown; - bool OnKeyDown(const Events::KeyDown& e) - { - auto it = m_KeyBindings.find(e.KeyCode); - if (it == m_KeyBindings.end()) { - return false; - } - - Events::InputCommand ic; - ic.PlayerID = 0; - std::tie(ic.Command, ic.Value) = it->second; - m_InputProxy->Publish(ic); - - return true; - } - + bool OnKeyDown(const Events::KeyDown& e); EventRelay m_EKeyUp; - bool OnKeyUp(const Events::KeyUp& e) - { - auto it = m_KeyBindings.find(e.KeyCode); - if (it == m_KeyBindings.end()) { - return false; - } + bool OnKeyUp(const Events::KeyUp& e); +}; - Events::InputCommand ic; - ic.PlayerID = 0; - std::tie(ic.Command, std::ignore) = it->second; - ic.Value = 0; - m_InputProxy->Publish(ic); - - return true; - } - -}; \ No newline at end of file +#endif \ No newline at end of file diff --git a/include/Game/Game.h b/include/Game/Game.h index 04d14be0..fbd4b017 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -38,11 +38,11 @@ private: SystemPipeline* m_SystemPipeline; RenderQueueFactory* m_RenderQueueFactory; - EventRelay m_EKeyUp; - bool testOnKeyUp(const Events::KeyUp& e); + EventRelay m_EInputCommand; + bool debugOnInputCommand(const Events::InputCommand& e); - void testIntialize(); - void testTick(double dt); + void debugInitialize(); + void debugTick(double dt); }; #endif diff --git a/resources/DefaultInput.ini b/resources/DefaultInput.ini new file mode 100644 index 00000000..bc012c52 --- /dev/null +++ b/resources/DefaultInput.ini @@ -0,0 +1,9 @@ +[Bindings] +W=Forward +A=Left +S=Back +D=Right +R=Reload +Space=Jump +LeftControl=Crouch +LeftShift=Sprint diff --git a/src/Engine/Input/InputProxy.cpp b/src/Engine/Input/InputProxy.cpp index 3a6626df..65f3f7b4 100644 --- a/src/Engine/Input/InputProxy.cpp +++ b/src/Engine/Input/InputProxy.cpp @@ -1,221 +1,80 @@ #include "Input/InputProxy.h" -#include "Core/World.h" +#include "Input/InputHandler.h" -//InputProxy::InputProxy(EventBroker* eventBroker) -// : m_EventBroker(eventBroker) -//{ -// // Subscribe to events -// EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &InputProxy::OnMousePress); -// EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &InputProxy::OnMouseRelease); -// EVENT_SUBSCRIBE_MEMBER(m_EGamepadAxis, &InputProxy::OnGamepadAxis); -// EVENT_SUBSCRIBE_MEMBER(m_EGamepadButtonDown, &InputProxy::OnGamepadButtonDown); -// EVENT_SUBSCRIBE_MEMBER(m_EGamepadButtonUp, &InputProxy::OnGamepadButtonUp); -// EVENT_SUBSCRIBE_MEMBER(m_EBindKey, &InputProxy::OnBindKey); -// EVENT_SUBSCRIBE_MEMBER(m_EBindMouseButton, &InputProxy::OnBindMouseButton); -// EVENT_SUBSCRIBE_MEMBER(m_EBindGamepadAxis, &InputProxy::OnBindGamepadAxis); -// EVENT_SUBSCRIBE_MEMBER(m_EBindGamepadButton, &InputProxy::OnBindGamepadButton); -// -// SteamController()->Init(); -// -//} -// -//void InputProxy::Update(double dt) -//{ -// std::array controllers; -// int numControllers = SteamController()->GetConnectedControllers(controllers.data()); -// -// ControllerDigitalActionHandle_t debug_reload_handle = SteamController()->GetDigitalActionHandle("debug_reload"); -// SteamController()-> -//} -// -//bool InputProxy::OnKeyDown(const Events::KeyDown &event) -//{ -// auto range = m_KeyBindings.equal_range(event.KeyCode); -// for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { -// std::string command; -// float value; -// std::tie(command, value) = bindingIt->second; -// m_CommandKeyboardValues[command][event.KeyCode] = value; -// PublishCommand(1, command, GetCommandTotalValue(command)); -// } -// -// return true; -//} -// -//bool InputProxy::OnKeyUp(const Events::KeyUp &event) -//{ -// auto range = m_KeyBindings.equal_range(event.KeyCode); -// for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { -// std::string command; -// float value; -// std::tie(command, value) = bindingIt->second; -// m_CommandKeyboardValues[command][event.KeyCode] = 0; -// PublishCommand(1, command, GetCommandTotalValue(command));; -// } -// -// return true; -//} -// -//bool InputProxy::OnMousePress(const Events::MousePress &event) -//{ -// auto range = m_MouseButtonBindings.equal_range(event.Button); -// for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { -// std::string command; -// float value; -// std::tie(command, value) = bindingIt->second; -// m_CommandMouseButtonValues[command][event.Button] = value; -// PublishCommand(1, command, GetCommandTotalValue(command)); -// } -// -// return true; -//} -// -//bool InputProxy::OnMouseRelease(const Events::MouseRelease &event) -//{ -// auto range = m_MouseButtonBindings.equal_range(event.Button); -// for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { -// std::string command; -// float value; -// std::tie(command, value) = bindingIt->second; -// m_CommandMouseButtonValues[command][event.Button] = 0; -// PublishCommand(1, command, GetCommandTotalValue(command)); -// } -// -// return true; -//} -// -//bool InputProxy::OnGamepadAxis(const Events::GamepadAxis &event) -//{ -// auto range = m_GamepadAxisBindings.equal_range(event.Axis); -// for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { -// std::string command; -// float value; -// std::tie(command, value) = bindingIt->second; -// m_CommandGamepadAxisValues[command][event.Axis] = event.Value * value; -// PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); -// } -// -// return true; -//} -// -//bool InputProxy::OnGamepadButtonDown(const Events::GamepadButtonDown &event) -//{ -// auto range = m_GamepadButtonBindings.equal_range(event.Button); -// for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { -// std::string command; -// float value; -// std::tie(command, value) = bindingIt->second; -// m_CommandGamepadButtonValues[command][event.Button] = value; -// PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); -// } -// -// return true; -//} -// -//bool InputProxy::OnGamepadButtonUp(const Events::GamepadButtonUp &event) -//{ -// auto range = m_GamepadButtonBindings.equal_range(event.Button); -// for (auto bindingIt = range.first; bindingIt != range.second; bindingIt++) { -// std::string command; -// float value; -// std::tie(command, value) = bindingIt->second; -// m_CommandGamepadButtonValues[command][event.Button] = 0; -// PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); -// } -// -// return true; -//} -// -//bool InputProxy::OnBindKey(const Events::BindKey &event) -//{ -// if (event.Command.empty()) { -// return false; -// } -// -// m_KeyBindings.insert(std::make_pair(event.KeyCode, std::make_tuple(event.Command, event.Value))); -// LOG_DEBUG("Input: Bound key %i to %s", event.KeyCode, event.Command.c_str()); -// -// return true; -//} -// -//bool InputProxy::OnBindMouseButton(const Events::BindMouseButton &event) -//{ -// if (event.Command.empty()) { -// return false; -// } -// -// m_MouseButtonBindings.insert(std::make_pair(event.Button, std::make_tuple(event.Command, event.Value))); -// LOG_DEBUG("Input: Bound mouse button %i to %s", event.Button, event.Command.c_str()); -// -// return true; -//} -// -//bool InputProxy::OnBindGamepadAxis(const Events::BindGamepadAxis &event) -//{ -// if (event.Command.empty()) { -// return false; -// } -// -// m_GamepadAxisBindings.insert(std::make_pair(event.Axis, std::make_tuple(event.Command, event.Value))); -// LOG_DEBUG("Input: Bound gamepad axis %i to %s", event.Axis, event.Command.c_str()); -// -// return true; -//} -// -//bool InputProxy::OnBindGamepadButton(const Events::BindGamepadButton &event) -//{ -// if (event.Command.empty()) { -// return false; -// } -// -// m_GamepadButtonBindings.insert(std::make_pair(event.Button, std::make_tuple(event.Command, event.Value))); -// LOG_DEBUG("Input: Bound gamepad axis %i to %s", event.Button, event.Command.c_str()); -// -// return true; -//} -// -//float InputProxy::GetCommandTotalValue(std::string command) -//{ -// float value = 0.f; -// -// auto keyboardIt = m_CommandKeyboardValues.find(command); -// if (keyboardIt != m_CommandKeyboardValues.end()) { -// for (auto &key : keyboardIt->second) { -// value += key.second; -// } -// } -// -// auto mouseButtonIt = m_CommandMouseButtonValues.find(command); -// if (mouseButtonIt != m_CommandMouseButtonValues.end()) { -// for (auto &button : mouseButtonIt->second) { -// value += button.second; -// } -// } -// -// auto gamepadAxisIt = m_CommandGamepadAxisValues.find(command); -// if (gamepadAxisIt != m_CommandGamepadAxisValues.end()) { -// for (auto &axis : gamepadAxisIt->second) { -// value += axis.second; -// } -// } -// -// auto gamepadButtonIt = m_CommandGamepadButtonValues.find(command); -// if (gamepadButtonIt != m_CommandGamepadButtonValues.end()) { -// for (auto &button : gamepadButtonIt->second) { -// value += button.second; -// } -// } -// -// return std::max(-1.f, std::min(value, 1.f)); -//} -// -//void InputProxy::PublishCommand(int playerID, std::string command, float value) -//{ -// Events::InputCommand e; -// e.PlayerID = playerID; -// e.Command = command; -// e.Value = value; -// m_EventBroker->Publish(e); -// -// LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, playerID); -//} +InputProxy::InputProxy(EventBroker* eventBroker) + : m_EventBroker(eventBroker) +{ + EVENT_SUBSCRIBE_MEMBER(m_EBindOrigin, &InputProxy::OnBindOrigin); +} + +InputProxy::~InputProxy() +{ + for (auto& handler : m_Handlers) { + delete handler; + } +} + +void InputProxy::LoadBindings(std::string file) +{ + auto config = ResourceManager::Load(file); + for (auto& origin : config->GetAll("Bindings")) { + Events::BindOrigin e; + e.Origin = origin.first; + e.Command = origin.second; + e.Value = 1.f; + OnBindOrigin(e); + } +} + +void InputProxy::Update(double dt) +{ + m_EventBroker->Process(); + m_EventBroker->Process(); + for (auto& handler : m_Handlers) { + handler->Update(dt); + } +} + +void InputProxy::Process() +{ + // Accumulate the input values of all unique commands published by input handlers + for (auto& pair : m_CommandQueue) { + Events::InputCommand e; + e.PlayerID = pair.first.first; + e.Command = pair.first.second; + e.Value = 0; + for (auto& value : pair.second) { + e.Value += value; + } + e.Value = std::max(-1.f, std::min(e.Value, 1.f)); + m_EventBroker->Publish(e); + LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); + } + m_CommandQueue.clear(); +} + +void InputProxy::Publish(const Events::InputCommand& e) +{ + auto key = std::make_pair(e.PlayerID, e.Command); + m_CommandQueue[key].push_back(e.Value); +} + +bool InputProxy::OnBindOrigin(const Events::BindOrigin& e) +{ + bool originBound = false; + for (auto& handler : m_Handlers) { + bool result = handler->BindOrigin(e.Origin, e.Command, e.Value); + if (result) { + if (originBound) { + LOG_WARNING("Multiple handlers responded to binding input origin \"%s\"!", e.Origin.c_str()); + } + originBound = true; + } + } + + if (!originBound) { + LOG_ERROR("No input handler responded to binding input origin \"%s\"!", e.Origin.c_str()); + } + + return originBound; +} diff --git a/src/Engine/Input/KeyboardInputHandler.cpp b/src/Engine/Input/KeyboardInputHandler.cpp new file mode 100644 index 00000000..1686d8cd --- /dev/null +++ b/src/Engine/Input/KeyboardInputHandler.cpp @@ -0,0 +1,172 @@ +#include "Input/KeyboardInputHandler.h" + +KeyboardInputHandler::KeyboardInputHandler(EventBroker* eventBroker, InputProxy* inputProxy) : InputHandler(eventBroker, inputProxy) +{ + EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &KeyboardInputHandler::OnKeyDown); + EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &KeyboardInputHandler::OnKeyUp); + + m_OriginKeyCodes["Space"] = GLFW_KEY_SPACE; + m_OriginKeyCodes["Apostrophe"] = GLFW_KEY_APOSTROPHE; + m_OriginKeyCodes["Comma"] = GLFW_KEY_COMMA; + m_OriginKeyCodes["Minus"] = GLFW_KEY_MINUS; + m_OriginKeyCodes["Period"] = GLFW_KEY_PERIOD; + m_OriginKeyCodes["Slash"] = GLFW_KEY_SLASH; + m_OriginKeyCodes["0"] = GLFW_KEY_0; + m_OriginKeyCodes["1"] = GLFW_KEY_1; + m_OriginKeyCodes["2"] = GLFW_KEY_2; + m_OriginKeyCodes["3"] = GLFW_KEY_3; + m_OriginKeyCodes["4"] = GLFW_KEY_4; + m_OriginKeyCodes["5"] = GLFW_KEY_5; + m_OriginKeyCodes["6"] = GLFW_KEY_6; + m_OriginKeyCodes["7"] = GLFW_KEY_7; + m_OriginKeyCodes["8"] = GLFW_KEY_8; + m_OriginKeyCodes["9"] = GLFW_KEY_9; + m_OriginKeyCodes["Semicolon"] = GLFW_KEY_SEMICOLON; + m_OriginKeyCodes["Equal"] = GLFW_KEY_EQUAL; + m_OriginKeyCodes["A"] = GLFW_KEY_A; + m_OriginKeyCodes["B"] = GLFW_KEY_B; + m_OriginKeyCodes["C"] = GLFW_KEY_C; + m_OriginKeyCodes["D"] = GLFW_KEY_D; + m_OriginKeyCodes["E"] = GLFW_KEY_E; + m_OriginKeyCodes["F"] = GLFW_KEY_F; + m_OriginKeyCodes["G"] = GLFW_KEY_G; + m_OriginKeyCodes["H"] = GLFW_KEY_H; + m_OriginKeyCodes["I"] = GLFW_KEY_I; + m_OriginKeyCodes["J"] = GLFW_KEY_J; + m_OriginKeyCodes["K"] = GLFW_KEY_K; + m_OriginKeyCodes["L"] = GLFW_KEY_L; + m_OriginKeyCodes["M"] = GLFW_KEY_M; + m_OriginKeyCodes["N"] = GLFW_KEY_N; + m_OriginKeyCodes["O"] = GLFW_KEY_O; + m_OriginKeyCodes["P"] = GLFW_KEY_P; + m_OriginKeyCodes["Q"] = GLFW_KEY_Q; + m_OriginKeyCodes["R"] = GLFW_KEY_R; + m_OriginKeyCodes["S"] = GLFW_KEY_S; + m_OriginKeyCodes["T"] = GLFW_KEY_T; + m_OriginKeyCodes["U"] = GLFW_KEY_U; + m_OriginKeyCodes["V"] = GLFW_KEY_V; + m_OriginKeyCodes["W"] = GLFW_KEY_W; + m_OriginKeyCodes["X"] = GLFW_KEY_X; + m_OriginKeyCodes["Y"] = GLFW_KEY_Y; + m_OriginKeyCodes["Z"] = GLFW_KEY_Z; + m_OriginKeyCodes["LeftBracket"] = GLFW_KEY_LEFT_BRACKET; + m_OriginKeyCodes["Backslash"] = GLFW_KEY_BACKSLASH; + m_OriginKeyCodes["RightBracket"] = GLFW_KEY_RIGHT_BRACKET; + m_OriginKeyCodes["Accent"] = GLFW_KEY_GRAVE_ACCENT; + m_OriginKeyCodes["W1"] = GLFW_KEY_WORLD_1; + m_OriginKeyCodes["W2"] = GLFW_KEY_WORLD_2; + m_OriginKeyCodes["Escape"] = GLFW_KEY_ESCAPE; + m_OriginKeyCodes["Enter"] = GLFW_KEY_ENTER; + m_OriginKeyCodes["Tab"] = GLFW_KEY_TAB; + m_OriginKeyCodes["Backspace"] = GLFW_KEY_BACKSPACE; + m_OriginKeyCodes["Insert"] = GLFW_KEY_INSERT; + m_OriginKeyCodes["Delete"] = GLFW_KEY_DELETE; + m_OriginKeyCodes["Right"] = GLFW_KEY_RIGHT; + m_OriginKeyCodes["Left"] = GLFW_KEY_LEFT; + m_OriginKeyCodes["Down"] = GLFW_KEY_DOWN; + m_OriginKeyCodes["Up"] = GLFW_KEY_UP; + m_OriginKeyCodes["PgUp"] = GLFW_KEY_PAGE_UP; + m_OriginKeyCodes["PgDn"] = GLFW_KEY_PAGE_DOWN; + m_OriginKeyCodes["Home"] = GLFW_KEY_HOME; + m_OriginKeyCodes["End"] = GLFW_KEY_END; + m_OriginKeyCodes["CapsLock"] = GLFW_KEY_CAPS_LOCK; + m_OriginKeyCodes["ScrollLock"] = GLFW_KEY_SCROLL_LOCK; + m_OriginKeyCodes["NumLock"] = GLFW_KEY_NUM_LOCK; + m_OriginKeyCodes["PrintScreen"] = GLFW_KEY_PRINT_SCREEN; + m_OriginKeyCodes["Pause"] = GLFW_KEY_PAUSE; + m_OriginKeyCodes["F1"] = GLFW_KEY_F1; + m_OriginKeyCodes["F2"] = GLFW_KEY_F2; + m_OriginKeyCodes["F3"] = GLFW_KEY_F3; + m_OriginKeyCodes["F4"] = GLFW_KEY_F4; + m_OriginKeyCodes["F5"] = GLFW_KEY_F5; + m_OriginKeyCodes["F6"] = GLFW_KEY_F6; + m_OriginKeyCodes["F7"] = GLFW_KEY_F7; + m_OriginKeyCodes["F8"] = GLFW_KEY_F8; + m_OriginKeyCodes["F9"] = GLFW_KEY_F9; + m_OriginKeyCodes["F10"] = GLFW_KEY_F10; + m_OriginKeyCodes["F11"] = GLFW_KEY_F11; + m_OriginKeyCodes["F12"] = GLFW_KEY_F12; + m_OriginKeyCodes["F13"] = GLFW_KEY_F13; + m_OriginKeyCodes["F14"] = GLFW_KEY_F14; + m_OriginKeyCodes["F15"] = GLFW_KEY_F15; + m_OriginKeyCodes["F16"] = GLFW_KEY_F16; + m_OriginKeyCodes["F17"] = GLFW_KEY_F17; + m_OriginKeyCodes["F18"] = GLFW_KEY_F18; + m_OriginKeyCodes["F19"] = GLFW_KEY_F19; + m_OriginKeyCodes["F20"] = GLFW_KEY_F20; + m_OriginKeyCodes["F21"] = GLFW_KEY_F21; + m_OriginKeyCodes["F22"] = GLFW_KEY_F22; + m_OriginKeyCodes["F23"] = GLFW_KEY_F23; + m_OriginKeyCodes["F24"] = GLFW_KEY_F24; + m_OriginKeyCodes["F25"] = GLFW_KEY_F25; + m_OriginKeyCodes["KP0"] = GLFW_KEY_KP_0; + m_OriginKeyCodes["KP1"] = GLFW_KEY_KP_1; + m_OriginKeyCodes["KP2"] = GLFW_KEY_KP_2; + m_OriginKeyCodes["KP3"] = GLFW_KEY_KP_3; + m_OriginKeyCodes["KP4"] = GLFW_KEY_KP_4; + m_OriginKeyCodes["KP5"] = GLFW_KEY_KP_5; + m_OriginKeyCodes["KP6"] = GLFW_KEY_KP_6; + m_OriginKeyCodes["KP7"] = GLFW_KEY_KP_7; + m_OriginKeyCodes["KP8"] = GLFW_KEY_KP_8; + m_OriginKeyCodes["KP9"] = GLFW_KEY_KP_9; + m_OriginKeyCodes["KPDecimal"] = GLFW_KEY_KP_DECIMAL; + m_OriginKeyCodes["KPDivide"] = GLFW_KEY_KP_DIVIDE; + m_OriginKeyCodes["KPMultiply"] = GLFW_KEY_KP_MULTIPLY; + m_OriginKeyCodes["KPSubtract"] = GLFW_KEY_KP_SUBTRACT; + m_OriginKeyCodes["KPAdd"] = GLFW_KEY_KP_ADD; + m_OriginKeyCodes["KPEnter"] = GLFW_KEY_KP_ENTER; + m_OriginKeyCodes["KPEqual"] = GLFW_KEY_KP_EQUAL; + m_OriginKeyCodes["LeftShift"] = GLFW_KEY_LEFT_SHIFT; + m_OriginKeyCodes["LeftControl"] = GLFW_KEY_LEFT_CONTROL; + m_OriginKeyCodes["LeftAlt"] = GLFW_KEY_LEFT_ALT; + m_OriginKeyCodes["LeftSuper"] = GLFW_KEY_LEFT_SUPER; + m_OriginKeyCodes["RightShift"] = GLFW_KEY_RIGHT_SHIFT; + m_OriginKeyCodes["RightControl"] = GLFW_KEY_RIGHT_CONTROL; + m_OriginKeyCodes["RightAlt"] = GLFW_KEY_RIGHT_ALT; + m_OriginKeyCodes["RightSuper"] = GLFW_KEY_RIGHT_SUPER; + m_OriginKeyCodes["Menu"] = GLFW_KEY_MENU; +} + +bool KeyboardInputHandler::BindOrigin(std::string origin, std::string command, float value) +{ + auto originIt = m_OriginKeyCodes.find(origin); + if (originIt == m_OriginKeyCodes.end()) { + return false; + } + + int keyCode = originIt->second; + m_KeyBindings[keyCode] = std::make_tuple(command, value); + return true; +} + +bool KeyboardInputHandler::OnKeyDown(const Events::KeyDown& e) +{ + auto it = m_KeyBindings.find(e.KeyCode); + if (it == m_KeyBindings.end()) { + return false; + } + + Events::InputCommand ic; + ic.PlayerID = 0; + std::tie(ic.Command, ic.Value) = it->second; + m_InputProxy->Publish(ic); + + return true; +} + +bool KeyboardInputHandler::OnKeyUp(const Events::KeyUp& e) +{ + auto it = m_KeyBindings.find(e.KeyCode); + if (it == m_KeyBindings.end()) { + return false; + } + + Events::InputCommand ic; + ic.PlayerID = 0; + std::tie(ic.Command, std::ignore) = it->second; + ic.Value = 0; + m_InputProxy->Publish(ic); + + return true; +} + diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 5451289e..5ae9295a 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -31,7 +31,7 @@ Game::Game(int argc, char* argv[]) m_InputManager = new InputManager(m_Renderer->Window(), m_EventBroker); m_InputProxy = new InputProxy(m_EventBroker); m_InputProxy->AddHandler(); - m_InputProxy->AddHandler(); + m_InputProxy->LoadBindings("Input.ini"); // Create the root level GUI frame m_FrameStack = new GUI::Frame(m_EventBroker); @@ -54,7 +54,7 @@ Game::Game(int argc, char* argv[]) m_LastTime = glfwGetTime(); - testIntialize(); + debugInitialize(); } Game::~Game() @@ -80,7 +80,7 @@ void Game::Tick() // Iterate through systems and update world! m_SystemPipeline->Update(m_World, dt); - testTick(dt); + debugTick(dt); m_Renderer->Update(dt); m_RenderQueueFactory->Update(m_World); @@ -93,9 +93,9 @@ void Game::Tick() } -bool Game::testOnKeyUp(const Events::KeyUp& e) +bool Game::debugOnInputCommand(const Events::InputCommand& e) { - if (e.KeyCode == GLFW_KEY_R) { + if (e.Command == "DebugReload" && e.Value == 1) { std::string mapToLoad = m_Config->Get("Debug.LoadMap", ""); if (!mapToLoad.empty()) { delete m_World; @@ -108,12 +108,12 @@ bool Game::testOnKeyUp(const Events::KeyUp& e) return false; } -void Game::testIntialize() +void Game::debugInitialize() { - EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Game::testOnKeyUp); + EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Game::debugOnInputCommand); } -void Game::testTick(double dt) +void Game::debugTick(double dt) { m_EventBroker->Process(); } diff --git a/tools/deploy.bat b/tools/deploy.bat index 5ef074ef..22d513e6 100755 --- a/tools/deploy.bat +++ b/tools/deploy.bat @@ -21,6 +21,7 @@ RMDIR /S /Q "%DeployLocation%\Shaders" MKLINK "%DeployLocation%\Shaders\" "resources\Shaders" /J :: Configuration files MKLINK "%DeployLocation%\DefaultConfig.ini" "resources\DefaultConfig.ini" /H +MKLINK "%DeployLocation%\DefaultInput.ini" "resources\DefaultInput.ini" /H :: Platform specific binaries IF "%~1"=="" GOTO :EOF From 013f911ae262f71e6679f9aa6282aabd023e63ca Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 11 Dec 2015 14:49:43 +0100 Subject: [PATCH 5/8] Added MouseInputHandler and firstPersonInputController to aid in creation of first person movement. --- include/Engine/Core/InputController.h | 11 +- .../Engine/Input/FirstPersonInputController.h | 40 ++++++ include/Engine/Input/MouseInputHandler.h | 33 +++++ include/Game/Game.h | 1 + resources/DefaultInput.ini | 4 + src/Engine/Input/InputProxy.cpp | 2 +- src/Engine/Input/MouseInputHandler.cpp | 121 ++++++++++++++++++ src/Game/Game.cpp | 1 + 8 files changed, 205 insertions(+), 8 deletions(-) create mode 100644 include/Engine/Input/FirstPersonInputController.h create mode 100644 include/Engine/Input/MouseInputHandler.h create mode 100644 src/Engine/Input/MouseInputHandler.cpp diff --git a/include/Engine/Core/InputController.h b/include/Engine/Core/InputController.h index b91fb448..b87d1eff 100644 --- a/include/Engine/Core/InputController.h +++ b/include/Engine/Core/InputController.h @@ -11,25 +11,22 @@ template class InputController { public: - InputController(std::shared_ptr eventBroker) - : EventBroker(eventBroker) + InputController(EventBroker* eventBroker) + : m_EventBroker(eventBroker) { Initialize(); } virtual void Initialize() { EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &InputController::OnCommand); - EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &InputController::OnMouseMove); } - virtual bool OnCommand(const Events::InputCommand &event) { return false; } - virtual bool OnMouseMove(const Events::MouseMove &event) { return false; } + virtual bool OnCommand(const Events::InputCommand& e) { return false; } protected: - std::shared_ptr EventBroker; + EventBroker* m_EventBroker; private: EventRelay m_EInputCommand; - EventRelay m_EMouseMove; }; #endif diff --git a/include/Engine/Input/FirstPersonInputController.h b/include/Engine/Input/FirstPersonInputController.h new file mode 100644 index 00000000..e8b832b5 --- /dev/null +++ b/include/Engine/Input/FirstPersonInputController.h @@ -0,0 +1,40 @@ +#ifndef FirstPersonInputController_h__ +#define FirstPersonInputController_h__ + +#include "../GLM.h" +#include "../Core/InputController.h" + +template +class FirstPersonInputController : public InputController +{ +public: + FirstPersonInputController(EventBroker* eventBroker, unsigned int playerID) + : InputController(eventBroker) + , m_PlayerID(playerID) + { } + + const glm::quat Orientation() const { return m_Orientation; } + + virtual bool OnCommand(const Events::InputCommand& e) override + { + if (m_PlayerID != e.PlayerID) { + return false; + } + + if (e.Command == "Pitch") { + float val = glm::radians(e.Value); + m_Orientation = m_Orientation * glm::angleAxis(-val, glm::vec3(1, 0, 0)); + } + + if (e.Command == "Yaw") { + float val = glm::radians(e.Value); + m_Orientation = glm::angleAxis(-val, glm::vec3(0, 1, 0)) * m_Orientation; + } + } + +private: + const unsigned int m_PlayerID; + glm::quat m_Orientation; +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Input/MouseInputHandler.h b/include/Engine/Input/MouseInputHandler.h new file mode 100644 index 00000000..39889755 --- /dev/null +++ b/include/Engine/Input/MouseInputHandler.h @@ -0,0 +1,33 @@ +#ifndef MouseInputHandler_h__ +#define MouseInputHandler_h__ + +#include +#include "InputHandler.h" +#include "Core/EMousePress.h" +#include "Core/EMouseRelease.h" +#include "Core/EMouseMove.h" + +class MouseInputHandler : public InputHandler +{ +public: + MouseInputHandler(EventBroker* eventBroker, InputProxy* inputProxy); + + bool BindOrigin(std::string origin, std::string command, float value) override; + +private: + std::unordered_map m_OriginCodes; + std::unordered_map m_OriginAxes; + std::unordered_map> m_Bindings; // GLFW_MOUSE_BUTTON... -> command string & value + std::unordered_map> m_Axes; // Axis -> command string & value + + EventRelay m_EMousePress; + bool OnMousePress(const Events::MousePress& e); + EventRelay m_EMouseRelease; + bool OnMouseRelease(const Events::MouseRelease& e); + EventRelay m_EMouseMove; + bool OnMouseMove(const Events::MouseMove& e); + + bool hasOrigin(std::string origin); +}; + +#endif diff --git a/include/Game/Game.h b/include/Game/Game.h index fbd4b017..57941e55 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -11,6 +11,7 @@ #include "Rendering/RenderQueueFactory.h" #include "Input/InputProxy.h" #include "Input/KeyboardInputHandler.h" +#include "Input/MouseInputHandler.h" #include "Core/EKeyDown.h" #include "Core/EntityXMLFile.h" #include "Core/SystemPipeline.h" diff --git a/resources/DefaultInput.ini b/resources/DefaultInput.ini index bc012c52..b6bbd20f 100644 --- a/resources/DefaultInput.ini +++ b/resources/DefaultInput.ini @@ -1,3 +1,7 @@ +[Mouse] +Sensitivity=0.5 +InvertPitch=false + [Bindings] W=Forward A=Left diff --git a/src/Engine/Input/InputProxy.cpp b/src/Engine/Input/InputProxy.cpp index 65f3f7b4..430cb250 100644 --- a/src/Engine/Input/InputProxy.cpp +++ b/src/Engine/Input/InputProxy.cpp @@ -46,7 +46,7 @@ void InputProxy::Process() for (auto& value : pair.second) { e.Value += value; } - e.Value = std::max(-1.f, std::min(e.Value, 1.f)); + //e.Value = std::max(-1.f, std::min(e.Value, 1.f)); m_EventBroker->Publish(e); LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); } diff --git a/src/Engine/Input/MouseInputHandler.cpp b/src/Engine/Input/MouseInputHandler.cpp new file mode 100644 index 00000000..2e9c8137 --- /dev/null +++ b/src/Engine/Input/MouseInputHandler.cpp @@ -0,0 +1,121 @@ +#include "Input/MouseInputHandler.h" + +MouseInputHandler::MouseInputHandler(EventBroker* eventBroker, InputProxy* inputProxy) + : InputHandler(eventBroker, inputProxy) +{ + EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &MouseInputHandler::OnMousePress); + EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &MouseInputHandler::OnMouseRelease); + EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &MouseInputHandler::OnMouseMove); + + m_OriginCodes["Mouse1"] = GLFW_MOUSE_BUTTON_1; + m_OriginCodes["MouseLeft"] = GLFW_MOUSE_BUTTON_LEFT; + m_OriginCodes["Mouse2"] = GLFW_MOUSE_BUTTON_2; + m_OriginCodes["MouseRight"] = GLFW_MOUSE_BUTTON_RIGHT; + m_OriginCodes["Mouse3"] = GLFW_MOUSE_BUTTON_3; + m_OriginCodes["MouseMiddle"] = GLFW_MOUSE_BUTTON_MIDDLE; + m_OriginCodes["Mouse4"] = GLFW_MOUSE_BUTTON_4; + m_OriginCodes["Mouse5"] = GLFW_MOUSE_BUTTON_5; + m_OriginCodes["Mouse6"] = GLFW_MOUSE_BUTTON_6; + m_OriginCodes["Mouse7"] = GLFW_MOUSE_BUTTON_7; + m_OriginCodes["Mouse8"] = GLFW_MOUSE_BUTTON_8; + + m_OriginAxes["MouseX"] = 'X'; + m_OriginAxes["MouseY"] = 'Y'; +} + +bool MouseInputHandler::BindOrigin(std::string origin, std::string command, float value) +{ + auto originCode = m_OriginCodes.find(origin); + if (originCode != m_OriginCodes.end()) { + int code = originCode->second; + m_Bindings[code] = std::make_tuple(command, value); + return true; + } + + auto originAxis = m_OriginAxes.find(origin); + if (originAxis != m_OriginAxes.end()) { + char axis = originAxis->second; + float multiplier = 1.f; + // Sensitivity + multiplier *= ResourceManager::Load("Input.ini")->Get("Mouse.Sensitivity", 1.f); + if (axis == 'Y') { + if (ResourceManager::Load("Input.ini")->Get("Mouse.InvertPitch", false)) { + multiplier *= -1.f; + } + } + m_Axes[axis] = std::make_tuple(command, value * multiplier); + return true; + } + + return false; +} + +bool MouseInputHandler::OnMousePress(const Events::MousePress& e) +{ + auto it = m_Bindings.find(e.Button); + if (it == m_Bindings.end()) { + return false; + } + + Events::InputCommand ic; + ic.PlayerID = 0; + std::tie(ic.Command, ic.Value) = it->second; + m_InputProxy->Publish(ic); + + return true; +} + +bool MouseInputHandler::OnMouseRelease(const Events::MouseRelease& e) +{ + auto it = m_Bindings.find(e.Button); + if (it == m_Bindings.end()) { + return false; + } + + Events::InputCommand ic; + ic.PlayerID = 0; + std::tie(ic.Command, std::ignore) = it->second; + ic.Value = 0; + m_InputProxy->Publish(ic); + + return true; +} + +bool MouseInputHandler::OnMouseMove(const Events::MouseMove& e) +{ + if (std::abs(e.DeltaX) > 0) { + auto it = m_Axes.find('X'); + if (it != m_Axes.end()) { + Events::InputCommand ic; + ic.PlayerID = 0; + std::tie(ic.Command, ic.Value) = it->second; + ic.Value *= e.DeltaX; + m_InputProxy->Publish(ic); + } + } + + if (std::abs(e.DeltaY) > 0) { + auto it = m_Axes.find('Y'); + if (it != m_Axes.end()) { + Events::InputCommand ic; + ic.PlayerID = 0; + std::tie(ic.Command, ic.Value) = it->second; + ic.Value *= e.DeltaY; + m_InputProxy->Publish(ic); + } + } + + return true; +} + +bool MouseInputHandler::hasOrigin(std::string origin) +{ + if (m_OriginCodes.find(origin) == m_OriginCodes.end()) { + return false; + } + if (m_OriginAxes.find(origin) == m_OriginAxes.end()) { + return false; + } + return true; +} + diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 5ae9295a..be988529 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -31,6 +31,7 @@ Game::Game(int argc, char* argv[]) m_InputManager = new InputManager(m_Renderer->Window(), m_EventBroker); m_InputProxy = new InputProxy(m_EventBroker); m_InputProxy->AddHandler(); + m_InputProxy->AddHandler(); m_InputProxy->LoadBindings("Input.ini"); // Create the root level GUI frame From 2e7e8d3546f9248ca20b609b043b7727e946883e Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 11 Dec 2015 14:50:14 +0100 Subject: [PATCH 6/8] Clearing raw input events from event broker to encourage use of input commands instead --- src/Game/Game.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index be988529..b7391bac 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -76,6 +76,7 @@ void Game::Tick() m_EventBroker->Swap(); m_InputProxy->Update(dt); m_EventBroker->Swap(); + m_EventBroker->Clear(); m_InputProxy->Process(); m_EventBroker->Swap(); From 03e226ffeaa34d26fee2bd60fa9e6d0081418ab9 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 11 Dec 2015 17:15:33 +0100 Subject: [PATCH 7/8] Refactored InputProxy to gracefully handle cases where multiple origins send the same command --- .../Engine/Input/FirstPersonInputController.h | 48 ++++++++++++++---- include/Engine/Input/InputHandler.h | 3 +- include/Engine/Input/InputProxy.h | 8 ++- include/Engine/Input/KeyboardInputHandler.h | 2 + resources/DefaultInput.ini | 11 +++-- src/Engine/Input/InputProxy.cpp | 49 ++++++++++++------- src/Engine/Input/KeyboardInputHandler.cpp | 27 ++++++---- 7 files changed, 104 insertions(+), 44 deletions(-) diff --git a/include/Engine/Input/FirstPersonInputController.h b/include/Engine/Input/FirstPersonInputController.h index e8b832b5..d8584494 100644 --- a/include/Engine/Input/FirstPersonInputController.h +++ b/include/Engine/Input/FirstPersonInputController.h @@ -3,6 +3,7 @@ #include "../GLM.h" #include "../Core/InputController.h" +#include "../Core/ELockMouse.h" template class FirstPersonInputController : public InputController @@ -11,9 +12,26 @@ public: FirstPersonInputController(EventBroker* eventBroker, unsigned int playerID) : InputController(eventBroker) , m_PlayerID(playerID) - { } + { + EVENT_SUBSCRIBE_MEMBER(m_ELockMouse, &FirstPersonInputController::OnLockMouse); + EVENT_SUBSCRIBE_MEMBER(m_EUnlockMouse, &FirstPersonInputController::OnUnlockMouse); + } const glm::quat Orientation() const { return m_Orientation; } + + void LockMouse() + { + Events::LockMouse e; + m_EventBroker->Publish(e); + m_MouseLocked = true; + } + + void UnlockMouse() + { + Events::UnlockMouse e; + m_EventBroker->Publish(e); + m_MouseLocked = false; + } virtual bool OnCommand(const Events::InputCommand& e) override { @@ -21,20 +39,32 @@ public: return false; } - if (e.Command == "Pitch") { - float val = glm::radians(e.Value); - m_Orientation = m_Orientation * glm::angleAxis(-val, glm::vec3(1, 0, 0)); + if (m_MouseLocked) { + if (e.Command == "Pitch") { + float val = glm::radians(e.Value); + m_Orientation = m_Orientation * glm::angleAxis(-val, glm::vec3(1, 0, 0)); + return true; + } + + if (e.Command == "Yaw") { + float val = glm::radians(e.Value); + m_Orientation = glm::angleAxis(-val, glm::vec3(0, 1, 0)) * m_Orientation; + return true; + } } - if (e.Command == "Yaw") { - float val = glm::radians(e.Value); - m_Orientation = glm::angleAxis(-val, glm::vec3(0, 1, 0)) * m_Orientation; - } + return false; } -private: +protected: const unsigned int m_PlayerID; glm::quat m_Orientation; + bool m_MouseLocked = false; + + EventRelay m_ELockMouse; + bool OnLockMouse(const Events::LockMouse& e) { m_MouseLocked = true; return true; } + EventRelay m_EUnlockMouse; + bool OnUnlockMouse(const Events::UnlockMouse& e) { m_MouseLocked = false; return true; } }; #endif \ No newline at end of file diff --git a/include/Engine/Input/InputHandler.h b/include/Engine/Input/InputHandler.h index c8ecb3af..a9b3c038 100644 --- a/include/Engine/Input/InputHandler.h +++ b/include/Engine/Input/InputHandler.h @@ -13,8 +13,9 @@ public: , m_InputProxy(inputProxy) { } - virtual void Update(double dt) { } virtual bool BindOrigin(std::string origin, std::string command, float value) = 0; + virtual void Update(double dt) { } + virtual float GetCommandValue(std::string command) = 0; protected: EventBroker* m_EventBroker; diff --git a/include/Engine/Input/InputProxy.h b/include/Engine/Input/InputProxy.h index d3ee57d9..43c4940c 100644 --- a/include/Engine/Input/InputProxy.h +++ b/include/Engine/Input/InputProxy.h @@ -20,13 +20,17 @@ public: void Process(); template void AddHandler(); - void Publish(const Events::InputCommand& e); protected: EventBroker* m_EventBroker; std::vector m_Handlers; + std::map> m_CommandHandlers; + // Represents every unique command (has of PlayerID & Command) and all values reported for that command this frame - std::map, std::vector> m_CommandQueue; + //std::map, std::vector> m_CommandQueue; + + std::map m_CurrentCommandValues; + std::map m_LastCommandValues; EventRelay m_EBindOrigin; bool OnBindOrigin(const Events::BindOrigin& e); diff --git a/include/Engine/Input/KeyboardInputHandler.h b/include/Engine/Input/KeyboardInputHandler.h index 53b08f7a..49597fce 100644 --- a/include/Engine/Input/KeyboardInputHandler.h +++ b/include/Engine/Input/KeyboardInputHandler.h @@ -12,10 +12,12 @@ public: KeyboardInputHandler(EventBroker* eventBroker, InputProxy* inputProxy); bool BindOrigin(std::string origin, std::string command, float value) override; + virtual float GetCommandValue(std::string command) override; private: std::unordered_map m_OriginKeyCodes; std::unordered_map> m_KeyBindings; // GLFW_KEY... -> command string & value + std::unordered_map m_CommandValues; EventRelay m_EKeyDown; bool OnKeyDown(const Events::KeyDown& e); diff --git a/resources/DefaultInput.ini b/resources/DefaultInput.ini index b6bbd20f..3be67287 100644 --- a/resources/DefaultInput.ini +++ b/resources/DefaultInput.ini @@ -3,10 +3,13 @@ Sensitivity=0.5 InvertPitch=false [Bindings] -W=Forward -A=Left -S=Back -D=Right +MouseLeft=PrimaryFire +MouseX=Yaw +MouseY=Pitch +W=+Forward +S=-Forward +D=+Right +A=-Right R=Reload Space=Jump LeftControl=Crouch diff --git a/src/Engine/Input/InputProxy.cpp b/src/Engine/Input/InputProxy.cpp index 430cb250..3fe29b7d 100644 --- a/src/Engine/Input/InputProxy.cpp +++ b/src/Engine/Input/InputProxy.cpp @@ -22,7 +22,16 @@ void InputProxy::LoadBindings(std::string file) e.Origin = origin.first; e.Command = origin.second; e.Value = 1.f; - OnBindOrigin(e); + if (!e.Command.empty()) { + char prefix = e.Command.at(0); + if (prefix == '+' || prefix == '-') { + e.Command = e.Command.substr(1); + if (prefix == '-') { + e.Value *= -1.f; + } + } + OnBindOrigin(e); + } } } @@ -37,26 +46,26 @@ void InputProxy::Update(double dt) void InputProxy::Process() { - // Accumulate the input values of all unique commands published by input handlers - for (auto& pair : m_CommandQueue) { - Events::InputCommand e; - e.PlayerID = pair.first.first; - e.Command = pair.first.second; - e.Value = 0; - for (auto& value : pair.second) { - e.Value += value; + for (auto& pair : m_CommandHandlers) { + const std::string& command = pair.first; + auto handlers = pair.second; + m_CurrentCommandValues[command] = 0.f; + for (auto& handler : handlers) { + m_CurrentCommandValues[command] += handler->GetCommandValue(command); } - //e.Value = std::max(-1.f, std::min(e.Value, 1.f)); - m_EventBroker->Publish(e); - LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); - } - m_CommandQueue.clear(); -} -void InputProxy::Publish(const Events::InputCommand& e) -{ - auto key = std::make_pair(e.PlayerID, e.Command); - m_CommandQueue[key].push_back(e.Value); + auto last = m_LastCommandValues.find(command); + float currentValue = m_CurrentCommandValues[command]; + if (last == m_LastCommandValues.end() || last->second != currentValue) { + Events::InputCommand e; + e.PlayerID = -1; + e.Command = command; + e.Value = currentValue; + m_EventBroker->Publish(e); + LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); + m_LastCommandValues[command] = currentValue; + } + } } bool InputProxy::OnBindOrigin(const Events::BindOrigin& e) @@ -65,6 +74,8 @@ bool InputProxy::OnBindOrigin(const Events::BindOrigin& e) for (auto& handler : m_Handlers) { bool result = handler->BindOrigin(e.Origin, e.Command, e.Value); if (result) { + m_CommandHandlers[e.Command].insert(handler); + m_LastCommandValues[e.Command] = 0.f; if (originBound) { LOG_WARNING("Multiple handlers responded to binding input origin \"%s\"!", e.Origin.c_str()); } diff --git a/src/Engine/Input/KeyboardInputHandler.cpp b/src/Engine/Input/KeyboardInputHandler.cpp index 1686d8cd..0a0994aa 100644 --- a/src/Engine/Input/KeyboardInputHandler.cpp +++ b/src/Engine/Input/KeyboardInputHandler.cpp @@ -146,10 +146,10 @@ bool KeyboardInputHandler::OnKeyDown(const Events::KeyDown& e) return false; } - Events::InputCommand ic; - ic.PlayerID = 0; - std::tie(ic.Command, ic.Value) = it->second; - m_InputProxy->Publish(ic); + std::string command; + float value; + std::tie(command, value) = it->second; + m_CommandValues[command] += value; return true; } @@ -161,12 +161,21 @@ bool KeyboardInputHandler::OnKeyUp(const Events::KeyUp& e) return false; } - Events::InputCommand ic; - ic.PlayerID = 0; - std::tie(ic.Command, std::ignore) = it->second; - ic.Value = 0; - m_InputProxy->Publish(ic); + std::string command; + float value; + std::tie(command, value) = it->second; + m_CommandValues[command] -= value; return true; } +float KeyboardInputHandler::GetCommandValue(std::string command) +{ + auto it = m_CommandValues.find(command); + if (it != m_CommandValues.end()) { + return m_CommandValues[command]; + } else { + return 0.f; + } +} + From 30474b70df67aa57e5b6bde07350ada014d7e5eb Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 11 Dec 2015 18:03:48 +0100 Subject: [PATCH 8/8] MouseInputHandler --- include/Engine/Input/InputProxy.h | 3 +- include/Engine/Input/MouseInputHandler.h | 5 ++ .../Rendering/DebugCameraInputController.h | 48 +++++++++++++++++++ src/Engine/Input/InputProxy.cpp | 21 ++++++++ src/Engine/Input/MouseInputHandler.cpp | 34 ++++++++----- src/Engine/Rendering/Renderer.cpp | 31 +++--------- src/Game/Game.cpp | 1 + 7 files changed, 104 insertions(+), 39 deletions(-) create mode 100644 include/Engine/Rendering/DebugCameraInputController.h diff --git a/include/Engine/Input/InputProxy.h b/include/Engine/Input/InputProxy.h index 43c4940c..c7817c22 100644 --- a/include/Engine/Input/InputProxy.h +++ b/include/Engine/Input/InputProxy.h @@ -20,6 +20,7 @@ public: void Process(); template void AddHandler(); + void Publish(const Events::InputCommand& e); protected: EventBroker* m_EventBroker; @@ -27,7 +28,7 @@ protected: std::map> m_CommandHandlers; // Represents every unique command (has of PlayerID & Command) and all values reported for that command this frame - //std::map, std::vector> m_CommandQueue; + std::map, std::vector> m_CommandQueue; std::map m_CurrentCommandValues; std::map m_LastCommandValues; diff --git a/include/Engine/Input/MouseInputHandler.h b/include/Engine/Input/MouseInputHandler.h index 39889755..b9193a2d 100644 --- a/include/Engine/Input/MouseInputHandler.h +++ b/include/Engine/Input/MouseInputHandler.h @@ -13,12 +13,15 @@ public: MouseInputHandler(EventBroker* eventBroker, InputProxy* inputProxy); bool BindOrigin(std::string origin, std::string command, float value) override; + virtual float GetCommandValue(std::string command) override; private: std::unordered_map m_OriginCodes; std::unordered_map m_OriginAxes; std::unordered_map> m_Bindings; // GLFW_MOUSE_BUTTON... -> command string & value std::unordered_map> m_Axes; // Axis -> command string & value + std::unordered_map m_CommandValues; + std::unordered_map m_ContinuousCommandValues; EventRelay m_EMousePress; bool OnMousePress(const Events::MousePress& e); @@ -28,6 +31,8 @@ private: bool OnMouseMove(const Events::MouseMove& e); bool hasOrigin(std::string origin); + + }; #endif diff --git a/include/Engine/Rendering/DebugCameraInputController.h b/include/Engine/Rendering/DebugCameraInputController.h new file mode 100644 index 00000000..551ae0f2 --- /dev/null +++ b/include/Engine/Rendering/DebugCameraInputController.h @@ -0,0 +1,48 @@ +#include "../Input/FirstPersonInputController.h" + +template +class DebugCameraInputController : public FirstPersonInputController +{ +public: + DebugCameraInputController(EventBroker* eventBroker, unsigned int playerID) + : FirstPersonInputController(eventBroker, playerID) + { } + + const glm::vec3 Position() const { return m_Position; } + void SetBaseSpeed(float speed) { m_BaseSpeed = speed; } + + virtual bool OnCommand(const Events::InputCommand& e) override + { + if (e.Command == "PrimaryFire") { + if (e.Value > 0) { + LockMouse(); + } else { + UnlockMouse(); + } + return false; + } + + if (e.Command == "Right") { + float value = std::max(-1.f, std::min(e.Value, 1.f)); + m_Velocity.x = value; + } + if (e.Command == "Forward") { + float value = std::max(-1.f, std::min(e.Value, 1.f)); + m_Velocity.z = -value; + } + + return FirstPersonInputController::OnCommand(e); + } + + void Update(double dt) + { + if (glm::length2(m_Velocity) > 0) { + m_Position += m_Orientation * (glm::normalize(m_Velocity) * m_BaseSpeed); + } + } + +protected: + glm::vec3 m_Position = glm::vec3(0, 0, 0); + glm::vec3 m_Velocity = glm::vec3(0, 0, 0); + float m_BaseSpeed = 0.1f; +}; \ No newline at end of file diff --git a/src/Engine/Input/InputProxy.cpp b/src/Engine/Input/InputProxy.cpp index 3fe29b7d..c3e4d669 100644 --- a/src/Engine/Input/InputProxy.cpp +++ b/src/Engine/Input/InputProxy.cpp @@ -66,6 +66,27 @@ void InputProxy::Process() m_LastCommandValues[command] = currentValue; } } + + // Accumulate the input values of all unique commands published by input handlers + for (auto& pair : m_CommandQueue) { + Events::InputCommand e; + e.PlayerID = pair.first.first; + e.Command = pair.first.second; + e.Value = 0; + for (auto& value : pair.second) { + e.Value += value; + } + //e.Value = std::max(-1.f, std::min(e.Value, 1.f)); + m_EventBroker->Publish(e); + LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); + } + m_CommandQueue.clear(); +} + +void InputProxy::Publish(const Events::InputCommand& e) +{ + auto key = std::make_pair(e.PlayerID, e.Command); + m_CommandQueue[key].push_back(e.Value); } bool InputProxy::OnBindOrigin(const Events::BindOrigin& e) diff --git a/src/Engine/Input/MouseInputHandler.cpp b/src/Engine/Input/MouseInputHandler.cpp index 2e9c8137..5996b43d 100644 --- a/src/Engine/Input/MouseInputHandler.cpp +++ b/src/Engine/Input/MouseInputHandler.cpp @@ -50,6 +50,16 @@ bool MouseInputHandler::BindOrigin(std::string origin, std::string command, floa return false; } +float MouseInputHandler::GetCommandValue(std::string command) +{ + auto it = m_CommandValues.find(command); + if (it != m_CommandValues.end()) { + return m_CommandValues[command]; + } else { + return 0.f; + } +} + bool MouseInputHandler::OnMousePress(const Events::MousePress& e) { auto it = m_Bindings.find(e.Button); @@ -57,10 +67,10 @@ bool MouseInputHandler::OnMousePress(const Events::MousePress& e) return false; } - Events::InputCommand ic; - ic.PlayerID = 0; - std::tie(ic.Command, ic.Value) = it->second; - m_InputProxy->Publish(ic); + std::string command; + float value; + std::tie(command, value) = it->second; + m_CommandValues[command] += value; return true; } @@ -72,11 +82,10 @@ bool MouseInputHandler::OnMouseRelease(const Events::MouseRelease& e) return false; } - Events::InputCommand ic; - ic.PlayerID = 0; - std::tie(ic.Command, std::ignore) = it->second; - ic.Value = 0; - m_InputProxy->Publish(ic); + std::string command; + float value; + std::tie(command, value) = it->second; + m_CommandValues[command] -= value; return true; } @@ -87,7 +96,7 @@ bool MouseInputHandler::OnMouseMove(const Events::MouseMove& e) auto it = m_Axes.find('X'); if (it != m_Axes.end()) { Events::InputCommand ic; - ic.PlayerID = 0; + ic.PlayerID = -1; std::tie(ic.Command, ic.Value) = it->second; ic.Value *= e.DeltaX; m_InputProxy->Publish(ic); @@ -98,7 +107,7 @@ bool MouseInputHandler::OnMouseMove(const Events::MouseMove& e) auto it = m_Axes.find('Y'); if (it != m_Axes.end()) { Events::InputCommand ic; - ic.PlayerID = 0; + ic.PlayerID = -1; std::tie(ic.Command, ic.Value) = it->second; ic.Value *= e.DeltaY; m_InputProxy->Publish(ic); @@ -117,5 +126,4 @@ bool MouseInputHandler::hasOrigin(std::string origin) return false; } return true; -} - +} \ No newline at end of file diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index 6f4235d2..3bc1d9da 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -1,4 +1,5 @@ #include "Rendering/Renderer.h" +#include "Rendering/DebugCameraInputController.h" void Renderer::Initialize() { @@ -83,6 +84,8 @@ void Renderer::InitializeShaders() void Renderer::InputUpdate(double dt) { + static DebugCameraInputController firstPersonInputController(m_EventBroker, -1); + glm::vec3 m_Position = m_Camera->Position(); if (glfwGetKey(m_Window, GLFW_KEY_O) == GLFW_PRESS) { @@ -112,37 +115,15 @@ void Renderer::InputUpdate(double dt) m_CameraMoveSpeed = 0.5f; } - - static double mousePosX, mousePosY; - glfwGetCursorPos(m_Window, &mousePosX, &mousePosY); - - if (glfwGetKey(m_Window, GLFW_KEY_SPACE) == GLFW_PRESS) { - - - double deltaX, deltaY; - deltaX = mousePosX - (float)Resolution().Width / 2; - deltaY = mousePosY - (float)Resolution().Height / 2; - - float rotationY = -deltaY / 300.f; - float rotationX = -deltaX / 300.f; - glm::quat orientation = m_Camera->Orientation(); - - - orientation = orientation * glm::angleAxis(rotationY, glm::vec3(1, 0, 0)); - orientation = glm::angleAxis(rotationX, glm::vec3(0, 1, 0)) * orientation; - - m_Camera->SetOrientation(orientation); - - glfwSetCursorPos(m_Window, Resolution().Width / 2, Resolution().Height / 2); - } - m_Camera->SetPosition(m_Position); + firstPersonInputController.Update(dt); + m_Camera->SetOrientation(firstPersonInputController.Orientation()); + m_Camera->SetPosition(firstPersonInputController.Position()); } void Renderer::Update(double dt) { m_EventBroker->Process(); InputUpdate(dt); - } void Renderer::Draw(RenderQueueCollection& rq) diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index b7391bac..026857d2 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -71,6 +71,7 @@ void Game::Tick() m_LastTime = currentTime; // Handle input in a weird looking but responsive way + m_EventBroker->Process(); m_EventBroker->Swap(); m_InputManager->Update(dt); m_EventBroker->Swap();