fysxasteroids/engine/EventBase.cc

90 lines
2.1 KiB
C++
Raw Permalink Normal View History

#include "EventBase.h"
#include "EventManager.h"
2010-04-05 23:38:59 +02:00
#include "Logging.h"
namespace Engine {
EventManager* EventManagerInstance = NULL;
int EventManager::OnInit (int argc, char* argv[]) {
EventManagerInstance = this;
return 0;
}
void EventManager::OnDestroy() {
EventManagerInstance = NULL;
}
bool EventManager::RegisterListener (Module *listener_module, const int event_type) {
LogDebug ("Registering Event listener %x for event type %d", listener_module, event_type);
2010-04-05 23:38:59 +02:00
mEventTypeListeners[event_type].push_back(listener_module);
2010-04-05 23:38:59 +02:00
return true;
}
void EventManager::Process () {
while (mQueuedEvents.size() > 0) {
TriggerEvent (mQueuedEvents.front());
mQueuedEvents.pop();
}
}
bool EventManager::QueueEvent (const EventBasePtr &event) {
if (!HasEventTypeListener(event->mEventType))
return false;
mQueuedEvents.push (event);
return true;
}
bool EventManager::TriggerEvent (const EventBasePtr &event) {
if (!HasEventTypeListener (event->mEventType))
return false;
std::vector<Module*>::iterator listener_iter = mEventTypeListeners[event->mEventType].begin();
2010-04-05 23:38:59 +02:00
for ( ; listener_iter != mEventTypeListeners[event->mEventType].end(); listener_iter++) {
if ((*listener_iter)->SendEvent (event))
2010-04-05 23:38:59 +02:00
return true;
}
return false;
}
/*
* Global Functions
*/
/** \brief Registers a listener to a given event type */
bool RegisterListener (Module *listener_module, const int event_type) {
2010-04-05 23:38:59 +02:00
if (!EventManagerInstance) {
LogError ("Could not register Module: EventManager not initialized!");
2010-04-05 23:38:59 +02:00
return false;
}
return EventManagerInstance->RegisterListener (listener_module, event_type);
2010-04-05 23:38:59 +02:00
}
/** \brief Calls all event listeners to handle the events */
bool QueueEvent (const EventBasePtr &event) {
if (!EventManagerInstance) {
LogError ("Could not queue event: EventManager not initialized!");
return false;
}
return EventManagerInstance->QueueEvent (event);
}
/** \brief Calls the listener handlers immediately */
bool TriggerEvent (const EventBasePtr &event) {
if (!EventManagerInstance) {
LogError ("Could not trigger event: EventManager not initialized!");
return false;
}
return EventManagerInstance->TriggerEvent (event);
}
};