2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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 "base/CCEventDispatcher.h"
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#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"
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID || AX_TARGET_PLATFORM == AX_PLATFORM_IOS || \
|
|
|
|
AX_TARGET_PLATFORM == AX_PLATFORM_MAC || AX_TARGET_PLATFORM == AX_PLATFORM_LINUX || \
|
|
|
|
AX_TARGET_PLATFORM == AX_PLATFORM_WIN32)
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "base/CCEventListenerController.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
#include "2d/CCScene.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCEventType.h"
|
|
|
|
#include "2d/CCCamera.h"
|
|
|
|
|
|
|
|
#define DUMP_LISTENER_ITEM_PRIORITY_INFO 0
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
class DispatchGuard
|
|
|
|
{
|
|
|
|
public:
|
2021-12-25 10:04:45 +08:00
|
|
|
DispatchGuard(int& count) : _count(count) { ++_count; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
~DispatchGuard() { --_count; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
int& _count;
|
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
static EventListener::ListenerID __getListenerID(Event* event)
|
|
|
|
{
|
|
|
|
EventListener::ListenerID ret;
|
|
|
|
switch (event->getType())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
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;
|
|
|
|
case Event::Type::MOUSE:
|
|
|
|
ret = EventListenerMouse::LISTENER_ID;
|
|
|
|
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.
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "Don't call this method if the event is for touch.");
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID || AX_TARGET_PLATFORM == AX_PLATFORM_IOS || \
|
|
|
|
AX_TARGET_PLATFORM == AX_PLATFORM_MAC || AX_TARGET_PLATFORM == AX_PLATFORM_LINUX || \
|
|
|
|
AX_TARGET_PLATFORM == AX_PLATFORM_WIN32)
|
2021-12-25 10:04:45 +08:00
|
|
|
case Event::Type::GAME_CONTROLLER:
|
|
|
|
ret = EventListenerController::LISTENER_ID;
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
2021-12-25 10:04:45 +08:00
|
|
|
default:
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "Invalid type!");
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
EventDispatcher::EventListenerVector::EventListenerVector()
|
|
|
|
: _fixedListeners(nullptr), _sceneGraphListeners(nullptr), _gt0Index(0)
|
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
EventDispatcher::EventListenerVector::~EventListenerVector()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(_sceneGraphListeners);
|
|
|
|
AX_SAFE_DELETE(_fixedListeners);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t EventDispatcher::EventListenerVector::size() const
|
|
|
|
{
|
|
|
|
size_t ret = 0;
|
|
|
|
if (_sceneGraphListeners)
|
|
|
|
ret += _sceneGraphListeners->size();
|
|
|
|
if (_fixedListeners)
|
|
|
|
ret += _fixedListeners->size();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EventDispatcher::EventListenerVector::empty() const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return (_sceneGraphListeners == nullptr || _sceneGraphListeners->empty()) &&
|
|
|
|
(_fixedListeners == nullptr || _fixedListeners->empty());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-08-08 13:18:33 +08:00
|
|
|
void EventDispatcher::EventListenerVector::emplace_back(EventListener* listener)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_NODE_DEBUG_VERIFY_EVENT_LISTENERS
|
|
|
|
AXASSERT(_sceneGraphListeners == nullptr ||
|
2021-12-25 10:04:45 +08:00
|
|
|
std::count(_sceneGraphListeners->begin(), _sceneGraphListeners->end(), listener) == 0,
|
2019-11-23 20:27:39 +08:00
|
|
|
"Listener should not be added twice!");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_fixedListeners == nullptr || std::count(_fixedListeners->begin(), _fixedListeners->end(), listener) == 0,
|
2019-11-23 20:27:39 +08:00
|
|
|
"Listener should not be added twice!");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (listener->getFixedPriority() == 0)
|
|
|
|
{
|
|
|
|
if (_sceneGraphListeners == nullptr)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_sceneGraphListeners = new std::vector<EventListener*>();
|
2019-11-23 20:27:39 +08:00
|
|
|
_sceneGraphListeners->reserve(100);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-08-08 13:18:33 +08:00
|
|
|
_sceneGraphListeners->emplace_back(listener);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_fixedListeners == nullptr)
|
|
|
|
{
|
|
|
|
_fixedListeners = new std::vector<EventListener*>();
|
|
|
|
_fixedListeners->reserve(100);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-08-08 13:18:33 +08:00
|
|
|
_fixedListeners->emplace_back(listener);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::EventListenerVector::clearSceneGraphListeners()
|
|
|
|
{
|
|
|
|
if (_sceneGraphListeners)
|
|
|
|
{
|
|
|
|
_sceneGraphListeners->clear();
|
|
|
|
delete _sceneGraphListeners;
|
|
|
|
_sceneGraphListeners = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::EventListenerVector::clearFixedListeners()
|
|
|
|
{
|
|
|
|
if (_fixedListeners)
|
|
|
|
{
|
|
|
|
_fixedListeners->clear();
|
|
|
|
delete _fixedListeners;
|
|
|
|
_fixedListeners = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::EventListenerVector::clear()
|
|
|
|
{
|
|
|
|
clearSceneGraphListeners();
|
|
|
|
clearFixedListeners();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
EventDispatcher::EventDispatcher() : _inDispatch(0), _isEnabled(false), _nodePriorityIndex(0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_toAddedListeners.reserve(50);
|
|
|
|
_toRemovedListeners.reserve(50);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
EventDispatcher::~EventDispatcher()
|
|
|
|
{
|
|
|
|
// Clear internal custom listener IDs from set,
|
|
|
|
// so removeAllEventListeners would clean internal custom listeners.
|
|
|
|
_internalCustomListenerIDs.clear();
|
|
|
|
removeAllEventListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::visitTarget(Node* node, bool isRootNode)
|
|
|
|
{
|
|
|
|
node->sortAllChildren();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
int i = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
auto& children = node->getChildren();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto childrenCount = children.size();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (childrenCount > 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Node* child = nullptr;
|
|
|
|
// visit children zOrder < 0
|
2021-12-25 10:04:45 +08:00
|
|
|
for (; i < childrenCount; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
child = children.at(i);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (child && child->getLocalZOrder() < 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
visitTarget(child, false);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_nodeListenersMap.find(node) != _nodeListenersMap.end())
|
|
|
|
{
|
2022-08-08 13:18:33 +08:00
|
|
|
_globalZOrderNodeMap[node->getGlobalZOrder()].emplace_back(node);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (; i < childrenCount; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
child = children.at(i);
|
|
|
|
if (child)
|
|
|
|
visitTarget(child, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_nodeListenersMap.find(node) != _nodeListenersMap.end())
|
|
|
|
{
|
2022-08-08 13:18:33 +08:00
|
|
|
_globalZOrderNodeMap[node->getGlobalZOrder()].emplace_back(node);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (isRootNode)
|
|
|
|
{
|
|
|
|
std::vector<float> globalZOrders;
|
|
|
|
globalZOrders.reserve(_globalZOrderNodeMap.size());
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
for (const auto& e : _globalZOrderNodeMap)
|
|
|
|
{
|
2022-08-08 13:18:33 +08:00
|
|
|
globalZOrders.emplace_back(e.first);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
std::stable_sort(globalZOrders.begin(), globalZOrders.end(),
|
|
|
|
[](const float a, const float b) { return a < b; });
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
for (const auto& globalZ : globalZOrders)
|
|
|
|
{
|
|
|
|
for (const auto& n : _globalZOrderNodeMap[globalZ])
|
|
|
|
{
|
|
|
|
_nodePriorityMap[n] = ++_nodePriorityIndex;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_globalZOrderNodeMap.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void EventDispatcher::pauseEventListenersForTarget(Node* target, bool recursive /* = false */)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto listenerIter = _nodeListenersMap.find(target);
|
|
|
|
if (listenerIter != _nodeListenersMap.end())
|
|
|
|
{
|
|
|
|
auto listeners = listenerIter->second;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& l : *listeners)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
l->setPaused(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& listener : _toAddedListeners)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (listener->getAssociatedNode() == target)
|
|
|
|
{
|
|
|
|
listener->setPaused(true);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (recursive)
|
|
|
|
{
|
|
|
|
const auto& children = target->getChildren();
|
|
|
|
for (const auto& child : children)
|
|
|
|
{
|
|
|
|
pauseEventListenersForTarget(child, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void EventDispatcher::resumeEventListenersForTarget(Node* target, bool recursive /* = false */)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto listenerIter = _nodeListenersMap.find(target);
|
|
|
|
if (listenerIter != _nodeListenersMap.end())
|
|
|
|
{
|
|
|
|
auto listeners = listenerIter->second;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& l : *listeners)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
l->setPaused(false);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& listener : _toAddedListeners)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (listener->getAssociatedNode() == target)
|
|
|
|
{
|
|
|
|
listener->setPaused(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setDirtyForNode(target);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (recursive)
|
|
|
|
{
|
|
|
|
const auto& children = target->getChildren();
|
|
|
|
for (const auto& child : children)
|
|
|
|
{
|
|
|
|
resumeEventListenersForTarget(child, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void EventDispatcher::removeEventListenersForTarget(Node* target, bool recursive /* = false */)
|
2019-11-23 20:27:39 +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())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto listeners = listenerIter->second;
|
2019-11-23 20:27:39 +08:00
|
|
|
auto listenersCopy = *listeners;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& l : listenersCopy)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
removeEventListener(l);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// 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.)
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto iter = _toAddedListeners.begin(); iter != _toAddedListeners.end();)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
EventListener* listener = *iter;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (listener->getAssociatedNode() == target)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
listener->setAssociatedNode(nullptr); // Ensure no dangling ptr to the target node.
|
2019-11-23 20:27:39 +08:00
|
|
|
listener->setRegistered(false);
|
|
|
|
releaseListener(listener);
|
|
|
|
iter = _toAddedListeners.erase(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto found = _nodeListenersMap.find(node);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (found != _nodeListenersMap.end())
|
|
|
|
{
|
|
|
|
listeners = found->second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
listeners = new std::vector<EventListener*>();
|
2019-11-23 20:27:39 +08:00
|
|
|
_nodeListenersMap.emplace(node, listeners);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-08-08 13:18:33 +08:00
|
|
|
listeners->emplace_back(listener);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::dissociateNodeAndEventListener(Node* node, EventListener* listener)
|
|
|
|
{
|
|
|
|
std::vector<EventListener*>* listeners = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto found = _nodeListenersMap.find(node);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (found != _nodeListenersMap.end())
|
|
|
|
{
|
|
|
|
listeners = found->second;
|
|
|
|
auto iter = std::find(listeners->begin(), listeners->end(), listener);
|
|
|
|
if (iter != listeners->end())
|
|
|
|
{
|
|
|
|
listeners->erase(iter);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (listeners->empty())
|
|
|
|
{
|
|
|
|
_nodeListenersMap.erase(found);
|
|
|
|
delete listeners;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::addEventListener(EventListener* listener)
|
|
|
|
{
|
|
|
|
if (_inDispatch == 0)
|
|
|
|
{
|
|
|
|
forceAddEventListener(listener);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-08 13:18:33 +08:00
|
|
|
_toAddedListeners.emplace_back(listener);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
|
|
|
|
if (sEngine)
|
|
|
|
{
|
|
|
|
sEngine->retainScriptObject(this, listener);
|
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
#endif // AX_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2019-11-23 20:27:39 +08:00
|
|
|
listener->retain();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::forceAddEventListener(EventListener* listener)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
EventListenerVector* listeners = nullptr;
|
|
|
|
auto listenerID = listener->getListenerID();
|
|
|
|
auto itr = _listenerMap.find(listenerID);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (itr == _listenerMap.end())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
listeners = new EventListenerVector();
|
2019-11-23 20:27:39 +08:00
|
|
|
_listenerMap.emplace(listenerID, listeners);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
listeners = itr->second;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-08-08 13:18:33 +08:00
|
|
|
listeners->emplace_back(listener);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (listener->getFixedPriority() == 0)
|
|
|
|
{
|
|
|
|
setDirty(listenerID, DirtyFlag::SCENE_GRAPH_PRIORITY);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto node = listener->getAssociatedNode();
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(node != nullptr, "Invalid scene graph priority!");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
associateNodeAndEventListener(node, listener);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!node->isRunning())
|
|
|
|
{
|
|
|
|
listener->setPaused(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setDirty(listenerID, DirtyFlag::FIXED_PRIORITY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(listener && node, "Invalid parameters.");
|
|
|
|
AXASSERT(!listener->isRegistered(), "The listener has been registered.");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!listener->checkAvailable())
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
listener->setAssociatedNode(node);
|
|
|
|
listener->setFixedPriority(0);
|
|
|
|
listener->setRegistered(true);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
addEventListener(listener);
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
#if AX_NODE_DEBUG_VERIFY_EVENT_LISTENERS && _AX_DEBUG > 0
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
void EventDispatcher::debugCheckNodeHasNoEventListenersOnDestruction(Node* node)
|
|
|
|
{
|
|
|
|
// Check the listeners map
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& keyValuePair : _listenerMap)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const EventListenerVector* eventListenerVector = keyValuePair.second;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (eventListenerVector)
|
|
|
|
{
|
|
|
|
if (eventListenerVector->getSceneGraphPriorityListeners())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (EventListener* listener : *eventListenerVector->getSceneGraphPriorityListeners())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(!listener || listener->getAssociatedNode() != node,
|
2019-11-23 20:27:39 +08:00
|
|
|
"Node should have no event listeners registered for it upon destruction!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Check the node listeners map
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& keyValuePair : _nodeListenersMap)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(keyValuePair.first != node, "Node should have no event listeners registered for it upon destruction!");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (keyValuePair.second)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (EventListener* listener : *keyValuePair.second)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(listener->getAssociatedNode() != node,
|
2019-11-23 20:27:39 +08:00
|
|
|
"Node should have no event listeners registered for it upon destruction!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Check the node priority map
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& keyValuePair : _nodePriorityMap)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(keyValuePair.first != node, "Node should have no event listeners registered for it upon destruction!");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Check the to be added list
|
2021-12-25 10:04:45 +08:00
|
|
|
for (EventListener* listener : _toAddedListeners)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(listener->getAssociatedNode() != node,
|
2019-11-23 20:27:39 +08:00
|
|
|
"Node should have no event listeners registered for it upon destruction!");
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Check the dirty nodes set
|
2021-12-25 10:04:45 +08:00
|
|
|
for (Node* dirtyNode : _dirtyNodes)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(dirtyNode != node, "Node should have no event listeners registered for it upon destruction!");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
#endif // #if AX_NODE_DEBUG_VERIFY_EVENT_LISTENERS && _AX_DEBUG > 0
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(listener, "Invalid parameters.");
|
|
|
|
AXASSERT(!listener->isRegistered(), "The listener has been registered.");
|
|
|
|
AXASSERT(fixedPriority != 0,
|
2021-12-25 10:04:45 +08:00
|
|
|
"0 priority is forbidden for fixed priority since it's used for scene graph based priority.");
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!listener->checkAvailable())
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
listener->setAssociatedNode(nullptr);
|
|
|
|
listener->setFixedPriority(fixedPriority);
|
|
|
|
listener->setRegistered(true);
|
|
|
|
listener->setPaused(false);
|
|
|
|
|
|
|
|
addEventListener(listener);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
EventListenerCustom* EventDispatcher::addCustomEventListener(std::string_view eventName,
|
2021-12-25 10:04:45 +08:00
|
|
|
const std::function<void(EventCustom*)>& callback)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
EventListenerCustom* listener = EventListenerCustom::create(eventName, callback);
|
2019-11-23 20:27:39 +08:00
|
|
|
addEventListenerWithFixedPriority(listener, 1);
|
|
|
|
return listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::removeEventListener(EventListener* listener)
|
|
|
|
{
|
|
|
|
if (listener == nullptr)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// 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;
|
|
|
|
|
|
|
|
bool isFound = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto removeListenerInVector = [&](std::vector<EventListener*>* listeners) {
|
2019-11-23 20:27:39 +08:00
|
|
|
if (listeners == nullptr)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
for (auto iter = listeners->begin(); iter != listeners->end(); ++iter)
|
|
|
|
{
|
|
|
|
auto l = *iter;
|
|
|
|
if (l == listener)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RETAIN(l);
|
2019-11-23 20:27:39 +08:00
|
|
|
l->setRegistered(false);
|
|
|
|
if (l->getAssociatedNode() != nullptr)
|
|
|
|
{
|
|
|
|
dissociateNodeAndEventListener(l->getAssociatedNode(), l);
|
2021-12-25 10:04:45 +08:00
|
|
|
l->setAssociatedNode(nullptr); // nullptr out the node pointer so we don't have any dangling
|
|
|
|
// pointers to destroyed nodes.
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_inDispatch == 0)
|
|
|
|
{
|
|
|
|
iter = listeners->erase(iter);
|
|
|
|
releaseListener(l);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-08 13:18:33 +08:00
|
|
|
_toRemovedListeners.emplace_back(l);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
isFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
for (auto iter = _listenerMap.begin(); iter != _listenerMap.end();)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto listeners = iter->second;
|
|
|
|
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_NODE_DEBUG_VERIFY_EVENT_LISTENERS
|
|
|
|
AXASSERT(
|
2021-12-25 10:04:45 +08:00
|
|
|
_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.");
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_inDispatch != 0 || !fixedPriorityListeners ||
|
2021-12-25 10:04:45 +08:00
|
|
|
std::count(fixedPriorityListeners->begin(), fixedPriorityListeners->end(), listener) == 0,
|
2019-11-23 20:27:39 +08:00
|
|
|
"Listener should be in no lists after this is done if we're not currently in dispatch mode.");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (iter->second->empty())
|
|
|
|
{
|
|
|
|
_priorityDirtyFlagMap.erase(listener->getListenerID());
|
|
|
|
auto list = iter->second;
|
2021-12-25 10:04:45 +08:00
|
|
|
iter = _listenerMap.erase(iter);
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(list);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (isFound)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFound)
|
|
|
|
{
|
|
|
|
releaseListener(listener);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto iter = _toAddedListeners.begin(); iter != _toAddedListeners.end(); ++iter)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (*iter == listener)
|
|
|
|
{
|
|
|
|
listener->setRegistered(false);
|
|
|
|
releaseListener(listener);
|
|
|
|
_toAddedListeners.erase(iter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::setPriority(EventListener* listener, int fixedPriority)
|
|
|
|
{
|
|
|
|
if (listener == nullptr)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& iter : _listenerMap)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto fixedPriorityListeners = iter.second->getFixedPriorityListeners();
|
|
|
|
if (fixedPriorityListeners)
|
|
|
|
{
|
|
|
|
auto found = std::find(fixedPriorityListeners->begin(), fixedPriorityListeners->end(), listener);
|
|
|
|
if (found != fixedPriorityListeners->end())
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(listener->getAssociatedNode() == nullptr,
|
2021-12-25 10:04:45 +08:00
|
|
|
"Can't set fixed priority with scene graph based listener.");
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (listener->getFixedPriority() != fixedPriority)
|
|
|
|
{
|
|
|
|
listener->setFixedPriority(fixedPriority);
|
|
|
|
setDirty(listener->getListenerID(), DirtyFlag::FIXED_PRIORITY);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void EventDispatcher::dispatchEventToListeners(EventListenerVector* listeners,
|
|
|
|
const std::function<bool(EventListener*)>& onEvent)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
bool shouldStopPropagation = false;
|
|
|
|
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ssize_t i = 0;
|
|
|
|
// priority < 0
|
|
|
|
if (fixedPriorityListeners)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(listeners->getGt0Index() <= static_cast<ssize_t>(fixedPriorityListeners->size()),
|
2021-12-25 10:04:45 +08:00
|
|
|
"Out of range exception!");
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (sceneGraphPriorityListeners)
|
|
|
|
{
|
|
|
|
if (!shouldStopPropagation)
|
|
|
|
{
|
|
|
|
// priority == 0, scene graph priority
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& l : *sceneGraphPriorityListeners)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (l->isEnabled() && !l->isPaused() && l->isRegistered() && onEvent(l))
|
|
|
|
{
|
|
|
|
shouldStopPropagation = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (fixedPriorityListeners)
|
|
|
|
{
|
|
|
|
if (!shouldStopPropagation)
|
|
|
|
{
|
|
|
|
// priority > 0
|
|
|
|
ssize_t size = fixedPriorityListeners->size();
|
|
|
|
for (; i < size; ++i)
|
|
|
|
{
|
|
|
|
auto l = fixedPriorityListeners->at(i);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (l->isEnabled() && !l->isPaused() && l->isRegistered() && onEvent(l))
|
|
|
|
{
|
|
|
|
shouldStopPropagation = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void EventDispatcher::dispatchTouchEventToListeners(EventListenerVector* listeners,
|
|
|
|
const std::function<bool(EventListener*)>& onEvent)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
bool shouldStopPropagation = false;
|
|
|
|
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ssize_t i = 0;
|
|
|
|
// priority < 0
|
|
|
|
if (fixedPriorityListeners)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(listeners->getGt0Index() <= static_cast<ssize_t>(fixedPriorityListeners->size()),
|
2021-12-25 10:04:45 +08:00
|
|
|
"Out of range exception!");
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto scene = Director::getInstance()->getRunningScene();
|
|
|
|
if (scene && sceneGraphPriorityListeners)
|
|
|
|
{
|
|
|
|
if (!shouldStopPropagation)
|
|
|
|
{
|
|
|
|
// priority == 0, scene graph priority
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// first, get all enabled, unPaused and registered listeners
|
|
|
|
std::vector<EventListener*> sceneListeners;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& l : *sceneGraphPriorityListeners)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (l->isEnabled() && !l->isPaused() && l->isRegistered())
|
|
|
|
{
|
2022-08-08 13:18:33 +08:00
|
|
|
sceneListeners.emplace_back(l);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
auto cameras = scene->getCameras();
|
|
|
|
for (auto rit = cameras.rbegin(), ritRend = cameras.rend(); rit != ritRend; ++rit)
|
|
|
|
{
|
|
|
|
Camera* camera = *rit;
|
|
|
|
if (camera->isVisible() == false)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Camera::_visitingCamera = camera;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto cameraFlag = (unsigned short)camera->getCameraFlag();
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& l : sceneListeners)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (nullptr == l->getAssociatedNode() ||
|
|
|
|
0 == (l->getAssociatedNode()->getCameraMask() & cameraFlag))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (onEvent(l))
|
|
|
|
{
|
|
|
|
shouldStopPropagation = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shouldStopPropagation)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Camera::_visitingCamera = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (fixedPriorityListeners)
|
|
|
|
{
|
|
|
|
if (!shouldStopPropagation)
|
|
|
|
{
|
|
|
|
// priority > 0
|
|
|
|
ssize_t size = fixedPriorityListeners->size();
|
|
|
|
for (; i < size; ++i)
|
|
|
|
{
|
|
|
|
auto l = fixedPriorityListeners->at(i);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (l->isEnabled() && !l->isPaused() && l->isRegistered() && onEvent(l))
|
|
|
|
{
|
|
|
|
shouldStopPropagation = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::dispatchEvent(Event* event)
|
|
|
|
{
|
|
|
|
if (!_isEnabled)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
updateDirtyFlagForSceneGraph();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
DispatchGuard guard(_inDispatch);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (event->getType() == Event::Type::TOUCH)
|
|
|
|
{
|
|
|
|
dispatchTouchEvent(static_cast<EventTouch*>(event));
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto listenerID = __getListenerID(event);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
sortEventListeners(listenerID);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto pfnDispatchEventToListeners = &EventDispatcher::dispatchEventToListeners;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (event->getType() == Event::Type::MOUSE)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
pfnDispatchEventToListeners = &EventDispatcher::dispatchTouchEventToListeners;
|
|
|
|
}
|
|
|
|
auto iter = _listenerMap.find(listenerID);
|
|
|
|
if (iter != _listenerMap.end())
|
|
|
|
{
|
|
|
|
auto listeners = iter->second;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto onEvent = [&event](EventListener* listener) -> bool {
|
2019-11-23 20:27:39 +08:00
|
|
|
event->setCurrentTarget(listener->getAssociatedNode());
|
|
|
|
listener->_onEvent(event);
|
|
|
|
return event->isStopped();
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
(this->*pfnDispatchEventToListeners)(listeners, onEvent);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
updateListeners(event);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void EventDispatcher::dispatchCustomEvent(std::string_view eventName, void* optionalUserData)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
EventCustom ev(eventName);
|
|
|
|
ev.setUserData(optionalUserData);
|
|
|
|
dispatchEvent(&ev);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool EventDispatcher::hasEventListener(std::string_view listenerID) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return getListeners(listenerID) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::dispatchTouchEvent(EventTouch* event)
|
|
|
|
{
|
|
|
|
sortEventListeners(EventListenerTouchOneByOne::LISTENER_ID);
|
|
|
|
sortEventListeners(EventListenerTouchAllAtOnce::LISTENER_ID);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto oneByOneListeners = getListeners(EventListenerTouchOneByOne::LISTENER_ID);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto allAtOnceListeners = getListeners(EventListenerTouchAllAtOnce::LISTENER_ID);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// If there aren't any touch listeners, return directly.
|
|
|
|
if (nullptr == oneByOneListeners && nullptr == allAtOnceListeners)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bool isNeedsMutableSet = (oneByOneListeners && allAtOnceListeners);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
const std::vector<Touch*>& originalTouches = event->getTouches();
|
|
|
|
std::vector<Touch*> mutableTouches(originalTouches.size());
|
|
|
|
std::copy(originalTouches.begin(), originalTouches.end(), mutableTouches.begin());
|
|
|
|
|
|
|
|
//
|
|
|
|
// process the target handlers 1st
|
|
|
|
//
|
|
|
|
if (oneByOneListeners)
|
|
|
|
{
|
|
|
|
auto mutableTouchesIter = mutableTouches.begin();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& touches : originalTouches)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
bool isSwallowed = false;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto onTouchEvent = [&](EventListener* l) -> bool { // Return true to break
|
2019-11-23 20:27:39 +08:00
|
|
|
EventListenerTouchOneByOne* listener = static_cast<EventListenerTouchOneByOne*>(l);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Skip if the listener was removed.
|
|
|
|
if (!listener->_isRegistered)
|
|
|
|
return false;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
event->setCurrentTarget(listener->_node);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bool isClaimed = false;
|
|
|
|
std::vector<Touch*>::iterator removedIter;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
EventTouch::EventCode eventCode = event->getEventCode();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (eventCode == EventTouch::EventCode::BEGAN)
|
|
|
|
{
|
|
|
|
if (listener->onTouchBegan)
|
|
|
|
{
|
|
|
|
isClaimed = listener->onTouchBegan(touches, event);
|
|
|
|
if (isClaimed && listener->_isRegistered)
|
|
|
|
{
|
2022-08-08 13:18:33 +08:00
|
|
|
listener->_claimedTouches.emplace_back(touches);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (!listener->_claimedTouches.empty() &&
|
|
|
|
((removedIter = std::find(listener->_claimedTouches.begin(), listener->_claimedTouches.end(),
|
|
|
|
touches)) != listener->_claimedTouches.end()))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
isClaimed = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
switch (eventCode)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case EventTouch::EventCode::MOVED:
|
|
|
|
if (listener->onTouchMoved)
|
|
|
|
{
|
|
|
|
listener->onTouchMoved(touches, event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventTouch::EventCode::ENDED:
|
|
|
|
if (listener->onTouchEnded)
|
|
|
|
{
|
|
|
|
listener->onTouchEnded(touches, event);
|
|
|
|
}
|
|
|
|
if (listener->_isRegistered)
|
|
|
|
{
|
|
|
|
listener->_claimedTouches.erase(removedIter);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventTouch::EventCode::CANCELLED:
|
|
|
|
if (listener->onTouchCancelled)
|
|
|
|
{
|
|
|
|
listener->onTouchCancelled(touches, event);
|
|
|
|
}
|
|
|
|
if (listener->_isRegistered)
|
|
|
|
{
|
|
|
|
listener->_claimedTouches.erase(removedIter);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "The eventcode is invalid.");
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// If the event was stopped, return directly.
|
|
|
|
if (event->isStopped())
|
|
|
|
{
|
|
|
|
updateListeners(event);
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(touches->getID() == (*mutableTouchesIter)->getID(),
|
2019-11-23 20:27:39 +08:00
|
|
|
"touches ID should be equal to mutableTouchesIter's ID.");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (isClaimed && listener->_isRegistered && listener->_needSwallow)
|
|
|
|
{
|
|
|
|
if (isNeedsMutableSet)
|
|
|
|
{
|
|
|
|
mutableTouchesIter = mutableTouches.erase(mutableTouchesIter);
|
2021-12-25 10:04:45 +08:00
|
|
|
isSwallowed = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
//
|
|
|
|
dispatchTouchEventToListeners(oneByOneListeners, onTouchEvent);
|
|
|
|
if (event->isStopped())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!isSwallowed)
|
|
|
|
++mutableTouchesIter;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
//
|
|
|
|
// process standard handlers 2nd
|
|
|
|
//
|
2020-08-18 14:29:09 +08:00
|
|
|
if (allAtOnceListeners && !mutableTouches.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto onTouchesEvent = [&](EventListener* l) -> bool {
|
2019-11-23 20:27:39 +08:00
|
|
|
EventListenerTouchAllAtOnce* listener = static_cast<EventListenerTouchAllAtOnce*>(l);
|
|
|
|
// Skip if the listener was removed.
|
|
|
|
if (!listener->_isRegistered)
|
|
|
|
return false;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
event->setCurrentTarget(listener->_node);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
switch (event->getEventCode())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case EventTouch::EventCode::BEGAN:
|
|
|
|
if (listener->onTouchesBegan)
|
|
|
|
{
|
|
|
|
listener->onTouchesBegan(mutableTouches, event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventTouch::EventCode::MOVED:
|
|
|
|
if (listener->onTouchesMoved)
|
|
|
|
{
|
|
|
|
listener->onTouchesMoved(mutableTouches, event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventTouch::EventCode::ENDED:
|
|
|
|
if (listener->onTouchesEnded)
|
|
|
|
{
|
|
|
|
listener->onTouchesEnded(mutableTouches, event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventTouch::EventCode::CANCELLED:
|
|
|
|
if (listener->onTouchesCancelled)
|
|
|
|
{
|
|
|
|
listener->onTouchesCancelled(mutableTouches, event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "The eventcode is invalid.");
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// If the event was stopped, return directly.
|
|
|
|
if (event->isStopped())
|
|
|
|
{
|
|
|
|
updateListeners(event);
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
dispatchTouchEventToListeners(allAtOnceListeners, onTouchesEvent);
|
|
|
|
if (event->isStopped())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
updateListeners(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::updateListeners(Event* event)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_inDispatch > 0, "If program goes here, there should be event in dispatch.");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (_inDispatch > 1)
|
|
|
|
return;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto onUpdateListeners = [this](const EventListener::ListenerID& listenerID) {
|
2019-11-23 20:27:39 +08:00
|
|
|
auto listenersIter = _listenerMap.find(listenerID);
|
|
|
|
if (listenersIter == _listenerMap.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto listeners = listenersIter->second;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (sceneGraphPriorityListeners)
|
|
|
|
{
|
|
|
|
for (auto iter = sceneGraphPriorityListeners->begin(); iter != sceneGraphPriorityListeners->end();)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
releaseListener(l);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (fixedPriorityListeners)
|
|
|
|
{
|
|
|
|
for (auto iter = fixedPriorityListeners->begin(); iter != fixedPriorityListeners->end();)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
releaseListener(l);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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));
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_inDispatch == 1, "_inDispatch should be 1 here.");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!_toAddedListeners.empty())
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& listener : _toAddedListeners)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
forceAddEventListener(listener);
|
|
|
|
}
|
|
|
|
_toAddedListeners.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_toRemovedListeners.empty())
|
|
|
|
{
|
|
|
|
cleanToRemovedListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::updateDirtyFlagForSceneGraph()
|
|
|
|
{
|
|
|
|
if (!_dirtyNodes.empty())
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& node : _dirtyNodes)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto iter = _nodeListenersMap.find(node);
|
|
|
|
if (iter != _nodeListenersMap.end())
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& l : *iter->second)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
setDirty(l->getListenerID(), DirtyFlag::SCENE_GRAPH_PRIORITY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_dirtyNodes.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void EventDispatcher::sortEventListeners(std::string_view listenerID)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
DirtyFlag dirtyFlag = DirtyFlag::NONE;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto dirtyIter = _priorityDirtyFlagMap.find(listenerID);
|
|
|
|
if (dirtyIter != _priorityDirtyFlagMap.end())
|
|
|
|
{
|
|
|
|
dirtyFlag = dirtyIter->second;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void EventDispatcher::sortEventListenersOfSceneGraphPriority(std::string_view listenerID, Node* rootNode)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto listeners = getListeners(listenerID);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (listeners == nullptr)
|
|
|
|
return;
|
|
|
|
auto sceneGraphListeners = listeners->getSceneGraphPriorityListeners();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (sceneGraphListeners == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Reset priority index
|
|
|
|
_nodePriorityIndex = 0;
|
|
|
|
_nodePriorityMap.clear();
|
|
|
|
|
|
|
|
visitTarget(rootNode, true);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// After sort: priority < 0, > 0
|
2021-12-25 10:04:45 +08:00
|
|
|
std::stable_sort(sceneGraphListeners->begin(), sceneGraphListeners->end(),
|
|
|
|
[this](const EventListener* l1, const EventListener* l2) {
|
|
|
|
return _nodePriorityMap[l1->getAssociatedNode()] > _nodePriorityMap[l2->getAssociatedNode()];
|
|
|
|
});
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#if DUMP_LISTENER_ITEM_PRIORITY_INFO
|
|
|
|
log("-----------------------------------");
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& l : *sceneGraphListeners)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
log("listener priority: node ([%s]%p), priority (%d)", typeid(*l->_node).name(), l->_node,
|
|
|
|
_nodePriorityMap[l->_node]);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void EventDispatcher::sortEventListenersOfFixedPriority(std::string_view listenerID)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto listeners = getListeners(listenerID);
|
|
|
|
|
|
|
|
if (listeners == nullptr)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto fixedListeners = listeners->getFixedPriorityListeners();
|
|
|
|
if (fixedListeners == nullptr)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// After sort: priority < 0, > 0
|
2021-12-25 10:04:45 +08:00
|
|
|
std::stable_sort(fixedListeners->begin(), fixedListeners->end(),
|
|
|
|
[](const EventListener* l1, const EventListener* l2) {
|
|
|
|
return l1->getFixedPriority() < l2->getFixedPriority();
|
|
|
|
});
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// FIXME: Should use binary search
|
|
|
|
int index = 0;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& listener : *fixedListeners)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (listener->getFixedPriority() >= 0)
|
|
|
|
break;
|
|
|
|
++index;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
listeners->setGt0Index(index);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#if DUMP_LISTENER_ITEM_PRIORITY_INFO
|
|
|
|
log("-----------------------------------");
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& l : *fixedListeners)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
log("listener priority: node (%p), fixed (%d)", l->_node, l->_fixedPriority);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
EventDispatcher::EventListenerVector* EventDispatcher::getListeners(std::string_view listenerID) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto iter = _listenerMap.find(listenerID);
|
|
|
|
if (iter != _listenerMap.end())
|
|
|
|
{
|
|
|
|
return iter->second;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void EventDispatcher::removeEventListenersForListenerID(std::string_view listenerID)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto listenerItemIter = _listenerMap.find(listenerID);
|
|
|
|
if (listenerItemIter != _listenerMap.end())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto listeners = listenerItemIter->second;
|
|
|
|
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto removeAllListenersInVector = [&](std::vector<EventListener*>* listenerVector) {
|
2019-11-23 20:27:39 +08:00
|
|
|
if (listenerVector == nullptr)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
for (auto iter = listenerVector->begin(); iter != listenerVector->end();)
|
|
|
|
{
|
|
|
|
auto l = *iter;
|
|
|
|
l->setRegistered(false);
|
|
|
|
if (l->getAssociatedNode() != nullptr)
|
|
|
|
{
|
|
|
|
dissociateNodeAndEventListener(l->getAssociatedNode(), l);
|
2021-12-25 10:04:45 +08:00
|
|
|
l->setAssociatedNode(nullptr); // nullptr out the node pointer so we don't have any dangling
|
|
|
|
// pointers to destroyed nodes.
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_inDispatch == 0)
|
|
|
|
{
|
|
|
|
iter = listenerVector->erase(iter);
|
|
|
|
releaseListener(l);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
removeAllListenersInVector(sceneGraphPriorityListeners);
|
|
|
|
removeAllListenersInVector(fixedPriorityListeners);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Remove the dirty flag according the 'listenerID'.
|
|
|
|
// No need to check whether the dispatcher is dispatching event.
|
|
|
|
_priorityDirtyFlagMap.erase(listenerID);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!_inDispatch)
|
|
|
|
{
|
|
|
|
listeners->clear();
|
|
|
|
delete listeners;
|
|
|
|
_listenerMap.erase(listenerItemIter);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
for (auto iter = _toAddedListeners.begin(); iter != _toAddedListeners.end();)
|
|
|
|
{
|
|
|
|
if ((*iter)->getListenerID() == listenerID)
|
|
|
|
{
|
|
|
|
(*iter)->setRegistered(false);
|
|
|
|
releaseListener(*iter);
|
|
|
|
iter = _toAddedListeners.erase(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "Invalid listener type!");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void EventDispatcher::removeCustomEventListeners(std::string_view customEventName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
removeEventListenersForListenerID(customEventName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::removeAllEventListeners()
|
|
|
|
{
|
|
|
|
bool cleanMap = true;
|
|
|
|
std::vector<EventListener::ListenerID> types;
|
|
|
|
types.reserve(_listenerMap.size());
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
for (const auto& e : _listenerMap)
|
|
|
|
{
|
|
|
|
if (_internalCustomListenerIDs.find(e.first) != _internalCustomListenerIDs.end())
|
|
|
|
{
|
|
|
|
cleanMap = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-08 13:18:33 +08:00
|
|
|
types.emplace_back(e.first);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& type : types)
|
|
|
|
{
|
|
|
|
removeEventListenersForListenerID(type);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!_inDispatch && cleanMap)
|
|
|
|
{
|
|
|
|
_listenerMap.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::setEnabled(bool isEnabled)
|
|
|
|
{
|
|
|
|
_isEnabled = isEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EventDispatcher::isEnabled() const
|
|
|
|
{
|
|
|
|
return _isEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::setDirtyForNode(Node* node)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// Mark the node dirty only when there is an eventlistener associated with it.
|
2019-11-23 20:27:39 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void EventDispatcher::setDirty(std::string_view listenerID, DirtyFlag flag)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto iter = _priorityDirtyFlagMap.find(listenerID);
|
|
|
|
if (iter == _priorityDirtyFlagMap.end())
|
|
|
|
{
|
|
|
|
_priorityDirtyFlagMap.emplace(listenerID, flag);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
int ret = (int)flag | (int)iter->second;
|
|
|
|
iter->second = (DirtyFlag)ret;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::cleanToRemovedListeners()
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& l : _toRemovedListeners)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto listenersIter = _listenerMap.find(l->getListenerID());
|
|
|
|
if (listenersIter == _listenerMap.end())
|
|
|
|
{
|
|
|
|
releaseListener(l);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool find = false;
|
|
|
|
auto listeners = listenersIter->second;
|
|
|
|
auto fixedPriorityListeners = listeners->getFixedPriorityListeners();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners();
|
|
|
|
|
|
|
|
if (sceneGraphPriorityListeners)
|
|
|
|
{
|
|
|
|
auto machedIter = std::find(sceneGraphPriorityListeners->begin(), sceneGraphPriorityListeners->end(), l);
|
|
|
|
if (machedIter != sceneGraphPriorityListeners->end())
|
|
|
|
{
|
|
|
|
find = true;
|
|
|
|
releaseListener(l);
|
|
|
|
sceneGraphPriorityListeners->erase(machedIter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fixedPriorityListeners)
|
|
|
|
{
|
|
|
|
auto machedIter = std::find(fixedPriorityListeners->begin(), fixedPriorityListeners->end(), l);
|
|
|
|
if (machedIter != fixedPriorityListeners->end())
|
|
|
|
{
|
|
|
|
find = true;
|
|
|
|
releaseListener(l);
|
|
|
|
fixedPriorityListeners->erase(machedIter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (find)
|
|
|
|
{
|
|
|
|
if (sceneGraphPriorityListeners && sceneGraphPriorityListeners->empty())
|
|
|
|
{
|
|
|
|
listeners->clearSceneGraphListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fixedPriorityListeners && fixedPriorityListeners->empty())
|
|
|
|
{
|
|
|
|
listeners->clearFixedListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(l);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_toRemovedListeners.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcher::releaseListener(EventListener* listener)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
|
|
|
|
if (listener && sEngine)
|
|
|
|
{
|
|
|
|
sEngine->releaseScriptObject(this, listener);
|
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
#endif // AX_ENABLE_GC_FOR_NATIVE_OBJECTS
|
|
|
|
AX_SAFE_RELEASE(listener);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|