moved event handling into Modules instead of having seperate event listener objects
This commit is contained in:
@@ -22,7 +22,7 @@ SET ( ENGINE_SRCS
|
||||
PhysicsBase.cc
|
||||
PhysicsEntityBase.cc
|
||||
ViewBase.cc
|
||||
EventsBase.cc
|
||||
EventBase.cc
|
||||
OverlayBase.cc
|
||||
|
||||
Commands.cc
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@
|
||||
#include "ControllerBase.h"
|
||||
|
||||
#include "EntityFactoryBase.h"
|
||||
#include "EventsBase.h"
|
||||
#include "EventManager.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
|
||||
+1
-1
@@ -154,6 +154,6 @@ ControllerBase* EngineGetController ();
|
||||
#include "CommandsGlobal.h"
|
||||
#include "ModelBaseGlobal.h"
|
||||
#include "ViewBaseGlobal.h"
|
||||
#include "EventsBaseGlobal.h"
|
||||
#include "EventBaseGlobal.h"
|
||||
|
||||
#endif // _ENGINE_H
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "EventsBase.h"
|
||||
#include "EventBase.h"
|
||||
#include "EventManager.h"
|
||||
#include "Logging.h"
|
||||
|
||||
namespace Engine {
|
||||
@@ -15,10 +16,10 @@ void EventManager::OnDestroy() {
|
||||
EventManagerInstance = NULL;
|
||||
}
|
||||
|
||||
bool EventManager::RegisterListener (const EventListenerBase *listener, const int event_type) {
|
||||
LogDebug ("Registering Event listener %x for event type %d", listener, event_type);
|
||||
bool EventManager::RegisterListener (Module *listener_module, const int event_type) {
|
||||
LogDebug ("Registering Event listener %x for event type %d", listener_module, event_type);
|
||||
|
||||
mEventTypeListeners[event_type].push_back(listener);
|
||||
mEventTypeListeners[event_type].push_back(listener_module);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -42,9 +43,9 @@ bool EventManager::TriggerEvent (const EventBasePtr &event) {
|
||||
if (!HasEventTypeListener (event->mEventType))
|
||||
return false;
|
||||
|
||||
std::vector<const EventListenerBase*>::iterator listener_iter = mEventTypeListeners[event->mEventType].begin();
|
||||
std::vector<Module*>::iterator listener_iter = mEventTypeListeners[event->mEventType].begin();
|
||||
for ( ; listener_iter != mEventTypeListeners[event->mEventType].end(); listener_iter++) {
|
||||
if ((*listener_iter)->HandleEvent (event))
|
||||
if ((*listener_iter)->SendEvent (event))
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -55,13 +56,13 @@ bool EventManager::TriggerEvent (const EventBasePtr &event) {
|
||||
* Global Functions
|
||||
*/
|
||||
/** \brief Registers a listener to a given event type */
|
||||
bool RegisterListener (const EventListenerBase *listener, const int event_type) {
|
||||
bool RegisterListener (Module *listener_module, const int event_type) {
|
||||
if (!EventManagerInstance) {
|
||||
LogError ("Could not register EventListenerBase: EventManager not initialized!");
|
||||
LogError ("Could not register Module: EventManager not initialized!");
|
||||
return false;
|
||||
}
|
||||
|
||||
return EventManagerInstance->RegisterListener (listener, event_type);
|
||||
return EventManagerInstance->RegisterListener (listener_module, event_type);
|
||||
}
|
||||
|
||||
/** \brief Calls all event listeners to handle the events */
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef EVENTSBASE_H
|
||||
#define EVENTSBASE_H
|
||||
|
||||
// #include "Module.h"
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
|
||||
namespace Engine {
|
||||
|
||||
class Module;
|
||||
|
||||
/** \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;
|
||||
|
||||
}
|
||||
|
||||
#endif /* EVENTSBASE_H */
|
||||
@@ -1,12 +1,12 @@
|
||||
#ifndef EVENTSBASEGLOBAL_H
|
||||
#define EVENTSBASEGLOBAL_H
|
||||
|
||||
#include "EventsBase.h"
|
||||
#include "EventBase.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
/** \brief Registers a listener to a given event type */
|
||||
bool RegisterListener (const EventListenerBase *listener, const int event_type);
|
||||
bool RegisterListener (Module *listener_module, const int event_type);
|
||||
/** \brief Calls all event listeners to handle the events */
|
||||
bool QueueEvent (const EventBasePtr &event);
|
||||
/** \brief Calls the listener handlers immediately */
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef EVENTSBASE_H
|
||||
#define EVENTSBASE_H
|
||||
#ifndef EVENTSMANAGER_H
|
||||
#define EVENTSMANAGER_H
|
||||
|
||||
#include "Module.h"
|
||||
// #include "Module.h"
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
@@ -10,47 +10,20 @@
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
|
||||
#include "EventBase.h"
|
||||
#include "Module.h"
|
||||
|
||||
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
|
||||
/** \brief Keeps track of all the Modules that registered for a given event type
|
||||
*
|
||||
* 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);
|
||||
bool RegisterListener (Module *listener_module, const int event_type);
|
||||
/** \brief Calls all event listeners to handle the events */
|
||||
void Process ();
|
||||
/** \brief Queues the until Process() gets called */
|
||||
@@ -79,10 +52,10 @@ class EventManager : public Module {
|
||||
|
||||
private:
|
||||
/** \brief Contains for each event type the list of listeners */
|
||||
std::map <int, std::vector<const EventListenerBase*> > mEventTypeListeners;
|
||||
std::map <int, std::vector<Module*> > mEventTypeListeners;
|
||||
std::queue <EventBasePtr> mQueuedEvents;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* EVENTSBASE_H */
|
||||
#endif /* EVENTSMANAGER_H */
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef MODULE_H
|
||||
#define MODULE_H
|
||||
|
||||
#include "EventBase.h"
|
||||
|
||||
namespace Engine {
|
||||
|
||||
/** \brief Base class for the separate modules
|
||||
@@ -36,6 +38,7 @@ class Module {
|
||||
/** \brief Calls the function that registers its commands to the
|
||||
* Commandsystem */
|
||||
virtual void RegisterCommands () { OnRegisterCommands (); }
|
||||
virtual bool SendEvent (const EventBasePtr &event) { return OnReceiveEvent (event); }
|
||||
|
||||
protected:
|
||||
/** \brief The actual function being called when Init () is called */
|
||||
@@ -44,6 +47,8 @@ class Module {
|
||||
virtual void OnDestroy () { };
|
||||
/** \brief Registers the commands of the Module */
|
||||
virtual void OnRegisterCommands () { };
|
||||
/** \brief Reacts on a event that was sent to the module */
|
||||
virtual bool OnReceiveEvent (const EventBasePtr &event) { return false; };
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -17,5 +17,5 @@ SET_TARGET_PROPERTIES ( ${PROJECT_EXECUTABLES} PROPERTIES
|
||||
|
||||
SUBDIRS (tests)
|
||||
|
||||
ADD_LIBRARY ( coll2d ${COLL2D_SRCS} )
|
||||
ADD_LIBRARY ( coll2d SHARED ${COLL2D_SRCS} )
|
||||
|
||||
|
||||
@@ -6,5 +6,4 @@ SET ( SRCS
|
||||
mathlib.cc
|
||||
main.cc
|
||||
)
|
||||
|
||||
ADD_LIBRARY ( mathlib mathlib.cc mathlib.h)
|
||||
ADD_LIBRARY ( mathlib SHARED mathlib.cc mathlib.h)
|
||||
|
||||
@@ -23,7 +23,7 @@ IF( WIN32 )
|
||||
ADD_DEFINITIONS( -DOGLFT_BUILD )
|
||||
ENDIF( WIN32 )
|
||||
|
||||
ADD_LIBRARY( oglft STATIC ${sources} )
|
||||
ADD_LIBRARY( oglft SHARED ${sources} )
|
||||
TARGET_LINK_LIBRARIES(
|
||||
oglft
|
||||
${FREETYPE2_LIBRARIES}
|
||||
|
||||
@@ -9,7 +9,7 @@ SET ( TESTS_SRCS
|
||||
main.cc
|
||||
CommandsTests.cc
|
||||
EntityTests.cc
|
||||
EventsBaseTests.cc
|
||||
EventBaseTests.cc
|
||||
PhysicsTests.cc
|
||||
ControllerBaseTests.cc
|
||||
)
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#include <UnitTest++.h>
|
||||
|
||||
#include "Logging.h"
|
||||
#include "EventsBase.h"
|
||||
#include "Module.h"
|
||||
#include "EventManager.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace Engine;
|
||||
@@ -9,18 +10,21 @@ using namespace Engine;
|
||||
int global_event_type = -1;
|
||||
string global_event_string = "";
|
||||
|
||||
class TestEventListener : public EventListenerBase {
|
||||
public:
|
||||
TestEventListener() {
|
||||
mName = "TestEventListener";
|
||||
}
|
||||
struct EventsFixture;
|
||||
|
||||
virtual bool HandleEvent (const EventBasePtr &event) const {
|
||||
class TestEventModule : public Module {
|
||||
private:
|
||||
virtual int OnInit (int argc, char* argv[]) {
|
||||
return 0;
|
||||
};
|
||||
virtual bool OnReceiveEvent (const EventBasePtr &event) {
|
||||
global_event_type = event->mEventType;
|
||||
global_event_string = event->mEventData;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
friend class EventsFixture;
|
||||
};
|
||||
|
||||
struct EventsFixture {
|
||||
@@ -43,17 +47,17 @@ struct EventsFixture {
|
||||
|
||||
Logging LoggingModule;
|
||||
|
||||
TestEventListener Listener;
|
||||
TestEventModule TestModule;
|
||||
EventManager *TestEventManager;
|
||||
};
|
||||
|
||||
TEST_FIXTURE ( EventsFixture, TestEventListenerHandleEvent ) {
|
||||
TEST_FIXTURE ( EventsFixture, TestEventModuleSendEvent ) {
|
||||
EventBasePtr event (new EventBase);
|
||||
|
||||
event->mEventType = 1;
|
||||
event->mEventData = "test";
|
||||
|
||||
Listener.HandleEvent (event);
|
||||
TestModule.SendEvent (event);
|
||||
|
||||
CHECK_EQUAL (1, global_event_type);
|
||||
CHECK_EQUAL ("test", global_event_string);
|
||||
@@ -65,17 +69,17 @@ TEST_FIXTURE ( EventsFixture, TestTestEventManagerZeroListeners ) {
|
||||
}
|
||||
|
||||
TEST_FIXTURE ( EventsFixture, TestTestEventManagerAddListener ) {
|
||||
TestEventManager->RegisterListener (&Listener, 1);
|
||||
TestEventManager->RegisterListener (&TestModule, 1);
|
||||
|
||||
CHECK_EQUAL (true, TestEventManager->HasEventTypeListener(1));
|
||||
CHECK_EQUAL (false, TestEventManager->HasEventTypeListener(2));
|
||||
|
||||
TestEventManager->RegisterListener (&Listener, 1);
|
||||
TestEventManager->RegisterListener (&TestModule, 1);
|
||||
CHECK_EQUAL (2, TestEventManager->GetEventTypeListenerCount(1));
|
||||
}
|
||||
|
||||
TEST_FIXTURE ( EventsFixture, TestTestEventManagerTriggerEvent ) {
|
||||
TestEventManager->RegisterListener (&Listener, 1);
|
||||
TestEventManager->RegisterListener (&TestModule, 1);
|
||||
|
||||
CHECK_EQUAL (true, TestEventManager->HasEventTypeListener(1));
|
||||
|
||||
@@ -94,7 +98,7 @@ TEST_FIXTURE ( EventsFixture, TestTestEventManagerTriggerEvent ) {
|
||||
}
|
||||
|
||||
TEST_FIXTURE ( EventsFixture, TestTestEventManagerQueueEvent ) {
|
||||
TestEventManager->RegisterListener (&Listener, 1);
|
||||
TestEventManager->RegisterListener (&TestModule, 1);
|
||||
|
||||
EventBasePtr event (new EventBase);
|
||||
|
||||
@@ -111,7 +115,7 @@ TEST_FIXTURE ( EventsFixture, TestTestEventManagerQueueEvent ) {
|
||||
}
|
||||
|
||||
TEST_FIXTURE ( EventsFixture, TestTestEventManagerProcess ) {
|
||||
TestEventManager->RegisterListener (&Listener, 1);
|
||||
TestEventManager->RegisterListener (&TestModule, 1);
|
||||
|
||||
EventBasePtr event (new EventBase);
|
||||
|
||||
Reference in New Issue
Block a user