Basic input binding and command publishing system
This commit is contained in:
+109
-1
@@ -12,6 +12,30 @@ void Systems::InputSystem::Initialize()
|
||||
// Subscribe to events
|
||||
m_EKeyDown = decltype(m_EKeyDown)(std::bind(&Systems::InputSystem::OnKeyDown, this, std::placeholders::_1));
|
||||
EventBroker->Subscribe(m_EKeyDown);
|
||||
m_EKeyUp = decltype(m_EKeyUp)(std::bind(&Systems::InputSystem::OnKeyUp, this, std::placeholders::_1));
|
||||
EventBroker->Subscribe(m_EKeyUp);
|
||||
m_EMousePress = decltype(m_EMousePress)(std::bind(&Systems::InputSystem::OnMousePress, this, std::placeholders::_1));
|
||||
EventBroker->Subscribe(m_EMousePress);
|
||||
m_EMouseRelease = decltype(m_EMouseRelease)(std::bind(&Systems::InputSystem::OnMouseRelease, this, std::placeholders::_1));
|
||||
EventBroker->Subscribe(m_EMouseRelease);
|
||||
m_EBindKey = decltype(m_EBindKey)(std::bind(&Systems::InputSystem::OnBindKey, this, std::placeholders::_1));
|
||||
EventBroker->Subscribe(m_EBindKey);
|
||||
m_EBindMouseButton = decltype(m_EBindMouseButton)(std::bind(&Systems::InputSystem::OnBindMouseButton, this, std::placeholders::_1));
|
||||
EventBroker->Subscribe(m_EBindMouseButton);
|
||||
|
||||
{
|
||||
Events::BindKey e;
|
||||
e.KeyCode = GLFW_KEY_W;
|
||||
e.Command = "+forward";
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
{
|
||||
Events::BindMouseButton e;
|
||||
e.Button = GLFW_MOUSE_BUTTON_1;
|
||||
e.Command = "+attack";
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
}
|
||||
|
||||
void Systems::InputSystem::Update(double dt)
|
||||
@@ -37,5 +61,89 @@ void Systems::InputSystem::Update(double dt)
|
||||
|
||||
bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event)
|
||||
{
|
||||
auto bindingIt = m_KeyBindings.find(event.KeyCode);
|
||||
if (bindingIt != m_KeyBindings.end())
|
||||
{
|
||||
PublishCommand(0, bindingIt->second, false);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
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__
|
||||
|
||||
#include <array>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "System.h"
|
||||
#include "Renderer.h"
|
||||
#include "Components/Input.h"
|
||||
#include "Events/KeyUp.h"
|
||||
#include "Events/KeyDown.h"
|
||||
#include "Events/MousePress.h"
|
||||
#include "Events/MouseRelease.h"
|
||||
#include "Events/BindKey.h"
|
||||
#include "Events/BindMouseButton.h"
|
||||
#include "Events/InputCommand.h"
|
||||
#include "Events/InputCommandValue.h"
|
||||
|
||||
namespace Systems
|
||||
{
|
||||
@@ -27,9 +34,26 @@ public:
|
||||
private:
|
||||
std::shared_ptr<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;
|
||||
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);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user