I think it looks good. I would make the classes that listen to the events register their event handlers with the event manager. This helps you register sub-objects events. ie if a scroll bar is made up of a slider and two buttons, the slider and button's event handlers need to be registered when the scroll bar's register event handlers function is called.
void cButton::registerEventHandles ( cEventHandler * EventHandler ) { EventHandler->registerEventHandle ( this, &cButton::handleMouseButton ); EventHandler->registerEventHandle ( this, &cButton::handleMouseMove );};bool cButton::handleMouseButton ( const cMouseButtonEvent *ButtonEvent ){// handle mose button event}bool cButton::handleMouseMove ( const cMouseMoveEvent *MoveEvent ){// handle mouse move event}
I would also use static strings for the event names and event paramaters would be members in inherited classes from cEvent. Hard coding events shouldnt be a problem. You dont have a PlayerMoveRight event, you have a PlayerMove event, and the paramaters tell the system which way the player moved.
#define GIVE_NAME( NAME ) virtual const cString getName ( ) const { static cString name ( #NAME ); return name; }// never used directly, use inherited classes below insteadclass cMouseEvent : public cEvent {public: cMouseEvent ( const int x, const int y ) : mX ( x ), mY ( y ), cEvent ( &cMouseTarget::mTaget, 0.0f ) { } int mX; int mY;};class cMouseButtonEvent : public cMouseEvent{public: cMouseButtonEvent ( const int x, const int y, const int button, const bool pressed ) : cMouseEvent ( x, y ), mButton ( button ), mPressed ( pressed) { } GIVE_NAME ( cMouseButtonEvent ); int mButton; bool mPressed;};class cMouseMoveEvent : public cMouseEvent{public: cMouseMoveEvent ( const int x, const int y, const int dx, const int dy ) : cMouseEvent ( x, y ), mDX ( dx ), mDY ( dy ) { } GIVE_NAME ( cMouseMoveEvent ); int mDX; int mDY;};class cMouseScrollEvent : public cMouseEvent{public: cMouseScrollEvent ( const int x, const int y, const int scrollValue ) : cMouseEvent ( x, y ), mScrollValue ( scrollValue ) { } GIVE_NAME ( cMouseScrollEvent ); int mScrollValue;};
Hope that makes sence.
EDIT:
I forgot to mention that if your strings are stored in a hash table, then string compares reduce to a pointer compare.
The registerEventHandle method can also figure out the type of event that the handler is for based on the prototype of the handler. it knows that
bool cButton::handleMouseMove ( const cMouseMoveEvent *MoveEvent )
is for cMouseMoveEvent's because thats the paramater it takes. You can do this with template paramater deduction as follows:
template < class T, class cEventT > void registerEventHandle ( T* obj, bool ( T::*memFn )( cEventT* ) ) { cString eventName ( cTypeInfo::getTypeInfo < cEventT > ( ).mTypeName ); cArray < cEventHandle * > ** array = mHandles.findItem ( eventName ); if ( !array || !*array ) { array = mHandles.insertItem ( eventName, new cArray < cEventHandle * > ( ) ); } ASSERT ( array ); (*array)->add ( new cMemberEventHandle<T, cEventT>(obj, memFn) ); }