axmol/cocos/2d/CCEventDispatcher.cpp

1176 lines
34 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 "CCEventListenerAcceleration.h"
#include "CCEventListenerMouse.h"
#include "CCEventListenerKeyboard.h"
2013-12-21 16:56:28 +08:00
#include "CCEventListenerCustom.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 = EventListenerAcceleration::LISTENER_ID;
break;
case Event::Type::CUSTOM:
{
auto customEvent = static_cast<EventCustom*>(event);
ret = customEvent->getEventName();
}
break;
case Event::Type::KEYBOARD:
ret = EventListenerKeyboard::LISTENER_ID;
break;
2013-10-31 14:19:36 +08:00
case Event::Type::MOUSE:
ret = EventListenerMouse::LISTENER_ID;
2013-10-31 14:19:36 +08:00
break;
case Event::Type::TOUCH:
// Touch listener is very special, it contains two kinds of listeners, EventListenerTouchOneByOne and EventListenerTouchAllAtOnce.
// return UNKNOWN instead.
CCASSERT(false, "Don't call this method if the event is for touch.");
break;
default:
CCASSERT(false, "Invalid type!");
break;
}
return ret;
}
2013-12-27 15:06:16 +08:00
EventDispatcher::EventListenerVector::EventListenerVector() :
_fixedListeners(nullptr),
_sceneGraphListeners(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
{
2013-12-05 17:19:01 +08:00
int i = 0;
auto& children = node->getChildren();
2013-12-05 17:19:01 +08:00
auto childrenCount = children.size();
if(childrenCount > 0)
{
Node* child = nullptr;
// visit children zOrder < 0
for( ; i < childrenCount; i++ )
{
child = children.at(i);
if ( child && child->getZOrder() < 0 )
visitTarget(child);
else
break;
}
_nodePriorityMap.insert(std::make_pair(node, ++_nodePriorityIndex));
for( ; i < childrenCount; i++ )
{
child = children.at(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
}
2013-12-21 16:56:28 +08:00
listener->retain();
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);
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);
2013-09-20 22:23:13 +08:00
addEventListener(listener);
2013-09-20 22:23:13 +08:00
}
2013-12-21 16:56:28 +08:00
EventListenerCustom* EventDispatcher::addCustomEventListener(const std::string &eventName, std::function<void(EventCustom*)> callback)
{
EventListenerCustom *listener = EventListenerCustom::create(eventName, callback);
addEventListenerWithFixedPriority(listener, 1);
return 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
{
listener->release();
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-12-27 15:06:16 +08:00
ssize_t i = 0;
// priority < 0
if (fixedPriorityListeners)
{
2013-12-27 15:06:16 +08:00
bool isEmpty = fixedPriorityListeners->empty();
for (; !isEmpty && 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-12-27 15:06:16 +08:00
ssize_t size = fixedPriorityListeners->size();
for (; i < 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::dispatchCustomEvent(const std::string &eventName, void *optionalUserData)
{
EventCustom ev(eventName);
ev.setUserData(optionalUserData);
dispatchEvent(&ev);
}
2013-09-20 22:23:13 +08:00
void EventDispatcher::dispatchTouchEvent(EventTouch* event)
{
sortEventListeners(EventListenerTouchOneByOne::LISTENER_ID);
sortEventListeners(EventListenerTouchAllAtOnce::LISTENER_ID);
auto oneByOnelisteners = getListeners(EventListenerTouchOneByOne::LISTENER_ID);
auto allAtOncelisteners = getListeners(EventListenerTouchAllAtOnce::LISTENER_ID);
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](const 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(EventListenerTouchOneByOne::LISTENER_ID);
onUpdateListeners(EventListenerTouchAllAtOnce::LISTENER_ID);
}
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(const 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(const 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(const 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-12-05 17:19:01 +08:00
int 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(const 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(const 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)->release();
iter = _toAddedListeners.erase(iter);
}
else
{
++iter;
}
}
2013-09-20 22:23:13 +08:00
}
void EventDispatcher::removeEventListeners(EventListener::Type listenerType)
{
if (listenerType == EventListener::Type::TOUCH_ONE_BY_ONE)
{
removeEventListenersForListenerID(EventListenerTouchOneByOne::LISTENER_ID);
}
else if (listenerType == EventListener::Type::TOUCH_ALL_AT_ONCE)
{
removeEventListenersForListenerID(EventListenerTouchAllAtOnce::LISTENER_ID);
}
else if (listenerType == EventListener::Type::MOUSE)
{
removeEventListenersForListenerID(EventListenerMouse::LISTENER_ID);
}
else if (listenerType == EventListener::Type::ACCELERATION)
{
removeEventListenersForListenerID(EventListenerAcceleration::LISTENER_ID);
}
else if (listenerType == EventListener::Type::KEYBOARD)
{
removeEventListenersForListenerID(EventListenerKeyboard::LISTENER_ID);
}
else
{
CCASSERT(false, "Invalid listener type!");
}
}
void EventDispatcher::removeCustomEventListeners(const std::string& customEventName)
{
removeEventListenersForListenerID(customEventName);
}
void EventDispatcher::removeAllEventListeners()
2013-09-20 22:23:13 +08:00
{
2013-12-06 16:32:06 +08:00
std::vector<EventListener::ListenerID> types(_listeners.size());
for (const auto& e : _listeners)
2013-09-20 22:23:13 +08:00
{
types.push_back(e.first);
2013-09-20 22:23:13 +08:00
}
for (const 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(const 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