WIP InputController for easier command handling in systems

This commit is contained in:
2014-05-06 14:33:02 +02:00
parent 0e068be77b
commit 8c8edd0e8e
10 changed files with 77 additions and 52 deletions
+4
View File
@@ -6,6 +6,10 @@
#include <unordered_map>
#include <list>
#define EVENT_SUBSCRIBE_MEMBER(relay, handler) \
relay = decltype(relay)(std::bind(handler, this, std::placeholders::_1)); \
EventBroker->Subscribe(relay);
struct Event
{
protected:
+3
View File
@@ -1,6 +1,8 @@
#ifndef Events_InputCommand_h__
#define Events_InputCommand_h__
#include <boost/any.hpp>
#include "EventBroker.h"
namespace Events
@@ -10,6 +12,7 @@ struct InputCommand : Event
{
unsigned int PlayerID;
std::string Command;
boost::any Value;
};
}
-18
View File
@@ -1,18 +0,0 @@
#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__
+27 -3
View File
@@ -8,6 +8,14 @@ void GameWorld::Initialize()
m_ResourceManager.Preload("Model", "Models/Placeholders/PhysicsTest/Plane.obj");
m_ResourceManager.Preload("Model", "Models/Placeholders/PhysicsTest/ArrowCube.obj");
BindKey(GLFW_KEY_W, "+forward");
BindKey(GLFW_KEY_S, "+backward");
BindKey(GLFW_KEY_A, "+left");
BindKey(GLFW_KEY_D, "+right");
BindMouseButton(GLFW_MOUSE_BUTTON_1, "+attack");
BindMouseButton(GLFW_MOUSE_BUTTON_2, "+attack2");
BindMouseButton(GLFW_MOUSE_BUTTON_3, "+attack3");
RegisterComponents();
{
@@ -42,7 +50,7 @@ void GameWorld::Initialize()
CommitEntity(ground);
}
{
/*{
auto TankTest = CreateEntity();
auto transform = AddComponent<Components::Transform>(TankTest, "Transform");
transform->Position = glm::vec3(1.5f, 0.7f, 5.f);
@@ -85,7 +93,7 @@ void GameWorld::Initialize()
auto physics = AddComponent<Components::Physics>(ball, "Physics");
physics->Mass = 1;
CommitEntity(ball);
}
}*/
{
auto entity = CreateEntity();
@@ -138,4 +146,20 @@ void GameWorld::AddSystems()
AddSystem("SoundSystem");
AddSystem("PhysicsSystem");
AddSystem("RenderSystem");
}
}
void GameWorld::BindKey(int keyCode, std::string command)
{
Events::BindKey e;
e.KeyCode = keyCode;
e.Command = command;
m_EventBroker->Publish(e);
}
void GameWorld::BindMouseButton(int button, std::string command)
{
Events::BindMouseButton e;
e.Button = button;
e.Command = command;
m_EventBroker->Publish(e);
}
+3
View File
@@ -47,6 +47,9 @@ public:
private:
std::shared_ptr<Renderer> m_Renderer;
void BindKey(int keyCode, std::string command);
void BindMouseButton(int button, std::string command);
};
#endif // GameWorld_h__
+30
View File
@@ -0,0 +1,30 @@
#ifndef InputController_h__
#define InputController_h__
#include <memory>
#include "EventBroker.h"
#include "Events/InputCommand.h"
class InputController
{
public:
InputController(std::shared_ptr<::EventBroker> eventBroker)
: EventBroker(EventBroker) { Initialize(); }
virtual void Initialize()
{
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &InputController::OnCommand)
}
virtual bool OnCommand(const Events::InputCommand &event) { }
protected:
std::shared_ptr<::EventBroker> EventBroker;
private:
EventRelay<Events::InputCommand> m_EInputCommand;
};
#endif // InputController_h__
+6 -26
View File
@@ -10,32 +10,12 @@ void Systems::InputSystem::RegisterComponents(ComponentFactory* cf)
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);
}
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_EBindKey, &Systems::InputSystem::OnBindKey)
EVENT_SUBSCRIBE_MEMBER(m_EBindMouseButton, &Systems::InputSystem::OnBindMouseButton)
}
void Systems::InputSystem::Update(double dt)
-1
View File
@@ -14,7 +14,6 @@
#include "Events/BindKey.h"
#include "Events/BindMouseButton.h"
#include "Events/InputCommand.h"
#include "Events/InputCommandValue.h"
namespace Systems
{
+1 -1
View File
@@ -142,7 +142,6 @@
<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\KeyUp.h" />
<ClInclude Include="..\..\src\Events\MouseMove.h" />
@@ -153,6 +152,7 @@
<ClInclude Include="..\..\src\GameWorld.h" />
<ClInclude Include="..\..\src\GUI\Frame.h" />
<ClInclude Include="..\..\src\EventBroker.h" />
<ClInclude Include="..\..\src\InputController.h" />
<ClInclude Include="..\..\src\InputManager.h" />
<ClInclude Include="..\..\src\Model.h" />
<ClInclude Include="..\..\src\OBJ.h" />
@@ -261,12 +261,12 @@
<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>
<ClInclude Include="..\..\src\InputController.h">
<Filter>Input</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\Shaders\AABB.frag.glsl">