diff --git a/cocos2dx/event_dispatcher/CCAccelerationEventListener.cpp b/cocos2dx/event_dispatcher/CCAccelerationEventListener.cpp index 992733d2ea..8b2d9dbf9c 100644 --- a/cocos2dx/event_dispatcher/CCAccelerationEventListener.cpp +++ b/cocos2dx/event_dispatcher/CCAccelerationEventListener.cpp @@ -28,12 +28,8 @@ NS_CC_BEGIN AccelerationEventListener::AccelerationEventListener() -: EventListener(AccelerationEvent::EVENT_TYPE, nullptr) { - onEvent = [this](Event* event){ - auto accEvent = static_cast(event); - this->onAccelerationEvent(&accEvent->acc, event); - }; + } AccelerationEventListener::~AccelerationEventListener() @@ -41,11 +37,58 @@ AccelerationEventListener::~AccelerationEventListener() CCLOGINFO("In the destructor of AccelerationEventListener. %p", this); } -std::shared_ptr AccelerationEventListener::create(std::function callback) +AccelerationEventListener* AccelerationEventListener::create(std::function callback) { - std::shared_ptr ret(new AccelerationEventListener()); - ret->onAccelerationEvent = callback; + AccelerationEventListener* ret = new AccelerationEventListener(); + if (ret && ret->init(callback)) + { + ret->autorelease(); + } + else + { + CC_SAFE_DELETE(ret); + } + return ret; } +bool AccelerationEventListener::init(std::function callback) +{ + auto listener = [this](Event* event){ + auto accEvent = static_cast(event); + this->onAccelerationEvent(&accEvent->acc, event); + }; + + if (EventListener::init(AccelerationEvent::EVENT_TYPE, listener)) + { + onAccelerationEvent = callback; + return true; + } + + return false; +} + +AccelerationEventListener* AccelerationEventListener::clone() +{ + auto ret = new AccelerationEventListener(); + + if (ret && ret->init(onAccelerationEvent)) + { + ret->autorelease(); + } + else + { + CC_SAFE_DELETE(ret); + } + + return ret; +} + +bool AccelerationEventListener::checkAvaiable() +{ + CCASSERT(onAccelerationEvent, ""); + + return true; +} + NS_CC_END diff --git a/cocos2dx/event_dispatcher/CCAccelerationEventListener.h b/cocos2dx/event_dispatcher/CCAccelerationEventListener.h index 190a986bf7..b107cbfc76 100644 --- a/cocos2dx/event_dispatcher/CCAccelerationEventListener.h +++ b/cocos2dx/event_dispatcher/CCAccelerationEventListener.h @@ -33,11 +33,15 @@ NS_CC_BEGIN class AccelerationEventListener : public EventListener { public: - static std::shared_ptr create(std::function callback); - + static AccelerationEventListener* create(std::function callback); + virtual AccelerationEventListener* clone() override; + ~AccelerationEventListener(); private: AccelerationEventListener(); + virtual bool checkAvaiable() override; + + bool init(std::function callback); std::function onAccelerationEvent; }; diff --git a/cocos2dx/event_dispatcher/CCEventDispatcher.cpp b/cocos2dx/event_dispatcher/CCEventDispatcher.cpp index ef976059fb..3812a03880 100644 --- a/cocos2dx/event_dispatcher/CCEventDispatcher.cpp +++ b/cocos2dx/event_dispatcher/CCEventDispatcher.cpp @@ -94,9 +94,9 @@ void EventDispatcher::registerEventListenerWithItem(EventListenerItem* item) listenerList->push_front(item); } -int EventDispatcher::registerEventListenerWithSceneGraphPriority(std::shared_ptr listener, Node* node) +int EventDispatcher::registerEventListenerWithSceneGraphPriority(EventListener* listener, Node* node) { - CCASSERT(listener->_isRegistered, "The listener has been registered."); + CCASSERT(!listener->_isRegistered, "The listener has been registered."); if (!listener->checkAvaiable()) return 0; @@ -106,6 +106,7 @@ int EventDispatcher::registerEventListenerWithSceneGraphPriority(std::shared_ptr item->node = node; item->fixedPriority = 0; item->listener = listener; + item->listener->retain(); item->listener->_isRegistered = true; registerEventListenerWithItem(item); @@ -116,9 +117,9 @@ int EventDispatcher::registerEventListenerWithSceneGraphPriority(std::shared_ptr return item->id; } -int EventDispatcher::registerEventListenerWithFixedPriority(std::shared_ptr listener, int fixedPriority) +int EventDispatcher::registerEventListenerWithFixedPriority(EventListener* listener, int fixedPriority) { - CCASSERT(listener->_isRegistered, "The listener has been registered."); + CCASSERT(!listener->_isRegistered, "The listener has been registered."); if (!listener->checkAvaiable()) return 0; @@ -128,6 +129,7 @@ int EventDispatcher::registerEventListenerWithFixedPriority(std::shared_ptrnode = nullptr; item->fixedPriority = fixedPriority; item->listener = listener; + item->listener->retain(); item->listener->_isRegistered = true; registerEventListenerWithItem(item); @@ -150,6 +152,7 @@ void EventDispatcher::unregisterEventListener(int listenerId) if (_inDispatch == 0) { + (*itemIter)->listener->release(); delete (*itemIter); iter->second->remove(*itemIter); } @@ -268,7 +271,7 @@ void EventDispatcher::dispatchTouchEvent(TouchEvent* event) for (auto iter = touchListeners->begin(); iter != touchListeners->end(); ++iter) { - auto touchEventListener = std::static_pointer_cast((*iter)->listener); + TouchEventListener* touchEventListener = static_cast((*iter)->listener); if (touchEventListener->_dispatchMode == Touch::DispatchMode::ONE_BY_ONE) { @@ -309,12 +312,11 @@ void EventDispatcher::dispatchTouchEvent(TouchEvent* event) continue; event->setCurrentTarget((*oneByOneIter)->node); - CCLOG("touch target : %p, %s", (*oneByOneIter)->node, typeid(*(*oneByOneIter)->node).name()); bool isClaimed = false; std::vector::iterator removedIter; - auto touchEventListener = std::static_pointer_cast((*oneByOneIter)->listener); + auto touchEventListener = static_cast((*oneByOneIter)->listener); TouchEvent::EventCode eventCode = event->getEventCode(); if (eventCode == TouchEvent::EventCode::BEGAN) @@ -399,7 +401,7 @@ void EventDispatcher::dispatchTouchEvent(TouchEvent* event) event->setCurrentTarget((*allInOneIter)->node); - auto touchEventListener = std::static_pointer_cast((*allInOneIter)->listener); + auto touchEventListener = static_cast((*allInOneIter)->listener); switch (event->getEventCode()) { @@ -541,6 +543,7 @@ void EventDispatcher::removeListenersForEventType(const std::string& eventType) { for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end(); ++iter) { + (*iter)->listener->release(); delete (*iter); } @@ -561,6 +564,7 @@ void EventDispatcher::removeAllListeners() { for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end(); ++iter) { + (*iter)->listener->release(); delete (*iter); } diff --git a/cocos2dx/event_dispatcher/CCEventDispatcher.h b/cocos2dx/event_dispatcher/CCEventDispatcher.h index 32d41873f0..08be317ce6 100644 --- a/cocos2dx/event_dispatcher/CCEventDispatcher.h +++ b/cocos2dx/event_dispatcher/CCEventDispatcher.h @@ -57,12 +57,12 @@ public: /** Registers a callback function for an specified event with the priority of scene graph. * @return The unique ID for the listener. */ - int registerEventListenerWithSceneGraphPriority(std::shared_ptr listener, Node* node); + int registerEventListenerWithSceneGraphPriority(EventListener* listener, Node* node); /** Registers a callback function for an specified event with the fixed priority. * @return The unique ID for the listener. */ - int registerEventListenerWithFixedPriority(std::shared_ptr listener, int fixedPriority); + int registerEventListenerWithFixedPriority(EventListener* listener, int fixedPriority); /** Unregisters a callback function by the unique ID. */ void unregisterEventListener(int listenerId); @@ -98,10 +98,10 @@ public: private: struct EventListenerItem { - int id; - int fixedPriority; // The higher the number, the higher the priority - Node* node; // Weak reference. - std::shared_ptr listener; + int id; + int fixedPriority; // The higher the number, the higher the priority + Node* node; // Weak reference. + EventListener* listener; }; /** Constructor of EventDispatcher */ diff --git a/cocos2dx/event_dispatcher/CCEventListener.cpp b/cocos2dx/event_dispatcher/CCEventListener.cpp index 26e91fcc2c..15e54c462b 100644 --- a/cocos2dx/event_dispatcher/CCEventListener.cpp +++ b/cocos2dx/event_dispatcher/CCEventListener.cpp @@ -27,16 +27,21 @@ NS_CC_BEGIN -std::shared_ptr EventListener::create(const std::string& eventType, std::function callback) +EventListener* EventListener::create(const std::string& eventType, std::function callback) { - std::shared_ptr ret(new EventListener(eventType, callback)); + EventListener* ret = new EventListener(); + if (ret && ret->init(eventType, callback)) + { + ret->autorelease(); + } + else + { + CC_SAFE_DELETE(ret); + } return ret; } -EventListener::EventListener(const std::string& t, std::functioncallback) -: onEvent(callback) -, type(t) -, _isRegistered(false) +EventListener::EventListener() {} EventListener::~EventListener() @@ -44,14 +49,31 @@ EventListener::~EventListener() CCLOGINFO("In the destructor of EventListener. %p", this); } +bool EventListener::init(const std::string& t, std::functioncallback) +{ + onEvent = callback; + type = t; + _isRegistered = false; + + return true; +} + bool EventListener::checkAvaiable() { return (onEvent != nullptr); } -std::shared_ptr EventListener::clone() +EventListener* EventListener::clone() { - std::shared_ptr ret(new EventListener(type, onEvent)); + EventListener* ret = new EventListener(); + if (ret && ret->init(type, onEvent)) + { + ret->autorelease(); + } + else + { + CC_SAFE_DELETE(ret); + } return ret; } diff --git a/cocos2dx/event_dispatcher/CCEventListener.h b/cocos2dx/event_dispatcher/CCEventListener.h index fd415be847..8971276d0f 100644 --- a/cocos2dx/event_dispatcher/CCEventListener.h +++ b/cocos2dx/event_dispatcher/CCEventListener.h @@ -26,6 +26,7 @@ #define cocos2d_libs_EventListener_h #include "platform/CCPlatformMacros.h" +#include "cocoa/CCObject.h" #include #include @@ -35,18 +36,18 @@ NS_CC_BEGIN class Event; -class EventListener +class EventListener : public Object { public: - static std::shared_ptr create(const std::string& eventType, std::function callback); + static EventListener* create(const std::string& eventType, std::function callback); protected: - EventListener(const std::string& t, std::functioncallback); - + EventListener(); + bool init(const std::string& t, std::functioncallback); public: virtual ~EventListener(); virtual bool checkAvaiable(); - virtual std::shared_ptr clone(); + virtual EventListener* clone(); protected: std::function onEvent; std::string type; diff --git a/cocos2dx/event_dispatcher/CCKeyboardEventListener.cpp b/cocos2dx/event_dispatcher/CCKeyboardEventListener.cpp index f4157000d6..4d997d0287 100644 --- a/cocos2dx/event_dispatcher/CCKeyboardEventListener.cpp +++ b/cocos2dx/event_dispatcher/CCKeyboardEventListener.cpp @@ -36,28 +36,64 @@ bool KeyboardEventListener::checkAvaiable() return true; } -std::shared_ptr KeyboardEventListener::create() +KeyboardEventListener* KeyboardEventListener::create() { - std::shared_ptr ret(new KeyboardEventListener()); + auto ret = new KeyboardEventListener(); + if (ret && ret->init()) + { + ret->autorelease(); + } + else + { + CC_SAFE_DELETE(ret); + } + return ret; +} + +KeyboardEventListener* KeyboardEventListener::clone() +{ + auto ret = new KeyboardEventListener(); + if (ret && ret->init()) + { + ret->autorelease(); + ret->onKeyPressed = onKeyPressed; + ret->onKeyReleased = onKeyReleased; + } + else + { + CC_SAFE_DELETE(ret); + } return ret; } KeyboardEventListener::KeyboardEventListener() -: EventListener(KeyboardEvent::EVENT_TYPE, nullptr) -, onKeyPressed(nullptr) +: onKeyPressed(nullptr) , onKeyReleased(nullptr) { - onEvent = [this](Event* event){ +} + +bool KeyboardEventListener::init() +{ + auto listener = [this](Event* event){ auto keyboardEvent = static_cast(event); if (keyboardEvent->_isPressed) { - onKeyPressed(keyboardEvent->_keyCode, event); + if (onKeyPressed != nullptr) + onKeyPressed(keyboardEvent->_keyCode, event); } else { - onKeyReleased(keyboardEvent->_keyCode, event); + if (onKeyReleased != nullptr) + onKeyReleased(keyboardEvent->_keyCode, event); } }; + + if (EventListener::init(KeyboardEvent::EVENT_TYPE, listener)) + { + return true; + } + + return false; } NS_CC_END diff --git a/cocos2dx/event_dispatcher/CCKeyboardEventListener.h b/cocos2dx/event_dispatcher/CCKeyboardEventListener.h index e2bdd7a625..64467a1c31 100644 --- a/cocos2dx/event_dispatcher/CCKeyboardEventListener.h +++ b/cocos2dx/event_dispatcher/CCKeyboardEventListener.h @@ -36,12 +36,15 @@ class Event; class KeyboardEventListener : public EventListener { public: - static std::shared_ptr create(); + static KeyboardEventListener* create(); + virtual KeyboardEventListener* clone() override; std::function onKeyPressed; std::function onKeyReleased; private: KeyboardEventListener(); + bool init(); + virtual bool checkAvaiable() override; }; diff --git a/cocos2dx/event_dispatcher/CCTouchEventListener.cpp b/cocos2dx/event_dispatcher/CCTouchEventListener.cpp index 222c26123c..c7d21b0f6e 100644 --- a/cocos2dx/event_dispatcher/CCTouchEventListener.cpp +++ b/cocos2dx/event_dispatcher/CCTouchEventListener.cpp @@ -30,9 +30,8 @@ NS_CC_BEGIN -TouchEventListener::TouchEventListener(Touch::DispatchMode mode) -: EventListener(TouchEvent::EVENT_TYPE, nullptr) -, onTouchBegan(nullptr) +TouchEventListener::TouchEventListener() +: onTouchBegan(nullptr) , onTouchMoved(nullptr) , onTouchEnded(nullptr) , onTouchCancelled(nullptr) @@ -41,19 +40,43 @@ TouchEventListener::TouchEventListener(Touch::DispatchMode mode) , onTouchesEnded(nullptr) , onTouchesCancelled(nullptr) , _needSwallow(false) -, _dispatchMode(mode) +, _dispatchMode(Touch::DispatchMode::ALL_AT_ONCE) { } +TouchEventListener::~TouchEventListener() +{ + CCLOGINFO("In the destructor of TouchEventListener, %p", this); +} + +bool TouchEventListener::init(Touch::DispatchMode mode) +{ + if (EventListener::init(TouchEvent::EVENT_TYPE, nullptr)) + { + _dispatchMode = mode; + return true; + } + + return false; +} + void TouchEventListener::setSwallowTouches(bool needSwallow) { CCASSERT(_dispatchMode == Touch::DispatchMode::ONE_BY_ONE, "Swallow touches only available in OneByOne mode."); _needSwallow = needSwallow; } -std::shared_ptr TouchEventListener::create(Touch::DispatchMode mode) +TouchEventListener* TouchEventListener::create(Touch::DispatchMode mode) { - std::shared_ptr ret(new TouchEventListener(mode)); + auto ret = new TouchEventListener(); + if (ret && ret->init(mode)) + { + ret->autorelease(); + } + else + { + CC_SAFE_DELETE(ret); + } return ret; } @@ -85,23 +108,30 @@ bool TouchEventListener::checkAvaiable() return true; } -std::shared_ptr TouchEventListener::clone() +TouchEventListener* TouchEventListener::clone() { - std::shared_ptr ret(new TouchEventListener(_dispatchMode)); - - ret->onTouchBegan = onTouchBegan; - ret->onTouchMoved = onTouchMoved; - ret->onTouchEnded = onTouchEnded; - ret->onTouchCancelled = onTouchCancelled; - ret->onTouchesBegan = onTouchesBegan; - ret->onTouchesMoved = onTouchesMoved; - ret->onTouchesEnded = onTouchesEnded; - ret->onTouchesCancelled = onTouchesCancelled; - - ret->_claimedTouches = _claimedTouches; - ret->_dispatchMode = _dispatchMode; - ret->_needSwallow = _needSwallow; - + auto ret = new TouchEventListener(); + if (ret && ret->init(_dispatchMode)) + { + ret->autorelease(); + + ret->onTouchBegan = onTouchBegan; + ret->onTouchMoved = onTouchMoved; + ret->onTouchEnded = onTouchEnded; + ret->onTouchCancelled = onTouchCancelled; + ret->onTouchesBegan = onTouchesBegan; + ret->onTouchesMoved = onTouchesMoved; + ret->onTouchesEnded = onTouchesEnded; + ret->onTouchesCancelled = onTouchesCancelled; + + ret->_claimedTouches = _claimedTouches; + ret->_dispatchMode = _dispatchMode; + ret->_needSwallow = _needSwallow; + } + else + { + CC_SAFE_DELETE(ret); + } return ret; } diff --git a/cocos2dx/event_dispatcher/CCTouchEventListener.h b/cocos2dx/event_dispatcher/CCTouchEventListener.h index ad6efb002b..2d16f9aa95 100644 --- a/cocos2dx/event_dispatcher/CCTouchEventListener.h +++ b/cocos2dx/event_dispatcher/CCTouchEventListener.h @@ -36,11 +36,14 @@ NS_CC_BEGIN class TouchEventListener : public EventListener { public: - static std::shared_ptr create(Touch::DispatchMode mode); - virtual std::shared_ptr clone() override; + static TouchEventListener* create(Touch::DispatchMode mode); + virtual TouchEventListener* clone() override; private: - TouchEventListener(Touch::DispatchMode mode); + TouchEventListener(); + bool init(Touch::DispatchMode mode); + + virtual ~TouchEventListener(); virtual bool checkAvaiable() override; public: