2013-09-20 22:23:13 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-09-20 22:23:13 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCEventDispatcher.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCEventCustom.h"
|
|
|
|
#include "base/CCEventListenerTouch.h"
|
|
|
|
#include "base/CCEventListenerAcceleration.h"
|
|
|
|
#include "base/CCEventListenerMouse.h"
|
|
|
|
#include "base/CCEventListenerKeyboard.h"
|
|
|
|
#include "base/CCEventListenerCustom.h"
|
2014-05-04 13:59:26 +08:00
|
|
|
#include "base/CCEventListenerFocus.h"
|
2014-06-30 00:28:21 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
#include "base/CCEventListenerController.h"
|
|
|
|
#endif
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCScene.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCEventType.h"
|
2015-06-18 10:23:52 +08:00
|
|
|
#include "2d/CCCamera.h"
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-24 20:50:21 +08:00
|
|
|
#define DUMP_LISTENER_ITEM_PRIORITY_INFO 0
|
2013-09-20 22:23:13 +08:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
class DispatchGuard
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DispatchGuard(int& count):
|
|
|
|
_count(count)
|
|
|
|
{
|
|
|
|
++_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
~DispatchGuard()
|
|
|
|
{
|
|
|
|
--_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int& _count;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-12-20 19:31:08 +08:00
|
|
|
static EventListener::ListenerID __getListenerID(Event* event)
|
2013-10-28 16:00:01 +08:00
|
|
|
{
|
|
|
|
EventListener::ListenerID ret;
|
|
|
|
switch (event->getType())
|
|
|
|
{
|
|
|
|
case Event::Type::ACCELERATION:
|
2013-12-20 19:31:08 +08:00
|
|
|
ret = EventListenerAcceleration::LISTENER_ID;
|
2013-10-28 16:00:01 +08:00
|
|
|
break;
|
|
|
|
case Event::Type::CUSTOM:
|
|
|
|
{
|
|
|
|
auto customEvent = static_cast<EventCustom*>(event);
|
2013-12-20 19:31:08 +08:00
|
|
|
ret = customEvent->getEventName();
|
2013-10-28 16:00:01 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Event::Type::KEYBOARD:
|
2013-12-20 19:31:08 +08:00
|
|
|
ret = EventListenerKeyboard::LISTENER_ID;
|
2013-10-28 16:00:01 +08:00
|
|
|
break;
|
2013-10-31 14:19:36 +08:00
|
|
|
case Event::Type::MOUSE:
|
2013-12-20 19:31:08 +08:00
|
|
|
ret = EventListenerMouse::LISTENER_ID;
|
2013-10-31 14:19:36 +08:00
|
|
|
break;
|
2014-05-04 13:59:26 +08:00
|
|
|
case Event::Type::FOCUS:
|
|
|
|
ret = EventListenerFocus::LISTENER_ID;
|
|
|
|
break;
|
2013-10-28 16:00:01 +08:00
|
|
|
case Event::Type::TOUCH:
|
|
|
|
// Touch listener is very special, it contains two kinds of listeners, EventListenerTouchOneByOne and EventListenerTouchAllAtOnce.
|
2013-12-20 19:31:08 +08:00
|
|
|
// return UNKNOWN instead.
|
|
|
|
CCASSERT(false, "Don't call this method if the event is for touch.");
|
2013-10-28 16:00:01 +08:00
|
|
|
break;
|
2014-06-30 00:28:21 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
case Event::Type::GAME_CONTROLLER:
|
|
|
|
ret = EventListenerController::LISTENER_ID;
|
|
|
|
break;
|
|
|
|
#endif
|
2013-10-28 16:00:01 +08:00
|
|
|
default:
|
|
|
|
CCASSERT(false, "Invalid type!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-27 15:06:16 +08:00
|
|
|
EventDispatcher::EventListenerVector::EventListenerVector() :
|
|
|
|
_fixedListeners(nullptr),
|
|
|
|
_sceneGraphListeners(nullptr),
|
|
|
|
_gt0Index(0)
|
2013-10-21 17:22:42 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
EventDispatcher::EventListenerVector::~EventListenerVector()
|
2013-10-21 17:22:42 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
CC_SAFE_DELETE(_sceneGraphListeners);
|
|
|
|
CC_SAFE_DELETE(_fixedListeners);
|
2013-10-21 17:22:42 +08:00
|
|
|
}
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
size_t EventDispatcher::EventListenerVector::size() const
|
2013-10-21 17:22:42 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
size_t ret = 0;
|
|
|
|
if (_sceneGraphListeners)
|
|
|
|
ret += _sceneGraphListeners->size();
|
|
|
|
if (_fixedListeners)
|
|
|
|
ret += _fixedListeners->size();
|
|
|
|
|
|
|
|
return ret;
|
2013-10-21 17:22:42 +08:00
|
|
|
}
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
bool EventDispatcher::EventListenerVector::empty() const
|
2013-10-21 17:22:42 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
return (_sceneGraphListeners == nullptr || _sceneGraphListeners->empty())
|
|
|
|
&& (_fixedListeners == nullptr || _fixedListeners->empty());
|
2013-10-21 17:22:42 +08:00
|
|
|
}
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
void EventDispatcher::EventListenerVector::push_back(EventListener* listener)
|
2013-10-21 17:22:42 +08:00
|
|
|
{
|
2014-03-28 06:39:52 +08:00
|
|
|
#if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS
|
2014-03-27 04:12:44 +08:00
|
|
|
CCASSERT(_sceneGraphListeners == nullptr ||
|
|
|
|
std::count(_sceneGraphListeners->begin(), _sceneGraphListeners->end(), listener) == 0,
|
|
|
|
"Listener should not be added twice!");
|
|
|
|
|
|
|
|
CCASSERT(_fixedListeners == nullptr ||
|
|
|
|
std::count(_fixedListeners->begin(), _fixedListeners->end(), listener) == 0,
|
|
|
|
"Listener should not be added twice!");
|
2014-03-28 06:39:52 +08:00
|
|
|
#endif
|
2014-03-27 04:12:44 +08:00
|
|
|
|
2013-10-28 16:00:01 +08:00
|
|
|
if (listener->getFixedPriority() == 0)
|
2013-10-21 17:22:42 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
if (_sceneGraphListeners == nullptr)
|
2013-10-21 17:22:42 +08:00
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
_sceneGraphListeners = new (std::nothrow) std::vector<EventListener*>();
|
2013-10-23 11:27:24 +08:00
|
|
|
_sceneGraphListeners->reserve(100);
|
2013-10-21 17:22:42 +08:00
|
|
|
}
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
_sceneGraphListeners->push_back(listener);
|
2013-10-21 17:22:42 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
if (_fixedListeners == nullptr)
|
2013-10-21 17:22:42 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
_fixedListeners = new std::vector<EventListener*>();
|
|
|
|
_fixedListeners->reserve(100);
|
2013-10-21 17:22:42 +08:00
|
|
|
}
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
_fixedListeners->push_back(listener);
|
2013-10-21 17:22:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-24 17:27:22 +08:00
|
|
|
void EventDispatcher::EventListenerVector::clearSceneGraphListeners()
|
2013-10-21 17:22:42 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
if (_sceneGraphListeners)
|
2013-10-21 17:22:42 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
_sceneGraphListeners->clear();
|
|
|
|
delete _sceneGraphListeners;
|
|
|
|
_sceneGraphListeners = nullptr;
|
2013-10-21 17:22:42 +08:00
|
|
|
}
|
2013-10-24 17:27:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::EventListenerVector::clearFixedListeners()
|
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
if (_fixedListeners)
|
2013-10-21 17:22:42 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
_fixedListeners->clear();
|
|
|
|
delete _fixedListeners;
|
|
|
|
_fixedListeners = nullptr;
|
2013-10-21 17:22:42 +08:00
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2013-10-24 17:27:22 +08:00
|
|
|
void EventDispatcher::EventListenerVector::clear()
|
|
|
|
{
|
|
|
|
clearSceneGraphListeners();
|
|
|
|
clearFixedListeners();
|
|
|
|
}
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
|
2013-09-20 22:23:13 +08:00
|
|
|
EventDispatcher::EventDispatcher()
|
|
|
|
: _inDispatch(0)
|
2014-04-08 15:08:34 +08:00
|
|
|
, _isEnabled(false)
|
2013-10-25 17:03:50 +08:00
|
|
|
, _nodePriorityIndex(0)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
|
|
|
_toAddedListeners.reserve(50);
|
2015-10-19 14:07:50 +08:00
|
|
|
_toRemovedListeners.reserve(50);
|
2014-02-28 15:09:53 +08:00
|
|
|
|
|
|
|
// fixed #4129: Mark the following listener IDs for internal use.
|
|
|
|
// Therefore, internal listeners would not be cleaned when removeAllEventListeners is invoked.
|
|
|
|
_internalCustomListenerIDs.insert(EVENT_COME_TO_FOREGROUND);
|
|
|
|
_internalCustomListenerIDs.insert(EVENT_COME_TO_BACKGROUND);
|
2014-07-09 23:03:04 +08:00
|
|
|
_internalCustomListenerIDs.insert(EVENT_RENDERER_RECREATED);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EventDispatcher::~EventDispatcher()
|
|
|
|
{
|
2014-02-28 15:09:53 +08:00
|
|
|
// Clear internal custom listener IDs from set,
|
|
|
|
// so removeAllEventListeners would clean internal custom listeners.
|
|
|
|
_internalCustomListenerIDs.clear();
|
2013-10-25 16:34:26 +08:00
|
|
|
removeAllEventListeners();
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2014-01-21 10:23:05 +08:00
|
|
|
void EventDispatcher::visitTarget(Node* node, bool isRootNode)
|
2015-07-01 14:39:35 +08:00
|
|
|
{
|
|
|
|
node->sortAllChildren();
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
int i = 0;
|
2013-11-29 10:37:40 +08:00
|
|
|
auto& children = node->getChildren();
|
2013-11-28 17:57:13 +08:00
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
auto childrenCount = children.size();
|
2013-10-23 16:14:03 +08:00
|
|
|
|
|
|
|
if(childrenCount > 0)
|
|
|
|
{
|
|
|
|
Node* child = nullptr;
|
|
|
|
// visit children zOrder < 0
|
|
|
|
for( ; i < childrenCount; i++ )
|
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
child = children.at(i);
|
2013-10-23 16:14:03 +08:00
|
|
|
|
2014-01-20 15:03:30 +08:00
|
|
|
if ( child && child->getLocalZOrder() < 0 )
|
2014-01-21 10:23:05 +08:00
|
|
|
visitTarget(child, false);
|
2013-10-23 16:14:03 +08:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-01-21 10:23:05 +08:00
|
|
|
if (_nodeListenersMap.find(node) != _nodeListenersMap.end())
|
|
|
|
{
|
|
|
|
_globalZOrderNodeMap[node->getGlobalZOrder()].push_back(node);
|
|
|
|
}
|
2013-10-23 16:14:03 +08:00
|
|
|
|
|
|
|
for( ; i < childrenCount; i++ )
|
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
child = children.at(i);
|
2013-10-23 16:14:03 +08:00
|
|
|
if (child)
|
2014-01-21 10:23:05 +08:00
|
|
|
visitTarget(child, false);
|
2013-10-23 16:14:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-21 10:23:05 +08:00
|
|
|
if (_nodeListenersMap.find(node) != _nodeListenersMap.end())
|
|
|
|
{
|
|
|
|
_globalZOrderNodeMap[node->getGlobalZOrder()].push_back(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isRootNode)
|
|
|
|
{
|
2014-01-22 08:36:19 +08:00
|
|
|
std::vector<float> globalZOrders;
|
2014-01-21 10:23:05 +08:00
|
|
|
globalZOrders.reserve(_globalZOrderNodeMap.size());
|
|
|
|
|
|
|
|
for (const auto& e : _globalZOrderNodeMap)
|
|
|
|
{
|
|
|
|
globalZOrders.push_back(e.first);
|
|
|
|
}
|
|
|
|
|
2014-01-22 08:36:19 +08:00
|
|
|
std::sort(globalZOrders.begin(), globalZOrders.end(), [](const float a, const float b){
|
2014-01-21 10:23:05 +08:00
|
|
|
return a < b;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const auto& globalZ : globalZOrders)
|
|
|
|
{
|
|
|
|
for (const auto& n : _globalZOrderNodeMap[globalZ])
|
|
|
|
{
|
|
|
|
_nodePriorityMap[n] = ++_nodePriorityIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_globalZOrderNodeMap.clear();
|
2013-10-23 16:14:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
void EventDispatcher::pauseEventListenersForTarget(Node* target, bool recursive/* = false */)
|
2013-10-23 16:14:03 +08:00
|
|
|
{
|
2014-03-02 15:54:36 +08:00
|
|
|
auto listenerIter = _nodeListenersMap.find(target);
|
2013-10-23 16:14:03 +08:00
|
|
|
if (listenerIter != _nodeListenersMap.end())
|
|
|
|
{
|
|
|
|
auto listeners = listenerIter->second;
|
|
|
|
for (auto& l : *listeners)
|
|
|
|
{
|
2013-10-28 16:00:01 +08:00
|
|
|
l->setPaused(true);
|
2013-10-23 16:14:03 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-14 17:58:55 +08:00
|
|
|
|
2014-05-15 09:19:59 +08:00
|
|
|
for (auto& listener : _toAddedListeners)
|
2014-05-14 17:58:55 +08:00
|
|
|
{
|
2014-05-15 09:19:59 +08:00
|
|
|
if (listener->getAssociatedNode() == target)
|
2014-05-14 17:58:55 +08:00
|
|
|
{
|
2014-05-15 09:19:59 +08:00
|
|
|
listener->setPaused(true);
|
2014-05-14 17:58:55 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-02 15:54:36 +08:00
|
|
|
|
|
|
|
if (recursive)
|
|
|
|
{
|
|
|
|
const auto& children = target->getChildren();
|
|
|
|
for (const auto& child : children)
|
|
|
|
{
|
|
|
|
pauseEventListenersForTarget(child, true);
|
|
|
|
}
|
|
|
|
}
|
2013-10-23 16:14:03 +08:00
|
|
|
}
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
void EventDispatcher::resumeEventListenersForTarget(Node* target, bool recursive/* = false */)
|
2013-10-23 16:14:03 +08:00
|
|
|
{
|
2014-03-02 15:54:36 +08:00
|
|
|
auto listenerIter = _nodeListenersMap.find(target);
|
2013-10-23 16:14:03 +08:00
|
|
|
if (listenerIter != _nodeListenersMap.end())
|
|
|
|
{
|
|
|
|
auto listeners = listenerIter->second;
|
|
|
|
for (auto& l : *listeners)
|
|
|
|
{
|
2013-10-28 16:00:01 +08:00
|
|
|
l->setPaused(false);
|
2013-10-23 16:14:03 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-14 17:58:55 +08:00
|
|
|
|
2014-05-15 09:19:59 +08:00
|
|
|
for (auto& listener : _toAddedListeners)
|
2014-05-14 17:58:55 +08:00
|
|
|
{
|
2014-05-15 09:19:59 +08:00
|
|
|
if (listener->getAssociatedNode() == target)
|
2014-05-14 17:58:55 +08:00
|
|
|
{
|
2014-05-15 09:19:59 +08:00
|
|
|
listener->setPaused(false);
|
2014-05-14 17:58:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
setDirtyForNode(target);
|
|
|
|
|
|
|
|
if (recursive)
|
|
|
|
{
|
|
|
|
const auto& children = target->getChildren();
|
|
|
|
for (const auto& child : children)
|
|
|
|
{
|
|
|
|
resumeEventListenersForTarget(child, true);
|
|
|
|
}
|
|
|
|
}
|
2013-10-23 16:14:03 +08:00
|
|
|
}
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
void EventDispatcher::removeEventListenersForTarget(Node* target, bool recursive/* = false */)
|
2013-10-23 16:14:03 +08:00
|
|
|
{
|
2014-03-27 04:12:44 +08:00
|
|
|
// Ensure the node is removed from these immediately also.
|
|
|
|
// Don't want any dangling pointers or the possibility of dealing with deleted objects..
|
|
|
|
_nodePriorityMap.erase(target);
|
|
|
|
_dirtyNodes.erase(target);
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
auto listenerIter = _nodeListenersMap.find(target);
|
2013-10-23 16:14:03 +08:00
|
|
|
if (listenerIter != _nodeListenersMap.end())
|
|
|
|
{
|
|
|
|
auto listeners = listenerIter->second;
|
2013-10-23 17:13:03 +08:00
|
|
|
auto listenersCopy = *listeners;
|
|
|
|
for (auto& l : listenersCopy)
|
2013-10-23 16:14:03 +08:00
|
|
|
{
|
|
|
|
removeEventListener(l);
|
|
|
|
}
|
|
|
|
}
|
2014-03-02 15:54:36 +08:00
|
|
|
|
2014-04-08 07:19:26 +08:00
|
|
|
// Bug fix: ensure there are no references to the node in the list of listeners to be added.
|
|
|
|
// If we find any listeners associated with the destroyed node in this list then remove them.
|
|
|
|
// This is to catch the scenario where the node gets destroyed before it's listener
|
|
|
|
// is added into the event dispatcher fully. This could happen if a node registers a listener
|
|
|
|
// and gets destroyed while we are dispatching an event (touch etc.)
|
|
|
|
for (auto iter = _toAddedListeners.begin(); iter != _toAddedListeners.end(); )
|
|
|
|
{
|
|
|
|
EventListener * listener = *iter;
|
|
|
|
|
2014-04-15 12:50:33 +08:00
|
|
|
if (listener->getAssociatedNode() == target)
|
2014-04-08 07:19:26 +08:00
|
|
|
{
|
2014-04-15 12:50:33 +08:00
|
|
|
listener->setAssociatedNode(nullptr); // Ensure no dangling ptr to the target node.
|
2014-04-08 11:17:38 +08:00
|
|
|
listener->setRegistered(false);
|
2014-04-08 07:19:26 +08:00
|
|
|
listener->release();
|
|
|
|
iter = _toAddedListeners.erase(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
if (recursive)
|
|
|
|
{
|
|
|
|
const auto& children = target->getChildren();
|
|
|
|
for (const auto& child : children)
|
|
|
|
{
|
|
|
|
removeEventListenersForTarget(child, true);
|
|
|
|
}
|
|
|
|
}
|
2013-10-23 16:14:03 +08:00
|
|
|
}
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
void EventDispatcher::associateNodeAndEventListener(Node* node, EventListener* listener)
|
|
|
|
{
|
|
|
|
std::vector<EventListener*>* listeners = nullptr;
|
|
|
|
auto found = _nodeListenersMap.find(node);
|
|
|
|
if (found != _nodeListenersMap.end())
|
|
|
|
{
|
|
|
|
listeners = found->second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
listeners = new (std::nothrow) std::vector<EventListener*>();
|
2014-01-04 10:28:09 +08:00
|
|
|
_nodeListenersMap.insert(std::make_pair(node, listeners));
|
2013-10-23 11:27:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
listeners->push_back(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::dissociateNodeAndEventListener(Node* node, EventListener* listener)
|
|
|
|
{
|
|
|
|
std::vector<EventListener*>* listeners = nullptr;
|
|
|
|
auto found = _nodeListenersMap.find(node);
|
|
|
|
if (found != _nodeListenersMap.end())
|
|
|
|
{
|
|
|
|
listeners = found->second;
|
|
|
|
auto iter = std::find(listeners->begin(), listeners->end(), listener);
|
|
|
|
if (iter != listeners->end())
|
|
|
|
{
|
|
|
|
listeners->erase(iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listeners->empty())
|
|
|
|
{
|
|
|
|
_nodeListenersMap.erase(found);
|
2013-10-24 17:27:22 +08:00
|
|
|
delete listeners;
|
2013-10-23 11:27:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::addEventListener(EventListener* listener)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
|
|
|
if (_inDispatch == 0)
|
|
|
|
{
|
2014-01-04 10:28:09 +08:00
|
|
|
forceAddEventListener(listener);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_toAddedListeners.push_back(listener);
|
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2014-01-04 10:28:09 +08:00
|
|
|
listener->retain();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::forceAddEventListener(EventListener* listener)
|
|
|
|
{
|
|
|
|
EventListenerVector* listeners = nullptr;
|
|
|
|
EventListener::ListenerID listenerID = listener->getListenerID();
|
2014-02-28 12:01:47 +08:00
|
|
|
auto itr = _listenerMap.find(listenerID);
|
|
|
|
if (itr == _listenerMap.end())
|
2014-01-04 10:28:09 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
listeners = new (std::nothrow) EventListenerVector();
|
2014-02-28 12:01:47 +08:00
|
|
|
_listenerMap.insert(std::make_pair(listenerID, listeners));
|
2014-01-04 10:28:09 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
listeners = itr->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners->push_back(listener);
|
|
|
|
|
|
|
|
if (listener->getFixedPriority() == 0)
|
|
|
|
{
|
|
|
|
setDirty(listenerID, DirtyFlag::SCENE_GRAPH_PRIORITY);
|
|
|
|
|
2014-04-15 12:50:33 +08:00
|
|
|
auto node = listener->getAssociatedNode();
|
2014-01-04 10:28:09 +08:00
|
|
|
CCASSERT(node != nullptr, "Invalid scene graph priority!");
|
|
|
|
|
|
|
|
associateNodeAndEventListener(node, listener);
|
|
|
|
|
|
|
|
if (node->isRunning())
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
2014-03-02 15:54:36 +08:00
|
|
|
resumeEventListenersForTarget(node);
|
2013-10-23 11:27:24 +08:00
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-20 14:22:30 +08:00
|
|
|
setDirty(listenerID, DirtyFlag::FIXED_PRIORITY);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)
|
|
|
|
{
|
|
|
|
CCASSERT(listener && node, "Invalid parameters.");
|
2013-10-28 16:00:01 +08:00
|
|
|
CCASSERT(!listener->isRegistered(), "The listener has been registered.");
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-12 11:25:28 +08:00
|
|
|
if (!listener->checkAvailable())
|
2013-09-20 22:23:13 +08:00
|
|
|
return;
|
|
|
|
|
2014-04-15 12:50:33 +08:00
|
|
|
listener->setAssociatedNode(node);
|
2013-10-28 16:00:01 +08:00
|
|
|
listener->setFixedPriority(0);
|
|
|
|
listener->setRegistered(true);
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
addEventListener(listener);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2014-03-28 03:29:28 +08:00
|
|
|
#if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS && COCOS2D_DEBUG > 0
|
2014-03-27 04:12:44 +08:00
|
|
|
|
|
|
|
void EventDispatcher::debugCheckNodeHasNoEventListenersOnDestruction(Node* node)
|
|
|
|
{
|
|
|
|
// Check the listeners map
|
|
|
|
for (const auto & keyValuePair : _listenerMap)
|
|
|
|
{
|
|
|
|
const EventListenerVector * eventListenerVector = keyValuePair.second;
|
|
|
|
|
|
|
|
if (eventListenerVector)
|
|
|
|
{
|
|
|
|
if (eventListenerVector->getSceneGraphPriorityListeners())
|
|
|
|
{
|
|
|
|
for (EventListener * listener : *eventListenerVector->getSceneGraphPriorityListeners())
|
|
|
|
{
|
|
|
|
CCASSERT(!listener ||
|
2014-04-15 12:50:33 +08:00
|
|
|
listener->getAssociatedNode() != node,
|
2014-03-27 04:12:44 +08:00
|
|
|
"Node should have no event listeners registered for it upon destruction!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the node listeners map
|
|
|
|
for (const auto & keyValuePair : _nodeListenersMap)
|
|
|
|
{
|
|
|
|
CCASSERT(keyValuePair.first != node, "Node should have no event listeners registered for it upon destruction!");
|
|
|
|
|
|
|
|
if (keyValuePair.second)
|
|
|
|
{
|
|
|
|
for (EventListener * listener : *keyValuePair.second)
|
|
|
|
{
|
2014-04-15 12:50:33 +08:00
|
|
|
CCASSERT(listener->getAssociatedNode() != node,
|
2014-03-27 04:12:44 +08:00
|
|
|
"Node should have no event listeners registered for it upon destruction!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the node priority map
|
|
|
|
for (const auto & keyValuePair : _nodePriorityMap)
|
|
|
|
{
|
|
|
|
CCASSERT(keyValuePair.first != node,
|
|
|
|
"Node should have no event listeners registered for it upon destruction!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the to be added list
|
|
|
|
for (EventListener * listener : _toAddedListeners)
|
|
|
|
{
|
2014-04-15 12:50:33 +08:00
|
|
|
CCASSERT(listener->getAssociatedNode() != node,
|
2014-03-27 04:12:44 +08:00
|
|
|
"Node should have no event listeners registered for it upon destruction!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the dirty nodes set
|
|
|
|
for (Node * dirtyNode : _dirtyNodes)
|
|
|
|
{
|
|
|
|
CCASSERT(dirtyNode != node,
|
|
|
|
"Node should have no event listeners registered for it upon destruction!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 03:29:28 +08:00
|
|
|
#endif // #if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS && COCOS2D_DEBUG > 0
|
2014-03-27 04:12:44 +08:00
|
|
|
|
|
|
|
|
2013-09-20 22:23:13 +08:00
|
|
|
void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)
|
|
|
|
{
|
|
|
|
CCASSERT(listener, "Invalid parameters.");
|
2013-10-28 16:00:01 +08:00
|
|
|
CCASSERT(!listener->isRegistered(), "The listener has been registered.");
|
2013-09-20 22:23:13 +08:00
|
|
|
CCASSERT(fixedPriority != 0, "0 priority is forbidden for fixed priority since it's used for scene graph based priority.");
|
|
|
|
|
2013-10-12 11:25:28 +08:00
|
|
|
if (!listener->checkAvailable())
|
2013-09-20 22:23:13 +08:00
|
|
|
return;
|
|
|
|
|
2014-04-15 12:50:33 +08:00
|
|
|
listener->setAssociatedNode(nullptr);
|
2013-10-28 16:00:01 +08:00
|
|
|
listener->setFixedPriority(fixedPriority);
|
|
|
|
listener->setRegistered(true);
|
|
|
|
listener->setPaused(false);
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
addEventListener(listener);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
EventListenerCustom* EventDispatcher::addCustomEventListener(const std::string &eventName, const std::function<void(EventCustom*)>& callback)
|
2013-12-21 16:56:28 +08:00
|
|
|
{
|
|
|
|
EventListenerCustom *listener = EventListenerCustom::create(eventName, callback);
|
|
|
|
addEventListenerWithFixedPriority(listener, 1);
|
|
|
|
return listener;
|
|
|
|
}
|
|
|
|
|
2013-09-20 22:23:13 +08:00
|
|
|
void EventDispatcher::removeEventListener(EventListener* listener)
|
|
|
|
{
|
|
|
|
if (listener == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool isFound = false;
|
2014-03-02 15:54:36 +08:00
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
auto removeListenerInVector = [&](std::vector<EventListener*>* listeners){
|
|
|
|
if (listeners == nullptr)
|
|
|
|
return;
|
2013-10-21 17:22:42 +08:00
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
for (auto iter = listeners->begin(); iter != listeners->end(); ++iter)
|
|
|
|
{
|
|
|
|
auto l = *iter;
|
|
|
|
if (l == listener)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-24 11:17:29 +08:00
|
|
|
CC_SAFE_RETAIN(l);
|
2013-10-28 16:00:01 +08:00
|
|
|
l->setRegistered(false);
|
2014-04-15 12:50:33 +08:00
|
|
|
if (l->getAssociatedNode() != nullptr)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2014-04-15 12:50:33 +08:00
|
|
|
dissociateNodeAndEventListener(l->getAssociatedNode(), l);
|
2014-07-10 00:45:27 +08:00
|
|
|
l->setAssociatedNode(nullptr); // nullptr out the node pointer so we don't have any dangling pointers to destroyed nodes.
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_inDispatch == 0)
|
|
|
|
{
|
2015-10-19 14:07:50 +08:00
|
|
|
iter = listeners->erase(iter);
|
2014-03-28 06:14:37 +08:00
|
|
|
CC_SAFE_RELEASE(l);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
2015-10-19 14:07:50 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_toRemovedListeners.push_back(l);
|
|
|
|
}
|
2014-03-28 06:22:11 +08:00
|
|
|
|
2013-09-20 22:23:13 +08:00
|
|
|
isFound = true;
|
2013-10-23 11:27:24 +08:00
|
|
|
break;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
2013-10-23 11:27:24 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-28 12:01:47 +08:00
|
|
|
for (auto iter = _listenerMap.begin(); iter != _listenerMap.end();)
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
|
|
|
auto listeners = iter->second;
|
|
|
|
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
|
|
|
|
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
|
2013-10-24 11:17:29 +08:00
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
removeListenerInVector(sceneGraphPriorityListeners);
|
2014-03-02 16:16:03 +08:00
|
|
|
if (isFound)
|
|
|
|
{
|
|
|
|
// fixed #4160: Dirty flag need to be updated after listeners were removed.
|
|
|
|
setDirty(listener->getListenerID(), DirtyFlag::SCENE_GRAPH_PRIORITY);
|
|
|
|
}
|
|
|
|
else
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
|
|
|
removeListenerInVector(fixedPriorityListeners);
|
2014-03-02 16:16:03 +08:00
|
|
|
if (isFound)
|
|
|
|
{
|
|
|
|
setDirty(listener->getListenerID(), DirtyFlag::FIXED_PRIORITY);
|
|
|
|
}
|
2013-10-23 11:27:24 +08:00
|
|
|
}
|
2014-03-27 04:12:44 +08:00
|
|
|
|
2014-03-28 06:39:52 +08:00
|
|
|
#if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS
|
2014-03-27 04:12:44 +08:00
|
|
|
CCASSERT(_inDispatch != 0 ||
|
|
|
|
!sceneGraphPriorityListeners ||
|
|
|
|
std::count(sceneGraphPriorityListeners->begin(), sceneGraphPriorityListeners->end(), listener) == 0,
|
|
|
|
"Listener should be in no lists after this is done if we're not currently in dispatch mode.");
|
|
|
|
|
|
|
|
CCASSERT(_inDispatch != 0 ||
|
|
|
|
!fixedPriorityListeners ||
|
|
|
|
std::count(fixedPriorityListeners->begin(), fixedPriorityListeners->end(), listener) == 0,
|
2014-03-28 06:39:52 +08:00
|
|
|
"Listener should be in no lists after this is done if we're not currently in dispatch mode.");
|
|
|
|
#endif
|
2013-09-20 22:23:13 +08:00
|
|
|
|
|
|
|
if (iter->second->empty())
|
|
|
|
{
|
2013-10-28 16:00:01 +08:00
|
|
|
_priorityDirtyFlagMap.erase(listener->getListenerID());
|
2013-09-20 22:23:13 +08:00
|
|
|
auto list = iter->second;
|
2014-02-28 12:01:47 +08:00
|
|
|
iter = _listenerMap.erase(iter);
|
2013-09-20 22:23:13 +08:00
|
|
|
CC_SAFE_DELETE(list);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFound)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-02 22:08:08 +08:00
|
|
|
if (isFound)
|
|
|
|
{
|
2013-09-20 22:23:13 +08:00
|
|
|
CC_SAFE_RELEASE(listener);
|
2013-11-02 22:08:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(auto iter = _toAddedListeners.begin(); iter != _toAddedListeners.end(); ++iter)
|
|
|
|
{
|
|
|
|
if (*iter == listener)
|
2013-11-02 15:03:54 +08:00
|
|
|
{
|
2014-04-07 12:59:40 +08:00
|
|
|
listener->setRegistered(false);
|
2014-01-02 19:59:10 +08:00
|
|
|
listener->release();
|
2013-11-02 15:03:54 +08:00
|
|
|
_toAddedListeners.erase(iter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::setPriority(EventListener* listener, int fixedPriority)
|
|
|
|
{
|
|
|
|
if (listener == nullptr)
|
|
|
|
return;
|
|
|
|
|
2014-02-28 12:01:47 +08:00
|
|
|
for (auto iter = _listenerMap.begin(); iter != _listenerMap.end(); ++iter)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
auto fixedPriorityListeners = iter->second->getFixedPriorityListeners();
|
|
|
|
if (fixedPriorityListeners)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
auto found = std::find(fixedPriorityListeners->begin(), fixedPriorityListeners->end(), listener);
|
|
|
|
if (found != fixedPriorityListeners->end())
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2014-04-15 12:50:33 +08:00
|
|
|
CCASSERT(listener->getAssociatedNode() == nullptr, "Can't set fixed priority with scene graph based listener.");
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-28 16:00:01 +08:00
|
|
|
if (listener->getFixedPriority() != fixedPriority)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-28 16:00:01 +08:00
|
|
|
listener->setFixedPriority(fixedPriority);
|
2014-01-20 14:22:30 +08:00
|
|
|
setDirty(listener->getListenerID(), DirtyFlag::FIXED_PRIORITY);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
void EventDispatcher::dispatchEventToListeners(EventListenerVector* listeners, const std::function<bool(EventListener*)>& onEvent)
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
|
|
|
bool shouldStopPropagation = false;
|
|
|
|
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
|
|
|
|
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
|
|
|
|
|
2013-12-27 15:06:16 +08:00
|
|
|
ssize_t i = 0;
|
2013-10-23 11:27:24 +08:00
|
|
|
// priority < 0
|
|
|
|
if (fixedPriorityListeners)
|
|
|
|
{
|
2014-03-24 10:12:40 +08:00
|
|
|
CCASSERT(listeners->getGt0Index() <= static_cast<ssize_t>(fixedPriorityListeners->size()), "Out of range exception!");
|
2014-03-02 16:16:03 +08:00
|
|
|
|
|
|
|
if (!fixedPriorityListeners->empty())
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
2014-03-02 16:16:03 +08:00
|
|
|
for (; i < listeners->getGt0Index(); ++i)
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
2014-03-02 16:16:03 +08:00
|
|
|
auto l = fixedPriorityListeners->at(i);
|
2014-04-03 15:32:16 +08:00
|
|
|
if (l->isEnabled() && !l->isPaused() && l->isRegistered() && onEvent(l))
|
2014-03-02 16:16:03 +08:00
|
|
|
{
|
|
|
|
shouldStopPropagation = true;
|
|
|
|
break;
|
|
|
|
}
|
2013-10-23 11:27:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sceneGraphPriorityListeners)
|
|
|
|
{
|
|
|
|
if (!shouldStopPropagation)
|
|
|
|
{
|
|
|
|
// priority == 0, scene graph priority
|
2013-10-24 11:17:29 +08:00
|
|
|
for (auto& l : *sceneGraphPriorityListeners)
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
2014-04-03 15:32:16 +08:00
|
|
|
if (l->isEnabled() && !l->isPaused() && l->isRegistered() && onEvent(l))
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
|
|
|
shouldStopPropagation = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-18 10:23:52 +08:00
|
|
|
|
|
|
|
if (fixedPriorityListeners)
|
|
|
|
{
|
|
|
|
if (!shouldStopPropagation)
|
|
|
|
{
|
|
|
|
// priority > 0
|
|
|
|
ssize_t size = fixedPriorityListeners->size();
|
|
|
|
for (; i < size; ++i)
|
|
|
|
{
|
|
|
|
auto l = fixedPriorityListeners->at(i);
|
|
|
|
|
|
|
|
if (l->isEnabled() && !l->isPaused() && l->isRegistered() && onEvent(l))
|
|
|
|
{
|
|
|
|
shouldStopPropagation = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-23 11:27:24 +08:00
|
|
|
|
2015-06-18 10:23:52 +08:00
|
|
|
void EventDispatcher::dispatchTouchEventToListeners(EventListenerVector* listeners, const std::function<bool(EventListener*)>& onEvent)
|
|
|
|
{
|
|
|
|
bool shouldStopPropagation = false;
|
|
|
|
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
|
|
|
|
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
|
|
|
|
|
|
|
|
ssize_t i = 0;
|
|
|
|
// priority < 0
|
|
|
|
if (fixedPriorityListeners)
|
|
|
|
{
|
|
|
|
CCASSERT(listeners->getGt0Index() <= static_cast<ssize_t>(fixedPriorityListeners->size()), "Out of range exception!");
|
|
|
|
|
|
|
|
if (!fixedPriorityListeners->empty())
|
|
|
|
{
|
|
|
|
for (; i < listeners->getGt0Index(); ++i)
|
|
|
|
{
|
|
|
|
auto l = fixedPriorityListeners->at(i);
|
|
|
|
if (l->isEnabled() && !l->isPaused() && l->isRegistered() && onEvent(l))
|
|
|
|
{
|
|
|
|
shouldStopPropagation = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 16:41:03 +08:00
|
|
|
auto scene = Director::getInstance()->getRunningScene();
|
|
|
|
if (scene && sceneGraphPriorityListeners)
|
2015-06-18 10:23:52 +08:00
|
|
|
{
|
|
|
|
if (!shouldStopPropagation)
|
|
|
|
{
|
|
|
|
// priority == 0, scene graph priority
|
|
|
|
|
|
|
|
// first, get all enabled, unPaused and registered listeners
|
|
|
|
std::vector<EventListener*> sceneListeners;
|
|
|
|
for (auto& l : *sceneGraphPriorityListeners)
|
|
|
|
{
|
|
|
|
if (l->isEnabled() && !l->isPaused() && l->isRegistered())
|
|
|
|
{
|
|
|
|
sceneListeners.push_back(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// second, for all camera call all listeners
|
2015-09-22 16:08:23 +08:00
|
|
|
// get a copy of cameras, prevent it's been modified in listener callback
|
|
|
|
// if camera's depth is greater, process it earlier
|
2015-07-24 16:41:03 +08:00
|
|
|
auto cameras = scene->getCameras();
|
2015-06-18 10:23:52 +08:00
|
|
|
Camera* camera;
|
|
|
|
for (int j = int(cameras.size()) - 1; j >= 0; --j)
|
|
|
|
{
|
|
|
|
camera = cameras[j];
|
|
|
|
if (camera->isVisible() == false)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Camera::_visitingCamera = camera;
|
2015-09-16 11:24:55 +08:00
|
|
|
auto cameraFlag = (unsigned short)camera->getCameraFlag();
|
2015-06-18 10:23:52 +08:00
|
|
|
for (auto& l : sceneListeners)
|
|
|
|
{
|
2015-09-22 14:52:16 +08:00
|
|
|
if (nullptr == l->getAssociatedNode() || 0 == (l->getAssociatedNode()->getCameraMask() & cameraFlag))
|
2015-09-16 11:24:55 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-18 10:23:52 +08:00
|
|
|
if (onEvent(l))
|
|
|
|
{
|
|
|
|
shouldStopPropagation = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shouldStopPropagation)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Camera::_visitingCamera = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
if (fixedPriorityListeners)
|
|
|
|
{
|
|
|
|
if (!shouldStopPropagation)
|
|
|
|
{
|
|
|
|
// priority > 0
|
2013-12-27 15:06:16 +08:00
|
|
|
ssize_t size = fixedPriorityListeners->size();
|
|
|
|
for (; i < size; ++i)
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
|
|
|
auto l = fixedPriorityListeners->at(i);
|
|
|
|
|
2014-04-03 15:32:16 +08:00
|
|
|
if (l->isEnabled() && !l->isPaused() && l->isRegistered() && onEvent(l))
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
|
|
|
shouldStopPropagation = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::dispatchEvent(Event* event)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
|
|
|
if (!_isEnabled)
|
|
|
|
return;
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
updateDirtyFlagForSceneGraph();
|
|
|
|
|
|
|
|
|
2013-10-25 10:35:48 +08:00
|
|
|
DispatchGuard guard(_inDispatch);
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-28 16:00:01 +08:00
|
|
|
if (event->getType() == Event::Type::TOUCH)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-25 10:35:48 +08:00
|
|
|
dispatchTouchEvent(static_cast<EventTouch*>(event));
|
|
|
|
return;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
2013-10-23 11:27:24 +08:00
|
|
|
|
2013-12-20 19:31:08 +08:00
|
|
|
auto listenerID = __getListenerID(event);
|
2013-10-28 16:00:01 +08:00
|
|
|
|
|
|
|
sortEventListeners(listenerID);
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2015-06-18 10:23:52 +08:00
|
|
|
auto pfnDispatchEventToListeners = &EventDispatcher::dispatchEventToListeners;
|
|
|
|
if (event->getType() == Event::Type::MOUSE) {
|
|
|
|
pfnDispatchEventToListeners = &EventDispatcher::dispatchTouchEventToListeners;
|
|
|
|
}
|
2014-02-28 12:01:47 +08:00
|
|
|
auto iter = _listenerMap.find(listenerID);
|
|
|
|
if (iter != _listenerMap.end())
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
auto listeners = iter->second;
|
|
|
|
|
|
|
|
auto onEvent = [&event](EventListener* listener) -> bool{
|
2014-04-15 12:50:33 +08:00
|
|
|
event->setCurrentTarget(listener->getAssociatedNode());
|
2013-10-23 11:27:24 +08:00
|
|
|
listener->_onEvent(event);
|
|
|
|
return event->isStopped();
|
|
|
|
};
|
|
|
|
|
2015-06-18 10:23:52 +08:00
|
|
|
(this->*pfnDispatchEventToListeners)(listeners, onEvent);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2013-10-28 16:00:01 +08:00
|
|
|
updateListeners(event);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-22 02:55:16 +08:00
|
|
|
void EventDispatcher::dispatchCustomEvent(const std::string &eventName, void *optionalUserData)
|
|
|
|
{
|
|
|
|
EventCustom ev(eventName);
|
|
|
|
ev.setUserData(optionalUserData);
|
|
|
|
dispatchEvent(&ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-20 22:23:13 +08:00
|
|
|
void EventDispatcher::dispatchTouchEvent(EventTouch* event)
|
|
|
|
{
|
2013-12-20 19:31:08 +08:00
|
|
|
sortEventListeners(EventListenerTouchOneByOne::LISTENER_ID);
|
|
|
|
sortEventListeners(EventListenerTouchAllAtOnce::LISTENER_ID);
|
2013-10-23 11:27:24 +08:00
|
|
|
|
2014-02-10 11:15:56 +08:00
|
|
|
auto oneByOneListeners = getListeners(EventListenerTouchOneByOne::LISTENER_ID);
|
|
|
|
auto allAtOnceListeners = getListeners(EventListenerTouchAllAtOnce::LISTENER_ID);
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-12 11:25:28 +08:00
|
|
|
// If there aren't any touch listeners, return directly.
|
2014-02-10 11:15:56 +08:00
|
|
|
if (nullptr == oneByOneListeners && nullptr == allAtOnceListeners)
|
2013-10-12 11:25:28 +08:00
|
|
|
return;
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2014-02-10 11:15:56 +08:00
|
|
|
bool isNeedsMutableSet = (oneByOneListeners && allAtOnceListeners);
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2014-02-28 13:43:28 +08:00
|
|
|
const std::vector<Touch*>& originalTouches = event->getTouches();
|
2014-02-10 11:15:56 +08:00
|
|
|
std::vector<Touch*> mutableTouches(originalTouches.size());
|
|
|
|
std::copy(originalTouches.begin(), originalTouches.end(), mutableTouches.begin());
|
2013-10-23 11:27:24 +08:00
|
|
|
|
2013-09-20 22:23:13 +08:00
|
|
|
//
|
|
|
|
// process the target handlers 1st
|
|
|
|
//
|
2014-02-10 11:15:56 +08:00
|
|
|
if (oneByOneListeners)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
|
|
|
auto mutableTouchesIter = mutableTouches.begin();
|
2014-02-10 11:15:56 +08:00
|
|
|
auto touchesIter = originalTouches.begin();
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2014-02-10 11:15:56 +08:00
|
|
|
for (; touchesIter != originalTouches.end(); ++touchesIter)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
|
|
|
bool isSwallowed = false;
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
auto onTouchEvent = [&](EventListener* l) -> bool { // Return true to break
|
|
|
|
EventListenerTouchOneByOne* listener = static_cast<EventListenerTouchOneByOne*>(l);
|
|
|
|
|
2013-09-20 22:23:13 +08:00
|
|
|
// Skip if the listener was removed.
|
2013-10-23 11:27:24 +08:00
|
|
|
if (!listener->_isRegistered)
|
|
|
|
return false;
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
event->setCurrentTarget(listener->_node);
|
2013-09-20 22:23:13 +08:00
|
|
|
|
|
|
|
bool isClaimed = false;
|
|
|
|
std::vector<Touch*>::iterator removedIter;
|
|
|
|
|
|
|
|
EventTouch::EventCode eventCode = event->getEventCode();
|
|
|
|
|
|
|
|
if (eventCode == EventTouch::EventCode::BEGAN)
|
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
if (listener->onTouchBegan)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
isClaimed = listener->onTouchBegan(*touchesIter, event);
|
|
|
|
if (isClaimed && listener->_isRegistered)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
listener->_claimedTouches.push_back(*touchesIter);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-23 11:27:24 +08:00
|
|
|
else if (listener->_claimedTouches.size() > 0
|
|
|
|
&& ((removedIter = std::find(listener->_claimedTouches.begin(), listener->_claimedTouches.end(), *touchesIter)) != listener->_claimedTouches.end()))
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
|
|
|
isClaimed = true;
|
|
|
|
|
|
|
|
switch (eventCode)
|
|
|
|
{
|
|
|
|
case EventTouch::EventCode::MOVED:
|
2013-10-23 11:27:24 +08:00
|
|
|
if (listener->onTouchMoved)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
listener->onTouchMoved(*touchesIter, event);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventTouch::EventCode::ENDED:
|
2013-10-23 11:27:24 +08:00
|
|
|
if (listener->onTouchEnded)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
listener->onTouchEnded(*touchesIter, event);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
2013-10-23 11:27:24 +08:00
|
|
|
if (listener->_isRegistered)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
listener->_claimedTouches.erase(removedIter);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventTouch::EventCode::CANCELLED:
|
2013-10-23 11:27:24 +08:00
|
|
|
if (listener->onTouchCancelled)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
listener->onTouchCancelled(*touchesIter, event);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
2013-10-23 11:27:24 +08:00
|
|
|
if (listener->_isRegistered)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
listener->_claimedTouches.erase(removedIter);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CCASSERT(false, "The eventcode is invalid.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the event was stopped, return directly.
|
|
|
|
if (event->isStopped())
|
|
|
|
{
|
2013-10-28 16:00:01 +08:00
|
|
|
updateListeners(event);
|
2013-10-23 11:27:24 +08:00
|
|
|
return true;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2015-07-14 15:28:36 +08:00
|
|
|
CCASSERT((*touchesIter)->getID() == (*mutableTouchesIter)->getID(),
|
|
|
|
"touchesIter ID should be equal to mutableTouchesIter's ID.");
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
if (isClaimed && listener->_isRegistered && listener->_needSwallow)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
|
|
|
if (isNeedsMutableSet)
|
|
|
|
{
|
|
|
|
mutableTouchesIter = mutableTouches.erase(mutableTouchesIter);
|
|
|
|
isSwallowed = true;
|
|
|
|
}
|
2013-10-23 11:27:24 +08:00
|
|
|
return true;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
2013-10-23 11:27:24 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2015-06-18 10:23:52 +08:00
|
|
|
dispatchTouchEventToListeners(oneByOneListeners, onTouchEvent);
|
2013-10-23 11:27:24 +08:00
|
|
|
if (event->isStopped())
|
|
|
|
{
|
|
|
|
return;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isSwallowed)
|
|
|
|
++mutableTouchesIter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// process standard handlers 2nd
|
|
|
|
//
|
2014-02-10 11:15:56 +08:00
|
|
|
if (allAtOnceListeners && mutableTouches.size() > 0)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
|
|
|
|
auto onTouchesEvent = [&](EventListener* l) -> bool{
|
|
|
|
EventListenerTouchAllAtOnce* listener = static_cast<EventListenerTouchAllAtOnce*>(l);
|
2013-09-20 22:23:13 +08:00
|
|
|
// Skip if the listener was removed.
|
2013-10-23 11:27:24 +08:00
|
|
|
if (!listener->_isRegistered)
|
|
|
|
return false;
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
event->setCurrentTarget(listener->_node);
|
2013-09-20 22:23:13 +08:00
|
|
|
|
|
|
|
switch (event->getEventCode())
|
|
|
|
{
|
|
|
|
case EventTouch::EventCode::BEGAN:
|
2013-10-23 11:27:24 +08:00
|
|
|
if (listener->onTouchesBegan)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
listener->onTouchesBegan(mutableTouches, event);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventTouch::EventCode::MOVED:
|
2013-10-23 11:27:24 +08:00
|
|
|
if (listener->onTouchesMoved)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
listener->onTouchesMoved(mutableTouches, event);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventTouch::EventCode::ENDED:
|
2013-10-23 11:27:24 +08:00
|
|
|
if (listener->onTouchesEnded)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
listener->onTouchesEnded(mutableTouches, event);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventTouch::EventCode::CANCELLED:
|
2013-10-23 11:27:24 +08:00
|
|
|
if (listener->onTouchesCancelled)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
listener->onTouchesCancelled(mutableTouches, event);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CCASSERT(false, "The eventcode is invalid.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the event was stopped, return directly.
|
|
|
|
if (event->isStopped())
|
|
|
|
{
|
2013-10-28 16:00:01 +08:00
|
|
|
updateListeners(event);
|
2014-01-27 16:30:20 +08:00
|
|
|
return true;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
2013-10-23 11:27:24 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2015-06-18 10:23:52 +08:00
|
|
|
dispatchTouchEventToListeners(allAtOnceListeners, onTouchesEvent);
|
2013-10-23 11:27:24 +08:00
|
|
|
if (event->isStopped())
|
|
|
|
{
|
|
|
|
return;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-28 16:00:01 +08:00
|
|
|
updateListeners(event);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2013-10-28 16:00:01 +08:00
|
|
|
void EventDispatcher::updateListeners(Event* event)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2014-03-06 10:45:29 +08:00
|
|
|
CCASSERT(_inDispatch > 0, "If program goes here, there should be event in dispatch.");
|
2015-01-13 18:35:32 +08:00
|
|
|
|
|
|
|
if (_inDispatch > 1)
|
|
|
|
return;
|
|
|
|
|
2013-12-20 19:31:08 +08:00
|
|
|
auto onUpdateListeners = [this](const EventListener::ListenerID& listenerID)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2014-02-28 12:01:47 +08:00
|
|
|
auto listenersIter = _listenerMap.find(listenerID);
|
|
|
|
if (listenersIter == _listenerMap.end())
|
2013-10-25 10:35:48 +08:00
|
|
|
return;
|
2014-03-06 10:45:29 +08:00
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
auto listeners = listenersIter->second;
|
2014-03-06 10:45:29 +08:00
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
|
|
|
|
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
|
|
|
|
|
|
|
|
if (sceneGraphPriorityListeners)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
for (auto iter = sceneGraphPriorityListeners->begin(); iter != sceneGraphPriorityListeners->end();)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
auto l = *iter;
|
2014-03-28 06:22:11 +08:00
|
|
|
if (!l->isRegistered())
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
|
|
|
iter = sceneGraphPriorityListeners->erase(iter);
|
2015-10-19 14:07:50 +08:00
|
|
|
// if item in toRemove list, remove it from the list
|
|
|
|
auto matchIter = std::find(_toRemovedListeners.begin(), _toRemovedListeners.end(), l);
|
|
|
|
if (matchIter != _toRemovedListeners.end())
|
|
|
|
_toRemovedListeners.erase(matchIter);
|
2013-10-24 11:17:29 +08:00
|
|
|
l->release();
|
2013-10-23 11:27:24 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
2013-10-23 11:27:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fixedPriorityListeners)
|
|
|
|
{
|
|
|
|
for (auto iter = fixedPriorityListeners->begin(); iter != fixedPriorityListeners->end();)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
auto l = *iter;
|
2014-03-28 06:22:11 +08:00
|
|
|
if (!l->isRegistered())
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
|
|
|
iter = fixedPriorityListeners->erase(iter);
|
2015-10-19 14:07:50 +08:00
|
|
|
// if item in toRemove list, remove it from the list
|
|
|
|
auto matchIter = std::find(_toRemovedListeners.begin(), _toRemovedListeners.end(), l);
|
|
|
|
if (matchIter != _toRemovedListeners.end())
|
|
|
|
_toRemovedListeners.erase(matchIter);
|
2013-10-24 11:17:29 +08:00
|
|
|
l->release();
|
2013-10-23 11:27:24 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-24 17:27:22 +08:00
|
|
|
if (sceneGraphPriorityListeners && sceneGraphPriorityListeners->empty())
|
|
|
|
{
|
|
|
|
listeners->clearSceneGraphListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fixedPriorityListeners && fixedPriorityListeners->empty())
|
|
|
|
{
|
|
|
|
listeners->clearFixedListeners();
|
|
|
|
}
|
2013-10-25 10:35:48 +08:00
|
|
|
};
|
2014-03-06 10:45:29 +08:00
|
|
|
|
2013-10-28 16:00:01 +08:00
|
|
|
if (event->getType() == Event::Type::TOUCH)
|
2013-10-25 10:35:48 +08:00
|
|
|
{
|
2013-12-20 19:31:08 +08:00
|
|
|
onUpdateListeners(EventListenerTouchOneByOne::LISTENER_ID);
|
|
|
|
onUpdateListeners(EventListenerTouchAllAtOnce::LISTENER_ID);
|
2013-10-25 10:35:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-20 19:31:08 +08:00
|
|
|
onUpdateListeners(__getListenerID(event));
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2014-03-06 10:45:29 +08:00
|
|
|
CCASSERT(_inDispatch == 1, "_inDispatch should be 1 here.");
|
|
|
|
|
|
|
|
for (auto iter = _listenerMap.begin(); iter != _listenerMap.end();)
|
|
|
|
{
|
|
|
|
if (iter->second->empty())
|
|
|
|
{
|
|
|
|
_priorityDirtyFlagMap.erase(iter->first);
|
|
|
|
delete iter->second;
|
|
|
|
iter = _listenerMap.erase(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 10:35:48 +08:00
|
|
|
|
2013-09-20 22:23:13 +08:00
|
|
|
if (!_toAddedListeners.empty())
|
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
for (auto& listener : _toAddedListeners)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2014-01-04 10:28:09 +08:00
|
|
|
forceAddEventListener(listener);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
_toAddedListeners.clear();
|
|
|
|
}
|
2015-10-19 14:07:50 +08:00
|
|
|
|
|
|
|
if (!_toRemovedListeners.empty())
|
|
|
|
{
|
2015-10-19 16:06:39 +08:00
|
|
|
cleanToRemovedListeners();
|
2015-10-19 14:07:50 +08:00
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
void EventDispatcher::updateDirtyFlagForSceneGraph()
|
|
|
|
{
|
|
|
|
if (!_dirtyNodes.empty())
|
|
|
|
{
|
|
|
|
for (auto& node : _dirtyNodes)
|
|
|
|
{
|
|
|
|
auto iter = _nodeListenersMap.find(node);
|
|
|
|
if (iter != _nodeListenersMap.end())
|
|
|
|
{
|
|
|
|
for (auto& l : *iter->second)
|
|
|
|
{
|
2013-10-28 16:00:01 +08:00
|
|
|
setDirty(l->getListenerID(), DirtyFlag::SCENE_GRAPH_PRIORITY);
|
2013-10-23 11:27:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_dirtyNodes.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:31:08 +08:00
|
|
|
void EventDispatcher::sortEventListeners(const EventListener::ListenerID& listenerID)
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
2013-10-25 10:35:48 +08:00
|
|
|
DirtyFlag dirtyFlag = DirtyFlag::NONE;
|
2013-10-23 11:27:24 +08:00
|
|
|
|
2013-10-28 16:00:01 +08:00
|
|
|
auto dirtyIter = _priorityDirtyFlagMap.find(listenerID);
|
2013-10-25 10:35:48 +08:00
|
|
|
if (dirtyIter != _priorityDirtyFlagMap.end())
|
|
|
|
{
|
|
|
|
dirtyFlag = dirtyIter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dirtyFlag != DirtyFlag::NONE)
|
|
|
|
{
|
2014-04-08 15:08:34 +08:00
|
|
|
// Clear the dirty flag first, if `rootNode` is nullptr, then set its dirty flag of scene graph priority
|
|
|
|
dirtyIter->second = DirtyFlag::NONE;
|
|
|
|
|
2014-01-20 14:22:30 +08:00
|
|
|
if ((int)dirtyFlag & (int)DirtyFlag::FIXED_PRIORITY)
|
2013-10-25 10:35:48 +08:00
|
|
|
{
|
2013-10-28 16:00:01 +08:00
|
|
|
sortEventListenersOfFixedPriority(listenerID);
|
2013-10-25 10:35:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((int)dirtyFlag & (int)DirtyFlag::SCENE_GRAPH_PRIORITY)
|
|
|
|
{
|
2014-04-08 15:08:34 +08:00
|
|
|
auto rootNode = Director::getInstance()->getRunningScene();
|
|
|
|
if (rootNode)
|
|
|
|
{
|
|
|
|
sortEventListenersOfSceneGraphPriority(listenerID, rootNode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dirtyIter->second = DirtyFlag::SCENE_GRAPH_PRIORITY;
|
|
|
|
}
|
2013-10-25 10:35:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-08 15:08:34 +08:00
|
|
|
void EventDispatcher::sortEventListenersOfSceneGraphPriority(const EventListener::ListenerID& listenerID, Node* rootNode)
|
2013-10-25 10:35:48 +08:00
|
|
|
{
|
2013-10-28 16:00:01 +08:00
|
|
|
auto listeners = getListeners(listenerID);
|
2013-10-23 11:27:24 +08:00
|
|
|
|
|
|
|
if (listeners == nullptr)
|
|
|
|
return;
|
2014-03-04 16:43:04 +08:00
|
|
|
auto sceneGraphListeners = listeners->getSceneGraphPriorityListeners();
|
|
|
|
|
|
|
|
if (sceneGraphListeners == nullptr)
|
|
|
|
return;
|
2014-04-08 15:08:34 +08:00
|
|
|
|
2013-10-23 17:26:14 +08:00
|
|
|
// Reset priority index
|
2013-10-25 17:03:50 +08:00
|
|
|
_nodePriorityIndex = 0;
|
2013-10-23 17:26:14 +08:00
|
|
|
_nodePriorityMap.clear();
|
|
|
|
|
2014-01-21 10:23:05 +08:00
|
|
|
visitTarget(rootNode, true);
|
2013-10-23 11:27:24 +08:00
|
|
|
|
|
|
|
// After sort: priority < 0, > 0
|
2014-02-10 11:15:56 +08:00
|
|
|
std::sort(sceneGraphListeners->begin(), sceneGraphListeners->end(), [this](const EventListener* l1, const EventListener* l2) {
|
2014-04-15 12:50:33 +08:00
|
|
|
return _nodePriorityMap[l1->getAssociatedNode()] > _nodePriorityMap[l2->getAssociatedNode()];
|
2013-10-23 11:27:24 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
#if DUMP_LISTENER_ITEM_PRIORITY_INFO
|
|
|
|
log("-----------------------------------");
|
2014-02-10 11:15:56 +08:00
|
|
|
for (auto& l : *sceneGraphListeners)
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
2013-10-23 17:26:14 +08:00
|
|
|
log("listener priority: node ([%s]%p), priority (%d)", typeid(*l->_node).name(), l->_node, _nodePriorityMap[l->_node]);
|
2013-10-23 11:27:24 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:31:08 +08:00
|
|
|
void EventDispatcher::sortEventListenersOfFixedPriority(const EventListener::ListenerID& listenerID)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-28 16:00:01 +08:00
|
|
|
auto listeners = getListeners(listenerID);
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
if (listeners == nullptr)
|
2013-09-20 22:23:13 +08:00
|
|
|
return;
|
|
|
|
|
2014-02-10 11:15:56 +08:00
|
|
|
auto fixedListeners = listeners->getFixedPriorityListeners();
|
2014-03-04 16:43:04 +08:00
|
|
|
if (fixedListeners == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// After sort: priority < 0, > 0
|
2014-02-10 11:15:56 +08:00
|
|
|
std::sort(fixedListeners->begin(), fixedListeners->end(), [](const EventListener* l1, const EventListener* l2) {
|
2013-10-28 16:00:01 +08:00
|
|
|
return l1->getFixedPriority() < l2->getFixedPriority();
|
2013-09-20 22:23:13 +08:00
|
|
|
});
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
// FIXME: Should use binary search
|
2013-12-05 17:19:01 +08:00
|
|
|
int index = 0;
|
2014-02-10 11:15:56 +08:00
|
|
|
for (auto& listener : *fixedListeners)
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
2013-10-28 16:00:01 +08:00
|
|
|
if (listener->getFixedPriority() >= 0)
|
2013-10-23 11:27:24 +08:00
|
|
|
break;
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners->setGt0Index(index);
|
|
|
|
|
2013-09-20 22:23:13 +08:00
|
|
|
#if DUMP_LISTENER_ITEM_PRIORITY_INFO
|
|
|
|
log("-----------------------------------");
|
2014-02-10 11:15:56 +08:00
|
|
|
for (auto& l : *fixedListeners)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
log("listener priority: node (%p), fixed (%d)", l->_node, l->_fixedPriority);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:31:08 +08:00
|
|
|
EventDispatcher::EventListenerVector* EventDispatcher::getListeners(const EventListener::ListenerID& listenerID)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2014-02-28 12:01:47 +08:00
|
|
|
auto iter = _listenerMap.find(listenerID);
|
|
|
|
if (iter != _listenerMap.end())
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:31:08 +08:00
|
|
|
void EventDispatcher::removeEventListenersForListenerID(const EventListener::ListenerID& listenerID)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2014-02-28 12:01:47 +08:00
|
|
|
auto listenerItemIter = _listenerMap.find(listenerID);
|
|
|
|
if (listenerItemIter != _listenerMap.end())
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
auto listeners = listenerItemIter->second;
|
|
|
|
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
|
|
|
|
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
|
|
|
|
|
|
|
|
auto removeAllListenersInVector = [&](std::vector<EventListener*>* listenerVector){
|
|
|
|
if (listenerVector == nullptr)
|
|
|
|
return;
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
for (auto iter = listenerVector->begin(); iter != listenerVector->end();)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
auto l = *iter;
|
2013-10-28 16:00:01 +08:00
|
|
|
l->setRegistered(false);
|
2014-04-15 12:50:33 +08:00
|
|
|
if (l->getAssociatedNode() != nullptr)
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
2014-04-15 12:50:33 +08:00
|
|
|
dissociateNodeAndEventListener(l->getAssociatedNode(), l);
|
2014-07-10 00:45:27 +08:00
|
|
|
l->setAssociatedNode(nullptr); // nullptr out the node pointer so we don't have any dangling pointers to destroyed nodes.
|
2013-10-23 11:27:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_inDispatch == 0)
|
|
|
|
{
|
|
|
|
iter = listenerVector->erase(iter);
|
2013-10-25 10:35:48 +08:00
|
|
|
CC_SAFE_RELEASE(l);
|
2013-10-23 11:27:24 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
2013-10-23 11:27:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
removeAllListenersInVector(sceneGraphPriorityListeners);
|
|
|
|
removeAllListenersInVector(fixedPriorityListeners);
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2014-03-02 16:16:03 +08:00
|
|
|
// Remove the dirty flag according the 'listenerID'.
|
|
|
|
// No need to check whether the dispatcher is dispatching event.
|
|
|
|
_priorityDirtyFlagMap.erase(listenerID);
|
|
|
|
|
2013-09-20 22:23:13 +08:00
|
|
|
if (!_inDispatch)
|
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
listeners->clear();
|
|
|
|
delete listeners;
|
2014-02-28 12:01:47 +08:00
|
|
|
_listenerMap.erase(listenerItemIter);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
}
|
2013-11-02 22:08:08 +08:00
|
|
|
|
2014-01-02 19:59:10 +08:00
|
|
|
for (auto iter = _toAddedListeners.begin(); iter != _toAddedListeners.end();)
|
2013-11-02 22:08:08 +08:00
|
|
|
{
|
|
|
|
if ((*iter)->getListenerID() == listenerID)
|
|
|
|
{
|
2014-04-07 12:59:40 +08:00
|
|
|
(*iter)->setRegistered(false);
|
2014-01-02 19:59:10 +08:00
|
|
|
(*iter)->release();
|
2013-11-02 22:08:08 +08:00
|
|
|
iter = _toAddedListeners.erase(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
void EventDispatcher::removeEventListenersForType(EventListener::Type listenerType)
|
2013-10-29 14:57:16 +08:00
|
|
|
{
|
2013-12-20 19:31:08 +08:00
|
|
|
if (listenerType == EventListener::Type::TOUCH_ONE_BY_ONE)
|
|
|
|
{
|
|
|
|
removeEventListenersForListenerID(EventListenerTouchOneByOne::LISTENER_ID);
|
|
|
|
}
|
|
|
|
else if (listenerType == EventListener::Type::TOUCH_ALL_AT_ONCE)
|
|
|
|
{
|
|
|
|
removeEventListenersForListenerID(EventListenerTouchAllAtOnce::LISTENER_ID);
|
|
|
|
}
|
|
|
|
else if (listenerType == EventListener::Type::MOUSE)
|
|
|
|
{
|
|
|
|
removeEventListenersForListenerID(EventListenerMouse::LISTENER_ID);
|
|
|
|
}
|
|
|
|
else if (listenerType == EventListener::Type::ACCELERATION)
|
|
|
|
{
|
|
|
|
removeEventListenersForListenerID(EventListenerAcceleration::LISTENER_ID);
|
|
|
|
}
|
|
|
|
else if (listenerType == EventListener::Type::KEYBOARD)
|
|
|
|
{
|
|
|
|
removeEventListenersForListenerID(EventListenerKeyboard::LISTENER_ID);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(false, "Invalid listener type!");
|
|
|
|
}
|
2013-10-29 14:57:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::removeCustomEventListeners(const std::string& customEventName)
|
|
|
|
{
|
2013-12-20 19:31:08 +08:00
|
|
|
removeEventListenersForListenerID(customEventName);
|
2013-10-29 14:57:16 +08:00
|
|
|
}
|
|
|
|
|
2013-10-25 16:34:26 +08:00
|
|
|
void EventDispatcher::removeAllEventListeners()
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2014-02-28 15:09:53 +08:00
|
|
|
bool cleanMap = true;
|
2014-02-28 12:01:47 +08:00
|
|
|
std::vector<EventListener::ListenerID> types(_listenerMap.size());
|
2014-02-28 15:09:53 +08:00
|
|
|
|
2014-02-28 12:01:47 +08:00
|
|
|
for (const auto& e : _listenerMap)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2014-02-28 15:09:53 +08:00
|
|
|
if (_internalCustomListenerIDs.find(e.first) != _internalCustomListenerIDs.end())
|
|
|
|
{
|
|
|
|
cleanMap = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
types.push_back(e.first);
|
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-20 19:31:08 +08:00
|
|
|
for (const auto& type : types)
|
2013-10-23 11:27:24 +08:00
|
|
|
{
|
2013-10-29 14:57:16 +08:00
|
|
|
removeEventListenersForListenerID(type);
|
2013-10-23 11:27:24 +08:00
|
|
|
}
|
|
|
|
|
2014-02-28 15:09:53 +08:00
|
|
|
if (!_inDispatch && cleanMap)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2014-02-28 12:01:47 +08:00
|
|
|
_listenerMap.clear();
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::setEnabled(bool isEnabled)
|
|
|
|
{
|
|
|
|
_isEnabled = isEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EventDispatcher::isEnabled() const
|
|
|
|
{
|
|
|
|
return _isEnabled;
|
|
|
|
}
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
void EventDispatcher::setDirtyForNode(Node* node)
|
|
|
|
{
|
2014-01-22 02:10:50 +08:00
|
|
|
// Mark the node dirty only when there is an eventlistener associated with it.
|
2013-10-24 17:27:22 +08:00
|
|
|
if (_nodeListenersMap.find(node) != _nodeListenersMap.end())
|
|
|
|
{
|
|
|
|
_dirtyNodes.insert(node);
|
|
|
|
}
|
2014-04-10 15:31:22 +08:00
|
|
|
|
|
|
|
// Also set the dirty flag for node's children
|
|
|
|
const auto& children = node->getChildren();
|
|
|
|
for (const auto& child : children)
|
|
|
|
{
|
|
|
|
setDirtyForNode(child);
|
|
|
|
}
|
2013-10-23 11:27:24 +08:00
|
|
|
}
|
|
|
|
|
2013-12-20 19:31:08 +08:00
|
|
|
void EventDispatcher::setDirty(const EventListener::ListenerID& listenerID, DirtyFlag flag)
|
2013-10-25 10:35:48 +08:00
|
|
|
{
|
2013-10-28 16:00:01 +08:00
|
|
|
auto iter = _priorityDirtyFlagMap.find(listenerID);
|
2013-09-20 22:23:13 +08:00
|
|
|
if (iter == _priorityDirtyFlagMap.end())
|
|
|
|
{
|
2013-10-28 16:00:01 +08:00
|
|
|
_priorityDirtyFlagMap.insert(std::make_pair(listenerID, flag));
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
int ret = (int)flag | (int)iter->second;
|
|
|
|
iter->second = (DirtyFlag) ret;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 16:06:39 +08:00
|
|
|
void EventDispatcher::cleanToRemovedListeners()
|
2015-10-19 14:07:50 +08:00
|
|
|
{
|
|
|
|
for (auto& l : _toRemovedListeners)
|
|
|
|
{
|
|
|
|
auto listenersIter = _listenerMap.find(l->getListenerID());
|
|
|
|
if (listenersIter == _listenerMap.end())
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(l);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool find = false;
|
|
|
|
auto listeners = listenersIter->second;
|
|
|
|
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
|
|
|
|
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
|
|
|
|
|
|
|
|
if (sceneGraphPriorityListeners)
|
|
|
|
{
|
|
|
|
auto machedIter = std::find(sceneGraphPriorityListeners->begin(), sceneGraphPriorityListeners->end(), l);
|
|
|
|
if (machedIter != sceneGraphPriorityListeners->end())
|
|
|
|
{
|
|
|
|
find = true;
|
|
|
|
CC_SAFE_RELEASE(l);
|
|
|
|
sceneGraphPriorityListeners->erase(machedIter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fixedPriorityListeners)
|
|
|
|
{
|
|
|
|
auto machedIter = std::find(fixedPriorityListeners->begin(), fixedPriorityListeners->end(), l);
|
|
|
|
if (machedIter != fixedPriorityListeners->end())
|
|
|
|
{
|
|
|
|
find = true;
|
|
|
|
CC_SAFE_RELEASE(l);
|
|
|
|
fixedPriorityListeners->erase(machedIter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (find)
|
|
|
|
{
|
|
|
|
if (sceneGraphPriorityListeners && sceneGraphPriorityListeners->empty())
|
|
|
|
{
|
|
|
|
listeners->clearSceneGraphListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fixedPriorityListeners && fixedPriorityListeners->empty())
|
|
|
|
{
|
|
|
|
listeners->clearFixedListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
CC_SAFE_RELEASE(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
_toRemovedListeners.clear();
|
|
|
|
}
|
|
|
|
|
2013-09-20 22:23:13 +08:00
|
|
|
NS_CC_END
|