axmol/cocos/base/CCEventDispatcher.cpp

1624 lines
50 KiB
C++
Raw Normal View History

2013-09-20 22:23:13 +08:00
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
2013-09-20 22:23:13 +08:00
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.
****************************************************************************/
2014-04-30 08:37:36 +08:00
#include "base/CCEventDispatcher.h"
2014-08-29 15:39:52 +08:00
#include <algorithm>
2014-04-30 08:37:36 +08:00
#include "base/CCEventCustom.h"
#include "base/CCEventListenerTouch.h"
#include "base/CCEventListenerAcceleration.h"
#include "base/CCEventListenerMouse.h"
#include "base/CCEventListenerKeyboard.h"
#include "base/CCEventListenerCustom.h"
#include "base/CCEventListenerFocus.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "base/CCEventListenerController.h"
#endif
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "2d/CCScene.h"
2014-04-30 08:37:36 +08:00
#include "base/CCDirector.h"
#include "base/CCEventType.h"
#include "2d/CCCamera.h"
2013-09-20 22:23:13 +08:00
#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::FOCUS:
ret = EventListenerFocus::LISTENER_ID;
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;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
case Event::Type::GAME_CONTROLLER:
ret = EventListenerController::LISTENER_ID;
break;
#endif
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 CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS
Fix crashes in event dispatch other related safety checks/fixes 1: Add a fix to prevent events from being sent to scene nodes which are destroyed. Previously when a node was being destroyed it would try to unregister any event listeners it would have. This process would fail however in the case where event dispatching was already underway because we can't modify the listeners list while we are iterating through it. This is a pretty common occurrence since we often tear down & switch scenes in response to button clicks etc. Fix the problem by nulling out the slot for the listener we are removing in this case, so that it's node no longer receives any events after it is destroyed. The event dispatching code will cleanup this unused/nulled slot once the event processing loop has terminated. 2: When removing an event listener in cocos2d::EventDispatcher, ensure immediately it gets removed from the node priority map and dirty nodes set in order to ensure we don't inadvertently access any dangling pointers to nodes. 3: Add debug checks to ensure nodes have no associated event listeners after they are destroyed. This check will catch the problem fixed by (1) if it is still present. 4: Add some extra debug sanity checks in the event dispatcher to ensure nodes are not added to the lists twice and that they are being properly removed from the lists under certain conditions. 5: In cocos2d::Node's destructor NULL out members after releasing them in case they somehow happen to be accessed during the destructor. Move the release of the event dispatcher to the very end, since we need to check against it in debug.
2014-03-27 04:12:44 +08:00
CCASSERT(_sceneGraphListeners == nullptr ||
std::count(_sceneGraphListeners->begin(), _sceneGraphListeners->end(), listener) == 0,
"Listener should not be added twice!");
CCASSERT(_fixedListeners == nullptr ||
std::count(_fixedListeners->begin(), _fixedListeners->end(), listener) == 0,
"Listener should not be added twice!");
#endif
Fix crashes in event dispatch other related safety checks/fixes 1: Add a fix to prevent events from being sent to scene nodes which are destroyed. Previously when a node was being destroyed it would try to unregister any event listeners it would have. This process would fail however in the case where event dispatching was already underway because we can't modify the listeners list while we are iterating through it. This is a pretty common occurrence since we often tear down & switch scenes in response to button clicks etc. Fix the problem by nulling out the slot for the listener we are removing in this case, so that it's node no longer receives any events after it is destroyed. The event dispatching code will cleanup this unused/nulled slot once the event processing loop has terminated. 2: When removing an event listener in cocos2d::EventDispatcher, ensure immediately it gets removed from the node priority map and dirty nodes set in order to ensure we don't inadvertently access any dangling pointers to nodes. 3: Add debug checks to ensure nodes have no associated event listeners after they are destroyed. This check will catch the problem fixed by (1) if it is still present. 4: Add some extra debug sanity checks in the event dispatcher to ensure nodes are not added to the lists twice and that they are being properly removed from the lists under certain conditions. 5: In cocos2d::Node's destructor NULL out members after releasing them in case they somehow happen to be accessed during the destructor. Move the release of the event dispatcher to the very end, since we need to check against it in debug.
2014-03-27 04:12:44 +08:00
if (listener->getFixedPriority() == 0)
{
if (_sceneGraphListeners == nullptr)
{
_sceneGraphListeners = new (std::nothrow) 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(false)
, _nodePriorityIndex(0)
2013-09-20 22:23:13 +08:00
{
_toAddedListeners.reserve(50);
_toRemovedListeners.reserve(50);
// fixed #4129: Mark the following listener IDs for internal use.
// Therefore, internal listeners would not be cleaned when removeAllEventListeners is invoked.
_internalCustomListenerIDs.insert(EVENT_COME_TO_FOREGROUND);
_internalCustomListenerIDs.insert(EVENT_COME_TO_BACKGROUND);
_internalCustomListenerIDs.insert(EVENT_RENDERER_RECREATED);
2013-09-20 22:23:13 +08:00
}
EventDispatcher::~EventDispatcher()
{
// Clear internal custom listener IDs from set,
// so removeAllEventListeners would clean internal custom listeners.
_internalCustomListenerIDs.clear();
removeAllEventListeners();
2013-09-20 22:23:13 +08:00
}
void EventDispatcher::visitTarget(Node* node, bool isRootNode)
2015-07-01 14:39:35 +08:00
{
node->sortAllChildren();
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->getLocalZOrder() < 0 )
visitTarget(child, false);
else
break;
}
if (_nodeListenersMap.find(node) != _nodeListenersMap.end())
{
_globalZOrderNodeMap[node->getGlobalZOrder()].push_back(node);
}
for( ; i < childrenCount; i++ )
{
child = children.at(i);
if (child)
visitTarget(child, false);
}
}
else
{
if (_nodeListenersMap.find(node) != _nodeListenersMap.end())
{
_globalZOrderNodeMap[node->getGlobalZOrder()].push_back(node);
}
}
if (isRootNode)
{
2014-01-22 08:36:19 +08:00
std::vector<float> globalZOrders;
globalZOrders.reserve(_globalZOrderNodeMap.size());
for (const auto& e : _globalZOrderNodeMap)
{
globalZOrders.push_back(e.first);
}
2014-01-22 08:36:19 +08:00
std::sort(globalZOrders.begin(), globalZOrders.end(), [](const float a, const float b){
return a < b;
});
for (const auto& globalZ : globalZOrders)
{
for (const auto& n : _globalZOrderNodeMap[globalZ])
{
_nodePriorityMap[n] = ++_nodePriorityIndex;
}
}
_globalZOrderNodeMap.clear();
}
}
void EventDispatcher::pauseEventListenersForTarget(Node* target, bool recursive/* = false */)
{
auto listenerIter = _nodeListenersMap.find(target);
if (listenerIter != _nodeListenersMap.end())
{
auto listeners = listenerIter->second;
for (auto& l : *listeners)
{
l->setPaused(true);
}
}
2014-05-14 17:58:55 +08:00
2014-05-15 09:19:59 +08:00
for (auto& listener : _toAddedListeners)
2014-05-14 17:58:55 +08:00
{
2014-05-15 09:19:59 +08:00
if (listener->getAssociatedNode() == target)
2014-05-14 17:58:55 +08:00
{
2014-05-15 09:19:59 +08:00
listener->setPaused(true);
2014-05-14 17:58:55 +08:00
}
}
if (recursive)
{
const auto& children = target->getChildren();
for (const auto& child : children)
{
pauseEventListenersForTarget(child, true);
}
}
}
void EventDispatcher::resumeEventListenersForTarget(Node* target, bool recursive/* = false */)
{
auto listenerIter = _nodeListenersMap.find(target);
if (listenerIter != _nodeListenersMap.end())
{
auto listeners = listenerIter->second;
for (auto& l : *listeners)
{
l->setPaused(false);
}
}
2014-05-14 17:58:55 +08:00
2014-05-15 09:19:59 +08:00
for (auto& listener : _toAddedListeners)
2014-05-14 17:58:55 +08:00
{
2014-05-15 09:19:59 +08:00
if (listener->getAssociatedNode() == target)
2014-05-14 17:58:55 +08:00
{
2014-05-15 09:19:59 +08:00
listener->setPaused(false);
2014-05-14 17:58:55 +08:00
}
}
setDirtyForNode(target);
if (recursive)
{
const auto& children = target->getChildren();
for (const auto& child : children)
{
resumeEventListenersForTarget(child, true);
}
}
}
void EventDispatcher::removeEventListenersForTarget(Node* target, bool recursive/* = false */)
{
Fix crashes in event dispatch other related safety checks/fixes 1: Add a fix to prevent events from being sent to scene nodes which are destroyed. Previously when a node was being destroyed it would try to unregister any event listeners it would have. This process would fail however in the case where event dispatching was already underway because we can't modify the listeners list while we are iterating through it. This is a pretty common occurrence since we often tear down & switch scenes in response to button clicks etc. Fix the problem by nulling out the slot for the listener we are removing in this case, so that it's node no longer receives any events after it is destroyed. The event dispatching code will cleanup this unused/nulled slot once the event processing loop has terminated. 2: When removing an event listener in cocos2d::EventDispatcher, ensure immediately it gets removed from the node priority map and dirty nodes set in order to ensure we don't inadvertently access any dangling pointers to nodes. 3: Add debug checks to ensure nodes have no associated event listeners after they are destroyed. This check will catch the problem fixed by (1) if it is still present. 4: Add some extra debug sanity checks in the event dispatcher to ensure nodes are not added to the lists twice and that they are being properly removed from the lists under certain conditions. 5: In cocos2d::Node's destructor NULL out members after releasing them in case they somehow happen to be accessed during the destructor. Move the release of the event dispatcher to the very end, since we need to check against it in debug.
2014-03-27 04:12:44 +08:00
// Ensure the node is removed from these immediately also.
// Don't want any dangling pointers or the possibility of dealing with deleted objects..
_nodePriorityMap.erase(target);
_dirtyNodes.erase(target);
auto listenerIter = _nodeListenersMap.find(target);
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);
}
}
// Bug fix: ensure there are no references to the node in the list of listeners to be added.
// If we find any listeners associated with the destroyed node in this list then remove them.
// This is to catch the scenario where the node gets destroyed before it's listener
// is added into the event dispatcher fully. This could happen if a node registers a listener
// and gets destroyed while we are dispatching an event (touch etc.)
for (auto iter = _toAddedListeners.begin(); iter != _toAddedListeners.end(); )
{
EventListener * listener = *iter;
if (listener->getAssociatedNode() == target)
{
listener->setAssociatedNode(nullptr); // Ensure no dangling ptr to the target node.
listener->setRegistered(false);
2015-12-17 21:55:47 +08:00
releaseListener(listener);
iter = _toAddedListeners.erase(iter);
}
else
{
++iter;
}
}
if (recursive)
{
const auto& children = target->getChildren();
for (const auto& child : children)
{
removeEventListenersForTarget(child, true);
}
}
}
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::nothrow) std::vector<EventListener*>();
_nodeListenersMap.insert(std::make_pair(node, listeners));
}
listeners->push_back(listener);
}
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)
{
forceAddEventListener(listener);
}
else
{
_toAddedListeners.push_back(listener);
}
2015-12-17 21:55:47 +08:00
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
if (sEngine)
{
2015-12-17 21:55:47 +08:00
sEngine->retainScriptObject(this, listener);
}
2015-12-17 21:55:47 +08:00
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
listener->retain();
}
void EventDispatcher::forceAddEventListener(EventListener* listener)
{
EventListenerVector* listeners = nullptr;
EventListener::ListenerID listenerID = listener->getListenerID();
auto itr = _listenerMap.find(listenerID);
if (itr == _listenerMap.end())
{
listeners = new (std::nothrow) EventListenerVector();
_listenerMap.insert(std::make_pair(listenerID, listeners));
}
else
{
listeners = itr->second;
}
listeners->push_back(listener);
if (listener->getFixedPriority() == 0)
{
setDirty(listenerID, DirtyFlag::SCENE_GRAPH_PRIORITY);
auto node = listener->getAssociatedNode();
CCASSERT(node != nullptr, "Invalid scene graph priority!");
associateNodeAndEventListener(node, listener);
if (node->isRunning())
{
resumeEventListenersForTarget(node);
}
2013-09-20 22:23:13 +08:00
}
else
{
setDirty(listenerID, DirtyFlag::FIXED_PRIORITY);
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->setAssociatedNode(node);
listener->setFixedPriority(0);
listener->setRegistered(true);
addEventListener(listener);
2013-09-20 22:23:13 +08:00
}
#if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS && COCOS2D_DEBUG > 0
Fix crashes in event dispatch other related safety checks/fixes 1: Add a fix to prevent events from being sent to scene nodes which are destroyed. Previously when a node was being destroyed it would try to unregister any event listeners it would have. This process would fail however in the case where event dispatching was already underway because we can't modify the listeners list while we are iterating through it. This is a pretty common occurrence since we often tear down & switch scenes in response to button clicks etc. Fix the problem by nulling out the slot for the listener we are removing in this case, so that it's node no longer receives any events after it is destroyed. The event dispatching code will cleanup this unused/nulled slot once the event processing loop has terminated. 2: When removing an event listener in cocos2d::EventDispatcher, ensure immediately it gets removed from the node priority map and dirty nodes set in order to ensure we don't inadvertently access any dangling pointers to nodes. 3: Add debug checks to ensure nodes have no associated event listeners after they are destroyed. This check will catch the problem fixed by (1) if it is still present. 4: Add some extra debug sanity checks in the event dispatcher to ensure nodes are not added to the lists twice and that they are being properly removed from the lists under certain conditions. 5: In cocos2d::Node's destructor NULL out members after releasing them in case they somehow happen to be accessed during the destructor. Move the release of the event dispatcher to the very end, since we need to check against it in debug.
2014-03-27 04:12:44 +08:00
void EventDispatcher::debugCheckNodeHasNoEventListenersOnDestruction(Node* node)
{
// Check the listeners map
for (const auto & keyValuePair : _listenerMap)
{
const EventListenerVector * eventListenerVector = keyValuePair.second;
if (eventListenerVector)
{
if (eventListenerVector->getSceneGraphPriorityListeners())
{
for (EventListener * listener : *eventListenerVector->getSceneGraphPriorityListeners())
{
CCASSERT(!listener ||
listener->getAssociatedNode() != node,
Fix crashes in event dispatch other related safety checks/fixes 1: Add a fix to prevent events from being sent to scene nodes which are destroyed. Previously when a node was being destroyed it would try to unregister any event listeners it would have. This process would fail however in the case where event dispatching was already underway because we can't modify the listeners list while we are iterating through it. This is a pretty common occurrence since we often tear down & switch scenes in response to button clicks etc. Fix the problem by nulling out the slot for the listener we are removing in this case, so that it's node no longer receives any events after it is destroyed. The event dispatching code will cleanup this unused/nulled slot once the event processing loop has terminated. 2: When removing an event listener in cocos2d::EventDispatcher, ensure immediately it gets removed from the node priority map and dirty nodes set in order to ensure we don't inadvertently access any dangling pointers to nodes. 3: Add debug checks to ensure nodes have no associated event listeners after they are destroyed. This check will catch the problem fixed by (1) if it is still present. 4: Add some extra debug sanity checks in the event dispatcher to ensure nodes are not added to the lists twice and that they are being properly removed from the lists under certain conditions. 5: In cocos2d::Node's destructor NULL out members after releasing them in case they somehow happen to be accessed during the destructor. Move the release of the event dispatcher to the very end, since we need to check against it in debug.
2014-03-27 04:12:44 +08:00
"Node should have no event listeners registered for it upon destruction!");
}
}
}
}
// Check the node listeners map
for (const auto & keyValuePair : _nodeListenersMap)
{
CCASSERT(keyValuePair.first != node, "Node should have no event listeners registered for it upon destruction!");
if (keyValuePair.second)
{
for (EventListener * listener : *keyValuePair.second)
{
CCASSERT(listener->getAssociatedNode() != node,
Fix crashes in event dispatch other related safety checks/fixes 1: Add a fix to prevent events from being sent to scene nodes which are destroyed. Previously when a node was being destroyed it would try to unregister any event listeners it would have. This process would fail however in the case where event dispatching was already underway because we can't modify the listeners list while we are iterating through it. This is a pretty common occurrence since we often tear down & switch scenes in response to button clicks etc. Fix the problem by nulling out the slot for the listener we are removing in this case, so that it's node no longer receives any events after it is destroyed. The event dispatching code will cleanup this unused/nulled slot once the event processing loop has terminated. 2: When removing an event listener in cocos2d::EventDispatcher, ensure immediately it gets removed from the node priority map and dirty nodes set in order to ensure we don't inadvertently access any dangling pointers to nodes. 3: Add debug checks to ensure nodes have no associated event listeners after they are destroyed. This check will catch the problem fixed by (1) if it is still present. 4: Add some extra debug sanity checks in the event dispatcher to ensure nodes are not added to the lists twice and that they are being properly removed from the lists under certain conditions. 5: In cocos2d::Node's destructor NULL out members after releasing them in case they somehow happen to be accessed during the destructor. Move the release of the event dispatcher to the very end, since we need to check against it in debug.
2014-03-27 04:12:44 +08:00
"Node should have no event listeners registered for it upon destruction!");
}
}
}
// Check the node priority map
for (const auto & keyValuePair : _nodePriorityMap)
{
CCASSERT(keyValuePair.first != node,
"Node should have no event listeners registered for it upon destruction!");
}
// Check the to be added list
for (EventListener * listener : _toAddedListeners)
{
CCASSERT(listener->getAssociatedNode() != node,
Fix crashes in event dispatch other related safety checks/fixes 1: Add a fix to prevent events from being sent to scene nodes which are destroyed. Previously when a node was being destroyed it would try to unregister any event listeners it would have. This process would fail however in the case where event dispatching was already underway because we can't modify the listeners list while we are iterating through it. This is a pretty common occurrence since we often tear down & switch scenes in response to button clicks etc. Fix the problem by nulling out the slot for the listener we are removing in this case, so that it's node no longer receives any events after it is destroyed. The event dispatching code will cleanup this unused/nulled slot once the event processing loop has terminated. 2: When removing an event listener in cocos2d::EventDispatcher, ensure immediately it gets removed from the node priority map and dirty nodes set in order to ensure we don't inadvertently access any dangling pointers to nodes. 3: Add debug checks to ensure nodes have no associated event listeners after they are destroyed. This check will catch the problem fixed by (1) if it is still present. 4: Add some extra debug sanity checks in the event dispatcher to ensure nodes are not added to the lists twice and that they are being properly removed from the lists under certain conditions. 5: In cocos2d::Node's destructor NULL out members after releasing them in case they somehow happen to be accessed during the destructor. Move the release of the event dispatcher to the very end, since we need to check against it in debug.
2014-03-27 04:12:44 +08:00
"Node should have no event listeners registered for it upon destruction!");
}
// Check the dirty nodes set
for (Node * dirtyNode : _dirtyNodes)
{
CCASSERT(dirtyNode != node,
"Node should have no event listeners registered for it upon destruction!");
}
}
#endif // #if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS && COCOS2D_DEBUG > 0
Fix crashes in event dispatch other related safety checks/fixes 1: Add a fix to prevent events from being sent to scene nodes which are destroyed. Previously when a node was being destroyed it would try to unregister any event listeners it would have. This process would fail however in the case where event dispatching was already underway because we can't modify the listeners list while we are iterating through it. This is a pretty common occurrence since we often tear down & switch scenes in response to button clicks etc. Fix the problem by nulling out the slot for the listener we are removing in this case, so that it's node no longer receives any events after it is destroyed. The event dispatching code will cleanup this unused/nulled slot once the event processing loop has terminated. 2: When removing an event listener in cocos2d::EventDispatcher, ensure immediately it gets removed from the node priority map and dirty nodes set in order to ensure we don't inadvertently access any dangling pointers to nodes. 3: Add debug checks to ensure nodes have no associated event listeners after they are destroyed. This check will catch the problem fixed by (1) if it is still present. 4: Add some extra debug sanity checks in the event dispatcher to ensure nodes are not added to the lists twice and that they are being properly removed from the lists under certain conditions. 5: In cocos2d::Node's destructor NULL out members after releasing them in case they somehow happen to be accessed during the destructor. Move the release of the event dispatcher to the very end, since we need to check against it in debug.
2014-03-27 04:12:44 +08:00
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->setAssociatedNode(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
}
EventListenerCustom* EventDispatcher::addCustomEventListener(const std::string &eventName, const std::function<void(EventCustom*)>& callback)
2013-12-21 16:56:28 +08:00
{
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;
// just return if listener is in _toRemovedListeners to avoid remove listeners more than once
if (std::find(_toRemovedListeners.begin(), _toRemovedListeners.end(), listener) != _toRemovedListeners.end())
return;
2013-09-20 22:23:13 +08:00
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->getAssociatedNode() != nullptr)
2013-09-20 22:23:13 +08:00
{
dissociateNodeAndEventListener(l->getAssociatedNode(), l);
l->setAssociatedNode(nullptr); // nullptr out the node pointer so we don't have any dangling pointers to destroyed nodes.
2013-09-20 22:23:13 +08:00
}
if (_inDispatch == 0)
{
iter = listeners->erase(iter);
2015-12-17 21:55:47 +08:00
releaseListener(l);
2013-09-20 22:23:13 +08:00
}
else
{
_toRemovedListeners.push_back(l);
}
2013-09-20 22:23:13 +08:00
isFound = true;
break;
2013-09-20 22:23:13 +08:00
}
}
};
for (auto iter = _listenerMap.begin(); iter != _listenerMap.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)
{
// fixed #4160: Dirty flag need to be updated after listeners were removed.
setDirty(listener->getListenerID(), DirtyFlag::SCENE_GRAPH_PRIORITY);
}
else
{
removeListenerInVector(fixedPriorityListeners);
if (isFound)
{
setDirty(listener->getListenerID(), DirtyFlag::FIXED_PRIORITY);
}
}
Fix crashes in event dispatch other related safety checks/fixes 1: Add a fix to prevent events from being sent to scene nodes which are destroyed. Previously when a node was being destroyed it would try to unregister any event listeners it would have. This process would fail however in the case where event dispatching was already underway because we can't modify the listeners list while we are iterating through it. This is a pretty common occurrence since we often tear down & switch scenes in response to button clicks etc. Fix the problem by nulling out the slot for the listener we are removing in this case, so that it's node no longer receives any events after it is destroyed. The event dispatching code will cleanup this unused/nulled slot once the event processing loop has terminated. 2: When removing an event listener in cocos2d::EventDispatcher, ensure immediately it gets removed from the node priority map and dirty nodes set in order to ensure we don't inadvertently access any dangling pointers to nodes. 3: Add debug checks to ensure nodes have no associated event listeners after they are destroyed. This check will catch the problem fixed by (1) if it is still present. 4: Add some extra debug sanity checks in the event dispatcher to ensure nodes are not added to the lists twice and that they are being properly removed from the lists under certain conditions. 5: In cocos2d::Node's destructor NULL out members after releasing them in case they somehow happen to be accessed during the destructor. Move the release of the event dispatcher to the very end, since we need to check against it in debug.
2014-03-27 04:12:44 +08:00
#if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS
Fix crashes in event dispatch other related safety checks/fixes 1: Add a fix to prevent events from being sent to scene nodes which are destroyed. Previously when a node was being destroyed it would try to unregister any event listeners it would have. This process would fail however in the case where event dispatching was already underway because we can't modify the listeners list while we are iterating through it. This is a pretty common occurrence since we often tear down & switch scenes in response to button clicks etc. Fix the problem by nulling out the slot for the listener we are removing in this case, so that it's node no longer receives any events after it is destroyed. The event dispatching code will cleanup this unused/nulled slot once the event processing loop has terminated. 2: When removing an event listener in cocos2d::EventDispatcher, ensure immediately it gets removed from the node priority map and dirty nodes set in order to ensure we don't inadvertently access any dangling pointers to nodes. 3: Add debug checks to ensure nodes have no associated event listeners after they are destroyed. This check will catch the problem fixed by (1) if it is still present. 4: Add some extra debug sanity checks in the event dispatcher to ensure nodes are not added to the lists twice and that they are being properly removed from the lists under certain conditions. 5: In cocos2d::Node's destructor NULL out members after releasing them in case they somehow happen to be accessed during the destructor. Move the release of the event dispatcher to the very end, since we need to check against it in debug.
2014-03-27 04:12:44 +08:00
CCASSERT(_inDispatch != 0 ||
!sceneGraphPriorityListeners ||
std::count(sceneGraphPriorityListeners->begin(), sceneGraphPriorityListeners->end(), listener) == 0,
"Listener should be in no lists after this is done if we're not currently in dispatch mode.");
CCASSERT(_inDispatch != 0 ||
!fixedPriorityListeners ||
std::count(fixedPriorityListeners->begin(), fixedPriorityListeners->end(), listener) == 0,
"Listener should be in no lists after this is done if we're not currently in dispatch mode.");
#endif
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 = _listenerMap.erase(iter);
2013-09-20 22:23:13 +08:00
CC_SAFE_DELETE(list);
}
else
{
++iter;
}
if (isFound)
break;
}
if (isFound)
{
2015-12-17 21:55:47 +08:00
releaseListener(listener);
}
else
{
for(auto iter = _toAddedListeners.begin(); iter != _toAddedListeners.end(); ++iter)
{
if (*iter == listener)
2013-11-02 15:03:54 +08:00
{
listener->setRegistered(false);
2015-12-17 21:55:47 +08:00
releaseListener(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 = _listenerMap.begin(); iter != _listenerMap.end(); ++iter)
2013-09-20 22:23:13 +08:00
{
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->getAssociatedNode() == 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_PRIORITY);
2013-09-20 22:23:13 +08:00
}
return;
}
}
}
}
void EventDispatcher::dispatchEventToListeners(EventListenerVector* listeners, const 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)
{
2014-03-24 10:12:40 +08:00
CCASSERT(listeners->getGt0Index() <= static_cast<ssize_t>(fixedPriorityListeners->size()), "Out of range exception!");
if (!fixedPriorityListeners->empty())
{
for (; i < listeners->getGt0Index(); ++i)
{
auto l = fixedPriorityListeners->at(i);
if (l->isEnabled() && !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)
{
if (l->isEnabled() && !l->isPaused() && l->isRegistered() && onEvent(l))
{
shouldStopPropagation = true;
break;
}
}
}
}
if (fixedPriorityListeners)
{
if (!shouldStopPropagation)
{
// priority > 0
ssize_t size = fixedPriorityListeners->size();
for (; i < size; ++i)
{
auto l = fixedPriorityListeners->at(i);
if (l->isEnabled() && !l->isPaused() && l->isRegistered() && onEvent(l))
{
shouldStopPropagation = true;
break;
}
}
}
}
}
void EventDispatcher::dispatchTouchEventToListeners(EventListenerVector* listeners, const std::function<bool(EventListener*)>& onEvent)
{
bool shouldStopPropagation = false;
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
ssize_t i = 0;
// priority < 0
if (fixedPriorityListeners)
{
CCASSERT(listeners->getGt0Index() <= static_cast<ssize_t>(fixedPriorityListeners->size()), "Out of range exception!");
if (!fixedPriorityListeners->empty())
{
for (; i < listeners->getGt0Index(); ++i)
{
auto l = fixedPriorityListeners->at(i);
if (l->isEnabled() && !l->isPaused() && l->isRegistered() && onEvent(l))
{
shouldStopPropagation = true;
break;
}
}
}
}
auto scene = Director::getInstance()->getRunningScene();
if (scene && sceneGraphPriorityListeners)
{
if (!shouldStopPropagation)
{
// priority == 0, scene graph priority
// first, get all enabled, unPaused and registered listeners
std::vector<EventListener*> sceneListeners;
for (auto& l : *sceneGraphPriorityListeners)
{
if (l->isEnabled() && !l->isPaused() && l->isRegistered())
{
sceneListeners.push_back(l);
}
}
// second, for all camera call all listeners
// get a copy of cameras, prevent it's been modified in listener callback
// if camera's depth is greater, process it earlier
Squashed commit of the following: VR support for cocos2d-x commit 087aff0aec24b81418fa2678ce0cae2d4c1e2e01 Merge: b32d329 fc44d0d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jun 15 11:26:33 2016 -0700 Merge branch 'v3' into vr commit b32d329f9331a4f9bbbbf946b88b31db7559934d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jun 15 11:25:27 2016 -0700 fix: dont' include oculus files commit 816928c6a8782984830aa92de5bed038c1306cdc Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jun 15 10:57:26 2016 -0700 fix: missing guard in header commit 2abd4eb5a1fc961c2cbae9b00809b5e6409740db Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jun 15 10:43:49 2016 -0700 removed VR engines... should be part of package manager commit 583179755d1c66c02e898297230d0f882e629b98 Merge: 12f4f71 b6d6bb0 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jun 15 10:42:37 2016 -0700 Merge branch 'vr' of github.com:ricardoquesada/cocos2d-x into vr commit 12f4f71aca4fa15231976a7727faf40648d62313 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jun 15 10:42:12 2016 -0700 fix: new VR API... easier to enable/disable different VR renderers commit b6d6bb087f54cbd272fbfb9e1cf1cd6bba776ffa Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Jun 14 20:48:04 2016 -0700 fix: compiles and runs on win10 commit e73aa8902118377abbd2192c757104c4531d2a9e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Jun 14 20:27:06 2016 -0700 fix: vr works again commit 5615e276507edf8602f043f3130204a89dbaba69 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Jun 14 18:55:18 2016 -0700 fix: compiles on windows commit 494061ee4da8fbc5616f83efb64b2c0c3932778e Merge: 284910b fd3b6d4 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Jun 14 17:13:47 2016 -0700 Merge branch 'v3' into vr commit 284910b204ee0dd76d949ea8f2b2f6b1b72e533f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Jun 14 17:12:25 2016 -0700 android vr: better performance commit aa8328e8029143dd7c6c724f38915f0fb3b64abe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Jun 13 16:10:14 2016 -0700 fix: low pass filter in accel and magnet commit f6d9b622abff5abb95ad60fa7139f3ce1ec9caab Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Jun 13 15:43:00 2016 -0700 fix: removes debugging info commit 2004f0ce5605dad70ff8656a058073181346f083 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Jun 13 14:54:32 2016 -0700 fix: kind of works on android! yeah! commit d6dcb6a3410fda053f0d6fbc00af817a13a86d3b Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jun 10 09:44:53 2016 -0700 logging commit 7e5d6ad52d39642c111e7b690173338af4e94092 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Jun 7 22:53:07 2016 -0300 android head tracking compiles, doesn't work yet... how to debug java code? commit cbf5f6482aa0ae002a7dc40045ef3ffdbe192e26 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Jun 7 19:45:41 2016 -0300 reading sensor from android not compiling yet commit 6ee0a3c2c43f8e8a603bfce9fddb0cce5ce79415 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Jun 7 11:45:06 2016 -0300 compiles on android... finally commit fb728da756ca7ee94b316c113e2239a77d9f4b53 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Jun 6 18:15:12 2016 -0300 fix: compile vr android files only on android commit 90db6daef382d142bb60207d2b16936dd66ee245 Merge: 2e56f03 cc936af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Jun 6 12:15:56 2016 -0300 Merge pull request #11 from songchengjiang/riq_vr daydream VR platform supporting commit cc936afac44518a00b90ec3133aaac5d00f0b91f Author: songchengjiang <moses_jc@sina.com> Date: Mon Jun 6 10:38:27 2016 +0800 remove VR 3rdparty dependence commit 5f7a1a9c80f84cc915763ad6f52ffc524dd59309 Author: songchengjiang <moses_jc@sina.com> Date: Mon Jun 6 10:34:37 2016 +0800 remove VR 3rdparty dependence commit 3f72ecde2a259357a137bec1e99e731a2e78df84 Author: songchengjiang <moses_jc@sina.com> Date: Mon Jun 6 10:28:43 2016 +0800 remove VR 3rdparty dependence commit 2e56f032d1daced1b66b984acacc6f5007f5ecc3 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jun 3 10:45:44 2016 -0300 fix: started android support commit 1910c9c488ee4315110081b60438aa1b7c1011f2 Author: songchengjiang <moses_jc@sina.com> Date: Thu Jun 2 15:58:34 2016 +0800 add CCVRGvrRenderer and CCVRGvrHeadTracker framework commit 96200eedea9234287153d71b4f198077cc49389a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue May 31 13:07:44 2016 -0300 fix: little fixes commit a2eb8114b2969beca83762ba829c8cb809b1615a Merge: 20a74e0 5fddebc Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue May 31 12:12:59 2016 -0300 Merge pull request #10 from songchengjiang/riq_vr bugs fixed commit 5fddebcae997db9e1a4108413d73b0d057a61e83 Author: songchengjiang <moses_jc@sina.com> Date: Fri May 27 15:57:30 2016 +0800 parameter corrected commit 925aad012ae513ebeeec682b003123a3c543759f Merge: 9b5e02f 20a74e0 Author: songchengjiang <songcheng.jiang@chukong-inc.com> Date: Fri May 27 15:46:13 2016 +0800 Merge branch 'vr' of https://github.com/ricardoquesada/cocos2d-x into riq_vr Conflicts: cocos/2d/CCScene.cpp commit 9b5e02fa7c9fcdd15cfea99769560ed30b976e9e Author: songchengjiang <songcheng.jiang@chukong-inc.com> Date: Fri May 27 14:36:31 2016 +0800 fixed ProjectionMatrix error on oculus platform fixed ScrollView error on oculus platform commit 3c63ead1943d1cf8aa3c0fd722a4b3834db323b1 Author: songchengjiang <moses_jc@sina.com> Date: Fri May 27 10:51:46 2016 +0800 bugs fixed mobile VR platforms: fixed ProjectionMatrix error fixed culling error of ScrollView commit 20a74e064708e6bf9d15cc5551d1f86af9d24010 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 20 09:22:45 2016 -0300 fix: setAdditionalTransform support reference commit 8341df82a02683b8e4ae02654e90617a2e5ced6d Merge: 83751de 469d38d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 18 07:38:49 2016 -0700 Merge pull request #8 from songchengjiang/riq_vr Finished VR integration of SKDs based on VRProtocol commit 469d38d778cd5f947098a08d8fd14ca6f32b0502 Author: songchengjiang <moses_jc@sina.com> Date: Wed May 18 15:32:32 2016 +0800 fix bug of deepoon's headtracking commit 83751deac133910c24321ddaddff93bf736de884 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue May 17 09:41:17 2016 -0700 fix: Scene inverts the eye matrix commit 41ae41969a71fd5b07396faac78e777c4afbe5c4 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue May 17 06:54:34 2016 -0700 fix: inversed matrix for camera commit 049dee721356a47b19f5e51a0face6a5a1647095 Author: songchengjiang <songcheng.jiang@chukong-inc.com> Date: Tue May 17 15:51:57 2016 +0800 add headtracker of oculus commit 3f6478352a4baebc684105156389655edbac7e6f Author: songchengjiang <moses_jc@sina.com> Date: Tue May 17 14:22:56 2016 +0800 add headtracker of SKDs gearvr/deepoon/cardboard commit 59df985b72adf0dbb5a4c66c80ae7299b16ae909 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon May 16 22:12:23 2016 -0700 fix: head tracker works commit 306c59da0175708c96b5757f5f27afd6a6592fe3 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon May 16 17:55:43 2016 -0700 fix: adds missing methods to `VRIHeadTracker` commit 91100b8a86d78801fbcd08e99427d6a6363ad69b Author: songchengjiang <songcheng.jiang@chukong-inc.com> Date: Mon May 16 14:17:21 2016 +0800 Rename Oculus's files commit 01ef6215de936a8d8bb15f9ec195cb81b2b4b18f Author: songchengjiang <moses_jc@sina.com> Date: Mon May 16 11:41:56 2016 +0800 Rename files based on riq's changes commit 734efbb045064d24df095c5b8fc604ef2aab1dea Merge: 91449c9 769a883 Author: songchengjiang <songcheng.jiang@chukong-inc.com> Date: Mon May 16 09:57:57 2016 +0800 Merge branch 'riq_vr' of https://github.com/songchengjiang/cocos2d-x into riq_vr Conflicts: build/cocos2d_libs.xcodeproj/project.pbxproj cocos/vr/CCVRGeneric.cpp cocos/vr/CCVRGenericHeadTracker.h commit 91449c9d23e357549cb1aeae6d1454274def1aab Merge: d3e4550 a33faaf Author: songchengjiang <songcheng.jiang@chukong-inc.com> Date: Mon May 16 09:54:44 2016 +0800 Merge branch 'vr' of https://github.com/ricardoquesada/cocos2d-x into riq_vr Conflicts: build/cocos2d_libs.xcodeproj/project.pbxproj cocos/platform/CCGLView.cpp commit a33faafa1a2b6cffdc32d87f3c1c321f46f94789 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Sat May 14 00:57:54 2016 -0700 fix: adds head tracker commit ea348cf72d3e54dc3864571bff1405909dbb65c9 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 13 18:40:32 2016 -0700 fix: removes red background commit 113c7debe9a35fc142183a9012cac40063e17efe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 13 18:39:31 2016 -0700 fix: centers image commit 769a883c93f4f7c6191462626845f01d6f8596fe Author: songchengjiang <moses_jc@sina.com> Date: Fri May 13 20:36:19 2016 +0800 Merge branch 'vr' of https://github.com/ricardoquesada/cocos2d-x into riq_vr commit d3e45501cd2cada87735390a407fbd44e61cd84e Author: songchengjiang <songcheng.jiang@chukong-inc.com> Date: Fri May 13 17:52:46 2016 +0800 update win32 project for VR commit ba0fdb8b8e1efaf8fdf88e044e701fb5bedb4839 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 12 23:21:50 2016 -0700 fix: no hardcoded values code is simpler, fixed a few bugs commit b30596cdae4cdcf39d960a39661a79200b27c7a8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 12 21:50:18 2016 -0700 fix: simplified distortion commit 37b184f084e7b50bac9a5a3c59a87f8f03440b53 Author: songchengjiang <songcheng.jiang@chukong-inc.com> Date: Fri May 13 09:39:34 2016 +0800 finished VR rendering of Oculus commit f7d74cd0a2479541c0546d0ec41fe4f867405fcc Author: songchengjiang <moses_jc@sina.com> Date: Thu May 12 10:00:43 2016 +0800 Finished GearVR and Deepoon VR rendering integration commit 776fb4fd7420f1c14403eb476df1e9716362f1e5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 11 16:56:57 2016 -0700 fix: distortion working... with too many hardcoded values time to "un-hardcode" the values commit 0584773cca88c5b3dee72821830841f617cfb21c Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue May 10 19:49:07 2016 -0700 feat: distortion WIP commit 9b5ef01776eaa617dd4677c3824e50c1f9da41c6 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue May 10 15:49:17 2016 -0700 fix: simpler one texture that holds both left and right eye commit bfff504c499c253a0c36b342e6b5bcb0edf4fed7 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon May 9 10:50:43 2016 -0700 fix: Camera code is cleaner commit fcf730bc2afc6c4552787273577e1942088c2e42 Author: songchengjiang <moses_jc@sina.com> Date: Mon May 9 16:43:58 2016 +0800 support cardboard VR rendering commit f88b834b70fbfb28db0c8442e68984c61192d7cc Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 6 21:16:10 2016 -0700 fix: proj fixes. works ok in any resolution commit f980a616837b0f259d9564e622c78e0f4869ed53 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 6 19:08:47 2016 -0700 fix: renders ok in any device commit 4799ad32ea38ddd217e80e868c3d9021c03e88ce Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Apr 28 16:46:30 2016 -0700 fix: renders something fix: passing camera to scene adds a way to return user camera fix: stereo rendering works fix: viewport left is correct fix: scissor is not needed... fix: works! fix: minor fixes fix: new approach... almost working fix: camera is moved whitespace fixes fix: whitespaces fix: new line commit c137a53aba227cf2e2a1809b55cb9b3da25d432b Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 27 18:56:41 2016 -0700 fix: VR refactor... still WIP commit 16fde77d71b8309e982bf6fa4f4ee0acea5fc0d1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Apr 26 22:21:21 2016 -0700 feat: VR, initial commit
2016-06-16 02:33:25 +08:00
for (auto& camera: scene->getCameras())
{
if (camera->isVisible() == false)
{
continue;
}
Camera::_visitingCamera = camera;
auto cameraFlag = (unsigned short)camera->getCameraFlag();
for (auto& l : sceneListeners)
{
if (nullptr == l->getAssociatedNode() || 0 == (l->getAssociatedNode()->getCameraMask() & cameraFlag))
{
continue;
}
if (onEvent(l))
{
shouldStopPropagation = true;
break;
}
}
if (shouldStopPropagation)
{
break;
}
}
Camera::_visitingCamera = nullptr;
}
}
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);
if (l->isEnabled() && !l->isPaused() && l->isRegistered() && onEvent(l))
{
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 pfnDispatchEventToListeners = &EventDispatcher::dispatchEventToListeners;
if (event->getType() == Event::Type::MOUSE) {
pfnDispatchEventToListeners = &EventDispatcher::dispatchTouchEventToListeners;
}
auto iter = _listenerMap.find(listenerID);
if (iter != _listenerMap.end())
2013-09-20 22:23:13 +08:00
{
auto listeners = iter->second;
auto onEvent = [&event](EventListener* listener) -> bool{
event->setCurrentTarget(listener->getAssociatedNode());
listener->_onEvent(event);
return event->isStopped();
};
(this->*pfnDispatchEventToListeners)(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
const std::vector<Touch*>& originalTouches = event->getTouches();
std::vector<Touch*> mutableTouches(originalTouches.size());
std::copy(originalTouches.begin(), originalTouches.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 = originalTouches.begin();
2013-09-20 22:23:13 +08:00
for (; touchesIter != originalTouches.end(); ++touchesIter)
2013-09-20 22:23:13 +08:00
{
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(),
"touchesIter ID should be equal to mutableTouchesIter's ID.");
2013-09-20 22:23:13 +08:00
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;
};
//
dispatchTouchEventToListeners(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 true;
2013-09-20 22:23:13 +08:00
}
return false;
};
dispatchTouchEventToListeners(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
{
CCASSERT(_inDispatch > 0, "If program goes here, there should be event in dispatch.");
if (_inDispatch > 1)
return;
auto onUpdateListeners = [this](const EventListener::ListenerID& listenerID)
2013-09-20 22:23:13 +08:00
{
auto listenersIter = _listenerMap.find(listenerID);
if (listenersIter == _listenerMap.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);
// if item in toRemove list, remove it from the list
auto matchIter = std::find(_toRemovedListeners.begin(), _toRemovedListeners.end(), l);
if (matchIter != _toRemovedListeners.end())
_toRemovedListeners.erase(matchIter);
2015-12-17 21:55:47 +08:00
releaseListener(l);
}
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);
// if item in toRemove list, remove it from the list
auto matchIter = std::find(_toRemovedListeners.begin(), _toRemovedListeners.end(), l);
if (matchIter != _toRemovedListeners.end())
_toRemovedListeners.erase(matchIter);
2015-12-17 21:55:47 +08:00
releaseListener(l);
}
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 (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
}
CCASSERT(_inDispatch == 1, "_inDispatch should be 1 here.");
for (auto iter = _listenerMap.begin(); iter != _listenerMap.end();)
{
if (iter->second->empty())
{
_priorityDirtyFlagMap.erase(iter->first);
delete iter->second;
iter = _listenerMap.erase(iter);
}
else
{
++iter;
}
}
2013-09-20 22:23:13 +08:00
if (!_toAddedListeners.empty())
{
for (auto& listener : _toAddedListeners)
2013-09-20 22:23:13 +08:00
{
forceAddEventListener(listener);
2013-09-20 22:23:13 +08:00
}
_toAddedListeners.clear();
}
if (!_toRemovedListeners.empty())
{
2015-10-19 16:06:39 +08:00
cleanToRemovedListeners();
}
2013-09-20 22:23:13 +08:00
}
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)
{
// Clear the dirty flag first, if `rootNode` is nullptr, then set its dirty flag of scene graph priority
dirtyIter->second = DirtyFlag::NONE;
if ((int)dirtyFlag & (int)DirtyFlag::FIXED_PRIORITY)
{
sortEventListenersOfFixedPriority(listenerID);
}
if ((int)dirtyFlag & (int)DirtyFlag::SCENE_GRAPH_PRIORITY)
{
auto rootNode = Director::getInstance()->getRunningScene();
if (rootNode)
{
sortEventListenersOfSceneGraphPriority(listenerID, rootNode);
}
else
{
dirtyIter->second = DirtyFlag::SCENE_GRAPH_PRIORITY;
}
}
}
}
void EventDispatcher::sortEventListenersOfSceneGraphPriority(const EventListener::ListenerID& listenerID, Node* rootNode)
{
auto listeners = getListeners(listenerID);
if (listeners == nullptr)
return;
auto sceneGraphListeners = listeners->getSceneGraphPriorityListeners();
if (sceneGraphListeners == nullptr)
return;
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, true);
// After sort: priority < 0, > 0
std::sort(sceneGraphListeners->begin(), sceneGraphListeners->end(), [this](const EventListener* l1, const EventListener* l2) {
return _nodePriorityMap[l1->getAssociatedNode()] > _nodePriorityMap[l2->getAssociatedNode()];
});
#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;
auto fixedListeners = listeners->getFixedPriorityListeners();
if (fixedListeners == nullptr)
return;
// After sort: priority < 0, > 0
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 = _listenerMap.find(listenerID);
if (iter != _listenerMap.end())
2013-09-20 22:23:13 +08:00
{
return iter->second;
}
return nullptr;
}
void EventDispatcher::removeEventListenersForListenerID(const EventListener::ListenerID& listenerID)
2013-09-20 22:23:13 +08:00
{
auto listenerItemIter = _listenerMap.find(listenerID);
if (listenerItemIter != _listenerMap.end())
2013-09-20 22:23:13 +08:00
{
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->getAssociatedNode() != nullptr)
{
dissociateNodeAndEventListener(l->getAssociatedNode(), l);
l->setAssociatedNode(nullptr); // nullptr out the node pointer so we don't have any dangling pointers to destroyed nodes.
}
if (_inDispatch == 0)
{
iter = listenerVector->erase(iter);
2015-12-17 21:55:47 +08:00
releaseListener(l);
}
else
{
++iter;
}
2013-09-20 22:23:13 +08:00
}
};
removeAllListenersInVector(sceneGraphPriorityListeners);
removeAllListenersInVector(fixedPriorityListeners);
2013-09-20 22:23:13 +08:00
// Remove the dirty flag according the 'listenerID'.
// No need to check whether the dispatcher is dispatching event.
_priorityDirtyFlagMap.erase(listenerID);
2013-09-20 22:23:13 +08:00
if (!_inDispatch)
{
listeners->clear();
delete listeners;
_listenerMap.erase(listenerItemIter);
2013-09-20 22:23:13 +08:00
}
}
for (auto iter = _toAddedListeners.begin(); iter != _toAddedListeners.end();)
{
if ((*iter)->getListenerID() == listenerID)
{
(*iter)->setRegistered(false);
2015-12-17 21:55:47 +08:00
releaseListener(*iter);
iter = _toAddedListeners.erase(iter);
}
else
{
++iter;
}
}
2013-09-20 22:23:13 +08:00
}
void EventDispatcher::removeEventListenersForType(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
{
bool cleanMap = true;
std::vector<EventListener::ListenerID> types;
types.reserve(_listenerMap.size());
for (const auto& e : _listenerMap)
2013-09-20 22:23:13 +08:00
{
if (_internalCustomListenerIDs.find(e.first) != _internalCustomListenerIDs.end())
{
cleanMap = false;
}
else
{
types.push_back(e.first);
}
2013-09-20 22:23:13 +08:00
}
for (const auto& type : types)
{
removeEventListenersForListenerID(type);
}
if (!_inDispatch && cleanMap)
2013-09-20 22:23:13 +08:00
{
_listenerMap.clear();
2013-09-20 22:23:13 +08:00
}
}
void EventDispatcher::setEnabled(bool isEnabled)
{
_isEnabled = isEnabled;
}
bool EventDispatcher::isEnabled() const
{
return _isEnabled;
}
void EventDispatcher::setDirtyForNode(Node* node)
{
// Mark the node dirty only when there is an eventlistener associated with it.
2013-10-24 17:27:22 +08:00
if (_nodeListenersMap.find(node) != _nodeListenersMap.end())
{
_dirtyNodes.insert(node);
}
// Also set the dirty flag for node's children
const auto& children = node->getChildren();
for (const auto& child : children)
{
setDirtyForNode(child);
}
}
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
}
}
2015-10-19 16:06:39 +08:00
void EventDispatcher::cleanToRemovedListeners()
{
for (auto& l : _toRemovedListeners)
{
auto listenersIter = _listenerMap.find(l->getListenerID());
if (listenersIter == _listenerMap.end())
{
2015-12-17 21:55:47 +08:00
releaseListener(l);
continue;
}
bool find = false;
auto listeners = listenersIter->second;
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
if (sceneGraphPriorityListeners)
{
auto machedIter = std::find(sceneGraphPriorityListeners->begin(), sceneGraphPriorityListeners->end(), l);
if (machedIter != sceneGraphPriorityListeners->end())
{
find = true;
2015-12-17 21:55:47 +08:00
releaseListener(l);
sceneGraphPriorityListeners->erase(machedIter);
}
}
if (fixedPriorityListeners)
{
auto machedIter = std::find(fixedPriorityListeners->begin(), fixedPriorityListeners->end(), l);
if (machedIter != fixedPriorityListeners->end())
{
find = true;
2015-12-17 21:55:47 +08:00
releaseListener(l);
fixedPriorityListeners->erase(machedIter);
}
}
if (find)
{
if (sceneGraphPriorityListeners && sceneGraphPriorityListeners->empty())
{
listeners->clearSceneGraphListeners();
}
if (fixedPriorityListeners && fixedPriorityListeners->empty())
{
listeners->clearFixedListeners();
}
}
else
CC_SAFE_RELEASE(l);
}
_toRemovedListeners.clear();
}
2015-12-17 21:55:47 +08:00
void EventDispatcher::releaseListener(EventListener* listener)
{
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
if (listener && sEngine)
2015-12-17 21:55:47 +08:00
{
sEngine->releaseScriptObject(this, listener);
}
2015-12-17 21:55:47 +08:00
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
CC_SAFE_RELEASE(listener);
}
2013-09-20 22:23:13 +08:00
NS_CC_END