axmol/cocos/2d/CCEventDispatcher.cpp

1135 lines
33 KiB
C++
Raw Normal View History

2013-09-20 22:23:13 +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-21 14:40:54 +08:00
#include "CCEventTouch.h"
#include "CCEventCustom.h"
2013-09-21 14:40:54 +08:00
#include "CCEventListenerTouch.h"
#include "CCNode.h"
2013-09-20 22:23:13 +08:00
#include "CCDirector.h"
#include <algorithm>
#define DUMP_LISTENER_ITEM_PRIORITY_INFO 0
2013-09-20 22:23:13 +08:00
namespace
{
class DispatchGuard
{
public:
DispatchGuard(int& count):
_count(count)
{
++_count;
}
~DispatchGuard()
{
--_count;
}
private:
int& _count;
};
}
NS_CC_BEGIN
static EventListener::ListenerID getListenerID(Event* event)
{
EventListener::ListenerID ret;
switch (event->getType())
{
case Event::Type::ACCELERATION:
ret = static_cast<EventListener::ListenerID>(EventListener::Type::ACCELERATION);
break;
case Event::Type::CUSTOM:
{
auto customEvent = static_cast<EventCustom*>(event);
auto listenerID = std::hash<std::string>()(customEvent->getEventName());
ret = static_cast<EventListener::ListenerID>(listenerID);
}
break;
case Event::Type::KEYBOARD:
ret = static_cast<EventListener::ListenerID>(EventListener::Type::KEYBOARD);
break;
2013-10-31 14:19:36 +08:00
case Event::Type::MOUSE:
ret = static_cast<EventListener::ListenerID>(EventListener::Type::MOUSE);
break;
case Event::Type::TOUCH:
// Touch listener is very special, it contains two kinds of listeners, EventListenerTouchOneByOne and EventListenerTouchAllAtOnce.
// return UNKNOW instead.
ret = static_cast<EventListener::ListenerID>(EventListener::Type::UNKNOWN);
break;
default:
CCASSERT(false, "Invalid type!");
break;
}
return ret;
}
EventDispatcher::EventListenerVector::EventListenerVector()
: _sceneGraphListeners(nullptr)
, _fixedListeners(nullptr)
, _gt0Index(0)
{
}
EventDispatcher::EventListenerVector::~EventListenerVector()
{
CC_SAFE_DELETE(_sceneGraphListeners);
CC_SAFE_DELETE(_fixedListeners);
}
size_t EventDispatcher::EventListenerVector::size() const
{
size_t ret = 0;
if (_sceneGraphListeners)
ret += _sceneGraphListeners->size();
if (_fixedListeners)
ret += _fixedListeners->size();
return ret;
}
bool EventDispatcher::EventListenerVector::empty() const
{
return (_sceneGraphListeners == nullptr || _sceneGraphListeners->empty())
&& (_fixedListeners == nullptr || _fixedListeners->empty());
}
void EventDispatcher::EventListenerVector::push_back(EventListener* listener)
{
if (listener->getFixedPriority() == 0)
{
if (_sceneGraphListeners == nullptr)
{
_sceneGraphListeners = new std::vector<EventListener*>();
_sceneGraphListeners->reserve(100);
}
_sceneGraphListeners->push_back(listener);
}
else
{
if (_fixedListeners == nullptr)
{
_fixedListeners = new std::vector<EventListener*>();
_fixedListeners->reserve(100);
}
_fixedListeners->push_back(listener);
}
}
2013-10-24 17:27:22 +08:00
void EventDispatcher::EventListenerVector::clearSceneGraphListeners()
{
if (_sceneGraphListeners)
{
_sceneGraphListeners->clear();
delete _sceneGraphListeners;
_sceneGraphListeners = nullptr;
}
2013-10-24 17:27:22 +08:00
}
void EventDispatcher::EventListenerVector::clearFixedListeners()
{
if (_fixedListeners)
{
_fixedListeners->clear();
delete _fixedListeners;
_fixedListeners = nullptr;
}
2013-09-20 22:23:13 +08:00
}
2013-10-24 17:27:22 +08:00
void EventDispatcher::EventListenerVector::clear()
{
clearSceneGraphListeners();
clearFixedListeners();
}
2013-09-20 22:23:13 +08:00
EventDispatcher::EventDispatcher()
: _inDispatch(0)
, _isEnabled(true)
, _nodePriorityIndex(0)
2013-09-20 22:23:13 +08:00
{
_toAddedListeners.reserve(50);
}
EventDispatcher::~EventDispatcher()
{
removeAllEventListeners();
2013-09-20 22:23:13 +08:00
}
void EventDispatcher::visitTarget(Node* node)
2013-10-23 17:26:14 +08:00
{
long i = 0;
auto children = node->getChildren();
long childrenCount = children.count();
if(childrenCount > 0)
{
Node* child = nullptr;
// visit children zOrder < 0
for( ; i < childrenCount; i++ )
{
child = children[i];
if ( child && child->getZOrder() < 0 )
visitTarget(child);
else
break;
}
_nodePriorityMap.insert(std::make_pair(node, ++_nodePriorityIndex));
for( ; i < childrenCount; i++ )
{
child = children[i];
if (child)
visitTarget(child);
}
}
else
{
_nodePriorityMap.insert(std::make_pair(node, ++_nodePriorityIndex));
}
}
void EventDispatcher::pauseTarget(Node* node)
{
auto listenerIter = _nodeListenersMap.find(node);
if (listenerIter != _nodeListenersMap.end())
{
auto listeners = listenerIter->second;
for (auto& l : *listeners)
{
l->setPaused(true);
}
}
}
void EventDispatcher::resumeTarget(Node* node)
{
auto listenerIter = _nodeListenersMap.find(node);
if (listenerIter != _nodeListenersMap.end())
{
auto listeners = listenerIter->second;
for (auto& l : *listeners)
{
l->setPaused(false);
}
}
setDirtyForNode(node);
}
void EventDispatcher::cleanTarget(Node* node)
{
auto listenerIter = _nodeListenersMap.find(node);
if (listenerIter != _nodeListenersMap.end())
{
auto listeners = listenerIter->second;
2013-10-23 17:13:03 +08:00
auto listenersCopy = *listeners;
for (auto& l : listenersCopy)
{
removeEventListener(l);
}
}
}
void EventDispatcher::associateNodeAndEventListener(Node* node, EventListener* listener)
{
std::vector<EventListener*>* listeners = nullptr;
auto found = _nodeListenersMap.find(node);
if (found != _nodeListenersMap.end())
{
listeners = found->second;
}
else
{
listeners = new std::vector<EventListener*>();
}
listeners->push_back(listener);
_nodeListenersMap.insert(std::make_pair(node, listeners));
}
void EventDispatcher::dissociateNodeAndEventListener(Node* node, EventListener* listener)
{
std::vector<EventListener*>* listeners = nullptr;
auto found = _nodeListenersMap.find(node);
if (found != _nodeListenersMap.end())
{
listeners = found->second;
auto iter = std::find(listeners->begin(), listeners->end(), listener);
if (iter != listeners->end())
{
listeners->erase(iter);
}
if (listeners->empty())
{
_nodeListenersMap.erase(found);
2013-10-24 17:27:22 +08:00
delete listeners;
}
}
}
void EventDispatcher::addEventListener(EventListener* listener)
2013-09-20 22:23:13 +08:00
{
if (_inDispatch == 0)
{
EventListenerVector* listenerList = nullptr;
2013-09-20 22:23:13 +08:00
auto iter = _listeners.find(listener->getListenerID());
2013-09-20 22:23:13 +08:00
if (iter == _listeners.end())
{
listenerList = new EventListenerVector();
_listeners.insert(std::make_pair(listener->getListenerID(), listenerList));
2013-09-20 22:23:13 +08:00
}
else
{
listenerList = iter->second;
}
listenerList->push_back(listener);
if (listener->getFixedPriority() == 0)
{
setDirty(listener->getListenerID(), DirtyFlag::SCENE_GRAPH_PRIORITY);
}
else
{
setDirty(listener->getListenerID(), DirtyFlag::FIXED_PRITORY);
}
2013-09-20 22:23:13 +08:00
}
else
{
_toAddedListeners.push_back(listener);
2013-09-20 22:23:13 +08:00
}
}
void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)
{
CCASSERT(listener && node, "Invalid parameters.");
CCASSERT(!listener->isRegistered(), "The listener has been registered.");
2013-09-20 22:23:13 +08:00
if (!listener->checkAvailable())
2013-09-20 22:23:13 +08:00
return;
listener->setSceneGraphPriority(node);
listener->setFixedPriority(0);
listener->setRegistered(true);
listener->retain();
addEventListener(listener);
associateNodeAndEventListener(node, listener);
if (node->isRunning())
{
resumeTarget(node);
}
2013-09-20 22:23:13 +08:00
}
void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)
{
CCASSERT(listener, "Invalid parameters.");
CCASSERT(!listener->isRegistered(), "The listener has been registered.");
2013-09-20 22:23:13 +08:00
CCASSERT(fixedPriority != 0, "0 priority is forbidden for fixed priority since it's used for scene graph based priority.");
if (!listener->checkAvailable())
2013-09-20 22:23:13 +08:00
return;
listener->setSceneGraphPriority(nullptr);
listener->setFixedPriority(fixedPriority);
listener->setRegistered(true);
listener->setPaused(false);
listener->retain();
2013-09-20 22:23:13 +08:00
addEventListener(listener);
2013-09-20 22:23:13 +08:00
}
void EventDispatcher::removeEventListener(EventListener* listener)
{
if (listener == nullptr)
return;
bool isFound = false;
auto removeListenerInVector = [&](std::vector<EventListener*>* listeners){
if (listeners == nullptr)
return;
for (auto iter = listeners->begin(); iter != listeners->end(); ++iter)
{
auto l = *iter;
if (l == listener)
2013-09-20 22:23:13 +08:00
{
2013-10-24 11:17:29 +08:00
CC_SAFE_RETAIN(l);
l->setRegistered(false);
if (l->getSceneGraphPriority() != nullptr)
2013-09-20 22:23:13 +08:00
{
dissociateNodeAndEventListener(l->getSceneGraphPriority(), l);
2013-09-20 22:23:13 +08:00
}
if (_inDispatch == 0)
{
listeners->erase(iter);
2013-10-24 17:27:22 +08:00
CC_SAFE_RELEASE(l);
2013-09-20 22:23:13 +08:00
}
2013-09-20 22:23:13 +08:00
isFound = true;
break;
2013-09-20 22:23:13 +08:00
}
}
};
for (auto iter = _listeners.begin(); iter != _listeners.end();)
{
auto listeners = iter->second;
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
2013-10-24 11:17:29 +08:00
removeListenerInVector(sceneGraphPriorityListeners);
if (!isFound)
{
removeListenerInVector(fixedPriorityListeners);
}
2013-09-20 22:23:13 +08:00
if (iter->second->empty())
{
_priorityDirtyFlagMap.erase(listener->getListenerID());
2013-09-20 22:23:13 +08:00
auto list = iter->second;
iter = _listeners.erase(iter);
CC_SAFE_DELETE(list);
}
else
{
++iter;
}
if (isFound)
break;
}
if (isFound)
{
2013-09-20 22:23:13 +08:00
CC_SAFE_RELEASE(listener);
}
else
{
for(auto iter = _toAddedListeners.begin(); iter != _toAddedListeners.end(); ++iter)
{
if (*iter == listener)
2013-11-02 15:03:54 +08:00
{
_toAddedListeners.erase(iter);
break;
}
}
2013-09-20 22:23:13 +08:00
}
}
void EventDispatcher::setPriority(EventListener* listener, int fixedPriority)
{
if (listener == nullptr)
return;
for (auto iter = _listeners.begin(); iter != _listeners.end(); ++iter)
{
auto fixedPriorityListeners = iter->second->getFixedPriorityListeners();
if (fixedPriorityListeners)
2013-09-20 22:23:13 +08:00
{
auto found = std::find(fixedPriorityListeners->begin(), fixedPriorityListeners->end(), listener);
if (found != fixedPriorityListeners->end())
2013-09-20 22:23:13 +08:00
{
CCASSERT(listener->getSceneGraphPriority() == nullptr, "Can't set fixed priority with scene graph based listener.");
2013-09-20 22:23:13 +08:00
if (listener->getFixedPriority() != fixedPriority)
2013-09-20 22:23:13 +08:00
{
listener->setFixedPriority(fixedPriority);
setDirty(listener->getListenerID(), DirtyFlag::FIXED_PRITORY);
2013-09-20 22:23:13 +08:00
}
return;
}
}
}
}
void EventDispatcher::dispatchEventToListeners(EventListenerVector* listeners, std::function<bool(EventListener*)> onEvent)
{
bool shouldStopPropagation = false;
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
2013-11-20 10:39:30 +08:00
long i = 0;
// priority < 0
if (fixedPriorityListeners)
{
for (; !fixedPriorityListeners->empty() && i < listeners->getGt0Index(); ++i)
{
auto l = fixedPriorityListeners->at(i);
2013-10-24 11:17:29 +08:00
if (!l->isPaused() && l->isRegistered() && onEvent(l))
{
shouldStopPropagation = true;
break;
}
}
}
if (sceneGraphPriorityListeners)
{
if (!shouldStopPropagation)
{
// priority == 0, scene graph priority
2013-10-24 11:17:29 +08:00
for (auto& l : *sceneGraphPriorityListeners)
{
2013-10-24 11:17:29 +08:00
if (!l->isPaused() && l->isRegistered() && onEvent(l))
{
shouldStopPropagation = true;
break;
}
}
}
}
if (fixedPriorityListeners)
{
if (!shouldStopPropagation)
{
// priority > 0
2013-11-20 10:39:30 +08:00
for (; i < static_cast<long>(fixedPriorityListeners->size()); ++i)
{
auto l = fixedPriorityListeners->at(i);
2013-10-24 11:17:29 +08:00
if (!l->isPaused() && l->isRegistered() && onEvent(fixedPriorityListeners->at(i)))
{
shouldStopPropagation = true;
break;
}
}
}
}
}
void EventDispatcher::dispatchEvent(Event* event)
2013-09-20 22:23:13 +08:00
{
if (!_isEnabled)
return;
updateDirtyFlagForSceneGraph();
DispatchGuard guard(_inDispatch);
2013-09-20 22:23:13 +08:00
if (event->getType() == Event::Type::TOUCH)
2013-09-20 22:23:13 +08:00
{
dispatchTouchEvent(static_cast<EventTouch*>(event));
return;
2013-09-20 22:23:13 +08:00
}
auto listenerID = getListenerID(event);
sortEventListeners(listenerID);
2013-09-20 22:23:13 +08:00
auto iter = _listeners.find(listenerID);
2013-09-20 22:23:13 +08:00
if (iter != _listeners.end())
{
auto listeners = iter->second;
auto onEvent = [&event](EventListener* listener) -> bool{
event->setCurrentTarget(listener->getSceneGraphPriority());
listener->_onEvent(event);
return event->isStopped();
};
dispatchEventToListeners(listeners, onEvent);
2013-09-20 22:23:13 +08:00
}
updateListeners(event);
2013-09-20 22:23:13 +08:00
}
void EventDispatcher::dispatchTouchEvent(EventTouch* event)
{
auto touchOneByOneID = static_cast<EventListener::ListenerID>(EventListener::Type::TOUCH_ONE_BY_ONE);
auto touchAllAtOnceID = static_cast<EventListener::ListenerID>(EventListener::Type::TOUCH_ALL_AT_ONCE);
sortEventListeners(touchOneByOneID);
sortEventListeners(touchAllAtOnceID);
auto oneByOnelisteners = getListeners(touchOneByOneID);
auto allAtOncelisteners = getListeners(touchAllAtOnceID);
2013-09-20 22:23:13 +08:00
// If there aren't any touch listeners, return directly.
if (nullptr == oneByOnelisteners && nullptr == allAtOncelisteners)
return;
2013-09-20 22:23:13 +08:00
bool isNeedsMutableSet = (oneByOnelisteners && allAtOncelisteners);
2013-09-20 22:23:13 +08:00
std::vector<Touch*> orignalTouches = event->getTouches();
std::vector<Touch*> mutableTouches(orignalTouches.size());
std::copy(orignalTouches.begin(), orignalTouches.end(), mutableTouches.begin());
2013-09-20 22:23:13 +08:00
//
// process the target handlers 1st
//
if (oneByOnelisteners)
2013-09-20 22:23:13 +08:00
{
auto mutableTouchesIter = mutableTouches.begin();
auto touchesIter = orignalTouches.begin();
for (; touchesIter != orignalTouches.end(); ++touchesIter)
{
bool isSwallowed = false;
auto onTouchEvent = [&](EventListener* l) -> bool { // Return true to break
EventListenerTouchOneByOne* listener = static_cast<EventListenerTouchOneByOne*>(l);
2013-09-20 22:23:13 +08:00
// Skip if the listener was removed.
if (!listener->_isRegistered)
return false;
2013-09-20 22:23:13 +08:00
event->setCurrentTarget(listener->_node);
2013-09-20 22:23:13 +08:00
bool isClaimed = false;
std::vector<Touch*>::iterator removedIter;
EventTouch::EventCode eventCode = event->getEventCode();
if (eventCode == EventTouch::EventCode::BEGAN)
{
if (listener->onTouchBegan)
2013-09-20 22:23:13 +08:00
{
isClaimed = listener->onTouchBegan(*touchesIter, event);
if (isClaimed && listener->_isRegistered)
2013-09-20 22:23:13 +08:00
{
listener->_claimedTouches.push_back(*touchesIter);
2013-09-20 22:23:13 +08:00
}
}
}
else if (listener->_claimedTouches.size() > 0
&& ((removedIter = std::find(listener->_claimedTouches.begin(), listener->_claimedTouches.end(), *touchesIter)) != listener->_claimedTouches.end()))
2013-09-20 22:23:13 +08:00
{
isClaimed = true;
switch (eventCode)
{
case EventTouch::EventCode::MOVED:
if (listener->onTouchMoved)
2013-09-20 22:23:13 +08:00
{
listener->onTouchMoved(*touchesIter, event);
2013-09-20 22:23:13 +08:00
}
break;
case EventTouch::EventCode::ENDED:
if (listener->onTouchEnded)
2013-09-20 22:23:13 +08:00
{
listener->onTouchEnded(*touchesIter, event);
2013-09-20 22:23:13 +08:00
}
if (listener->_isRegistered)
2013-09-20 22:23:13 +08:00
{
listener->_claimedTouches.erase(removedIter);
2013-09-20 22:23:13 +08:00
}
break;
case EventTouch::EventCode::CANCELLED:
if (listener->onTouchCancelled)
2013-09-20 22:23:13 +08:00
{
listener->onTouchCancelled(*touchesIter, event);
2013-09-20 22:23:13 +08:00
}
if (listener->_isRegistered)
2013-09-20 22:23:13 +08:00
{
listener->_claimedTouches.erase(removedIter);
2013-09-20 22:23:13 +08:00
}
break;
default:
CCASSERT(false, "The eventcode is invalid.");
break;
}
}
// If the event was stopped, return directly.
if (event->isStopped())
{
updateListeners(event);
return true;
2013-09-20 22:23:13 +08:00
}
CCASSERT((*touchesIter)->getID() == (*mutableTouchesIter)->getID(), "");
if (isClaimed && listener->_isRegistered && listener->_needSwallow)
2013-09-20 22:23:13 +08:00
{
if (isNeedsMutableSet)
{
mutableTouchesIter = mutableTouches.erase(mutableTouchesIter);
isSwallowed = true;
}
return true;
2013-09-20 22:23:13 +08:00
}
return false;
};
//
dispatchEventToListeners(oneByOnelisteners, onTouchEvent);
if (event->isStopped())
{
return;
2013-09-20 22:23:13 +08:00
}
if (!isSwallowed)
++mutableTouchesIter;
}
}
//
// process standard handlers 2nd
//
if (allAtOncelisteners && mutableTouches.size() > 0)
2013-09-20 22:23:13 +08:00
{
auto onTouchesEvent = [&](EventListener* l) -> bool{
EventListenerTouchAllAtOnce* listener = static_cast<EventListenerTouchAllAtOnce*>(l);
2013-09-20 22:23:13 +08:00
// Skip if the listener was removed.
if (!listener->_isRegistered)
return false;
2013-09-20 22:23:13 +08:00
event->setCurrentTarget(listener->_node);
2013-09-20 22:23:13 +08:00
switch (event->getEventCode())
{
case EventTouch::EventCode::BEGAN:
if (listener->onTouchesBegan)
2013-09-20 22:23:13 +08:00
{
listener->onTouchesBegan(mutableTouches, event);
2013-09-20 22:23:13 +08:00
}
break;
case EventTouch::EventCode::MOVED:
if (listener->onTouchesMoved)
2013-09-20 22:23:13 +08:00
{
listener->onTouchesMoved(mutableTouches, event);
2013-09-20 22:23:13 +08:00
}
break;
case EventTouch::EventCode::ENDED:
if (listener->onTouchesEnded)
2013-09-20 22:23:13 +08:00
{
listener->onTouchesEnded(mutableTouches, event);
2013-09-20 22:23:13 +08:00
}
break;
case EventTouch::EventCode::CANCELLED:
if (listener->onTouchesCancelled)
2013-09-20 22:23:13 +08:00
{
listener->onTouchesCancelled(mutableTouches, event);
2013-09-20 22:23:13 +08:00
}
break;
default:
CCASSERT(false, "The eventcode is invalid.");
break;
}
// If the event was stopped, return directly.
if (event->isStopped())
{
updateListeners(event);
return false;
2013-09-20 22:23:13 +08:00
}
return false;
};
dispatchEventToListeners(allAtOncelisteners, onTouchesEvent);
if (event->isStopped())
{
return;
2013-09-20 22:23:13 +08:00
}
}
updateListeners(event);
2013-09-20 22:23:13 +08:00
}
void EventDispatcher::updateListeners(Event* event)
2013-09-20 22:23:13 +08:00
{
auto onUpdateListeners = [this](EventListener::ListenerID listenerID)
2013-09-20 22:23:13 +08:00
{
auto listenersIter = _listeners.find(listenerID);
if (listenersIter == _listeners.end())
return;
auto listeners = listenersIter->second;
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
if (sceneGraphPriorityListeners)
2013-09-20 22:23:13 +08:00
{
for (auto iter = sceneGraphPriorityListeners->begin(); iter != sceneGraphPriorityListeners->end();)
2013-09-20 22:23:13 +08:00
{
auto l = *iter;
if (!l->isRegistered())
{
iter = sceneGraphPriorityListeners->erase(iter);
2013-10-24 11:17:29 +08:00
l->release();
}
else
{
++iter;
}
2013-09-20 22:23:13 +08:00
}
}
if (fixedPriorityListeners)
{
for (auto iter = fixedPriorityListeners->begin(); iter != fixedPriorityListeners->end();)
2013-09-20 22:23:13 +08:00
{
auto l = *iter;
if (!l->isRegistered())
{
iter = fixedPriorityListeners->erase(iter);
2013-10-24 11:17:29 +08:00
l->release();
}
else
{
++iter;
}
2013-09-20 22:23:13 +08:00
}
}
2013-10-24 17:27:22 +08:00
if (sceneGraphPriorityListeners && sceneGraphPriorityListeners->empty())
{
listeners->clearSceneGraphListeners();
}
if (fixedPriorityListeners && fixedPriorityListeners->empty())
{
listeners->clearFixedListeners();
}
if (listenersIter->second->empty())
2013-09-20 22:23:13 +08:00
{
_priorityDirtyFlagMap.erase(listenersIter->first);
delete listenersIter->second;
listenersIter = _listeners.erase(listenersIter);
2013-09-20 22:23:13 +08:00
}
else
{
++listenersIter;
2013-09-20 22:23:13 +08:00
}
};
if (event->getType() == Event::Type::TOUCH)
{
onUpdateListeners(static_cast<EventListener::ListenerID>(EventListener::Type::TOUCH_ONE_BY_ONE));
onUpdateListeners(static_cast<EventListener::ListenerID>(EventListener::Type::TOUCH_ALL_AT_ONCE));
}
else
{
onUpdateListeners(getListenerID(event));
2013-09-20 22:23:13 +08:00
}
2013-09-20 22:23:13 +08:00
if (!_toAddedListeners.empty())
{
EventListenerVector* listeners = nullptr;
2013-09-20 22:23:13 +08:00
for (auto& listener : _toAddedListeners)
2013-09-20 22:23:13 +08:00
{
EventListener::ListenerID listenerID = listener->getListenerID();
auto itr = _listeners.find(listenerID);
2013-09-20 22:23:13 +08:00
if (itr == _listeners.end())
{
listeners = new EventListenerVector();
_listeners.insert(std::make_pair(listenerID, listeners));
2013-09-20 22:23:13 +08:00
}
else
{
listeners = itr->second;
2013-09-20 22:23:13 +08:00
}
listeners->push_back(listener);
2013-09-20 22:23:13 +08:00
if (listener->getFixedPriority() == 0)
{
setDirty(listenerID, DirtyFlag::SCENE_GRAPH_PRIORITY);
}
else
{
setDirty(listenerID, DirtyFlag::FIXED_PRITORY);
}
2013-09-20 22:23:13 +08:00
}
_toAddedListeners.clear();
}
}
void EventDispatcher::updateDirtyFlagForSceneGraph()
{
if (!_dirtyNodes.empty())
{
for (auto& node : _dirtyNodes)
{
auto iter = _nodeListenersMap.find(node);
if (iter != _nodeListenersMap.end())
{
for (auto& l : *iter->second)
{
setDirty(l->getListenerID(), DirtyFlag::SCENE_GRAPH_PRIORITY);
}
}
}
_dirtyNodes.clear();
}
}
void EventDispatcher::sortEventListeners(EventListener::ListenerID listenerID)
{
DirtyFlag dirtyFlag = DirtyFlag::NONE;
auto dirtyIter = _priorityDirtyFlagMap.find(listenerID);
if (dirtyIter != _priorityDirtyFlagMap.end())
{
dirtyFlag = dirtyIter->second;
}
if (dirtyFlag != DirtyFlag::NONE)
{
if ((int)dirtyFlag & (int)DirtyFlag::FIXED_PRITORY)
{
sortEventListenersOfFixedPriority(listenerID);
}
if ((int)dirtyFlag & (int)DirtyFlag::SCENE_GRAPH_PRIORITY)
{
sortEventListenersOfSceneGraphPriority(listenerID);
}
dirtyIter->second = DirtyFlag::NONE;
}
}
void EventDispatcher::sortEventListenersOfSceneGraphPriority(EventListener::ListenerID listenerID)
{
auto listeners = getListeners(listenerID);
if (listeners == nullptr)
return;
Node* rootNode = (Node*)Director::getInstance()->getRunningScene();
2013-10-23 17:26:14 +08:00
// Reset priority index
_nodePriorityIndex = 0;
2013-10-23 17:26:14 +08:00
_nodePriorityMap.clear();
visitTarget(rootNode);
// After sort: priority < 0, > 0
auto sceneGraphlisteners = listeners->getSceneGraphPriorityListeners();
std::sort(sceneGraphlisteners->begin(), sceneGraphlisteners->end(), [this](const EventListener* l1, const EventListener* l2) {
return _nodePriorityMap[l1->getSceneGraphPriority()] > _nodePriorityMap[l2->getSceneGraphPriority()];
});
#if DUMP_LISTENER_ITEM_PRIORITY_INFO
log("-----------------------------------");
for (auto& l : *sceneGraphlisteners)
{
2013-10-23 17:26:14 +08:00
log("listener priority: node ([%s]%p), priority (%d)", typeid(*l->_node).name(), l->_node, _nodePriorityMap[l->_node]);
}
#endif
}
void EventDispatcher::sortEventListenersOfFixedPriority(EventListener::ListenerID listenerID)
2013-09-20 22:23:13 +08:00
{
auto listeners = getListeners(listenerID);
2013-09-20 22:23:13 +08:00
if (listeners == nullptr)
2013-09-20 22:23:13 +08:00
return;
// After sort: priority < 0, > 0
auto fixedlisteners = listeners->getFixedPriorityListeners();
std::sort(fixedlisteners->begin(), fixedlisteners->end(), [](const EventListener* l1, const EventListener* l2) {
return l1->getFixedPriority() < l2->getFixedPriority();
2013-09-20 22:23:13 +08:00
});
// FIXME: Should use binary search
2013-11-20 10:39:30 +08:00
long index = 0;
for (auto& listener : *fixedlisteners)
{
if (listener->getFixedPriority() >= 0)
break;
++index;
}
listeners->setGt0Index(index);
2013-09-20 22:23:13 +08:00
#if DUMP_LISTENER_ITEM_PRIORITY_INFO
log("-----------------------------------");
for (auto& l : *fixedlisteners)
2013-09-20 22:23:13 +08:00
{
log("listener priority: node (%p), fixed (%d)", l->_node, l->_fixedPriority);
2013-09-20 22:23:13 +08:00
}
#endif
}
EventDispatcher::EventListenerVector* EventDispatcher::getListeners(EventListener::ListenerID listenerID)
2013-09-20 22:23:13 +08:00
{
auto iter = _listeners.find(listenerID);
2013-09-20 22:23:13 +08:00
if (iter != _listeners.end())
{
return iter->second;
}
return nullptr;
}
void EventDispatcher::removeEventListenersForListenerID(EventListener::ListenerID listenerID)
2013-09-20 22:23:13 +08:00
{
auto listenerItemIter = _listeners.find(listenerID);
2013-09-20 22:23:13 +08:00
if (listenerItemIter != _listeners.end())
{
auto listeners = listenerItemIter->second;
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
auto removeAllListenersInVector = [&](std::vector<EventListener*>* listenerVector){
if (listenerVector == nullptr)
return;
2013-09-20 22:23:13 +08:00
for (auto iter = listenerVector->begin(); iter != listenerVector->end();)
2013-09-20 22:23:13 +08:00
{
auto l = *iter;
l->setRegistered(false);
if (l->getSceneGraphPriority() != nullptr)
{
dissociateNodeAndEventListener(l->getSceneGraphPriority(), l);
}
if (_inDispatch == 0)
{
iter = listenerVector->erase(iter);
CC_SAFE_RELEASE(l);
}
else
{
++iter;
}
2013-09-20 22:23:13 +08:00
}
};
removeAllListenersInVector(sceneGraphPriorityListeners);
removeAllListenersInVector(fixedPriorityListeners);
2013-09-20 22:23:13 +08:00
if (!_inDispatch)
{
listeners->clear();
delete listeners;
2013-09-20 22:23:13 +08:00
_listeners.erase(listenerItemIter);
_priorityDirtyFlagMap.erase(listenerID);
2013-09-20 22:23:13 +08:00
}
}
for(auto iter = _toAddedListeners.begin(); iter != _toAddedListeners.end();)
{
if ((*iter)->getListenerID() == listenerID)
{
iter = _toAddedListeners.erase(iter);
}
else
{
++iter;
}
}
2013-09-20 22:23:13 +08:00
}
void EventDispatcher::removeEventListeners(EventListener::Type listenerType)
{
CCASSERT(listenerType != EventListener::Type::CUSTOM, "Not support custom event listener type, please use EventDispatcher::removeCustomEventListeners instead.");
removeEventListenersForListenerID(static_cast<EventListener::ListenerID>(listenerType));
}
void EventDispatcher::removeCustomEventListeners(const std::string& customEventName)
{
removeEventListenersForListenerID(std::hash<std::string>()(customEventName));
}
void EventDispatcher::removeAllEventListeners()
2013-09-20 22:23:13 +08:00
{
std::vector<int> types(_listeners.size());
for (auto iter = _listeners.begin(); iter != _listeners.end(); ++iter)
2013-09-20 22:23:13 +08:00
{
types.push_back(iter->first);
2013-09-20 22:23:13 +08:00
}
for (auto& type : types)
{
removeEventListenersForListenerID(type);
}
2013-09-20 22:23:13 +08:00
if (!_inDispatch)
{
_listeners.clear();
}
}
void EventDispatcher::setEnabled(bool isEnabled)
{
_isEnabled = isEnabled;
}
bool EventDispatcher::isEnabled() const
{
return _isEnabled;
}
void EventDispatcher::setDirtyForNode(Node* node)
{
2013-10-24 17:27:22 +08:00
// Mark the node dirty only when there was an eventlistener associates with it.
if (_nodeListenersMap.find(node) != _nodeListenersMap.end())
{
_dirtyNodes.insert(node);
}
}
void EventDispatcher::setDirty(EventListener::ListenerID listenerID, DirtyFlag flag)
{
auto iter = _priorityDirtyFlagMap.find(listenerID);
2013-09-20 22:23:13 +08:00
if (iter == _priorityDirtyFlagMap.end())
{
_priorityDirtyFlagMap.insert(std::make_pair(listenerID, flag));
2013-09-20 22:23:13 +08:00
}
else
{
int ret = (int)flag | (int)iter->second;
iter->second = (DirtyFlag) ret;
2013-09-20 22:23:13 +08:00
}
}
NS_CC_END