/**************************************************************************** Copyright (c) 2021 Xiamen Yaji Software Co., Ltd. http://www.cocos.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated engine source code (the "Software"), a limited, worldwide, royalty-free, non-assignable, revocable and non-exclusive license to use Cocos Creator solely to develop games on your target platforms. You shall not use Cocos Creator software for developing other software or tools that's used for developing games. You are not granted to publish, distribute, sublicense, and/or sell copies of Cocos Creator. The software or tools in this License Agreement are licensed, not sold. Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. 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. ****************************************************************************/ #pragma once #include #include "base/memory/Memory.h" #include "base/std/any.h" #include "base/std/container/string.h" #include "core/event/CallbacksInvoker.h" //#include "core/event/Event.h" #include "core/scene-graph/NodeEvent.h" namespace cc { const ccstd::vector TOUCH_EVENTS{ cc::NodeEventType::TOUCH_START, cc::NodeEventType::TOUCH_MOVE, cc::NodeEventType::TOUCH_END, cc::NodeEventType::TOUCH_CANCEL}; const ccstd::vector MOUSE_EVENTS{ cc::NodeEventType::MOUSE_DOWN, cc::NodeEventType::MOUSE_ENTER, cc::NodeEventType::MOUSE_MOVE, cc::NodeEventType::MOUSE_LEAVE, cc::NodeEventType::MOUSE_UP, cc::NodeEventType::MOUSE_WHEEL}; class Node; class NodeEventProcessor final { public: NodeEventProcessor() = default; explicit NodeEventProcessor(Node *node); ~NodeEventProcessor(); inline Node *getNode() { return _node; } void reattach(); void destroy(); /** * @zh * 分发事件到事件流中。 * * @param event - 分派到事件流中的事件对象。 */ // void dispatchEvent(event::Event *event); bool hasEventListener(const CallbacksInvoker::KeyType &type) const; bool hasEventListener(const CallbacksInvoker::KeyType &type, CallbackInfoBase::ID cbID) const; bool hasEventListener(const CallbacksInvoker::KeyType &type, void *target) const; bool hasEventListener(const CallbacksInvoker::KeyType &type, void *target, CallbackInfoBase::ID cbID) const; template bool hasEventListener(const CallbacksInvoker::KeyType &type, void (Target::*memberFn)(Args...), Target *target) const; static bool checkListeners(Node *node, const ccstd::vector &events); template void on(const CallbacksInvoker::KeyType &type, std::function &&callback, CallbackInfoBase::ID &cbID, bool useCapture = false); template void on(const CallbacksInvoker::KeyType &type, void (Target::*memberFn)(Args...), Target *target, bool useCapture = false); template void on(const CallbacksInvoker::KeyType &type, std::function &&callback, Target *target, CallbackInfoBase::ID &cbID, bool useCapture = false); template std::enable_if_t::value, void> on(const CallbacksInvoker::KeyType &type, LambdaType &&callback, Target *target, CallbackInfoBase::ID &cbID, bool useCapture = false); template std::enable_if_t::value, void> on(const CallbacksInvoker::KeyType &type, LambdaType &&callback, CallbackInfoBase::ID &cbID, bool useCapture = false); template void once(const CallbacksInvoker::KeyType &type, void (Target::*memberFn)(Args...), Target *target, bool useCapture = false); template void once(const CallbacksInvoker::KeyType &type, std::function &&callback, CallbackInfoBase::ID &cbID, bool useCapture = false); template void once(const CallbacksInvoker::KeyType &type, std::function &&callback, Target *target, CallbackInfoBase::ID &cbID, bool useCapture = false); template std::enable_if_t::value, void> once(const CallbacksInvoker::KeyType &type, LambdaType &&callback, Target *target, CallbackInfoBase::ID &cbID, bool useCapture = false); template std::enable_if_t::value, void> once(const CallbacksInvoker::KeyType &type, LambdaType &&callback, CallbackInfoBase::ID &cbID, bool useCapture = false); void off(const CallbacksInvoker::KeyType &type, CallbackInfoBase::ID cbID, bool useCapture = false); void off(const CallbacksInvoker::KeyType &type, void *target, bool useCapture = false); template void off(const CallbacksInvoker::KeyType &type, void (Target::*memberFn)(Args...), Target *target, bool useCapture = false); /** * @zh * 通过事件名发送自定义事件 * * @param type - event type * @param args - The arguments to be passed to the callback */ template void emit(const CallbacksInvoker::KeyType &type, Args &&...args); void targetOff(const CallbacksInvoker::KeyType &target); void getCapturingTargets(const CallbacksInvoker::KeyType &type, ccstd::vector &targets) const; void getBubblingTargets(const CallbacksInvoker::KeyType &type, ccstd::vector &targets) const; inline CallbacksInvoker *getBubblingTargets() const { return _bubblingTargets; } inline CallbacksInvoker *getCapturingTargets() const { return _capturingTargets; } // inline event::EventListener *getTouchListener() const { return _touchListener; } // inline event::EventListener *getMouseListener() const { return _mouseListener; } private: /** * @zh * 节点冒泡事件监听器 */ CallbacksInvoker *_bubblingTargets{nullptr}; /** * @zh * 节点捕获事件监听器 */ CallbacksInvoker *_capturingTargets{nullptr}; // /** // * @zh // * 触摸监听器 // */ // event::EventListenerTouchOneByOne *_touchListener{nullptr}; // // /** // * @zh // * 鼠标监听器 // */ // event::EventListenerMouse *_mouseListener{nullptr}; Node *_node{nullptr}; // bool checknSetupSysEvent(const CallbacksInvoker::KeyType &type); template void onDispatch(const CallbacksInvoker::KeyType &type, std::function &&callback, CallbackInfoBase::ID &cbID, bool useCapture = false); template void onDispatch(const CallbacksInvoker::KeyType &type, void (Target::*memberFn)(Args...), Target *target, bool useCapture = false); template void onDispatch(const CallbacksInvoker::KeyType &type, std::function &&callback, Target *target, CallbackInfoBase::ID &cbID, bool useCapture = false); void offDispatch(const CallbacksInvoker::KeyType &type, CallbackInfoBase::ID cbID, bool useCapture = false); void offDispatch(const CallbacksInvoker::KeyType &type, void *target, bool useCapture = false); template void offDispatch(const CallbacksInvoker::KeyType &type, void (Target::*memberFn)(Args...), Target *target, bool useCapture = false); CC_DISALLOW_COPY_MOVE_ASSIGN(NodeEventProcessor); }; template void NodeEventProcessor::emit(const CallbacksInvoker::KeyType &type, Args &&...args) { if (_bubblingTargets != nullptr) { _bubblingTargets->emit(type, std::forward(args)...); } } template void NodeEventProcessor::onDispatch(const CallbacksInvoker::KeyType &type, std::function &&callback, CallbackInfoBase::ID &cbID, bool useCapture) { CallbacksInvoker *listeners = nullptr; if (useCapture) { if (_capturingTargets == nullptr) { _capturingTargets = ccnew CallbacksInvoker(); } listeners = _capturingTargets; } else { if (_bubblingTargets == nullptr) { _bubblingTargets = ccnew CallbacksInvoker(); } listeners = _bubblingTargets; } if (!listeners->hasEventListener(type)) { listeners->on(type, std::forward>(callback), cbID); } } template void NodeEventProcessor::onDispatch(const CallbacksInvoker::KeyType &type, std::function &&callback, Target *target, CallbackInfoBase::ID &cbID, bool useCapture) { CallbacksInvoker *listeners = nullptr; if (useCapture) { if (_capturingTargets == nullptr) { _capturingTargets = ccnew CallbacksInvoker(); } listeners = _capturingTargets; } else { if (_bubblingTargets == nullptr) { _bubblingTargets = ccnew CallbacksInvoker(); } listeners = _bubblingTargets; } if (!listeners->hasEventListener(type)) { listeners->on(type, std::forward>(callback), target, cbID); } } template void NodeEventProcessor::onDispatch(const CallbacksInvoker::KeyType &type, void (Target::*memberFn)(Args...), Target *target, bool useCapture) { CallbacksInvoker *listeners = nullptr; if (useCapture) { if (_capturingTargets == nullptr) { _capturingTargets = ccnew CallbacksInvoker(); } listeners = _capturingTargets; } else { if (_bubblingTargets == nullptr) { _bubblingTargets = ccnew CallbacksInvoker(); } listeners = _bubblingTargets; } if (!listeners->hasEventListener(type)) { listeners->on(type, memberFn, target); } } template void NodeEventProcessor::on(const CallbacksInvoker::KeyType &type, std::function &&callback, CallbackInfoBase::ID &cbID, bool /*useCapture*/) { // bool forDispatch = checknSetupSysEvent(type); // if (forDispatch) { // onDispatch(type, std::forward>(callback), cbID, useCapture); // } else { if (_bubblingTargets == nullptr) { _bubblingTargets = ccnew CallbacksInvoker(); } _bubblingTargets->on(type, std::forward>(callback), cbID); } } template void NodeEventProcessor::on(const CallbacksInvoker::KeyType &type, std::function &&callback, Target * /*target*/, CallbackInfoBase::ID &cbID, bool /*useCapture*/) { // bool forDispatch = checknSetupSysEvent(type); // if (forDispatch) { // onDispatch(type, std::forward>(callback), target, cbID, useCapture); // } else { if (_bubblingTargets == nullptr) { _bubblingTargets = ccnew CallbacksInvoker(); } _bubblingTargets->on(type, std::forward>(callback), cbID); } } template std::enable_if_t::value, void> NodeEventProcessor::on(const CallbacksInvoker::KeyType &type, LambdaType &&callback, Target *target, CallbackInfoBase::ID &cbID, bool /*useCapture*/) { // bool forDispatch = checknSetupSysEvent(type); // if (forDispatch) { // onDispatch(type, CallbacksInvoker::toFunction(std::forward(callback)), target, cbID, useCapture); // } else { if (_bubblingTargets == nullptr) { _bubblingTargets = ccnew CallbacksInvoker(); } _bubblingTargets->on(type, callback, target, cbID); } } template std::enable_if_t::value, void> NodeEventProcessor::on(const CallbacksInvoker::KeyType &type, LambdaType &&callback, CallbackInfoBase::ID &cbID, bool /*useCapture*/) { // bool forDispatch = checknSetupSysEvent(type); // if (forDispatch) { // onDispatch(type, CallbacksInvoker::toFunction(std::forward(callback)), cbID, useCapture); // } else { if (_bubblingTargets == nullptr) { _bubblingTargets = ccnew CallbacksInvoker(); } _bubblingTargets->on(type, callback, cbID); } } template void NodeEventProcessor::on(const CallbacksInvoker::KeyType &type, void (Target::*memberFn)(Args...), Target *target, bool /*useCapture*/) { using CallbackInfoType = CallbackInfo; // bool forDispatch = checknSetupSysEvent(type); // if (forDispatch) { // onDispatch(type, memberFn, target, useCapture); // } else { if (_bubblingTargets == nullptr) { _bubblingTargets = ccnew CallbacksInvoker(); } _bubblingTargets->on(type, memberFn, target); } } template void NodeEventProcessor::once(const CallbacksInvoker::KeyType &type, std::function &&callback, CallbackInfoBase::ID &cbID, bool useCapture /* = false*/) { once(type, std::forward(callback), nullptr, cbID, useCapture); } template void NodeEventProcessor::once(const CallbacksInvoker::KeyType &type, void (Target::*memberFn)(Args...), Target *target, bool useCapture) { CallbacksInvoker *listeners = nullptr; if (useCapture) { if (_capturingTargets == nullptr) { _capturingTargets = ccnew CallbacksInvoker(); } listeners = _capturingTargets; } else { if (_bubblingTargets == nullptr) { _bubblingTargets = ccnew CallbacksInvoker(); } listeners = _bubblingTargets; } listeners->on(type, memberFn, target, true); listeners->on( type, [=](Args... /*unused*/) { off(type, memberFn, target); }, true); } template void NodeEventProcessor::once(const CallbacksInvoker::KeyType &type, std::function &&callback, Target *target, CallbackInfoBase::ID &cbID, bool useCapture) { CallbacksInvoker *listeners = nullptr; if (useCapture) { if (_capturingTargets == nullptr) { _capturingTargets = ccnew CallbacksInvoker(); } listeners = _capturingTargets; } else { if (_bubblingTargets == nullptr) { _bubblingTargets = ccnew CallbacksInvoker(); } listeners = _bubblingTargets; } listeners->on(type, std::forward>(callback), target, cbID, true); CallbackInfoBase::ID cacheID = cbID; listeners->on( type, [=](Args... /*unused*/) { off(type, cacheID); }, target, true); } template std::enable_if_t::value, void> NodeEventProcessor::once(const CallbacksInvoker::KeyType &type, LambdaType &&callback, Target *target, CallbackInfoBase::ID &cbID, bool useCapture) { once(type, CallbacksInvoker::toFunction(callback), target, cbID, useCapture); } template std::enable_if_t::value, void> NodeEventProcessor::once(const CallbacksInvoker::KeyType &type, LambdaType &&callback, CallbackInfoBase::ID &cbID, bool useCapture) { once(type, std::forward(callback), nullptr, cbID, useCapture); } template void NodeEventProcessor::off(const CallbacksInvoker::KeyType &type, void (Target::*memberFn)(Args...), Target *target, bool /*useCapture*/) { // bool touchEventExist = std::find(TOUCH_EVENTS.begin(), TOUCH_EVENTS.end(), type) != TOUCH_EVENTS.end(); // bool mouseEventExist = std::find(MOUSE_EVENTS.begin(), MOUSE_EVENTS.end(), type) != MOUSE_EVENTS.end(); // if (touchEventExist || mouseEventExist) { // offDispatch(type, memberFn, target, useCapture); // // if (touchEventExist) { // if (_touchListener && !checkListeners(_node, TOUCH_EVENTS)) { // event::EventManager::getInstance()->removeEventListener(_touchListener); // _touchListener = nullptr; // } // } else if (mouseEventExist) { // if (_mouseListener && !checkListeners(_node, MOUSE_EVENTS)) { // event::EventManager::getInstance()->removeEventListener(_mouseListener); // _mouseListener = nullptr; // } // } // } else if (_bubblingTargets != nullptr) { _bubblingTargets->off(type, memberFn, target); } } template void NodeEventProcessor::offDispatch(const CallbacksInvoker::KeyType &type, void (Target::*memberFn)(Args...), Target *target, bool useCapture) { if (memberFn == nullptr) { if (_capturingTargets != nullptr) { _capturingTargets->offAll(type, target); } if (_bubblingTargets != nullptr) { _bubblingTargets->offAll(type, target); } } else { CallbacksInvoker *listeners = useCapture ? _capturingTargets : _bubblingTargets; if (listeners != nullptr) { listeners->off(type, memberFn, target); } } } template bool NodeEventProcessor::hasEventListener(const CallbacksInvoker::KeyType &type, void (Target::*memberFn)(Args...), Target *target) const { bool has = false; if (_bubblingTargets) { has = _bubblingTargets->hasEventListener(type, memberFn, target); } if (!has && _capturingTargets) { has = _capturingTargets->hasEventListener(type, memberFn, target); } return has; } } // namespace cc