From 80d1c315b3fab035ce91bffeeee6330fda7e146a Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 18 Sep 2013 13:00:08 +0800 Subject: [PATCH] issue #2087: Modify singleton implement for EventDispatcher, adding EventDispatcher::destroyInstance(). --- .../event_dispatcher/CCEventDispatcher.cpp | 22 +++++++++++++++---- cocos2dx/event_dispatcher/CCEventDispatcher.h | 9 +++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/cocos2dx/event_dispatcher/CCEventDispatcher.cpp b/cocos2dx/event_dispatcher/CCEventDispatcher.cpp index c9b6f8718f..7aa26aede9 100644 --- a/cocos2dx/event_dispatcher/CCEventDispatcher.cpp +++ b/cocos2dx/event_dispatcher/CCEventDispatcher.cpp @@ -58,6 +58,8 @@ private: NS_CC_BEGIN +static EventDispatcher* g_instance = nullptr; + EventDispatcher::EventListenerItem::~EventListenerItem() { CC_SAFE_RELEASE(this->node); @@ -73,12 +75,22 @@ EventDispatcher::EventDispatcher() EventDispatcher::~EventDispatcher() { + removeAllListeners(); } EventDispatcher* EventDispatcher::getInstance() { - static EventDispatcher _instance; - return &_instance; + if (g_instance == nullptr) + { + g_instance = new EventDispatcher(); + } + + return g_instance; +} + +void EventDispatcher::destroyInstance() +{ + CC_SAFE_DELETE(g_instance); } void EventDispatcher::addEventListenerWithItem(EventListenerItem* item) @@ -249,13 +261,15 @@ void EventDispatcher::setPriorityWithFixedValue(EventListener* listener, int fix } } -void EventDispatcher::dispatchEvent(Event* event, bool toSortListeners) +void EventDispatcher::dispatchEvent(Event* event, bool forceSortListeners) { if (_listeners == nullptr || !_isEnabled) return; - if (toSortListeners) + if (forceSortListeners) + { sortAllEventListenerItemsForType(event->_type); + } DispatchGuard guard(_inDispatch); diff --git a/cocos2dx/event_dispatcher/CCEventDispatcher.h b/cocos2dx/event_dispatcher/CCEventDispatcher.h index 51aff99973..8d11d4e620 100644 --- a/cocos2dx/event_dispatcher/CCEventDispatcher.h +++ b/cocos2dx/event_dispatcher/CCEventDispatcher.h @@ -55,6 +55,9 @@ public: /** Gets the singleton of EventDispatcher */ static EventDispatcher* getInstance(); + /** Destroys the singleton of EventDispatcher */ + static void destroyInstance(); + /** Adds a event listener for a specified event with the priority of scene graph. * @param listener The listener of a specified event. * @param node The priority of the listener is based on the draw order of this node. @@ -98,7 +101,7 @@ public: * Also removes all EventListeners marked for deletion from the * event dispatcher list. */ - void dispatchEvent(Event* event, bool toSortListeners = true); + void dispatchEvent(Event* event, bool forceSortListeners = true); public: /** Destructor of EventDispatcher */ @@ -141,8 +144,8 @@ private: std::map*>* _listeners; std::vector _toAddedListeners; - int _inDispatch; - bool _isEnabled; + int _inDispatch; ///< Whether it's in dispatching event + bool _isEnabled; ///< Whether to enable dispatching event };