89 lines
2.4 KiB
C
89 lines
2.4 KiB
C
|
#ifndef EVENTSBASE_H
|
||
|
#define EVENTSBASE_H
|
||
|
|
||
|
#include "Module.h"
|
||
|
|
||
|
#include <boost/shared_ptr.hpp>
|
||
|
|
||
|
#include <string>
|
||
|
#include <map>
|
||
|
#include <vector>
|
||
|
#include <queue>
|
||
|
|
||
|
namespace Engine {
|
||
|
|
||
|
/** \brief Contains all information relevant to the Event */
|
||
|
struct EventBase {
|
||
|
int mEventType;
|
||
|
|
||
|
/** \brief This might later be used for de-/serializing of the event data */
|
||
|
std::string mEventData;
|
||
|
float mEventFloat;
|
||
|
int mEventInt;
|
||
|
unsigned int mEventUnsignedInt;
|
||
|
};
|
||
|
|
||
|
typedef boost::shared_ptr<EventBase> EventBasePtr;
|
||
|
|
||
|
/** \brief Takes care of the handling of the Event
|
||
|
*
|
||
|
* Processing of the Event is done in the EventListenerBase::HandleEvent()
|
||
|
* function.
|
||
|
*/
|
||
|
class EventListenerBase {
|
||
|
public:
|
||
|
virtual ~EventListenerBase() {};
|
||
|
/** \brief Handles the Event */
|
||
|
virtual bool HandleEvent (const EventBasePtr &event) const = 0;
|
||
|
void just_for_the_vtable() {};
|
||
|
|
||
|
/** \brief For debugging */
|
||
|
std::string mName;
|
||
|
};
|
||
|
|
||
|
typedef boost::shared_ptr<EventListenerBase> EventListenerPtr;
|
||
|
|
||
|
/** \brief Keeps track of all the EventListenerBase objects and receives Event notifications.
|
||
|
*/
|
||
|
class EventManager : public Module {
|
||
|
public:
|
||
|
virtual ~EventManager() {};
|
||
|
|
||
|
/** \brief Registers a listener to a given event type */
|
||
|
bool RegisterListener (const EventListenerBase *listener, const int event_type);
|
||
|
/** \brief Calls all event listeners to handle the events */
|
||
|
void Process ();
|
||
|
/** \brief Queues the until Process() gets called */
|
||
|
bool QueueEvent (const EventBasePtr &event);
|
||
|
/** \brief Calls the listener handlers immediately */
|
||
|
bool TriggerEvent (const EventBasePtr &event);
|
||
|
|
||
|
/** \brief Returns true if there is a listener for a given event type */
|
||
|
bool HasEventTypeListener (const int event_type) {
|
||
|
return mEventTypeListeners.find(event_type) != mEventTypeListeners.end();
|
||
|
}
|
||
|
/** \brief Returns the number of listeners for a given event type */
|
||
|
unsigned int GetEventTypeListenerCount (const int event_type) {
|
||
|
if (!HasEventTypeListener (event_type))
|
||
|
return 0;
|
||
|
|
||
|
return (mEventTypeListeners.find(event_type))->second.size();
|
||
|
}
|
||
|
unsigned int GetQueuedEventCount () {
|
||
|
return mQueuedEvents.size();
|
||
|
}
|
||
|
|
||
|
protected:
|
||
|
virtual int OnInit (int argc, char* argv[]);
|
||
|
virtual void OnDestroy();
|
||
|
|
||
|
private:
|
||
|
/** \brief Contains for each event type the list of listeners */
|
||
|
std::map <int, std::vector<const EventListenerBase*> > mEventTypeListeners;
|
||
|
std::queue <EventBasePtr> mQueuedEvents;
|
||
|
};
|
||
|
|
||
|
}
|
||
|
|
||
|
#endif /* EVENTSBASE_H */
|