From 8102272b1512cd2173ee4f2e48d285d01ab97c47 Mon Sep 17 00:00:00 2001 From: James Chen Date: Sun, 15 Sep 2013 11:06:19 +0800 Subject: [PATCH] [dispatcher] Updating comments of EventDispatcher, renaming some functions and some performance improves when dispatching event. --- .../event_dispatcher/CCEventDispatcher.cpp | 73 +++++++++---------- cocos2dx/event_dispatcher/CCEventDispatcher.h | 25 ++++--- 2 files changed, 51 insertions(+), 47 deletions(-) diff --git a/cocos2dx/event_dispatcher/CCEventDispatcher.cpp b/cocos2dx/event_dispatcher/CCEventDispatcher.cpp index 391f66f02c..755a42bb98 100644 --- a/cocos2dx/event_dispatcher/CCEventDispatcher.cpp +++ b/cocos2dx/event_dispatcher/CCEventDispatcher.cpp @@ -73,7 +73,7 @@ EventDispatcher* EventDispatcher::getInstance() return &_instance; } -void EventDispatcher::registerEventListenerWithItem(EventListenerItem* item) +void EventDispatcher::addEventListenerWithItem(EventListenerItem* item) { if (!_listeners) { @@ -106,7 +106,7 @@ void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* list item->listener->retain(); item->listener->_isRegistered = true; - registerEventListenerWithItem(item); + addEventListenerWithItem(item); _eventNodes.push_back(node); node->associateEventListener(listener); @@ -126,7 +126,7 @@ void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, item->listener->retain(); item->listener->_isRegistered = true; - registerEventListenerWithItem(item); + addEventListenerWithItem(item); } void EventDispatcher::removeEventListener(EventListener* listener) @@ -220,7 +220,7 @@ void EventDispatcher::dispatchEvent(Event* event) if (_listeners == nullptr || !_isEnabled) return; - sortAllEventListenerItems(); + sortAllEventListenerItemsForType(event->_type); DispatchGuard guard(_inDispatch); @@ -253,7 +253,7 @@ void EventDispatcher::dispatchEvent(Event* event) void EventDispatcher::dispatchTouchEvent(TouchEvent* event) { - auto touchListeners = getListeners(TouchEvent::EVENT_TYPE); + auto touchListeners = getListenerItemsForType(TouchEvent::EVENT_TYPE); if (touchListeners == nullptr) return; @@ -446,7 +446,7 @@ void EventDispatcher::removeUnregisteredListeners() return; auto listenerItemIter = _listeners->begin(); - while ( listenerItemIter != _listeners->end()) + while (listenerItemIter != _listeners->end()) { auto removeIterBegin = std::remove_if(listenerItemIter->second->begin(), listenerItemIter->second->end(), [](const EventListenerItem* item){ return item->listener == nullptr; @@ -477,43 +477,42 @@ void EventDispatcher::removeUnregisteredListeners() } } -void EventDispatcher::sortAllEventListenerItems() +void EventDispatcher::sortAllEventListenerItemsForType(const std::string &eventType) { if (_listeners == nullptr) return; - for (auto listenerItemIter = _listeners->begin(); listenerItemIter != _listeners->end(); ++listenerItemIter) - { - // After sort: priority < 0, = 0, scene graph, > 0 - listenerItemIter->second->sort([](const EventListenerItem* item1, const EventListenerItem* item2) { - // item1 and item2 are both using fixed priority. - if (nullptr == item1->node && nullptr == item2->node) - { - return item1->fixedPriority > item2->fixedPriority; - } - // item1 and item2 are both using scene graph based priority. - else if (nullptr != item1->node && nullptr != item2->node) - { - return item1->node->getEventPriority() > item2->node->getEventPriority(); - } - else if (nullptr != item1->node && nullptr == item2->node) - { - return 0 < item2->fixedPriority; - } - else if (nullptr == item1->node && nullptr != item2->node) - { - return item1->fixedPriority < 0; - } - else - { - CCASSERT(false, "sort event node error..."); - return false; - } - }); - } + auto listenerList = getListenerItemsForType(eventType); + + // After sort: priority < 0, = 0, scene graph, > 0 + listenerList->sort([](const EventListenerItem* item1, const EventListenerItem* item2) { + // item1 and item2 are both using fixed priority. + if (nullptr == item1->node && nullptr == item2->node) + { + return item1->fixedPriority > item2->fixedPriority; + } + // item1 and item2 are both using scene graph based priority. + else if (nullptr != item1->node && nullptr != item2->node) + { + return item1->node->getEventPriority() > item2->node->getEventPriority(); + } + else if (nullptr != item1->node && nullptr == item2->node) + { + return 0 < item2->fixedPriority; + } + else if (nullptr == item1->node && nullptr != item2->node) + { + return item1->fixedPriority < 0; + } + else + { + CCASSERT(false, "sort event node error..."); + return false; + } + }); } -std::list* EventDispatcher::getListeners(const std::string& eventType) +std::list* EventDispatcher::getListenerItemsForType(const std::string &eventType) { if (_listeners != nullptr) { diff --git a/cocos2dx/event_dispatcher/CCEventDispatcher.h b/cocos2dx/event_dispatcher/CCEventDispatcher.h index 0fa51c6614..8a1748da88 100644 --- a/cocos2dx/event_dispatcher/CCEventDispatcher.h +++ b/cocos2dx/event_dispatcher/CCEventDispatcher.h @@ -54,18 +54,22 @@ public: /** Gets the singleton of EventDispatcher */ static EventDispatcher* getInstance(); - /** Registers a callback function for an specified event with the priority of scene graph. + /** Adds a event listener for a specified event with the priority of scene graph. + * @param listener The listener of a specified event. + * @param node The priority of the listener is based on the draw order of this node. */ void addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node); - /** Registers a callback function for an specified event with the fixed priority. + /** Adds a event listener for a specified event with the fixed priority. + * @param listener The listener of a specified event. + * @param fixedPriority The fixed priority of the listener. */ void addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority); - /** Unregisters a callback function by the unique ID. */ + /** Remove a listener */ void removeEventListener(EventListener* listener); - /** Removes listeners by event type */ + /** Removes all listeners with the same event type */ void removeListenersForEventType(const std::string& eventType); /** Removes all listeners */ @@ -104,18 +108,19 @@ private: /** Constructor of EventDispatcher */ EventDispatcher(); - void registerEventListenerWithItem(EventListenerItem* item); + /** Adds event listener with item */ + void addEventListenerWithItem(EventListenerItem* item); /** Touch event needs to be processed different with other events since it needs support ALL_AT_ONCE and ONE_BY_NONE mode. */ void dispatchTouchEvent(TouchEvent* event); - /** Gets event listener list by event type. */ - std::list* getListeners(const std::string& eventType); + /** Gets event the listener list for the event type. */ + std::list* getListenerItemsForType(const std::string& eventType); - /** Sorts all listeners by priority */ - void sortAllEventListenerItems(); + /** Sorts the listeners of specified type by priority */ + void sortAllEventListenerItemsForType(const std::string& eventType); - /** Remove listeners that have been unregistered. */ + /** Removes all listeners that have been unregistered. */ void removeUnregisteredListeners(); private: