123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629 |
- /****************************************************************************
- Copyright (c) 2013-2016 Chukong Technologies Inc.
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
- 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.
- ****************************************************************************/
- #include "base/CCEventDispatcher.h"
- #include <algorithm>
- #include "base/CCEventCustom.h"
- #include "base/CCEventListenerTouch.h"
- #include "base/CCEventListenerAcceleration.h"
- #include "base/CCEventListenerMouse.h"
- #include "base/CCEventListenerKeyboard.h"
- #include "base/CCEventListenerCustom.h"
- #include "base/CCEventListenerFocus.h"
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
- #include "base/CCEventListenerController.h"
- #endif
- #include "2d/CCScene.h"
- #include "base/CCDirector.h"
- #include "base/CCEventType.h"
- #include "2d/CCCamera.h"
- #define DUMP_LISTENER_ITEM_PRIORITY_INFO 0
- namespace
- {
- class DispatchGuard
- {
- public:
- DispatchGuard(int& count):
- _count(count)
- {
- ++_count;
- }
- ~DispatchGuard()
- {
- --_count;
- }
- private:
- int& _count;
- };
- }
- NS_CC_BEGIN
- static EventListener::ListenerID __getListenerID(Event* event)
- {
- EventListener::ListenerID ret;
- switch (event->getType())
- {
- case Event::Type::ACCELERATION:
- ret = EventListenerAcceleration::LISTENER_ID;
- break;
- case Event::Type::CUSTOM:
- {
- auto customEvent = static_cast<EventCustom*>(event);
- ret = customEvent->getEventName();
- }
- break;
- case Event::Type::KEYBOARD:
- ret = EventListenerKeyboard::LISTENER_ID;
- break;
- case Event::Type::MOUSE:
- ret = EventListenerMouse::LISTENER_ID;
- break;
- case Event::Type::FOCUS:
- ret = EventListenerFocus::LISTENER_ID;
- break;
- case Event::Type::TOUCH:
- // Touch listener is very special, it contains two kinds of listeners, EventListenerTouchOneByOne and EventListenerTouchAllAtOnce.
- // return UNKNOWN instead.
- CCASSERT(false, "Don't call this method if the event is for touch.");
- break;
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
- case Event::Type::GAME_CONTROLLER:
- ret = EventListenerController::LISTENER_ID;
- break;
- #endif
- default:
- CCASSERT(false, "Invalid type!");
- break;
- }
-
- return ret;
- }
- EventDispatcher::EventListenerVector::EventListenerVector() :
- _fixedListeners(nullptr),
- _sceneGraphListeners(nullptr),
- _gt0Index(0)
- {
- }
- EventDispatcher::EventListenerVector::~EventListenerVector()
- {
- CC_SAFE_DELETE(_sceneGraphListeners);
- CC_SAFE_DELETE(_fixedListeners);
- }
- size_t EventDispatcher::EventListenerVector::size() const
- {
- size_t ret = 0;
- if (_sceneGraphListeners)
- ret += _sceneGraphListeners->size();
- if (_fixedListeners)
- ret += _fixedListeners->size();
-
- return ret;
- }
- bool EventDispatcher::EventListenerVector::empty() const
- {
- return (_sceneGraphListeners == nullptr || _sceneGraphListeners->empty())
- && (_fixedListeners == nullptr || _fixedListeners->empty());
- }
- void EventDispatcher::EventListenerVector::push_back(EventListener* listener)
- {
- #if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS
- 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!");
- #endif
- if (listener->getFixedPriority() == 0)
- {
- if (_sceneGraphListeners == nullptr)
- {
- _sceneGraphListeners = new (std::nothrow) std::vector<EventListener*>();
- _sceneGraphListeners->reserve(100);
- }
-
- _sceneGraphListeners->push_back(listener);
- }
- else
- {
- if (_fixedListeners == nullptr)
- {
- _fixedListeners = new std::vector<EventListener*>();
- _fixedListeners->reserve(100);
- }
-
- _fixedListeners->push_back(listener);
- }
- }
- void EventDispatcher::EventListenerVector::clearSceneGraphListeners()
- {
- if (_sceneGraphListeners)
- {
- _sceneGraphListeners->clear();
- delete _sceneGraphListeners;
- _sceneGraphListeners = nullptr;
- }
- }
- void EventDispatcher::EventListenerVector::clearFixedListeners()
- {
- if (_fixedListeners)
- {
- _fixedListeners->clear();
- delete _fixedListeners;
- _fixedListeners = nullptr;
- }
- }
- void EventDispatcher::EventListenerVector::clear()
- {
- clearSceneGraphListeners();
- clearFixedListeners();
- }
- EventDispatcher::EventDispatcher()
- : _inDispatch(0)
- , _isEnabled(false)
- , _nodePriorityIndex(0)
- {
- _toAddedListeners.reserve(50);
- _toRemovedListeners.reserve(50);
-
- // 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);
- _internalCustomListenerIDs.insert(EVENT_RENDERER_RECREATED);
- }
- EventDispatcher::~EventDispatcher()
- {
- // Clear internal custom listener IDs from set,
- // so removeAllEventListeners would clean internal custom listeners.
- _internalCustomListenerIDs.clear();
- removeAllEventListeners();
- }
- void EventDispatcher::visitTarget(Node* node, bool isRootNode)
- {
- node->sortAllChildren();
-
- int i = 0;
- auto& children = node->getChildren();
-
- auto childrenCount = children.size();
-
- if(childrenCount > 0)
- {
- Node* child = nullptr;
- // visit children zOrder < 0
- for( ; i < childrenCount; i++ )
- {
- child = children.at(i);
-
- if ( child && child->getLocalZOrder() < 0 )
- visitTarget(child, false);
- else
- break;
- }
-
- if (_nodeListenersMap.find(node) != _nodeListenersMap.end())
- {
- _globalZOrderNodeMap[node->getGlobalZOrder()].push_back(node);
- }
-
- for( ; i < childrenCount; i++ )
- {
- child = children.at(i);
- if (child)
- visitTarget(child, false);
- }
- }
- else
- {
- if (_nodeListenersMap.find(node) != _nodeListenersMap.end())
- {
- _globalZOrderNodeMap[node->getGlobalZOrder()].push_back(node);
- }
- }
-
- if (isRootNode)
- {
- std::vector<float> globalZOrders;
- globalZOrders.reserve(_globalZOrderNodeMap.size());
-
- for (const auto& e : _globalZOrderNodeMap)
- {
- globalZOrders.push_back(e.first);
- }
-
- std::stable_sort(globalZOrders.begin(), globalZOrders.end(), [](const float a, const float b){
- return a < b;
- });
-
- for (const auto& globalZ : globalZOrders)
- {
- for (const auto& n : _globalZOrderNodeMap[globalZ])
- {
- _nodePriorityMap[n] = ++_nodePriorityIndex;
- }
- }
-
- _globalZOrderNodeMap.clear();
- }
- }
- void EventDispatcher::pauseEventListenersForTarget(Node* target, bool recursive/* = false */)
- {
- auto listenerIter = _nodeListenersMap.find(target);
- if (listenerIter != _nodeListenersMap.end())
- {
- auto listeners = listenerIter->second;
- for (auto& l : *listeners)
- {
- l->setPaused(true);
- }
- }
- for (auto& listener : _toAddedListeners)
- {
- if (listener->getAssociatedNode() == target)
- {
- listener->setPaused(true);
- }
- }
-
- if (recursive)
- {
- const auto& children = target->getChildren();
- for (const auto& child : children)
- {
- pauseEventListenersForTarget(child, true);
- }
- }
- }
- void EventDispatcher::resumeEventListenersForTarget(Node* target, bool recursive/* = false */)
- {
- auto listenerIter = _nodeListenersMap.find(target);
- if (listenerIter != _nodeListenersMap.end())
- {
- auto listeners = listenerIter->second;
- for (auto& l : *listeners)
- {
- l->setPaused(false);
- }
- }
-
- for (auto& listener : _toAddedListeners)
- {
- if (listener->getAssociatedNode() == target)
- {
- listener->setPaused(false);
- }
- }
- setDirtyForNode(target);
-
- if (recursive)
- {
- const auto& children = target->getChildren();
- for (const auto& child : children)
- {
- resumeEventListenersForTarget(child, true);
- }
- }
- }
- void EventDispatcher::removeEventListenersForTarget(Node* target, bool recursive/* = false */)
- {
- // 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);
- auto listenerIter = _nodeListenersMap.find(target);
- if (listenerIter != _nodeListenersMap.end())
- {
- auto listeners = listenerIter->second;
- auto listenersCopy = *listeners;
- for (auto& l : listenersCopy)
- {
- removeEventListener(l);
- }
- }
-
- // 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;
-
- if (listener->getAssociatedNode() == target)
- {
- listener->setAssociatedNode(nullptr); // Ensure no dangling ptr to the target node.
- listener->setRegistered(false);
- releaseListener(listener);
- iter = _toAddedListeners.erase(iter);
- }
- else
- {
- ++iter;
- }
- }
-
- if (recursive)
- {
- const auto& children = target->getChildren();
- for (const auto& child : children)
- {
- removeEventListenersForTarget(child, true);
- }
- }
- }
- 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
- {
- listeners = new (std::nothrow) std::vector<EventListener*>();
- _nodeListenersMap.emplace(node, listeners);
- }
-
- 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);
- delete listeners;
- }
- }
- }
- void EventDispatcher::addEventListener(EventListener* listener)
- {
- if (_inDispatch == 0)
- {
- forceAddEventListener(listener);
- }
- else
- {
- _toAddedListeners.push_back(listener);
- }
- #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
- if (sEngine)
- {
- sEngine->retainScriptObject(this, listener);
- }
- #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- listener->retain();
- }
- void EventDispatcher::forceAddEventListener(EventListener* listener)
- {
- EventListenerVector* listeners = nullptr;
- EventListener::ListenerID listenerID = listener->getListenerID();
- auto itr = _listenerMap.find(listenerID);
- if (itr == _listenerMap.end())
- {
-
- listeners = new (std::nothrow) EventListenerVector();
- _listenerMap.emplace(listenerID, listeners);
- }
- else
- {
- listeners = itr->second;
- }
-
- listeners->push_back(listener);
-
- if (listener->getFixedPriority() == 0)
- {
- setDirty(listenerID, DirtyFlag::SCENE_GRAPH_PRIORITY);
-
- auto node = listener->getAssociatedNode();
- CCASSERT(node != nullptr, "Invalid scene graph priority!");
-
- associateNodeAndEventListener(node, listener);
-
- if (!node->isRunning())
- {
- listener->setPaused(true);
- }
- }
- else
- {
- setDirty(listenerID, DirtyFlag::FIXED_PRIORITY);
- }
- }
- void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)
- {
- CCASSERT(listener && node, "Invalid parameters.");
- CCASSERT(!listener->isRegistered(), "The listener has been registered.");
-
- if (!listener->checkAvailable())
- return;
-
- listener->setAssociatedNode(node);
- listener->setFixedPriority(0);
- listener->setRegistered(true);
-
- addEventListener(listener);
- }
- #if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS && COCOS2D_DEBUG > 0
- 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 ||
- listener->getAssociatedNode() != node,
- "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)
- {
- CCASSERT(listener->getAssociatedNode() != node,
- "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)
- {
- CCASSERT(listener->getAssociatedNode() != node,
- "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!");
- }
- }
- #endif // #if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS && COCOS2D_DEBUG > 0
- void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)
- {
- CCASSERT(listener, "Invalid parameters.");
- CCASSERT(!listener->isRegistered(), "The listener has been registered.");
- CCASSERT(fixedPriority != 0, "0 priority is forbidden for fixed priority since it's used for scene graph based priority.");
-
- if (!listener->checkAvailable())
- return;
-
- listener->setAssociatedNode(nullptr);
- listener->setFixedPriority(fixedPriority);
- listener->setRegistered(true);
- listener->setPaused(false);
- addEventListener(listener);
- }
- EventListenerCustom* EventDispatcher::addCustomEventListener(const std::string &eventName, const std::function<void(EventCustom*)>& callback)
- {
- EventListenerCustom *listener = EventListenerCustom::create(eventName, callback);
- addEventListenerWithFixedPriority(listener, 1);
- return listener;
- }
- void EventDispatcher::removeEventListener(EventListener* listener)
- {
- if (listener == nullptr)
- return;
-
- // just return if listener is in _toRemovedListeners to avoid remove listeners more than once
- if (std::find(_toRemovedListeners.begin(), _toRemovedListeners.end(), listener) != _toRemovedListeners.end())
- return;
- bool isFound = false;
-
- auto removeListenerInVector = [&](std::vector<EventListener*>* listeners){
- if (listeners == nullptr)
- return;
-
- for (auto iter = listeners->begin(); iter != listeners->end(); ++iter)
- {
- auto l = *iter;
- if (l == listener)
- {
- CC_SAFE_RETAIN(l);
- l->setRegistered(false);
- if (l->getAssociatedNode() != nullptr)
- {
- dissociateNodeAndEventListener(l->getAssociatedNode(), l);
- l->setAssociatedNode(nullptr); // nullptr out the node pointer so we don't have any dangling pointers to destroyed nodes.
- }
-
- if (_inDispatch == 0)
- {
- iter = listeners->erase(iter);
- releaseListener(l);
- }
- else
- {
- _toRemovedListeners.push_back(l);
- }
-
- isFound = true;
- break;
- }
- }
- };
-
- for (auto iter = _listenerMap.begin(); iter != _listenerMap.end();)
- {
- auto listeners = iter->second;
- auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
- auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
- removeListenerInVector(sceneGraphPriorityListeners);
- if (isFound)
- {
- // fixed #4160: Dirty flag need to be updated after listeners were removed.
- setDirty(listener->getListenerID(), DirtyFlag::SCENE_GRAPH_PRIORITY);
- }
- else
- {
- removeListenerInVector(fixedPriorityListeners);
- if (isFound)
- {
- setDirty(listener->getListenerID(), DirtyFlag::FIXED_PRIORITY);
- }
- }
-
- #if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS
- 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,
- "Listener should be in no lists after this is done if we're not currently in dispatch mode.");
- #endif
- if (iter->second->empty())
- {
- _priorityDirtyFlagMap.erase(listener->getListenerID());
- auto list = iter->second;
- iter = _listenerMap.erase(iter);
- CC_SAFE_DELETE(list);
- }
- else
- {
- ++iter;
- }
-
- if (isFound)
- break;
- }
- if (isFound)
- {
- releaseListener(listener);
- }
- else
- {
- for(auto iter = _toAddedListeners.begin(); iter != _toAddedListeners.end(); ++iter)
- {
- if (*iter == listener)
- {
- listener->setRegistered(false);
- releaseListener(listener);
- _toAddedListeners.erase(iter);
- break;
- }
- }
- }
- }
- void EventDispatcher::setPriority(EventListener* listener, int fixedPriority)
- {
- if (listener == nullptr)
- return;
-
- for (auto& iter : _listenerMap)
- {
- auto fixedPriorityListeners = iter.second->getFixedPriorityListeners();
- if (fixedPriorityListeners)
- {
- auto found = std::find(fixedPriorityListeners->begin(), fixedPriorityListeners->end(), listener);
- if (found != fixedPriorityListeners->end())
- {
- CCASSERT(listener->getAssociatedNode() == nullptr, "Can't set fixed priority with scene graph based listener.");
-
- if (listener->getFixedPriority() != fixedPriority)
- {
- listener->setFixedPriority(fixedPriority);
- setDirty(listener->getListenerID(), DirtyFlag::FIXED_PRIORITY);
- }
- return;
- }
- }
- }
- }
- void EventDispatcher::dispatchEventToListeners(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;
- }
- }
- }
- }
-
- if (sceneGraphPriorityListeners)
- {
- if (!shouldStopPropagation)
- {
- // priority == 0, scene graph priority
- for (auto& l : *sceneGraphPriorityListeners)
- {
- if (l->isEnabled() && !l->isPaused() && l->isRegistered() && onEvent(l))
- {
- shouldStopPropagation = true;
- break;
- }
- }
- }
- }
-
- 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;
- }
- }
- }
- }
- }
- 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;
- }
- }
- }
- }
-
- auto scene = Director::getInstance()->getRunningScene();
- if (scene && sceneGraphPriorityListeners)
- {
- 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
- // get a copy of cameras, prevent it's been modified in listener callback
- // if camera's depth is greater, process it earlier
- auto cameras = scene->getCameras();
- for (auto rit = cameras.rbegin(), ritRend = cameras.rend(); rit != ritRend; ++rit)
- {
- Camera* camera = *rit;
- if (camera->isVisible() == false)
- {
- continue;
- }
-
- Camera::_visitingCamera = camera;
- auto cameraFlag = (unsigned short)camera->getCameraFlag();
- for (auto& l : sceneListeners)
- {
- if (nullptr == l->getAssociatedNode() || 0 == (l->getAssociatedNode()->getCameraMask() & cameraFlag))
- {
- continue;
- }
- if (onEvent(l))
- {
- shouldStopPropagation = true;
- break;
- }
- }
- if (shouldStopPropagation)
- {
- break;
- }
- }
- Camera::_visitingCamera = nullptr;
- }
- }
-
- 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;
- }
- }
- }
- }
- }
- void EventDispatcher::dispatchEvent(Event* event)
- {
- if (!_isEnabled)
- return;
-
- updateDirtyFlagForSceneGraph();
-
-
- DispatchGuard guard(_inDispatch);
-
- if (event->getType() == Event::Type::TOUCH)
- {
- dispatchTouchEvent(static_cast<EventTouch*>(event));
- return;
- }
-
- auto listenerID = __getListenerID(event);
-
- sortEventListeners(listenerID);
-
- auto pfnDispatchEventToListeners = &EventDispatcher::dispatchEventToListeners;
- if (event->getType() == Event::Type::MOUSE) {
- pfnDispatchEventToListeners = &EventDispatcher::dispatchTouchEventToListeners;
- }
- auto iter = _listenerMap.find(listenerID);
- if (iter != _listenerMap.end())
- {
- auto listeners = iter->second;
-
- auto onEvent = [&event](EventListener* listener) -> bool{
- event->setCurrentTarget(listener->getAssociatedNode());
- listener->_onEvent(event);
- return event->isStopped();
- };
-
- (this->*pfnDispatchEventToListeners)(listeners, onEvent);
- }
-
- updateListeners(event);
- }
- void EventDispatcher::dispatchCustomEvent(const std::string &eventName, void *optionalUserData)
- {
- EventCustom ev(eventName);
- ev.setUserData(optionalUserData);
- dispatchEvent(&ev);
- }
- bool EventDispatcher::hasEventListener(const EventListener::ListenerID& listenerID) const
- {
- return getListeners(listenerID) != nullptr;
- }
- void EventDispatcher::dispatchTouchEvent(EventTouch* event)
- {
- sortEventListeners(EventListenerTouchOneByOne::LISTENER_ID);
- sortEventListeners(EventListenerTouchAllAtOnce::LISTENER_ID);
-
- auto oneByOneListeners = getListeners(EventListenerTouchOneByOne::LISTENER_ID);
- auto allAtOnceListeners = getListeners(EventListenerTouchAllAtOnce::LISTENER_ID);
-
- // If there aren't any touch listeners, return directly.
- if (nullptr == oneByOneListeners && nullptr == allAtOnceListeners)
- return;
-
- bool isNeedsMutableSet = (oneByOneListeners && allAtOnceListeners);
-
- const std::vector<Touch*>& originalTouches = event->getTouches();
- std::vector<Touch*> mutableTouches(originalTouches.size());
- std::copy(originalTouches.begin(), originalTouches.end(), mutableTouches.begin());
- //
- // process the target handlers 1st
- //
- if (oneByOneListeners)
- {
- auto mutableTouchesIter = mutableTouches.begin();
-
- for (auto& touches : originalTouches)
- {
- bool isSwallowed = false;
- auto onTouchEvent = [&](EventListener* l) -> bool { // Return true to break
- EventListenerTouchOneByOne* listener = static_cast<EventListenerTouchOneByOne*>(l);
-
- // Skip if the listener was removed.
- if (!listener->_isRegistered)
- return false;
-
- event->setCurrentTarget(listener->_node);
-
- bool isClaimed = false;
- std::vector<Touch*>::iterator removedIter;
-
- EventTouch::EventCode eventCode = event->getEventCode();
-
- if (eventCode == EventTouch::EventCode::BEGAN)
- {
- if (listener->onTouchBegan)
- {
- isClaimed = listener->onTouchBegan(touches, event);
- if (isClaimed && listener->_isRegistered)
- {
- listener->_claimedTouches.push_back(touches);
- }
- }
- }
- else if (listener->_claimedTouches.size() > 0
- && ((removedIter = std::find(listener->_claimedTouches.begin(), listener->_claimedTouches.end(), touches)) != listener->_claimedTouches.end()))
- {
- isClaimed = true;
-
- switch (eventCode)
- {
- case EventTouch::EventCode::MOVED:
- if (listener->onTouchMoved)
- {
- listener->onTouchMoved(touches, event);
- }
- break;
- case EventTouch::EventCode::ENDED:
- if (listener->onTouchEnded)
- {
- listener->onTouchEnded(touches, event);
- }
- if (listener->_isRegistered)
- {
- listener->_claimedTouches.erase(removedIter);
- }
- break;
- case EventTouch::EventCode::CANCELLED:
- if (listener->onTouchCancelled)
- {
- listener->onTouchCancelled(touches, event);
- }
- if (listener->_isRegistered)
- {
- listener->_claimedTouches.erase(removedIter);
- }
- break;
- default:
- CCASSERT(false, "The eventcode is invalid.");
- break;
- }
- }
-
- // If the event was stopped, return directly.
- if (event->isStopped())
- {
- updateListeners(event);
- return true;
- }
-
- CCASSERT(touches->getID() == (*mutableTouchesIter)->getID(),
- "touches ID should be equal to mutableTouchesIter's ID.");
-
- if (isClaimed && listener->_isRegistered && listener->_needSwallow)
- {
- if (isNeedsMutableSet)
- {
- mutableTouchesIter = mutableTouches.erase(mutableTouchesIter);
- isSwallowed = true;
- }
- return true;
- }
-
- return false;
- };
-
- //
- dispatchTouchEventToListeners(oneByOneListeners, onTouchEvent);
- if (event->isStopped())
- {
- return;
- }
-
- if (!isSwallowed)
- ++mutableTouchesIter;
- }
- }
-
- //
- // process standard handlers 2nd
- //
- if (allAtOnceListeners && mutableTouches.size() > 0)
- {
-
- auto onTouchesEvent = [&](EventListener* l) -> bool{
- EventListenerTouchAllAtOnce* listener = static_cast<EventListenerTouchAllAtOnce*>(l);
- // Skip if the listener was removed.
- if (!listener->_isRegistered)
- return false;
-
- event->setCurrentTarget(listener->_node);
-
- switch (event->getEventCode())
- {
- case EventTouch::EventCode::BEGAN:
- if (listener->onTouchesBegan)
- {
- listener->onTouchesBegan(mutableTouches, event);
- }
- break;
- case EventTouch::EventCode::MOVED:
- if (listener->onTouchesMoved)
- {
- listener->onTouchesMoved(mutableTouches, event);
- }
- break;
- case EventTouch::EventCode::ENDED:
- if (listener->onTouchesEnded)
- {
- listener->onTouchesEnded(mutableTouches, event);
- }
- break;
- case EventTouch::EventCode::CANCELLED:
- if (listener->onTouchesCancelled)
- {
- listener->onTouchesCancelled(mutableTouches, event);
- }
- break;
- default:
- CCASSERT(false, "The eventcode is invalid.");
- break;
- }
-
- // If the event was stopped, return directly.
- if (event->isStopped())
- {
- updateListeners(event);
- return true;
- }
-
- return false;
- };
-
- dispatchTouchEventToListeners(allAtOnceListeners, onTouchesEvent);
- if (event->isStopped())
- {
- return;
- }
- }
-
- updateListeners(event);
- }
- void EventDispatcher::updateListeners(Event* event)
- {
- CCASSERT(_inDispatch > 0, "If program goes here, there should be event in dispatch.");
- if (_inDispatch > 1)
- return;
- auto onUpdateListeners = [this](const EventListener::ListenerID& listenerID)
- {
- auto listenersIter = _listenerMap.find(listenerID);
- if (listenersIter == _listenerMap.end())
- return;
- auto listeners = listenersIter->second;
-
- auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
- auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
-
- if (sceneGraphPriorityListeners)
- {
- for (auto iter = sceneGraphPriorityListeners->begin(); iter != sceneGraphPriorityListeners->end();)
- {
- auto l = *iter;
- if (!l->isRegistered())
- {
- iter = sceneGraphPriorityListeners->erase(iter);
- // 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);
- releaseListener(l);
- }
- else
- {
- ++iter;
- }
- }
- }
-
- if (fixedPriorityListeners)
- {
- for (auto iter = fixedPriorityListeners->begin(); iter != fixedPriorityListeners->end();)
- {
- auto l = *iter;
- if (!l->isRegistered())
- {
- iter = fixedPriorityListeners->erase(iter);
- // 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);
- releaseListener(l);
- }
- else
- {
- ++iter;
- }
- }
- }
-
- if (sceneGraphPriorityListeners && sceneGraphPriorityListeners->empty())
- {
- listeners->clearSceneGraphListeners();
- }
- if (fixedPriorityListeners && fixedPriorityListeners->empty())
- {
- listeners->clearFixedListeners();
- }
- };
- if (event->getType() == Event::Type::TOUCH)
- {
- onUpdateListeners(EventListenerTouchOneByOne::LISTENER_ID);
- onUpdateListeners(EventListenerTouchAllAtOnce::LISTENER_ID);
- }
- else
- {
- onUpdateListeners(__getListenerID(event));
- }
-
- 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;
- }
- }
-
- if (!_toAddedListeners.empty())
- {
- for (auto& listener : _toAddedListeners)
- {
- forceAddEventListener(listener);
- }
- _toAddedListeners.clear();
- }
- if (!_toRemovedListeners.empty())
- {
- cleanToRemovedListeners();
- }
- }
- void EventDispatcher::updateDirtyFlagForSceneGraph()
- {
- if (!_dirtyNodes.empty())
- {
- for (auto& node : _dirtyNodes)
- {
- auto iter = _nodeListenersMap.find(node);
- if (iter != _nodeListenersMap.end())
- {
- for (auto& l : *iter->second)
- {
- setDirty(l->getListenerID(), DirtyFlag::SCENE_GRAPH_PRIORITY);
- }
- }
- }
-
- _dirtyNodes.clear();
- }
- }
- void EventDispatcher::sortEventListeners(const EventListener::ListenerID& listenerID)
- {
- DirtyFlag dirtyFlag = DirtyFlag::NONE;
-
- auto dirtyIter = _priorityDirtyFlagMap.find(listenerID);
- if (dirtyIter != _priorityDirtyFlagMap.end())
- {
- dirtyFlag = dirtyIter->second;
- }
-
- 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);
- }
-
- if ((int)dirtyFlag & (int)DirtyFlag::SCENE_GRAPH_PRIORITY)
- {
- auto rootNode = Director::getInstance()->getRunningScene();
- if (rootNode)
- {
- sortEventListenersOfSceneGraphPriority(listenerID, rootNode);
- }
- else
- {
- dirtyIter->second = DirtyFlag::SCENE_GRAPH_PRIORITY;
- }
- }
- }
- }
- void EventDispatcher::sortEventListenersOfSceneGraphPriority(const EventListener::ListenerID& listenerID, Node* rootNode)
- {
- auto listeners = getListeners(listenerID);
-
- if (listeners == nullptr)
- return;
- auto sceneGraphListeners = listeners->getSceneGraphPriorityListeners();
-
- if (sceneGraphListeners == nullptr)
- return;
- // Reset priority index
- _nodePriorityIndex = 0;
- _nodePriorityMap.clear();
- visitTarget(rootNode, true);
-
- // After sort: priority < 0, > 0
- std::stable_sort(sceneGraphListeners->begin(), sceneGraphListeners->end(), [this](const EventListener* l1, const EventListener* l2) {
- return _nodePriorityMap[l1->getAssociatedNode()] > _nodePriorityMap[l2->getAssociatedNode()];
- });
-
- #if DUMP_LISTENER_ITEM_PRIORITY_INFO
- log("-----------------------------------");
- for (auto& l : *sceneGraphListeners)
- {
- log("listener priority: node ([%s]%p), priority (%d)", typeid(*l->_node).name(), l->_node, _nodePriorityMap[l->_node]);
- }
- #endif
- }
- void EventDispatcher::sortEventListenersOfFixedPriority(const EventListener::ListenerID& listenerID)
- {
- auto listeners = getListeners(listenerID);
- if (listeners == nullptr)
- return;
-
- auto fixedListeners = listeners->getFixedPriorityListeners();
- if (fixedListeners == nullptr)
- return;
-
- // After sort: priority < 0, > 0
- std::stable_sort(fixedListeners->begin(), fixedListeners->end(), [](const EventListener* l1, const EventListener* l2) {
- return l1->getFixedPriority() < l2->getFixedPriority();
- });
-
- // FIXME: Should use binary search
- int index = 0;
- for (auto& listener : *fixedListeners)
- {
- if (listener->getFixedPriority() >= 0)
- break;
- ++index;
- }
-
- listeners->setGt0Index(index);
-
- #if DUMP_LISTENER_ITEM_PRIORITY_INFO
- log("-----------------------------------");
- for (auto& l : *fixedListeners)
- {
- log("listener priority: node (%p), fixed (%d)", l->_node, l->_fixedPriority);
- }
- #endif
-
- }
- EventDispatcher::EventListenerVector* EventDispatcher::getListeners(const EventListener::ListenerID& listenerID) const
- {
- auto iter = _listenerMap.find(listenerID);
- if (iter != _listenerMap.end())
- {
- return iter->second;
- }
-
- return nullptr;
- }
- void EventDispatcher::removeEventListenersForListenerID(const EventListener::ListenerID& listenerID)
- {
- auto listenerItemIter = _listenerMap.find(listenerID);
- if (listenerItemIter != _listenerMap.end())
- {
- auto listeners = listenerItemIter->second;
- auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
- auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
-
- auto removeAllListenersInVector = [&](std::vector<EventListener*>* listenerVector){
- if (listenerVector == nullptr)
- return;
-
- for (auto iter = listenerVector->begin(); iter != listenerVector->end();)
- {
- auto l = *iter;
- l->setRegistered(false);
- if (l->getAssociatedNode() != nullptr)
- {
- dissociateNodeAndEventListener(l->getAssociatedNode(), l);
- l->setAssociatedNode(nullptr); // nullptr out the node pointer so we don't have any dangling pointers to destroyed nodes.
- }
-
- if (_inDispatch == 0)
- {
- iter = listenerVector->erase(iter);
- releaseListener(l);
- }
- else
- {
- ++iter;
- }
- }
- };
-
- removeAllListenersInVector(sceneGraphPriorityListeners);
- removeAllListenersInVector(fixedPriorityListeners);
-
- // Remove the dirty flag according the 'listenerID'.
- // No need to check whether the dispatcher is dispatching event.
- _priorityDirtyFlagMap.erase(listenerID);
-
- if (!_inDispatch)
- {
- listeners->clear();
- delete listeners;
- _listenerMap.erase(listenerItemIter);
- }
- }
-
- for (auto iter = _toAddedListeners.begin(); iter != _toAddedListeners.end();)
- {
- if ((*iter)->getListenerID() == listenerID)
- {
- (*iter)->setRegistered(false);
- releaseListener(*iter);
- iter = _toAddedListeners.erase(iter);
- }
- else
- {
- ++iter;
- }
- }
- }
- void EventDispatcher::removeEventListenersForType(EventListener::Type listenerType)
- {
- 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!");
- }
- }
- void EventDispatcher::removeCustomEventListeners(const std::string& customEventName)
- {
- removeEventListenersForListenerID(customEventName);
- }
- void EventDispatcher::removeAllEventListeners()
- {
- bool cleanMap = true;
- std::vector<EventListener::ListenerID> types;
- types.reserve(_listenerMap.size());
-
- for (const auto& e : _listenerMap)
- {
- if (_internalCustomListenerIDs.find(e.first) != _internalCustomListenerIDs.end())
- {
- cleanMap = false;
- }
- else
- {
- types.push_back(e.first);
- }
- }
- for (const auto& type : types)
- {
- removeEventListenersForListenerID(type);
- }
-
- if (!_inDispatch && cleanMap)
- {
- _listenerMap.clear();
- }
- }
- void EventDispatcher::setEnabled(bool isEnabled)
- {
- _isEnabled = isEnabled;
- }
- bool EventDispatcher::isEnabled() const
- {
- return _isEnabled;
- }
- void EventDispatcher::setDirtyForNode(Node* node)
- {
- // Mark the node dirty only when there is an eventlistener associated with it.
- if (_nodeListenersMap.find(node) != _nodeListenersMap.end())
- {
- _dirtyNodes.insert(node);
- }
- // Also set the dirty flag for node's children
- const auto& children = node->getChildren();
- for (const auto& child : children)
- {
- setDirtyForNode(child);
- }
- }
- void EventDispatcher::setDirty(const EventListener::ListenerID& listenerID, DirtyFlag flag)
- {
- auto iter = _priorityDirtyFlagMap.find(listenerID);
- if (iter == _priorityDirtyFlagMap.end())
- {
- _priorityDirtyFlagMap.emplace(listenerID, flag);
- }
- else
- {
- int ret = (int)flag | (int)iter->second;
- iter->second = (DirtyFlag) ret;
- }
- }
- void EventDispatcher::cleanToRemovedListeners()
- {
- for (auto& l : _toRemovedListeners)
- {
- auto listenersIter = _listenerMap.find(l->getListenerID());
- if (listenersIter == _listenerMap.end())
- {
- releaseListener(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;
- releaseListener(l);
- sceneGraphPriorityListeners->erase(machedIter);
- }
- }
- if (fixedPriorityListeners)
- {
- auto machedIter = std::find(fixedPriorityListeners->begin(), fixedPriorityListeners->end(), l);
- if (machedIter != fixedPriorityListeners->end())
- {
- find = true;
- releaseListener(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();
- }
- void EventDispatcher::releaseListener(EventListener* listener)
- {
- #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
- if (listener && sEngine)
- {
- sEngine->releaseScriptObject(this, listener);
- }
- #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- CC_SAFE_RELEASE(listener);
- }
- NS_CC_END
|