Beautifully broken

This commit is contained in:
2014-05-22 22:04:54 +02:00
parent 609687d6b4
commit 4102756e33
14 changed files with 135 additions and 48 deletions
+1
View File
@@ -38,6 +38,7 @@ public:
m_InputManager->Update(dt);
m_World->Update(dt);
m_Renderer->Draw(dt);
m_EventBroker->Clear();
glfwPollEvents();
}
+46 -7
View File
@@ -1,5 +1,6 @@
#include "PrecompiledHeader.h"
#include "EventBroker.h"
#include "Events/BindKey.h"
BaseEventRelay::~BaseEventRelay()
{
@@ -11,19 +12,57 @@ BaseEventRelay::~BaseEventRelay()
void EventBroker::Unsubscribe(BaseEventRelay &relay) // ?
{
auto itpair = m_Subscribers.equal_range(relay.m_TypeName);
/*auto itpair = m_Subscribers.equal_range(relay.m_EventTypeName);
for (auto it = itpair.first; it != itpair.second; ++it)
{
if (it->second == &relay)
{
m_Subscribers.erase(it);
break;
}
if (it->second == &relay)
{
m_Subscribers.erase(it);
break;
}
}*/
}
void EventBroker::Subscribe(BaseEventRelay &relay)
{
relay.m_Broker = this;
m_Subscribers.insert(std::make_pair(relay.m_TypeName, &relay));
m_ContextSubscribers[relay.m_ContextTypeName][relay.m_EventTypeName] = &relay;
}
int EventBroker::Process(std::string contextTypeName)
{
auto it = m_ContextSubscribers.find(contextTypeName);
if (it == m_ContextSubscribers.end())
return 0;
int eventsProcessed = 0;
EventRelays_t &relays = it->second;
for (auto &pair : *m_EventQueueRead)
{
std::string &eventTypeName = pair.first;
std::shared_ptr<Event> event = pair.second;
/*if (eventTypeName == "struct Events::BindKey")
{
auto bindKey = static_cast<Events::BindKey*>(event.get());
LOG_DEBUG("HsssEJ");
}*/
auto it2 = relays.find(eventTypeName);
if (it2 == relays.end())
continue;
auto relay = it2->second;
relay->Receive(event);
eventsProcessed++;
}
return eventsProcessed;
}
void EventBroker::Clear()
{
std::swap(m_EventQueueRead, m_EventQueueWrite);
m_EventQueueWrite->clear();
}
+58 -16
View File
@@ -23,19 +23,22 @@ class BaseEventRelay
friend class EventBroker;
protected:
BaseEventRelay(std::string typeName)
: m_TypeName(typeName), m_Broker(nullptr) { }
BaseEventRelay(std::string contextTypeName, std::string eventTypeName)
: m_ContextTypeName(contextTypeName)
, m_EventTypeName(eventTypeName)
, m_Broker(nullptr) { }
~BaseEventRelay();
public:
virtual bool Receive(const Event &event) = 0;
virtual bool Receive(std::shared_ptr<Event> event) = 0;
protected:
std::string m_TypeName;
std::string m_ContextTypeName;
std::string m_EventTypeName;
EventBroker* m_Broker;
};
template <typename EventType>
template <typename ContextType, typename EventType>
class EventRelay : public BaseEventRelay
{
public:
@@ -43,24 +46,24 @@ public:
EventRelay()
: m_Callback(nullptr)
, BaseEventRelay(typeid(EventType).name()) { }
, BaseEventRelay(typeid(ContextType).name(), typeid(EventType).name()) { }
EventRelay(CallbackType callback)
: m_Callback(callback)
, BaseEventRelay(typeid(EventType).name()) { }
, BaseEventRelay(typeid(ContextType).name(), typeid(EventType).name()) { }
protected:
bool Receive(const Event &event) override;
bool Receive(std::shared_ptr<Event> event) override;
private:
CallbackType m_Callback;
};
template <typename EventType>
bool EventRelay<EventType>::Receive(const Event &event)
template <typename ContextType, typename EventType>
bool EventRelay<ContextType, EventType>::Receive(std::shared_ptr<Event> event)
{
if (m_Callback != nullptr)
{
return m_Callback(static_cast<const EventType&>(event));
return m_Callback(static_cast<const EventType&>(*event.get()));
}
else
{
@@ -70,27 +73,66 @@ bool EventRelay<EventType>::Receive(const Event &event)
class EventBroker
{
template <typename EventType> friend class EventRelay;
template <typename ContextType, typename EventType> friend class EventRelay;
public:
EventBroker()
{
m_EventQueueRead = std::make_shared<EventQueue_t>();
m_EventQueueWrite = std::make_shared<EventQueue_t>();
}
void Subscribe(BaseEventRelay &relay);
template <typename EventType>
void Publish(const EventType &event);
void Subscribe(BaseEventRelay &relay);
// Process all events no matter the context.
/*void Process()
{
}*/
/*
Process all events in a given context.
Returns: Number of events processed
*/
template <typename ContextType>
int Process();
int Process(std::string contextTypeName);
void Clear();
void Unsubscribe(BaseEventRelay &relay);
private:
std::unordered_multimap<std::string, BaseEventRelay*> m_Subscribers;
typedef std::string ContextTypeName_t; // typeid(ContextType).name()
typedef std::string EventTypeName_t; // typeid(EventType).name()
typedef std::unordered_map<EventTypeName_t, BaseEventRelay*> EventRelays_t;
typedef std::unordered_map<ContextTypeName_t, EventRelays_t> ContextSubscribers_t;
ContextSubscribers_t m_ContextSubscribers;
typedef std::list<std::pair<EventTypeName_t, std::shared_ptr<Event>>> EventQueue_t;
std::shared_ptr<EventQueue_t> m_EventQueueRead;
std::shared_ptr<EventQueue_t> m_EventQueueWrite;
};
template <typename EventType>
void EventBroker::Publish(const EventType &event)
{
auto itpair = m_Subscribers.equal_range(typeid(EventType).name());
/*auto itpair = m_Subscribers.equal_range(typeid(EventType).name());
for (auto it = itpair.first; it != itpair.second; ++it)
{
it->second->Receive(event);
}
}*/
m_EventQueueWrite->push_back(std::make_pair(typeid(EventType).name(), std::shared_ptr<Event>(new EventType(event))));
}
template <typename ContextType>
int EventBroker::Process()
{
const std::string contextTypeName = typeid(ContextType).name();
return Process(contextTypeName);
}
#endif // MessageRelay_h__
+3 -2
View File
@@ -7,6 +7,7 @@
#include "Events/InputCommand.h"
#include "Events/MouseMove.h"
template <typename EventContext>
class InputController
{
public:
@@ -26,8 +27,8 @@ protected:
std::shared_ptr<::EventBroker> EventBroker;
private:
EventRelay<Events::InputCommand> m_EInputCommand;
EventRelay<Events::MouseMove> m_EMouseMove;
EventRelay<EventContext, Events::InputCommand> m_EInputCommand;
EventRelay<EventContext, Events::MouseMove> m_EMouseMove;
};
#endif // InputController_h__
+2
View File
@@ -13,6 +13,8 @@ void InputManager::Initialize()
void InputManager::Update(double dt)
{
EventBroker->Process<InputManager>();
m_LastKeyState = m_CurrentKeyState;
m_LastMouseState = m_CurrentMouseState;
m_LastMouseX = m_CurrentMouseX;
+2 -2
View File
@@ -39,9 +39,9 @@ private:
GLFWwindow* m_GLFWWindow;
std::shared_ptr<::EventBroker> EventBroker;
EventRelay<Events::LockMouse> m_ELockMouse;
EventRelay<InputManager, Events::LockMouse> m_ELockMouse;
bool OnLockMouse(const Events::LockMouse &event);
EventRelay<Events::UnlockMouse> m_EUnlockMouse;
EventRelay<InputManager, Events::UnlockMouse> m_EUnlockMouse;
bool OnUnlockMouse(const Events::UnlockMouse &event);
std::array<int, GLFW_KEY_LAST+1> m_CurrentKeyState;
+1 -1
View File
@@ -19,7 +19,7 @@ public:
void Update(double dt) override;
EventRelay<Events::KeyDown> m_EKeyDown;
EventRelay<DebugSystem, Events::KeyDown> m_EKeyDown;
bool OnKeyDown(const Events::KeyDown &event);
//void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
+1 -1
View File
@@ -27,7 +27,7 @@ private:
std::unique_ptr<FreeSteeringInputController> m_InputController;
};
class FreeSteeringSystem::FreeSteeringInputController : InputController
class FreeSteeringSystem::FreeSteeringInputController : InputController<FreeSteeringSystem>
{
public:
FreeSteeringInputController(std::shared_ptr<::EventBroker> eventBroker)
+1 -1
View File
@@ -27,7 +27,7 @@ private:
std::map<EntityID, double> m_TimeSinceLastShot;
};
class HelicopterSteeringSystem::HelicopterSteeringInputController : InputController
class HelicopterSteeringSystem::HelicopterSteeringInputController : InputController<HelicopterSteeringSystem>
{
public:
HelicopterSteeringInputController(std::shared_ptr<::EventBroker> eventBroker)
+11 -11
View File
@@ -45,28 +45,28 @@ private:
std::unordered_map<Gamepad::Button, std::tuple<std::string, float>> m_GamepadButtonBindings; // Gamepad::Button -> command string
// Input events
EventRelay<Events::KeyDown> m_EKeyDown;
EventRelay<InputSystem, Events::KeyDown> m_EKeyDown;
bool OnKeyDown(const Events::KeyDown &event);
EventRelay<Events::KeyUp> m_EKeyUp;
EventRelay<InputSystem, Events::KeyUp> m_EKeyUp;
bool OnKeyUp(const Events::KeyUp &event);
EventRelay<Events::MousePress> m_EMousePress;
EventRelay<InputSystem, Events::MousePress> m_EMousePress;
bool OnMousePress(const Events::MousePress &event);
EventRelay<Events::MouseRelease> m_EMouseRelease;
EventRelay<InputSystem, Events::MouseRelease> m_EMouseRelease;
bool OnMouseRelease(const Events::MouseRelease &event);
EventRelay<Events::GamepadAxis> m_EGamepadAxis;
EventRelay<InputSystem, Events::GamepadAxis> m_EGamepadAxis;
bool OnGamepadAxis(const Events::GamepadAxis &event);
EventRelay<Events::GamepadButtonDown> m_EGamepadButtonDown;
EventRelay<InputSystem, Events::GamepadButtonDown> m_EGamepadButtonDown;
bool OnGamepadButtonDown(const Events::GamepadButtonDown &event);
EventRelay<Events::GamepadButtonUp> m_EGamepadButtonUp;
EventRelay<InputSystem, Events::GamepadButtonUp> m_EGamepadButtonUp;
bool OnGamepadButtonUp(const Events::GamepadButtonUp &event);
// Input binding events
EventRelay<Events::BindKey> m_EBindKey;
EventRelay<InputSystem, Events::BindKey> m_EBindKey;
bool OnBindKey(const Events::BindKey &event);
EventRelay<Events::BindMouseButton> m_EBindMouseButton;
EventRelay<InputSystem, Events::BindMouseButton> m_EBindMouseButton;
bool OnBindMouseButton(const Events::BindMouseButton &event);
EventRelay<Events::BindGamepadAxis> m_EBindGamepadAxis;
EventRelay<InputSystem, Events::BindGamepadAxis> m_EBindGamepadAxis;
bool OnBindGamepadAxis(const Events::BindGamepadAxis &event);
EventRelay<Events::BindGamepadButton> m_EBindGamepadButton;
EventRelay<InputSystem, Events::BindGamepadButton> m_EBindGamepadButton;
bool OnBindGamepadButton(const Events::BindGamepadButton &event);
float GetCommandTotalValue(std::string command);
+4 -4
View File
@@ -113,13 +113,13 @@ private:
hkpWorld* m_PhysicsWorld;
// Events
EventRelay<Events::TankSteer> m_ETankSteer;
EventRelay<PhysicsSystem, Events::TankSteer> m_ETankSteer;
bool OnTankSteer(const Events::TankSteer &event);
EventRelay<Events::SetVelocity> m_ESetVelocity;
EventRelay<PhysicsSystem, Events::SetVelocity> m_ESetVelocity;
bool OnSetVelocity(const Events::SetVelocity &event);
EventRelay<Events::ApplyForce> m_EApplyForce;
EventRelay<PhysicsSystem, Events::ApplyForce> m_EApplyForce;
bool OnApplyForce(const Events::ApplyForce &event);
EventRelay<Events::ApplyPointImpulse> m_EApplyPointImpulse;
EventRelay<PhysicsSystem, Events::ApplyPointImpulse> m_EApplyPointImpulse;
bool OnApplyPointImpulse(const Events::ApplyPointImpulse &event);
void SetUpPhysicsState(EntityID entity, EntityID parent);
+1 -1
View File
@@ -44,7 +44,7 @@ private:
//unsigned long dataSize;
// Events
EventRelay<Events::PlaySound> m_EPlaySound;
EventRelay<SoundSystem, Events::PlaySound> m_EPlaySound;
bool OnPlaySound(const Events::PlaySound &event);
std::map<Component*, ALuint> m_Sources;
+1 -1
View File
@@ -37,7 +37,7 @@ namespace Systems
std::map<EntityID, double> m_TimeSinceLastShot;
};
class TankSteeringSystem::TankSteeringInputController : InputController
class TankSteeringSystem::TankSteeringInputController : InputController<TankSteeringSystem>
{
public:
TankSteeringInputController(std::shared_ptr<::EventBroker> eventBroker, int playerID)
+2
View File
@@ -36,7 +36,9 @@ void World::Update(double dt)
{
for (auto pair : m_Systems)
{
const std::string &type = pair.first;
auto system = pair.second;
m_EventBroker->Process(type);
system->Update(dt);
RecursiveUpdate(system, dt, 0);
}