From 49bba30409df9a16e017915505b466729febbc90 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 8 Apr 2014 15:08:34 +0800 Subject: [PATCH 1/2] closed #4710: [win32] Crash in EventDispatcher if game starts up with mouse moving frequently --- cocos/2d/CCDirector.cpp | 13 +++++++++---- cocos/2d/CCEventDispatcher.cpp | 26 ++++++++++++++++---------- cocos/2d/CCEventDispatcher.h | 2 +- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/cocos/2d/CCDirector.cpp b/cocos/2d/CCDirector.cpp index 98bdeb83a6..be7757e66c 100644 --- a/cocos/2d/CCDirector.cpp +++ b/cocos/2d/CCDirector.cpp @@ -393,7 +393,10 @@ void Director::setOpenGLView(GLView *openGLView) CHECK_GL_ERROR_DEBUG(); -// _touchDispatcher->setDispatchEvents(true); + if (_eventDispatcher) + { + _eventDispatcher->setEnabled(true); + } } } @@ -747,9 +750,11 @@ void Director::purgeDirector() // cleanup scheduler getScheduler()->unscheduleAll(); - // don't release the event handlers - // They are needed in case the director is run again -// _touchDispatcher->removeAllDelegates(); + // Disable event dispatching + if (_eventDispatcher) + { + _eventDispatcher->setEnabled(false); + } if (_runningScene) { diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index 93e3c85bd7..54a38a313c 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -31,7 +31,7 @@ #include "CCEventListenerKeyboard.h" #include "CCEventListenerCustom.h" -#include "CCNode.h" +#include "CCScene.h" #include "CCDirector.h" #include "CCEventType.h" @@ -191,7 +191,7 @@ void EventDispatcher::EventListenerVector::clear() EventDispatcher::EventDispatcher() : _inDispatch(0) -, _isEnabled(true) +, _isEnabled(false) , _nodePriorityIndex(0) { _toAddedListeners.reserve(50); @@ -1125,6 +1125,9 @@ void EventDispatcher::sortEventListeners(const EventListener::ListenerID& listen if (dirtyFlag != DirtyFlag::NONE) { + // Clear the dirty flag first, if `rootNode` is nullptr, then set its dirty flag of scene graph priority + dirtyIter->second = DirtyFlag::NONE; + if ((int)dirtyFlag & (int)DirtyFlag::FIXED_PRIORITY) { sortEventListenersOfFixedPriority(listenerID); @@ -1132,14 +1135,20 @@ void EventDispatcher::sortEventListeners(const EventListener::ListenerID& listen if ((int)dirtyFlag & (int)DirtyFlag::SCENE_GRAPH_PRIORITY) { - sortEventListenersOfSceneGraphPriority(listenerID); + auto rootNode = Director::getInstance()->getRunningScene(); + if (rootNode) + { + sortEventListenersOfSceneGraphPriority(listenerID, rootNode); + } + else + { + dirtyIter->second = DirtyFlag::SCENE_GRAPH_PRIORITY; + } } - - dirtyIter->second = DirtyFlag::NONE; } } -void EventDispatcher::sortEventListenersOfSceneGraphPriority(const EventListener::ListenerID& listenerID) +void EventDispatcher::sortEventListenersOfSceneGraphPriority(const EventListener::ListenerID& listenerID, Node* rootNode) { auto listeners = getListeners(listenerID); @@ -1149,8 +1158,7 @@ void EventDispatcher::sortEventListenersOfSceneGraphPriority(const EventListener if (sceneGraphListeners == nullptr) return; - - Node* rootNode = (Node*)Director::getInstance()->getRunningScene(); + // Reset priority index _nodePriorityIndex = 0; _nodePriorityMap.clear(); @@ -1158,7 +1166,6 @@ void EventDispatcher::sortEventListenersOfSceneGraphPriority(const EventListener visitTarget(rootNode, true); // After sort: priority < 0, > 0 - std::sort(sceneGraphListeners->begin(), sceneGraphListeners->end(), [this](const EventListener* l1, const EventListener* l2) { return _nodePriorityMap[l1->getSceneGraphPriority()] > _nodePriorityMap[l2->getSceneGraphPriority()]; }); @@ -1351,7 +1358,6 @@ void EventDispatcher::setEnabled(bool isEnabled) _isEnabled = isEnabled; } - bool EventDispatcher::isEnabled() const { return _isEnabled; diff --git a/cocos/2d/CCEventDispatcher.h b/cocos/2d/CCEventDispatcher.h index 002f42a224..16d7b62149 100644 --- a/cocos/2d/CCEventDispatcher.h +++ b/cocos/2d/CCEventDispatcher.h @@ -207,7 +207,7 @@ protected: void sortEventListeners(const EventListener::ListenerID& listenerID); /** Sorts the listeners of specified type by scene graph priority */ - void sortEventListenersOfSceneGraphPriority(const EventListener::ListenerID& listenerID); + void sortEventListenersOfSceneGraphPriority(const EventListener::ListenerID& listenerID, Node* rootNode); /** Sorts the listeners of specified type by fixed priority */ void sortEventListenersOfFixedPriority(const EventListener::ListenerID& listenerID); From 8f451d48a17e226259130577c8c6ea908f882d3c Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 8 Apr 2014 15:11:04 +0800 Subject: [PATCH 2/2] issue #4710: Fixed wrong indention. --- cocos/2d/CCDirector.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cocos/2d/CCDirector.cpp b/cocos/2d/CCDirector.cpp index be7757e66c..3ca022104e 100644 --- a/cocos/2d/CCDirector.cpp +++ b/cocos/2d/CCDirector.cpp @@ -393,10 +393,10 @@ void Director::setOpenGLView(GLView *openGLView) CHECK_GL_ERROR_DEBUG(); - if (_eventDispatcher) - { - _eventDispatcher->setEnabled(true); - } + if (_eventDispatcher) + { + _eventDispatcher->setEnabled(true); + } } } @@ -751,10 +751,10 @@ void Director::purgeDirector() getScheduler()->unscheduleAll(); // Disable event dispatching - if (_eventDispatcher) - { - _eventDispatcher->setEnabled(false); - } + if (_eventDispatcher) + { + _eventDispatcher->setEnabled(false); + } if (_runningScene) {