2013-09-03 18:22:03 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
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 "CCEventDispatcher.h"
|
|
|
|
#include "CCEvent.h"
|
2013-09-12 17:31:37 +08:00
|
|
|
#include "CCTouchEvent.h"
|
|
|
|
#include "CCTouchEventListener.h"
|
2013-09-03 18:22:03 +08:00
|
|
|
#include "base_nodes/CCNode.h"
|
|
|
|
#include "CCDirector.h"
|
2013-09-12 17:31:37 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
class DispatchGuard
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DispatchGuard(int& count):
|
|
|
|
_count(count)
|
|
|
|
{
|
|
|
|
++_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
~DispatchGuard()
|
|
|
|
{
|
|
|
|
--_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int& _count;
|
|
|
|
};
|
|
|
|
|
|
|
|
int _eventId = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
EventDispatcher::EventDispatcher()
|
|
|
|
: _inDispatch(0)
|
|
|
|
, _listeners(nullptr)
|
|
|
|
, _isEnabled(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EventDispatcher::~EventDispatcher()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EventDispatcher* EventDispatcher::getInstance()
|
|
|
|
{
|
|
|
|
static EventDispatcher _instance;
|
|
|
|
return &_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::registerEventListenerWithItem(EventListenerItem* item)
|
|
|
|
{
|
|
|
|
if (!_listeners)
|
|
|
|
{
|
|
|
|
_listeners = new std::map<std::string, std::list<EventListenerItem*>*>();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto itr = _listeners->find(item->listener->type);
|
|
|
|
if (itr == _listeners->end())
|
|
|
|
{
|
|
|
|
_listeners->insert(std::make_pair(item->listener->type, new std::list<EventListenerItem*>()));
|
|
|
|
itr = _listeners->find(item->listener->type);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto listenerList = itr->second;
|
|
|
|
|
|
|
|
listenerList->push_front(item);
|
|
|
|
}
|
|
|
|
|
2013-09-13 18:00:56 +08:00
|
|
|
int EventDispatcher::registerEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)
|
2013-09-03 18:22:03 +08:00
|
|
|
{
|
2013-09-13 18:00:56 +08:00
|
|
|
CCASSERT(!listener->_isRegistered, "The listener has been registered.");
|
2013-09-13 16:36:54 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
if (!listener->checkAvaiable())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
EventListenerItem* item = new EventListenerItem();
|
|
|
|
item->id = ++_eventId;
|
|
|
|
item->node = node;
|
|
|
|
item->fixedPriority = 0;
|
|
|
|
item->listener = listener;
|
2013-09-13 18:00:56 +08:00
|
|
|
item->listener->retain();
|
2013-09-13 16:36:54 +08:00
|
|
|
item->listener->_isRegistered = true;
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
registerEventListenerWithItem(item);
|
|
|
|
|
|
|
|
_eventNodes.push_back(node);
|
|
|
|
node->addEventId(item->id);
|
|
|
|
|
|
|
|
return item->id;
|
|
|
|
}
|
|
|
|
|
2013-09-13 18:00:56 +08:00
|
|
|
int EventDispatcher::registerEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)
|
2013-09-03 18:22:03 +08:00
|
|
|
{
|
2013-09-13 18:00:56 +08:00
|
|
|
CCASSERT(!listener->_isRegistered, "The listener has been registered.");
|
2013-09-13 16:36:54 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
if (!listener->checkAvaiable())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
EventListenerItem* item = new EventListenerItem();
|
|
|
|
item->id = ++_eventId;
|
|
|
|
item->node = nullptr;
|
|
|
|
item->fixedPriority = fixedPriority;
|
|
|
|
item->listener = listener;
|
2013-09-13 18:00:56 +08:00
|
|
|
item->listener->retain();
|
2013-09-13 16:36:54 +08:00
|
|
|
item->listener->_isRegistered = true;
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
registerEventListenerWithItem(item);
|
|
|
|
|
|
|
|
return item->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::unregisterEventListener(int listenerId)
|
|
|
|
{
|
|
|
|
if (_listeners == nullptr || listenerId <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto iter = _listeners->begin(); iter != _listeners->end();)
|
|
|
|
{
|
|
|
|
for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter)
|
|
|
|
{
|
|
|
|
if ((*itemIter)->id == listenerId)
|
|
|
|
{
|
|
|
|
(*itemIter)->id = 0;
|
|
|
|
|
|
|
|
if (_inDispatch == 0)
|
|
|
|
{
|
2013-09-13 18:00:56 +08:00
|
|
|
(*itemIter)->listener->release();
|
2013-09-03 18:22:03 +08:00
|
|
|
delete (*itemIter);
|
|
|
|
iter->second->remove(*itemIter);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iter->second->empty())
|
|
|
|
{
|
|
|
|
auto list = iter->second;
|
|
|
|
iter = _listeners->erase(iter);
|
|
|
|
CC_SAFE_DELETE(list);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_listeners->empty())
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_listeners);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::setPriorityWithSceneGraph(int listenerId, Node* node)
|
|
|
|
{
|
|
|
|
if (_listeners == nullptr || listenerId <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto iter = _listeners->begin(); iter != _listeners->end(); ++iter)
|
|
|
|
{
|
|
|
|
for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter)
|
|
|
|
{
|
|
|
|
auto item = *itemIter;
|
|
|
|
if (item->id == listenerId)
|
|
|
|
{
|
|
|
|
// FIXME: fixed priority --> scene graph's priority.
|
|
|
|
item->fixedPriority = 0;
|
|
|
|
item->node = node;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::setPriorityWithFixedValue(int listenerId, int fixedPriority)
|
|
|
|
{
|
|
|
|
if (_listeners == nullptr || listenerId <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto iter = _listeners->begin(); iter != _listeners->end(); ++iter)
|
|
|
|
{
|
|
|
|
for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter)
|
|
|
|
{
|
|
|
|
auto item = *itemIter;
|
|
|
|
if (item->id == listenerId)
|
|
|
|
{
|
|
|
|
// FIXME: scene graph's priority --> fixed priority.
|
|
|
|
item->fixedPriority = fixedPriority;
|
|
|
|
item->node = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::dispatchEvent(Event* event)
|
|
|
|
{
|
|
|
|
if (_listeners == nullptr || !_isEnabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sortAllEventListenerItems();
|
|
|
|
|
|
|
|
DispatchGuard guard(_inDispatch);
|
|
|
|
|
2013-09-12 17:31:37 +08:00
|
|
|
if (event->_type == TouchEvent::EVENT_TYPE)
|
|
|
|
{
|
|
|
|
dispatchTouchEvent(static_cast<TouchEvent*>(event));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
if (_listeners)
|
|
|
|
{
|
|
|
|
auto iter = _listeners->find(event->getType());
|
|
|
|
if (iter != _listeners->end())
|
|
|
|
{
|
|
|
|
auto listenerList = iter->second;
|
|
|
|
for (auto listenerIter = listenerList->begin(); listenerIter != listenerList->end(); ++listenerIter)
|
|
|
|
{
|
|
|
|
CCASSERT(*listenerIter, "listener is invalid.");
|
|
|
|
|
|
|
|
(*listenerIter)->listener->onEvent(event);
|
|
|
|
|
|
|
|
if (event->isStopped())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-12 20:45:35 +08:00
|
|
|
|
|
|
|
removeUnregisteredListeners();
|
2013-09-03 18:22:03 +08:00
|
|
|
}
|
|
|
|
|
2013-09-12 17:31:37 +08:00
|
|
|
void EventDispatcher::dispatchTouchEvent(TouchEvent* event)
|
|
|
|
{
|
|
|
|
auto touchListeners = getListeners(TouchEvent::EVENT_TYPE);
|
2013-09-12 20:45:35 +08:00
|
|
|
if (touchListeners == nullptr)
|
|
|
|
return;
|
2013-09-12 17:31:37 +08:00
|
|
|
|
|
|
|
std::vector<EventDispatcher::EventListenerItem*> oneByOnelisteners;
|
|
|
|
oneByOnelisteners.reserve(touchListeners->size());
|
|
|
|
|
|
|
|
std::vector<EventDispatcher::EventListenerItem*> allInOnelisteners;
|
|
|
|
allInOnelisteners.reserve(touchListeners->size());
|
|
|
|
|
|
|
|
for (auto iter = touchListeners->begin(); iter != touchListeners->end(); ++iter)
|
|
|
|
{
|
2013-09-13 18:00:56 +08:00
|
|
|
TouchEventListener* touchEventListener = static_cast<TouchEventListener*>((*iter)->listener);
|
2013-09-12 17:31:37 +08:00
|
|
|
|
|
|
|
if (touchEventListener->_dispatchMode == Touch::DispatchMode::ONE_BY_ONE)
|
|
|
|
{
|
|
|
|
oneByOnelisteners.push_back(*iter);
|
|
|
|
}
|
|
|
|
else if (touchEventListener->_dispatchMode == Touch::DispatchMode::ALL_AT_ONCE)
|
|
|
|
{
|
|
|
|
allInOnelisteners.push_back(*iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(false, "Not supported touch listener type.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isNeedsMutableSet = (oneByOnelisteners.size() > 0 && allInOnelisteners.size() > 0);
|
|
|
|
|
|
|
|
std::vector<Touch*> orignalTouches = event->getTouches();
|
|
|
|
std::vector<Touch*> mutableTouches(orignalTouches.size());
|
|
|
|
std::copy(orignalTouches.begin(), orignalTouches.end(), mutableTouches.begin());
|
|
|
|
|
|
|
|
//
|
|
|
|
// process the target handlers 1st
|
|
|
|
//
|
|
|
|
if (oneByOnelisteners.size() > 0)
|
|
|
|
{
|
|
|
|
auto mutableTouchesIter = mutableTouches.begin();
|
|
|
|
auto touchesIter = orignalTouches.begin();
|
|
|
|
|
|
|
|
for (; touchesIter != orignalTouches.end(); ++touchesIter)
|
|
|
|
{
|
|
|
|
bool isSwallowed = false;
|
|
|
|
auto oneByOneIter = oneByOnelisteners.begin();
|
|
|
|
for (; oneByOneIter != oneByOnelisteners.end(); ++oneByOneIter)
|
|
|
|
{
|
2013-09-12 20:45:35 +08:00
|
|
|
// Skip if the listener was removed.
|
|
|
|
if ((*oneByOneIter)->id == 0)
|
|
|
|
continue;
|
2013-09-13 16:36:54 +08:00
|
|
|
|
|
|
|
event->setCurrentTarget((*oneByOneIter)->node);
|
2013-09-12 20:45:35 +08:00
|
|
|
|
2013-09-12 17:31:37 +08:00
|
|
|
bool isClaimed = false;
|
|
|
|
std::vector<Touch*>::iterator removedIter;
|
|
|
|
|
2013-09-13 18:00:56 +08:00
|
|
|
auto touchEventListener = static_cast<TouchEventListener*>((*oneByOneIter)->listener);
|
2013-09-12 17:31:37 +08:00
|
|
|
TouchEvent::EventCode eventCode = event->getEventCode();
|
|
|
|
|
|
|
|
if (eventCode == TouchEvent::EventCode::BEGAN)
|
|
|
|
{
|
|
|
|
if (touchEventListener->onTouchBegan)
|
|
|
|
{
|
|
|
|
isClaimed = touchEventListener->onTouchBegan(*touchesIter, event);
|
|
|
|
if (isClaimed)
|
|
|
|
{
|
|
|
|
touchEventListener->_claimedTouches.push_back(*touchesIter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (touchEventListener->_claimedTouches.size() > 0
|
|
|
|
&& ((removedIter = std::find(touchEventListener->_claimedTouches.begin(), touchEventListener->_claimedTouches.end(), *touchesIter)) != touchEventListener->_claimedTouches.end()))
|
|
|
|
{
|
|
|
|
isClaimed = true;
|
|
|
|
|
|
|
|
switch (eventCode)
|
|
|
|
{
|
|
|
|
case TouchEvent::EventCode::MOVED:
|
|
|
|
if (touchEventListener->onTouchMoved)
|
|
|
|
{
|
|
|
|
touchEventListener->onTouchMoved(*touchesIter, event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TouchEvent::EventCode::ENDED:
|
|
|
|
if (touchEventListener->onTouchEnded)
|
|
|
|
{
|
|
|
|
touchEventListener->onTouchEnded(*touchesIter, event);
|
|
|
|
}
|
2013-09-13 16:36:54 +08:00
|
|
|
touchEventListener->_claimedTouches.erase(removedIter);
|
2013-09-12 17:31:37 +08:00
|
|
|
break;
|
|
|
|
case TouchEvent::EventCode::CANCELLED:
|
|
|
|
if (touchEventListener->onTouchCancelled)
|
|
|
|
{
|
|
|
|
touchEventListener->onTouchCancelled(*touchesIter, event);
|
|
|
|
}
|
2013-09-13 16:36:54 +08:00
|
|
|
touchEventListener->_claimedTouches.erase(removedIter);
|
2013-09-12 17:31:37 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CCASSERT(false, "The eventcode is invalid.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-12 20:45:35 +08:00
|
|
|
// If the event was stopped, return directly.
|
|
|
|
if (event->isStopped())
|
|
|
|
{
|
|
|
|
removeUnregisteredListeners();
|
|
|
|
return;
|
|
|
|
}
|
2013-09-12 17:31:37 +08:00
|
|
|
|
|
|
|
CCASSERT((*touchesIter)->getID() == (*mutableTouchesIter)->getID(), "");
|
|
|
|
|
|
|
|
if (isClaimed && touchEventListener->_needSwallow)
|
|
|
|
{
|
|
|
|
if (isNeedsMutableSet)
|
|
|
|
{
|
|
|
|
mutableTouchesIter = mutableTouches.erase(mutableTouchesIter);
|
|
|
|
isSwallowed = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isSwallowed)
|
|
|
|
++mutableTouchesIter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// process standard handlers 2nd
|
|
|
|
//
|
|
|
|
if (allInOnelisteners.size() > 0 && mutableTouches.size() > 0)
|
|
|
|
{
|
|
|
|
for (auto allInOneIter = allInOnelisteners.begin(); allInOneIter != allInOnelisteners.end(); ++allInOneIter)
|
|
|
|
{
|
|
|
|
// Skip if the listener was removed.
|
|
|
|
if ((*allInOneIter)->id == 0)
|
|
|
|
continue;
|
|
|
|
|
2013-09-13 16:36:54 +08:00
|
|
|
event->setCurrentTarget((*allInOneIter)->node);
|
|
|
|
|
2013-09-13 18:00:56 +08:00
|
|
|
auto touchEventListener = static_cast<TouchEventListener*>((*allInOneIter)->listener);
|
2013-09-12 17:31:37 +08:00
|
|
|
|
|
|
|
switch (event->getEventCode())
|
|
|
|
{
|
|
|
|
case TouchEvent::EventCode::BEGAN:
|
|
|
|
if (touchEventListener->onTouchesBegan)
|
|
|
|
{
|
|
|
|
touchEventListener->onTouchesBegan(mutableTouches, event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TouchEvent::EventCode::MOVED:
|
|
|
|
if (touchEventListener->onTouchesMoved)
|
|
|
|
{
|
|
|
|
touchEventListener->onTouchesMoved(mutableTouches, event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TouchEvent::EventCode::ENDED:
|
|
|
|
if (touchEventListener->onTouchesEnded)
|
|
|
|
{
|
|
|
|
touchEventListener->onTouchesEnded(mutableTouches, event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TouchEvent::EventCode::CANCELLED:
|
|
|
|
if (touchEventListener->onTouchesCancelled)
|
|
|
|
{
|
|
|
|
touchEventListener->onTouchesCancelled(mutableTouches, event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CCASSERT(false, "The eventcode is invalid.");
|
|
|
|
break;
|
|
|
|
}
|
2013-09-12 20:45:35 +08:00
|
|
|
|
|
|
|
// If the event was stopped, return directly.
|
|
|
|
if (event->isStopped())
|
|
|
|
{
|
|
|
|
removeUnregisteredListeners();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeUnregisteredListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::removeUnregisteredListeners()
|
|
|
|
{
|
|
|
|
if (!_listeners)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto listenerItemIter = _listeners->begin();
|
|
|
|
while ( listenerItemIter != _listeners->end())
|
|
|
|
{
|
|
|
|
auto removeIterBegin = std::remove_if(listenerItemIter->second->begin(), listenerItemIter->second->end(), [](const EventListenerItem* item){
|
|
|
|
return item->id == 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (auto iter = removeIterBegin; iter != listenerItemIter->second->end(); ++iter)
|
|
|
|
{
|
|
|
|
delete (*iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
listenerItemIter->second->erase(removeIterBegin, listenerItemIter->second->end());
|
|
|
|
|
|
|
|
if (listenerItemIter->second->empty())
|
|
|
|
{
|
|
|
|
delete listenerItemIter->second;
|
|
|
|
listenerItemIter = _listeners->erase(listenerItemIter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++listenerItemIter;
|
2013-09-12 17:31:37 +08:00
|
|
|
}
|
|
|
|
}
|
2013-09-12 20:45:35 +08:00
|
|
|
|
|
|
|
if (_listeners->empty())
|
|
|
|
{
|
|
|
|
delete _listeners;
|
|
|
|
_listeners = nullptr;
|
|
|
|
}
|
2013-09-12 17:31:37 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void EventDispatcher::sortAllEventListenerItems()
|
|
|
|
{
|
2013-09-12 20:45:35 +08:00
|
|
|
if (_listeners == nullptr)
|
|
|
|
return;
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<EventDispatcher::EventListenerItem*>* EventDispatcher::getListeners(const std::string& eventType)
|
|
|
|
{
|
2013-09-12 20:45:35 +08:00
|
|
|
if (_listeners != nullptr)
|
2013-09-03 18:22:03 +08:00
|
|
|
{
|
2013-09-12 20:45:35 +08:00
|
|
|
auto iter = _listeners->find(eventType);
|
|
|
|
if (iter != _listeners->end())
|
|
|
|
{
|
|
|
|
return iter->second;
|
|
|
|
}
|
2013-09-03 18:22:03 +08:00
|
|
|
}
|
2013-09-12 20:45:35 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-12 20:45:35 +08:00
|
|
|
void EventDispatcher::removeListenersForEventType(const std::string& eventType)
|
|
|
|
{
|
|
|
|
if (_listeners == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto listenerItemIter = _listeners->find(eventType);
|
|
|
|
if (listenerItemIter != _listeners->end())
|
|
|
|
{
|
|
|
|
for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end(); ++iter)
|
|
|
|
{
|
2013-09-13 18:00:56 +08:00
|
|
|
(*iter)->listener->release();
|
2013-09-12 20:45:35 +08:00
|
|
|
delete (*iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
listenerItemIter->second->clear();
|
|
|
|
|
|
|
|
delete listenerItemIter->second;
|
|
|
|
|
|
|
|
_listeners->erase(listenerItemIter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::removeAllListeners()
|
|
|
|
{
|
|
|
|
if (_listeners == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto listenerItemIter = _listeners->begin(); listenerItemIter != _listeners->end(); ++listenerItemIter)
|
|
|
|
{
|
|
|
|
for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end(); ++iter)
|
|
|
|
{
|
2013-09-13 18:00:56 +08:00
|
|
|
(*iter)->listener->release();
|
2013-09-12 20:45:35 +08:00
|
|
|
delete (*iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
listenerItemIter->second->clear();
|
|
|
|
|
|
|
|
delete listenerItemIter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete _listeners;
|
|
|
|
_listeners = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
NS_CC_END
|