[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;
}
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,15 +477,15 @@ 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)
{
auto listenerList = getListenerItemsForType(eventType);
// 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.
if (nullptr == item1->node && nullptr == item2->node)
{
@ -510,10 +510,9 @@ void EventDispatcher::sortAllEventListenerItems()
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)
{

View File

@ -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<EventListenerItem*>* getListeners(const std::string& eventType);
/** Gets event the listener list for the event type. */
std::list<EventListenerItem*>* 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: