diff --git a/deps b/deps index 1b478d31..9861acd7 160000 --- a/deps +++ b/deps @@ -1 +1 @@ -Subproject commit 1b478d3159f12273059a684ee8e187f4a25c89f0 +Subproject commit 9861acd762c66729c6d27be6ae2eddf69f06232a diff --git a/include/Engine/Input/SteamControllerInputHandler.h b/include/Engine/Input/SteamControllerInputHandler.h new file mode 100644 index 00000000..f9da189e --- /dev/null +++ b/include/Engine/Input/SteamControllerInputHandler.h @@ -0,0 +1,98 @@ +#include +#include "InputProxy.h" + +class SteamControllerInputHandler : public InputHandler +{ +public: + SteamControllerInputHandler(EventBroker* eventBroker, InputProxy* inputProxy) + : InputHandler(eventBroker, inputProxy) + { + SteamController()->Init(); + } + + ~SteamControllerInputHandler() + { + SteamController()->Shutdown(); + } + + void Update(double dt) override + { + std::array controllers; + int numControllers = SteamController()->GetConnectedControllers(controllers.data()); + for (int i = 0; i < numControllers; i++) { + auto controllerHandle = controllers.at(i); + auto actionSetHandle = SteamController()->GetActionSetHandle("InGameControls"); + //SteamController()->ShowBindingPanel(controllerHandle); + SteamController()->ActivateActionSet(controllerHandle, actionSetHandle); + for (auto& command : m_Commands) { + Events::InputCommand ic; + ic.PlayerID = i + 1; + ic.Command = command.first; + + auto digitalActionHandle = m_DigitalActionHandles.at(ic.Command); + auto handle = SteamController()->GetDigitalActionHandle("DebugReload"); + auto data = SteamController()->GetDigitalActionData(controllerHandle, handle); + LOG_DEBUG("Controller %i, active %i, value %i", i, data.bActive, data.bState); + if (data.bState) { + ic.Value = command.second; + } else { + ic.Value = 0.f; + } + //m_InputProxy->Publish(ic); + } + } + + } + + bool BindOrigin(std::string origin, std::string command, float value) override + { + if (origin != "SteamController") { + return false; + } + + m_Commands[command] = value; + m_DigitalActionHandles[command] = SteamController()->GetDigitalActionHandle(command.c_str()); + return true; + } + +private: + std::map m_Commands; + std::map m_DigitalActionHandles; + + //std::unordered_map> m_CommandKeyboardValues; // command string -> keyboard key value for command + 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; + } + +}; diff --git a/include/Game/Game.h b/include/Game/Game.h index f5eaced3..727d8b36 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/SteamControllerInputHandler.h" #include "Core/EKeyDown.h" #include "Core/EntityXMLFile.h" #include "Core/SystemPipeline.h" diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index ab66a73a..d7482925 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -2,6 +2,8 @@ Game::Game(int argc, char* argv[]) { + bool steamResult = SteamAPI_Init(); + ResourceManager::RegisterType("ConfigFile"); ResourceManager::RegisterType("Model"); ResourceManager::RegisterType("Texture"); @@ -66,6 +68,8 @@ void Game::Tick() double dt = currentTime - m_LastTime; m_LastTime = currentTime; + SteamAPI_RunCallbacks(); + // Handle input in a weird looking but responsive way m_EventBroker->Swap(); m_InputManager->Update(dt); @@ -108,6 +112,28 @@ bool Game::testOnKeyUp(const Events::KeyUp& e) void Game::testIntialize() { EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Game::testOnKeyUp); + + { + Events::BindOrigin e; + e.Origin = "R"; + e.Command = "DebugReload"; + e.Value = 1.f; + m_EventBroker->Publish(e); + } + { + Events::BindOrigin e; + e.Origin = "SteamController"; + e.Command = "DebugReload"; + e.Value = 1.f; + m_EventBroker->Publish(e); + } + { + Events::BindOrigin e; + e.Origin = "X"; + e.Command = "DebugReload"; + e.Value = 1.f; + m_EventBroker->Publish(e); + } } void Game::testTick(double dt)