Merge pull request #3681 from dumganhar/iss2087-new-event-dispatcher

issue #2087: Small fix for EventDispatcher. Adding EventDispatcher::destroyInstance method.[ci skip]
This commit is contained in:
James Chen 2013-09-17 22:44:13 -07:00
commit 39259c0553
5 changed files with 35 additions and 11 deletions

View File

@ -49,6 +49,7 @@ THE SOFTWARE.
#include "actions/CCActionManager.h" #include "actions/CCActionManager.h"
#include "sprite_nodes/CCAnimationCache.h" #include "sprite_nodes/CCAnimationCache.h"
#include "event_dispatcher/CCTouch.h" #include "event_dispatcher/CCTouch.h"
#include "event_dispatcher/CCEventDispatcher.h"
#include "support/user_default/CCUserDefault.h" #include "support/user_default/CCUserDefault.h"
#include "shaders/ccGLStateCache.h" #include "shaders/ccGLStateCache.h"
#include "shaders/CCShaderCache.h" #include "shaders/CCShaderCache.h"
@ -696,6 +697,7 @@ void Director::purgeDirector()
// cocos2d-x specific data structures // cocos2d-x specific data structures
UserDefault::destroyInstance(); UserDefault::destroyInstance();
NotificationCenter::destroyInstance(); NotificationCenter::destroyInstance();
EventDispatcher::destroyInstance();
GL::invalidateStateCache(); GL::invalidateStateCache();

View File

@ -126,6 +126,8 @@ Node::Node(void)
, _isTransitionFinished(false) , _isTransitionFinished(false)
, _updateScriptHandler(0) , _updateScriptHandler(0)
, _componentContainer(NULL) , _componentContainer(NULL)
, _eventPriority(0)
, _oldEventPriority(0)
{ {
// set default scheduler and actionManager // set default scheduler and actionManager
Director *director = Director::getInstance(); Director *director = Director::getInstance();
@ -1307,9 +1309,11 @@ void Node::dissociateEventListener(EventListener* listener)
void Node::removeAllEventListeners() void Node::removeAllEventListeners()
{ {
auto dispatcher = EventDispatcher::getInstance();
for (auto& listener : _eventlisteners) for (auto& listener : _eventlisteners)
{ {
EventDispatcher::getInstance()->removeEventListener(listener); dispatcher->removeEventListener(listener);
} }
} }

View File

@ -1464,8 +1464,9 @@ protected:
ComponentContainer *_componentContainer; ///< Dictionary of components ComponentContainer *_componentContainer; ///< Dictionary of components
int _eventPriority; int _eventPriority; ///< The scene graph based priority of event listener.
static int _globalEventPriorityIndex; int _oldEventPriority; ///< The old scene graph based priority of event listener.
static int _globalEventPriorityIndex; ///< The index of global event priority.
}; };
//#pragma mark - NodeRGBA //#pragma mark - NodeRGBA

View File

@ -58,6 +58,8 @@ private:
NS_CC_BEGIN NS_CC_BEGIN
static EventDispatcher* g_instance = nullptr;
EventDispatcher::EventListenerItem::~EventListenerItem() EventDispatcher::EventListenerItem::~EventListenerItem()
{ {
CC_SAFE_RELEASE(this->node); CC_SAFE_RELEASE(this->node);
@ -73,12 +75,22 @@ EventDispatcher::EventDispatcher()
EventDispatcher::~EventDispatcher() EventDispatcher::~EventDispatcher()
{ {
removeAllListeners();
} }
EventDispatcher* EventDispatcher::getInstance() EventDispatcher* EventDispatcher::getInstance()
{ {
static EventDispatcher _instance; if (g_instance == nullptr)
return &_instance; {
g_instance = new EventDispatcher();
}
return g_instance;
}
void EventDispatcher::destroyInstance()
{
CC_SAFE_DELETE(g_instance);
} }
void EventDispatcher::addEventListenerWithItem(EventListenerItem* item) 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) if (_listeners == nullptr || !_isEnabled)
return; return;
if (toSortListeners) if (forceSortListeners)
{
sortAllEventListenerItemsForType(event->_type); sortAllEventListenerItemsForType(event->_type);
}
DispatchGuard guard(_inDispatch); DispatchGuard guard(_inDispatch);

View File

@ -55,6 +55,9 @@ public:
/** Gets the singleton of EventDispatcher */ /** Gets the singleton of EventDispatcher */
static EventDispatcher* getInstance(); 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. /** Adds a event listener for a specified event with the priority of scene graph.
* @param listener The listener of a specified event. * @param listener The listener of a specified event.
* @param node The priority of the listener is based on the draw order of this node. * @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 * Also removes all EventListeners marked for deletion from the
* event dispatcher list. * event dispatcher list.
*/ */
void dispatchEvent(Event* event, bool toSortListeners = true); void dispatchEvent(Event* event, bool forceSortListeners = true);
public: public:
/** Destructor of EventDispatcher */ /** Destructor of EventDispatcher */
@ -141,8 +144,8 @@ private:
std::map<std::string, std::vector<EventListenerItem*>*>* _listeners; std::map<std::string, std::vector<EventListenerItem*>*>* _listeners;
std::vector<EventListenerItem*> _toAddedListeners; std::vector<EventListenerItem*> _toAddedListeners;
int _inDispatch; int _inDispatch; ///< Whether it's in dispatching event
bool _isEnabled; bool _isEnabled; ///< Whether to enable dispatching event
}; };