[dispatcher] Using std::vector instead of std::list for EventListenerItem container.

This commit is contained in:
James Chen 2013-09-15 21:58:54 +08:00
parent 9891077bba
commit 3e01dd5edf
2 changed files with 10 additions and 9 deletions

View File

@ -77,19 +77,19 @@ void EventDispatcher::addEventListenerWithItem(EventListenerItem* item)
{ {
if (!_listeners) if (!_listeners)
{ {
_listeners = new std::map<std::string, std::list<EventListenerItem*>*>(); _listeners = new std::map<std::string, std::vector<EventListenerItem*>*>();
} }
auto itr = _listeners->find(item->listener->type); auto itr = _listeners->find(item->listener->type);
if (itr == _listeners->end()) if (itr == _listeners->end())
{ {
_listeners->insert(std::make_pair(item->listener->type, new std::list<EventListenerItem*>())); _listeners->insert(std::make_pair(item->listener->type, new std::vector<EventListenerItem*>()));
itr = _listeners->find(item->listener->type); itr = _listeners->find(item->listener->type);
} }
auto listenerList = itr->second; auto listenerList = itr->second;
listenerList->push_front(item); listenerList->insert(listenerList->begin(), item);
} }
void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node) void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)
@ -144,7 +144,7 @@ void EventDispatcher::removeEventListener(EventListener* listener)
{ {
(*itemIter)->listener->release(); (*itemIter)->listener->release();
delete (*itemIter); delete (*itemIter);
iter->second->remove(*itemIter); iter->second->erase(itemIter);
} }
else else
{ {
@ -268,7 +268,7 @@ void EventDispatcher::dispatchTouchEvent(TouchEvent* event)
for (auto iter = touchListeners->begin(); iter != touchListeners->end(); ++iter) for (auto iter = touchListeners->begin(); iter != touchListeners->end(); ++iter)
{ {
TouchEventListener* touchEventListener = static_cast<TouchEventListener*>((*iter)->listener); auto touchEventListener = static_cast<TouchEventListener*>((*iter)->listener);
if (touchEventListener->_dispatchMode == Touch::DispatchMode::ONE_BY_ONE) if (touchEventListener->_dispatchMode == Touch::DispatchMode::ONE_BY_ONE)
{ {
@ -488,7 +488,7 @@ void EventDispatcher::sortAllEventListenerItemsForType(const std::string &eventT
auto listenerList = getListenerItemsForType(eventType); auto listenerList = getListenerItemsForType(eventType);
// After sort: priority < 0, = 0, scene graph, > 0 // After sort: priority < 0, = 0, scene graph, > 0
listenerList->sort([](const EventListenerItem* item1, const EventListenerItem* item2) { std::sort(listenerList->begin(), listenerList->end(), [](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)
{ {
@ -515,7 +515,7 @@ void EventDispatcher::sortAllEventListenerItemsForType(const std::string &eventT
}); });
} }
std::list<EventDispatcher::EventListenerItem*>* EventDispatcher::getListenerItemsForType(const std::string &eventType) std::vector<EventDispatcher::EventListenerItem*>* EventDispatcher::getListenerItemsForType(const std::string &eventType)
{ {
if (_listeners != nullptr) if (_listeners != nullptr)
{ {

View File

@ -32,6 +32,7 @@
#include <string> #include <string>
#include <map> #include <map>
#include <list> #include <list>
#include <vector>
NS_CC_BEGIN NS_CC_BEGIN
@ -115,7 +116,7 @@ private:
void dispatchTouchEvent(TouchEvent* event); void dispatchTouchEvent(TouchEvent* event);
/** Gets event the listener list for the event type. */ /** Gets event the listener list for the event type. */
std::list<EventListenerItem*>* getListenerItemsForType(const std::string& eventType); std::vector<EventListenerItem*>* getListenerItemsForType(const std::string& eventType);
/** Sorts the listeners of specified type by priority */ /** Sorts the listeners of specified type by priority */
void sortAllEventListenerItemsForType(const std::string& eventType); void sortAllEventListenerItemsForType(const std::string& eventType);
@ -127,7 +128,7 @@ private:
/** /**
* Listeners map. * Listeners map.
*/ */
std::map<std::string, std::list<EventListenerItem*>*>* _listeners; std::map<std::string, std::vector<EventListenerItem*>*>* _listeners;
int _inDispatch; int _inDispatch;
std::list<Node*> _eventNodes; std::list<Node*> _eventNodes;