Basic input binding and command publishing system
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef Events_BindKey_h__
|
||||||
|
#define Events_BindKey_h__
|
||||||
|
|
||||||
|
#include "EventBroker.h"
|
||||||
|
|
||||||
|
namespace Events
|
||||||
|
{
|
||||||
|
|
||||||
|
struct BindKey : Event
|
||||||
|
{
|
||||||
|
int KeyCode;
|
||||||
|
std::string Command;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // Events_BindKey_h__
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef Events_BindMouseButton_h__
|
||||||
|
#define Events_BindMouseButton_h__
|
||||||
|
|
||||||
|
#include "EventBroker.h"
|
||||||
|
|
||||||
|
namespace Events
|
||||||
|
{
|
||||||
|
|
||||||
|
struct BindMouseButton : Event
|
||||||
|
{
|
||||||
|
int Button;
|
||||||
|
std::string Command;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // Events_BindMouseButton_h__
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef Events_InputCommand_h__
|
||||||
|
#define Events_InputCommand_h__
|
||||||
|
|
||||||
|
#include "EventBroker.h"
|
||||||
|
|
||||||
|
namespace Events
|
||||||
|
{
|
||||||
|
|
||||||
|
struct InputCommand : Event
|
||||||
|
{
|
||||||
|
unsigned int PlayerID;
|
||||||
|
std::string Command;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // Events_InputCommand_h__
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef Events_InputCommandValue_h__
|
||||||
|
#define Events_InputCommandValue_h__
|
||||||
|
|
||||||
|
#include "EventBroker.h"
|
||||||
|
#include "Events/InputCommand.h"
|
||||||
|
|
||||||
|
namespace Events
|
||||||
|
{
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct InputCommandValue : public InputCommand
|
||||||
|
{
|
||||||
|
T Value;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // Events_InputCommandValue_h__
|
||||||
@@ -1,12 +1,6 @@
|
|||||||
#include "PrecompiledHeader.h"
|
#include "PrecompiledHeader.h"
|
||||||
#include "InputManager.h"
|
#include "InputManager.h"
|
||||||
|
|
||||||
InputManager::InputManager(GLFWwindow* window, std::shared_ptr<EventBroker> eventBroker) : m_GLFWWindow(window)
|
|
||||||
, m_EventBroker(eventBroker)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void InputManager::Update(double dt)
|
void InputManager::Update(double dt)
|
||||||
{
|
{
|
||||||
m_LastKeyState = m_CurrentKeyState;
|
m_LastKeyState = m_CurrentKeyState;
|
||||||
@@ -43,7 +37,7 @@ void InputManager::Update(double dt)
|
|||||||
if (m_CurrentMouseState[i] != m_LastMouseState[i])
|
if (m_CurrentMouseState[i] != m_LastMouseState[i])
|
||||||
{
|
{
|
||||||
// Publish mouse button events
|
// Publish mouse button events
|
||||||
if (m_CurrentKeyState[i])
|
if (m_CurrentMouseState[i])
|
||||||
{
|
{
|
||||||
Events::MousePress e;
|
Events::MousePress e;
|
||||||
e.Button = i;
|
e.Button = i;
|
||||||
|
|||||||
+10
-1
@@ -13,7 +13,16 @@
|
|||||||
class InputManager
|
class InputManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
InputManager(GLFWwindow* window, std::shared_ptr<EventBroker> eventBroker);
|
InputManager(GLFWwindow* window, std::shared_ptr<EventBroker> eventBroker)
|
||||||
|
: m_GLFWWindow(window)
|
||||||
|
, m_EventBroker(eventBroker)
|
||||||
|
, m_CurrentKeyState()
|
||||||
|
, m_LastKeyState()
|
||||||
|
, m_CurrentMouseState()
|
||||||
|
, m_LastMouseState()
|
||||||
|
, m_CurrentMouseX(0), m_CurrentMouseY(0)
|
||||||
|
, m_LastMouseX(0), m_LastMouseY(0)
|
||||||
|
, m_CurrentMouseDeltaX(0), m_CurrentMouseDeltaY(0) { }
|
||||||
|
|
||||||
void Update(double dt);
|
void Update(double dt);
|
||||||
|
|
||||||
|
|||||||
+109
-1
@@ -12,6 +12,30 @@ void Systems::InputSystem::Initialize()
|
|||||||
// Subscribe to events
|
// Subscribe to events
|
||||||
m_EKeyDown = decltype(m_EKeyDown)(std::bind(&Systems::InputSystem::OnKeyDown, this, std::placeholders::_1));
|
m_EKeyDown = decltype(m_EKeyDown)(std::bind(&Systems::InputSystem::OnKeyDown, this, std::placeholders::_1));
|
||||||
EventBroker->Subscribe(m_EKeyDown);
|
EventBroker->Subscribe(m_EKeyDown);
|
||||||
|
m_EKeyUp = decltype(m_EKeyUp)(std::bind(&Systems::InputSystem::OnKeyUp, this, std::placeholders::_1));
|
||||||
|
EventBroker->Subscribe(m_EKeyUp);
|
||||||
|
m_EMousePress = decltype(m_EMousePress)(std::bind(&Systems::InputSystem::OnMousePress, this, std::placeholders::_1));
|
||||||
|
EventBroker->Subscribe(m_EMousePress);
|
||||||
|
m_EMouseRelease = decltype(m_EMouseRelease)(std::bind(&Systems::InputSystem::OnMouseRelease, this, std::placeholders::_1));
|
||||||
|
EventBroker->Subscribe(m_EMouseRelease);
|
||||||
|
m_EBindKey = decltype(m_EBindKey)(std::bind(&Systems::InputSystem::OnBindKey, this, std::placeholders::_1));
|
||||||
|
EventBroker->Subscribe(m_EBindKey);
|
||||||
|
m_EBindMouseButton = decltype(m_EBindMouseButton)(std::bind(&Systems::InputSystem::OnBindMouseButton, this, std::placeholders::_1));
|
||||||
|
EventBroker->Subscribe(m_EBindMouseButton);
|
||||||
|
|
||||||
|
{
|
||||||
|
Events::BindKey e;
|
||||||
|
e.KeyCode = GLFW_KEY_W;
|
||||||
|
e.Command = "+forward";
|
||||||
|
EventBroker->Publish(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
Events::BindMouseButton e;
|
||||||
|
e.Button = GLFW_MOUSE_BUTTON_1;
|
||||||
|
e.Command = "+attack";
|
||||||
|
EventBroker->Publish(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Systems::InputSystem::Update(double dt)
|
void Systems::InputSystem::Update(double dt)
|
||||||
@@ -37,5 +61,89 @@ void Systems::InputSystem::Update(double dt)
|
|||||||
|
|
||||||
bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event)
|
bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event)
|
||||||
{
|
{
|
||||||
|
auto bindingIt = m_KeyBindings.find(event.KeyCode);
|
||||||
|
if (bindingIt != m_KeyBindings.end())
|
||||||
|
{
|
||||||
|
PublishCommand(0, bindingIt->second, false);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Systems::InputSystem::OnKeyUp(const Events::KeyUp &event)
|
||||||
|
{
|
||||||
|
auto bindingIt = m_KeyBindings.find(event.KeyCode);
|
||||||
|
if (bindingIt != m_KeyBindings.end())
|
||||||
|
{
|
||||||
|
PublishCommand(0, bindingIt->second, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Systems::InputSystem::OnMousePress(const Events::MousePress &event)
|
||||||
|
{
|
||||||
|
auto bindingIt = m_MouseButtonBindings.find(event.Button);
|
||||||
|
if (bindingIt != m_MouseButtonBindings.end())
|
||||||
|
{
|
||||||
|
PublishCommand(0, bindingIt->second, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Systems::InputSystem::OnMouseRelease(const Events::MouseRelease &event)
|
||||||
|
{
|
||||||
|
auto bindingIt = m_MouseButtonBindings.find(event.Button);
|
||||||
|
if (bindingIt != m_MouseButtonBindings.end())
|
||||||
|
{
|
||||||
|
PublishCommand(0, bindingIt->second, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Systems::InputSystem::OnBindKey(const Events::BindKey &event)
|
||||||
|
{
|
||||||
|
if (event.Command.empty())
|
||||||
|
{
|
||||||
|
m_KeyBindings.erase(event.KeyCode);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_KeyBindings[event.KeyCode] = event.Command;
|
||||||
|
LOG_DEBUG("Input: Bound key %c to %s", (char)event.KeyCode, event.Command.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Systems::InputSystem::OnBindMouseButton(const Events::BindMouseButton &event)
|
||||||
|
{
|
||||||
|
if (event.Command.empty())
|
||||||
|
{
|
||||||
|
m_MouseButtonBindings.erase(event.Button);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_MouseButtonBindings[event.Button] = event.Command;
|
||||||
|
LOG_DEBUG("Input: Bound mouse button %i to %s", event.Button, event.Command.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Systems::InputSystem::PublishCommand(int playerID, std::string command, bool release /*= false*/)
|
||||||
|
{
|
||||||
|
if (release && command.at(0) == '+')
|
||||||
|
{
|
||||||
|
command[0] = '-';
|
||||||
|
}
|
||||||
|
|
||||||
|
Events::InputCommand e;
|
||||||
|
e.PlayerID = playerID;
|
||||||
|
e.Command = command;
|
||||||
|
EventBroker->Publish(e);
|
||||||
|
|
||||||
|
LOG_DEBUG("Input: Published command %s for player %i", e.Command.c_str(), playerID);
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,12 +2,19 @@
|
|||||||
#define InputSystem_h__
|
#define InputSystem_h__
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
#include "System.h"
|
#include "System.h"
|
||||||
#include "Renderer.h"
|
#include "Renderer.h"
|
||||||
#include "Components/Input.h"
|
#include "Components/Input.h"
|
||||||
#include "Events/KeyUp.h"
|
#include "Events/KeyUp.h"
|
||||||
#include "Events/KeyDown.h"
|
#include "Events/KeyDown.h"
|
||||||
|
#include "Events/MousePress.h"
|
||||||
|
#include "Events/MouseRelease.h"
|
||||||
|
#include "Events/BindKey.h"
|
||||||
|
#include "Events/BindMouseButton.h"
|
||||||
|
#include "Events/InputCommand.h"
|
||||||
|
#include "Events/InputCommandValue.h"
|
||||||
|
|
||||||
namespace Systems
|
namespace Systems
|
||||||
{
|
{
|
||||||
@@ -27,9 +34,26 @@ public:
|
|||||||
private:
|
private:
|
||||||
std::shared_ptr<Renderer> m_Renderer;
|
std::shared_ptr<Renderer> m_Renderer;
|
||||||
|
|
||||||
// Events
|
// Input binding tables
|
||||||
|
std::unordered_map<int, std::string> m_KeyBindings; // GLFW_KEY... -> command string
|
||||||
|
std::unordered_map<int, std::string> m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string
|
||||||
|
|
||||||
|
// Input events
|
||||||
EventRelay<Events::KeyDown> m_EKeyDown;
|
EventRelay<Events::KeyDown> m_EKeyDown;
|
||||||
bool OnKeyDown(const Events::KeyDown &event);
|
bool OnKeyDown(const Events::KeyDown &event);
|
||||||
|
EventRelay<Events::KeyUp> m_EKeyUp;
|
||||||
|
bool OnKeyUp(const Events::KeyUp &event);
|
||||||
|
EventRelay<Events::MousePress> m_EMousePress;
|
||||||
|
bool OnMousePress(const Events::MousePress &event);
|
||||||
|
EventRelay<Events::MouseRelease> m_EMouseRelease;
|
||||||
|
bool OnMouseRelease(const Events::MouseRelease &event);
|
||||||
|
// Input binding events
|
||||||
|
EventRelay<Events::BindKey> m_EBindKey;
|
||||||
|
bool OnBindKey(const Events::BindKey &event);
|
||||||
|
EventRelay<Events::BindMouseButton> m_EBindMouseButton;
|
||||||
|
bool OnBindMouseButton(const Events::BindMouseButton &event);
|
||||||
|
|
||||||
|
void PublishCommand(int playerID, std::string command, bool release = false);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -139,6 +139,10 @@
|
|||||||
<ClInclude Include="..\..\src\CubemapTexture.h" />
|
<ClInclude Include="..\..\src\CubemapTexture.h" />
|
||||||
<ClInclude Include="..\..\src\Engine.h" />
|
<ClInclude Include="..\..\src\Engine.h" />
|
||||||
<ClInclude Include="..\..\src\Entity.h" />
|
<ClInclude Include="..\..\src\Entity.h" />
|
||||||
|
<ClInclude Include="..\..\src\Events\BindKey.h" />
|
||||||
|
<ClInclude Include="..\..\src\Events\BindMouseButton.h" />
|
||||||
|
<ClInclude Include="..\..\src\Events\InputCommand.h" />
|
||||||
|
<ClInclude Include="..\..\src\Events\InputCommandValue.h" />
|
||||||
<ClInclude Include="..\..\src\Events\KeyDown.h" />
|
<ClInclude Include="..\..\src\Events\KeyDown.h" />
|
||||||
<ClInclude Include="..\..\src\Events\KeyUp.h" />
|
<ClInclude Include="..\..\src\Events\KeyUp.h" />
|
||||||
<ClInclude Include="..\..\src\Events\MouseMove.h" />
|
<ClInclude Include="..\..\src\Events\MouseMove.h" />
|
||||||
|
|||||||
@@ -51,7 +51,9 @@
|
|||||||
<ClCompile Include="..\..\src\ResourceManager.cpp" />
|
<ClCompile Include="..\..\src\ResourceManager.cpp" />
|
||||||
<ClCompile Include="..\..\src\Sound.cpp" />
|
<ClCompile Include="..\..\src\Sound.cpp" />
|
||||||
<ClCompile Include="..\..\src\EventBroker.cpp" />
|
<ClCompile Include="..\..\src\EventBroker.cpp" />
|
||||||
<ClCompile Include="..\..\src\InputManager.cpp" />
|
<ClCompile Include="..\..\src\InputManager.cpp">
|
||||||
|
<Filter>Input</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Filter Include="Util">
|
<Filter Include="Util">
|
||||||
@@ -253,6 +255,18 @@
|
|||||||
<ClInclude Include="..\..\src\Events\MouseMove.h">
|
<ClInclude Include="..\..\src\Events\MouseMove.h">
|
||||||
<Filter>Input\Events</Filter>
|
<Filter>Input\Events</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\src\Events\BindKey.h">
|
||||||
|
<Filter>Input\Events</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\src\Events\BindMouseButton.h">
|
||||||
|
<Filter>Input\Events</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\src\Events\InputCommandValue.h">
|
||||||
|
<Filter>Input\Events</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\src\Events\InputCommand.h">
|
||||||
|
<Filter>Input\Events</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="..\..\src\Shaders\AABB.frag.glsl">
|
<None Include="..\..\src\Shaders\AABB.frag.glsl">
|
||||||
|
|||||||
Reference in New Issue
Block a user