Merge pull request #6169 from dumganhar/iss4710-eventdispatcher-crash

closed #4710: [win32] Crash in EventDispatcher if game starts up with mouse moving frequently
This commit is contained in:
James Chen 2014-04-08 15:31:52 +08:00
commit 5df765ef0c
3 changed files with 26 additions and 15 deletions

View File

@ -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)
{

View File

@ -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);
@ -1150,7 +1159,6 @@ 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;

View File

@ -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);