[dispatcher] Updating comments of EventDispatcher, renaming some functions and some performance improves when dispatching event.

This commit is contained in:
James Chen 2013-09-15 11:06:19 +08:00
parent 1ed980d27c
commit 8102272b15
2 changed files with 51 additions and 47 deletions

View File

@ -73,7 +73,7 @@ EventDispatcher* EventDispatcher::getInstance()
return &_instance; return &_instance;
} }
void EventDispatcher::registerEventListenerWithItem(EventListenerItem* item) void EventDispatcher::addEventListenerWithItem(EventListenerItem* item)
{ {
if (!_listeners) if (!_listeners)
{ {
@ -106,7 +106,7 @@ void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* list
item->listener->retain(); item->listener->retain();
item->listener->_isRegistered = true; item->listener->_isRegistered = true;
registerEventListenerWithItem(item); addEventListenerWithItem(item);
_eventNodes.push_back(node); _eventNodes.push_back(node);
node->associateEventListener(listener); node->associateEventListener(listener);
@ -126,7 +126,7 @@ void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener,
item->listener->retain(); item->listener->retain();
item->listener->_isRegistered = true; item->listener->_isRegistered = true;
registerEventListenerWithItem(item); addEventListenerWithItem(item);
} }
void EventDispatcher::removeEventListener(EventListener* listener) void EventDispatcher::removeEventListener(EventListener* listener)
@ -220,7 +220,7 @@ void EventDispatcher::dispatchEvent(Event* event)
if (_listeners == nullptr || !_isEnabled) if (_listeners == nullptr || !_isEnabled)
return; return;
sortAllEventListenerItems(); sortAllEventListenerItemsForType(event->_type);
DispatchGuard guard(_inDispatch); DispatchGuard guard(_inDispatch);
@ -253,7 +253,7 @@ void EventDispatcher::dispatchEvent(Event* event)
void EventDispatcher::dispatchTouchEvent(TouchEvent* event) void EventDispatcher::dispatchTouchEvent(TouchEvent* event)
{ {
auto touchListeners = getListeners(TouchEvent::EVENT_TYPE); auto touchListeners = getListenerItemsForType(TouchEvent::EVENT_TYPE);
if (touchListeners == nullptr) if (touchListeners == nullptr)
return; return;
@ -446,7 +446,7 @@ void EventDispatcher::removeUnregisteredListeners()
return; return;
auto listenerItemIter = _listeners->begin(); 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){ auto removeIterBegin = std::remove_if(listenerItemIter->second->begin(), listenerItemIter->second->end(), [](const EventListenerItem* item){
return item->listener == nullptr; return item->listener == nullptr;
@ -477,43 +477,42 @@ void EventDispatcher::removeUnregisteredListeners()
} }
} }
void EventDispatcher::sortAllEventListenerItems() void EventDispatcher::sortAllEventListenerItemsForType(const std::string &eventType)
{ {
if (_listeners == nullptr) if (_listeners == nullptr)
return; return;
for (auto listenerItemIter = _listeners->begin(); listenerItemIter != _listeners->end(); ++listenerItemIter) auto listenerList = getListenerItemsForType(eventType);
{
// After sort: priority < 0, = 0, scene graph, > 0 // After sort: priority < 0, = 0, scene graph, > 0
listenerItemIter->second->sort([](const EventListenerItem* item1, const EventListenerItem* item2) { listenerList->sort([](const EventListenerItem* item1, const EventListenerItem* item2) {
// item1 and item2 are both using fixed priority. // item1 and item2 are both using fixed priority.
if (nullptr == item1->node && nullptr == item2->node) if (nullptr == item1->node && nullptr == item2->node)
{ {
return item1->fixedPriority > item2->fixedPriority; return item1->fixedPriority > item2->fixedPriority;
} }
// item1 and item2 are both using scene graph based priority. // item1 and item2 are both using scene graph based priority.
else if (nullptr != item1->node && nullptr != item2->node) else if (nullptr != item1->node && nullptr != item2->node)
{ {
return item1->node->getEventPriority() > item2->node->getEventPriority(); return item1->node->getEventPriority() > item2->node->getEventPriority();
} }
else if (nullptr != item1->node && nullptr == item2->node) else if (nullptr != item1->node && nullptr == item2->node)
{ {
return 0 < item2->fixedPriority; return 0 < item2->fixedPriority;
} }
else if (nullptr == item1->node && nullptr != item2->node) else if (nullptr == item1->node && nullptr != item2->node)
{ {
return item1->fixedPriority < 0; return item1->fixedPriority < 0;
} }
else else
{ {
CCASSERT(false, "sort event node error..."); CCASSERT(false, "sort event node error...");
return false; return false;
} }
}); });
}
} }
std::list<EventDispatcher::EventListenerItem*>* EventDispatcher::getListeners(const std::string& eventType) std::list<EventDispatcher::EventListenerItem*>* EventDispatcher::getListenerItemsForType(const std::string &eventType)
{ {
if (_listeners != nullptr) if (_listeners != nullptr)
{ {

View File

@ -54,18 +54,22 @@ public:
/** Gets the singleton of EventDispatcher */ /** Gets the singleton of EventDispatcher */
static EventDispatcher* getInstance(); 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); 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); void addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority);
/** Unregisters a callback function by the unique ID. */ /** Remove a listener */
void removeEventListener(EventListener* listener); void removeEventListener(EventListener* listener);
/** Removes listeners by event type */ /** Removes all listeners with the same event type */
void removeListenersForEventType(const std::string& eventType); void removeListenersForEventType(const std::string& eventType);
/** Removes all listeners */ /** Removes all listeners */
@ -104,18 +108,19 @@ private:
/** Constructor of EventDispatcher */ /** Constructor of EventDispatcher */
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. */ /** 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); void dispatchTouchEvent(TouchEvent* event);
/** Gets event listener list by event type. */ /** Gets event the listener list for the event type. */
std::list<EventListenerItem*>* getListeners(const std::string& eventType); std::list<EventListenerItem*>* getListenerItemsForType(const std::string& eventType);
/** Sorts all listeners by priority */ /** Sorts the listeners of specified type by priority */
void sortAllEventListenerItems(); void sortAllEventListenerItemsForType(const std::string& eventType);
/** Remove listeners that have been unregistered. */ /** Removes all listeners that have been unregistered. */
void removeUnregisteredListeners(); void removeUnregisteredListeners();
private: private: