From 779e539658687b7ab81a70802a8c65bc6227633e Mon Sep 17 00:00:00 2001 From: James Chen Date: Sat, 12 Oct 2013 11:25:28 +0800 Subject: [PATCH 01/42] Remove event controller codes from Layer class. HelloCpp works. --- cocos/2d/CCEventDispatcher.cpp | 51 +--- cocos/2d/CCEventListener.cpp | 2 +- cocos/2d/CCEventListener.h | 2 +- cocos/2d/CCEventListenerAcceleration.cpp | 2 +- cocos/2d/CCEventListenerAcceleration.h | 2 +- cocos/2d/CCEventListenerCustom.cpp | 4 +- cocos/2d/CCEventListenerCustom.h | 2 +- cocos/2d/CCEventListenerKeyboard.cpp | 2 +- cocos/2d/CCEventListenerKeyboard.h | 2 +- cocos/2d/CCEventListenerTouch.cpp | 131 +++++--- cocos/2d/CCEventListenerTouch.h | 52 +++- cocos/2d/CCEventTouch.cpp | 3 + cocos/2d/CCEventTouch.h | 5 + cocos/2d/CCLayer.cpp | 365 ----------------------- cocos/2d/CCLayer.h | 76 ----- cocos/2d/CCMenu.cpp | 14 +- cocos/2d/CCMenu.h | 8 +- 17 files changed, 166 insertions(+), 557 deletions(-) diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index 14be190f68..0e596b788a 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -125,7 +125,7 @@ void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* list CCASSERT(listener && node, "Invalid parameters."); CCASSERT(!listener->_isRegistered, "The listener has been registered."); - if (!listener->checkAvaiable()) + if (!listener->checkAvailable()) return; auto item = new EventListenerItem(); @@ -147,7 +147,7 @@ void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, CCASSERT(!listener->_isRegistered, "The listener has been registered."); CCASSERT(fixedPriority != 0, "0 priority is forbidden for fixed priority since it's used for scene graph based priority."); - if (!listener->checkAvaiable()) + if (!listener->checkAvailable()) return; auto item = new EventListenerItem(); @@ -294,37 +294,14 @@ void EventDispatcher::dispatchEvent(Event* event, bool forceSortListeners) void EventDispatcher::dispatchTouchEvent(EventTouch* event) { - auto touchListeners = getListenerItemsForType(EventTouch::EVENT_TYPE); - if (touchListeners == nullptr) + auto oneByOnelisteners = getListenerItemsForType(EventTouch::MODE_ONE_BY_ONE); + auto allAtOncelisteners = getListenerItemsForType(EventTouch::MODE_ALL_AT_ONCE); + + // If there aren't any touch listeners, return directly. + if (nullptr == oneByOnelisteners && nullptr == allAtOncelisteners) return; - std::vector oneByOnelisteners; - oneByOnelisteners.reserve(touchListeners->size()); - - std::vector allInOnelisteners; - allInOnelisteners.reserve(touchListeners->size()); - - EventListenerTouch* touchEventListener = nullptr; - - std::for_each(touchListeners->begin(), touchListeners->end(), [&](EventListenerItem*& item){ - - touchEventListener = static_cast(item->listener); - - if (touchEventListener->_dispatchMode == Touch::DispatchMode::ONE_BY_ONE) - { - oneByOnelisteners.push_back(item); - } - else if (touchEventListener->_dispatchMode == Touch::DispatchMode::ALL_AT_ONCE) - { - allInOnelisteners.push_back(item); - } - else - { - CCASSERT(false, "Not supported touch listener type."); - } - }); - - bool isNeedsMutableSet = (oneByOnelisteners.size() > 0 && allInOnelisteners.size() > 0); + bool isNeedsMutableSet = (oneByOnelisteners && allAtOncelisteners); std::vector orignalTouches = event->getTouches(); std::vector mutableTouches(orignalTouches.size()); @@ -333,7 +310,7 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) // // process the target handlers 1st // - if (oneByOnelisteners.size() > 0) + if (oneByOnelisteners) { auto mutableTouchesIter = mutableTouches.begin(); auto touchesIter = orignalTouches.begin(); @@ -342,7 +319,7 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) { bool isSwallowed = false; - for (auto& item : oneByOnelisteners) + for (auto& item : *oneByOnelisteners) { // Skip if the listener was removed. if (item->listener == nullptr) @@ -353,7 +330,7 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) bool isClaimed = false; std::vector::iterator removedIter; - auto touchEventListener = static_cast(item->listener); + auto touchEventListener = static_cast(item->listener); EventTouch::EventCode eventCode = event->getEventCode(); if (eventCode == EventTouch::EventCode::BEGAN) @@ -434,9 +411,9 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) // // process standard handlers 2nd // - if (allInOnelisteners.size() > 0 && mutableTouches.size() > 0) + if (allAtOncelisteners && mutableTouches.size() > 0) { - for (auto& item : allInOnelisteners) + for (auto& item : *allAtOncelisteners) { // Skip if the listener was removed. if (item->listener == nullptr) @@ -444,7 +421,7 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) event->setCurrentTarget(item->node); - auto touchEventListener = static_cast(item->listener); + auto touchEventListener = static_cast(item->listener); switch (event->getEventCode()) { diff --git a/cocos/2d/CCEventListener.cpp b/cocos/2d/CCEventListener.cpp index ce3c787883..451be9a8c8 100644 --- a/cocos/2d/CCEventListener.cpp +++ b/cocos/2d/CCEventListener.cpp @@ -44,7 +44,7 @@ bool EventListener::init(const std::string& t, std::function callb return true; } -bool EventListener::checkAvaiable() +bool EventListener::checkAvailable() { return (_onEvent != nullptr); } diff --git a/cocos/2d/CCEventListener.h b/cocos/2d/CCEventListener.h index 82c47445e0..559cf21739 100644 --- a/cocos/2d/CCEventListener.h +++ b/cocos/2d/CCEventListener.h @@ -54,7 +54,7 @@ public: virtual ~EventListener(); /** Checks whether the listener is available. */ - virtual bool checkAvaiable() = 0; + virtual bool checkAvailable() = 0; /** Clones the listener, its subclasses have to override this method. */ virtual EventListener* clone() = 0; diff --git a/cocos/2d/CCEventListenerAcceleration.cpp b/cocos/2d/CCEventListenerAcceleration.cpp index e7e1f6055b..d8b01fc4ce 100644 --- a/cocos/2d/CCEventListenerAcceleration.cpp +++ b/cocos/2d/CCEventListenerAcceleration.cpp @@ -84,7 +84,7 @@ EventListenerAcceleration* EventListenerAcceleration::clone() return ret; } -bool EventListenerAcceleration::checkAvaiable() +bool EventListenerAcceleration::checkAvailable() { CCASSERT(onAccelerationEvent, ""); diff --git a/cocos/2d/CCEventListenerAcceleration.h b/cocos/2d/CCEventListenerAcceleration.h index 1fb9536be7..2905df9486 100644 --- a/cocos/2d/CCEventListenerAcceleration.h +++ b/cocos/2d/CCEventListenerAcceleration.h @@ -38,7 +38,7 @@ public: /// Overrides virtual EventListenerAcceleration* clone() override; - virtual bool checkAvaiable() override; + virtual bool checkAvailable() override; private: EventListenerAcceleration(); diff --git a/cocos/2d/CCEventListenerCustom.cpp b/cocos/2d/CCEventListenerCustom.cpp index 7be79b9dcb..abe4153198 100644 --- a/cocos/2d/CCEventListenerCustom.cpp +++ b/cocos/2d/CCEventListenerCustom.cpp @@ -80,10 +80,10 @@ EventListenerCustom* EventListenerCustom::clone() return ret; } -bool EventListenerCustom::checkAvaiable() +bool EventListenerCustom::checkAvailable() { bool ret = false; - if (EventListener::checkAvaiable() && _onCustomEvent != nullptr) + if (EventListener::checkAvailable() && _onCustomEvent != nullptr) { ret = true; } diff --git a/cocos/2d/CCEventListenerCustom.h b/cocos/2d/CCEventListenerCustom.h index 190ed63f95..a823d4a4ba 100644 --- a/cocos/2d/CCEventListenerCustom.h +++ b/cocos/2d/CCEventListenerCustom.h @@ -59,7 +59,7 @@ public: static EventListenerCustom* create(const std::string& eventName, std::function callback); /// Overrides - virtual bool checkAvaiable() override; + virtual bool checkAvailable() override; virtual EventListenerCustom* clone() override; protected: diff --git a/cocos/2d/CCEventListenerKeyboard.cpp b/cocos/2d/CCEventListenerKeyboard.cpp index 80709f8cef..a6e3b617ae 100644 --- a/cocos/2d/CCEventListenerKeyboard.cpp +++ b/cocos/2d/CCEventListenerKeyboard.cpp @@ -29,7 +29,7 @@ NS_CC_BEGIN -bool EventListenerKeyboard::checkAvaiable() +bool EventListenerKeyboard::checkAvailable() { CCASSERT(onKeyPressed && onKeyReleased, ""); diff --git a/cocos/2d/CCEventListenerKeyboard.h b/cocos/2d/CCEventListenerKeyboard.h index f7b4c288d9..dd223da533 100644 --- a/cocos/2d/CCEventListenerKeyboard.h +++ b/cocos/2d/CCEventListenerKeyboard.h @@ -40,7 +40,7 @@ public: /// Overrides virtual EventListenerKeyboard* clone() override; - virtual bool checkAvaiable() override; + virtual bool checkAvailable() override; std::function onKeyPressed; std::function onKeyReleased; diff --git a/cocos/2d/CCEventListenerTouch.cpp b/cocos/2d/CCEventListenerTouch.cpp index ec418c2949..49ec1462c9 100644 --- a/cocos/2d/CCEventListenerTouch.cpp +++ b/cocos/2d/CCEventListenerTouch.cpp @@ -30,46 +30,39 @@ NS_CC_BEGIN -EventListenerTouch::EventListenerTouch() +EventListenerTouchOneByOne::EventListenerTouchOneByOne() : onTouchBegan(nullptr) , onTouchMoved(nullptr) , onTouchEnded(nullptr) , onTouchCancelled(nullptr) -, onTouchesBegan(nullptr) -, onTouchesMoved(nullptr) -, onTouchesEnded(nullptr) -, onTouchesCancelled(nullptr) , _needSwallow(false) -, _dispatchMode(Touch::DispatchMode::ALL_AT_ONCE) { } -EventListenerTouch::~EventListenerTouch() +EventListenerTouchOneByOne::~EventListenerTouchOneByOne() { - CCLOGINFO("In the destructor of TouchEventListener, %p", this); + CCLOGINFO("In the destructor of EventListenerTouchOneByOne, %p", this); } -bool EventListenerTouch::init(Touch::DispatchMode mode) +bool EventListenerTouchOneByOne::init() { - if (EventListener::init(EventTouch::EVENT_TYPE, nullptr)) + if (EventListener::init(EventTouch::MODE_ONE_BY_ONE, nullptr)) { - _dispatchMode = mode; return true; } return false; } -void EventListenerTouch::setSwallowTouches(bool needSwallow) +void EventListenerTouchOneByOne::setSwallowTouches(bool needSwallow) { - CCASSERT(_dispatchMode == Touch::DispatchMode::ONE_BY_ONE, "Swallow touches only available in OneByOne mode."); _needSwallow = needSwallow; } -EventListenerTouch* EventListenerTouch::create(Touch::DispatchMode mode) +EventListenerTouchOneByOne* EventListenerTouchOneByOne::create() { - auto ret = new EventListenerTouch(); - if (ret && ret->init(mode)) + auto ret = new EventListenerTouchOneByOne(); + if (ret && ret->init()) { ret->autorelease(); } @@ -80,38 +73,22 @@ EventListenerTouch* EventListenerTouch::create(Touch::DispatchMode mode) return ret; } -bool EventListenerTouch::checkAvaiable() +bool EventListenerTouchOneByOne::checkAvailable() { - if (_dispatchMode == Touch::DispatchMode::ALL_AT_ONCE) + if (onTouchBegan == nullptr && onTouchMoved == nullptr + && onTouchEnded == nullptr && onTouchCancelled == nullptr) { - if (onTouchesBegan == nullptr && onTouchesMoved == nullptr - && onTouchesEnded == nullptr && onTouchesCancelled == nullptr) - { - CCASSERT(false, "Invalid TouchEventListener."); - return false; - } - } - else if (_dispatchMode == Touch::DispatchMode::ONE_BY_ONE) - { - if (onTouchBegan == nullptr && onTouchMoved == nullptr - && onTouchEnded == nullptr && onTouchCancelled == nullptr) - { - CCASSERT(false, "Invalid TouchEventListener."); - return false; - } - } - else - { - CCASSERT(false, ""); + CCASSERT(false, "Invalid TouchEventListener."); + return false; } return true; } -EventListenerTouch* EventListenerTouch::clone() +EventListenerTouchOneByOne* EventListenerTouchOneByOne::clone() { - auto ret = new EventListenerTouch(); - if (ret && ret->init(_dispatchMode)) + auto ret = new EventListenerTouchOneByOne(); + if (ret && ret->init()) { ret->autorelease(); @@ -119,13 +96,8 @@ EventListenerTouch* EventListenerTouch::clone() 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 @@ -135,4 +107,73 @@ EventListenerTouch* EventListenerTouch::clone() return ret; } +///////// +EventListenerTouchAllAtOnce::EventListenerTouchAllAtOnce() +: onTouchesBegan(nullptr) +, onTouchesMoved(nullptr) +, onTouchesEnded(nullptr) +, onTouchesCancelled(nullptr) +{ +} + +EventListenerTouchAllAtOnce::~EventListenerTouchAllAtOnce() +{ + CCLOGINFO("In the destructor of EventListenerTouchAllAtOnce, %p", this); +} + +bool EventListenerTouchAllAtOnce::init() +{ + if (EventListener::init(EventTouch::MODE_ALL_AT_ONCE, nullptr)) + { + return true; + } + + return false; +} + +EventListenerTouchAllAtOnce* EventListenerTouchAllAtOnce::create() +{ + auto ret = new EventListenerTouchAllAtOnce(); + if (ret && ret->init()) + { + ret->autorelease(); + } + else + { + CC_SAFE_DELETE(ret); + } + return ret; +} + +bool EventListenerTouchAllAtOnce::checkAvailable() +{ + if (onTouchesBegan == nullptr && onTouchesMoved == nullptr + && onTouchesEnded == nullptr && onTouchesCancelled == nullptr) + { + CCASSERT(false, "Invalid TouchEventListener."); + return false; + } + + return true; +} + +EventListenerTouchAllAtOnce* EventListenerTouchAllAtOnce::clone() +{ + auto ret = new EventListenerTouchAllAtOnce(); + if (ret && ret->init()) + { + ret->autorelease(); + + ret->onTouchesBegan = onTouchesBegan; + ret->onTouchesMoved = onTouchesMoved; + ret->onTouchesEnded = onTouchesEnded; + ret->onTouchesCancelled = onTouchesCancelled; + } + else + { + CC_SAFE_DELETE(ret); + } + return ret; +} + NS_CC_END \ No newline at end of file diff --git a/cocos/2d/CCEventListenerTouch.h b/cocos/2d/CCEventListenerTouch.h index 3002676bda..d34de1b237 100644 --- a/cocos/2d/CCEventListenerTouch.h +++ b/cocos/2d/CCEventListenerTouch.h @@ -33,38 +33,58 @@ NS_CC_BEGIN -class EventListenerTouch : public EventListener + +class EventListenerTouchOneByOne : public EventListener { public: - static EventListenerTouch* create(Touch::DispatchMode mode); + static EventListenerTouchOneByOne* create(); + + virtual ~EventListenerTouchOneByOne(); + + void setSwallowTouches(bool needSwallow); /// Overrides - virtual EventListenerTouch* clone() override; - virtual bool checkAvaiable() override; - - virtual ~EventListenerTouch(); - -private: - EventListenerTouch(); - bool init(Touch::DispatchMode mode); - + virtual EventListenerTouchOneByOne* clone() override; + virtual bool checkAvailable() override; + // + public: std::function onTouchBegan; std::function onTouchMoved; std::function onTouchEnded; std::function onTouchCancelled; +private: + EventListenerTouchOneByOne(); + bool init(); + + std::vector _claimedTouches; + bool _needSwallow; + + friend class EventDispatcher; +}; + + +class EventListenerTouchAllAtOnce : public EventListener +{ +public: + static EventListenerTouchAllAtOnce* create(); + virtual ~EventListenerTouchAllAtOnce(); + + /// Overrides + virtual EventListenerTouchAllAtOnce* clone() override; + virtual bool checkAvailable() override; + // +public: std::function&, Event*)> onTouchesBegan; std::function&, Event*)> onTouchesMoved; std::function&, Event*)> onTouchesEnded; std::function&, Event*)> onTouchesCancelled; - void setSwallowTouches(bool needSwallow); - private: - std::vector _claimedTouches; - bool _needSwallow; - Touch::DispatchMode _dispatchMode; + EventListenerTouchAllAtOnce(); + bool init(); +private: friend class EventDispatcher; }; diff --git a/cocos/2d/CCEventTouch.cpp b/cocos/2d/CCEventTouch.cpp index dbb0963775..987d87d0e9 100644 --- a/cocos/2d/CCEventTouch.cpp +++ b/cocos/2d/CCEventTouch.cpp @@ -26,6 +26,9 @@ NS_CC_BEGIN +const char* EventTouch::MODE_ONE_BY_ONE = "TouchEventOneByOne"; +const char* EventTouch::MODE_ALL_AT_ONCE = "TouchEventAllAtOnce"; + const char* EventTouch::EVENT_TYPE = "TouchEvent"; NS_CC_END diff --git a/cocos/2d/CCEventTouch.h b/cocos/2d/CCEventTouch.h index 3d54cecea7..558bfe0593 100644 --- a/cocos/2d/CCEventTouch.h +++ b/cocos/2d/CCEventTouch.h @@ -36,7 +36,12 @@ NS_CC_BEGIN class EventTouch : public Event { public: + static const char* MODE_ONE_BY_ONE; + static const char* MODE_ALL_AT_ONCE; + static const char* EVENT_TYPE; + + static const int MAX_TOUCHES = 5; enum class EventCode diff --git a/cocos/2d/CCLayer.cpp b/cocos/2d/CCLayer.cpp index 19346ea844..717570e6fe 100644 --- a/cocos/2d/CCLayer.cpp +++ b/cocos/2d/CCLayer.cpp @@ -48,14 +48,6 @@ NS_CC_BEGIN // Layer Layer::Layer() -: _touchEnabled(false) -, _accelerometerEnabled(false) -, _keyboardEnabled(false) -, _touchMode(Touch::DispatchMode::ALL_AT_ONCE) -, _swallowsTouches(true) -, _touchListener(nullptr) -, _keyboardListener(nullptr) -, _accelerationListener(nullptr) { _ignoreAnchorPointForPosition = true; setAnchorPoint(Point(0.5f, 0.5f)); @@ -74,8 +66,6 @@ bool Layer::init() Director * pDirector; CC_BREAK_IF(!(pDirector = Director::getInstance())); this->setContentSize(pDirector->getWinSize()); - setTouchEnabled(false); - setAccelerometerEnabled(false); // success bRet = true; } while(0); @@ -97,44 +87,6 @@ Layer *Layer::create() } } -/// Touch and Accelerometer related - -void Layer::addTouchListener() -{ - if (_touchListener != nullptr) - return; - - auto dispatcher = EventDispatcher::getInstance(); - - if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE ) - { - // Register Touch Event - auto listener = EventListenerTouch::create(Touch::DispatchMode::ALL_AT_ONCE); - - listener->onTouchesBegan = CC_CALLBACK_2(Layer::onTouchesBegan, this); - listener->onTouchesMoved = CC_CALLBACK_2(Layer::onTouchesMoved, this); - listener->onTouchesEnded = CC_CALLBACK_2(Layer::onTouchesEnded, this); - listener->onTouchesCancelled = CC_CALLBACK_2(Layer::onTouchesCancelled, this); - - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); - _touchListener = listener; - } - else - { - // Register Touch Event - auto listener = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); - listener->setSwallowTouches(_swallowsTouches); - - listener->onTouchBegan = CC_CALLBACK_2(Layer::onTouchBegan, this); - listener->onTouchMoved = CC_CALLBACK_2(Layer::onTouchMoved, this); - listener->onTouchEnded = CC_CALLBACK_2(Layer::onTouchEnded, this); - listener->onTouchCancelled = CC_CALLBACK_2(Layer::onTouchCancelled, this); - - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); - _touchListener = listener; - } -} - int Layer::executeScriptTouchHandler(EventTouch::EventCode eventType, Touch* touch) { if (kScriptTypeNone != _scriptType) @@ -160,323 +112,6 @@ int Layer::executeScriptTouchesHandler(EventTouch::EventCode eventType, const st return 0; } -/// isTouchEnabled getter -bool Layer::isTouchEnabled() const -{ - return _touchEnabled; -} - -/// isTouchEnabled setter -void Layer::setTouchEnabled(bool enabled) -{ - if (_touchEnabled != enabled) - { - _touchEnabled = enabled; - if (_running) - { - if (enabled) - { - this->addTouchListener(); - } - else - { - EventDispatcher::getInstance()->removeEventListener(_touchListener); - _touchListener = nullptr; - } - } - } -} - -void Layer::setTouchMode(Touch::DispatchMode mode) -{ - if(_touchMode != mode) - { - _touchMode = mode; - - if( _touchEnabled) - { - setTouchEnabled(false); - setTouchEnabled(true); - } - } -} - -void Layer::setSwallowsTouches(bool swallowsTouches) -{ - if (_swallowsTouches != swallowsTouches) - { - _swallowsTouches = swallowsTouches; - - if( _touchEnabled) - { - setTouchEnabled(false); - setTouchEnabled(true); - } - } -} - -Touch::DispatchMode Layer::getTouchMode() const -{ - return _touchMode; -} - -bool Layer::isSwallowsTouches() const -{ - return _swallowsTouches; -} - - - -/// isAccelerometerEnabled getter -bool Layer::isAccelerometerEnabled() const -{ - return _accelerometerEnabled; -} -/// isAccelerometerEnabled setter -void Layer::setAccelerometerEnabled(bool enabled) -{ - if (enabled != _accelerometerEnabled) - { - _accelerometerEnabled = enabled; - - Device::setAccelerometerEnabled(enabled); - - if (_running) - { - auto dispatcher = EventDispatcher::getInstance(); - dispatcher->removeEventListener(_accelerationListener); - _accelerationListener = nullptr; - - if (enabled) - { - _accelerationListener = EventListenerAcceleration::create(CC_CALLBACK_2(Layer::onAcceleration, this)); - dispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this); - } - } - } -} - - -void Layer::setAccelerometerInterval(double interval) { - if (_accelerometerEnabled) - { - if (_running) - { - Device::setAccelerometerInterval(interval); - } - } -} - - -void Layer::onAcceleration(Acceleration* pAccelerationValue, Event* event) -{ - CC_UNUSED_PARAM(pAccelerationValue); - - if(kScriptTypeNone != _scriptType) - { - BasicScriptData data(this,(void*)pAccelerationValue); - ScriptEvent event(kAccelerometerEvent,&data); - ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); - } -} - -void Layer::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event) -{ - CC_UNUSED_PARAM(keyCode); - CC_UNUSED_PARAM(event); -} - -void Layer::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) -{ - CC_UNUSED_PARAM(event); - if(kScriptTypeNone != _scriptType) - { - KeypadScriptData data(keyCode, this); - ScriptEvent event(kKeypadEvent,&data); - ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); - } -} - -/// isKeyboardEnabled getter -bool Layer::isKeyboardEnabled() const -{ - return _keyboardEnabled; -} -/// isKeyboardEnabled setter -void Layer::setKeyboardEnabled(bool enabled) -{ - if (enabled != _keyboardEnabled) - { - _keyboardEnabled = enabled; - - auto dispatcher = EventDispatcher::getInstance(); - dispatcher->removeEventListener(_keyboardListener); - _keyboardListener = nullptr; - - if (enabled) - { - auto listener = EventListenerKeyboard::create(); - listener->onKeyPressed = CC_CALLBACK_2(Layer::onKeyPressed, this); - listener->onKeyReleased = CC_CALLBACK_2(Layer::onKeyReleased, this); - - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); - _keyboardListener = listener; - } - } -} - -/// Callbacks -void Layer::onEnter() -{ - // register 'parent' nodes first - // since events are propagated in reverse order - if (_touchEnabled) - { - this->addTouchListener(); - } - - // then iterate over all the children - Node::onEnter(); - - // add this layer to concern the Accelerometer Sensor - if (_accelerometerEnabled) - { - auto dispatcher = EventDispatcher::getInstance(); - dispatcher->removeEventListener(_accelerationListener); - _accelerationListener = EventListenerAcceleration::create(CC_CALLBACK_2(Layer::onAcceleration, this)); - dispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this); - } -} - -void Layer::onExit() -{ - auto dispatcher = EventDispatcher::getInstance(); - - dispatcher->removeEventListener(_touchListener); - _touchListener = nullptr; - - // remove this layer from the delegates who concern Accelerometer Sensor - dispatcher->removeEventListener(_accelerationListener); - _accelerationListener = nullptr; - - // remove this layer from the delegates who concern the keypad msg - dispatcher->removeEventListener(_keyboardListener); - _keyboardListener = nullptr; - - Node::onExit(); -} - -void Layer::onEnterTransitionDidFinish() -{ - if (_accelerometerEnabled) - { - auto dispatcher = EventDispatcher::getInstance(); - dispatcher->removeEventListener(_accelerationListener); - _accelerationListener = EventListenerAcceleration::create(CC_CALLBACK_2(Layer::onAcceleration, this)); - dispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this); - } - - Node::onEnterTransitionDidFinish(); -} - -bool Layer::onTouchBegan(Touch *pTouch, Event *pEvent) -{ - if (kScriptTypeNone != _scriptType) - { - return executeScriptTouchHandler(EventTouch::EventCode::BEGAN, pTouch) == 0 ? false : true; - } - - CC_UNUSED_PARAM(pTouch); - CC_UNUSED_PARAM(pEvent); - CCASSERT(false, "Layer#ccTouchBegan override me"); - return true; -} - -void Layer::onTouchMoved(Touch *pTouch, Event *pEvent) -{ - if (kScriptTypeNone != _scriptType) - { - executeScriptTouchHandler(EventTouch::EventCode::MOVED, pTouch); - return; - } - - CC_UNUSED_PARAM(pTouch); - CC_UNUSED_PARAM(pEvent); -} - -void Layer::onTouchEnded(Touch *pTouch, Event *pEvent) -{ - if (kScriptTypeNone != _scriptType) - { - executeScriptTouchHandler(EventTouch::EventCode::ENDED, pTouch); - return; - } - - CC_UNUSED_PARAM(pTouch); - CC_UNUSED_PARAM(pEvent); -} - -void Layer::onTouchCancelled(Touch *pTouch, Event *pEvent) -{ - if (kScriptTypeNone != _scriptType) - { - executeScriptTouchHandler(EventTouch::EventCode::CANCELLED, pTouch); - return; - } - - CC_UNUSED_PARAM(pTouch); - CC_UNUSED_PARAM(pEvent); -} - -void Layer::onTouchesBegan(const std::vector& pTouches, Event *pEvent) -{ - if (kScriptTypeNone != _scriptType) - { - executeScriptTouchesHandler(EventTouch::EventCode::BEGAN, pTouches); - return; - } - - CC_UNUSED_PARAM(pTouches); - CC_UNUSED_PARAM(pEvent); -} - -void Layer::onTouchesMoved(const std::vector& pTouches, Event *pEvent) -{ - if (kScriptTypeNone != _scriptType) - { - executeScriptTouchesHandler(EventTouch::EventCode::MOVED, pTouches); - return; - } - - CC_UNUSED_PARAM(pTouches); - CC_UNUSED_PARAM(pEvent); -} - -void Layer::onTouchesEnded(const std::vector& pTouches, Event *pEvent) -{ - if (kScriptTypeNone != _scriptType) - { - executeScriptTouchesHandler(EventTouch::EventCode::ENDED, pTouches); - return; - } - - CC_UNUSED_PARAM(pTouches); - CC_UNUSED_PARAM(pEvent); -} - -void Layer::onTouchesCancelled(const std::vector& pTouches, Event *pEvent) -{ - if (kScriptTypeNone != _scriptType) - { - executeScriptTouchesHandler(EventTouch::EventCode::CANCELLED, pTouches); - return; - } - - CC_UNUSED_PARAM(pTouches); - CC_UNUSED_PARAM(pEvent); -} - - #ifdef CC_USE_PHYSICS void Layer::addChild(Node* child) { diff --git a/cocos/2d/CCLayer.h b/cocos/2d/CCLayer.h index 0dad16ff21..09effe2fcd 100644 --- a/cocos/2d/CCLayer.h +++ b/cocos/2d/CCLayer.h @@ -46,10 +46,6 @@ NS_CC_BEGIN class TouchScriptHandlerEntry; -class EventListenerTouch; -class EventListenerKeyboard; -class EventListenerAcceleration; - // // Layer // @@ -86,22 +82,9 @@ public: CC_DEPRECATED_ATTRIBUTE virtual void ccTouchesEnded(Set *pTouches, Event *pEvent) final {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);} CC_DEPRECATED_ATTRIBUTE virtual void ccTouchesCancelled(Set *pTouches, Event *pEvent) final {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);} - // default implements are used to call script callback if exist - virtual bool onTouchBegan(Touch *touch, Event *event); - virtual void onTouchMoved(Touch *touch, Event *event); - virtual void onTouchEnded(Touch *touch, Event *event); - virtual void onTouchCancelled(Touch *touch, Event *event); - -// // default implements are used to call script callback if exist - virtual void onTouchesBegan(const std::vector& touches, Event *event); - virtual void onTouchesMoved(const std::vector& touches, Event *event); - virtual void onTouchesEnded(const std::vector& touches, Event *event); - virtual void onTouchesCancelled(const std::vector&touches, Event *event); /** @deprecated Please override onAcceleration */ CC_DEPRECATED_ATTRIBUTE virtual void didAccelerate(Acceleration* accelerationValue) final {}; - - virtual void onAcceleration(Acceleration* acc, Event* event); /** If isTouchEnabled, this method is called onEnter. Override it to change the way Layer receives touch events. @@ -115,48 +98,13 @@ public: */ CC_DEPRECATED_ATTRIBUTE virtual void registerWithTouchDispatcher() final {}; - /** whether or not it will receive Touch events. - You can enable / disable touch events with this property. - Only the touches of this node will be affected. This "method" is not propagated to it's children. - @since v0.8.1 - */ - virtual bool isTouchEnabled() const; - virtual void setTouchEnabled(bool value); - - virtual void setTouchMode(Touch::DispatchMode mode); - virtual Touch::DispatchMode getTouchMode() const; - - /** swallowsTouches of the touch events. Default is true */ - virtual void setSwallowsTouches(bool swallowsTouches); - virtual bool isSwallowsTouches() const; - - /** whether or not it will receive Accelerometer events - You can enable / disable accelerometer events with this property. - @since v0.8.1 - */ - virtual bool isAccelerometerEnabled() const; - virtual void setAccelerometerEnabled(bool value); - virtual void setAccelerometerInterval(double interval); - - /** whether or not it will receive keyboard or keypad events - You can enable / disable accelerometer events with this property. - it's new in cocos2d-x - */ - - virtual bool isKeyboardEnabled() const; - virtual void setKeyboardEnabled(bool value); /** Please use onKeyPressed instead. */ virtual void keyPressed(int keyCode) final {}; /** Please use onKeyReleased instead. */ virtual void keyReleased(int keyCode) final {}; - - virtual void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event); - virtual void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event); - CC_DEPRECATED_ATTRIBUTE virtual bool isKeypadEnabled() const final { return isKeyboardEnabled(); }; - CC_DEPRECATED_ATTRIBUTE virtual void setKeypadEnabled(bool value) { setKeyboardEnabled(value); }; /** @deprecated Please override onKeyReleased and check the keycode of KeyboardEvent::KeyCode::Menu(KEY_BACKSPACE) instead. */ CC_DEPRECATED_ATTRIBUTE virtual void keyBackClicked() final {}; @@ -164,21 +112,6 @@ public: // // Overrides // - /** - * @js NA - * @lua NA - */ - virtual void onEnter() override; - /** - * @js NA - * @lua NA - */ - virtual void onExit() override; - /** - * @js NA - * @lua NA - */ - virtual void onEnterTransitionDidFinish() override; #ifdef CC_USE_PHYSICS virtual void addChild(Node* child) override; @@ -186,15 +119,6 @@ public: virtual void addChild(Node* child, int zOrder, int tag) override; #endif // CC_USE_PHYSICS -protected: - void addTouchListener(); - - bool _touchEnabled; - bool _accelerometerEnabled; - bool _keyboardEnabled; - EventListenerTouch* _touchListener; - EventListenerKeyboard* _keyboardListener; - EventListenerAcceleration* _accelerationListener; private: Touch::DispatchMode _touchMode; bool _swallowsTouches; diff --git a/cocos/2d/CCMenu.cpp b/cocos/2d/CCMenu.cpp index c0c10e9cdb..fc38405355 100644 --- a/cocos/2d/CCMenu.cpp +++ b/cocos/2d/CCMenu.cpp @@ -127,9 +127,6 @@ bool Menu::initWithArray(Array* pArrayOfItems) { if (Layer::init()) { - setTouchMode(Touch::DispatchMode::ONE_BY_ONE); - setTouchEnabled(true); - _enabled = true; // menu in the center of the screen Size s = Director::getInstance()->getWinSize(); @@ -188,8 +185,15 @@ void Menu::onEnter() { Layer::onEnter(); - setTouchEnabled(true); - setTouchMode(Touch::DispatchMode::ONE_BY_ONE); + auto eventDispatcher = EventDispatcher::getInstance(); + + auto touchListener = EventListenerTouchOneByOne::create(); + touchListener->onTouchBegan = CC_CALLBACK_2(Menu::onTouchBegan, this); + touchListener->onTouchMoved = CC_CALLBACK_2(Menu::onTouchMoved, this); + touchListener->onTouchEnded = CC_CALLBACK_2(Menu::onTouchEnded, this); + touchListener->onTouchCancelled = CC_CALLBACK_2(Menu::onTouchCancelled, this); + + eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); } void Menu::onExit() diff --git a/cocos/2d/CCMenu.h b/cocos/2d/CCMenu.h index bd685786c4..a735d138db 100644 --- a/cocos/2d/CCMenu.h +++ b/cocos/2d/CCMenu.h @@ -119,10 +119,10 @@ public: virtual void addChild(Node * child, int zOrder) override; virtual void addChild(Node * child, int zOrder, int tag) override; - virtual bool onTouchBegan(Touch* touch, Event* event) override; - virtual void onTouchEnded(Touch* touch, Event* event) override; - virtual void onTouchCancelled(Touch* touch, Event* event) override; - virtual void onTouchMoved(Touch* touch, Event* event) override; + virtual bool onTouchBegan(Touch* touch, Event* event); + virtual void onTouchEnded(Touch* touch, Event* event); + virtual void onTouchCancelled(Touch* touch, Event* event); + virtual void onTouchMoved(Touch* touch, Event* event); virtual void onEnter() override; virtual void onExit() override; From cfaef2a01ab78140ce8ea67e24055be0e30cec90 Mon Sep 17 00:00:00 2001 From: James Chen Date: Sat, 12 Oct 2013 14:02:01 +0800 Subject: [PATCH 02/42] Adding onEnterHook, onXXXHook for Node. --- cocos/2d/CCNode.cpp | 25 +++++++++++++++ cocos/2d/CCNode.h | 6 +++- .../cocosbuilder/CCLayerLoader.cpp | 4 +-- .../cocostudio/CCInputDelegate.cpp | 4 +-- cocos/gui/UILayer.cpp | 15 ++++++--- .../GUI/CCControlExtension/CCControl.cpp | 15 +++++---- .../CCControlExtension/CCControlButton.cpp | 1 - .../CCControlColourPicker.cpp | 1 - .../CCControlExtension/CCControlHuePicker.cpp | 1 - .../CCControlPotentiometer.cpp | 2 -- .../CCControlSaturationBrightnessPicker.cpp | 1 - .../CCControlExtension/CCControlSlider.cpp | 1 - .../CCControlExtension/CCControlStepper.cpp | 2 -- .../CCControlExtension/CCControlSwitch.cpp | 1 - extensions/GUI/CCScrollView/CCScrollView.cpp | 32 ++++++++++++------- extensions/GUI/CCScrollView/CCScrollView.h | 5 --- .../AccelerometerTest/AccelerometerTest.cpp | 6 ++-- .../AccelerometerTest/AccelerometerTest.h | 2 +- .../Cpp/TestCpp/Classes/Box2DTest/Box2dTest.h | 2 +- .../Cpp/TestCpp/Classes/BugsTest/Bug-624.cpp | 17 ++++++++-- .../Cpp/TestCpp/Classes/BugsTest/Bug-914.cpp | 9 +++++- .../Cpp/TestCpp/Classes/BugsTest/Bug-914.h | 4 +-- .../Cpp/TestCpp/Classes/BugsTest/BugsTest.h | 4 +-- .../ClickAndMoveTest/ClickAndMoveTest.cpp | 9 ++++-- .../ClickAndMoveTest/ClickAndMoveTest.h | 4 +-- .../ClippingNodeTest/ClippingNodeTest.cpp | 18 +++++++++-- .../ClippingNodeTest/ClippingNodeTest.h | 2 +- .../TestCpp/Classes/PhysicsTest/PhysicsTest.h | 4 +-- 28 files changed, 133 insertions(+), 64 deletions(-) diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 2145829c34..33f9963526 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -136,6 +136,11 @@ Node::Node(void) , _physicsBody(nullptr) #endif { + onEnterHook = nullptr; + onEnterTransitionDidFinishHook = nullptr; + onExitHook = nullptr; + onExitTransitionDidStartHook = nullptr; + // set default scheduler and actionManager Director *director = Director::getInstance(); _actionManager = director->getActionManager(); @@ -945,6 +950,11 @@ void Node::onEnter() ScriptEvent scriptEvent(kNodeEvent,(void*)&data); ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent); } + + if (onEnterHook) + { + onEnterHook(); + } } void Node::onEnterTransitionDidFinish() @@ -960,10 +970,20 @@ void Node::onEnterTransitionDidFinish() ScriptEvent scriptEvent(kNodeEvent,(void*)&data); ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent); } + + if (onEnterTransitionDidFinishHook) + { + onEnterTransitionDidFinishHook(); + } } void Node::onExitTransitionDidStart() { + if (onExitTransitionDidStartHook) + { + onExitTransitionDidStartHook(); + } + arrayMakeObjectsPerformSelector(_children, onExitTransitionDidStart, Node*); if (_scriptType != kScriptTypeNone) { @@ -976,6 +996,11 @@ void Node::onExitTransitionDidStart() void Node::onExit() { + if (onExitHook) + { + onExitHook(); + } + this->pauseSchedulerAndActions(); _running = false; diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index ca0529756b..ca0a8eca96 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -928,6 +928,7 @@ public: * @lua NA */ virtual void onEnter(); + std::function onEnterHook; /** Event callback that is invoked when the Node enters in the 'stage'. * If the Node enters the 'stage' with a transition, this event is called when the transition finishes. @@ -936,6 +937,7 @@ public: * @lua NA */ virtual void onEnterTransitionDidFinish(); + std::function onEnterTransitionDidFinishHook; /** * Event callback that is invoked every time the Node leaves the 'stage'. @@ -946,6 +948,7 @@ public: * @lua NA */ virtual void onExit(); + std::function onExitHook; /** * Event callback that is called every time the Node leaves the 'stage'. @@ -954,7 +957,8 @@ public: * @lua NA */ virtual void onExitTransitionDidStart(); - + std::function onExitTransitionDidStartHook; + /// @} end of event callbacks. diff --git a/cocos/editor-support/cocosbuilder/CCLayerLoader.cpp b/cocos/editor-support/cocosbuilder/CCLayerLoader.cpp index f13cf610f6..ebca5a8839 100644 --- a/cocos/editor-support/cocosbuilder/CCLayerLoader.cpp +++ b/cocos/editor-support/cocosbuilder/CCLayerLoader.cpp @@ -14,9 +14,9 @@ namespace cocosbuilder { void LayerLoader::onHandlePropTypeCheck(Node * pNode, Node * pParent, const char * pPropertyName, bool pCheck, CCBReader * ccbReader) { if(strcmp(pPropertyName, PROPERTY_TOUCH_ENABLED) == 0) { - ((Layer *)pNode)->setTouchEnabled(pCheck); +// FIXME: ((Layer *)pNode)->setTouchEnabled(pCheck); } else if(strcmp(pPropertyName, PROPERTY_ACCELEROMETER_ENABLED) == 0) { - ((Layer *)pNode)->setAccelerometerEnabled(pCheck); +// FIXME: ((Layer *)pNode)->setAccelerometerEnabled(pCheck); } else if(strcmp(pPropertyName, PROPERTY_MOUSE_ENABLED) == 0) { // TODO XXX CCLOG("The property '%s' is not supported!", PROPERTY_MOUSE_ENABLED); diff --git a/cocos/editor-support/cocostudio/CCInputDelegate.cpp b/cocos/editor-support/cocostudio/CCInputDelegate.cpp index 9f4fd46a8d..968645bc4e 100644 --- a/cocos/editor-support/cocostudio/CCInputDelegate.cpp +++ b/cocos/editor-support/cocostudio/CCInputDelegate.cpp @@ -112,7 +112,7 @@ void InputDelegate::setTouchEnabled(bool enabled) { if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE ) { // Register Touch Event - auto listener = EventListenerTouch::create(Touch::DispatchMode::ALL_AT_ONCE); + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(InputDelegate::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(InputDelegate::onTouchesMoved, this); @@ -123,7 +123,7 @@ void InputDelegate::setTouchEnabled(bool enabled) _touchListener = listener; } else { // Register Touch Event - auto listener = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); + auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(InputDelegate::onTouchBegan, this); diff --git a/cocos/gui/UILayer.cpp b/cocos/gui/UILayer.cpp index f92d73c9c7..891301bf5b 100644 --- a/cocos/gui/UILayer.cpp +++ b/cocos/gui/UILayer.cpp @@ -74,15 +74,22 @@ UILayer* UILayer::create(void) void UILayer::onEnter() { - setTouchMode(Touch::DispatchMode::ONE_BY_ONE); - setTouchEnabled(true); CCLayer::onEnter(); + auto listener = EventListenerTouchOneByOne::create(); + listener->setSwallowTouches(true); + + listener->onTouchBegan = CC_CALLBACK_2(UILayer::onTouchBegan, this); + listener->onTouchMoved = CC_CALLBACK_2(UILayer::onTouchMoved, this); + listener->onTouchEnded = CC_CALLBACK_2(UILayer::onTouchEnded, this); + listener->onTouchCancelled = CC_CALLBACK_2(UILayer::onTouchCancelled, this); + + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); } void UILayer::onExit() { - setTouchEnabled(false); +// setTouchEnabled(false); CCLayer::onExit(); } @@ -164,4 +171,4 @@ void UILayer::onTouchCancelled(Touch *pTouch, Event *pEvent) _inputManager->onTouchCancelled(pTouch); } -} \ No newline at end of file +} diff --git a/extensions/GUI/CCControlExtension/CCControl.cpp b/extensions/GUI/CCControlExtension/CCControl.cpp index d12859b703..1419edbc36 100644 --- a/extensions/GUI/CCControlExtension/CCControl.cpp +++ b/extensions/GUI/CCControlExtension/CCControl.cpp @@ -93,17 +93,18 @@ Control::~Control() CC_SAFE_RELEASE(_dispatchTable); } -////Menu - Events -//void Control::registerWithTouchDispatcher() -//{ -// Director::getInstance()->getTouchDispatcher()->addTargetedDelegate(this, getTouchPriority(), true); -//} - void Control::onEnter() { Layer::onEnter(); - this->setTouchMode(Touch::DispatchMode::ONE_BY_ONE); + auto dispatcher = EventDispatcher::getInstance(); + auto touchListener = EventListenerTouchOneByOne::create(); + touchListener->onTouchBegan = CC_CALLBACK_2(Control::onTouchBegan, this); + touchListener->onTouchMoved = CC_CALLBACK_2(Control::onTouchMoved, this); + touchListener->onTouchEnded = CC_CALLBACK_2(Control::onTouchEnded, this); + touchListener->onTouchCancelled = CC_CALLBACK_2(Control::onTouchCancelled, this); + + dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); } void Control::onExit() diff --git a/extensions/GUI/CCControlExtension/CCControlButton.cpp b/extensions/GUI/CCControlExtension/CCControlButton.cpp index 4f889f7066..5e4e102aba 100644 --- a/extensions/GUI/CCControlExtension/CCControlButton.cpp +++ b/extensions/GUI/CCControlExtension/CCControlButton.cpp @@ -96,7 +96,6 @@ bool ControlButton::initWithLabelAndBackgroundSprite(Node* node, Scale9Sprite* b this->setTitleLabelDispatchTable(Dictionary::create()); this->setBackgroundSpriteDispatchTable(Dictionary::create()); - setTouchEnabled(true); _isPushed = false; _zoomOnTouchDown = true; diff --git a/extensions/GUI/CCControlExtension/CCControlColourPicker.cpp b/extensions/GUI/CCControlExtension/CCControlColourPicker.cpp index 18da7f0abf..082c929bea 100644 --- a/extensions/GUI/CCControlExtension/CCControlColourPicker.cpp +++ b/extensions/GUI/CCControlExtension/CCControlColourPicker.cpp @@ -54,7 +54,6 @@ bool ControlColourPicker::init() { if (Control::init()) { - setTouchEnabled(true); // Cache the sprites SpriteFrameCache::getInstance()->addSpriteFramesWithFile("extensions/CCControlColourPickerSpriteSheet.plist"); diff --git a/extensions/GUI/CCControlExtension/CCControlHuePicker.cpp b/extensions/GUI/CCControlExtension/CCControlHuePicker.cpp index 48230e84ab..6e5600edf0 100644 --- a/extensions/GUI/CCControlExtension/CCControlHuePicker.cpp +++ b/extensions/GUI/CCControlExtension/CCControlHuePicker.cpp @@ -62,7 +62,6 @@ bool ControlHuePicker::initWithTargetAndPos(Node* target, Point pos) { if (Control::init()) { - setTouchEnabled(true); // Add background and slider sprites this->setBackground(ControlUtils::addSpriteToTargetWithPosAndAnchor("huePickerBackground.png", target, pos, Point(0.0f, 0.0f))); this->setSlider(ControlUtils::addSpriteToTargetWithPosAndAnchor("colourPicker.png", target, pos, Point(0.5f, 0.5f))); diff --git a/extensions/GUI/CCControlExtension/CCControlPotentiometer.cpp b/extensions/GUI/CCControlExtension/CCControlPotentiometer.cpp index 3a28593955..ab3fccdb74 100644 --- a/extensions/GUI/CCControlExtension/CCControlPotentiometer.cpp +++ b/extensions/GUI/CCControlExtension/CCControlPotentiometer.cpp @@ -76,8 +76,6 @@ bool ControlPotentiometer::initWithTrackSprite_ProgressTimer_ThumbSprite(Sprite* { if (Control::init()) { - setTouchEnabled(true); - setProgressTimer(progressTimer); setThumbSprite(thumbSprite); thumbSprite->setPosition(progressTimer->getPosition()); diff --git a/extensions/GUI/CCControlExtension/CCControlSaturationBrightnessPicker.cpp b/extensions/GUI/CCControlExtension/CCControlSaturationBrightnessPicker.cpp index 3252de4c94..4d59fca00d 100644 --- a/extensions/GUI/CCControlExtension/CCControlSaturationBrightnessPicker.cpp +++ b/extensions/GUI/CCControlExtension/CCControlSaturationBrightnessPicker.cpp @@ -60,7 +60,6 @@ bool ControlSaturationBrightnessPicker::initWithTargetAndPos(Node* target, Point { if (Control::init()) { - setTouchEnabled(true); // Add background and slider sprites _background=ControlUtils::addSpriteToTargetWithPosAndAnchor("colourPickerBackground.png", target, pos, Point(0.0f, 0.0f)); _overlay=ControlUtils::addSpriteToTargetWithPosAndAnchor("colourPickerOverlay.png", target, pos, Point(0.0f, 0.0f)); diff --git a/extensions/GUI/CCControlExtension/CCControlSlider.cpp b/extensions/GUI/CCControlExtension/CCControlSlider.cpp index 1ba96962b6..bafd327d69 100644 --- a/extensions/GUI/CCControlExtension/CCControlSlider.cpp +++ b/extensions/GUI/CCControlExtension/CCControlSlider.cpp @@ -84,7 +84,6 @@ ControlSlider* ControlSlider::create(Sprite * backgroundSprite, Sprite* pogressS CCASSERT(thumbSprite, "Thumb sprite must be not nil"); ignoreAnchorPointForPosition(false); - setTouchEnabled(true); this->setBackgroundSprite(backgroundSprite); this->setProgressSprite(progressSprite); diff --git a/extensions/GUI/CCControlExtension/CCControlStepper.cpp b/extensions/GUI/CCControlExtension/CCControlStepper.cpp index 7aee633bda..4c4df49589 100644 --- a/extensions/GUI/CCControlExtension/CCControlStepper.cpp +++ b/extensions/GUI/CCControlExtension/CCControlStepper.cpp @@ -74,8 +74,6 @@ bool ControlStepper::initWithMinusSpriteAndPlusSprite(Sprite *minusSprite, Sprit CCASSERT(minusSprite, "Minus sprite must be not nil"); CCASSERT(plusSprite, "Plus sprite must be not nil"); - setTouchEnabled(true); - // Set the default values _autorepeat = true; _continuous = true; diff --git a/extensions/GUI/CCControlExtension/CCControlSwitch.cpp b/extensions/GUI/CCControlExtension/CCControlSwitch.cpp index dcf7fd555e..fe67a3f905 100644 --- a/extensions/GUI/CCControlExtension/CCControlSwitch.cpp +++ b/extensions/GUI/CCControlExtension/CCControlSwitch.cpp @@ -346,7 +346,6 @@ bool ControlSwitch::initWithMaskSprite(Sprite *maskSprite, Sprite * onSprite, Sp CCASSERT(offSprite, "offSprite must not be nil."); CCASSERT(thumbSprite, "thumbSprite must not be nil."); - setTouchEnabled(true); _on = true; _switchSprite = new ControlSwitchSprite(); diff --git a/extensions/GUI/CCScrollView/CCScrollView.cpp b/extensions/GUI/CCScrollView/CCScrollView.cpp index ee6a416310..e5f4c0503e 100644 --- a/extensions/GUI/CCScrollView/CCScrollView.cpp +++ b/extensions/GUI/CCScrollView/CCScrollView.cpp @@ -109,8 +109,16 @@ bool ScrollView::initWithViewSize(Size size, Node *container/* = NULL*/) this->setViewSize(size); - setTouchEnabled(true); - setTouchMode(Touch::DispatchMode::ONE_BY_ONE); + this->onEnterHook = [this]() { + auto dispatcher = EventDispatcher::getInstance(); + auto listener = EventListenerTouchOneByOne::create(); + listener->onTouchBegan = CC_CALLBACK_2(ScrollView::onTouchBegan, this); + listener->onTouchMoved = CC_CALLBACK_2(ScrollView::onTouchMoved, this); + listener->onTouchEnded = CC_CALLBACK_2(ScrollView::onTouchEnded, this); + listener->onTouchCancelled = CC_CALLBACK_2(ScrollView::onTouchCancelled, this); + + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + }; _touches.reserve(EventTouch::MAX_TOUCHES); @@ -177,16 +185,16 @@ void ScrollView::resume(Object* sender) _container->resumeSchedulerAndActions(); } -void ScrollView::setTouchEnabled(bool e) -{ - Layer::setTouchEnabled(e); - if (!e) - { - _dragging = false; - _touchMoved = false; - _touches.clear(); - } -} +//void ScrollView::setTouchEnabled(bool e) +//{ +// Layer::setTouchEnabled(e); +// if (!e) +// { +// _dragging = false; +// _touchMoved = false; +// _touches.clear(); +// } +//} void ScrollView::setContentOffset(Point offset, bool animated/* = false*/) { diff --git a/extensions/GUI/CCScrollView/CCScrollView.h b/extensions/GUI/CCScrollView/CCScrollView.h index 5b002887ee..42b9d78425 100644 --- a/extensions/GUI/CCScrollView/CCScrollView.h +++ b/extensions/GUI/CCScrollView/CCScrollView.h @@ -216,10 +216,6 @@ public: virtual void onTouchCancelled(Touch *touch, Event *event); // Overrides -// virtual bool ccTouchBegan(Touch *pTouch, Event *pEvent) override; -// virtual void ccTouchMoved(Touch *pTouch, Event *pEvent) override; -// virtual void ccTouchEnded(Touch *pTouch, Event *pEvent) override; -// virtual void ccTouchCancelled(Touch *pTouch, Event *pEvent) override; virtual void setContentSize(const Size & size) override; virtual const Size& getContentSize() const override; /** @@ -230,7 +226,6 @@ public: virtual void addChild(Node * child, int zOrder, int tag) override; virtual void addChild(Node * child, int zOrder) override; virtual void addChild(Node * child) override; - void setTouchEnabled(bool e) override; protected: /** diff --git a/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.cpp b/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.cpp index 4f06570737..ada7540cc6 100644 --- a/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.cpp +++ b/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.cpp @@ -32,8 +32,10 @@ void AccelerometerTest::onEnter() { Layer::onEnter(); - setAccelerometerEnabled(true); - + auto dispatcher = EventDispatcher::getInstance(); + + auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(AccelerometerTest::onAcceleration, this)); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto label = LabelTTF::create(title().c_str(), "Arial", 32); addChild(label, 1); diff --git a/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.h b/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.h index f1524be1ab..7e4cc7edf1 100644 --- a/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.h +++ b/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.h @@ -15,7 +15,7 @@ public: AccelerometerTest(void); ~AccelerometerTest(void); - virtual void onAcceleration(Acceleration* acc, Event* event) override; + virtual void onAcceleration(Acceleration* acc, Event* event); virtual std::string title(); virtual void onEnter(); diff --git a/samples/Cpp/TestCpp/Classes/Box2DTest/Box2dTest.h b/samples/Cpp/TestCpp/Classes/Box2DTest/Box2dTest.h index 9e04493964..480d572f7a 100644 --- a/samples/Cpp/TestCpp/Classes/Box2DTest/Box2dTest.h +++ b/samples/Cpp/TestCpp/Classes/Box2DTest/Box2dTest.h @@ -21,7 +21,7 @@ public: void addNewSpriteAtPosition(Point p); void update(float dt); - virtual void onTouchesEnded(const std::vector& touches, Event* event) override; + virtual void onTouchesEnded(const std::vector& touches, Event* event); //CREATE_NODE(Box2DTestLayer); } ; diff --git a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-624.cpp b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-624.cpp index 2f03948186..a86859ebb1 100644 --- a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-624.cpp +++ b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-624.cpp @@ -19,7 +19,13 @@ bool Bug624Layer::init() label->setPosition(Point(size.width/2, size.height/2)); addChild(label); - setAccelerometerEnabled(true); + + this->onEnterHook = [this](){ + auto dispatcher = EventDispatcher::getInstance(); + auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(Bug624Layer::onAcceleration, this)); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + }; + schedule(schedule_selector(Bug624Layer::switchLayer), 5.0f); return true; @@ -56,7 +62,14 @@ bool Bug624Layer2::init() label->setPosition(Point(size.width/2, size.height/2)); addChild(label); - setAccelerometerEnabled(true); + + this->onEnterHook = [this](){ + auto dispatcher = EventDispatcher::getInstance(); + auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(Bug624Layer2::onAcceleration, this)); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + }; + + schedule(schedule_selector(Bug624Layer2::switchLayer), 5.0f); return true; diff --git a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.cpp b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.cpp index 64665fd18f..337d9b1006 100644 --- a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.cpp +++ b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.cpp @@ -30,7 +30,14 @@ bool Bug914Layer::init() // Apple recommends to re-assign "self" with the "super" return value if (BugsTestBaseLayer::init()) { - setTouchEnabled(true); + this->onEnterHook = [this](){ + auto dispatcher = EventDispatcher::getInstance(); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesBegan = CC_CALLBACK_2(Bug914Layer::onTouchesBegan, this); + listener->onTouchesMoved = CC_CALLBACK_2(Bug914Layer::onTouchesMoved, this); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + }; + // ask director the the window size auto size = Director::getInstance()->getWinSize(); LayerColor *layer; diff --git a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.h b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.h index af5f8c1bdd..41ee2c25c8 100644 --- a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.h +++ b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.h @@ -9,8 +9,8 @@ public: static Scene* scene(); virtual bool init(); - virtual void onTouchesMoved(const std::vector& touches, Event * event) override; - virtual void onTouchesBegan(const std::vector& touches, Event * event) override; + virtual void onTouchesMoved(const std::vector& touches, Event * event); + virtual void onTouchesBegan(const std::vector& touches, Event * event); void restart(Object* sender); CREATE_FUNC(Bug914Layer); diff --git a/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.h b/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.h index 8ea7d2bbbd..88de62b0a1 100644 --- a/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.h +++ b/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.h @@ -8,8 +8,8 @@ class BugsTestMainLayer : public Layer public: virtual void onEnter(); - virtual void onTouchesBegan(const std::vector& touches, Event *event) override; - virtual void onTouchesMoved(const std::vector&touches, Event *event) override; + virtual void onTouchesBegan(const std::vector& touches, Event *event); + virtual void onTouchesMoved(const std::vector&touches, Event *event); protected: Point _beginPos; diff --git a/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.cpp b/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.cpp index 4215b8810d..5a87658f35 100644 --- a/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.cpp @@ -17,8 +17,13 @@ void ClickAndMoveTestScene::runThisTest() MainLayer::MainLayer() { - setTouchEnabled(true); - setTouchMode(Touch::DispatchMode::ONE_BY_ONE); + this->onEnterHook = [this](){ + auto dispatcher = EventDispatcher::getInstance(); + auto listener = EventListenerTouchOneByOne::create(); + listener->onTouchBegan = CC_CALLBACK_2(MainLayer::onTouchBegan, this); + listener->onTouchEnded = CC_CALLBACK_2(MainLayer::onTouchEnded, this); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + }; auto sprite = Sprite::create(s_pathGrossini); diff --git a/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.h b/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.h index f223d0b5ee..7cb8f7a3b4 100644 --- a/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.h +++ b/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.h @@ -13,8 +13,8 @@ class MainLayer : public Layer { public: MainLayer(); - virtual bool onTouchBegan(Touch* touch, Event *event) override; - virtual void onTouchEnded(Touch* touch, Event *event) override; + virtual bool onTouchBegan(Touch* touch, Event *event); + virtual void onTouchEnded(Touch* touch, Event *event); }; #endif diff --git a/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.cpp b/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.cpp index 51e9791097..fcde7712ad 100644 --- a/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.cpp @@ -448,8 +448,13 @@ void HoleDemo::setup() _outerClipper->addChild(holesClipper); this->addChild(_outerClipper); - - this->setTouchEnabled(true); + + this->onEnterHook = [this](){ + auto dispatcher = EventDispatcher::getInstance(); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesBegan = CC_CALLBACK_2(HoleDemo::onTouchesBegan, this); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + }; } void HoleDemo::pokeHoleAtPoint(Point point) @@ -526,7 +531,14 @@ void ScrollViewDemo::setup() _scrolling = false; - this->setTouchEnabled(true); + this->onEnterHook = [this](){ + auto dispatcher = EventDispatcher::getInstance(); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesBegan = CC_CALLBACK_2(ScrollViewDemo::onTouchesBegan, this); + listener->onTouchesMoved = CC_CALLBACK_2(ScrollViewDemo::onTouchesMoved, this); + listener->onTouchesEnded = CC_CALLBACK_2(ScrollViewDemo::onTouchesEnded, this); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + }; } void ScrollViewDemo::onTouchesBegan(const std::vector& touches, Event *event) diff --git a/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.h b/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.h index 11445bf406..0cb0c21d6d 100644 --- a/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.h +++ b/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.h @@ -98,7 +98,7 @@ public: virtual std::string title(); virtual std::string subtitle(); void pokeHoleAtPoint(Point point); - virtual void onTouchesBegan(const std::vector& touches, Event *event) override; + virtual void onTouchesBegan(const std::vector& touches, Event *event); private: ClippingNode* _outerClipper; Node* _holes; diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h index 0c685ef98e..7dbb28617b 100644 --- a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h @@ -57,8 +57,8 @@ public: void onEnter() override; std::string subtitle() override; - void onTouchesEnded(const std::vector& touches, Event* event) override; - void onAcceleration(Acceleration* acc, Event* event) override; + void onTouchesEnded(const std::vector& touches, Event* event); + void onAcceleration(Acceleration* acc, Event* event); }; class PhysicsDemoLogoSmash : public PhysicsDemo From f7e2c63230d8d3dde81baba0578d8aab199f0fed Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 21 Oct 2013 17:22:42 +0800 Subject: [PATCH 03/42] Refactor event dispatcher. Remove non-node relative member variables. --- cocos/2d/CCDirector.cpp | 2 - cocos/2d/CCEventDispatcher.cpp | 185 +++++++++++++++++++++++++++++---- cocos/2d/CCEventDispatcher.h | 43 +++++++- cocos/2d/CCEventListener.h | 2 +- cocos/2d/CCNode.cpp | 23 +--- cocos/2d/CCNode.h | 21 +--- 6 files changed, 210 insertions(+), 66 deletions(-) diff --git a/cocos/2d/CCDirector.cpp b/cocos/2d/CCDirector.cpp index 8d1d8cb5df..9dc388ea11 100644 --- a/cocos/2d/CCDirector.cpp +++ b/cocos/2d/CCDirector.cpp @@ -229,8 +229,6 @@ void Director::setGLDefaultValues() // Draw the Scene void Director::drawScene() { - Node::resetEventPriorityIndex(); - // calculate "global" dt calculateDeltaTime(); diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index 0e596b788a..57a8d5ffde 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -59,10 +59,156 @@ private: NS_CC_BEGIN static EventDispatcher* g_instance = nullptr; +static int s_eventPriorityIndex = 0; + +void EventDispatcher::visitNode(Node* node) +{ + int i = 0; + int childrenCount = node->_children ? node->_children->count() : 0; + + if(childrenCount > 0) + { + Node* child = nullptr; + // visit children zOrder < 0 + for( ; i < childrenCount; i++ ) + { + child = static_cast( node->_children->getObjectAtIndex(i) ); + + if ( child && child->_ZOrder < 0 ) + visitNode(child); + else + break; + } + + node->_eventPriority = ++s_eventPriorityIndex; + + for( ; i < childrenCount; i++ ) + { + child = static_cast( node->_children->getObjectAtIndex(i) ); + if (child) + visitNode(child); + } + } + else + { + node->_eventPriority = ++s_eventPriorityIndex; + } +} + +EventDispatcher::EventListenerItem::EventListenerItem() +: fixedPriority(0) +, listener(nullptr) +, node(nullptr) +{ + +} EventDispatcher::EventListenerItem::~EventListenerItem() { - CC_SAFE_RELEASE(this->node); +} + +EventDispatcher::EventListenerItemVector::EventListenerItemVector() +: _sceneGraphItems(nullptr) +, _fixedItems(nullptr) +{ +} + +EventDispatcher::EventListenerItemVector::~EventListenerItemVector() +{ + CC_SAFE_DELETE(_sceneGraphItems); + CC_SAFE_DELETE(_fixedItems); +} + +size_t EventDispatcher::EventListenerItemVector::size() const +{ + size_t ret = 0; + if (_sceneGraphItems) + ret += _sceneGraphItems->size(); + if (_fixedItems) + ret += _fixedItems->size(); + + return ret; +} + +bool EventDispatcher::EventListenerItemVector::empty() const +{ + return (_sceneGraphItems == nullptr || _sceneGraphItems->empty()) + && (_fixedItems == nullptr || _fixedItems->empty()); +} + +void EventDispatcher::EventListenerItemVector::iterate(IterateCallback cb, EventDispatcher::EventListenerItemVector::IterateMode mode/* = IterateMode::ALL*/) +{ + auto loop = [&cb](std::vector* items) -> bool { + for (auto iter = items->begin(); iter != items->end(); ++iter) + { + if (cb(iter, items)) + return false; + } + return true; + }; + + switch (mode) + { + case IterateMode::FIXED_PRIORITY_LESS_THAN_0: + loop(_lt0); + break; + case IterateMode::SCENE_GRAPH_PRIORITY: + loop(_eq0); + break; + case IterateMode::FIXED_PRIORITY_GREATER_THAN_0: + loop(_gt0); + break; + case IterateMode::ALL: + { + if (!loop(_lt0)) return; + if (!loop(_eq0)) return; + if (!loop(_gt0)) return; + } + default: + break; + } +} + +void EventDispatcher::EventListenerItemVector::push_back(EventDispatcher::EventListenerItem* item) +{ + if (item->fixedPriority == 0) + { + if (_sceneGraphItems == nullptr) + { + _sceneGraphItems = new std::vector(); + _sceneGraphItems->reserve(100); + } + + _sceneGraphItems->push_back(item); + } + else + { + if (_fixedItems == nullptr) + { + _fixedItems = new std::vector(); + _fixedItems->reserve(100); + } + + _fixedItems->push_back(item); + } +} + +void EventDispatcher::EventListenerItemVector::remove(EventDispatcher::EventListenerItem* item) +{ + if (item->fixedPriority < 0) + { + CCASSERT(_lt0, "vector invaild"); + + } + else if (item->fixedPriority == 0) + { + CCASSERT(_eq0, "vector invaild"); + + } + else + { + CCASSERT(_gt0, "vector invalid"); + } } EventDispatcher::EventDispatcher() @@ -96,13 +242,13 @@ void EventDispatcher::addEventListenerWithItem(EventListenerItem* item) { if (_inDispatch == 0) { - std::vector* listenerList = nullptr; + EventListenerItemVector* listenerList = nullptr; auto iter = _listeners.find(item->listener->_type); if (iter == _listeners.end()) { - listenerList = new std::vector(); - listenerList->reserve(100); + listenerList = new EventListenerItemVector(); +// listenerList->reserve(100); _listeners.insert(std::make_pair(item->listener->_type, listenerList)); } else @@ -110,7 +256,7 @@ void EventDispatcher::addEventListenerWithItem(EventListenerItem* item) listenerList = iter->second; } - listenerList->insert(listenerList->begin(), item); + listenerList->push_back(item); setDirtyForEventType(item->listener->_type, true); } @@ -169,32 +315,35 @@ void EventDispatcher::removeEventListener(EventListener* listener) for (auto iter = _listeners.begin(); iter != _listeners.end();) { - for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter) - { - if ((*itemIter)->listener == listener) + iter->second->iterate([&](std::vector::iterator itemIt, std::vector* current) -> bool { + + auto item = *itemIt; + if (item->listener == listener) { CC_SAFE_RETAIN(listener); - (*itemIter)->listener->_isRegistered = false; - if ((*itemIter)->node != nullptr) + item->listener->_isRegistered = false; + if (item->node != nullptr) { - (*itemIter)->node->dissociateEventListener(listener); + item->node->dissociateEventListener(listener); } - (*itemIter)->listener->release(); + item->listener->release(); if (_inDispatch == 0) { - delete (*itemIter); - iter->second->erase(itemIter); + current->erase(itemIt); + delete item; } else { - (*itemIter)->listener = nullptr; + item->listener = nullptr; } - + isFound = true; - break; + return false; } - } + + return true; + }); if (iter->second->empty()) { diff --git a/cocos/2d/CCEventDispatcher.h b/cocos/2d/CCEventDispatcher.h index 06b6e430c7..04191ea6d8 100644 --- a/cocos/2d/CCEventDispatcher.h +++ b/cocos/2d/CCEventDispatcher.h @@ -100,23 +100,58 @@ public: */ void dispatchEvent(Event* event, bool forceSortListeners = false); + void sortSceneGraphListeners(const std::string& eventType); + void setDirtyForEventType(const std::string& eventType, bool isDirty); bool isDirtyForEventType(const std::string& eventType); + void visitNode(Node* node); + public: /** Destructor of EventDispatcher */ ~EventDispatcher(); private: + struct EventListenerItem { - int fixedPriority; // The higher the number, the higher the priority - Node* node; // Weak reference. + int fixedPriority; // The higher the number, the higher the priority, 0 is for scene graph base priority. + Node* node; // Weak reference. EventListener* listener; + + EventListenerItem(); ~EventListenerItem(); }; + + class EventListenerItemVector + { + public: + EventListenerItemVector(); + ~EventListenerItemVector(); + size_t size() const; + bool empty() const; + + enum class IterateMode + { + FIXED_PRIORITY_LESS_THAN_0, + SCENE_GRAPH_PRIORITY, + FIXED_PRIORITY_GREATER_THAN_0, + ALL + }; + + typedef std::function::iterator, std::vector*)> IterateCallback; + + void iterate(IterateCallback cb, IterateMode mode = IterateMode::ALL); + void push_back(EventListenerItem* item); + void remove(EventListenerItem* item); + + private: + std::vector* _fixedItems; + std::vector* _sceneGraphItems; + }; + /** Constructor of EventDispatcher */ EventDispatcher(); @@ -127,7 +162,7 @@ private: void dispatchTouchEvent(EventTouch* event); /** Gets event the listener list for the event type. */ - std::vector* getListenerItemsForType(const std::string& eventType); + EventListenerItemVector* getListenerItemsForType(const std::string& eventType); /** Sorts the listeners of specified type by priority */ void sortAllEventListenerItemsForType(const std::string& eventType); @@ -142,7 +177,7 @@ private: /** * Listeners map. */ - std::map*> _listeners; + std::map _listeners; /// Priority dirty flag std::map _priorityDirtyFlagMap; diff --git a/cocos/2d/CCEventListener.h b/cocos/2d/CCEventListener.h index 559cf21739..982531416c 100644 --- a/cocos/2d/CCEventListener.h +++ b/cocos/2d/CCEventListener.h @@ -39,7 +39,7 @@ class Event; /** * The base class of event listener. * If you need custom listener which with different callback, you need to inherit this class. - * For instance, you could refer to AccelerationEventListener, KeyboardEventListener or TouchEventListener, CustomEventListener. + * For instance, you could refer to EventListenerAcceleration, EventListenerKeyboard, EventListenerTouchOneByOne, EventListenerCustom. */ class EventListener : public Object { diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 33f9963526..aad3d3fe6d 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -89,10 +89,10 @@ bool nodeComparisonLess(Object* p1, Object* p2) // XXX: Yes, nodes might have a sort problem once every 15 days if the game runs at 60 FPS and each frame sprites are reordered. static int s_globalOrderOfArrival = 1; -int Node::_globalEventPriorityIndex = 0; Node::Node(void) -: _rotationX(0.0f) +: _eventPriority(0) +, _rotationX(0.0f) , _rotationY(0.0f) , _scaleX(1.0f) , _scaleY(1.0f) @@ -130,8 +130,6 @@ Node::Node(void) , _isTransitionFinished(false) , _updateScriptHandler(0) , _componentContainer(NULL) -, _eventPriority(0) -, _oldEventPriority(0) #ifdef CC_USE_PHYSICS , _physicsBody(nullptr) #endif @@ -864,7 +862,6 @@ void Node::visit() } // self draw this->draw(); - updateEventPriorityIndex(); for( ; i < _children->count(); i++ ) { @@ -876,7 +873,6 @@ void Node::visit() else { this->draw(); - updateEventPriorityIndex(); } // reset for next frame @@ -1367,11 +1363,6 @@ void Node::removeAllComponents() _componentContainer->removeAll(); } -void Node::resetEventPriorityIndex() -{ - _globalEventPriorityIndex = 0; -} - void Node::associateEventListener(EventListener* listener) { _eventlisteners.insert(listener); @@ -1394,16 +1385,6 @@ void Node::removeAllEventListeners() } } -void Node::setDirtyForAllEventListeners() -{ - auto dispatcher = EventDispatcher::getInstance(); - - for (auto& listener : _eventlisteners) - { - dispatcher->setDirtyForEventType(listener->_type, true); - } -} - #ifdef CC_USE_PHYSICS void Node::setPhysicsBody(PhysicsBody* body) { diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index ca0a8eca96..b5c42f2301 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -1407,32 +1407,17 @@ private: friend class Director; friend class EventDispatcher; - int getEventPriority() const { return _eventPriority; }; - void associateEventListener(EventListener* listener); void dissociateEventListener(EventListener* listener); - static void resetEventPriorityIndex(); std::set _eventlisteners; + int _eventPriority; protected: - /// Upates event priority for this node. - inline void updateEventPriorityIndex() { - _oldEventPriority = _eventPriority; - _eventPriority = ++_globalEventPriorityIndex; - if (_oldEventPriority != _eventPriority) - { - setDirtyForAllEventListeners(); - } - }; - /// Removes all event listeners that associated with this node. void removeAllEventListeners(); - /// Sets dirty for event listener. - void setDirtyForAllEventListeners(); - /// lazy allocs void childrenAlloc(void); @@ -1509,10 +1494,6 @@ protected: ccScriptType _scriptType; ///< type of script binding, lua or javascript ComponentContainer *_componentContainer; ///< Dictionary of components - - int _eventPriority; ///< The scene graph based priority of event listener. - int _oldEventPriority; ///< The old scene graph based priority of event listener. - static int _globalEventPriorityIndex; ///< The index of global event priority. #ifdef CC_USE_PHYSICS PhysicsBody* _physicsBody; ///< the physicsBody the node have From 1f3863e787b97955796e23ad22c718ed142932c3 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 23 Oct 2013 11:27:24 +0800 Subject: [PATCH 04/42] Event Dispatcher refactor commit. TestCpp could run. --- cocos/2d/CCEventDispatcher.cpp | 903 +++++++++++------- cocos/2d/CCEventDispatcher.h | 105 +- cocos/2d/CCEventListener.cpp | 1 + cocos/2d/CCEventListener.h | 12 +- cocos/2d/CCMenu.cpp | 2 + cocos/2d/CCNode.cpp | 61 +- cocos/2d/CCNode.h | 18 - cocos/2d/CCParticleBatchNode.cpp | 1 - cocos/2d/CCRenderTexture.cpp | 1 - cocos/2d/CCSpriteBatchNode.cpp | 1 - cocos/2d/platform/CCEGLViewProtocol.cpp | 6 +- .../editor-support/cocostudio/CCArmature.cpp | 1 - .../editor-support/cocostudio/CCBatchNode.cpp | 1 - extensions/GUI/CCScrollView/CCScrollView.cpp | 20 +- .../Classes/ActionsTest/ActionsTest.cpp | 2 +- .../Classes/Box2DTestBed/Box2dView.cpp | 20 +- .../TestCpp/Classes/Box2DTestBed/Box2dView.h | 3 +- .../Cpp/TestCpp/Classes/BugsTest/Bug-624.cpp | 16 +- .../Cpp/TestCpp/Classes/BugsTest/Bug-914.cpp | 12 +- .../Cpp/TestCpp/Classes/BugsTest/BugsTest.cpp | 2 +- .../Classes/ChipmunkTest/ChipmunkTest.cpp | 4 +- .../Classes/ChipmunkTest/ChipmunkTest.h | 4 +- .../ClickAndMoveTest/ClickAndMoveTest.cpp | 12 +- .../ClippingNodeTest/ClippingNodeTest.cpp | 24 +- .../CocosDenshionTest/CocosDenshionTest.cpp | 4 +- .../Cpp/TestCpp/Classes/CurlTest/CurlTest.cpp | 2 +- .../Cpp/TestCpp/Classes/CurlTest/CurlTest.h | 2 +- .../CocoStudioArmatureTest/ArmatureScene.cpp | 6 +- .../CocoStudioArmatureTest/ArmatureScene.h | 2 +- .../Classes/ExtensionsTest/ExtensionsTest.cpp | 2 +- .../Classes/KeyboardTest/KeyboardTest.cpp | 2 +- .../TestCpp/Classes/KeypadTest/KeypadTest.cpp | 2 +- .../TestCpp/Classes/KeypadTest/KeypadTest.h | 2 +- .../TestCpp/Classes/LabelTest/LabelTest.cpp | 2 +- .../Classes/LabelTest/LabelTestNew.cpp | 2 +- .../TestCpp/Classes/LayerTest/LayerTest.cpp | 4 +- .../Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp | 18 +- .../Cpp/TestCpp/Classes/MenuTest/MenuTest.h | 1 + .../MotionStreakTest/MotionStreakTest.cpp | 2 +- .../Classes/MutiTouchTest/MutiTouchTest.cpp | 2 +- .../NewEventDispatcherTest.cpp | 6 +- .../Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp | 2 +- .../Classes/ParallaxTest/ParallaxTest.cpp | 2 +- .../Classes/ParticleTest/ParticleTest.cpp | 2 +- .../PerformanceTouchesTest.cpp | 14 +- .../PerformanceTest/PerformanceTouchesTest.h | 16 +- .../Classes/PhysicsTest/PhysicsTest.cpp | 4 +- .../RenderTextureTest/RenderTextureTest.cpp | 6 +- .../SpriteTest/SpriteTest.cpp.REMOVED.git-id | 2 +- .../Classes/TextInputTest/TextInputTest.cpp | 4 +- .../Classes/TileMapTest/TileMapTest.cpp | 2 +- .../TestCpp/Classes/TouchesTest/Paddle.cpp | 2 +- samples/Cpp/TestCpp/Classes/controller.cpp | 4 +- 53 files changed, 763 insertions(+), 590 deletions(-) diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index 57a8d5ffde..7933a5edd6 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -31,7 +31,7 @@ #include -#define DUMP_LISTENER_ITEM_PRIORITY_INFO 0 +#define DUMP_LISTENER_ITEM_PRIORITY_INFO 1 namespace { @@ -61,155 +61,156 @@ NS_CC_BEGIN static EventDispatcher* g_instance = nullptr; static int s_eventPriorityIndex = 0; -void EventDispatcher::visitNode(Node* node) +void EventDispatcher::visitTarget(Node* node) { + // Reset priority index + s_eventPriorityIndex = 0; + _nodePriorityMap.clear(); + int i = 0; - int childrenCount = node->_children ? node->_children->count() : 0; + Array* children = node->getChildren(); + int childrenCount = children ? children->count() : 0; if(childrenCount > 0) { + Node* child = nullptr; // visit children zOrder < 0 for( ; i < childrenCount; i++ ) { - child = static_cast( node->_children->getObjectAtIndex(i) ); + child = static_cast( children->getObjectAtIndex(i) ); - if ( child && child->_ZOrder < 0 ) - visitNode(child); + if ( child && child->getZOrder() < 0 ) + visitTarget(child); else break; } - node->_eventPriority = ++s_eventPriorityIndex; + _nodePriorityMap.insert(std::make_pair(node, ++s_eventPriorityIndex)); for( ; i < childrenCount; i++ ) { - child = static_cast( node->_children->getObjectAtIndex(i) ); + child = static_cast( children->getObjectAtIndex(i) ); if (child) - visitNode(child); + visitTarget(child); } } else { - node->_eventPriority = ++s_eventPriorityIndex; + _nodePriorityMap.insert(std::make_pair(node, ++s_eventPriorityIndex)); } } -EventDispatcher::EventListenerItem::EventListenerItem() -: fixedPriority(0) -, listener(nullptr) -, node(nullptr) +void EventDispatcher::pauseTarget(Node* node) { - + auto listenerIter = _nodeListenersMap.find(node); + if (listenerIter != _nodeListenersMap.end()) + { + auto listeners = listenerIter->second; + for (auto& l : *listeners) + { + l->_paused = true; + } + } } -EventDispatcher::EventListenerItem::~EventListenerItem() +void EventDispatcher::resumeTarget(Node* node) +{ + auto listenerIter = _nodeListenersMap.find(node); + if (listenerIter != _nodeListenersMap.end()) + { + auto listeners = listenerIter->second; + for (auto& l : *listeners) + { + l->_paused = false; + } + } +} + +void EventDispatcher::cleanTarget(Node* node) +{ + auto listenerIter = _nodeListenersMap.find(node); + if (listenerIter != _nodeListenersMap.end()) + { + auto listeners = listenerIter->second; + for (auto& l : *listeners) + { + removeEventListener(l); + } + } +} + +EventDispatcher::EventListenerVector::EventListenerVector() +: _sceneGraphListeners(nullptr) +, _fixedListeners(nullptr) +, _gt0Index(0) { } -EventDispatcher::EventListenerItemVector::EventListenerItemVector() -: _sceneGraphItems(nullptr) -, _fixedItems(nullptr) +EventDispatcher::EventListenerVector::~EventListenerVector() { + CC_SAFE_DELETE(_sceneGraphListeners); + CC_SAFE_DELETE(_fixedListeners); } -EventDispatcher::EventListenerItemVector::~EventListenerItemVector() -{ - CC_SAFE_DELETE(_sceneGraphItems); - CC_SAFE_DELETE(_fixedItems); -} - -size_t EventDispatcher::EventListenerItemVector::size() const +size_t EventDispatcher::EventListenerVector::size() const { size_t ret = 0; - if (_sceneGraphItems) - ret += _sceneGraphItems->size(); - if (_fixedItems) - ret += _fixedItems->size(); + if (_sceneGraphListeners) + ret += _sceneGraphListeners->size(); + if (_fixedListeners) + ret += _fixedListeners->size(); return ret; } -bool EventDispatcher::EventListenerItemVector::empty() const +bool EventDispatcher::EventListenerVector::empty() const { - return (_sceneGraphItems == nullptr || _sceneGraphItems->empty()) - && (_fixedItems == nullptr || _fixedItems->empty()); + return (_sceneGraphListeners == nullptr || _sceneGraphListeners->empty()) + && (_fixedListeners == nullptr || _fixedListeners->empty()); } -void EventDispatcher::EventListenerItemVector::iterate(IterateCallback cb, EventDispatcher::EventListenerItemVector::IterateMode mode/* = IterateMode::ALL*/) +void EventDispatcher::EventListenerVector::push_back(EventListener* listener) { - auto loop = [&cb](std::vector* items) -> bool { - for (auto iter = items->begin(); iter != items->end(); ++iter) + if (listener->_fixedPriority == 0) + { + if (_sceneGraphListeners == nullptr) { - if (cb(iter, items)) - return false; + _sceneGraphListeners = new std::vector(); + _sceneGraphListeners->reserve(100); } - return true; - }; + + _sceneGraphListeners->push_back(listener); + } + else + { + if (_fixedListeners == nullptr) + { + _fixedListeners = new std::vector(); + _fixedListeners->reserve(100); + } + + _fixedListeners->push_back(listener); + } +} + +void EventDispatcher::EventListenerVector::clear() +{ + if (_sceneGraphListeners) + { + _sceneGraphListeners->clear(); + delete _sceneGraphListeners; + _sceneGraphListeners = nullptr; + } - switch (mode) + if (_fixedListeners) { - case IterateMode::FIXED_PRIORITY_LESS_THAN_0: - loop(_lt0); - break; - case IterateMode::SCENE_GRAPH_PRIORITY: - loop(_eq0); - break; - case IterateMode::FIXED_PRIORITY_GREATER_THAN_0: - loop(_gt0); - break; - case IterateMode::ALL: - { - if (!loop(_lt0)) return; - if (!loop(_eq0)) return; - if (!loop(_gt0)) return; - } - default: - break; + _fixedListeners->clear(); + delete _fixedListeners; + _fixedListeners = nullptr; } } -void EventDispatcher::EventListenerItemVector::push_back(EventDispatcher::EventListenerItem* item) -{ - if (item->fixedPriority == 0) - { - if (_sceneGraphItems == nullptr) - { - _sceneGraphItems = new std::vector(); - _sceneGraphItems->reserve(100); - } - - _sceneGraphItems->push_back(item); - } - else - { - if (_fixedItems == nullptr) - { - _fixedItems = new std::vector(); - _fixedItems->reserve(100); - } - - _fixedItems->push_back(item); - } -} - -void EventDispatcher::EventListenerItemVector::remove(EventDispatcher::EventListenerItem* item) -{ - if (item->fixedPriority < 0) - { - CCASSERT(_lt0, "vector invaild"); - - } - else if (item->fixedPriority == 0) - { - CCASSERT(_eq0, "vector invaild"); - - } - else - { - CCASSERT(_gt0, "vector invalid"); - } -} EventDispatcher::EventDispatcher() : _inDispatch(0) @@ -238,31 +239,75 @@ void EventDispatcher::destroyInstance() CC_SAFE_DELETE(g_instance); } -void EventDispatcher::addEventListenerWithItem(EventListenerItem* item) +void EventDispatcher::associateNodeAndEventListener(Node* node, EventListener* listener) +{ + std::vector* listeners = nullptr; + auto found = _nodeListenersMap.find(node); + if (found != _nodeListenersMap.end()) + { + listeners = found->second; + } + else + { + listeners = new std::vector(); + } + + listeners->push_back(listener); + + _nodeListenersMap.insert(std::make_pair(node, listeners)); +} + +void EventDispatcher::dissociateNodeAndEventListener(Node* node, EventListener* listener) +{ + std::vector* listeners = nullptr; + auto found = _nodeListenersMap.find(node); + if (found != _nodeListenersMap.end()) + { + listeners = found->second; + auto iter = std::find(listeners->begin(), listeners->end(), listener); + if (iter != listeners->end()) + { + listeners->erase(iter); + } + + if (listeners->empty()) + { + _nodeListenersMap.erase(found); + } + } +} + +void EventDispatcher::addEventListener(EventListener* listener) { if (_inDispatch == 0) { - EventListenerItemVector* listenerList = nullptr; + EventListenerVector* listenerList = nullptr; - auto iter = _listeners.find(item->listener->_type); + auto iter = _listeners.find(listener->_type); if (iter == _listeners.end()) { - listenerList = new EventListenerItemVector(); -// listenerList->reserve(100); - _listeners.insert(std::make_pair(item->listener->_type, listenerList)); + listenerList = new EventListenerVector(); + _listeners.insert(std::make_pair(listener->_type, listenerList)); } else { listenerList = iter->second; } - listenerList->push_back(item); - - setDirtyForEventType(item->listener->_type, true); + listenerList->push_back(listener); + + if (listener->_fixedPriority == 0) + { + setDirtyForEventType(listener->_type, DirtyFlag::SCENE_GRAPH_PRIORITY); + } + else + { + setDirtyForEventType(listener->_type, DirtyFlag::FIXED_PRITORY); + } } else { - _toAddedListeners.push_back(item); + _toAddedListeners.push_back(listener); } } @@ -274,17 +319,21 @@ void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* list if (!listener->checkAvailable()) return; - auto item = new EventListenerItem(); - item->node = node; - item->node->retain(); - item->fixedPriority = 0; - item->listener = listener; - item->listener->retain(); - item->listener->_isRegistered = true; + listener->_node = node; +// listener->_node->retain(); + listener->_fixedPriority = 0; - addEventListenerWithItem(item); + listener->retain(); + listener->_isRegistered = true; - node->associateEventListener(listener); + addEventListener(listener); + + associateNodeAndEventListener(node, listener); + + if (node->isRunning()) + { + resumeTarget(node); + } } void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority) @@ -296,14 +345,13 @@ void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, if (!listener->checkAvailable()) return; - auto item = new EventListenerItem(); - item->node = nullptr; - item->fixedPriority = fixedPriority; - item->listener = listener; - item->listener->retain(); - item->listener->_isRegistered = true; + listener->_node = nullptr; + listener->_fixedPriority = fixedPriority; + listener->retain(); + listener->_isRegistered = true; + listener->_paused = false; - addEventListenerWithItem(item); + addEventListener(listener); } void EventDispatcher::removeEventListener(EventListener* listener) @@ -313,37 +361,45 @@ void EventDispatcher::removeEventListener(EventListener* listener) bool isFound = false; - for (auto iter = _listeners.begin(); iter != _listeners.end();) - { - iter->second->iterate([&](std::vector::iterator itemIt, std::vector* current) -> bool { + auto removeListenerInVector = [&](std::vector* listeners){ + if (listeners == nullptr) + return; - auto item = *itemIt; - if (item->listener == listener) + for (auto iter = listeners->begin(); iter != listeners->end(); ++iter) + { + auto l = *iter; + if (l == listener) { CC_SAFE_RETAIN(listener); - item->listener->_isRegistered = false; - if (item->node != nullptr) + l->_isRegistered = false; + if (l->_node != nullptr) { - item->node->dissociateEventListener(listener); + dissociateNodeAndEventListener(l->_node, listener); } - item->listener->release(); + l->release(); if (_inDispatch == 0) { - current->erase(itemIt); - delete item; - } - else - { - item->listener = nullptr; + listeners->erase(iter); } isFound = true; - return false; + break; } - - return true; - }); + } + }; + + for (auto iter = _listeners.begin(); iter != _listeners.end();) + { + auto listeners = iter->second; + auto fixedPriorityListeners = listeners->getFixedPriorityListeners(); + auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners(); + + removeListenerInVector(sceneGraphPriorityListeners); + if (!isFound) + { + removeListenerInVector(fixedPriorityListeners); + } if (iter->second->empty()) { @@ -374,17 +430,18 @@ void EventDispatcher::setPriority(EventListener* listener, int fixedPriority) for (auto iter = _listeners.begin(); iter != _listeners.end(); ++iter) { - for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter) + auto fixedPriorityListeners = iter->second->getFixedPriorityListeners(); + if (fixedPriorityListeners) { - auto item = *itemIter; - if (item->listener == listener) + auto found = std::find(fixedPriorityListeners->begin(), fixedPriorityListeners->end(), listener); + if (found != fixedPriorityListeners->end()) { - CCASSERT(item->node, "Can't set fixed priority with scene graph based listener."); + CCASSERT(listener->_node, "Can't set fixed priority with scene graph based listener."); - if (item->fixedPriority != fixedPriority) + if (listener->_fixedPriority != fixedPriority) { - item->fixedPriority = fixedPriority; - setDirtyForEventType(listener->_type, true); + listener->_fixedPriority = fixedPriority; + setDirtyForEventType(listener->_type, DirtyFlag::FIXED_PRITORY); } return; } @@ -392,59 +449,151 @@ void EventDispatcher::setPriority(EventListener* listener, int fixedPriority) } } -void EventDispatcher::dispatchEvent(Event* event, bool forceSortListeners) +void EventDispatcher::dispatchEventToListeners(EventListenerVector* listeners, std::function onEvent) +{ + bool shouldStopPropagation = false; + auto fixedPriorityListeners = listeners->getFixedPriorityListeners(); + auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners(); + + int i = 0; + // priority < 0 + if (fixedPriorityListeners) + { + for (; !fixedPriorityListeners->empty() && i < listeners->getGt0Index(); ++i) + { + auto l = fixedPriorityListeners->at(i); + if (!l->isPaused() && onEvent(l)) + { + shouldStopPropagation = true; + break; + } + } + } + + if (sceneGraphPriorityListeners) + { + if (!shouldStopPropagation) + { + // priority == 0, scene graph priority + for (auto& listener : *sceneGraphPriorityListeners) + { + if (!listener->isPaused() && onEvent(listener)) + { + shouldStopPropagation = true; + break; + } + } + } + } + + if (fixedPriorityListeners) + { + if (!shouldStopPropagation) + { + // priority > 0 + for (; i < fixedPriorityListeners->size(); ++i) + { + auto l = fixedPriorityListeners->at(i); + + if (!l->isPaused() && onEvent(fixedPriorityListeners->at(i))) + { + shouldStopPropagation = true; + break; + } + } + } + } +} + +void EventDispatcher::dispatchEvent(Event* event) { if (!_isEnabled) return; - bool isDirty = false; + updateDirtyFlagForSceneGraph(); + + DirtyFlag dirtyFlag = DirtyFlag::NONE; + auto dirtyIter = _priorityDirtyFlagMap.find(event->_type); if (dirtyIter != _priorityDirtyFlagMap.end()) { - isDirty = dirtyIter->second; + dirtyFlag = dirtyIter->second; } - if (forceSortListeners || isDirty) + if (dirtyFlag != DirtyFlag::NONE) { - sortAllEventListenerItemsForType(event->_type); - // Sets the dirty flag to false - if (isDirty) + if ((int)dirtyFlag & (int)DirtyFlag::FIXED_PRITORY) { - dirtyIter->second = false; + sortEventListenersOfFixedPriority(event->_type); } + + if ((int)dirtyFlag & (int)DirtyFlag::SCENE_GRAPH_PRIORITY) + { + sortEventListenersOfSceneGraphPriority(event->_type); + } + + dirtyIter->second = DirtyFlag::NONE; } - + DispatchGuard guard(_inDispatch); - - if (event->_type == EventTouch::EVENT_TYPE) - { - dispatchTouchEvent(static_cast(event)); - return; - } auto iter = _listeners.find(event->getType()); if (iter != _listeners.end()) { - auto listenerList = iter->second; - for (auto& item : *listenerList) - { - CCASSERT(item, "listener item is invalid."); - - event->setCurrentTarget(item->node); - item->listener->_onEvent(event); - - if (event->isStopped()) - break; - } + auto listeners = iter->second; + + auto onEvent = [&event](EventListener* listener) -> bool{ + event->setCurrentTarget(listener->_node); + listener->_onEvent(event); + return event->isStopped(); + }; + + dispatchEventToListeners(listeners, onEvent); } - updateListenerItems(); + updateListeners(); } void EventDispatcher::dispatchTouchEvent(EventTouch* event) { - auto oneByOnelisteners = getListenerItemsForType(EventTouch::MODE_ONE_BY_ONE); - auto allAtOncelisteners = getListenerItemsForType(EventTouch::MODE_ALL_AT_ONCE); + if (!_isEnabled) + return; + + updateDirtyFlagForSceneGraph(); + + auto sortTouchListeners = [this](const std::string& type){ + + DirtyFlag dirtyFlag = DirtyFlag::NONE; + auto dirtyIter = _priorityDirtyFlagMap.find(type); + if (dirtyIter != _priorityDirtyFlagMap.end()) + { + dirtyFlag = dirtyIter->second; + } + + if (dirtyFlag != DirtyFlag::NONE) + { + if ((int)dirtyFlag & (int)DirtyFlag::FIXED_PRITORY) + { + sortEventListenersOfFixedPriority(type); + } + + if ((int)dirtyFlag & (int)DirtyFlag::SCENE_GRAPH_PRIORITY) + { + sortEventListenersOfSceneGraphPriority(type); + } + + dirtyIter->second = DirtyFlag::NONE; + } + + }; + + sortTouchListeners(EventTouch::MODE_ONE_BY_ONE); + sortTouchListeners(EventTouch::MODE_ALL_AT_ONCE); + + DispatchGuard guard(_inDispatch); + + auto oneByOnelisteners = getListeners(EventTouch::MODE_ONE_BY_ONE); + auto allAtOncelisteners = getListeners(EventTouch::MODE_ALL_AT_ONCE); // If there aren't any touch listeners, return directly. if (nullptr == oneByOnelisteners && nullptr == allAtOncelisteners) @@ -455,7 +604,7 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) std::vector orignalTouches = event->getTouches(); std::vector mutableTouches(orignalTouches.size()); std::copy(orignalTouches.begin(), orignalTouches.end(), mutableTouches.begin()); - + // // process the target handlers 1st // @@ -468,62 +617,63 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) { bool isSwallowed = false; - for (auto& item : *oneByOnelisteners) - { + auto onTouchEvent = [&](EventListener* l) -> bool { // Return true to break + EventListenerTouchOneByOne* listener = static_cast(l); + // Skip if the listener was removed. - if (item->listener == nullptr) - continue; + if (!listener->_isRegistered) + return false; - event->setCurrentTarget(item->node); + event->setCurrentTarget(listener->_node); bool isClaimed = false; std::vector::iterator removedIter; - auto touchEventListener = static_cast(item->listener); EventTouch::EventCode eventCode = event->getEventCode(); if (eventCode == EventTouch::EventCode::BEGAN) { - if (touchEventListener->onTouchBegan) + if (listener->onTouchBegan) { - isClaimed = touchEventListener->onTouchBegan(*touchesIter, event); - if (isClaimed && item->listener) + CCLOG("onTouchBegan..."); + isClaimed = listener->onTouchBegan(*touchesIter, event); + if (isClaimed && listener->_isRegistered) { - touchEventListener->_claimedTouches.push_back(*touchesIter); + listener->_claimedTouches.push_back(*touchesIter); } } } - else if (touchEventListener->_claimedTouches.size() > 0 - && ((removedIter = std::find(touchEventListener->_claimedTouches.begin(), touchEventListener->_claimedTouches.end(), *touchesIter)) != touchEventListener->_claimedTouches.end())) + else if (listener->_claimedTouches.size() > 0 + && ((removedIter = std::find(listener->_claimedTouches.begin(), listener->_claimedTouches.end(), *touchesIter)) != listener->_claimedTouches.end())) { isClaimed = true; switch (eventCode) { case EventTouch::EventCode::MOVED: - if (touchEventListener->onTouchMoved) + if (listener->onTouchMoved) { - touchEventListener->onTouchMoved(*touchesIter, event); + listener->onTouchMoved(*touchesIter, event); } break; case EventTouch::EventCode::ENDED: - if (touchEventListener->onTouchEnded) + if (listener->onTouchEnded) { - touchEventListener->onTouchEnded(*touchesIter, event); + listener->onTouchEnded(*touchesIter, event); } - if (item->listener) + if (listener->_isRegistered) { - touchEventListener->_claimedTouches.erase(removedIter); + listener->_claimedTouches.erase(removedIter); } break; case EventTouch::EventCode::CANCELLED: - if (touchEventListener->onTouchCancelled) + if (listener->onTouchCancelled) { - touchEventListener->onTouchCancelled(*touchesIter, event); + listener->onTouchCancelled(*touchesIter, event); } - if (item->listener) + if (listener->_isRegistered) { - touchEventListener->_claimedTouches.erase(removedIter); + listener->_claimedTouches.erase(removedIter); } break; default: @@ -535,21 +685,30 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) // If the event was stopped, return directly. if (event->isStopped()) { - updateListenerItems(); - return; + updateListeners(); + return true; } CCASSERT((*touchesIter)->getID() == (*mutableTouchesIter)->getID(), ""); - if (isClaimed && item->listener && touchEventListener->_needSwallow) + if (isClaimed && listener->_isRegistered && listener->_needSwallow) { if (isNeedsMutableSet) { mutableTouchesIter = mutableTouches.erase(mutableTouchesIter); isSwallowed = true; } - break; + return true; } + + return false; + }; + + // + dispatchEventToListeners(oneByOnelisteners, onTouchEvent); + if (event->isStopped()) + { + return; } if (!isSwallowed) @@ -562,40 +721,39 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) // if (allAtOncelisteners && mutableTouches.size() > 0) { - for (auto& item : *allAtOncelisteners) - { + + auto onTouchesEvent = [&](EventListener* l) -> bool{ + EventListenerTouchAllAtOnce* listener = static_cast(l); // Skip if the listener was removed. - if (item->listener == nullptr) - continue; + if (!listener->_isRegistered) + return false; - event->setCurrentTarget(item->node); - - auto touchEventListener = static_cast(item->listener); + event->setCurrentTarget(listener->_node); switch (event->getEventCode()) { case EventTouch::EventCode::BEGAN: - if (touchEventListener->onTouchesBegan) + if (listener->onTouchesBegan) { - touchEventListener->onTouchesBegan(mutableTouches, event); + listener->onTouchesBegan(mutableTouches, event); } break; case EventTouch::EventCode::MOVED: - if (touchEventListener->onTouchesMoved) + if (listener->onTouchesMoved) { - touchEventListener->onTouchesMoved(mutableTouches, event); + listener->onTouchesMoved(mutableTouches, event); } break; case EventTouch::EventCode::ENDED: - if (touchEventListener->onTouchesEnded) + if (listener->onTouchesEnded) { - touchEventListener->onTouchesEnded(mutableTouches, event); + listener->onTouchesEnded(mutableTouches, event); } break; case EventTouch::EventCode::CANCELLED: - if (touchEventListener->onTouchesCancelled) + if (listener->onTouchesCancelled) { - touchEventListener->onTouchesCancelled(mutableTouches, event); + listener->onTouchesCancelled(mutableTouches, event); } break; default: @@ -606,120 +764,196 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) // If the event was stopped, return directly. if (event->isStopped()) { - updateListenerItems(); - return; + updateListeners(); + return false; } + + return false; + }; + + dispatchEventToListeners(allAtOncelisteners, onTouchesEvent); + if (event->isStopped()) + { + return; } } - updateListenerItems(); + updateListeners(); } -void EventDispatcher::updateListenerItems() +void EventDispatcher::updateListeners() { - auto listenerItemIter = _listeners.begin(); - while (listenerItemIter != _listeners.end()) + auto listenersIter = _listeners.begin(); + while (listenersIter != _listeners.end()) { - for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end();) + auto listeners = listenersIter->second; + auto fixedPriorityListeners = listeners->getFixedPriorityListeners(); + auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners(); + + if (sceneGraphPriorityListeners) { - if ((*iter)->listener == nullptr) + for (auto iter = sceneGraphPriorityListeners->begin(); iter != sceneGraphPriorityListeners->end();) { - delete (*iter); - iter = listenerItemIter->second->erase(iter); - } - else - { - ++iter; + auto l = *iter; + if (!l->_isRegistered) + { + iter = sceneGraphPriorityListeners->erase(iter); + } + else + { + ++iter; + } } } - if (listenerItemIter->second->empty()) + if (fixedPriorityListeners) { - _priorityDirtyFlagMap.erase(listenerItemIter->first); - delete listenerItemIter->second; - listenerItemIter = _listeners.erase(listenerItemIter); + for (auto iter = fixedPriorityListeners->begin(); iter != fixedPriorityListeners->end();) + { + auto l = *iter; + if (!l->_isRegistered) + { + iter = fixedPriorityListeners->erase(iter); + } + else + { + ++iter; + } + } + } + + if (listenersIter->second->empty()) + { + _priorityDirtyFlagMap.erase(listenersIter->first); + delete listenersIter->second; + listenersIter = _listeners.erase(listenersIter); } else { - ++listenerItemIter; + ++listenersIter; } } if (!_toAddedListeners.empty()) { - std::vector* listenerList = nullptr; + EventListenerVector* listeners = nullptr; - for (auto& item : _toAddedListeners) + for (auto& listener : _toAddedListeners) { - auto itr = _listeners.find(item->listener->_type); + auto itr = _listeners.find(listener->_type); if (itr == _listeners.end()) { - listenerList = new std::vector(); - listenerList->reserve(100); - _listeners.insert(std::make_pair(item->listener->_type, listenerList)); + + listeners = new EventListenerVector(); + _listeners.insert(std::make_pair(listener->_type, listeners)); } else { - listenerList = itr->second; + listeners = itr->second; } - listenerList->push_back(item); + listeners->push_back(listener); - setDirtyForEventType(item->listener->_type, true); + if (listener->_fixedPriority == 0) + { + setDirtyForEventType(listener->_type, DirtyFlag::SCENE_GRAPH_PRIORITY); + } + else + { + setDirtyForEventType(listener->_type, DirtyFlag::FIXED_PRITORY); + } } _toAddedListeners.clear(); } } -void EventDispatcher::sortAllEventListenerItemsForType(const std::string &eventType) +void EventDispatcher::updateDirtyFlagForSceneGraph() +{ + if (!_dirtyNodes.empty()) + { + for (auto& node : _dirtyNodes) + { + auto iter = _nodeListenersMap.find(node); + if (iter != _nodeListenersMap.end()) + { + for (auto& l : *iter->second) + { + setDirtyForEventType(l->_type, DirtyFlag::SCENE_GRAPH_PRIORITY); + } + } + } + + _dirtyNodes.clear(); + } +} + +void EventDispatcher::sortEventListenersOfSceneGraphPriority(const std::string& eventType) { if (eventType.empty()) return; - auto listenerList = getListenerItemsForType(eventType); - - if (listenerList == nullptr) + auto listeners = getListeners(eventType); + + if (listeners == nullptr) return; - // After sort: priority < 0, = 0, scene graph, > 0 - std::sort(listenerList->begin(), listenerList->end(), [](const EventListenerItem* item1, const EventListenerItem* item2) { - - // item1 and item2 are both using fixed priority. - if (nullptr == item1->node && nullptr == item2->node) - { - return item1->fixedPriority < item2->fixedPriority; - } - // item1 and item2 are both using scene graph based priority. - else if (nullptr != item1->node && nullptr != item2->node) - { - return item1->node->getEventPriority() > item2->node->getEventPriority(); - } - else if (nullptr != item1->node && nullptr == item2->node) - { - return 0 < item2->fixedPriority; - } - else if (nullptr == item1->node && nullptr != item2->node) - { - return item1->fixedPriority <= 0; - } - else - { - CCASSERT(false, "sort event node error..."); - return false; - } + Node* rootNode = (Node*)Director::getInstance()->getRunningScene(); + + visitTarget(rootNode); + + // After sort: priority < 0, > 0 + auto sceneGraphlisteners = listeners->getSceneGraphPriorityListeners(); + std::sort(sceneGraphlisteners->begin(), sceneGraphlisteners->end(), [this](const EventListener* l1, const EventListener* l2) { + return _nodePriorityMap[l1->_node] >= _nodePriorityMap[l2->_node]; }); #if DUMP_LISTENER_ITEM_PRIORITY_INFO log("-----------------------------------"); - for (auto& item : *listenerList) + for (auto& l : *sceneGraphlisteners) { - log("listener item priority: node (%p), fixed (%d)", item->node, item->fixedPriority); + log("listener priority: node ([%s]%p), fixed (%d)", typeid(*l->_node).name(), l->_node, l->_fixedPriority); + } +#endif +} + +void EventDispatcher::sortEventListenersOfFixedPriority(const std::string &eventType) +{ + if (eventType.empty()) + return; + + auto listeners = getListeners(eventType); + + if (listeners == nullptr) + return; + + // After sort: priority < 0, > 0 + auto fixedlisteners = listeners->getFixedPriorityListeners(); + std::sort(fixedlisteners->begin(), fixedlisteners->end(), [](const EventListener* l1, const EventListener* l2) { + return l1->_fixedPriority < l2->_fixedPriority; + }); + + // FIXME: Should use binary search + int index = 0; + for (auto& listener : *fixedlisteners) + { + if (listener->_fixedPriority >= 0) + break; + ++index; + } + + listeners->setGt0Index(index); + +#if DUMP_LISTENER_ITEM_PRIORITY_INFO + log("-----------------------------------"); + for (auto& l : *fixedlisteners) + { + log("listener priority: node (%p), fixed (%d)", l->_node, l->_fixedPriority); } #endif } -std::vector* EventDispatcher::getListenerItemsForType(const std::string &eventType) +EventDispatcher::EventListenerVector* EventDispatcher::getListeners(const std::string &eventType) { auto iter = _listeners.find(eventType); if (iter != _listeners.end()) @@ -738,29 +972,42 @@ void EventDispatcher::removeListenersForEventType(const std::string& eventType) auto listenerItemIter = _listeners.find(eventType); if (listenerItemIter != _listeners.end()) { - for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end(); ++iter) - { - (*iter)->listener->_isRegistered = false; - if ((*iter)->node != nullptr) - { - (*iter)->node->dissociateEventListener((*iter)->listener); - } + auto listeners = listenerItemIter->second; + auto fixedPriorityListeners = listeners->getFixedPriorityListeners(); + auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners(); + + auto removeAllListenersInVector = [&](std::vector* listenerVector){ + if (listenerVector == nullptr) + return; - (*iter)->listener->release(); - if (_inDispatch) + for (auto iter = listenerVector->begin(); iter != listenerVector->end();) { - (*iter)->listener = nullptr; + auto l = *iter; + l->_isRegistered = false; + if (l->_node != nullptr) + { + dissociateNodeAndEventListener(l->_node, l); + } + + l->release(); + if (_inDispatch == 0) + { + iter = listenerVector->erase(iter); + } + else + { + ++iter; + } } - else - { - delete (*iter); - } - } + }; + + removeAllListenersInVector(sceneGraphPriorityListeners); + removeAllListenersInVector(fixedPriorityListeners); if (!_inDispatch) { - listenerItemIter->second->clear(); - delete listenerItemIter->second; + listeners->clear(); + delete listeners; _listeners.erase(listenerItemIter); _priorityDirtyFlagMap.erase(eventType); } @@ -769,36 +1016,18 @@ void EventDispatcher::removeListenersForEventType(const std::string& eventType) void EventDispatcher::removeAllListeners() { - for (auto listenerItemIter = _listeners.begin(); listenerItemIter != _listeners.end(); ++listenerItemIter) + std::vector types(_listeners.size()); + + for (auto iter = _listeners.begin(); iter != _listeners.end(); ++iter) { - for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end(); ++iter) - { - (*iter)->listener->_isRegistered = false; - if ((*iter)->node != nullptr) - { - (*iter)->node->dissociateEventListener((*iter)->listener); - } - - (*iter)->listener->release(); - if (_inDispatch) - { - (*iter)->listener = nullptr; - } - else - { - delete (*iter); - } - } - - if (!_inDispatch) - { - listenerItemIter->second->clear(); - delete listenerItemIter->second; - - _priorityDirtyFlagMap.clear(); - } + types.push_back(iter->first); } + for (auto& type : types) + { + removeListenersForEventType(type); + } + if (!_inDispatch) { _listeners.clear(); @@ -816,31 +1045,37 @@ bool EventDispatcher::isEnabled() const return _isEnabled; } -void EventDispatcher::setDirtyForEventType(const std::string& eventType, bool isDirty) +void EventDispatcher::setDirtyForNode(Node* node) +{ + _dirtyNodes.insert(node); +} + +void EventDispatcher::setDirtyForEventType(const std::string& eventType, DirtyFlag flag) { CCASSERT(!eventType.empty(), "Invalid event type."); auto iter = _priorityDirtyFlagMap.find(eventType); if (iter == _priorityDirtyFlagMap.end()) { - _priorityDirtyFlagMap.insert(std::make_pair(eventType, isDirty)); + _priorityDirtyFlagMap.insert(std::make_pair(eventType, flag)); } else { - iter->second = isDirty; + int ret = (int)flag | (int)iter->second; + iter->second = (DirtyFlag) ret; } } -bool EventDispatcher::isDirtyForEventType(const std::string& eventType) +EventDispatcher::DirtyFlag EventDispatcher::isDirtyForEventType(const std::string& eventType) { - bool isDirty = false; + DirtyFlag flag = DirtyFlag::NONE; auto iter = _priorityDirtyFlagMap.find(eventType); if (iter != _priorityDirtyFlagMap.end()) { - isDirty = iter->second; + flag = iter->second; } - return isDirty; + return flag; } NS_CC_END diff --git a/cocos/2d/CCEventDispatcher.h b/cocos/2d/CCEventDispatcher.h index 04191ea6d8..dc70491075 100644 --- a/cocos/2d/CCEventDispatcher.h +++ b/cocos/2d/CCEventDispatcher.h @@ -98,15 +98,31 @@ public: * Also removes all EventListeners marked for deletion from the * event dispatcher list. */ - void dispatchEvent(Event* event, bool forceSortListeners = false); - - void sortSceneGraphListeners(const std::string& eventType); + void dispatchEvent(Event* event); - void setDirtyForEventType(const std::string& eventType, bool isDirty); + /** Touch event needs to be processed different with other events since it needs support ALL_AT_ONCE and ONE_BY_NONE mode. */ + void dispatchTouchEvent(EventTouch* event); - bool isDirtyForEventType(const std::string& eventType); + /// Priority dirty flag + enum class DirtyFlag + { + NONE = 0, + FIXED_PRITORY = 1 << 0, + SCENE_GRAPH_PRIORITY = 1 << 1, + ALL = FIXED_PRITORY | SCENE_GRAPH_PRIORITY + }; - void visitNode(Node* node); + void setDirtyForNode(Node* node); + + void setDirtyForEventType(const std::string& eventType, DirtyFlag flag); + + DirtyFlag isDirtyForEventType(const std::string& eventType); + + void visitTarget(Node* node); + + void pauseTarget(Node* node); + void resumeTarget(Node* node); + void cleanTarget(Node* node); public: /** Destructor of EventDispatcher */ @@ -114,75 +130,68 @@ public: private: - struct EventListenerItem - { - int fixedPriority; // The higher the number, the higher the priority, 0 is for scene graph base priority. - Node* node; // Weak reference. - EventListener* listener; - - EventListenerItem(); - ~EventListenerItem(); - }; - - - class EventListenerItemVector + class EventListenerVector { public: - EventListenerItemVector(); - ~EventListenerItemVector(); + EventListenerVector(); + ~EventListenerVector(); size_t size() const; bool empty() const; - enum class IterateMode - { - FIXED_PRIORITY_LESS_THAN_0, - SCENE_GRAPH_PRIORITY, - FIXED_PRIORITY_GREATER_THAN_0, - ALL - }; - - typedef std::function::iterator, std::vector*)> IterateCallback; - - void iterate(IterateCallback cb, IterateMode mode = IterateMode::ALL); - void push_back(EventListenerItem* item); - void remove(EventListenerItem* item); + void push_back(EventListener* item); + void clear(); + inline std::vector* getFixedPriorityListeners() const { return _fixedListeners; }; + inline std::vector* getSceneGraphPriorityListeners() const { return _sceneGraphListeners; }; + inline int getGt0Index() const { return _gt0Index; }; + inline void setGt0Index(int index) { _gt0Index = index; }; private: - std::vector* _fixedItems; - std::vector* _sceneGraphItems; + std::vector* _fixedListeners; + std::vector* _sceneGraphListeners; + int _gt0Index; }; /** Constructor of EventDispatcher */ EventDispatcher(); /** Adds event listener with item */ - void addEventListenerWithItem(EventListenerItem* item); - - /** Touch event needs to be processed different with other events since it needs support ALL_AT_ONCE and ONE_BY_NONE mode. */ - void dispatchTouchEvent(EventTouch* event); + void addEventListener(EventListener* listener); /** Gets event the listener list for the event type. */ - EventListenerItemVector* getListenerItemsForType(const std::string& eventType); + EventListenerVector* getListeners(const std::string& eventType); - /** Sorts the listeners of specified type by priority */ - void sortAllEventListenerItemsForType(const std::string& eventType); + void updateDirtyFlagForSceneGraph(); - /** Updates all listener items + /** Sorts the listeners of specified type by scene graph priority */ + void sortEventListenersOfSceneGraphPriority(const std::string& eventType); + + /** Sorts the listeners of specified type by fixed priority */ + void sortEventListenersOfFixedPriority(const std::string& eventType); + + /** Updates all listeners * 1) Removes all listener items that have been marked as 'removed' when dispatching event. * 2) Adds all listener items that have been marked as 'added' when dispatching event. */ - void updateListenerItems(); + void updateListeners(); + void associateNodeAndEventListener(Node* node, EventListener* listener); + void dissociateNodeAndEventListener(Node* node, EventListener* listener); + + void dispatchEventToListeners(EventListenerVector* listeners, std::function onEvent); + private: /** * Listeners map. */ - std::map _listeners; + std::map _listeners; - /// Priority dirty flag - std::map _priorityDirtyFlagMap; + std::map _priorityDirtyFlagMap; - std::vector _toAddedListeners; + std::map*> _nodeListenersMap; + std::map _nodePriorityMap; + + std::vector _toAddedListeners; + std::set _dirtyNodes; int _inDispatch; ///< Whether it's in dispatching event bool _isEnabled; ///< Whether to enable dispatching event diff --git a/cocos/2d/CCEventListener.cpp b/cocos/2d/CCEventListener.cpp index 451be9a8c8..8b34ca5173 100644 --- a/cocos/2d/CCEventListener.cpp +++ b/cocos/2d/CCEventListener.cpp @@ -40,6 +40,7 @@ bool EventListener::init(const std::string& t, std::function callb _onEvent = callback; _type = t; _isRegistered = false; + _paused = true; return true; } diff --git a/cocos/2d/CCEventListener.h b/cocos/2d/CCEventListener.h index 982531416c..f68f9694f9 100644 --- a/cocos/2d/CCEventListener.h +++ b/cocos/2d/CCEventListener.h @@ -31,10 +31,12 @@ #include #include #include +#include NS_CC_BEGIN class Event; +class Node; /** * The base class of event listener. @@ -58,13 +60,21 @@ public: /** Clones the listener, its subclasses have to override this method. */ virtual EventListener* clone() = 0; + + inline bool isPaused() const { return _paused; }; + protected: std::function _onEvent; /// Event callback function std::string _type; /// Event type bool _isRegistered; /// Whether the listener has been added to dispatcher. + // The priority of event listener + int _fixedPriority; // The higher the number, the higher the priority, 0 is for scene graph base priority. + Node* _node; // scene graph based priority + bool _paused; + +private: friend class EventDispatcher; - friend class Node; }; NS_CC_END diff --git a/cocos/2d/CCMenu.cpp b/cocos/2d/CCMenu.cpp index fc38405355..3105a2d8a3 100644 --- a/cocos/2d/CCMenu.cpp +++ b/cocos/2d/CCMenu.cpp @@ -188,6 +188,8 @@ void Menu::onEnter() auto eventDispatcher = EventDispatcher::getInstance(); auto touchListener = EventListenerTouchOneByOne::create(); + touchListener->setSwallowTouches(true); + touchListener->onTouchBegan = CC_CALLBACK_2(Menu::onTouchBegan, this); touchListener->onTouchMoved = CC_CALLBACK_2(Menu::onTouchMoved, this); touchListener->onTouchEnded = CC_CALLBACK_2(Menu::onTouchEnded, this); diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index aad3d3fe6d..8b9d41f9ec 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -91,8 +91,7 @@ bool nodeComparisonLess(Object* p1, Object* p2) static int s_globalOrderOfArrival = 1; Node::Node(void) -: _eventPriority(0) -, _rotationX(0.0f) +: _rotationX(0.0f) , _rotationY(0.0f) , _scaleX(1.0f) , _scaleY(1.0f) @@ -134,11 +133,6 @@ Node::Node(void) , _physicsBody(nullptr) #endif { - onEnterHook = nullptr; - onEnterTransitionDidFinishHook = nullptr; - onExitHook = nullptr; - onExitTransitionDidStartHook = nullptr; - // set default scheduler and actionManager Director *director = Director::getInstance(); _actionManager = director->getActionManager(); @@ -188,7 +182,7 @@ Node::~Node() CC_SAFE_DELETE(_componentContainer); - removeAllEventListeners(); + EventDispatcher::getInstance()->cleanTarget(this); #ifdef CC_USE_PHYSICS CC_SAFE_RELEASE(_physicsBody); @@ -243,6 +237,8 @@ void Node::setZOrder(int z) { _parent->reorderChild(this, z); } + + EventDispatcher::getInstance()->setDirtyForNode(this); } /// vertexZ getter @@ -643,6 +639,8 @@ void Node::addChild(Node *child, int zOrder, int tag) child->onEnterTransitionDidFinish(); } } + + EventDispatcher::getInstance()->setDirtyForNode(this); } void Node::addChild(Node *child, int zOrder) @@ -936,7 +934,8 @@ void Node::onEnter() arrayMakeObjectsPerformSelector(_children, onEnter, Node*); this->resumeSchedulerAndActions(); - + EventDispatcher::getInstance()->resumeTarget(this); + _running = true; if (_scriptType != kScriptTypeNone) @@ -946,11 +945,6 @@ void Node::onEnter() ScriptEvent scriptEvent(kNodeEvent,(void*)&data); ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent); } - - if (onEnterHook) - { - onEnterHook(); - } } void Node::onEnterTransitionDidFinish() @@ -966,20 +960,10 @@ void Node::onEnterTransitionDidFinish() ScriptEvent scriptEvent(kNodeEvent,(void*)&data); ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent); } - - if (onEnterTransitionDidFinishHook) - { - onEnterTransitionDidFinishHook(); - } } void Node::onExitTransitionDidStart() { - if (onExitTransitionDidStartHook) - { - onExitTransitionDidStartHook(); - } - arrayMakeObjectsPerformSelector(_children, onExitTransitionDidStart, Node*); if (_scriptType != kScriptTypeNone) { @@ -992,10 +976,7 @@ void Node::onExitTransitionDidStart() void Node::onExit() { - if (onExitHook) - { - onExitHook(); - } + EventDispatcher::getInstance()->pauseTarget(this); this->pauseSchedulerAndActions(); @@ -1009,8 +990,6 @@ void Node::onExit() } arrayMakeObjectsPerformSelector(_children, onExit, Node*); - - removeAllEventListeners(); } void Node::setActionManager(ActionManager* actionManager) @@ -1363,28 +1342,6 @@ void Node::removeAllComponents() _componentContainer->removeAll(); } -void Node::associateEventListener(EventListener* listener) -{ - _eventlisteners.insert(listener); -} - -void Node::dissociateEventListener(EventListener* listener) -{ - _eventlisteners.erase(listener); -} - -void Node::removeAllEventListeners() -{ - auto dispatcher = EventDispatcher::getInstance(); - - auto eventListenersCopy = _eventlisteners; - - for (auto& listener : eventListenersCopy) - { - dispatcher->removeEventListener(listener); - } -} - #ifdef CC_USE_PHYSICS void Node::setPhysicsBody(PhysicsBody* body) { diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index b5c42f2301..fcc38b23b5 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -928,7 +928,6 @@ public: * @lua NA */ virtual void onEnter(); - std::function onEnterHook; /** Event callback that is invoked when the Node enters in the 'stage'. * If the Node enters the 'stage' with a transition, this event is called when the transition finishes. @@ -937,7 +936,6 @@ public: * @lua NA */ virtual void onEnterTransitionDidFinish(); - std::function onEnterTransitionDidFinishHook; /** * Event callback that is invoked every time the Node leaves the 'stage'. @@ -948,7 +946,6 @@ public: * @lua NA */ virtual void onExit(); - std::function onExitHook; /** * Event callback that is called every time the Node leaves the 'stage'. @@ -957,7 +954,6 @@ public: * @lua NA */ virtual void onExitTransitionDidStart(); - std::function onExitTransitionDidStartHook; /// @} end of event callbacks. @@ -1401,23 +1397,9 @@ public: virtual void updatePhysicsTransform(); #endif - - -private: - friend class Director; - friend class EventDispatcher; - - void associateEventListener(EventListener* listener); - void dissociateEventListener(EventListener* listener); - - std::set _eventlisteners; - int _eventPriority; protected: - /// Removes all event listeners that associated with this node. - void removeAllEventListeners(); - /// lazy allocs void childrenAlloc(void); diff --git a/cocos/2d/CCParticleBatchNode.cpp b/cocos/2d/CCParticleBatchNode.cpp index a7dbdc14d1..788fdd221c 100644 --- a/cocos/2d/CCParticleBatchNode.cpp +++ b/cocos/2d/CCParticleBatchNode.cpp @@ -144,7 +144,6 @@ void ParticleBatchNode::visit() transform(); draw(); - updateEventPriorityIndex(); if ( _grid && _grid->isActive()) { diff --git a/cocos/2d/CCRenderTexture.cpp b/cocos/2d/CCRenderTexture.cpp index 5bace186cf..d76c258f87 100644 --- a/cocos/2d/CCRenderTexture.cpp +++ b/cocos/2d/CCRenderTexture.cpp @@ -465,7 +465,6 @@ void RenderTexture::visit() transform(); _sprite->visit(); draw(); - updateEventPriorityIndex(); if (_grid && _grid->isActive()) { diff --git a/cocos/2d/CCSpriteBatchNode.cpp b/cocos/2d/CCSpriteBatchNode.cpp index b4be9bd045..195b5fa8eb 100644 --- a/cocos/2d/CCSpriteBatchNode.cpp +++ b/cocos/2d/CCSpriteBatchNode.cpp @@ -158,7 +158,6 @@ void SpriteBatchNode::visit(void) transform(); draw(); - updateEventPriorityIndex(); if (_grid && _grid->isActive()) { diff --git a/cocos/2d/platform/CCEGLViewProtocol.cpp b/cocos/2d/platform/CCEGLViewProtocol.cpp index 9b1e943019..094e584e97 100644 --- a/cocos/2d/platform/CCEGLViewProtocol.cpp +++ b/cocos/2d/platform/CCEGLViewProtocol.cpp @@ -247,7 +247,7 @@ void EGLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float y } touchEvent._eventCode = EventTouch::EventCode::BEGAN; - EventDispatcher::getInstance()->dispatchEvent(&touchEvent); + EventDispatcher::getInstance()->dispatchTouchEvent(&touchEvent); } void EGLViewProtocol::handleTouchesMove(int num, int ids[], float xs[], float ys[]) @@ -294,7 +294,7 @@ void EGLViewProtocol::handleTouchesMove(int num, int ids[], float xs[], float ys } touchEvent._eventCode = EventTouch::EventCode::MOVED; - EventDispatcher::getInstance()->dispatchEvent(&touchEvent); + EventDispatcher::getInstance()->dispatchTouchEvent(&touchEvent); } void EGLViewProtocol::handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode, int num, int ids[], float xs[], float ys[]) @@ -347,7 +347,7 @@ void EGLViewProtocol::handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode } touchEvent._eventCode = eventCode; - EventDispatcher::getInstance()->dispatchEvent(&touchEvent); + EventDispatcher::getInstance()->dispatchTouchEvent(&touchEvent); for (auto& touch : touchEvent._touches) { diff --git a/cocos/editor-support/cocostudio/CCArmature.cpp b/cocos/editor-support/cocostudio/CCArmature.cpp index 5885cbbd25..5c3b54e3df 100644 --- a/cocos/editor-support/cocostudio/CCArmature.cpp +++ b/cocos/editor-support/cocostudio/CCArmature.cpp @@ -624,7 +624,6 @@ void Armature::visit() transform(); sortAllChildren(); draw(); - updateEventPriorityIndex(); // reset for next frame _orderOfArrival = 0; diff --git a/cocos/editor-support/cocostudio/CCBatchNode.cpp b/cocos/editor-support/cocostudio/CCBatchNode.cpp index 0c6f7598ae..7ef2188fb8 100644 --- a/cocos/editor-support/cocostudio/CCBatchNode.cpp +++ b/cocos/editor-support/cocostudio/CCBatchNode.cpp @@ -81,7 +81,6 @@ void BatchNode::visit() transform(); sortAllChildren(); draw(); - updateEventPriorityIndex(); // reset for next frame _orderOfArrival = 0; diff --git a/extensions/GUI/CCScrollView/CCScrollView.cpp b/extensions/GUI/CCScrollView/CCScrollView.cpp index e5f4c0503e..ecc17ef0e7 100644 --- a/extensions/GUI/CCScrollView/CCScrollView.cpp +++ b/extensions/GUI/CCScrollView/CCScrollView.cpp @@ -109,16 +109,14 @@ bool ScrollView::initWithViewSize(Size size, Node *container/* = NULL*/) this->setViewSize(size); - this->onEnterHook = [this]() { - auto dispatcher = EventDispatcher::getInstance(); - auto listener = EventListenerTouchOneByOne::create(); - listener->onTouchBegan = CC_CALLBACK_2(ScrollView::onTouchBegan, this); - listener->onTouchMoved = CC_CALLBACK_2(ScrollView::onTouchMoved, this); - listener->onTouchEnded = CC_CALLBACK_2(ScrollView::onTouchEnded, this); - listener->onTouchCancelled = CC_CALLBACK_2(ScrollView::onTouchCancelled, this); - - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); - }; + auto dispatcher = EventDispatcher::getInstance(); + auto listener = EventListenerTouchOneByOne::create(); + listener->onTouchBegan = CC_CALLBACK_2(ScrollView::onTouchBegan, this); + listener->onTouchMoved = CC_CALLBACK_2(ScrollView::onTouchMoved, this); + listener->onTouchEnded = CC_CALLBACK_2(ScrollView::onTouchEnded, this); + listener->onTouchCancelled = CC_CALLBACK_2(ScrollView::onTouchCancelled, this); + + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); _touches.reserve(EventTouch::MAX_TOUCHES); @@ -578,7 +576,6 @@ void ScrollView::visit() // this draw this->draw(); - updateEventPriorityIndex(); // draw children zOrder >= 0 for( ; i < _children->count(); i++ ) @@ -591,7 +588,6 @@ void ScrollView::visit() else { this->draw(); - updateEventPriorityIndex(); } this->afterDraw(); diff --git a/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.cpp b/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.cpp index e0e3562a54..0e7a14ffdf 100644 --- a/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.cpp @@ -1375,7 +1375,7 @@ void ActionStacked::onEnter() this->centerSprites(0); - this->setTouchEnabled(true); +//cjh this->setTouchEnabled(true); auto s = Director::getInstance()->getWinSize(); this->addNewSpriteWithCoords(Point(s.width/2, s.height/2)); diff --git a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp index 4c958bebaa..9614180fb2 100644 --- a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp +++ b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp @@ -33,6 +33,7 @@ MenuLayer::MenuLayer(void) MenuLayer::~MenuLayer(void) { + EventDispatcher::getInstance()->removeEventListener(_touchListener); } MenuLayer* MenuLayer::menuWithEntryID(int entryId) @@ -52,9 +53,6 @@ bool MenuLayer::initWithEntryID(int entryId) m_entryID = entryId; - setTouchEnabled( true ); - setTouchMode(Touch::DispatchMode::ONE_BY_ONE); - Box2DView* view = Box2DView::viewWithEntryID( entryId ); addChild(view, 0, kTagBox2DNode); view->setScale(15); @@ -81,18 +79,17 @@ bool MenuLayer::initWithEntryID(int entryId) addChild(menu, 1); - // Removes touch event listener - EventDispatcher::getInstance()->removeEventListener(_touchListener); - // Adds touch event listener - auto listener = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); + auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(MenuLayer::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(MenuLayer::onTouchMoved, this); EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 1); + _touchListener = listener; + return true; } @@ -180,19 +177,16 @@ Box2DView* Box2DView::viewWithEntryID(int entryId) bool Box2DView::initWithEntryID(int entryId) { // setIsAccelerometerEnabled( true ); - setTouchEnabled( true ); +// FIXME cjh: setTouchEnabled( true ); schedule( schedule_selector(Box2DView::tick) ); m_entry = g_testEntries + entryId; m_test = m_entry->createFcn(); - setTouchMode(Touch::DispatchMode::ONE_BY_ONE); - // Removes Touch Event Listener - EventDispatcher::getInstance()->removeEventListener(_touchListener); // Adds Touch Event Listener - auto listener = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); + auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(Box2DView::onTouchBegan, this); @@ -232,6 +226,8 @@ void Box2DView::draw() Box2DView::~Box2DView() { + // Removes Touch Event Listener + EventDispatcher::getInstance()->removeEventListener(_touchListener); delete m_test; } // diff --git a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.h b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.h index 5f3e785535..681e44de6c 100644 --- a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.h +++ b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.h @@ -7,7 +7,7 @@ class MenuLayer : public Layer { int m_entryID; - + EventListenerTouchOneByOne* _touchListener; public: MenuLayer(void); virtual ~MenuLayer(void); @@ -30,6 +30,7 @@ struct TestEntry; class Test; class Box2DView : public Layer { + EventListenerTouchOneByOne* _touchListener; TestEntry* m_entry; Test* m_test; int m_entryID; diff --git a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-624.cpp b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-624.cpp index a86859ebb1..9eb6d6ab5c 100644 --- a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-624.cpp +++ b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-624.cpp @@ -20,11 +20,9 @@ bool Bug624Layer::init() label->setPosition(Point(size.width/2, size.height/2)); addChild(label); - this->onEnterHook = [this](){ - auto dispatcher = EventDispatcher::getInstance(); - auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(Bug624Layer::onAcceleration, this)); - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); - }; + auto dispatcher = EventDispatcher::getInstance(); + auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(Bug624Layer::onAcceleration, this)); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); schedule(schedule_selector(Bug624Layer::switchLayer), 5.0f); @@ -63,11 +61,9 @@ bool Bug624Layer2::init() label->setPosition(Point(size.width/2, size.height/2)); addChild(label); - this->onEnterHook = [this](){ - auto dispatcher = EventDispatcher::getInstance(); - auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(Bug624Layer2::onAcceleration, this)); - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); - }; + auto dispatcher = EventDispatcher::getInstance(); + auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(Bug624Layer2::onAcceleration, this)); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); schedule(schedule_selector(Bug624Layer2::switchLayer), 5.0f); diff --git a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.cpp b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.cpp index 337d9b1006..76ab5b3b31 100644 --- a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.cpp +++ b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.cpp @@ -30,13 +30,11 @@ bool Bug914Layer::init() // Apple recommends to re-assign "self" with the "super" return value if (BugsTestBaseLayer::init()) { - this->onEnterHook = [this](){ - auto dispatcher = EventDispatcher::getInstance(); - auto listener = EventListenerTouchAllAtOnce::create(); - listener->onTouchesBegan = CC_CALLBACK_2(Bug914Layer::onTouchesBegan, this); - listener->onTouchesMoved = CC_CALLBACK_2(Bug914Layer::onTouchesMoved, this); - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); - }; + auto dispatcher = EventDispatcher::getInstance(); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesBegan = CC_CALLBACK_2(Bug914Layer::onTouchesBegan, this); + listener->onTouchesMoved = CC_CALLBACK_2(Bug914Layer::onTouchesMoved, this); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); // ask director the the window size auto size = Director::getInstance()->getWinSize(); diff --git a/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.cpp b/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.cpp index 237b12077c..7f28de8df8 100644 --- a/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.cpp @@ -67,7 +67,7 @@ void BugsTestMainLayer::onEnter() _itmeMenu->setPosition(s_tCurPos); addChild(_itmeMenu); - setTouchEnabled(true); +//cjh setTouchEnabled(true); } void BugsTestMainLayer::onTouchesBegan(const std::vector& touches, Event *event) diff --git a/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.cpp b/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.cpp index f617ee501b..46cae7762e 100644 --- a/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.cpp @@ -21,8 +21,8 @@ ChipmunkTestLayer::ChipmunkTestLayer() { #if CC_ENABLE_CHIPMUNK_INTEGRATION // enable events - setTouchEnabled(true); - setAccelerometerEnabled(true); +//FIXME: setTouchEnabled(true); +// setAccelerometerEnabled(true); // title auto label = LabelTTF::create("Multi touch the screen", "Marker Felt", 36); diff --git a/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.h b/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.h index 33ae31cee7..3480f0314a 100644 --- a/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.h +++ b/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.h @@ -24,8 +24,8 @@ public: void addNewSpriteAtPosition(Point p); void update(float dt); void toggleDebugCallback(Object* sender); - virtual void onTouchesEnded(const std::vector& touches, Event* event) override; - virtual void onAcceleration(Acceleration* acc, Event* event) override; + virtual void onTouchesEnded(const std::vector& touches, Event* event); + virtual void onAcceleration(Acceleration* acc, Event* event); private: Texture2D* _spriteTexture; // weak ref diff --git a/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.cpp b/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.cpp index 5a87658f35..408188925f 100644 --- a/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.cpp @@ -17,13 +17,11 @@ void ClickAndMoveTestScene::runThisTest() MainLayer::MainLayer() { - this->onEnterHook = [this](){ - auto dispatcher = EventDispatcher::getInstance(); - auto listener = EventListenerTouchOneByOne::create(); - listener->onTouchBegan = CC_CALLBACK_2(MainLayer::onTouchBegan, this); - listener->onTouchEnded = CC_CALLBACK_2(MainLayer::onTouchEnded, this); - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); - }; + auto dispatcher = EventDispatcher::getInstance(); + auto listener = EventListenerTouchOneByOne::create(); + listener->onTouchBegan = CC_CALLBACK_2(MainLayer::onTouchBegan, this); + listener->onTouchEnded = CC_CALLBACK_2(MainLayer::onTouchEnded, this); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto sprite = Sprite::create(s_pathGrossini); diff --git a/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.cpp b/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.cpp index fcde7712ad..56ad34de68 100644 --- a/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.cpp @@ -449,12 +449,10 @@ void HoleDemo::setup() this->addChild(_outerClipper); - this->onEnterHook = [this](){ - auto dispatcher = EventDispatcher::getInstance(); - auto listener = EventListenerTouchAllAtOnce::create(); - listener->onTouchesBegan = CC_CALLBACK_2(HoleDemo::onTouchesBegan, this); - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); - }; + auto dispatcher = EventDispatcher::getInstance(); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesBegan = CC_CALLBACK_2(HoleDemo::onTouchesBegan, this); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); } void HoleDemo::pokeHoleAtPoint(Point point) @@ -531,14 +529,12 @@ void ScrollViewDemo::setup() _scrolling = false; - this->onEnterHook = [this](){ - auto dispatcher = EventDispatcher::getInstance(); - auto listener = EventListenerTouchAllAtOnce::create(); - listener->onTouchesBegan = CC_CALLBACK_2(ScrollViewDemo::onTouchesBegan, this); - listener->onTouchesMoved = CC_CALLBACK_2(ScrollViewDemo::onTouchesMoved, this); - listener->onTouchesEnded = CC_CALLBACK_2(ScrollViewDemo::onTouchesEnded, this); - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); - }; + auto dispatcher = EventDispatcher::getInstance(); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesBegan = CC_CALLBACK_2(ScrollViewDemo::onTouchesBegan, this); + listener->onTouchesMoved = CC_CALLBACK_2(ScrollViewDemo::onTouchesMoved, this); + listener->onTouchesEnded = CC_CALLBACK_2(ScrollViewDemo::onTouchesEnded, this); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); } void ScrollViewDemo::onTouchesBegan(const std::vector& touches, Event *event) diff --git a/samples/Cpp/TestCpp/Classes/CocosDenshionTest/CocosDenshionTest.cpp b/samples/Cpp/TestCpp/Classes/CocosDenshionTest/CocosDenshionTest.cpp index 207a26a2ab..9589cde586 100644 --- a/samples/Cpp/TestCpp/Classes/CocosDenshionTest/CocosDenshionTest.cpp +++ b/samples/Cpp/TestCpp/Classes/CocosDenshionTest/CocosDenshionTest.cpp @@ -65,7 +65,7 @@ private: // Director::getInstance()->getTouchDispatcher()->addTargetedDelegate(this, 100, true); // Register Touch Event - auto listener = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); + auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(Button::onTouchBegan, this); @@ -238,7 +238,7 @@ _sliderMusicVolume(NULL) // "stop all effects" // }; - setTouchEnabled(true); +//cjh setTouchEnabled(true); // preload background music and effect SimpleAudioEngine::getInstance()->preloadBackgroundMusic( MUSIC_FILE ); diff --git a/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.cpp b/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.cpp index b8cfc653b7..83945cd581 100644 --- a/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.cpp +++ b/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.cpp @@ -9,7 +9,7 @@ CurlTest::CurlTest() addChild(label, 0); label->setPosition( Point(VisibleRect::center().x, VisibleRect::top().y-50) ); - setTouchEnabled(true); +//FIXME cjh: setTouchEnabled(true); // create a label to display the tip string _label = LabelTTF::create("Touch the screen to connect", "Arial", 22); diff --git a/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.h b/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.h index da0290d0bf..9688d03e38 100644 --- a/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.h +++ b/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.h @@ -10,7 +10,7 @@ public: CurlTest(); ~CurlTest(); - virtual void onTouchesEnded(const std::vector& touches, cocos2d::Event *event) override; + virtual void onTouchesEnded(const std::vector& touches, cocos2d::Event *event); private: cocos2d::LabelTTF* _label; diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp index 66eb20472b..eaf14546be 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp @@ -477,7 +477,7 @@ void TestAnimationEvent::callback2() void TestParticleDisplay::onEnter() { ArmatureTestLayer::onEnter(); - setTouchEnabled(true); +//FIXME cjh: setTouchEnabled(true); animationID = 0; @@ -534,7 +534,7 @@ void TestParticleDisplay::onTouchesEnded(const std::vector& touches, Eve void TestUseMutiplePicture::onEnter() { ArmatureTestLayer::onEnter(); - setTouchEnabled(true); +//cjh setTouchEnabled(true); displayIndex = 0; @@ -935,7 +935,7 @@ std::string TestAnchorPoint::title() void TestArmatureNesting::onEnter() { ArmatureTestLayer::onEnter(); - setTouchEnabled(true); +//cjh setTouchEnabled(true); armature = Armature::create("cyborg"); armature->getAnimation()->playByIndex(1); diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.h index 625fdddf88..ccd07d1d72 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.h @@ -138,7 +138,7 @@ class TestUseMutiplePicture : public ArmatureTestLayer virtual void onExit(); virtual std::string title(); virtual std::string subtitle(); - virtual void onTouchesEnded(const std::vector& touches, Event* event) override; + virtual void onTouchesEnded(const std::vector& touches, Event* event); int displayIndex; cocostudio::Armature *armature; diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.cpp index 0c802b3f97..7c95239cfa 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.cpp @@ -118,7 +118,7 @@ void ExtensionsMainLayer::onEnter() _itemMenu->addChild(pItem, kItemTagBasic + i); } - setTouchEnabled(true); +//cjh setTouchEnabled(true); addChild(_itemMenu); } diff --git a/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp b/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp index 9e2fd40647..7140a44e8a 100644 --- a/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp +++ b/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp @@ -7,7 +7,7 @@ KeyboardTest::KeyboardTest() addChild(label, 0); label->setPosition( Point(s.width/2, s.height-50) ); - setKeyboardEnabled(true); +//cjh setKeyboardEnabled(true); // create a label to display the tip string _label = LabelTTF::create("Please press any key and see console log...", "Arial", 22); diff --git a/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp b/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp index 7cdbebbc6e..5bda6cccf2 100644 --- a/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp +++ b/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp @@ -7,7 +7,7 @@ KeypadTest::KeypadTest() addChild(label, 0); label->setPosition( Point(s.width/2, s.height-50) ); - setKeyboardEnabled(true); +//cjh setKeyboardEnabled(true); // create a label to display the tip string _label = LabelTTF::create("Please press any key...", "Arial", 22); diff --git a/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.h b/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.h index 760ab8eb01..e525511e4a 100644 --- a/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.h +++ b/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.h @@ -10,7 +10,7 @@ public: KeypadTest(); ~KeypadTest(); - virtual void onKeyReleased(EventKeyboard::KeyCode keycode, Event* event) override; + virtual void onKeyReleased(EventKeyboard::KeyCode keycode, Event* event); private: LabelTTF* _label; diff --git a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp index 36b434f139..52fc92c2e4 100644 --- a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp +++ b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp @@ -1135,7 +1135,7 @@ static float alignmentItemPadding = 50; static float menuItemPaddingCenter = 50; BitmapFontMultiLineAlignment::BitmapFontMultiLineAlignment() { - this->setTouchEnabled(true); +//cjh this->setTouchEnabled(true); // ask director the the window size auto size = Director::getInstance()->getWinSize(); diff --git a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp index d822776e62..47173da112 100644 --- a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp +++ b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp @@ -650,7 +650,7 @@ static float menuItemPaddingCenter = 50; LabelFNTMultiLineAlignment::LabelFNTMultiLineAlignment() { - this->setTouchEnabled(true); +//cjh this->setTouchEnabled(true); // ask director the the window size auto size = Director::getInstance()->getWinSize(); diff --git a/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp b/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp index 26175b6e3f..e27b82cf16 100644 --- a/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp +++ b/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp @@ -449,7 +449,7 @@ void LayerTest1::onEnter() { LayerTest::onEnter(); - setTouchEnabled(true); +//cjh setTouchEnabled(true); auto s = Director::getInstance()->getWinSize(); auto layer = LayerColor::create( Color4B(0xFF, 0x00, 0x00, 0x80), 200, 200); @@ -590,7 +590,7 @@ LayerGradientTest::LayerGradientTest() auto layer1 = LayerGradient::create(Color4B(255,0,0,255), Color4B(0,255,0,255), Point(0.9f, 0.9f)); addChild(layer1, 0, kTagLayer); - setTouchEnabled(true); +//cjh setTouchEnabled(true); auto label1 = LabelTTF::create("Compressed Interpolation: Enabled", "Marker Felt", 26); auto label2 = LabelTTF::create("Compressed Interpolation: Disabled", "Marker Felt", 26); diff --git a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp index 8a5809ee6b..a61f15225b 100644 --- a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp +++ b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp @@ -25,8 +25,8 @@ enum { //------------------------------------------------------------------ MenuLayerMainMenu::MenuLayerMainMenu() { - setTouchEnabled(true); - setTouchMode(Touch::DispatchMode::ONE_BY_ONE); +//cjh setTouchEnabled(true); +// setTouchMode(Touch::DispatchMode::ONE_BY_ONE); // Font Item auto spriteNormal = Sprite::create(s_MenuItem, Rect(0,23*2,115,23)); @@ -532,8 +532,8 @@ BugsTest::BugsTest() void BugsTest::issue1410MenuCallback(Object *sender) { auto menu = static_cast( static_cast(sender)->getParent() ); - menu->setTouchEnabled(false); - menu->setTouchEnabled(true); +//cjh menu->setTouchEnabled(false); +// menu->setTouchEnabled(true); log("NO CRASHES"); } @@ -541,8 +541,8 @@ void BugsTest::issue1410MenuCallback(Object *sender) void BugsTest::issue1410v2MenuCallback(cocos2d::Object *pSender) { auto menu = static_cast( static_cast(pSender)->getParent() ); - menu->setTouchEnabled(true); - menu->setTouchEnabled(false); +//cjh menu->setTouchEnabled(true); +// menu->setTouchEnabled(false); log("NO CRASHES. AND MENU SHOULD STOP WORKING"); } @@ -571,11 +571,11 @@ RemoveMenuItemWhenMove::RemoveMenuItemWhenMove() menu->setPosition(Point(s.width/2, s.height/2)); - setTouchEnabled(true); - setTouchMode(Touch::DispatchMode::ONE_BY_ONE); +//cjh setTouchEnabled(true); +// setTouchMode(Touch::DispatchMode::ONE_BY_ONE); // Register Touch Event - _touchListener = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); + _touchListener = EventListenerTouchOneByOne::create(); _touchListener->setSwallowTouches(false); _touchListener->onTouchBegan = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchBegan, this); diff --git a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h index f023871835..41072151c5 100644 --- a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h +++ b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h @@ -113,6 +113,7 @@ public: private: MenuItemFont *item; + EventListenerTouchOneByOne* _touchListener; }; diff --git a/samples/Cpp/TestCpp/Classes/MotionStreakTest/MotionStreakTest.cpp b/samples/Cpp/TestCpp/Classes/MotionStreakTest/MotionStreakTest.cpp index dcbb3a3beb..7c4fe4340f 100644 --- a/samples/Cpp/TestCpp/Classes/MotionStreakTest/MotionStreakTest.cpp +++ b/samples/Cpp/TestCpp/Classes/MotionStreakTest/MotionStreakTest.cpp @@ -121,7 +121,7 @@ void MotionStreakTest2::onEnter() { MotionStreakTest::onEnter(); - setTouchEnabled(true); +//cjh setTouchEnabled(true); auto s = Director::getInstance()->getWinSize(); diff --git a/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.cpp b/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.cpp index 9ef9656951..f92fa93299 100644 --- a/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.cpp +++ b/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.cpp @@ -56,7 +56,7 @@ bool MutiTouchTestLayer::init() { if (Layer::init()) { - setTouchEnabled(true); +//cjh setTouchEnabled(true); auto title = LabelTTF::create("Please touch the screen!", "", 24); title->setPosition(VisibleRect::top()+Point(0, -40)); diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp index 60a2f909a9..797f042ca7 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp @@ -141,7 +141,7 @@ void TouchableSpriteTest::onEnter() sprite2->addChild(sprite3, 1); // Make sprite1 touchable - auto listener1 = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); + auto listener1 = EventListenerTouchOneByOne::create(); listener1->setSwallowTouches(true); listener1->onTouchBegan = [](Touch* touch, Event* event){ @@ -243,7 +243,7 @@ public: auto dispatcher = EventDispatcher::getInstance(); - auto listener = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); + auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = [=](Touch* touch, Event* event){ @@ -348,7 +348,7 @@ void RemoveListenerWhenDispatching::onEnter() addChild(sprite1, 10); // Make sprite1 touchable - auto listener1 = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); + auto listener1 = EventListenerTouchOneByOne::create(); listener1->setSwallowTouches(true); setUserObject(listener1); diff --git a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp index 8689ee9f99..db7f53d7c2 100644 --- a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp @@ -723,7 +723,7 @@ std::string CameraCenterTest::subtitle() //------------------------------------------------------------------ ConvertToNode::ConvertToNode() { - setTouchEnabled(true); +//cjh setTouchEnabled(true); auto s = Director::getInstance()->getWinSize(); auto rotate = RotateBy::create(10, 360); diff --git a/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp b/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp index cfa80084b9..bc2a128603 100644 --- a/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp @@ -87,7 +87,7 @@ std::string Parallax1::title() Parallax2::Parallax2() { - setTouchEnabled( true ); +//cjh setTouchEnabled( true ); // Top Layer, a simple image auto cocosImage = Sprite::create(s_Power); diff --git a/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp b/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp index bd4b98dea6..414c915135 100644 --- a/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp @@ -1068,7 +1068,7 @@ void ParticleDemo::onEnter(void) _emitter = NULL; - setTouchEnabled( true ); +//cjh setTouchEnabled( true ); auto s = Director::getInstance()->getWinSize(); diff --git a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp index 60c5341463..a928a3bd23 100644 --- a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp +++ b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp @@ -119,9 +119,9 @@ std::string TouchesMainScene::title() void TouchesPerformTest1::onEnter() { TouchesMainScene::onEnter(); - setTouchEnabled(true); - setTouchMode(Touch::DispatchMode::ONE_BY_ONE); - setSwallowsTouches(true); +//cjh setTouchEnabled(true); +// setTouchMode(Touch::DispatchMode::ONE_BY_ONE); +// setSwallowsTouches(true); } std::string TouchesPerformTest1::title() @@ -158,7 +158,7 @@ void TouchesPerformTest1::onTouchCancelled(Touch* touch, Event* event) void TouchesPerformTest2::onEnter() { TouchesMainScene::onEnter(); - setTouchEnabled(true); +//cjh setTouchEnabled(true); } std::string TouchesPerformTest2::title() @@ -231,8 +231,8 @@ void TouchesPerformTest3::onEnter() { int zorder = rand() % TOUCHABLE_NODE_NUM; auto layer = new TouchableLayer(); - layer->setTouchEnabled(true); - layer->setTouchMode(Touch::DispatchMode::ONE_BY_ONE); +//cjh layer->setTouchEnabled(true); +// layer->setTouchMode(Touch::DispatchMode::ONE_BY_ONE); addChild(layer, zorder); layer->release(); } @@ -260,7 +260,7 @@ void TouchesPerformTest3::onEnter() { CC_PROFILER_START(TOUCH_PROFILER_NAME); - dispatcher->dispatchEvent(&event, false); + dispatcher->dispatchEvent(&event); CC_PROFILER_STOP(TOUCH_PROFILER_NAME); } diff --git a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.h b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.h index 47029a3bc6..5cb4d35ad9 100644 --- a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.h +++ b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.h @@ -36,10 +36,10 @@ public: virtual void onEnter() override; virtual std::string title() override; - virtual bool onTouchBegan(Touch* touch, Event* event) override; - virtual void onTouchMoved(Touch* touch, Event* event) override; - virtual void onTouchEnded(Touch* touch, Event* event) override; - virtual void onTouchCancelled(Touch* touch, Event* event) override; + virtual bool onTouchBegan(Touch* touch, Event* event) ; + virtual void onTouchMoved(Touch* touch, Event* event) ; + virtual void onTouchEnded(Touch* touch, Event* event) ; + virtual void onTouchCancelled(Touch* touch, Event* event) ; }; class TouchesPerformTest2 : public TouchesMainScene @@ -53,10 +53,10 @@ public: virtual void onEnter() override; virtual std::string title() override; - void onTouchesBegan(const std::vector& touches, Event* event) override; - void onTouchesMoved(const std::vector& touches, Event* event) override; - void onTouchesEnded(const std::vector& touches, Event* event) override; - void onTouchesCancelled(const std::vector& touches, Event* event) override; + void onTouchesBegan(const std::vector& touches, Event* event) ; + void onTouchesMoved(const std::vector& touches, Event* event) ; + void onTouchesEnded(const std::vector& touches, Event* event) ; + void onTouchesCancelled(const std::vector& touches, Event* event) ; }; class TouchesPerformTest3 : public PerformBasicLayer diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp index f0ceb3553c..4c56986746 100644 --- a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp @@ -197,8 +197,8 @@ void PhysicsDemoClickAdd::onEnter() PhysicsDemo::onEnter(); #ifdef CC_USE_PHYSICS - setTouchEnabled(true); - setAccelerometerEnabled(true); +//cjh setTouchEnabled(true); +// setAccelerometerEnabled(true); auto node = Node::create(); node->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size)); diff --git a/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp b/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp index 433415f677..1669b78c1f 100644 --- a/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp +++ b/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp @@ -111,7 +111,7 @@ RenderTextureSave::RenderTextureSave() _brush->retain(); _brush->setColor(Color3B::RED); _brush->setOpacity(20); - this->setTouchEnabled(true); +//cjh this->setTouchEnabled(true); // Save Image menu MenuItemFont::setFontSize(16); @@ -296,7 +296,7 @@ void RenderTextureScene::runThisTest() RenderTextureZbuffer::RenderTextureZbuffer() { - this->setTouchEnabled(true); +//cjh this->setTouchEnabled(true); auto size = Director::getInstance()->getWinSize(); auto label = LabelTTF::create("vertexZ = 50", "Marker Felt", 64); label->setPosition(Point(size.width / 2, size.height * 0.25f)); @@ -627,7 +627,7 @@ void SpriteRenderTextureBug::SimpleSprite::draw() SpriteRenderTextureBug::SpriteRenderTextureBug() { - setTouchEnabled(true); +//cjh setTouchEnabled(true); auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords(Point(s.width/2, s.height/2)); diff --git a/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id b/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id index 16b0471301..a3d466373a 100644 --- a/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id +++ b/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id @@ -1 +1 @@ -b17d2ec91cd40aaf09376a62f38f97ca3f7da2c7 \ No newline at end of file +4701ba8fb99f70629c5575dcf6fb3536543ac0b6 \ No newline at end of file diff --git a/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp b/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp index 3392a3e9f8..1645d017d8 100644 --- a/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp +++ b/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp @@ -126,10 +126,10 @@ void TextInputTest::onEnter() KeyboardNotificationLayer::KeyboardNotificationLayer() : _trackNode(0) { - setTouchEnabled(true); +//cjh setTouchEnabled(true); // Register Touch Event - auto listener = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); + auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(KeyboardNotificationLayer::onTouchBegan, this); listener->onTouchEnded = CC_CALLBACK_2(KeyboardNotificationLayer::onTouchEnded, this); diff --git a/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp b/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp index 761afa4a71..7be2b29c32 100644 --- a/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp +++ b/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp @@ -1430,7 +1430,7 @@ Layer* restartTileMapAction() TileDemo::TileDemo(void) : BaseTest() { - setTouchEnabled( true ); +//cjh setTouchEnabled( true ); } TileDemo::~TileDemo(void) diff --git a/samples/Cpp/TestCpp/Classes/TouchesTest/Paddle.cpp b/samples/Cpp/TestCpp/Classes/TouchesTest/Paddle.cpp index 2e3720d10c..e168d7a1bc 100644 --- a/samples/Cpp/TestCpp/Classes/TouchesTest/Paddle.cpp +++ b/samples/Cpp/TestCpp/Classes/TouchesTest/Paddle.cpp @@ -38,7 +38,7 @@ void Paddle::onEnter() Sprite::onEnter(); // Register Touch Event - auto listener = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); + auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(Paddle::onTouchBegan, this); diff --git a/samples/Cpp/TestCpp/Classes/controller.cpp b/samples/Cpp/TestCpp/Classes/controller.cpp index f78fb2c9af..c7b03f1acf 100644 --- a/samples/Cpp/TestCpp/Classes/controller.cpp +++ b/samples/Cpp/TestCpp/Classes/controller.cpp @@ -120,12 +120,12 @@ TestController::TestController() _itemMenu->setPosition(s_tCurPos); addChild(_itemMenu); - setTouchEnabled(true); +//cjh setTouchEnabled(true); addChild(menu, 1); // Register Touch Event - auto listener = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); + auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(TestController::onTouchBegan, this); From 9ed263056c126bf6efc6dc46ddd8154e48244771 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 23 Oct 2013 16:14:03 +0800 Subject: [PATCH 05/42] Use eventDispatcher to dispatch event. Remove controller codes in Layer. --- cocos/2d/CCEventDispatcher.cpp | 159 +++++++++--------- cocos/2d/CCEventListenerTouch.h | 1 - cocos/2d/CCMenu.h | 10 +- .../Cpp/SimpleGame/Classes/HelloWorldScene.h | 2 +- .../Classes/ActionsTest/ActionsTest.cpp | 4 +- .../TestCpp/Classes/ActionsTest/ActionsTest.h | 2 +- .../Cpp/TestCpp/Classes/Box2DTest/Box2dTest.h | 2 +- .../Classes/Box2DTestBed/Box2dView.cpp | 3 - .../TestCpp/Classes/Box2DTestBed/Box2dView.h | 10 +- .../Cpp/TestCpp/Classes/BugsTest/Bug-914.h | 4 +- .../Cpp/TestCpp/Classes/BugsTest/BugsTest.cpp | 7 +- .../Cpp/TestCpp/Classes/BugsTest/BugsTest.h | 4 +- .../Classes/ChipmunkTest/ChipmunkTest.cpp | 9 +- .../Classes/ChipmunkTest/ChipmunkTest.h | 2 +- .../ClickAndMoveTest/ClickAndMoveTest.h | 4 +- .../ClippingNodeTest/ClippingNodeTest.h | 8 +- .../CocosDenshionTest/CocosDenshionTest.cpp | 13 -- .../Cpp/TestCpp/Classes/CurlTest/CurlTest.cpp | 4 +- .../Cpp/TestCpp/Classes/CurlTest/CurlTest.h | 2 +- .../CocoStudioArmatureTest/ArmatureScene.cpp | 15 +- .../CocoStudioArmatureTest/ArmatureScene.h | 6 +- .../PlayerController.h | 2 +- .../Classes/ExtensionsTest/ExtensionsTest.cpp | 6 +- .../Classes/ExtensionsTest/ExtensionsTest.h | 4 +- .../Classes/KeyboardTest/KeyboardTest.cpp | 8 +- .../TestCpp/Classes/KeypadTest/KeypadTest.cpp | 7 +- .../TestCpp/Classes/LabelTest/LabelTest.cpp | 9 +- .../Cpp/TestCpp/Classes/LabelTest/LabelTest.h | 6 +- .../Classes/LabelTest/LabelTestNew.cpp | 8 +- .../TestCpp/Classes/LabelTest/LabelTestNew.h | 6 +- .../TestCpp/Classes/LayerTest/LayerTest.cpp | 11 +- .../Cpp/TestCpp/Classes/LayerTest/LayerTest.h | 8 +- .../Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp | 20 ++- .../Cpp/TestCpp/Classes/MenuTest/MenuTest.h | 12 +- .../MotionStreakTest/MotionStreakTest.cpp | 6 +- .../Classes/MutiTouchTest/MutiTouchTest.cpp | 6 +- .../Classes/MutiTouchTest/MutiTouchTest.h | 1 - .../NewEventDispatcherTest.cpp | 87 +++++++++- .../NewEventDispatcherTest.h | 12 ++ .../Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp | 5 +- .../Cpp/TestCpp/Classes/NodeTest/NodeTest.h | 2 +- .../Classes/ParallaxTest/ParallaxTest.cpp | 4 +- .../Classes/ParallaxTest/ParallaxTest.h | 2 +- .../Classes/ParticleTest/ParticleTest.cpp | 6 +- .../Classes/ParticleTest/ParticleTest.h | 6 +- .../PerformanceTouchesTest.cpp | 41 +++-- .../PerformanceTest/PerformanceTouchesTest.h | 8 +- .../Classes/PhysicsTest/PhysicsTest.cpp | 9 +- .../RenderTextureTest/RenderTextureTest.cpp | 18 +- .../RenderTextureTest/RenderTextureTest.h | 10 +- .../SpriteTest/SpriteTest.cpp.REMOVED.git-id | 2 +- .../TestCpp/Classes/SpriteTest/SpriteTest.h | 2 +- .../Classes/TextInputTest/TextInputTest.cpp | 4 - .../Classes/TextInputTest/TextInputTest.h | 1 - .../Classes/TileMapTest/TileMapTest.cpp | 4 +- .../TestCpp/Classes/TileMapTest/TileMapTest.h | 2 +- samples/Cpp/TestCpp/Classes/controller.cpp | 2 - 57 files changed, 392 insertions(+), 226 deletions(-) diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index 7933a5edd6..05a2a66ddf 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -61,85 +61,6 @@ NS_CC_BEGIN static EventDispatcher* g_instance = nullptr; static int s_eventPriorityIndex = 0; -void EventDispatcher::visitTarget(Node* node) -{ - // Reset priority index - s_eventPriorityIndex = 0; - _nodePriorityMap.clear(); - - int i = 0; - Array* children = node->getChildren(); - int childrenCount = children ? children->count() : 0; - - if(childrenCount > 0) - { - - Node* child = nullptr; - // visit children zOrder < 0 - for( ; i < childrenCount; i++ ) - { - child = static_cast( children->getObjectAtIndex(i) ); - - if ( child && child->getZOrder() < 0 ) - visitTarget(child); - else - break; - } - - _nodePriorityMap.insert(std::make_pair(node, ++s_eventPriorityIndex)); - - for( ; i < childrenCount; i++ ) - { - child = static_cast( children->getObjectAtIndex(i) ); - if (child) - visitTarget(child); - } - } - else - { - _nodePriorityMap.insert(std::make_pair(node, ++s_eventPriorityIndex)); - } -} - -void EventDispatcher::pauseTarget(Node* node) -{ - auto listenerIter = _nodeListenersMap.find(node); - if (listenerIter != _nodeListenersMap.end()) - { - auto listeners = listenerIter->second; - for (auto& l : *listeners) - { - l->_paused = true; - } - } -} - -void EventDispatcher::resumeTarget(Node* node) -{ - auto listenerIter = _nodeListenersMap.find(node); - if (listenerIter != _nodeListenersMap.end()) - { - auto listeners = listenerIter->second; - for (auto& l : *listeners) - { - l->_paused = false; - } - } -} - -void EventDispatcher::cleanTarget(Node* node) -{ - auto listenerIter = _nodeListenersMap.find(node); - if (listenerIter != _nodeListenersMap.end()) - { - auto listeners = listenerIter->second; - for (auto& l : *listeners) - { - removeEventListener(l); - } - } -} - EventDispatcher::EventListenerVector::EventListenerVector() : _sceneGraphListeners(nullptr) , _fixedListeners(nullptr) @@ -239,6 +160,85 @@ void EventDispatcher::destroyInstance() CC_SAFE_DELETE(g_instance); } +void EventDispatcher::visitTarget(Node* node) +{ + // Reset priority index + s_eventPriorityIndex = 0; + _nodePriorityMap.clear(); + + int i = 0; + Array* children = node->getChildren(); + int childrenCount = children ? children->count() : 0; + + if(childrenCount > 0) + { + + Node* child = nullptr; + // visit children zOrder < 0 + for( ; i < childrenCount; i++ ) + { + child = static_cast( children->getObjectAtIndex(i) ); + + if ( child && child->getZOrder() < 0 ) + visitTarget(child); + else + break; + } + + _nodePriorityMap.insert(std::make_pair(node, ++s_eventPriorityIndex)); + + for( ; i < childrenCount; i++ ) + { + child = static_cast( children->getObjectAtIndex(i) ); + if (child) + visitTarget(child); + } + } + else + { + _nodePriorityMap.insert(std::make_pair(node, ++s_eventPriorityIndex)); + } +} + +void EventDispatcher::pauseTarget(Node* node) +{ + auto listenerIter = _nodeListenersMap.find(node); + if (listenerIter != _nodeListenersMap.end()) + { + auto listeners = listenerIter->second; + for (auto& l : *listeners) + { + l->_paused = true; + } + } +} + +void EventDispatcher::resumeTarget(Node* node) +{ + auto listenerIter = _nodeListenersMap.find(node); + if (listenerIter != _nodeListenersMap.end()) + { + auto listeners = listenerIter->second; + for (auto& l : *listeners) + { + l->_paused = false; + } + } +} + +void EventDispatcher::cleanTarget(Node* node) +{ + auto listenerIter = _nodeListenersMap.find(node); + if (listenerIter != _nodeListenersMap.end()) + { + auto listeners = listenerIter->second; + for (auto& l : *listeners) + { + removeEventListener(l); + } + } +} + void EventDispatcher::associateNodeAndEventListener(Node* node, EventListener* listener) { std::vector* listeners = nullptr; @@ -635,7 +635,6 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) { if (listener->onTouchBegan) { - CCLOG("onTouchBegan..."); isClaimed = listener->onTouchBegan(*touchesIter, event); if (isClaimed && listener->_isRegistered) { diff --git a/cocos/2d/CCEventListenerTouch.h b/cocos/2d/CCEventListenerTouch.h index d34de1b237..09e6e6fe0c 100644 --- a/cocos/2d/CCEventListenerTouch.h +++ b/cocos/2d/CCEventListenerTouch.h @@ -33,7 +33,6 @@ NS_CC_BEGIN - class EventListenerTouchOneByOne : public EventListener { public: diff --git a/cocos/2d/CCMenu.h b/cocos/2d/CCMenu.h index a735d138db..64c275841a 100644 --- a/cocos/2d/CCMenu.h +++ b/cocos/2d/CCMenu.h @@ -112,6 +112,11 @@ public: virtual bool isEnabled() const { return _enabled; } virtual void setEnabled(bool value) { _enabled = value; }; + virtual bool onTouchBegan(Touch* touch, Event* event); + virtual void onTouchEnded(Touch* touch, Event* event); + virtual void onTouchCancelled(Touch* touch, Event* event); + virtual void onTouchMoved(Touch* touch, Event* event); + // overrides virtual void removeChild(Node* child, bool cleanup) override; @@ -119,11 +124,6 @@ public: virtual void addChild(Node * child, int zOrder) override; virtual void addChild(Node * child, int zOrder, int tag) override; - virtual bool onTouchBegan(Touch* touch, Event* event); - virtual void onTouchEnded(Touch* touch, Event* event); - virtual void onTouchCancelled(Touch* touch, Event* event); - virtual void onTouchMoved(Touch* touch, Event* event); - virtual void onEnter() override; virtual void onExit() override; virtual void setOpacityModifyRGB(bool bValue) override {CC_UNUSED_PARAM(bValue);} diff --git a/samples/Cpp/SimpleGame/Classes/HelloWorldScene.h b/samples/Cpp/SimpleGame/Classes/HelloWorldScene.h index a5f2478391..279f2c4399 100644 --- a/samples/Cpp/SimpleGame/Classes/HelloWorldScene.h +++ b/samples/Cpp/SimpleGame/Classes/HelloWorldScene.h @@ -30,7 +30,7 @@ public: void updateGame(float dt); - virtual void onTouchesEnded(const std::vector& touches, cocos2d::Event* event) override; + void onTouchesEnded(const std::vector& touches, cocos2d::Event* event) override; protected: diff --git a/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.cpp b/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.cpp index 0e7a14ffdf..c41f06c377 100644 --- a/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.cpp @@ -1375,7 +1375,9 @@ void ActionStacked::onEnter() this->centerSprites(0); -//cjh this->setTouchEnabled(true); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesEnded = CC_CALLBACK_2(ActionStacked::onTouchesEnded, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); auto s = Director::getInstance()->getWinSize(); this->addNewSpriteWithCoords(Point(s.width/2, s.height/2)); diff --git a/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.h b/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.h index c064ee3436..f67907ceba 100644 --- a/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.h +++ b/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.h @@ -305,7 +305,7 @@ public: virtual std::string subtitle(); virtual void addNewSpriteWithCoords(Point p); virtual void runActionsInSprite(Sprite* sprite); - virtual void onTouchesEnded(const std::vector& touches, Event* event); + void onTouchesEnded(const std::vector& touches, Event* event); }; class ActionMoveStacked : public ActionStacked diff --git a/samples/Cpp/TestCpp/Classes/Box2DTest/Box2dTest.h b/samples/Cpp/TestCpp/Classes/Box2DTest/Box2dTest.h index 480d572f7a..5c22274ba3 100644 --- a/samples/Cpp/TestCpp/Classes/Box2DTest/Box2dTest.h +++ b/samples/Cpp/TestCpp/Classes/Box2DTest/Box2dTest.h @@ -21,7 +21,7 @@ public: void addNewSpriteAtPosition(Point p); void update(float dt); - virtual void onTouchesEnded(const std::vector& touches, Event* event); + void onTouchesEnded(const std::vector& touches, Event* event); //CREATE_NODE(Box2DTestLayer); } ; diff --git a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp index 9614180fb2..0cfbabd90c 100644 --- a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp +++ b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp @@ -176,9 +176,6 @@ Box2DView* Box2DView::viewWithEntryID(int entryId) bool Box2DView::initWithEntryID(int entryId) { -// setIsAccelerometerEnabled( true ); -// FIXME cjh: setTouchEnabled( true ); - schedule( schedule_selector(Box2DView::tick) ); m_entry = g_testEntries + entryId; diff --git a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.h b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.h index 681e44de6c..d2afa656d6 100644 --- a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.h +++ b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.h @@ -19,8 +19,8 @@ public: void backCallback(Object* sender); - virtual bool onTouchBegan(Touch* touch, Event* event); - virtual void onTouchMoved(Touch* touch, Event* event); + bool onTouchBegan(Touch* touch, Event* event); + void onTouchMoved(Touch* touch, Event* event); public: static MenuLayer* menuWithEntryID(int entryId); @@ -44,9 +44,9 @@ public: void draw(); // virtual void registerWithTouchDispatcher(); - virtual bool onTouchBegan(Touch* touch, Event* event); - virtual void onTouchMoved(Touch* touch, Event* event); - virtual void onTouchEnded(Touch* touch, Event* event); + bool onTouchBegan(Touch* touch, Event* event); + void onTouchMoved(Touch* touch, Event* event); + void onTouchEnded(Touch* touch, Event* event); //virtual void accelerometer(UIAccelerometer* accelerometer, Acceleration* acceleration); static Box2DView* viewWithEntryID(int entryId); diff --git a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.h b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.h index 41ee2c25c8..d6af9092fe 100644 --- a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.h +++ b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.h @@ -9,8 +9,8 @@ public: static Scene* scene(); virtual bool init(); - virtual void onTouchesMoved(const std::vector& touches, Event * event); - virtual void onTouchesBegan(const std::vector& touches, Event * event); + void onTouchesMoved(const std::vector& touches, Event * event); + void onTouchesBegan(const std::vector& touches, Event * event); void restart(Object* sender); CREATE_FUNC(Bug914Layer); diff --git a/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.cpp b/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.cpp index 7f28de8df8..1d81217bbb 100644 --- a/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.cpp @@ -67,7 +67,12 @@ void BugsTestMainLayer::onEnter() _itmeMenu->setPosition(s_tCurPos); addChild(_itmeMenu); -//cjh setTouchEnabled(true); + + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesBegan = CC_CALLBACK_2(BugsTestMainLayer::onTouchesBegan, this); + listener->onTouchesMoved = CC_CALLBACK_2(BugsTestMainLayer::onTouchesMoved, this); + + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); } void BugsTestMainLayer::onTouchesBegan(const std::vector& touches, Event *event) diff --git a/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.h b/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.h index 88de62b0a1..06e6fb535c 100644 --- a/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.h +++ b/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.h @@ -8,8 +8,8 @@ class BugsTestMainLayer : public Layer public: virtual void onEnter(); - virtual void onTouchesBegan(const std::vector& touches, Event *event); - virtual void onTouchesMoved(const std::vector&touches, Event *event); + void onTouchesBegan(const std::vector& touches, Event *event); + void onTouchesMoved(const std::vector&touches, Event *event); protected: Point _beginPos; diff --git a/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.cpp b/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.cpp index 46cae7762e..bb3ef0e12f 100644 --- a/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.cpp @@ -21,9 +21,14 @@ ChipmunkTestLayer::ChipmunkTestLayer() { #if CC_ENABLE_CHIPMUNK_INTEGRATION // enable events -//FIXME: setTouchEnabled(true); -// setAccelerometerEnabled(true); + auto touchListener = EventListenerTouchAllAtOnce::create(); + touchListener->onTouchesEnded = CC_CALLBACK_2(ChipmunkTestLayer::onTouchesEnded, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(touchListener, this); + + auto accListener = EventListenerAcceleration::create(CC_CALLBACK_2(ChipmunkTestLayer::onAcceleration, this)); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(accListener, this); + // title auto label = LabelTTF::create("Multi touch the screen", "Marker Felt", 36); label->setPosition(Point( VisibleRect::center().x, VisibleRect::top().y - 30)); diff --git a/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.h b/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.h index 3480f0314a..046f53d3fa 100644 --- a/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.h +++ b/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.h @@ -24,7 +24,7 @@ public: void addNewSpriteAtPosition(Point p); void update(float dt); void toggleDebugCallback(Object* sender); - virtual void onTouchesEnded(const std::vector& touches, Event* event); + void onTouchesEnded(const std::vector& touches, Event* event); virtual void onAcceleration(Acceleration* acc, Event* event); private: diff --git a/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.h b/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.h index 7cb8f7a3b4..833ced291e 100644 --- a/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.h +++ b/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.h @@ -13,8 +13,8 @@ class MainLayer : public Layer { public: MainLayer(); - virtual bool onTouchBegan(Touch* touch, Event *event); - virtual void onTouchEnded(Touch* touch, Event *event); + bool onTouchBegan(Touch* touch, Event *event); + void onTouchEnded(Touch* touch, Event *event); }; #endif diff --git a/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.h b/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.h index 0cb0c21d6d..042e51ddf6 100644 --- a/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.h +++ b/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.h @@ -98,7 +98,7 @@ public: virtual std::string title(); virtual std::string subtitle(); void pokeHoleAtPoint(Point point); - virtual void onTouchesBegan(const std::vector& touches, Event *event); + void onTouchesBegan(const std::vector& touches, Event *event); private: ClippingNode* _outerClipper; Node* _holes; @@ -111,9 +111,9 @@ public: virtual std::string title(); virtual std::string subtitle(); virtual void setup(); - virtual void onTouchesBegan(const std::vector& touches, Event *event); - virtual void onTouchesMoved(const std::vector& touches, Event *event); - virtual void onTouchesEnded(const std::vector& touches, Event *event); + void onTouchesBegan(const std::vector& touches, Event *event); + void onTouchesMoved(const std::vector& touches, Event *event); + void onTouchesEnded(const std::vector& touches, Event *event); private: bool _scrolling; Point _lastPoint; diff --git a/samples/Cpp/TestCpp/Classes/CocosDenshionTest/CocosDenshionTest.cpp b/samples/Cpp/TestCpp/Classes/CocosDenshionTest/CocosDenshionTest.cpp index 9589cde586..e83cb001f1 100644 --- a/samples/Cpp/TestCpp/Classes/CocosDenshionTest/CocosDenshionTest.cpp +++ b/samples/Cpp/TestCpp/Classes/CocosDenshionTest/CocosDenshionTest.cpp @@ -227,19 +227,6 @@ _sliderMusicVolume(NULL) addSliders(); schedule(schedule_selector(CocosDenshionTest::updateVolumes)); -// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); - -// std::string testItems[] = { -// "unload effect", -// "pause effect", -// "resume effect", -// "pause all effects", -// "resume all effects", -// "stop all effects" -// }; - -//cjh setTouchEnabled(true); - // preload background music and effect SimpleAudioEngine::getInstance()->preloadBackgroundMusic( MUSIC_FILE ); SimpleAudioEngine::getInstance()->preloadEffect( EFFECT_FILE ); diff --git a/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.cpp b/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.cpp index 83945cd581..f3ad65db63 100644 --- a/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.cpp +++ b/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.cpp @@ -9,7 +9,9 @@ CurlTest::CurlTest() addChild(label, 0); label->setPosition( Point(VisibleRect::center().x, VisibleRect::top().y-50) ); -//FIXME cjh: setTouchEnabled(true); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesEnded = CC_CALLBACK_2(CurlTest::onTouchesEnded, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); // create a label to display the tip string _label = LabelTTF::create("Touch the screen to connect", "Arial", 22); diff --git a/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.h b/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.h index 9688d03e38..131e3b330a 100644 --- a/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.h +++ b/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.h @@ -10,7 +10,7 @@ public: CurlTest(); ~CurlTest(); - virtual void onTouchesEnded(const std::vector& touches, cocos2d::Event *event); + void onTouchesEnded(const std::vector& touches, cocos2d::Event *event); private: cocos2d::LabelTTF* _label; diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp index eaf14546be..fef387ac01 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp @@ -477,7 +477,10 @@ void TestAnimationEvent::callback2() void TestParticleDisplay::onEnter() { ArmatureTestLayer::onEnter(); -//FIXME cjh: setTouchEnabled(true); + + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesEnded = CC_CALLBACK_2(TestParticleDisplay::onTouchesEnded, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); animationID = 0; @@ -534,8 +537,11 @@ void TestParticleDisplay::onTouchesEnded(const std::vector& touches, Eve void TestUseMutiplePicture::onEnter() { ArmatureTestLayer::onEnter(); -//cjh setTouchEnabled(true); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesEnded = CC_CALLBACK_2(TestUseMutiplePicture::onTouchesEnded, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + displayIndex = 0; armature = Armature::create("Knight_f/Knight"); @@ -935,7 +941,10 @@ std::string TestAnchorPoint::title() void TestArmatureNesting::onEnter() { ArmatureTestLayer::onEnter(); -//cjh setTouchEnabled(true); + + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesEnded = CC_CALLBACK_2(TestArmatureNesting::onTouchesEnded, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); armature = Armature::create("cyborg"); armature->getAnimation()->playByIndex(1); diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.h index ccd07d1d72..51b47085b5 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.h @@ -138,7 +138,7 @@ class TestUseMutiplePicture : public ArmatureTestLayer virtual void onExit(); virtual std::string title(); virtual std::string subtitle(); - virtual void onTouchesEnded(const std::vector& touches, Event* event); + void onTouchesEnded(const std::vector& touches, Event* event); int displayIndex; cocostudio::Armature *armature; @@ -150,7 +150,7 @@ class TestParticleDisplay : public ArmatureTestLayer virtual void onExit(); virtual std::string title(); virtual std::string subtitle(); - virtual void onTouchesEnded(const std::vector& touches, Event* event); + void onTouchesEnded(const std::vector& touches, Event* event); int animationID; cocostudio::Armature *armature; @@ -251,7 +251,7 @@ public: virtual void onEnter(); virtual void onExit(); virtual std::string title(); - virtual void onTouchesEnded(const std::vector& touches, Event* event); + void onTouchesEnded(const std::vector& touches, Event* event); cocostudio::Armature *armature; int weaponIndex; diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.h index 671bdf7833..cc5233b539 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.h @@ -13,7 +13,7 @@ protected: virtual ~PlayerController(void); public: - virtual void onTouchesEnded(const std::vector& touches, cocos2d::Event *event) override; + void onTouchesEnded(const std::vector& touches, cocos2d::Event *event) override; public: virtual bool init(); diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.cpp index 7c95239cfa..5ba010f2a9 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.cpp @@ -118,7 +118,11 @@ void ExtensionsMainLayer::onEnter() _itemMenu->addChild(pItem, kItemTagBasic + i); } -//cjh setTouchEnabled(true); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesBegan = CC_CALLBACK_2(ExtensionsMainLayer::onTouchesBegan, this); + listener->onTouchesMoved = CC_CALLBACK_2(ExtensionsMainLayer::onTouchesMoved, this); + + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); addChild(_itemMenu); } diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.h index 0b392e4326..abf411fbce 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.h @@ -8,8 +8,8 @@ class ExtensionsMainLayer : public Layer public: virtual void onEnter(); - virtual void onTouchesBegan(const std::vector& touches, Event *event); - virtual void onTouchesMoved(const std::vector& touches, Event *event); + void onTouchesBegan(const std::vector& touches, Event *event); + void onTouchesMoved(const std::vector& touches, Event *event); Point _beginPos; Menu* _itemMenu; diff --git a/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp b/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp index 7140a44e8a..a8a164da01 100644 --- a/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp +++ b/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp @@ -7,8 +7,12 @@ KeyboardTest::KeyboardTest() addChild(label, 0); label->setPosition( Point(s.width/2, s.height-50) ); -//cjh setKeyboardEnabled(true); - + auto listener = EventListenerKeyboard::create(); + listener->onKeyPressed = CC_CALLBACK_2(KeyboardTest::onKeyPressed, this); + listener->onKeyReleased = CC_CALLBACK_2(KeyboardTest::onKeyReleased, this); + + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + // create a label to display the tip string _label = LabelTTF::create("Please press any key and see console log...", "Arial", 22); _label->setPosition(Point(s.width / 2, s.height / 2)); diff --git a/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp b/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp index 5bda6cccf2..c3191d5724 100644 --- a/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp +++ b/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp @@ -7,8 +7,11 @@ KeypadTest::KeypadTest() addChild(label, 0); label->setPosition( Point(s.width/2, s.height-50) ); -//cjh setKeyboardEnabled(true); - + auto listener = EventListenerKeyboard::create(); + listener->onKeyReleased = CC_CALLBACK_2(KeypadTest::onKeyReleased, this); + + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + // create a label to display the tip string _label = LabelTTF::create("Please press any key...", "Arial", 22); _label->setPosition(Point(s.width / 2, s.height / 2)); diff --git a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp index 52fc92c2e4..f95831074e 100644 --- a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp +++ b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp @@ -1135,8 +1135,13 @@ static float alignmentItemPadding = 50; static float menuItemPaddingCenter = 50; BitmapFontMultiLineAlignment::BitmapFontMultiLineAlignment() { -//cjh this->setTouchEnabled(true); - + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesBegan = CC_CALLBACK_2(BitmapFontMultiLineAlignment::onTouchesBegan, this); + listener->onTouchesMoved = CC_CALLBACK_2(BitmapFontMultiLineAlignment::onTouchesMoved, this); + listener->onTouchesEnded = CC_CALLBACK_2(BitmapFontMultiLineAlignment::onTouchesEnded, this); + + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + // ask director the the window size auto size = Director::getInstance()->getWinSize(); diff --git a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.h b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.h index 49b121628d..87ff938c97 100644 --- a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.h +++ b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.h @@ -228,9 +228,9 @@ public: virtual std::string subtitle(); void stringChanged(Object *sender); void alignmentChanged(Object *sender); - virtual void onTouchesBegan(const std::vector& touches, Event *event); - virtual void onTouchesEnded(const std::vector& touches, Event *event); - virtual void onTouchesMoved(const std::vector& touches, Event *event); + void onTouchesBegan(const std::vector& touches, Event *event); + void onTouchesEnded(const std::vector& touches, Event *event); + void onTouchesMoved(const std::vector& touches, Event *event); public: LabelBMFont *_labelShouldRetain; diff --git a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp index 47173da112..d6d5777ada 100644 --- a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp +++ b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp @@ -650,8 +650,12 @@ static float menuItemPaddingCenter = 50; LabelFNTMultiLineAlignment::LabelFNTMultiLineAlignment() { -//cjh this->setTouchEnabled(true); - + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesBegan = CC_CALLBACK_2(LabelFNTMultiLineAlignment::onTouchesBegan, this); + listener->onTouchesMoved = CC_CALLBACK_2(LabelFNTMultiLineAlignment::onTouchesMoved, this); + listener->onTouchesEnded = CC_CALLBACK_2(LabelFNTMultiLineAlignment::onTouchesEnded, this); + + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); // ask director the the window size auto size = Director::getInstance()->getWinSize(); diff --git a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.h b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.h index 1a8035b15f..d494858ffa 100644 --- a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.h +++ b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.h @@ -156,9 +156,9 @@ public: virtual std::string subtitle(); void stringChanged(Object *sender); void alignmentChanged(Object *sender); - virtual void onTouchesBegan(const std::vector& touches, Event *event); - virtual void onTouchesEnded(const std::vector& touches, Event *event); - virtual void onTouchesMoved(const std::vector& touches, Event *event); + void onTouchesBegan(const std::vector& touches, Event *event); + void onTouchesEnded(const std::vector& touches, Event *event); + void onTouchesMoved(const std::vector& touches, Event *event); public: Label *_labelShouldRetain; diff --git a/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp b/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp index e27b82cf16..9fcc560cdf 100644 --- a/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp +++ b/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp @@ -449,7 +449,12 @@ void LayerTest1::onEnter() { LayerTest::onEnter(); -//cjh setTouchEnabled(true); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesBegan = CC_CALLBACK_2(LayerTest1::onTouchesBegan, this); + listener->onTouchesMoved = CC_CALLBACK_2(LayerTest1::onTouchesMoved, this); + listener->onTouchesEnded = CC_CALLBACK_2(LayerTest1::onTouchesEnded, this); + + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); auto s = Director::getInstance()->getWinSize(); auto layer = LayerColor::create( Color4B(0xFF, 0x00, 0x00, 0x80), 200, 200); @@ -590,7 +595,9 @@ LayerGradientTest::LayerGradientTest() auto layer1 = LayerGradient::create(Color4B(255,0,0,255), Color4B(0,255,0,255), Point(0.9f, 0.9f)); addChild(layer1, 0, kTagLayer); -//cjh setTouchEnabled(true); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesMoved = CC_CALLBACK_2(LayerGradientTest::onTouchesMoved, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); auto label1 = LabelTTF::create("Compressed Interpolation: Enabled", "Marker Felt", 26); auto label2 = LabelTTF::create("Compressed Interpolation: Disabled", "Marker Felt", 26); diff --git a/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.h b/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.h index fbed1e7e91..dd381b001f 100644 --- a/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.h +++ b/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.h @@ -74,9 +74,9 @@ public: void updateSize(Point &touchLocation); - virtual void onTouchesBegan(const std::vector& touches, Event *event); - virtual void onTouchesMoved(const std::vector& touches, Event *event); - virtual void onTouchesEnded(const std::vector& touches, Event *event); + void onTouchesBegan(const std::vector& touches, Event *event); + void onTouchesMoved(const std::vector& touches, Event *event); + void onTouchesEnded(const std::vector& touches, Event *event); }; class LayerTest2 : public LayerTest @@ -99,7 +99,7 @@ class LayerGradientTest : public LayerTest { public: LayerGradientTest(); - virtual void onTouchesMoved(const std::vector& touches, Event *event); + void onTouchesMoved(const std::vector& touches, Event *event); virtual std::string title(); virtual std::string subtitle(); void toggleItem(cocos2d::Object *sender); diff --git a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp index a61f15225b..e1fb43cf39 100644 --- a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp +++ b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp @@ -25,8 +25,13 @@ enum { //------------------------------------------------------------------ MenuLayerMainMenu::MenuLayerMainMenu() { -//cjh setTouchEnabled(true); -// setTouchMode(Touch::DispatchMode::ONE_BY_ONE); + auto listener = EventListenerTouchOneByOne::create(); + listener->onTouchBegan = CC_CALLBACK_2(MenuLayerMainMenu::onTouchBegan, this); + listener->onTouchMoved = CC_CALLBACK_2(MenuLayerMainMenu::onTouchMoved, this); + listener->onTouchEnded = CC_CALLBACK_2(MenuLayerMainMenu::onTouchEnded, this); + listener->onTouchCancelled = CC_CALLBACK_2(MenuLayerMainMenu::onTouchCancelled, this); + + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); // Font Item auto spriteNormal = Sprite::create(s_MenuItem, Rect(0,23*2,115,23)); @@ -532,8 +537,8 @@ BugsTest::BugsTest() void BugsTest::issue1410MenuCallback(Object *sender) { auto menu = static_cast( static_cast(sender)->getParent() ); -//cjh menu->setTouchEnabled(false); -// menu->setTouchEnabled(true); + menu->setEnabled(false); + menu->setEnabled(true); log("NO CRASHES"); } @@ -541,8 +546,8 @@ void BugsTest::issue1410MenuCallback(Object *sender) void BugsTest::issue1410v2MenuCallback(cocos2d::Object *pSender) { auto menu = static_cast( static_cast(pSender)->getParent() ); -//cjh menu->setTouchEnabled(true); -// menu->setTouchEnabled(false); + menu->setEnabled(true); + menu->setEnabled(false); log("NO CRASHES. AND MENU SHOULD STOP WORKING"); } @@ -571,9 +576,6 @@ RemoveMenuItemWhenMove::RemoveMenuItemWhenMove() menu->setPosition(Point(s.width/2, s.height/2)); -//cjh setTouchEnabled(true); -// setTouchMode(Touch::DispatchMode::ONE_BY_ONE); - // Register Touch Event _touchListener = EventListenerTouchOneByOne::create(); _touchListener->setSwallowTouches(false); diff --git a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h index 41072151c5..127f8c1688 100644 --- a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h +++ b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h @@ -14,10 +14,10 @@ public: ~MenuLayerMainMenu(); public: - virtual bool onTouchBegan(Touch *touch, Event * event); - virtual void onTouchEnded(Touch *touch, Event * event); - virtual void onTouchCancelled(Touch *touch, Event * event); - virtual void onTouchMoved(Touch *touch, Event * event); + bool onTouchBegan(Touch *touch, Event * event); + void onTouchEnded(Touch *touch, Event * event); + void onTouchCancelled(Touch *touch, Event * event); + void onTouchMoved(Touch *touch, Event * event); void allowTouches(float dt); void menuCallback(Object* sender); @@ -106,8 +106,8 @@ class RemoveMenuItemWhenMove : public Layer public: RemoveMenuItemWhenMove(); ~RemoveMenuItemWhenMove(); - virtual bool onTouchBegan(Touch *touch, Event *event); - virtual void onTouchMoved(Touch *touch, Event *event); + bool onTouchBegan(Touch *touch, Event *event); + void onTouchMoved(Touch *touch, Event *event); void goBack(Object *pSender); diff --git a/samples/Cpp/TestCpp/Classes/MotionStreakTest/MotionStreakTest.cpp b/samples/Cpp/TestCpp/Classes/MotionStreakTest/MotionStreakTest.cpp index 7c4fe4340f..13a376ec29 100644 --- a/samples/Cpp/TestCpp/Classes/MotionStreakTest/MotionStreakTest.cpp +++ b/samples/Cpp/TestCpp/Classes/MotionStreakTest/MotionStreakTest.cpp @@ -121,8 +121,10 @@ void MotionStreakTest2::onEnter() { MotionStreakTest::onEnter(); -//cjh setTouchEnabled(true); - + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesMoved = CC_CALLBACK_2(MotionStreakTest2::onTouchesMoved, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + auto s = Director::getInstance()->getWinSize(); // create the streak object and add it to the scene diff --git a/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.cpp b/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.cpp index f92fa93299..42e6634de5 100644 --- a/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.cpp +++ b/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.cpp @@ -56,7 +56,11 @@ bool MutiTouchTestLayer::init() { if (Layer::init()) { -//cjh setTouchEnabled(true); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesBegan = CC_CALLBACK_2(MutiTouchTestLayer::onTouchesBegan, this); + listener->onTouchesMoved = CC_CALLBACK_2(MutiTouchTestLayer::onTouchesMoved, this); + listener->onTouchesEnded = CC_CALLBACK_2(MutiTouchTestLayer::onTouchesEnded, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); auto title = LabelTTF::create("Please touch the screen!", "", 24); title->setPosition(VisibleRect::top()+Point(0, -40)); diff --git a/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.h b/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.h index 397f9ae276..91333d6493 100644 --- a/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.h +++ b/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.h @@ -8,7 +8,6 @@ class MutiTouchTestLayer : public Layer public: bool init(); -// virtual void registerWithTouchDispatcher(void); void onTouchesBegan(const std::vector& touches, cocos2d::Event *event); void onTouchesMoved(const std::vector& touches, cocos2d::Event *event); void onTouchesEnded(const std::vector& touches, cocos2d::Event *event); diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp index 797f042ca7..d44129aee5 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp @@ -18,7 +18,8 @@ std::function createFunctions[] = CL(RemoveListenerWhenDispatching), CL(CustomEventTest), CL(LabelKeyboardEventTest), - CL(SpriteAccelerationEventTest) + CL(SpriteAccelerationEventTest), + CL(RemoveAndRetainNodeTest) }; unsigned int TEST_CASE_COUNT = sizeof(createFunctions) / sizeof(createFunctions[0]); @@ -565,4 +566,88 @@ std::string SpriteAccelerationEventTest::title() std::string SpriteAccelerationEventTest::subtitle() { return "Please move your device\n(Only available on mobile)"; +} + +// RemoveAndRetainNodeTest +void RemoveAndRetainNodeTest::onEnter() +{ + _spriteSaved = false; + + EventDispatcherTestDemo::onEnter(); + + auto dispatcher = EventDispatcher::getInstance(); + + Point origin = Director::getInstance()->getVisibleOrigin(); + Size size = Director::getInstance()->getVisibleSize(); + + _sprite = Sprite::create("Images/CyanSquare.png"); + _sprite->setPosition(origin+Point(size.width/2, size.height/2)); + addChild(_sprite, 10); + + // Make sprite1 touchable + auto listener1 = EventListenerTouchOneByOne::create(); + listener1->setSwallowTouches(true); + + listener1->onTouchBegan = [](Touch* touch, Event* event){ + auto target = static_cast(event->getCurrentTarget()); + + Point locationInNode = target->convertToNodeSpace(touch->getLocation()); + Size s = target->getContentSize(); + Rect rect = Rect(0, 0, s.width, s.height); + + if (rect.containsPoint(locationInNode)) + { + log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y); + target->setOpacity(180); + return true; + } + return false; + }; + + listener1->onTouchMoved = [](Touch* touch, Event* event){ + auto target = static_cast(event->getCurrentTarget()); + target->setPosition(target->getPosition() + touch->getDelta()); + }; + + listener1->onTouchEnded = [=](Touch* touch, Event* event){ + auto target = static_cast(event->getCurrentTarget()); + log("sprite onTouchesEnded.. "); + target->setOpacity(255); + }; + + dispatcher->addEventListenerWithSceneGraphPriority(listener1, _sprite); + + this->runAction(Sequence::create(DelayTime::create(5.0f), + CallFunc::create([this](){ + _spriteSaved = true; + _sprite->retain(); + _sprite->removeFromParent(); + }), + DelayTime::create(5.0f), + CallFunc::create([this](){ + _spriteSaved = false; + this->addChild(_sprite); + _sprite->release(); + }), + nullptr + )); +} + +void RemoveAndRetainNodeTest::onExit() +{ + EventDispatcherTestDemo::onExit(); + if (_spriteSaved) + { + _sprite->release(); + } +} + +std::string RemoveAndRetainNodeTest::title() +{ + return "RemoveAndRetainNodeTest"; +} + +std::string RemoveAndRetainNodeTest::subtitle() +{ + return ""; } \ No newline at end of file diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h index 2f5c186438..9af7022bde 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h @@ -83,4 +83,16 @@ public: virtual std::string subtitle() override; }; +class RemoveAndRetainNodeTest : public EventDispatcherTestDemo +{ +public: + virtual void onEnter() override; + virtual void onExit() override; + virtual std::string title() override; + virtual std::string subtitle() override; +private: + Sprite* _sprite; + bool _spriteSaved; +}; + #endif /* defined(__samples__NewEventDispatcherTest__) */ diff --git a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp index db7f53d7c2..3276a9b620 100644 --- a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp @@ -723,7 +723,10 @@ std::string CameraCenterTest::subtitle() //------------------------------------------------------------------ ConvertToNode::ConvertToNode() { -//cjh setTouchEnabled(true); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesEnded = CC_CALLBACK_2(ConvertToNode::onTouchesEnded, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + auto s = Director::getInstance()->getWinSize(); auto rotate = RotateBy::create(10, 360); diff --git a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.h b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.h index b99892e7ee..6bf14f8d5c 100644 --- a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.h +++ b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.h @@ -125,7 +125,7 @@ class ConvertToNode : public TestCocosNodeDemo { public: ConvertToNode(); - virtual void onTouchesEnded(const std::vector& touches, Event *event); + void onTouchesEnded(const std::vector& touches, Event *event); virtual std::string title(); virtual std::string subtitle(); }; diff --git a/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp b/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp index bc2a128603..e14105b62e 100644 --- a/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp @@ -87,7 +87,9 @@ std::string Parallax1::title() Parallax2::Parallax2() { -//cjh setTouchEnabled( true ); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesMoved = CC_CALLBACK_2(Parallax2::onTouchesMoved, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); // Top Layer, a simple image auto cocosImage = Sprite::create(s_Power); diff --git a/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.h b/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.h index 5d4b6da085..905e34beae 100644 --- a/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.h +++ b/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.h @@ -43,7 +43,7 @@ protected: public: Parallax2(); - virtual void onTouchesMoved(const std::vector& touches, Event *event); + void onTouchesMoved(const std::vector& touches, Event *event); virtual std::string title(); }; diff --git a/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp b/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp index 414c915135..9e35114538 100644 --- a/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp @@ -1068,7 +1068,11 @@ void ParticleDemo::onEnter(void) _emitter = NULL; -//cjh setTouchEnabled( true ); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesBegan = CC_CALLBACK_2(ParticleDemo::onTouchesBegan, this); + listener->onTouchesMoved = CC_CALLBACK_2(ParticleDemo::onTouchesMoved, this); + listener->onTouchesEnded = CC_CALLBACK_2(ParticleDemo::onTouchesEnded, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); auto s = Director::getInstance()->getWinSize(); diff --git a/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.h b/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.h index c17d8bc73a..4c9e63047f 100644 --- a/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.h +++ b/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.h @@ -33,9 +33,9 @@ public: void backCallback(Object* sender); void toggleCallback(Object* sender); - virtual void onTouchesBegan(const std::vector& touches, Event *event); - virtual void onTouchesMoved(const std::vector& touches, Event *event); - virtual void onTouchesEnded(const std::vector& touches, Event *event); + void onTouchesBegan(const std::vector& touches, Event *event); + void onTouchesMoved(const std::vector& touches, Event *event); + void onTouchesEnded(const std::vector& touches, Event *event); virtual void update(float dt); void setEmitterPosition(); diff --git a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp index a928a3bd23..5f1e9566ab 100644 --- a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp +++ b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp @@ -119,9 +119,13 @@ std::string TouchesMainScene::title() void TouchesPerformTest1::onEnter() { TouchesMainScene::onEnter(); -//cjh setTouchEnabled(true); -// setTouchMode(Touch::DispatchMode::ONE_BY_ONE); -// setSwallowsTouches(true); + auto listener = EventListenerTouchOneByOne::create(); + listener->setSwallowTouches(true); + listener->onTouchBegan = CC_CALLBACK_2(TouchesPerformTest1::onTouchBegan, this); + listener->onTouchMoved = CC_CALLBACK_2(TouchesPerformTest1::onTouchMoved, this); + listener->onTouchEnded = CC_CALLBACK_2(TouchesPerformTest1::onTouchEnded, this); + listener->onTouchCancelled = CC_CALLBACK_2(TouchesPerformTest1::onTouchCancelled, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); } std::string TouchesPerformTest1::title() @@ -158,7 +162,13 @@ void TouchesPerformTest1::onTouchCancelled(Touch* touch, Event* event) void TouchesPerformTest2::onEnter() { TouchesMainScene::onEnter(); -//cjh setTouchEnabled(true); + + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesBegan = CC_CALLBACK_2(TouchesPerformTest2::onTouchesBegan, this); + listener->onTouchesMoved = CC_CALLBACK_2(TouchesPerformTest2::onTouchesMoved, this); + listener->onTouchesEnded = CC_CALLBACK_2(TouchesPerformTest2::onTouchesEnded, this); + listener->onTouchesCancelled = CC_CALLBACK_2(TouchesPerformTest2::onTouchesCancelled, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); } std::string TouchesPerformTest2::title() @@ -194,18 +204,13 @@ void TouchesPerformTest2::onTouchesCancelled(const std::vector& touches, class TouchableLayer : public Layer { public: - virtual bool onTouchBegan(Touch *touch, Event *event) + bool onTouchBegan(Touch *touch, Event *event) { return false; } - virtual void onTouchMoved(Touch *touch, Event *event) {} - virtual void onTouchEnded(Touch *touch, Event *event) {} - virtual void onTouchCancelled(Touch *touch, Event *event) {} - - virtual void onTouchesBegan(const std::vector& touches, Event *event) {} - virtual void onTouchesMoved(const std::vector& touches, Event *event) {} - virtual void onTouchesEnded(const std::vector& touches, Event *event) {} - virtual void onTouchesCancelled(const std::vector&touches, Event *event) {} + void onTouchMoved(Touch *touch, Event *event) {} + void onTouchEnded(Touch *touch, Event *event) {} + void onTouchCancelled(Touch *touch, Event *event) {} }; @@ -231,8 +236,14 @@ void TouchesPerformTest3::onEnter() { int zorder = rand() % TOUCHABLE_NODE_NUM; auto layer = new TouchableLayer(); -//cjh layer->setTouchEnabled(true); -// layer->setTouchMode(Touch::DispatchMode::ONE_BY_ONE); + + auto listener = EventListenerTouchOneByOne::create(); + listener->onTouchBegan = CC_CALLBACK_2(TouchableLayer::onTouchBegan, layer); + listener->onTouchMoved = CC_CALLBACK_2(TouchableLayer::onTouchMoved, layer); + listener->onTouchEnded = CC_CALLBACK_2(TouchableLayer::onTouchEnded, layer); + listener->onTouchCancelled = CC_CALLBACK_2(TouchableLayer::onTouchCancelled, layer); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, layer); + addChild(layer, zorder); layer->release(); } diff --git a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.h b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.h index 5cb4d35ad9..11a93ca3ee 100644 --- a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.h +++ b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.h @@ -36,10 +36,10 @@ public: virtual void onEnter() override; virtual std::string title() override; - virtual bool onTouchBegan(Touch* touch, Event* event) ; - virtual void onTouchMoved(Touch* touch, Event* event) ; - virtual void onTouchEnded(Touch* touch, Event* event) ; - virtual void onTouchCancelled(Touch* touch, Event* event) ; + bool onTouchBegan(Touch* touch, Event* event) ; + void onTouchMoved(Touch* touch, Event* event) ; + void onTouchEnded(Touch* touch, Event* event) ; + void onTouchCancelled(Touch* touch, Event* event) ; }; class TouchesPerformTest2 : public TouchesMainScene diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp index 4c56986746..59513a0835 100644 --- a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp @@ -197,8 +197,13 @@ void PhysicsDemoClickAdd::onEnter() PhysicsDemo::onEnter(); #ifdef CC_USE_PHYSICS -//cjh setTouchEnabled(true); -// setAccelerometerEnabled(true); + + auto touchListener = EventListenerTouchAllAtOnce::create(); + touchListener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoClickAdd::onTouchesEnded, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(touchListener, this); + + auto accListener = EventListenerAcceleration::create(CC_CALLBACK_2(PhysicsDemoClickAdd::onAcceleration, this)); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(accListener, this); auto node = Node::create(); node->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size)); diff --git a/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp b/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp index 1669b78c1f..113d4adad3 100644 --- a/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp +++ b/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp @@ -111,8 +111,11 @@ RenderTextureSave::RenderTextureSave() _brush->retain(); _brush->setColor(Color3B::RED); _brush->setOpacity(20); -//cjh this->setTouchEnabled(true); - + + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesMoved = CC_CALLBACK_2(RenderTextureSave::onTouchesMoved, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + // Save Image menu MenuItemFont::setFontSize(16); auto item1 = MenuItemFont::create("Save Image", CC_CALLBACK_1(RenderTextureSave::saveImage, this)); @@ -296,7 +299,12 @@ void RenderTextureScene::runThisTest() RenderTextureZbuffer::RenderTextureZbuffer() { -//cjh this->setTouchEnabled(true); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesBegan = CC_CALLBACK_2(RenderTextureZbuffer::onTouchesBegan, this); + listener->onTouchesMoved = CC_CALLBACK_2(RenderTextureZbuffer::onTouchesMoved, this); + listener->onTouchesEnded = CC_CALLBACK_2(RenderTextureZbuffer::onTouchesEnded, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + auto size = Director::getInstance()->getWinSize(); auto label = LabelTTF::create("vertexZ = 50", "Marker Felt", 64); label->setPosition(Point(size.width / 2, size.height * 0.25f)); @@ -627,7 +635,9 @@ void SpriteRenderTextureBug::SimpleSprite::draw() SpriteRenderTextureBug::SpriteRenderTextureBug() { -//cjh setTouchEnabled(true); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesEnded = CC_CALLBACK_2(SpriteRenderTextureBug::onTouchesEnded, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords(Point(s.width/2, s.height/2)); diff --git a/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.h b/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.h index b6ce107092..afd985ec70 100644 --- a/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.h +++ b/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.h @@ -24,7 +24,7 @@ public: ~RenderTextureSave(); virtual std::string title(); virtual std::string subtitle(); - virtual void onTouchesMoved(const std::vector& touches, Event* event); + void onTouchesMoved(const std::vector& touches, Event* event); void clearImage(Object *pSender); void saveImage(Object *pSender); @@ -52,9 +52,9 @@ class RenderTextureZbuffer : public RenderTextureTest public: RenderTextureZbuffer(); - virtual void onTouchesMoved(const std::vector& touches, Event* event); - virtual void onTouchesBegan(const std::vector& touches, Event* event); - virtual void onTouchesEnded(const std::vector& touches, Event* event); + void onTouchesMoved(const std::vector& touches, Event* event); + void onTouchesBegan(const std::vector& touches, Event* event); + void onTouchesEnded(const std::vector& touches, Event* event); virtual std::string title(); virtual std::string subtitle(); @@ -116,7 +116,7 @@ class SimpleSprite : public Sprite public: SpriteRenderTextureBug(); - virtual void onTouchesEnded(const std::vector& touches, Event* event); + void onTouchesEnded(const std::vector& touches, Event* event); virtual std::string title(); virtual std::string subtitle(); diff --git a/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id b/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id index a3d466373a..ee1480acae 100644 --- a/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id +++ b/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id @@ -1 +1 @@ -4701ba8fb99f70629c5575dcf6fb3536543ac0b6 \ No newline at end of file +fdf35bd639fd5d617e5a280530d6aefe63b94857 \ No newline at end of file diff --git a/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.h b/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.h index 413ba75e8b..ca08fb64e3 100644 --- a/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.h +++ b/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.h @@ -39,7 +39,7 @@ class SpriteBatchNode1: public SpriteTestDemo public: SpriteBatchNode1(); void addNewSpriteWithCoords(Point p); - virtual void onTouchesEnded(const std::vector& touches, Event* event); + void onTouchesEnded(const std::vector& touches, Event* event); virtual std::string title(); }; diff --git a/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp b/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp index 1645d017d8..b922271656 100644 --- a/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp +++ b/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp @@ -126,14 +126,10 @@ void TextInputTest::onEnter() KeyboardNotificationLayer::KeyboardNotificationLayer() : _trackNode(0) { -//cjh setTouchEnabled(true); - // Register Touch Event auto listener = EventListenerTouchOneByOne::create(); - listener->onTouchBegan = CC_CALLBACK_2(KeyboardNotificationLayer::onTouchBegan, this); listener->onTouchEnded = CC_CALLBACK_2(KeyboardNotificationLayer::onTouchEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); } diff --git a/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.h b/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.h index 65cfb27d9a..7da7b893a1 100644 --- a/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.h +++ b/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.h @@ -37,7 +37,6 @@ public: virtual std::string subtitle() = 0; virtual void onClickTrackNode(bool bClicked) = 0; -// virtual void registerWithTouchDispatcher(); virtual void keyboardWillShow(IMEKeyboardNotificationInfo& info); // Layer diff --git a/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp b/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp index 7be2b29c32..48ed771306 100644 --- a/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp +++ b/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp @@ -1430,7 +1430,9 @@ Layer* restartTileMapAction() TileDemo::TileDemo(void) : BaseTest() { -//cjh setTouchEnabled( true ); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesMoved = CC_CALLBACK_2(TileDemo::onTouchesMoved, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); } TileDemo::~TileDemo(void) diff --git a/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.h b/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.h index e0039525a3..3331292415 100644 --- a/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.h +++ b/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.h @@ -18,7 +18,7 @@ public: void nextCallback(Object* sender); void backCallback(Object* sender); - virtual void onTouchesMoved(const std::vector& touches, Event *event); + void onTouchesMoved(const std::vector& touches, Event *event); }; class TileMapTest : public TileDemo diff --git a/samples/Cpp/TestCpp/Classes/controller.cpp b/samples/Cpp/TestCpp/Classes/controller.cpp index c7b03f1acf..fcd140e4f3 100644 --- a/samples/Cpp/TestCpp/Classes/controller.cpp +++ b/samples/Cpp/TestCpp/Classes/controller.cpp @@ -120,8 +120,6 @@ TestController::TestController() _itemMenu->setPosition(s_tCurPos); addChild(_itemMenu); -//cjh setTouchEnabled(true); - addChild(menu, 1); // Register Touch Event From 6f9bee5b1a0b2ee66e6614e3f0a31817506b670e Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 23 Oct 2013 16:28:38 +0800 Subject: [PATCH 06/42] bug fix in EventDispatcher. --- cocos/2d/CCEventDispatcher.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index 05a2a66ddf..ca28314d8c 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -436,7 +436,7 @@ void EventDispatcher::setPriority(EventListener* listener, int fixedPriority) auto found = std::find(fixedPriorityListeners->begin(), fixedPriorityListeners->end(), listener); if (found != fixedPriorityListeners->end()) { - CCASSERT(listener->_node, "Can't set fixed priority with scene graph based listener."); + CCASSERT(listener->_node == nullptr, "Can't set fixed priority with scene graph based listener."); if (listener->_fixedPriority != fixedPriority) { From b131e8c5095cccaa97ce3d08729bef76a5875213 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 23 Oct 2013 16:28:54 +0800 Subject: [PATCH 07/42] Update menu test. --- .../Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp | 19 +++++++++---------- .../Cpp/TestCpp/Classes/MenuTest/MenuTest.h | 1 + 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp index e1fb43cf39..7edfb6fe4e 100644 --- a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp +++ b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp @@ -25,13 +25,14 @@ enum { //------------------------------------------------------------------ MenuLayerMainMenu::MenuLayerMainMenu() { - auto listener = EventListenerTouchOneByOne::create(); - listener->onTouchBegan = CC_CALLBACK_2(MenuLayerMainMenu::onTouchBegan, this); - listener->onTouchMoved = CC_CALLBACK_2(MenuLayerMainMenu::onTouchMoved, this); - listener->onTouchEnded = CC_CALLBACK_2(MenuLayerMainMenu::onTouchEnded, this); - listener->onTouchCancelled = CC_CALLBACK_2(MenuLayerMainMenu::onTouchCancelled, this); + _touchListener = EventListenerTouchOneByOne::create(); + _touchListener->setSwallowTouches(true); + _touchListener->onTouchBegan = CC_CALLBACK_2(MenuLayerMainMenu::onTouchBegan, this); + _touchListener->onTouchMoved = CC_CALLBACK_2(MenuLayerMainMenu::onTouchMoved, this); + _touchListener->onTouchEnded = CC_CALLBACK_2(MenuLayerMainMenu::onTouchEnded, this); + _touchListener->onTouchCancelled = CC_CALLBACK_2(MenuLayerMainMenu::onTouchCancelled, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + EventDispatcher::getInstance()->addEventListenerWithFixedPriority(_touchListener, 1); // Font Item auto spriteNormal = Sprite::create(s_MenuItem, Rect(0,23*2,115,23)); @@ -152,8 +153,7 @@ void MenuLayerMainMenu::menuCallbackConfig(Object* sender) void MenuLayerMainMenu::allowTouches(float dt) { -// auto director = Director::getInstance(); -// director->getTouchDispatcher()->setPriority(Menu::HANDLER_PRIORITY+1, this); + EventDispatcher::getInstance()->setPriority(_touchListener, 1); unscheduleAllSelectors(); log("TOUCHES ALLOWED AGAIN"); } @@ -161,8 +161,7 @@ void MenuLayerMainMenu::allowTouches(float dt) void MenuLayerMainMenu::menuCallbackDisabled(Object* sender) { // hijack all touch events for 5 seconds -// auto director = Director::getInstance(); -// director->getTouchDispatcher()->setPriority(Menu::HANDLER_PRIORITY-1, this); + EventDispatcher::getInstance()->setPriority(_touchListener, -1); schedule(schedule_selector(MenuLayerMainMenu::allowTouches), 5.0f); log("TOUCHES DISABLED FOR 5 SECONDS"); } diff --git a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h index 127f8c1688..3a7b09eec9 100644 --- a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h +++ b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h @@ -8,6 +8,7 @@ class MenuLayerMainMenu : public Layer { protected: MenuItem* _disabledItem; + EventListenerTouchOneByOne* _touchListener; public: MenuLayerMainMenu(void); From a56475e872f9c96a44596f72b9fdfabf1f6a24a8 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 23 Oct 2013 16:35:37 +0800 Subject: [PATCH 08/42] Updating jsb. --- .../javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id | 2 +- cocos/scripting/javascript/bindings/cocos2d_specifics.hpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id b/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id index 082d55b8bd..4caa15159c 100644 --- a/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id +++ b/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id @@ -1 +1 @@ -33fef8c7bc7006ad55c27fd0ed9c9dd2c8064079 \ No newline at end of file +0d38b7e53d97995151ea296bdd1f4ed768ce2f9b \ No newline at end of file diff --git a/cocos/scripting/javascript/bindings/cocos2d_specifics.hpp b/cocos/scripting/javascript/bindings/cocos2d_specifics.hpp index 2981a726a8..f7e9f2bdb4 100644 --- a/cocos/scripting/javascript/bindings/cocos2d_specifics.hpp +++ b/cocos/scripting/javascript/bindings/cocos2d_specifics.hpp @@ -199,7 +199,8 @@ private: typedef std::pair TouchDelegatePair; static TouchDelegateMap sTouchDelegateMap; bool _needUnroot; - EventListenerTouch* _touchListener; + EventListenerTouchOneByOne* _touchListenerOneByOne; + EventListenerTouchAllAtOnce* _touchListenerAllAtOnce; }; From e59c49a5e3c06609877796d562c03e5776ff01db Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 23 Oct 2013 17:13:03 +0800 Subject: [PATCH 09/42] fix crash. --- cocos/2d/CCEventDispatcher.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index ca28314d8c..71e7b36704 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -232,7 +232,8 @@ void EventDispatcher::cleanTarget(Node* node) if (listenerIter != _nodeListenersMap.end()) { auto listeners = listenerIter->second; - for (auto& l : *listeners) + auto listenersCopy = *listeners; + for (auto& l : listenersCopy) { removeEventListener(l); } From aeef62d893be38a43e971112a8ffd219e1e9a351 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 23 Oct 2013 17:26:14 +0800 Subject: [PATCH 10/42] bug fix for EventDispatcher. --- cocos/2d/CCEventDispatcher.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index 71e7b36704..9df198f8e2 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -161,11 +161,7 @@ void EventDispatcher::destroyInstance() } void EventDispatcher::visitTarget(Node* node) -{ - // Reset priority index - s_eventPriorityIndex = 0; - _nodePriorityMap.clear(); - +{ int i = 0; Array* children = node->getChildren(); int childrenCount = children ? children->count() : 0; @@ -898,20 +894,23 @@ void EventDispatcher::sortEventListenersOfSceneGraphPriority(const std::string& return; Node* rootNode = (Node*)Director::getInstance()->getRunningScene(); - + // Reset priority index + s_eventPriorityIndex = 0; + _nodePriorityMap.clear(); + visitTarget(rootNode); // After sort: priority < 0, > 0 auto sceneGraphlisteners = listeners->getSceneGraphPriorityListeners(); std::sort(sceneGraphlisteners->begin(), sceneGraphlisteners->end(), [this](const EventListener* l1, const EventListener* l2) { - return _nodePriorityMap[l1->_node] >= _nodePriorityMap[l2->_node]; + return _nodePriorityMap[l1->_node] > _nodePriorityMap[l2->_node]; }); #if DUMP_LISTENER_ITEM_PRIORITY_INFO log("-----------------------------------"); for (auto& l : *sceneGraphlisteners) { - log("listener priority: node ([%s]%p), fixed (%d)", typeid(*l->_node).name(), l->_node, l->_fixedPriority); + log("listener priority: node ([%s]%p), priority (%d)", typeid(*l->_node).name(), l->_node, _nodePriorityMap[l->_node]); } #endif } From 5ecb664a58f34d80efa44846fab131234dad6008 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 24 Oct 2013 11:17:29 +0800 Subject: [PATCH 11/42] crash and memory leak fix. --- cocos/2d/CCEventDispatcher.cpp | 17 +++++++++-------- cocos/2d/CCEventListener.h | 1 + cocos/2d/CCMenu.cpp | 4 ++++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index 9df198f8e2..607c82181f 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -367,14 +367,13 @@ void EventDispatcher::removeEventListener(EventListener* listener) auto l = *iter; if (l == listener) { - CC_SAFE_RETAIN(listener); + CC_SAFE_RETAIN(l); l->_isRegistered = false; if (l->_node != nullptr) { - dissociateNodeAndEventListener(l->_node, listener); + dissociateNodeAndEventListener(l->_node, l); } - l->release(); if (_inDispatch == 0) { listeners->erase(iter); @@ -391,7 +390,7 @@ void EventDispatcher::removeEventListener(EventListener* listener) auto listeners = iter->second; auto fixedPriorityListeners = listeners->getFixedPriorityListeners(); auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners(); - + removeListenerInVector(sceneGraphPriorityListeners); if (!isFound) { @@ -459,7 +458,7 @@ void EventDispatcher::dispatchEventToListeners(EventListenerVector* listeners, s for (; !fixedPriorityListeners->empty() && i < listeners->getGt0Index(); ++i) { auto l = fixedPriorityListeners->at(i); - if (!l->isPaused() && onEvent(l)) + if (!l->isPaused() && l->isRegistered() && onEvent(l)) { shouldStopPropagation = true; break; @@ -472,9 +471,9 @@ void EventDispatcher::dispatchEventToListeners(EventListenerVector* listeners, s if (!shouldStopPropagation) { // priority == 0, scene graph priority - for (auto& listener : *sceneGraphPriorityListeners) + for (auto& l : *sceneGraphPriorityListeners) { - if (!listener->isPaused() && onEvent(listener)) + if (!l->isPaused() && l->isRegistered() && onEvent(l)) { shouldStopPropagation = true; break; @@ -492,7 +491,7 @@ void EventDispatcher::dispatchEventToListeners(EventListenerVector* listeners, s { auto l = fixedPriorityListeners->at(i); - if (!l->isPaused() && onEvent(fixedPriorityListeners->at(i))) + if (!l->isPaused() && l->isRegistered() && onEvent(fixedPriorityListeners->at(i))) { shouldStopPropagation = true; break; @@ -794,6 +793,7 @@ void EventDispatcher::updateListeners() if (!l->_isRegistered) { iter = sceneGraphPriorityListeners->erase(iter); + l->release(); } else { @@ -810,6 +810,7 @@ void EventDispatcher::updateListeners() if (!l->_isRegistered) { iter = fixedPriorityListeners->erase(iter); + l->release(); } else { diff --git a/cocos/2d/CCEventListener.h b/cocos/2d/CCEventListener.h index f68f9694f9..672d9c98d0 100644 --- a/cocos/2d/CCEventListener.h +++ b/cocos/2d/CCEventListener.h @@ -62,6 +62,7 @@ public: virtual EventListener* clone() = 0; inline bool isPaused() const { return _paused; }; + inline bool isRegistered() const { return _isRegistered; }; protected: std::function _onEvent; /// Event callback function diff --git a/cocos/2d/CCMenu.cpp b/cocos/2d/CCMenu.cpp index 3105a2d8a3..5a46765d91 100644 --- a/cocos/2d/CCMenu.cpp +++ b/cocos/2d/CCMenu.cpp @@ -259,22 +259,26 @@ bool Menu::onTouchBegan(Touch* touch, Event* event) void Menu::onTouchEnded(Touch* touch, Event* event) { CCASSERT(_state == Menu::State::TRACKING_TOUCH, "[Menu ccTouchEnded] -- invalid state"); + this->retain(); if (_selectedItem) { _selectedItem->unselected(); _selectedItem->activate(); } _state = Menu::State::WAITING; + this->release(); } void Menu::onTouchCancelled(Touch* touch, Event* event) { CCASSERT(_state == Menu::State::TRACKING_TOUCH, "[Menu ccTouchCancelled] -- invalid state"); + this->retain(); if (_selectedItem) { _selectedItem->unselected(); } _state = Menu::State::WAITING; + this->release(); } void Menu::onTouchMoved(Touch* touch, Event* event) From dc6e201eb61a7ae0bb099c21ad47bb8f55d36cc5 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 24 Oct 2013 17:27:22 +0800 Subject: [PATCH 12/42] bug fix for eventdispatcher. --- cocos/2d/CCEventDispatcher.cpp | 32 ++++++++++++++++++++++++++++---- cocos/2d/CCEventDispatcher.h | 2 ++ cocos/2d/CCNode.cpp | 2 +- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index 607c82181f..817134ec37 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -115,7 +115,7 @@ void EventDispatcher::EventListenerVector::push_back(EventListener* listener) } } -void EventDispatcher::EventListenerVector::clear() +void EventDispatcher::EventListenerVector::clearSceneGraphListeners() { if (_sceneGraphListeners) { @@ -123,7 +123,10 @@ void EventDispatcher::EventListenerVector::clear() delete _sceneGraphListeners; _sceneGraphListeners = nullptr; } - +} + +void EventDispatcher::EventListenerVector::clearFixedListeners() +{ if (_fixedListeners) { _fixedListeners->clear(); @@ -132,6 +135,12 @@ void EventDispatcher::EventListenerVector::clear() } } +void EventDispatcher::EventListenerVector::clear() +{ + clearSceneGraphListeners(); + clearFixedListeners(); +} + EventDispatcher::EventDispatcher() : _inDispatch(0) @@ -270,6 +279,7 @@ void EventDispatcher::dissociateNodeAndEventListener(Node* node, EventListener* if (listeners->empty()) { _nodeListenersMap.erase(found); + delete listeners; } } } @@ -317,7 +327,6 @@ void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* list return; listener->_node = node; -// listener->_node->retain(); listener->_fixedPriority = 0; listener->retain(); @@ -377,6 +386,7 @@ void EventDispatcher::removeEventListener(EventListener* listener) if (_inDispatch == 0) { listeners->erase(iter); + CC_SAFE_RELEASE(l); } isFound = true; @@ -819,6 +829,16 @@ void EventDispatcher::updateListeners() } } + if (sceneGraphPriorityListeners && sceneGraphPriorityListeners->empty()) + { + listeners->clearSceneGraphListeners(); + } + + if (fixedPriorityListeners && fixedPriorityListeners->empty()) + { + listeners->clearFixedListeners(); + } + if (listenersIter->second->empty()) { _priorityDirtyFlagMap.erase(listenersIter->first); @@ -1047,7 +1067,11 @@ bool EventDispatcher::isEnabled() const void EventDispatcher::setDirtyForNode(Node* node) { - _dirtyNodes.insert(node); + // Mark the node dirty only when there was an eventlistener associates with it. + if (_nodeListenersMap.find(node) != _nodeListenersMap.end()) + { + _dirtyNodes.insert(node); + } } void EventDispatcher::setDirtyForEventType(const std::string& eventType, DirtyFlag flag) diff --git a/cocos/2d/CCEventDispatcher.h b/cocos/2d/CCEventDispatcher.h index dc70491075..75f4f658af 100644 --- a/cocos/2d/CCEventDispatcher.h +++ b/cocos/2d/CCEventDispatcher.h @@ -139,6 +139,8 @@ private: bool empty() const; void push_back(EventListener* item); + void clearSceneGraphListeners(); + void clearFixedListeners(); void clear(); inline std::vector* getFixedPriorityListeners() const { return _fixedListeners; }; diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 8b9d41f9ec..ee19921a8d 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -640,7 +640,7 @@ void Node::addChild(Node *child, int zOrder, int tag) } } - EventDispatcher::getInstance()->setDirtyForNode(this); + EventDispatcher::getInstance()->setDirtyForNode(child); } void Node::addChild(Node *child, int zOrder) From 9632cd7949fa516e031190ea2e0ad84d6e6f021e Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 24 Oct 2013 17:27:57 +0800 Subject: [PATCH 13/42] memoryleak fix. --- cocos/2d/CCLayer.cpp | 9 ++++++++- cocos/2d/CCMenuItem.cpp | 18 +++++++++++++++++- cocos/2d/CCMenuItem.h | 4 ++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/cocos/2d/CCLayer.cpp b/cocos/2d/CCLayer.cpp index 717570e6fe..ed5caf3a71 100644 --- a/cocos/2d/CCLayer.cpp +++ b/cocos/2d/CCLayer.cpp @@ -650,7 +650,14 @@ LayerMultiplex::LayerMultiplex() } LayerMultiplex::~LayerMultiplex() { - CC_SAFE_RELEASE(_layers); + if (_layers) + { + for (auto& item : *_layers) + { + static_cast(item)->cleanup(); + } + _layers->release(); + } } LayerMultiplex * LayerMultiplex::create(Layer * layer, ...) diff --git a/cocos/2d/CCMenuItem.cpp b/cocos/2d/CCMenuItem.cpp index c735677e9f..24ec16a3a7 100644 --- a/cocos/2d/CCMenuItem.cpp +++ b/cocos/2d/CCMenuItem.cpp @@ -410,6 +410,15 @@ MenuItemFont * MenuItemFont::create(const char *value) return pRet; } +MenuItemFont::MenuItemFont() + : _fontSize(0), _fontName("") +{} + +MenuItemFont::~MenuItemFont() +{ + CCLOGINFO("In the destructor of MenuItemFont (%p).", this); +} + // XXX: deprecated bool MenuItemFont::initWithString(const char *value, Object* target, SEL_MenuHandler selector) { @@ -925,7 +934,14 @@ void MenuItemToggle::addSubItem(MenuItem *item) MenuItemToggle::~MenuItemToggle() { - CC_SAFE_RELEASE(_subItems); + if (_subItems) + { + for (auto& item : *_subItems) + { + static_cast(item)->cleanup(); + } + _subItems->release(); + } } void MenuItemToggle::setSelectedIndex(unsigned int index) diff --git a/cocos/2d/CCMenuItem.h b/cocos/2d/CCMenuItem.h index 021832df30..78b8d333ef 100644 --- a/cocos/2d/CCMenuItem.h +++ b/cocos/2d/CCMenuItem.h @@ -241,12 +241,12 @@ public: /** * @js ctor */ - MenuItemFont() : _fontSize(0), _fontName(""){} + MenuItemFont(); /** * @js NA * @lua NA */ - virtual ~MenuItemFont(){} + virtual ~MenuItemFont(); /** initializes a menu item from a string with a target/selector */ CC_DEPRECATED_ATTRIBUTE bool initWithString(const char *value, Object* target, SEL_MenuHandler selector); From 7854a13278ef39c65bec236176a3de9957bde8a6 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 24 Oct 2013 17:28:51 +0800 Subject: [PATCH 14/42] Small fix for Bug-422.cpp and MenuTest.cpp. --- samples/Cpp/TestCpp/Classes/BugsTest/Bug-422.cpp | 2 +- samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-422.cpp b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-422.cpp index 4ce9ce9190..6f5f7eb542 100644 --- a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-422.cpp +++ b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-422.cpp @@ -28,7 +28,7 @@ void Bug422Layer::reset() // => CRASH BOOM BANG auto node = getChildByTag(localtag-1); log("Menu: %p", node); - removeChild(node, false); + removeChild(node, true); // [self removeChildByTag:localtag-1 cleanup:NO]; auto item1 = MenuItemFont::create("One", CC_CALLBACK_1(Bug422Layer::menuCallback, this) ); diff --git a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp index 7edfb6fe4e..d295de75bc 100644 --- a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp +++ b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp @@ -138,6 +138,7 @@ void MenuLayerMainMenu::onTouchMoved(Touch *touch, Event * event) MenuLayerMainMenu::~MenuLayerMainMenu() { + EventDispatcher::getInstance()->removeEventListener(_touchListener); _disabledItem->release(); } From 9c4bc9e8ede2095b57abe3843cd174c3b27303e6 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 24 Oct 2013 17:46:13 +0800 Subject: [PATCH 15/42] Update physicsTest. --- samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp | 9 +++++++-- samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h | 6 ++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp index 59513a0835..f9b9d209de 100644 --- a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp @@ -511,7 +511,10 @@ PhysicsDemoRayCast::PhysicsDemoRayCast() void PhysicsDemoRayCast::onEnter() { PhysicsDemo::onEnter(); - setTouchEnabled(true); + + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoRayCast::onTouchesEnded, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); _scene->getPhysicsWorld()->setGravity(Point::ZERO); @@ -712,7 +715,9 @@ void PhysicsDemoJoints::onEnter() { PhysicsDemo::onEnter(); - setTouchEnabled(true); + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoJoints::onTouchesEnded, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); _scene->getPhysicsWorld()->setGravity(Point::ZERO); diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h index 7dbb28617b..6d0d9a46c0 100644 --- a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h @@ -90,7 +90,7 @@ public: void onEnter() override; std::string title() override; void update(float delta) override; - void onTouchesEnded(const std::vector& touches, Event* event) override; + void onTouchesEnded(const std::vector& touches, Event* event); void changeModeCallback(Object* sender); @@ -110,9 +110,7 @@ public: public: void onEnter() override; std::string title() override; - -private: - PhysicsShape* _touchesShape; + void onTouchesEnded(const std::vector& touches, Event* event) override; }; #endif From b5cac51f97ef1cd2f2fc3405c518dc4a6cf5d1dc Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 24 Oct 2013 17:58:30 +0800 Subject: [PATCH 16/42] PhysicsTest update. --- samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp | 5 +++++ samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp index f9b9d209de..874f5ebe61 100644 --- a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp @@ -722,6 +722,11 @@ void PhysicsDemoJoints::onEnter() _scene->getPhysicsWorld()->setGravity(Point::ZERO); +} + +void PhysicsDemoJoints::onTouchesEnded(const std::vector& touches, Event* event) +{ + } std::string PhysicsDemoJoints::title() diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h index 6d0d9a46c0..bb737b497d 100644 --- a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h @@ -41,9 +41,9 @@ public: Sprite* makeBox(float x, float y, Size size, PhysicsMaterial material = PhysicsMaterial(1.0f, 1.0f, 1.0f)); Sprite* makeTriangle(float x, float y, Size size, PhysicsMaterial material = PhysicsMaterial(1.0f, 1.0f, 1.0f)); - void onTouchesBegan(const std::vector& touches, Event* event) override; - void onTouchesMoved(const std::vector& touches, Event* event) override; - void onTouchesEnded(const std::vector& touches, Event* event) override; + void onTouchesBegan(const std::vector& touches, Event* event); + void onTouchesMoved(const std::vector& touches, Event* event); + void onTouchesEnded(const std::vector& touches, Event* event); protected: Texture2D* _spriteTexture; // weak ref @@ -110,7 +110,7 @@ public: public: void onEnter() override; std::string title() override; - void onTouchesEnded(const std::vector& touches, Event* event) override; + void onTouchesEnded(const std::vector& touches, Event* event); }; #endif From c4f9acbee71f37f7b43bc0f3a0fc165754540ab8 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 24 Oct 2013 20:50:21 +0800 Subject: [PATCH 17/42] disable DUMP_LISTENER_ITEM_PRIORITY_INFO. use std::unordered_map instead of std::map. --- cocos/2d/CCEventDispatcher.cpp | 3 ++- cocos/2d/CCEventDispatcher.h | 21 ++++++++++----------- cocos/2d/CCNode.cpp | 2 -- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index 817134ec37..f7d593fb8f 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -31,7 +31,7 @@ #include -#define DUMP_LISTENER_ITEM_PRIORITY_INFO 1 +#define DUMP_LISTENER_ITEM_PRIORITY_INFO 0 namespace { @@ -229,6 +229,7 @@ void EventDispatcher::resumeTarget(Node* node) l->_paused = false; } } + setDirtyForNode(node); } void EventDispatcher::cleanTarget(Node* node) diff --git a/cocos/2d/CCEventDispatcher.h b/cocos/2d/CCEventDispatcher.h index 75f4f658af..26d3154a65 100644 --- a/cocos/2d/CCEventDispatcher.h +++ b/cocos/2d/CCEventDispatcher.h @@ -30,7 +30,7 @@ #include #include -#include +#include #include #include @@ -114,12 +114,6 @@ public: void setDirtyForNode(Node* node); - void setDirtyForEventType(const std::string& eventType, DirtyFlag flag); - - DirtyFlag isDirtyForEventType(const std::string& eventType); - - void visitTarget(Node* node); - void pauseTarget(Node* node); void resumeTarget(Node* node); void cleanTarget(Node* node); @@ -181,16 +175,21 @@ private: void dispatchEventToListeners(EventListenerVector* listeners, std::function onEvent); + void setDirtyForEventType(const std::string& eventType, DirtyFlag flag); + DirtyFlag isDirtyForEventType(const std::string& eventType); + + void visitTarget(Node* node); + private: /** * Listeners map. */ - std::map _listeners; + std::unordered_map _listeners; - std::map _priorityDirtyFlagMap; + std::unordered_map _priorityDirtyFlagMap; - std::map*> _nodeListenersMap; - std::map _nodePriorityMap; + std::unordered_map*> _nodeListenersMap; + std::unordered_map _nodePriorityMap; std::vector _toAddedListeners; std::set _dirtyNodes; diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index ee19921a8d..427884abb0 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -639,8 +639,6 @@ void Node::addChild(Node *child, int zOrder, int tag) child->onEnterTransitionDidFinish(); } } - - EventDispatcher::getInstance()->setDirtyForNode(child); } void Node::addChild(Node *child, int zOrder) From 27b71ba73c7413896d64157790991894da0f2b07 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 25 Oct 2013 10:35:48 +0800 Subject: [PATCH 18/42] Use integer as event type and listener type. --- cocos/2d/CCEvent.cpp | 2 +- cocos/2d/CCEvent.h | 16 +- cocos/2d/CCEventAcceleration.cpp | 4 +- cocos/2d/CCEventAcceleration.h | 3 +- cocos/2d/CCEventCustom.cpp | 6 +- cocos/2d/CCEventCustom.h | 2 +- cocos/2d/CCEventDispatcher.cpp | 190 ++++++++---------- cocos/2d/CCEventDispatcher.h | 29 +-- cocos/2d/CCEventKeyboard.h | 2 +- cocos/2d/CCEventListener.cpp | 2 +- cocos/2d/CCEventListener.h | 17 +- cocos/2d/CCEventListenerAcceleration.cpp | 2 +- cocos/2d/CCEventListenerCustom.cpp | 8 +- cocos/2d/CCEventListenerCustom.h | 4 +- cocos/2d/CCEventListenerKeyboard.cpp | 2 +- cocos/2d/CCEventListenerTouch.cpp | 4 +- cocos/2d/CCEventListenerTouch.h | 4 + cocos/2d/CCEventTouch.cpp | 4 - cocos/2d/CCEventTouch.h | 8 +- cocos/2d/platform/CCEGLViewProtocol.cpp | 6 +- .../NewEventDispatcherTest.cpp | 7 +- 21 files changed, 155 insertions(+), 167 deletions(-) diff --git a/cocos/2d/CCEvent.cpp b/cocos/2d/CCEvent.cpp index 87cce7ab7d..c74beef87e 100644 --- a/cocos/2d/CCEvent.cpp +++ b/cocos/2d/CCEvent.cpp @@ -26,7 +26,7 @@ NS_CC_BEGIN -Event::Event(const std::string& type) +Event::Event(Type type) : _type(type) , _isStopped(false) , _currentTarget(nullptr) diff --git a/cocos/2d/CCEvent.h b/cocos/2d/CCEvent.h index 1652066fd3..348c2ba1d3 100644 --- a/cocos/2d/CCEvent.h +++ b/cocos/2d/CCEvent.h @@ -40,15 +40,25 @@ class Node; */ class Event { +public: + enum EventType + { + TYPE_TOUCH = 1, + TYPE_KEYBOARD = 3, + TYPE_ACCELERATION, + TYPE_CUSTOM + }; + + typedef int Type; protected: /** Constructor */ - Event(const std::string& type); + Event(Type type); public: /** Destructor */ virtual ~Event(); /** Gets the event type */ - inline const std::string& getType() const { return _type; }; + inline int getType() const { return _type; }; /** Stops propagation for current event */ inline void stopPropagation() { _isStopped = true; }; @@ -67,7 +77,7 @@ protected: /** Sets current target */ inline void setCurrentTarget(Node* target) { _currentTarget = target; }; - std::string _type; ///< Event type + Type _type; ///< Event type bool _isStopped; ///< whether the event has been stopped. Node* _currentTarget; ///< Current target diff --git a/cocos/2d/CCEventAcceleration.cpp b/cocos/2d/CCEventAcceleration.cpp index 962408202d..7039c03ff8 100644 --- a/cocos/2d/CCEventAcceleration.cpp +++ b/cocos/2d/CCEventAcceleration.cpp @@ -26,10 +26,8 @@ NS_CC_BEGIN -const char* EventAcceleration::EVENT_TYPE = "AccelerometerEvent"; - EventAcceleration::EventAcceleration(Acceleration acc) -: Event(EVENT_TYPE) +: Event(TYPE_ACCELERATION) , _acc(acc) { } diff --git a/cocos/2d/CCEventAcceleration.h b/cocos/2d/CCEventAcceleration.h index c5dfbe9ef5..1480ee6b0c 100644 --- a/cocos/2d/CCEventAcceleration.h +++ b/cocos/2d/CCEventAcceleration.h @@ -33,8 +33,7 @@ NS_CC_BEGIN class EventAcceleration : public Event { public: - static const char* EVENT_TYPE; - + EventAcceleration(Acceleration acc); private: diff --git a/cocos/2d/CCEventCustom.cpp b/cocos/2d/CCEventCustom.cpp index f9fc9d4a0e..89e56a3a69 100644 --- a/cocos/2d/CCEventCustom.cpp +++ b/cocos/2d/CCEventCustom.cpp @@ -23,13 +23,15 @@ ****************************************************************************/ #include "CCEventCustom.h" +#include "ccMacros.h" NS_CC_BEGIN -EventCustom::EventCustom(const std::string& eventName) -: Event(eventName) +EventCustom::EventCustom(Type type) +: Event(type) , _userData(nullptr) { + CCASSERT(type >= TYPE_CUSTOM, "custom type should be greater than TYPE_CUSTOM."); } NS_CC_END diff --git a/cocos/2d/CCEventCustom.h b/cocos/2d/CCEventCustom.h index c4521e3bbf..96676cdbfd 100644 --- a/cocos/2d/CCEventCustom.h +++ b/cocos/2d/CCEventCustom.h @@ -33,7 +33,7 @@ class EventCustom : public Event { public: /** Constructor */ - EventCustom(const std::string& eventName); + EventCustom(Type type); /** Set user data */ inline void setUserData(void* data) { _userData = data; }; diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index f7d593fb8f..a0e58363a3 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -306,11 +306,11 @@ void EventDispatcher::addEventListener(EventListener* listener) if (listener->_fixedPriority == 0) { - setDirtyForEventType(listener->_type, DirtyFlag::SCENE_GRAPH_PRIORITY); + setDirty(listener->_type, DirtyFlag::SCENE_GRAPH_PRIORITY); } else { - setDirtyForEventType(listener->_type, DirtyFlag::FIXED_PRITORY); + setDirty(listener->_type, DirtyFlag::FIXED_PRITORY); } } else @@ -448,7 +448,7 @@ void EventDispatcher::setPriority(EventListener* listener, int fixedPriority) if (listener->_fixedPriority != fixedPriority) { listener->_fixedPriority = fixedPriority; - setDirtyForEventType(listener->_type, DirtyFlag::FIXED_PRITORY); + setDirty(listener->_type, DirtyFlag::FIXED_PRITORY); } return; } @@ -519,31 +519,17 @@ void EventDispatcher::dispatchEvent(Event* event) updateDirtyFlagForSceneGraph(); - DirtyFlag dirtyFlag = DirtyFlag::NONE; - - auto dirtyIter = _priorityDirtyFlagMap.find(event->_type); - if (dirtyIter != _priorityDirtyFlagMap.end()) - { - dirtyFlag = dirtyIter->second; - } - - if (dirtyFlag != DirtyFlag::NONE) - { - if ((int)dirtyFlag & (int)DirtyFlag::FIXED_PRITORY) - { - sortEventListenersOfFixedPriority(event->_type); - } - - if ((int)dirtyFlag & (int)DirtyFlag::SCENE_GRAPH_PRIORITY) - { - sortEventListenersOfSceneGraphPriority(event->_type); - } - - dirtyIter->second = DirtyFlag::NONE; - } DispatchGuard guard(_inDispatch); + if (event->getType() == Event::TYPE_TOUCH) + { + dispatchTouchEvent(static_cast(event)); + return; + } + + sortEventListeners(event->getType()); + auto iter = _listeners.find(event->getType()); if (iter != _listeners.end()) { @@ -558,49 +544,16 @@ void EventDispatcher::dispatchEvent(Event* event) dispatchEventToListeners(listeners, onEvent); } - updateListeners(); + updateListeners(event->getType()); } void EventDispatcher::dispatchTouchEvent(EventTouch* event) { - if (!_isEnabled) - return; + sortEventListeners(EventListener::TYPE_TOUCH_ONE_BY_ONE); + sortEventListeners(EventListener::TYPE_TOUCH_ALL_AT_ONCE); - updateDirtyFlagForSceneGraph(); - - auto sortTouchListeners = [this](const std::string& type){ - - DirtyFlag dirtyFlag = DirtyFlag::NONE; - auto dirtyIter = _priorityDirtyFlagMap.find(type); - if (dirtyIter != _priorityDirtyFlagMap.end()) - { - dirtyFlag = dirtyIter->second; - } - - if (dirtyFlag != DirtyFlag::NONE) - { - if ((int)dirtyFlag & (int)DirtyFlag::FIXED_PRITORY) - { - sortEventListenersOfFixedPriority(type); - } - - if ((int)dirtyFlag & (int)DirtyFlag::SCENE_GRAPH_PRIORITY) - { - sortEventListenersOfSceneGraphPriority(type); - } - - dirtyIter->second = DirtyFlag::NONE; - } - - }; - - sortTouchListeners(EventTouch::MODE_ONE_BY_ONE); - sortTouchListeners(EventTouch::MODE_ALL_AT_ONCE); - - DispatchGuard guard(_inDispatch); - - auto oneByOnelisteners = getListeners(EventTouch::MODE_ONE_BY_ONE); - auto allAtOncelisteners = getListeners(EventTouch::MODE_ALL_AT_ONCE); + auto oneByOnelisteners = getListeners(EventListener::TYPE_TOUCH_ONE_BY_ONE); + auto allAtOncelisteners = getListeners(EventListener::TYPE_TOUCH_ALL_AT_ONCE); // If there aren't any touch listeners, return directly. if (nullptr == oneByOnelisteners && nullptr == allAtOncelisteners) @@ -691,7 +644,7 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) // If the event was stopped, return directly. if (event->isStopped()) { - updateListeners(); + updateListeners(event->getType()); return true; } @@ -770,7 +723,7 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) // If the event was stopped, return directly. if (event->isStopped()) { - updateListeners(); + updateListeners(event->getType()); return false; } @@ -784,14 +737,17 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) } } - updateListeners(); + updateListeners(event->getType()); } -void EventDispatcher::updateListeners() +void EventDispatcher::updateListeners(Event::Type eventType) { - auto listenersIter = _listeners.begin(); - while (listenersIter != _listeners.end()) + auto onUpdateListeners = [this](EventListener::Type listenerType) { + auto listenersIter = _listeners.find(listenerType); + if (listenersIter == _listeners.end()) + return; + auto listeners = listenersIter->second; auto fixedPriorityListeners = listeners->getFixedPriorityListeners(); auto sceneGraphPriorityListeners = listeners->getSceneGraphPriorityListeners(); @@ -850,7 +806,18 @@ void EventDispatcher::updateListeners() { ++listenersIter; } + }; + + if (eventType == Event::TYPE_TOUCH) + { + onUpdateListeners(EventListener::TYPE_TOUCH_ONE_BY_ONE); + onUpdateListeners(EventListener::TYPE_TOUCH_ALL_AT_ONCE); } + else + { + onUpdateListeners(eventType); + } + if (!_toAddedListeners.empty()) { @@ -874,11 +841,11 @@ void EventDispatcher::updateListeners() if (listener->_fixedPriority == 0) { - setDirtyForEventType(listener->_type, DirtyFlag::SCENE_GRAPH_PRIORITY); + setDirty(listener->_type, DirtyFlag::SCENE_GRAPH_PRIORITY); } else { - setDirtyForEventType(listener->_type, DirtyFlag::FIXED_PRITORY); + setDirty(listener->_type, DirtyFlag::FIXED_PRITORY); } } _toAddedListeners.clear(); @@ -896,7 +863,7 @@ void EventDispatcher::updateDirtyFlagForSceneGraph() { for (auto& l : *iter->second) { - setDirtyForEventType(l->_type, DirtyFlag::SCENE_GRAPH_PRIORITY); + setDirty(l->_type, DirtyFlag::SCENE_GRAPH_PRIORITY); } } } @@ -905,12 +872,35 @@ void EventDispatcher::updateDirtyFlagForSceneGraph() } } -void EventDispatcher::sortEventListenersOfSceneGraphPriority(const std::string& eventType) +void EventDispatcher::sortEventListeners(EventListener::Type eventListenerType) { - if (eventType.empty()) - return; + DirtyFlag dirtyFlag = DirtyFlag::NONE; - auto listeners = getListeners(eventType); + auto dirtyIter = _priorityDirtyFlagMap.find(eventListenerType); + if (dirtyIter != _priorityDirtyFlagMap.end()) + { + dirtyFlag = dirtyIter->second; + } + + if (dirtyFlag != DirtyFlag::NONE) + { + if ((int)dirtyFlag & (int)DirtyFlag::FIXED_PRITORY) + { + sortEventListenersOfFixedPriority(eventListenerType); + } + + if ((int)dirtyFlag & (int)DirtyFlag::SCENE_GRAPH_PRIORITY) + { + sortEventListenersOfSceneGraphPriority(eventListenerType); + } + + dirtyIter->second = DirtyFlag::NONE; + } +} + +void EventDispatcher::sortEventListenersOfSceneGraphPriority(EventListener::Type eventListenerType) +{ + auto listeners = getListeners(eventListenerType); if (listeners == nullptr) return; @@ -937,12 +927,9 @@ void EventDispatcher::sortEventListenersOfSceneGraphPriority(const std::string& #endif } -void EventDispatcher::sortEventListenersOfFixedPriority(const std::string &eventType) +void EventDispatcher::sortEventListenersOfFixedPriority(EventListener::Type eventListenerType) { - if (eventType.empty()) - return; - - auto listeners = getListeners(eventType); + auto listeners = getListeners(eventListenerType); if (listeners == nullptr) return; @@ -974,9 +961,9 @@ void EventDispatcher::sortEventListenersOfFixedPriority(const std::string &event } -EventDispatcher::EventListenerVector* EventDispatcher::getListeners(const std::string &eventType) +EventDispatcher::EventListenerVector* EventDispatcher::getListeners(EventListener::Type eventListenerType) { - auto iter = _listeners.find(eventType); + auto iter = _listeners.find(eventListenerType); if (iter != _listeners.end()) { return iter->second; @@ -985,12 +972,9 @@ EventDispatcher::EventListenerVector* EventDispatcher::getListeners(const std::s return nullptr; } -void EventDispatcher::removeListenersForEventType(const std::string& eventType) +void EventDispatcher::removeListeners(EventListener::Type eventListenerType) { - if (eventType.empty()) - return; - - auto listenerItemIter = _listeners.find(eventType); + auto listenerItemIter = _listeners.find(eventListenerType); if (listenerItemIter != _listeners.end()) { auto listeners = listenerItemIter->second; @@ -1010,10 +994,10 @@ void EventDispatcher::removeListenersForEventType(const std::string& eventType) dissociateNodeAndEventListener(l->_node, l); } - l->release(); if (_inDispatch == 0) { iter = listenerVector->erase(iter); + CC_SAFE_RELEASE(l); } else { @@ -1030,14 +1014,14 @@ void EventDispatcher::removeListenersForEventType(const std::string& eventType) listeners->clear(); delete listeners; _listeners.erase(listenerItemIter); - _priorityDirtyFlagMap.erase(eventType); + _priorityDirtyFlagMap.erase(eventListenerType); } } } void EventDispatcher::removeAllListeners() { - std::vector types(_listeners.size()); + std::vector types(_listeners.size()); for (auto iter = _listeners.begin(); iter != _listeners.end(); ++iter) { @@ -1046,7 +1030,7 @@ void EventDispatcher::removeAllListeners() for (auto& type : types) { - removeListenersForEventType(type); + removeListeners(type); } if (!_inDispatch) @@ -1075,14 +1059,12 @@ void EventDispatcher::setDirtyForNode(Node* node) } } -void EventDispatcher::setDirtyForEventType(const std::string& eventType, DirtyFlag flag) -{ - CCASSERT(!eventType.empty(), "Invalid event type."); - - auto iter = _priorityDirtyFlagMap.find(eventType); +void EventDispatcher::setDirty(EventListener::Type eventListenerType, DirtyFlag flag) +{ + auto iter = _priorityDirtyFlagMap.find(eventListenerType); if (iter == _priorityDirtyFlagMap.end()) { - _priorityDirtyFlagMap.insert(std::make_pair(eventType, flag)); + _priorityDirtyFlagMap.insert(std::make_pair(eventListenerType, flag)); } else { @@ -1091,16 +1073,4 @@ void EventDispatcher::setDirtyForEventType(const std::string& eventType, DirtyFl } } -EventDispatcher::DirtyFlag EventDispatcher::isDirtyForEventType(const std::string& eventType) -{ - DirtyFlag flag = DirtyFlag::NONE; - auto iter = _priorityDirtyFlagMap.find(eventType); - if (iter != _priorityDirtyFlagMap.end()) - { - flag = iter->second; - } - - return flag; -} - NS_CC_END diff --git a/cocos/2d/CCEventDispatcher.h b/cocos/2d/CCEventDispatcher.h index 26d3154a65..53f2224b2b 100644 --- a/cocos/2d/CCEventDispatcher.h +++ b/cocos/2d/CCEventDispatcher.h @@ -27,6 +27,7 @@ #include "CCPlatformMacros.h" #include "CCEventListener.h" +#include "CCEvent.h" #include #include @@ -80,7 +81,7 @@ public: void removeEventListener(EventListener* listener); /** Removes all listeners with the same event type */ - void removeListenersForEventType(const std::string& eventType); + void removeListeners(EventListener::Type eventListenerType); /** Removes all listeners */ void removeAllListeners(); @@ -100,9 +101,6 @@ public: */ void dispatchEvent(Event* event); - /** Touch event needs to be processed different with other events since it needs support ALL_AT_ONCE and ONE_BY_NONE mode. */ - void dispatchTouchEvent(EventTouch* event); - /// Priority dirty flag enum class DirtyFlag { @@ -153,30 +151,35 @@ private: /** Adds event listener with item */ void addEventListener(EventListener* listener); - /** Gets event the listener list for the event type. */ - EventListenerVector* getListeners(const std::string& eventType); + /** Gets event the listener list for the event listener type. */ + EventListenerVector* getListeners(EventListener::Type eventListenerType); void updateDirtyFlagForSceneGraph(); + /** Sort event listener */ + void sortEventListeners(EventListener::Type eventListenerType); + /** Sorts the listeners of specified type by scene graph priority */ - void sortEventListenersOfSceneGraphPriority(const std::string& eventType); + void sortEventListenersOfSceneGraphPriority(EventListener::Type eventListenerType); /** Sorts the listeners of specified type by fixed priority */ - void sortEventListenersOfFixedPriority(const std::string& eventType); + void sortEventListenersOfFixedPriority(EventListener::Type eventListenerType); /** Updates all listeners * 1) Removes all listener items that have been marked as 'removed' when dispatching event. * 2) Adds all listener items that have been marked as 'added' when dispatching event. */ - void updateListeners(); + void updateListeners(Event::Type eventType); + /** Touch event needs to be processed different with other events since it needs support ALL_AT_ONCE and ONE_BY_NONE mode. */ + void dispatchTouchEvent(EventTouch* event); + void associateNodeAndEventListener(Node* node, EventListener* listener); void dissociateNodeAndEventListener(Node* node, EventListener* listener); void dispatchEventToListeners(EventListenerVector* listeners, std::function onEvent); - void setDirtyForEventType(const std::string& eventType, DirtyFlag flag); - DirtyFlag isDirtyForEventType(const std::string& eventType); + void setDirty(EventListener::Type listenerType, DirtyFlag flag); void visitTarget(Node* node); @@ -184,9 +187,9 @@ private: /** * Listeners map. */ - std::unordered_map _listeners; + std::unordered_map _listeners; - std::unordered_map _priorityDirtyFlagMap; + std::unordered_map _priorityDirtyFlagMap; std::unordered_map*> _nodeListenersMap; std::unordered_map _nodePriorityMap; diff --git a/cocos/2d/CCEventKeyboard.h b/cocos/2d/CCEventKeyboard.h index fffc76e0ac..b8cf7f8976 100644 --- a/cocos/2d/CCEventKeyboard.h +++ b/cocos/2d/CCEventKeyboard.h @@ -199,7 +199,7 @@ public: static const char* EVENT_TYPE; EventKeyboard(KeyCode keyCode, bool isPressed) - : Event(EVENT_TYPE) + : Event(TYPE_KEYBOARD) , _keyCode(keyCode) , _isPressed(isPressed) {}; diff --git a/cocos/2d/CCEventListener.cpp b/cocos/2d/CCEventListener.cpp index 8b34ca5173..9040593e0e 100644 --- a/cocos/2d/CCEventListener.cpp +++ b/cocos/2d/CCEventListener.cpp @@ -35,7 +35,7 @@ EventListener::~EventListener() CCLOGINFO("In the destructor of EventListener. %p", this); } -bool EventListener::init(const std::string& t, std::function callback) +bool EventListener::init(Type t, std::function callback) { _onEvent = callback; _type = t; diff --git a/cocos/2d/CCEventListener.h b/cocos/2d/CCEventListener.h index 672d9c98d0..bcedcec0c8 100644 --- a/cocos/2d/CCEventListener.h +++ b/cocos/2d/CCEventListener.h @@ -44,13 +44,24 @@ class Node; * For instance, you could refer to EventListenerAcceleration, EventListenerKeyboard, EventListenerTouchOneByOne, EventListenerCustom. */ class EventListener : public Object -{ +{ +public: + enum EventListenerType + { + TYPE_TOUCH_ONE_BY_ONE = 1, + TYPE_TOUCH_ALL_AT_ONCE, + TYPE_KEYBOARD, + TYPE_ACCELERATION, + TYPE_CUSTOM + }; + + typedef int Type; protected: /** Constructor */ EventListener(); /** Initializes event with type and callback function */ - bool init(const std::string& t, std::functioncallback); + bool init(Type t, std::functioncallback); public: /** Destructor */ virtual ~EventListener(); @@ -66,7 +77,7 @@ public: protected: std::function _onEvent; /// Event callback function - std::string _type; /// Event type + Type _type; /// Event type bool _isRegistered; /// Whether the listener has been added to dispatcher. // The priority of event listener diff --git a/cocos/2d/CCEventListenerAcceleration.cpp b/cocos/2d/CCEventListenerAcceleration.cpp index d8b01fc4ce..01a9ef6398 100644 --- a/cocos/2d/CCEventListenerAcceleration.cpp +++ b/cocos/2d/CCEventListenerAcceleration.cpp @@ -59,7 +59,7 @@ bool EventListenerAcceleration::init(std::functiononAccelerationEvent(&accEvent->_acc, event); }; - if (EventListener::init(EventAcceleration::EVENT_TYPE, listener)) + if (EventListener::init(TYPE_ACCELERATION, listener)) { onAccelerationEvent = callback; return true; diff --git a/cocos/2d/CCEventListenerCustom.cpp b/cocos/2d/CCEventListenerCustom.cpp index abe4153198..05e562b567 100644 --- a/cocos/2d/CCEventListenerCustom.cpp +++ b/cocos/2d/CCEventListenerCustom.cpp @@ -32,10 +32,10 @@ EventListenerCustom::EventListenerCustom() { } -EventListenerCustom* EventListenerCustom::create(const std::string& eventName, std::function callback) +EventListenerCustom* EventListenerCustom::create(int type, std::function callback) { EventListenerCustom* ret = new EventListenerCustom(); - if (ret && ret->init(eventName, callback)) + if (ret && ret->init(type, callback)) { ret->autorelease(); } @@ -46,7 +46,7 @@ EventListenerCustom* EventListenerCustom::create(const std::string& eventName, s return ret; } -bool EventListenerCustom::init(const std::string& eventName, std::functioncallback) +bool EventListenerCustom::init(int type, std::functioncallback) { bool ret = false; @@ -59,7 +59,7 @@ bool EventListenerCustom::init(const std::string& eventName, std::function callback); + static EventListenerCustom* create(int type, std::function callback); /// Overrides virtual bool checkAvailable() override; @@ -67,7 +67,7 @@ protected: EventListenerCustom(); /** Initializes event with type and callback function */ - bool init(const std::string& eventName, std::function callback); + bool init(int type, std::function callback); std::function _onCustomEvent; }; diff --git a/cocos/2d/CCEventListenerKeyboard.cpp b/cocos/2d/CCEventListenerKeyboard.cpp index a6e3b617ae..fb733ed442 100644 --- a/cocos/2d/CCEventListenerKeyboard.cpp +++ b/cocos/2d/CCEventListenerKeyboard.cpp @@ -88,7 +88,7 @@ bool EventListenerKeyboard::init() } }; - if (EventListener::init(EventKeyboard::EVENT_TYPE, listener)) + if (EventListener::init(TYPE_KEYBOARD, listener)) { return true; } diff --git a/cocos/2d/CCEventListenerTouch.cpp b/cocos/2d/CCEventListenerTouch.cpp index 49ec1462c9..e753a113d0 100644 --- a/cocos/2d/CCEventListenerTouch.cpp +++ b/cocos/2d/CCEventListenerTouch.cpp @@ -46,7 +46,7 @@ EventListenerTouchOneByOne::~EventListenerTouchOneByOne() bool EventListenerTouchOneByOne::init() { - if (EventListener::init(EventTouch::MODE_ONE_BY_ONE, nullptr)) + if (EventListener::init(TYPE_TOUCH_ONE_BY_ONE, nullptr)) { return true; } @@ -123,7 +123,7 @@ EventListenerTouchAllAtOnce::~EventListenerTouchAllAtOnce() bool EventListenerTouchAllAtOnce::init() { - if (EventListener::init(EventTouch::MODE_ALL_AT_ONCE, nullptr)) + if (EventListener::init(TYPE_TOUCH_ALL_AT_ONCE, nullptr)) { return true; } diff --git a/cocos/2d/CCEventListenerTouch.h b/cocos/2d/CCEventListenerTouch.h index 09e6e6fe0c..a73dc1e992 100644 --- a/cocos/2d/CCEventListenerTouch.h +++ b/cocos/2d/CCEventListenerTouch.h @@ -36,6 +36,8 @@ NS_CC_BEGIN class EventListenerTouchOneByOne : public EventListener { public: + static const int TYPE = 0x01; + static EventListenerTouchOneByOne* create(); virtual ~EventListenerTouchOneByOne(); @@ -67,6 +69,8 @@ private: class EventListenerTouchAllAtOnce : public EventListener { public: + static const int TYPE = 0x02; + static EventListenerTouchAllAtOnce* create(); virtual ~EventListenerTouchAllAtOnce(); diff --git a/cocos/2d/CCEventTouch.cpp b/cocos/2d/CCEventTouch.cpp index 987d87d0e9..d0df614973 100644 --- a/cocos/2d/CCEventTouch.cpp +++ b/cocos/2d/CCEventTouch.cpp @@ -26,9 +26,5 @@ NS_CC_BEGIN -const char* EventTouch::MODE_ONE_BY_ONE = "TouchEventOneByOne"; -const char* EventTouch::MODE_ALL_AT_ONCE = "TouchEventAllAtOnce"; - -const char* EventTouch::EVENT_TYPE = "TouchEvent"; NS_CC_END diff --git a/cocos/2d/CCEventTouch.h b/cocos/2d/CCEventTouch.h index 558bfe0593..100d67cf95 100644 --- a/cocos/2d/CCEventTouch.h +++ b/cocos/2d/CCEventTouch.h @@ -36,12 +36,6 @@ NS_CC_BEGIN class EventTouch : public Event { public: - static const char* MODE_ONE_BY_ONE; - static const char* MODE_ALL_AT_ONCE; - - static const char* EVENT_TYPE; - - static const int MAX_TOUCHES = 5; enum class EventCode @@ -53,7 +47,7 @@ public: }; EventTouch() - : Event(EVENT_TYPE) + : Event(TYPE_TOUCH) { _touches.reserve(MAX_TOUCHES); } diff --git a/cocos/2d/platform/CCEGLViewProtocol.cpp b/cocos/2d/platform/CCEGLViewProtocol.cpp index 094e584e97..9b1e943019 100644 --- a/cocos/2d/platform/CCEGLViewProtocol.cpp +++ b/cocos/2d/platform/CCEGLViewProtocol.cpp @@ -247,7 +247,7 @@ void EGLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float y } touchEvent._eventCode = EventTouch::EventCode::BEGAN; - EventDispatcher::getInstance()->dispatchTouchEvent(&touchEvent); + EventDispatcher::getInstance()->dispatchEvent(&touchEvent); } void EGLViewProtocol::handleTouchesMove(int num, int ids[], float xs[], float ys[]) @@ -294,7 +294,7 @@ void EGLViewProtocol::handleTouchesMove(int num, int ids[], float xs[], float ys } touchEvent._eventCode = EventTouch::EventCode::MOVED; - EventDispatcher::getInstance()->dispatchTouchEvent(&touchEvent); + EventDispatcher::getInstance()->dispatchEvent(&touchEvent); } void EGLViewProtocol::handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode, int num, int ids[], float xs[], float ys[]) @@ -347,7 +347,7 @@ void EGLViewProtocol::handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode } touchEvent._eventCode = eventCode; - EventDispatcher::getInstance()->dispatchTouchEvent(&touchEvent); + EventDispatcher::getInstance()->dispatchEvent(&touchEvent); for (auto& touch : touchEvent._touches) { diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp index d44129aee5..d4b5f9988e 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp @@ -189,7 +189,7 @@ void TouchableSpriteTest::onEnter() auto senderItem = static_cast(sender); senderItem->setString("Only Next item could be clicked"); - EventDispatcher::getInstance()->removeListenersForEventType(EventTouch::EVENT_TYPE); + EventDispatcher::getInstance()->removeListeners(EventListener::TYPE_TOUCH_ONE_BY_ONE); auto nextItem = MenuItemFont::create("Next", [=](Object* sender){ nextCallback(nullptr); @@ -427,7 +427,8 @@ void CustomEventTest::onEnter() statusLabel->setPosition(origin + Point(size.width/2, size.height-90)); addChild(statusLabel); - _listener = EventListenerCustom::create("game_custom_event", [=](EventCustom* event){ + const int game_custom_event = EventListener::TYPE_CUSTOM + 1; + _listener = EventListenerCustom::create(game_custom_event, [=](EventCustom* event){ std::string str("Custom event received, "); char* buf = static_cast(event->getUserData()); str += buf; @@ -443,7 +444,7 @@ void CustomEventTest::onEnter() ++count; char* buf = new char[10]; sprintf(buf, "%d", count); - EventCustom event("game_custom_event"); + EventCustom event(game_custom_event); event.setUserData(buf); dispatcher->dispatchEvent(&event); }); From 30a3199a9132499143142dfbac9bca6c8908739a Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 25 Oct 2013 15:19:04 +0800 Subject: [PATCH 19/42] [Dispatcher] Removing unused codes. --- cocos/2d/CCEventKeyboard.cpp | 1 - cocos/2d/CCEventListenerAcceleration.cpp | 2 +- cocos/2d/CCEventListenerAcceleration.h | 2 +- cocos/2d/CCEventListenerKeyboard.h | 4 ++-- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cocos/2d/CCEventKeyboard.cpp b/cocos/2d/CCEventKeyboard.cpp index ceb2b49a8f..b02312fce1 100644 --- a/cocos/2d/CCEventKeyboard.cpp +++ b/cocos/2d/CCEventKeyboard.cpp @@ -27,6 +27,5 @@ NS_CC_BEGIN -const char* EventKeyboard::EVENT_TYPE = "KeyboardEvent"; NS_CC_END diff --git a/cocos/2d/CCEventListenerAcceleration.cpp b/cocos/2d/CCEventListenerAcceleration.cpp index 01a9ef6398..d1d5d22206 100644 --- a/cocos/2d/CCEventListenerAcceleration.cpp +++ b/cocos/2d/CCEventListenerAcceleration.cpp @@ -37,7 +37,7 @@ EventListenerAcceleration::~EventListenerAcceleration() CCLOGINFO("In the destructor of AccelerationEventListener. %p", this); } -EventListenerAcceleration* EventListenerAcceleration::create(std::function callback) +EventListenerAcceleration* EventListenerAcceleration::create(std::function callback) { EventListenerAcceleration* ret = new EventListenerAcceleration(); if (ret && ret->init(callback)) diff --git a/cocos/2d/CCEventListenerAcceleration.h b/cocos/2d/CCEventListenerAcceleration.h index 2905df9486..59ce3d8439 100644 --- a/cocos/2d/CCEventListenerAcceleration.h +++ b/cocos/2d/CCEventListenerAcceleration.h @@ -33,7 +33,7 @@ NS_CC_BEGIN class EventListenerAcceleration : public EventListener { public: - static EventListenerAcceleration* create(std::function callback); + static EventListenerAcceleration* create(std::function callback); virtual ~EventListenerAcceleration(); /// Overrides diff --git a/cocos/2d/CCEventListenerKeyboard.h b/cocos/2d/CCEventListenerKeyboard.h index dd223da533..c34d2add0d 100644 --- a/cocos/2d/CCEventListenerKeyboard.h +++ b/cocos/2d/CCEventListenerKeyboard.h @@ -42,8 +42,8 @@ public: virtual EventListenerKeyboard* clone() override; virtual bool checkAvailable() override; - std::function onKeyPressed; - std::function onKeyReleased; + std::function onKeyPressed; + std::function onKeyReleased; private: EventListenerKeyboard(); bool init(); From efa5afb1c3f6a67a287c436fd453344159e6be71 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 25 Oct 2013 15:19:25 +0800 Subject: [PATCH 20/42] [Dispatcher] Removing unused codes in Layer. --- cocos/2d/CCLayer.cpp | 25 ------------------------- cocos/2d/CCLayer.h | 7 ------- 2 files changed, 32 deletions(-) diff --git a/cocos/2d/CCLayer.cpp b/cocos/2d/CCLayer.cpp index ed5caf3a71..127261efd5 100644 --- a/cocos/2d/CCLayer.cpp +++ b/cocos/2d/CCLayer.cpp @@ -87,31 +87,6 @@ Layer *Layer::create() } } -int Layer::executeScriptTouchHandler(EventTouch::EventCode eventType, Touch* touch) -{ - if (kScriptTypeNone != _scriptType) - { - TouchScriptData data(eventType, this, touch); - ScriptEvent event(kTouchEvent, &data); - return ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); - } - - //can not reach it - return 0; -} - -int Layer::executeScriptTouchesHandler(EventTouch::EventCode eventType, const std::vector& touches) -{ - if (kScriptTypeNone != _scriptType) - { - TouchesScriptData data(eventType, this, touches); - ScriptEvent event(kTouchesEvent, &data); - return ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); - } - - return 0; -} - #ifdef CC_USE_PHYSICS void Layer::addChild(Node* child) { diff --git a/cocos/2d/CCLayer.h b/cocos/2d/CCLayer.h index 09effe2fcd..e94224b580 100644 --- a/cocos/2d/CCLayer.h +++ b/cocos/2d/CCLayer.h @@ -118,13 +118,6 @@ public: virtual void addChild(Node* child, int zOrder) override; virtual void addChild(Node* child, int zOrder, int tag) override; #endif // CC_USE_PHYSICS - -private: - Touch::DispatchMode _touchMode; - bool _swallowsTouches; - - int executeScriptTouchHandler(EventTouch::EventCode eventType, Touch* touch); - int executeScriptTouchesHandler(EventTouch::EventCode eventType, const std::vector& touches); }; #ifdef __apple__ From 1339ef7ecbb21c31a55ddcc895dbf62c5e694de3 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 25 Oct 2013 15:20:06 +0800 Subject: [PATCH 21/42] [JSB] These are only for v2.x JSB compatibility after removing controller codes in Layer. --- .../javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id b/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id index 4caa15159c..25626c360d 100644 --- a/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id +++ b/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id @@ -1 +1 @@ -0d38b7e53d97995151ea296bdd1f4ed768ce2f9b \ No newline at end of file +c4104d83bf7e5cc0b0eab7bb3753f9f053358054 \ No newline at end of file From 312e1bd27c14899288dc3e0d090d8f9f15030582 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 25 Oct 2013 15:40:21 +0800 Subject: [PATCH 22/42] [Dispatcher] Updating comments. --- cocos/2d/CCEventDispatcher.h | 54 ++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/cocos/2d/CCEventDispatcher.h b/cocos/2d/CCEventDispatcher.h index 53f2224b2b..25f1d8a509 100644 --- a/cocos/2d/CCEventDispatcher.h +++ b/cocos/2d/CCEventDispatcher.h @@ -80,7 +80,7 @@ public: */ void removeEventListener(EventListener* listener); - /** Removes all listeners with the same event type */ + /** Removes all listeners with the same event listener type */ void removeListeners(EventListener::Type eventListenerType); /** Removes all listeners */ @@ -101,19 +101,16 @@ public: */ void dispatchEvent(Event* event); - /// Priority dirty flag - enum class DirtyFlag - { - NONE = 0, - FIXED_PRITORY = 1 << 0, - SCENE_GRAPH_PRIORITY = 1 << 1, - ALL = FIXED_PRITORY | SCENE_GRAPH_PRIORITY - }; - + /** Sets the dirty flag for a node. */ void setDirtyForNode(Node* node); + /** Notifys event dispatcher that the node has been paused. */ void pauseTarget(Node* node); + + /** Notifys event dispatcher that the node has been resumed. */ void resumeTarget(Node* node); + + /** Notifys event dispatcher that the node has been deleted. */ void cleanTarget(Node* node); public: @@ -122,6 +119,9 @@ public: private: + /** + * The vector to store event listeners with scene graph based priority and fixed priority. + */ class EventListenerVector { public: @@ -154,6 +154,7 @@ private: /** Gets event the listener list for the event listener type. */ EventListenerVector* getListeners(EventListener::Type eventListenerType); + /** Update dirty flag */ void updateDirtyFlagForSceneGraph(); /** Sort event listener */ @@ -174,31 +175,54 @@ private: /** Touch event needs to be processed different with other events since it needs support ALL_AT_ONCE and ONE_BY_NONE mode. */ void dispatchTouchEvent(EventTouch* event); + /** Associates node with event listener */ void associateNodeAndEventListener(Node* node, EventListener* listener); + + /** Dissociates node with event listener */ void dissociateNodeAndEventListener(Node* node, EventListener* listener); + /** Dispatches event to listeners with a specified listener type */ void dispatchEventToListeners(EventListenerVector* listeners, std::function onEvent); + /// Priority dirty flag + enum class DirtyFlag + { + NONE = 0, + FIXED_PRITORY = 1 << 0, + SCENE_GRAPH_PRIORITY = 1 << 1, + ALL = FIXED_PRITORY | SCENE_GRAPH_PRIORITY + }; + + /** Sets the dirty flag for a specified listener type */ void setDirty(EventListener::Type listenerType, DirtyFlag flag); + /** Walks though scene graph to get the draw order for each node, it's called before sorting event listener with scene graph priority */ void visitTarget(Node* node); private: - /** - * Listeners map. - */ + /** Listeners map */ std::unordered_map _listeners; + /** The map of dirty flag */ std::unordered_map _priorityDirtyFlagMap; + /** The map of node and event listeners */ std::unordered_map*> _nodeListenersMap; + + /** The map of node and its event priority */ std::unordered_map _nodePriorityMap; + /** The listeners to be added after dispatching event */ std::vector _toAddedListeners; + + /** The nodes were associated with scene graph based priority listeners */ std::set _dirtyNodes; - int _inDispatch; ///< Whether it's in dispatching event - bool _isEnabled; ///< Whether to enable dispatching event + /** Whether the dispatcher is dispatching event */ + int _inDispatch; + + /** Whether to enable dispatching event */ + bool _isEnabled; }; From 262b54eef2cccb4d4b825f262b5c7ac3640b72b3 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 25 Oct 2013 16:06:52 +0800 Subject: [PATCH 23/42] issue #3069: Updating ScrollView. --- extensions/GUI/CCScrollView/CCScrollView.cpp | 43 ++++++++++++-------- extensions/GUI/CCScrollView/CCScrollView.h | 5 ++- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/extensions/GUI/CCScrollView/CCScrollView.cpp b/extensions/GUI/CCScrollView/CCScrollView.cpp index ecc17ef0e7..35287bb9ce 100644 --- a/extensions/GUI/CCScrollView/CCScrollView.cpp +++ b/extensions/GUI/CCScrollView/CCScrollView.cpp @@ -56,6 +56,7 @@ ScrollView::ScrollView() , _touchLength(0.0f) , _minScale(0.0f) , _maxScale(0.0f) +, _touchListener(nullptr) { } @@ -109,14 +110,7 @@ bool ScrollView::initWithViewSize(Size size, Node *container/* = NULL*/) this->setViewSize(size); - auto dispatcher = EventDispatcher::getInstance(); - auto listener = EventListenerTouchOneByOne::create(); - listener->onTouchBegan = CC_CALLBACK_2(ScrollView::onTouchBegan, this); - listener->onTouchMoved = CC_CALLBACK_2(ScrollView::onTouchMoved, this); - listener->onTouchEnded = CC_CALLBACK_2(ScrollView::onTouchEnded, this); - listener->onTouchCancelled = CC_CALLBACK_2(ScrollView::onTouchCancelled, this); - - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + setTouchEnabled(true); _touches.reserve(EventTouch::MAX_TOUCHES); @@ -183,16 +177,29 @@ void ScrollView::resume(Object* sender) _container->resumeSchedulerAndActions(); } -//void ScrollView::setTouchEnabled(bool e) -//{ -// Layer::setTouchEnabled(e); -// if (!e) -// { -// _dragging = false; -// _touchMoved = false; -// _touches.clear(); -// } -//} +void ScrollView::setTouchEnabled(bool enabled) +{ + auto dispatcher = EventDispatcher::getInstance(); + + dispatcher->removeEventListener(_touchListener); + + if (enabled) + { + _touchListener = EventListenerTouchOneByOne::create(); + _touchListener->onTouchBegan = CC_CALLBACK_2(ScrollView::onTouchBegan, this); + _touchListener->onTouchMoved = CC_CALLBACK_2(ScrollView::onTouchMoved, this); + _touchListener->onTouchEnded = CC_CALLBACK_2(ScrollView::onTouchEnded, this); + _touchListener->onTouchCancelled = CC_CALLBACK_2(ScrollView::onTouchCancelled, this); + + dispatcher->addEventListenerWithSceneGraphPriority(_touchListener, this); + } + else + { + _dragging = false; + _touchMoved = false; + _touches.clear(); + } +} void ScrollView::setContentOffset(Point offset, bool animated/* = false*/) { diff --git a/extensions/GUI/CCScrollView/CCScrollView.h b/extensions/GUI/CCScrollView/CCScrollView.h index 42b9d78425..bd24efc8fb 100644 --- a/extensions/GUI/CCScrollView/CCScrollView.h +++ b/extensions/GUI/CCScrollView/CCScrollView.h @@ -166,7 +166,7 @@ public: */ void resume(Object* sender); - + void setTouchEnabled(bool enabled); bool isDragging() const {return _dragging;} bool isTouchMoved() const { return _touchMoved; } bool isBounceable() const { return _bounceable; } @@ -347,6 +347,9 @@ protected: */ Rect _parentScissorRect; bool _scissorRestored; + + /** Touch listener */ + EventListenerTouchOneByOne* _touchListener; }; // end of GUI group From 0d2d522f12c3eca2b56c1412450b0171f48f04f6 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 25 Oct 2013 16:15:37 +0800 Subject: [PATCH 24/42] issue #3069: int --> EventListener::Type. --- cocos/2d/CCEventListenerCustom.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/2d/CCEventListenerCustom.cpp b/cocos/2d/CCEventListenerCustom.cpp index 05e562b567..3875f61c94 100644 --- a/cocos/2d/CCEventListenerCustom.cpp +++ b/cocos/2d/CCEventListenerCustom.cpp @@ -32,7 +32,7 @@ EventListenerCustom::EventListenerCustom() { } -EventListenerCustom* EventListenerCustom::create(int type, std::function callback) +EventListenerCustom* EventListenerCustom::create(Type type, std::function callback) { EventListenerCustom* ret = new EventListenerCustom(); if (ret && ret->init(type, callback)) From cf20329b71aaa175407175673acbd58351449f14 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 25 Oct 2013 16:33:54 +0800 Subject: [PATCH 25/42] issue #3069: Removing unused codes in UILayer. --- cocos/gui/UILayer.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/cocos/gui/UILayer.cpp b/cocos/gui/UILayer.cpp index 891301bf5b..d3a4e69fc5 100644 --- a/cocos/gui/UILayer.cpp +++ b/cocos/gui/UILayer.cpp @@ -89,7 +89,6 @@ void UILayer::onEnter() void UILayer::onExit() { -// setTouchEnabled(false); CCLayer::onExit(); } From 9bf44a6c83e67413bb9e245f03358be2e137340f Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 25 Oct 2013 16:34:26 +0800 Subject: [PATCH 26/42] issue #3069: Renaming 'removeListeners' to 'removeEventListeners'. --- cocos/2d/CCEventDispatcher.cpp | 8 ++++---- cocos/2d/CCEventDispatcher.h | 4 ++-- .../NewEventDispatcherTest/NewEventDispatcherTest.cpp | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index a0e58363a3..386191f8a6 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -151,7 +151,7 @@ EventDispatcher::EventDispatcher() EventDispatcher::~EventDispatcher() { - removeAllListeners(); + removeAllEventListeners(); } EventDispatcher* EventDispatcher::getInstance() @@ -972,7 +972,7 @@ EventDispatcher::EventListenerVector* EventDispatcher::getListeners(EventListene return nullptr; } -void EventDispatcher::removeListeners(EventListener::Type eventListenerType) +void EventDispatcher::removeEventListeners(EventListener::Type eventListenerType) { auto listenerItemIter = _listeners.find(eventListenerType); if (listenerItemIter != _listeners.end()) @@ -1019,7 +1019,7 @@ void EventDispatcher::removeListeners(EventListener::Type eventListenerType) } } -void EventDispatcher::removeAllListeners() +void EventDispatcher::removeAllEventListeners() { std::vector types(_listeners.size()); @@ -1030,7 +1030,7 @@ void EventDispatcher::removeAllListeners() for (auto& type : types) { - removeListeners(type); + removeEventListeners(type); } if (!_inDispatch) diff --git a/cocos/2d/CCEventDispatcher.h b/cocos/2d/CCEventDispatcher.h index 25f1d8a509..a678431f7d 100644 --- a/cocos/2d/CCEventDispatcher.h +++ b/cocos/2d/CCEventDispatcher.h @@ -81,10 +81,10 @@ public: void removeEventListener(EventListener* listener); /** Removes all listeners with the same event listener type */ - void removeListeners(EventListener::Type eventListenerType); + void removeEventListeners(EventListener::Type eventListenerType); /** Removes all listeners */ - void removeAllListeners(); + void removeAllEventListeners(); /** Sets listener's priority with fixed value. */ void setPriority(EventListener* listener, int fixedPriority); diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp index d4b5f9988e..cecb32a6d8 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp @@ -189,7 +189,7 @@ void TouchableSpriteTest::onEnter() auto senderItem = static_cast(sender); senderItem->setString("Only Next item could be clicked"); - EventDispatcher::getInstance()->removeListeners(EventListener::TYPE_TOUCH_ONE_BY_ONE); + EventDispatcher::getInstance()->removeEventListeners(EventListener::TYPE_TOUCH_ONE_BY_ONE); auto nextItem = MenuItemFont::create("Next", [=](Object* sender){ nextCallback(nullptr); From cb7e0b391bffcc32098be8c0fc520cb86e71170f Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 25 Oct 2013 16:37:40 +0800 Subject: [PATCH 27/42] issue #3069: Removing unused variables in CCEventListenerTouch.h. --- cocos/2d/CCEventListenerTouch.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cocos/2d/CCEventListenerTouch.h b/cocos/2d/CCEventListenerTouch.h index a73dc1e992..09e6e6fe0c 100644 --- a/cocos/2d/CCEventListenerTouch.h +++ b/cocos/2d/CCEventListenerTouch.h @@ -36,8 +36,6 @@ NS_CC_BEGIN class EventListenerTouchOneByOne : public EventListener { public: - static const int TYPE = 0x01; - static EventListenerTouchOneByOne* create(); virtual ~EventListenerTouchOneByOne(); @@ -69,8 +67,6 @@ private: class EventListenerTouchAllAtOnce : public EventListener { public: - static const int TYPE = 0x02; - static EventListenerTouchAllAtOnce* create(); virtual ~EventListenerTouchAllAtOnce(); From 063b012a9c692576cf4f6d87f6e00a57448df39e Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 25 Oct 2013 16:43:04 +0800 Subject: [PATCH 28/42] int --> Event::Type --- cocos/2d/CCEvent.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/2d/CCEvent.h b/cocos/2d/CCEvent.h index 348c2ba1d3..eecd7f396c 100644 --- a/cocos/2d/CCEvent.h +++ b/cocos/2d/CCEvent.h @@ -58,7 +58,7 @@ public: virtual ~Event(); /** Gets the event type */ - inline int getType() const { return _type; }; + inline Type getType() const { return _type; }; /** Stops propagation for current event */ inline void stopPropagation() { _isStopped = true; }; From 6c6757a04e3d4af114fa279a4930f3ce7b27d390 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 25 Oct 2013 17:03:50 +0800 Subject: [PATCH 29/42] issue #3069: Moving s_eventPriorityIndex to member variable of EventDispatcher. --- cocos/2d/CCEventDispatcher.cpp | 9 +++++---- cocos/2d/CCEventDispatcher.h | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index 386191f8a6..e8d6331c0a 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -59,7 +59,7 @@ private: NS_CC_BEGIN static EventDispatcher* g_instance = nullptr; -static int s_eventPriorityIndex = 0; + EventDispatcher::EventListenerVector::EventListenerVector() : _sceneGraphListeners(nullptr) @@ -145,6 +145,7 @@ void EventDispatcher::EventListenerVector::clear() EventDispatcher::EventDispatcher() : _inDispatch(0) , _isEnabled(true) +, _nodePriorityIndex(0) { _toAddedListeners.reserve(50); } @@ -190,7 +191,7 @@ void EventDispatcher::visitTarget(Node* node) break; } - _nodePriorityMap.insert(std::make_pair(node, ++s_eventPriorityIndex)); + _nodePriorityMap.insert(std::make_pair(node, ++_nodePriorityIndex)); for( ; i < childrenCount; i++ ) { @@ -201,7 +202,7 @@ void EventDispatcher::visitTarget(Node* node) } else { - _nodePriorityMap.insert(std::make_pair(node, ++s_eventPriorityIndex)); + _nodePriorityMap.insert(std::make_pair(node, ++_nodePriorityIndex)); } } @@ -907,7 +908,7 @@ void EventDispatcher::sortEventListenersOfSceneGraphPriority(EventListener::Type Node* rootNode = (Node*)Director::getInstance()->getRunningScene(); // Reset priority index - s_eventPriorityIndex = 0; + _nodePriorityIndex = 0; _nodePriorityMap.clear(); visitTarget(rootNode); diff --git a/cocos/2d/CCEventDispatcher.h b/cocos/2d/CCEventDispatcher.h index a678431f7d..60baf25222 100644 --- a/cocos/2d/CCEventDispatcher.h +++ b/cocos/2d/CCEventDispatcher.h @@ -223,6 +223,8 @@ private: /** Whether to enable dispatching event */ bool _isEnabled; + + int _nodePriorityIndex; }; From 48ce5e39fe6d4394c4c175c2690ef3ed119d9e40 Mon Sep 17 00:00:00 2001 From: James Chen Date: Sat, 26 Oct 2013 15:04:01 +0800 Subject: [PATCH 30/42] EventDispatcher is managed by Director now, like Scheduler and ActionManager. --- cocos/2d/CCDirector.cpp | 23 +++++++-- cocos/2d/CCDirector.h | 21 ++++++-- cocos/2d/CCEventDispatcher.cpp | 18 ------- cocos/2d/CCEventDispatcher.h | 14 ++---- cocos/2d/CCMenu.cpp | 4 +- cocos/2d/CCNode.cpp | 48 ++++++++++++++----- cocos/2d/CCNode.h | 28 ++++++++--- cocos/2d/CCTransition.cpp | 8 +--- cocos/2d/platform/CCEGLViewProtocol.cpp | 9 ++-- cocos/2d/platform/ios/CCDevice.mm | 2 +- cocos/2d/platform/mac/CCEGLView.mm | 3 +- .../cocostudio/CCInputDelegate.cpp | 16 ++++--- cocos/gui/UILayer.cpp | 2 +- .../cocos2d_specifics.cpp.REMOVED.git-id | 2 +- .../GUI/CCControlExtension/CCControl.cpp | 2 +- extensions/GUI/CCScrollView/CCScrollView.cpp | 14 +++--- .../AccelerometerTest/AccelerometerTest.cpp | 4 +- .../Classes/ActionsTest/ActionsTest.cpp | 2 +- .../Classes/Box2DTestBed/Box2dView.cpp | 8 ++-- .../Cpp/TestCpp/Classes/BugsTest/Bug-624.cpp | 6 +-- .../Cpp/TestCpp/Classes/BugsTest/Bug-914.cpp | 3 +- .../Cpp/TestCpp/Classes/BugsTest/BugsTest.cpp | 2 +- .../Classes/ChipmunkTest/ChipmunkTest.cpp | 4 +- .../ClickAndMoveTest/ClickAndMoveTest.cpp | 3 +- .../ClippingNodeTest/ClippingNodeTest.cpp | 6 +-- .../CocosDenshionTest/CocosDenshionTest.cpp | 2 +- .../Cpp/TestCpp/Classes/CurlTest/CurlTest.cpp | 2 +- .../CocoStudioArmatureTest/ArmatureScene.cpp | 6 +-- .../Classes/ExtensionsTest/ExtensionsTest.cpp | 2 +- .../Classes/KeyboardTest/KeyboardTest.cpp | 2 +- .../TestCpp/Classes/KeypadTest/KeypadTest.cpp | 2 +- .../TestCpp/Classes/LabelTest/LabelTest.cpp | 2 +- .../Classes/LabelTest/LabelTestNew.cpp | 2 +- .../TestCpp/Classes/LayerTest/LayerTest.cpp | 4 +- .../Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp | 12 ++--- .../MotionStreakTest/MotionStreakTest.cpp | 2 +- .../Classes/MutiTouchTest/MutiTouchTest.cpp | 2 +- .../NewEventDispatcherTest.cpp | 47 +++++++----------- .../Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp | 2 +- .../Classes/ParallaxTest/ParallaxTest.cpp | 2 +- .../Classes/ParticleTest/ParticleTest.cpp | 2 +- .../PerformanceTouchesTest.cpp | 12 ++--- .../Classes/PhysicsTest/PhysicsTest.cpp | 8 ++-- .../RenderTextureTest/RenderTextureTest.cpp | 6 +-- .../SpriteTest/SpriteTest.cpp.REMOVED.git-id | 2 +- .../Classes/TextInputTest/TextInputTest.cpp | 2 +- .../Classes/TileMapTest/TileMapTest.cpp | 2 +- .../TestCpp/Classes/TouchesTest/Paddle.cpp | 2 +- samples/Cpp/TestCpp/Classes/controller.cpp | 2 +- 49 files changed, 199 insertions(+), 182 deletions(-) diff --git a/cocos/2d/CCDirector.cpp b/cocos/2d/CCDirector.cpp index 9dc388ea11..ef8b38f423 100644 --- a/cocos/2d/CCDirector.cpp +++ b/cocos/2d/CCDirector.cpp @@ -59,7 +59,7 @@ THE SOFTWARE. #include "platform/CCImage.h" #include "CCEGLView.h" #include "CCConfiguration.h" - +#include "CCEventDispatcher.h" /** Position of the FPS @@ -143,6 +143,8 @@ bool Director::init(void) _actionManager = new ActionManager(); _scheduler->scheduleUpdateForTarget(_actionManager, Scheduler::PRIORITY_SYSTEM, false); + _eventDispatcher = new EventDispatcher(); + // create autorelease pool PoolManager::sharedPoolManager()->push(); @@ -162,7 +164,8 @@ Director::~Director(void) CC_SAFE_RELEASE(_scenesStack); CC_SAFE_RELEASE(_scheduler); CC_SAFE_RELEASE(_actionManager); - + CC_SAFE_RELEASE(_eventDispatcher); + // pop the autorelease pool PoolManager::sharedPoolManager()->pop(); PoolManager::purgePoolManager(); @@ -695,7 +698,6 @@ void Director::purgeDirector() // cocos2d-x specific data structures UserDefault::destroyInstance(); NotificationCenter::destroyInstance(); - EventDispatcher::destroyInstance(); GL::invalidateStateCache(); @@ -966,6 +968,21 @@ ActionManager* Director::getActionManager() const return _actionManager; } +EventDispatcher* Director::getEventDispatcher() const +{ + return _eventDispatcher; +} + +void Director::setEventDispatcher(EventDispatcher* dispatcher) +{ + if (_eventDispatcher != dispatcher) + { + CC_SAFE_RETAIN(dispatcher); + CC_SAFE_RELEASE(_eventDispatcher); + _eventDispatcher = dispatcher; + } +} + /*************************************************** * implementation of DisplayLinkDirector **************************************************/ diff --git a/cocos/2d/CCDirector.h b/cocos/2d/CCDirector.h index 4da35a96a8..5f0d6eb0a8 100644 --- a/cocos/2d/CCDirector.h +++ b/cocos/2d/CCDirector.h @@ -53,10 +53,7 @@ class DirectorDelegate; class Node; class Scheduler; class ActionManager; -class TouchDispatcher; -class KeyboardDispatcher; -class KeypadDispatcher; -class Accelerometer; +class EventDispatcher; /** @brief Class that creates and handles the main Window and manages how @@ -351,6 +348,17 @@ public: @since v2.0 */ void setActionManager(ActionManager* actionManager); + + /** Gets the EventDispatcher associated with this director + @since v3.0 + */ + EventDispatcher* getEventDispatcher() const; + + /** Sets the EventDispatcher associated with this director + @since v3.0 + */ + void setEventDispatcher(EventDispatcher* dispatcher); + /* Gets delta time since last tick to main loop */ float getDeltaTime() const; @@ -383,6 +391,11 @@ protected: @since v2.0 */ ActionManager* _actionManager; + + /** EventDispatcher associated with this director + @since v3.0 + */ + EventDispatcher* _eventDispatcher; /* delta time since last tick to main loop */ float _deltaTime; diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index e8d6331c0a..e0922b0796 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -58,9 +58,6 @@ private: NS_CC_BEGIN -static EventDispatcher* g_instance = nullptr; - - EventDispatcher::EventListenerVector::EventListenerVector() : _sceneGraphListeners(nullptr) , _fixedListeners(nullptr) @@ -155,21 +152,6 @@ EventDispatcher::~EventDispatcher() removeAllEventListeners(); } -EventDispatcher* EventDispatcher::getInstance() -{ - if (g_instance == nullptr) - { - g_instance = new EventDispatcher(); - } - - return g_instance; -} - -void EventDispatcher::destroyInstance() -{ - CC_SAFE_DELETE(g_instance); -} - void EventDispatcher::visitTarget(Node* node) { int i = 0; diff --git a/cocos/2d/CCEventDispatcher.h b/cocos/2d/CCEventDispatcher.h index 60baf25222..e3650a1fae 100644 --- a/cocos/2d/CCEventDispatcher.h +++ b/cocos/2d/CCEventDispatcher.h @@ -50,15 +50,9 @@ event listeners can be added and removed even from within an EventListener, while events are being dispatched. */ -class EventDispatcher +class EventDispatcher : public Object { public: - /** Gets the singleton of EventDispatcher */ - static EventDispatcher* getInstance(); - - /** Destroys the singleton of EventDispatcher */ - static void destroyInstance(); - /** Adds a event listener for a specified event with the priority of scene graph. * @param listener The listener of a specified event. * @param node The priority of the listener is based on the draw order of this node. @@ -113,7 +107,8 @@ public: /** Notifys event dispatcher that the node has been deleted. */ void cleanTarget(Node* node); -public: + /** Constructor of EventDispatcher */ + EventDispatcher(); /** Destructor of EventDispatcher */ ~EventDispatcher(); @@ -144,9 +139,6 @@ private: std::vector* _sceneGraphListeners; int _gt0Index; }; - - /** Constructor of EventDispatcher */ - EventDispatcher(); /** Adds event listener with item */ void addEventListener(EventListener* listener); diff --git a/cocos/2d/CCMenu.cpp b/cocos/2d/CCMenu.cpp index 5a46765d91..3b0cefbec3 100644 --- a/cocos/2d/CCMenu.cpp +++ b/cocos/2d/CCMenu.cpp @@ -185,8 +185,6 @@ void Menu::onEnter() { Layer::onEnter(); - auto eventDispatcher = EventDispatcher::getInstance(); - auto touchListener = EventListenerTouchOneByOne::create(); touchListener->setSwallowTouches(true); @@ -195,7 +193,7 @@ void Menu::onEnter() touchListener->onTouchEnded = CC_CALLBACK_2(Menu::onTouchEnded, this); touchListener->onTouchCancelled = CC_CALLBACK_2(Menu::onTouchCancelled, this); - eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); } void Menu::onExit() diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 427884abb0..5302648923 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -139,7 +139,9 @@ Node::Node(void) _actionManager->retain(); _scheduler = director->getScheduler(); _scheduler->retain(); - + _eventDispatcher = director->getEventDispatcher(); + _eventDispatcher->retain(); + ScriptEngineProtocol* pEngine = ScriptEngineManager::getInstance()->getScriptEngine(); _scriptType = pEngine != NULL ? pEngine->getScriptType() : kScriptTypeNone; } @@ -155,6 +157,10 @@ Node::~Node() CC_SAFE_RELEASE(_actionManager); CC_SAFE_RELEASE(_scheduler); + + _eventDispatcher->cleanTarget(this); + CC_SAFE_RELEASE(_eventDispatcher); + // attributes CC_SAFE_RELEASE(_camera); @@ -182,8 +188,6 @@ Node::~Node() CC_SAFE_DELETE(_componentContainer); - EventDispatcher::getInstance()->cleanTarget(this); - #ifdef CC_USE_PHYSICS CC_SAFE_RELEASE(_physicsBody); #endif @@ -238,7 +242,7 @@ void Node::setZOrder(int z) _parent->reorderChild(this, z); } - EventDispatcher::getInstance()->setDirtyForNode(this); + _eventDispatcher->setDirtyForNode(this); } /// vertexZ getter @@ -931,8 +935,7 @@ void Node::onEnter() arrayMakeObjectsPerformSelector(_children, onEnter, Node*); - this->resumeSchedulerAndActions(); - EventDispatcher::getInstance()->resumeTarget(this); + this->resume(); _running = true; @@ -974,9 +977,7 @@ void Node::onExitTransitionDidStart() void Node::onExit() { - EventDispatcher::getInstance()->pauseTarget(this); - - this->pauseSchedulerAndActions(); + this->pause(); _running = false; if (_scriptType != kScriptTypeNone) @@ -990,6 +991,17 @@ void Node::onExit() arrayMakeObjectsPerformSelector(_children, onExit, Node*); } +void Node::setEventDispatcher(EventDispatcher* dispatcher) +{ + if (dispatcher != _eventDispatcher) + { + _eventDispatcher->cleanTarget(this); + CC_SAFE_RETAIN(dispatcher); + CC_SAFE_RELEASE(_eventDispatcher); + _eventDispatcher = dispatcher; + } +} + void Node::setActionManager(ActionManager* actionManager) { if( actionManager != _actionManager ) { @@ -1115,16 +1127,28 @@ void Node::unscheduleAllSelectors() _scheduler->unscheduleAllForTarget(this); } -void Node::resumeSchedulerAndActions() +void Node::resume() { _scheduler->resumeTarget(this); _actionManager->resumeTarget(this); + _eventDispatcher->resumeTarget(this); +} + +void Node::pause() +{ + _scheduler->pauseTarget(this); + _actionManager->pauseTarget(this); + _eventDispatcher->pauseTarget(this); +} + +void Node::resumeSchedulerAndActions() +{ + resume(); } void Node::pauseSchedulerAndActions() { - _scheduler->pauseTarget(this); - _actionManager->pauseTarget(this); + pause(); } // override me diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index fcc38b23b5..cde05c34c3 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -995,6 +995,9 @@ public: /** @deprecated Use getBoundingBox instead */ CC_DEPRECATED_ATTRIBUTE inline virtual Rect boundingBox() const { return getBoundingBox(); } + virtual void setEventDispatcher(EventDispatcher* dispatcher); + virtual EventDispatcher* getEventDispatcher() const { return _eventDispatcher; }; + /// @{ /// @name Actions @@ -1192,16 +1195,27 @@ public: */ void unscheduleAllSelectors(void); - /** - * Resumes all scheduled selectors and actions. + /** + * Resumes all scheduled selectors, actions and event listeners. * This method is called internally by onEnter */ - void resumeSchedulerAndActions(void); - /** - * Pauses all scheduled selectors and actions. + void resume(void); + /** + * Pauses all scheduled selectors, actions and event listeners.. * This method is called internally by onExit */ - void pauseSchedulerAndActions(void); + void pause(void); + + /** + * Resumes all scheduled selectors, actions and event listeners. + * This method is called internally by onEnter + */ + CC_DEPRECATED_ATTRIBUTE void resumeSchedulerAndActions(void); + /** + * Pauses all scheduled selectors, actions and event listeners.. + * This method is called internally by onExit + */ + CC_DEPRECATED_ATTRIBUTE void pauseSchedulerAndActions(void); /* * Update method will be called automatically every frame if "scheduleUpdate" is called, and the node is "live" @@ -1461,6 +1475,8 @@ protected: ActionManager *_actionManager; ///< a pointer to ActionManager singleton, which is used to handle all the actions + EventDispatcher* _eventDispatcher; ///< event dispatcher used to dispatch all kinds of events + bool _running; ///< is running bool _visible; ///< is this node visible diff --git a/cocos/2d/CCTransition.cpp b/cocos/2d/CCTransition.cpp index 5dea9ae721..4642b7404b 100644 --- a/cocos/2d/CCTransition.cpp +++ b/cocos/2d/CCTransition.cpp @@ -160,9 +160,7 @@ void TransitionScene::onEnter() Scene::onEnter(); // disable events while transitions -// Director::getInstance()->getTouchDispatcher()->setDispatchEvents(false); - - EventDispatcher::getInstance()->setEnabled(false); + _eventDispatcher->setEnabled(false); // outScene should not receive the onEnter callback // only the onExitTransitionDidStart @@ -177,9 +175,7 @@ void TransitionScene::onExit() Scene::onExit(); // enable events while transitions -// Director::getInstance()->getTouchDispatcher()->setDispatchEvents(true); - - EventDispatcher::getInstance()->setEnabled(true); + _eventDispatcher->setEnabled(true); _outScene->onExit(); // _inScene should not receive the onEnter callback diff --git a/cocos/2d/platform/CCEGLViewProtocol.cpp b/cocos/2d/platform/CCEGLViewProtocol.cpp index 9b1e943019..17eafdf0d6 100644 --- a/cocos/2d/platform/CCEGLViewProtocol.cpp +++ b/cocos/2d/platform/CCEGLViewProtocol.cpp @@ -247,7 +247,8 @@ void EGLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float y } touchEvent._eventCode = EventTouch::EventCode::BEGAN; - EventDispatcher::getInstance()->dispatchEvent(&touchEvent); + auto dispatcher = Director::getInstance()->getEventDispatcher(); + dispatcher->dispatchEvent(&touchEvent); } void EGLViewProtocol::handleTouchesMove(int num, int ids[], float xs[], float ys[]) @@ -294,7 +295,8 @@ void EGLViewProtocol::handleTouchesMove(int num, int ids[], float xs[], float ys } touchEvent._eventCode = EventTouch::EventCode::MOVED; - EventDispatcher::getInstance()->dispatchEvent(&touchEvent); + auto dispatcher = Director::getInstance()->getEventDispatcher(); + dispatcher->dispatchEvent(&touchEvent); } void EGLViewProtocol::handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode, int num, int ids[], float xs[], float ys[]) @@ -347,7 +349,8 @@ void EGLViewProtocol::handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode } touchEvent._eventCode = eventCode; - EventDispatcher::getInstance()->dispatchEvent(&touchEvent); + auto dispatcher = Director::getInstance()->getEventDispatcher(); + dispatcher->dispatchEvent(&touchEvent); for (auto& touch : touchEvent._touches) { diff --git a/cocos/2d/platform/ios/CCDevice.mm b/cocos/2d/platform/ios/CCDevice.mm index 83212d575d..fdc9448849 100644 --- a/cocos/2d/platform/ios/CCDevice.mm +++ b/cocos/2d/platform/ios/CCDevice.mm @@ -100,7 +100,7 @@ static CCAccelerometerDispatcher* s_pAccelerometerDispatcher; } cocos2d::EventAcceleration event(*_acceleration); - cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event); + cocos2d::_eventDispatcherdispatchEvent(&event); } @end diff --git a/cocos/2d/platform/mac/CCEGLView.mm b/cocos/2d/platform/mac/CCEGLView.mm index d8dce7ba57..bd3ba3b5c5 100644 --- a/cocos/2d/platform/mac/CCEGLView.mm +++ b/cocos/2d/platform/mac/CCEGLView.mm @@ -239,7 +239,8 @@ void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x, void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action); - EventDispatcher::getInstance()->dispatchEvent(&event); + auto dispatcher = Director::getInstance()->getEventDispatcher(); + dispatcher->dispatchEvent(&event); } void EGLViewEventHandler::OnGLFWCharCallback(GLFWwindow *window, unsigned int character) diff --git a/cocos/editor-support/cocostudio/CCInputDelegate.cpp b/cocos/editor-support/cocostudio/CCInputDelegate.cpp index 968645bc4e..a134e3cdfe 100644 --- a/cocos/editor-support/cocostudio/CCInputDelegate.cpp +++ b/cocos/editor-support/cocostudio/CCInputDelegate.cpp @@ -43,7 +43,7 @@ InputDelegate::InputDelegate(void) InputDelegate::~InputDelegate(void) { - auto dispatcher = EventDispatcher::getInstance(); + auto dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->removeEventListener(_touchListener); dispatcher->removeEventListener(_keyboardListener); dispatcher->removeEventListener(_accelerometerListener); @@ -107,6 +107,7 @@ void InputDelegate::setTouchEnabled(bool enabled) { if (_touchEnabled != enabled) { + auto dispatcher = Director::getInstance()->getEventDispatcher(); _touchEnabled = enabled; if (enabled) { @@ -119,7 +120,7 @@ void InputDelegate::setTouchEnabled(bool enabled) listener->onTouchesEnded = CC_CALLBACK_2(InputDelegate::onTouchesEnded, this); listener->onTouchesCancelled = CC_CALLBACK_2(InputDelegate::onTouchesCancelled, this); - EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, _touchPriority); + dispatcher->addEventListenerWithFixedPriority(listener, _touchPriority); _touchListener = listener; } else { // Register Touch Event @@ -131,13 +132,13 @@ void InputDelegate::setTouchEnabled(bool enabled) listener->onTouchEnded = CC_CALLBACK_2(InputDelegate::onTouchEnded, this); listener->onTouchCancelled = CC_CALLBACK_2(InputDelegate::onTouchCancelled, this); - EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, _touchPriority); + dispatcher->addEventListenerWithFixedPriority(listener, _touchPriority); _touchListener = listener; } } else { - EventDispatcher::getInstance()->removeEventListener(_touchListener); + dispatcher->removeEventListener(_touchListener); } } } @@ -191,7 +192,7 @@ void InputDelegate::setAccelerometerEnabled(bool enabled) { _accelerometerEnabled = enabled; - auto dispatcher = EventDispatcher::getInstance(); + auto dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->removeEventListener(_accelerometerListener); _accelerometerListener = nullptr; @@ -215,7 +216,8 @@ void InputDelegate::setKeypadEnabled(bool enabled) { _keypadEnabled = enabled; - EventDispatcher::getInstance()->removeEventListener(_keyboardListener); + auto dispatcher = Director::getInstance()->getEventDispatcher(); + dispatcher->removeEventListener(_keyboardListener); if (enabled) { @@ -223,7 +225,7 @@ void InputDelegate::setKeypadEnabled(bool enabled) listener->onKeyPressed = CC_CALLBACK_2(InputDelegate::onKeyPressed, this); listener->onKeyReleased = CC_CALLBACK_2(InputDelegate::onKeyReleased, this); - EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, -1); + dispatcher->addEventListenerWithFixedPriority(listener, -1); _keyboardListener = listener; } } diff --git a/cocos/gui/UILayer.cpp b/cocos/gui/UILayer.cpp index d3a4e69fc5..bfdea4094f 100644 --- a/cocos/gui/UILayer.cpp +++ b/cocos/gui/UILayer.cpp @@ -84,7 +84,7 @@ void UILayer::onEnter() listener->onTouchEnded = CC_CALLBACK_2(UILayer::onTouchEnded, this); listener->onTouchCancelled = CC_CALLBACK_2(UILayer::onTouchCancelled, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); } void UILayer::onExit() diff --git a/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id b/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id index 25626c360d..c661a05129 100644 --- a/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id +++ b/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id @@ -1 +1 @@ -c4104d83bf7e5cc0b0eab7bb3753f9f053358054 \ No newline at end of file +1475f1478d176a00cc8d0f9b3302881dee7a8437 \ No newline at end of file diff --git a/extensions/GUI/CCControlExtension/CCControl.cpp b/extensions/GUI/CCControlExtension/CCControl.cpp index 1419edbc36..2b97094acc 100644 --- a/extensions/GUI/CCControlExtension/CCControl.cpp +++ b/extensions/GUI/CCControlExtension/CCControl.cpp @@ -97,7 +97,7 @@ void Control::onEnter() { Layer::onEnter(); - auto dispatcher = EventDispatcher::getInstance(); + auto dispatcher = Director::getInstance()->getEventDispatcher(); auto touchListener = EventListenerTouchOneByOne::create(); touchListener->onTouchBegan = CC_CALLBACK_2(Control::onTouchBegan, this); touchListener->onTouchMoved = CC_CALLBACK_2(Control::onTouchMoved, this); diff --git a/extensions/GUI/CCScrollView/CCScrollView.cpp b/extensions/GUI/CCScrollView/CCScrollView.cpp index 35287bb9ce..8db57d6377 100644 --- a/extensions/GUI/CCScrollView/CCScrollView.cpp +++ b/extensions/GUI/CCScrollView/CCScrollView.cpp @@ -151,7 +151,7 @@ bool ScrollView::isNodeVisible(Node* node) void ScrollView::pause(Object* sender) { - _container->pauseSchedulerAndActions(); + _container->pause(); Object* pObj = NULL; Array* pChildren = _container->getChildren(); @@ -159,7 +159,7 @@ void ScrollView::pause(Object* sender) CCARRAY_FOREACH(pChildren, pObj) { Node* pChild = static_cast(pObj); - pChild->pauseSchedulerAndActions(); + pChild->pause(); } } @@ -171,17 +171,15 @@ void ScrollView::resume(Object* sender) CCARRAY_FOREACH(pChildren, pObj) { Node* pChild = static_cast(pObj); - pChild->resumeSchedulerAndActions(); + pChild->resume(); } - _container->resumeSchedulerAndActions(); + _container->resume(); } void ScrollView::setTouchEnabled(bool enabled) { - auto dispatcher = EventDispatcher::getInstance(); - - dispatcher->removeEventListener(_touchListener); + _eventDispatcher->removeEventListener(_touchListener); if (enabled) { @@ -191,7 +189,7 @@ void ScrollView::setTouchEnabled(bool enabled) _touchListener->onTouchEnded = CC_CALLBACK_2(ScrollView::onTouchEnded, this); _touchListener->onTouchCancelled = CC_CALLBACK_2(ScrollView::onTouchCancelled, this); - dispatcher->addEventListenerWithSceneGraphPriority(_touchListener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(_touchListener, this); } else { diff --git a/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.cpp b/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.cpp index ada7540cc6..db535fe321 100644 --- a/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.cpp +++ b/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.cpp @@ -32,10 +32,8 @@ void AccelerometerTest::onEnter() { Layer::onEnter(); - auto dispatcher = EventDispatcher::getInstance(); - auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(AccelerometerTest::onAcceleration, this)); - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto label = LabelTTF::create(title().c_str(), "Arial", 32); addChild(label, 1); diff --git a/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.cpp b/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.cpp index c41f06c377..703e8bf40b 100644 --- a/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ActionsTest/ActionsTest.cpp @@ -1377,7 +1377,7 @@ void ActionStacked::onEnter() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(ActionStacked::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto s = Director::getInstance()->getWinSize(); this->addNewSpriteWithCoords(Point(s.width/2, s.height/2)); diff --git a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp index 0cfbabd90c..e50611afda 100644 --- a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp +++ b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp @@ -33,7 +33,7 @@ MenuLayer::MenuLayer(void) MenuLayer::~MenuLayer(void) { - EventDispatcher::getInstance()->removeEventListener(_touchListener); + _eventDispatcher->removeEventListener(_touchListener); } MenuLayer* MenuLayer::menuWithEntryID(int entryId) @@ -86,7 +86,7 @@ bool MenuLayer::initWithEntryID(int entryId) listener->onTouchBegan = CC_CALLBACK_2(MenuLayer::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(MenuLayer::onTouchMoved, this); - EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 1); + _eventDispatcher->addEventListenerWithFixedPriority(listener, 1); _touchListener = listener; @@ -190,7 +190,7 @@ bool Box2DView::initWithEntryID(int entryId) listener->onTouchMoved = CC_CALLBACK_2(Box2DView::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(Box2DView::onTouchEnded, this); - EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, -10); + _eventDispatcher->addEventListenerWithFixedPriority(listener, -10); _touchListener = listener; return true; @@ -224,7 +224,7 @@ void Box2DView::draw() Box2DView::~Box2DView() { // Removes Touch Event Listener - EventDispatcher::getInstance()->removeEventListener(_touchListener); + _eventDispatcher->removeEventListener(_touchListener); delete m_test; } // diff --git a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-624.cpp b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-624.cpp index 9eb6d6ab5c..e0c4ba23ed 100644 --- a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-624.cpp +++ b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-624.cpp @@ -20,9 +20,8 @@ bool Bug624Layer::init() label->setPosition(Point(size.width/2, size.height/2)); addChild(label); - auto dispatcher = EventDispatcher::getInstance(); auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(Bug624Layer::onAcceleration, this)); - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); schedule(schedule_selector(Bug624Layer::switchLayer), 5.0f); @@ -61,9 +60,8 @@ bool Bug624Layer2::init() label->setPosition(Point(size.width/2, size.height/2)); addChild(label); - auto dispatcher = EventDispatcher::getInstance(); auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(Bug624Layer2::onAcceleration, this)); - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); schedule(schedule_selector(Bug624Layer2::switchLayer), 5.0f); diff --git a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.cpp b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.cpp index 76ab5b3b31..1064524e1c 100644 --- a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.cpp +++ b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-914.cpp @@ -30,11 +30,10 @@ bool Bug914Layer::init() // Apple recommends to re-assign "self" with the "super" return value if (BugsTestBaseLayer::init()) { - auto dispatcher = EventDispatcher::getInstance(); auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(Bug914Layer::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(Bug914Layer::onTouchesMoved, this); - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); // ask director the the window size auto size = Director::getInstance()->getWinSize(); diff --git a/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.cpp b/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.cpp index 1d81217bbb..096ca45d21 100644 --- a/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/BugsTest/BugsTest.cpp @@ -72,7 +72,7 @@ void BugsTestMainLayer::onEnter() listener->onTouchesBegan = CC_CALLBACK_2(BugsTestMainLayer::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(BugsTestMainLayer::onTouchesMoved, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); } void BugsTestMainLayer::onTouchesBegan(const std::vector& touches, Event *event) diff --git a/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.cpp b/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.cpp index bb3ef0e12f..50d5dc7252 100644 --- a/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ChipmunkTest/ChipmunkTest.cpp @@ -24,10 +24,10 @@ ChipmunkTestLayer::ChipmunkTestLayer() auto touchListener = EventListenerTouchAllAtOnce::create(); touchListener->onTouchesEnded = CC_CALLBACK_2(ChipmunkTestLayer::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(touchListener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); auto accListener = EventListenerAcceleration::create(CC_CALLBACK_2(ChipmunkTestLayer::onAcceleration, this)); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(accListener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(accListener, this); // title auto label = LabelTTF::create("Multi touch the screen", "Marker Felt", 36); diff --git a/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.cpp b/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.cpp index 408188925f..6b45dd9c32 100644 --- a/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ClickAndMoveTest/ClickAndMoveTest.cpp @@ -17,11 +17,10 @@ void ClickAndMoveTestScene::runThisTest() MainLayer::MainLayer() { - auto dispatcher = EventDispatcher::getInstance(); auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(MainLayer::onTouchBegan, this); listener->onTouchEnded = CC_CALLBACK_2(MainLayer::onTouchEnded, this); - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto sprite = Sprite::create(s_pathGrossini); diff --git a/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.cpp b/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.cpp index 56ad34de68..93e26514a2 100644 --- a/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ClippingNodeTest/ClippingNodeTest.cpp @@ -449,10 +449,9 @@ void HoleDemo::setup() this->addChild(_outerClipper); - auto dispatcher = EventDispatcher::getInstance(); auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(HoleDemo::onTouchesBegan, this); - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); } void HoleDemo::pokeHoleAtPoint(Point point) @@ -529,12 +528,11 @@ void ScrollViewDemo::setup() _scrolling = false; - auto dispatcher = EventDispatcher::getInstance(); auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(ScrollViewDemo::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(ScrollViewDemo::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(ScrollViewDemo::onTouchesEnded, this); - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); } void ScrollViewDemo::onTouchesBegan(const std::vector& touches, Event *event) diff --git a/samples/Cpp/TestCpp/Classes/CocosDenshionTest/CocosDenshionTest.cpp b/samples/Cpp/TestCpp/Classes/CocosDenshionTest/CocosDenshionTest.cpp index e83cb001f1..63959537d8 100644 --- a/samples/Cpp/TestCpp/Classes/CocosDenshionTest/CocosDenshionTest.cpp +++ b/samples/Cpp/TestCpp/Classes/CocosDenshionTest/CocosDenshionTest.cpp @@ -72,7 +72,7 @@ private: listener->onTouchEnded = CC_CALLBACK_2(Button::onTouchEnded, this); listener->onTouchCancelled = CC_CALLBACK_2(Button::onTouchCancelled, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); } diff --git a/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.cpp b/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.cpp index f3ad65db63..8d92edf249 100644 --- a/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.cpp +++ b/samples/Cpp/TestCpp/Classes/CurlTest/CurlTest.cpp @@ -11,7 +11,7 @@ CurlTest::CurlTest() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(CurlTest::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); // create a label to display the tip string _label = LabelTTF::create("Touch the screen to connect", "Arial", 22); diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp index fef387ac01..1cd233ffd2 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp @@ -480,7 +480,7 @@ void TestParticleDisplay::onEnter() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(TestParticleDisplay::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); animationID = 0; @@ -540,7 +540,7 @@ void TestUseMutiplePicture::onEnter() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(TestUseMutiplePicture::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); displayIndex = 0; @@ -944,7 +944,7 @@ void TestArmatureNesting::onEnter() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(TestArmatureNesting::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); armature = Armature::create("cyborg"); armature->getAnimation()->playByIndex(1); diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.cpp index 5ba010f2a9..96089e9a2c 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.cpp @@ -122,7 +122,7 @@ void ExtensionsMainLayer::onEnter() listener->onTouchesBegan = CC_CALLBACK_2(ExtensionsMainLayer::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(ExtensionsMainLayer::onTouchesMoved, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); addChild(_itemMenu); } diff --git a/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp b/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp index a8a164da01..24b95f0df6 100644 --- a/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp +++ b/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp @@ -11,7 +11,7 @@ KeyboardTest::KeyboardTest() listener->onKeyPressed = CC_CALLBACK_2(KeyboardTest::onKeyPressed, this); listener->onKeyReleased = CC_CALLBACK_2(KeyboardTest::onKeyReleased, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); // create a label to display the tip string _label = LabelTTF::create("Please press any key and see console log...", "Arial", 22); diff --git a/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp b/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp index c3191d5724..b025680265 100644 --- a/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp +++ b/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp @@ -10,7 +10,7 @@ KeypadTest::KeypadTest() auto listener = EventListenerKeyboard::create(); listener->onKeyReleased = CC_CALLBACK_2(KeypadTest::onKeyReleased, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); // create a label to display the tip string _label = LabelTTF::create("Please press any key...", "Arial", 22); diff --git a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp index f95831074e..ea9405eabe 100644 --- a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp +++ b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp @@ -1140,7 +1140,7 @@ BitmapFontMultiLineAlignment::BitmapFontMultiLineAlignment() listener->onTouchesMoved = CC_CALLBACK_2(BitmapFontMultiLineAlignment::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(BitmapFontMultiLineAlignment::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); // ask director the the window size auto size = Director::getInstance()->getWinSize(); diff --git a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp index d6d5777ada..f22c5a7864 100644 --- a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp +++ b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp @@ -655,7 +655,7 @@ LabelFNTMultiLineAlignment::LabelFNTMultiLineAlignment() listener->onTouchesMoved = CC_CALLBACK_2(LabelFNTMultiLineAlignment::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(LabelFNTMultiLineAlignment::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); // ask director the the window size auto size = Director::getInstance()->getWinSize(); diff --git a/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp b/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp index 9fcc560cdf..581928a4f3 100644 --- a/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp +++ b/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp @@ -454,7 +454,7 @@ void LayerTest1::onEnter() listener->onTouchesMoved = CC_CALLBACK_2(LayerTest1::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(LayerTest1::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto s = Director::getInstance()->getWinSize(); auto layer = LayerColor::create( Color4B(0xFF, 0x00, 0x00, 0x80), 200, 200); @@ -597,7 +597,7 @@ LayerGradientTest::LayerGradientTest() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesMoved = CC_CALLBACK_2(LayerGradientTest::onTouchesMoved, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto label1 = LabelTTF::create("Compressed Interpolation: Enabled", "Marker Felt", 26); auto label2 = LabelTTF::create("Compressed Interpolation: Disabled", "Marker Felt", 26); diff --git a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp index d295de75bc..f85bb260cd 100644 --- a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp +++ b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp @@ -32,7 +32,7 @@ MenuLayerMainMenu::MenuLayerMainMenu() _touchListener->onTouchEnded = CC_CALLBACK_2(MenuLayerMainMenu::onTouchEnded, this); _touchListener->onTouchCancelled = CC_CALLBACK_2(MenuLayerMainMenu::onTouchCancelled, this); - EventDispatcher::getInstance()->addEventListenerWithFixedPriority(_touchListener, 1); + _eventDispatcher->addEventListenerWithFixedPriority(_touchListener, 1); // Font Item auto spriteNormal = Sprite::create(s_MenuItem, Rect(0,23*2,115,23)); @@ -138,7 +138,7 @@ void MenuLayerMainMenu::onTouchMoved(Touch *touch, Event * event) MenuLayerMainMenu::~MenuLayerMainMenu() { - EventDispatcher::getInstance()->removeEventListener(_touchListener); + _eventDispatcher->removeEventListener(_touchListener); _disabledItem->release(); } @@ -154,7 +154,7 @@ void MenuLayerMainMenu::menuCallbackConfig(Object* sender) void MenuLayerMainMenu::allowTouches(float dt) { - EventDispatcher::getInstance()->setPriority(_touchListener, 1); + _eventDispatcher->setPriority(_touchListener, 1); unscheduleAllSelectors(); log("TOUCHES ALLOWED AGAIN"); } @@ -162,7 +162,7 @@ void MenuLayerMainMenu::allowTouches(float dt) void MenuLayerMainMenu::menuCallbackDisabled(Object* sender) { // hijack all touch events for 5 seconds - EventDispatcher::getInstance()->setPriority(_touchListener, -1); + _eventDispatcher->setPriority(_touchListener, -1); schedule(schedule_selector(MenuLayerMainMenu::allowTouches), 5.0f); log("TOUCHES DISABLED FOR 5 SECONDS"); } @@ -583,7 +583,7 @@ RemoveMenuItemWhenMove::RemoveMenuItemWhenMove() _touchListener->onTouchBegan = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchBegan, this); _touchListener->onTouchMoved = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchMoved, this); - EventDispatcher::getInstance()->addEventListenerWithFixedPriority(_touchListener, -129); + _eventDispatcher->addEventListenerWithFixedPriority(_touchListener, -129); } @@ -594,7 +594,7 @@ void RemoveMenuItemWhenMove::goBack(Object *pSender) RemoveMenuItemWhenMove::~RemoveMenuItemWhenMove() { - EventDispatcher::getInstance()->removeEventListener(_touchListener); + _eventDispatcher->removeEventListener(_touchListener); CC_SAFE_RELEASE(item); } diff --git a/samples/Cpp/TestCpp/Classes/MotionStreakTest/MotionStreakTest.cpp b/samples/Cpp/TestCpp/Classes/MotionStreakTest/MotionStreakTest.cpp index 13a376ec29..9d9057c3e7 100644 --- a/samples/Cpp/TestCpp/Classes/MotionStreakTest/MotionStreakTest.cpp +++ b/samples/Cpp/TestCpp/Classes/MotionStreakTest/MotionStreakTest.cpp @@ -123,7 +123,7 @@ void MotionStreakTest2::onEnter() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesMoved = CC_CALLBACK_2(MotionStreakTest2::onTouchesMoved, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto s = Director::getInstance()->getWinSize(); diff --git a/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.cpp b/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.cpp index 42e6634de5..2287ff517a 100644 --- a/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.cpp +++ b/samples/Cpp/TestCpp/Classes/MutiTouchTest/MutiTouchTest.cpp @@ -60,7 +60,7 @@ bool MutiTouchTestLayer::init() listener->onTouchesBegan = CC_CALLBACK_2(MutiTouchTestLayer::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(MutiTouchTestLayer::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(MutiTouchTestLayer::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto title = LabelTTF::create("Please touch the screen!", "", 24); title->setPosition(VisibleRect::top()+Point(0, -40)); diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp index cecb32a6d8..aa45afa24d 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp @@ -124,8 +124,6 @@ void TouchableSpriteTest::onEnter() { EventDispatcherTestDemo::onEnter(); - auto dispatcher = EventDispatcher::getInstance(); - Point origin = Director::getInstance()->getVisibleOrigin(); Size size = Director::getInstance()->getVisibleSize(); @@ -180,16 +178,16 @@ void TouchableSpriteTest::onEnter() } }; - dispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1); - dispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2); - dispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite3); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite3); auto removeAllTouchItem = MenuItemFont::create("Remove All Touch Listeners", [this](Object* sender){ auto senderItem = static_cast(sender); senderItem->setString("Only Next item could be clicked"); - EventDispatcher::getInstance()->removeEventListeners(EventListener::TYPE_TOUCH_ONE_BY_ONE); + _eventDispatcher->removeEventListeners(EventListener::TYPE_TOUCH_ONE_BY_ONE); auto nextItem = MenuItemFont::create("Next", [=](Object* sender){ nextCallback(nullptr); @@ -242,8 +240,6 @@ public: { Sprite::onEnter(); - auto dispatcher = EventDispatcher::getInstance(); - auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); @@ -271,19 +267,18 @@ public: if (_useNodePriority) { - dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); } else { - dispatcher->addEventListenerWithFixedPriority(listener, _fixedPriority); + _eventDispatcher->addEventListenerWithFixedPriority(listener, _fixedPriority); } _listener = listener; } void onExit() override { - auto dispatcher = EventDispatcher::getInstance(); - dispatcher->removeEventListener(_listener); + _eventDispatcher->removeEventListener(_listener); Sprite::onExit(); } @@ -339,8 +334,6 @@ void RemoveListenerWhenDispatching::onEnter() { EventDispatcherTestDemo::onEnter(); - auto dispatcher = EventDispatcher::getInstance(); - Point origin = Director::getInstance()->getVisibleOrigin(); Size size = Director::getInstance()->getVisibleSize(); @@ -372,7 +365,7 @@ void RemoveListenerWhenDispatching::onEnter() sprite1->setColor(Color3B::WHITE); }; - dispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1); auto statusLabel = LabelTTF::create("The sprite could be touched!", "", 20); statusLabel->setPosition(origin + Point(size.width/2, size.height-90)); @@ -382,14 +375,14 @@ void RemoveListenerWhenDispatching::onEnter() auto toggleItem = MenuItemToggle::createWithCallback([=](Object* sender){ if (*enable) { - dispatcher->removeEventListener(listener1); + _eventDispatcher->removeEventListener(listener1); statusLabel->setString("The sprite could not be touched!"); (*enable) = false; } else { - dispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1); statusLabel->setString("The sprite could be touched!"); (*enable) = true; @@ -418,8 +411,6 @@ void CustomEventTest::onEnter() { EventDispatcherTestDemo::onEnter(); - auto dispatcher = EventDispatcher::getInstance(); - Point origin = Director::getInstance()->getVisibleOrigin(); Size size = Director::getInstance()->getVisibleSize(); @@ -437,7 +428,7 @@ void CustomEventTest::onEnter() delete[] buf; }); - dispatcher->addEventListenerWithFixedPriority(_listener, 1); + _eventDispatcher->addEventListenerWithFixedPriority(_listener, 1); auto sendItem = MenuItemFont::create("Send Custom Event", [=](Object* sender){ static int count = 0; @@ -446,7 +437,7 @@ void CustomEventTest::onEnter() sprintf(buf, "%d", count); EventCustom event(game_custom_event); event.setUserData(buf); - dispatcher->dispatchEvent(&event); + _eventDispatcher->dispatchEvent(&event); }); sendItem->setPosition(origin + Point(size.width/2, size.height/2)); auto menu = Menu::create(sendItem, nullptr); @@ -457,7 +448,7 @@ void CustomEventTest::onEnter() void CustomEventTest::onExit() { - EventDispatcher::getInstance()->removeEventListener(_listener); + _eventDispatcher->removeEventListener(_listener); EventDispatcherTestDemo::onExit(); } @@ -476,8 +467,6 @@ void LabelKeyboardEventTest::onEnter() { EventDispatcherTestDemo::onEnter(); - auto dispatcher = EventDispatcher::getInstance(); - Point origin = Director::getInstance()->getVisibleOrigin(); Size size = Director::getInstance()->getVisibleSize(); @@ -500,7 +489,7 @@ void LabelKeyboardEventTest::onEnter() label->setString(buf); }; - dispatcher->addEventListenerWithSceneGraphPriority(listener, statusLabel); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, statusLabel); } std::string LabelKeyboardEventTest::title() @@ -524,8 +513,6 @@ _pos = _max; \ EventDispatcherTestDemo::onEnter(); - auto dispatcher = EventDispatcher::getInstance(); - Point origin = Director::getInstance()->getVisibleOrigin(); Size size = Director::getInstance()->getVisibleSize(); @@ -550,7 +537,7 @@ _pos = _max; \ sprite->setPosition(ptNow); }); - dispatcher->addEventListenerWithSceneGraphPriority(listener, sprite); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, sprite); } void SpriteAccelerationEventTest::onExit() @@ -576,8 +563,6 @@ void RemoveAndRetainNodeTest::onEnter() EventDispatcherTestDemo::onEnter(); - auto dispatcher = EventDispatcher::getInstance(); - Point origin = Director::getInstance()->getVisibleOrigin(); Size size = Director::getInstance()->getVisibleSize(); @@ -616,7 +601,7 @@ void RemoveAndRetainNodeTest::onEnter() target->setOpacity(255); }; - dispatcher->addEventListenerWithSceneGraphPriority(listener1, _sprite); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, _sprite); this->runAction(Sequence::create(DelayTime::create(5.0f), CallFunc::create([this](){ diff --git a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp index 3276a9b620..82a8a6ebcb 100644 --- a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp @@ -725,7 +725,7 @@ ConvertToNode::ConvertToNode() { auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(ConvertToNode::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto s = Director::getInstance()->getWinSize(); diff --git a/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp b/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp index e14105b62e..5f58111b6c 100644 --- a/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp @@ -89,7 +89,7 @@ Parallax2::Parallax2() { auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesMoved = CC_CALLBACK_2(Parallax2::onTouchesMoved, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); // Top Layer, a simple image auto cocosImage = Sprite::create(s_Power); diff --git a/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp b/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp index 9e35114538..d238482244 100644 --- a/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp @@ -1072,7 +1072,7 @@ void ParticleDemo::onEnter(void) listener->onTouchesBegan = CC_CALLBACK_2(ParticleDemo::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(ParticleDemo::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(ParticleDemo::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto s = Director::getInstance()->getWinSize(); diff --git a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp index 5f1e9566ab..f3a804d907 100644 --- a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp +++ b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp @@ -125,7 +125,7 @@ void TouchesPerformTest1::onEnter() listener->onTouchMoved = CC_CALLBACK_2(TouchesPerformTest1::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(TouchesPerformTest1::onTouchEnded, this); listener->onTouchCancelled = CC_CALLBACK_2(TouchesPerformTest1::onTouchCancelled, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); } std::string TouchesPerformTest1::title() @@ -168,7 +168,7 @@ void TouchesPerformTest2::onEnter() listener->onTouchesMoved = CC_CALLBACK_2(TouchesPerformTest2::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(TouchesPerformTest2::onTouchesEnded, this); listener->onTouchesCancelled = CC_CALLBACK_2(TouchesPerformTest2::onTouchesCancelled, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); } std::string TouchesPerformTest2::title() @@ -242,14 +242,14 @@ void TouchesPerformTest3::onEnter() listener->onTouchMoved = CC_CALLBACK_2(TouchableLayer::onTouchMoved, layer); listener->onTouchEnded = CC_CALLBACK_2(TouchableLayer::onTouchEnded, layer); listener->onTouchCancelled = CC_CALLBACK_2(TouchableLayer::onTouchCancelled, layer); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, layer); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, layer); addChild(layer, zorder); layer->release(); } auto emitEventlabel = LabelTTF::create("Emit Touch Event", "", 24); - auto menuItem = MenuItemLabel::create(emitEventlabel, [](Object* sender){ + auto menuItem = MenuItemLabel::create(emitEventlabel, [this](Object* sender){ CC_PROFILER_PURGE_ALL(); @@ -265,13 +265,11 @@ void TouchesPerformTest3::onEnter() event.setEventCode(EventTouch::EventCode::BEGAN); event.setTouches(touches); - auto dispatcher = EventDispatcher::getInstance(); - for (int i = 0; i < 100; ++i) { CC_PROFILER_START(TOUCH_PROFILER_NAME); - dispatcher->dispatchEvent(&event); + _eventDispatcher->dispatchEvent(&event); CC_PROFILER_STOP(TOUCH_PROFILER_NAME); } diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp index 874f5ebe61..fab2a82437 100644 --- a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp @@ -200,10 +200,10 @@ void PhysicsDemoClickAdd::onEnter() auto touchListener = EventListenerTouchAllAtOnce::create(); touchListener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoClickAdd::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(touchListener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); auto accListener = EventListenerAcceleration::create(CC_CALLBACK_2(PhysicsDemoClickAdd::onAcceleration, this)); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(accListener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(accListener, this); auto node = Node::create(); node->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size)); @@ -514,7 +514,7 @@ void PhysicsDemoRayCast::onEnter() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoRayCast::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); _scene->getPhysicsWorld()->setGravity(Point::ZERO); @@ -717,7 +717,7 @@ void PhysicsDemoJoints::onEnter() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoJoints::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); _scene->getPhysicsWorld()->setGravity(Point::ZERO); diff --git a/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp b/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp index 113d4adad3..baf174ac9b 100644 --- a/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp +++ b/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp @@ -114,7 +114,7 @@ RenderTextureSave::RenderTextureSave() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesMoved = CC_CALLBACK_2(RenderTextureSave::onTouchesMoved, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); // Save Image menu MenuItemFont::setFontSize(16); @@ -303,7 +303,7 @@ RenderTextureZbuffer::RenderTextureZbuffer() listener->onTouchesBegan = CC_CALLBACK_2(RenderTextureZbuffer::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(RenderTextureZbuffer::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(RenderTextureZbuffer::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto size = Director::getInstance()->getWinSize(); auto label = LabelTTF::create("vertexZ = 50", "Marker Felt", 64); @@ -637,7 +637,7 @@ SpriteRenderTextureBug::SpriteRenderTextureBug() { auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(SpriteRenderTextureBug::onTouchesEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords(Point(s.width/2, s.height/2)); diff --git a/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id b/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id index ee1480acae..499b1b8dfe 100644 --- a/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id +++ b/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id @@ -1 +1 @@ -fdf35bd639fd5d617e5a280530d6aefe63b94857 \ No newline at end of file +71aa9486e8535b9bd65cae247bb86de59c83f522 \ No newline at end of file diff --git a/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp b/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp index b922271656..d0ac2cb6b6 100644 --- a/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp +++ b/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp @@ -130,7 +130,7 @@ KeyboardNotificationLayer::KeyboardNotificationLayer() auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(KeyboardNotificationLayer::onTouchBegan, this); listener->onTouchEnded = CC_CALLBACK_2(KeyboardNotificationLayer::onTouchEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); } //void KeyboardNotificationLayer::registerWithTouchDispatcher() diff --git a/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp b/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp index 48ed771306..8b18e2de4c 100644 --- a/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp +++ b/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp @@ -1432,7 +1432,7 @@ TileDemo::TileDemo(void) { auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesMoved = CC_CALLBACK_2(TileDemo::onTouchesMoved, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); } TileDemo::~TileDemo(void) diff --git a/samples/Cpp/TestCpp/Classes/TouchesTest/Paddle.cpp b/samples/Cpp/TestCpp/Classes/TouchesTest/Paddle.cpp index e168d7a1bc..0919b771eb 100644 --- a/samples/Cpp/TestCpp/Classes/TouchesTest/Paddle.cpp +++ b/samples/Cpp/TestCpp/Classes/TouchesTest/Paddle.cpp @@ -45,7 +45,7 @@ void Paddle::onEnter() listener->onTouchMoved = CC_CALLBACK_2(Paddle::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(Paddle::onTouchEnded, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); } void Paddle::onExit() diff --git a/samples/Cpp/TestCpp/Classes/controller.cpp b/samples/Cpp/TestCpp/Classes/controller.cpp index fcd140e4f3..32051bbd1b 100644 --- a/samples/Cpp/TestCpp/Classes/controller.cpp +++ b/samples/Cpp/TestCpp/Classes/controller.cpp @@ -129,7 +129,7 @@ TestController::TestController() listener->onTouchBegan = CC_CALLBACK_2(TestController::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(TestController::onTouchMoved, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); } TestController::~TestController() From 0622434321b4b3620f6242ca35b77ef028f30614 Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 28 Oct 2013 10:49:21 +0800 Subject: [PATCH 31/42] issue #3069: Using `std::string` to initialize `EventCustom`. It will be easier for editor parser to emit callback event. --- cocos/2d/CCEventCustom.cpp | 7 ++++--- cocos/2d/CCEventCustom.h | 2 +- cocos/2d/CCEventListenerCustom.cpp | 6 +++--- cocos/2d/CCEventListenerCustom.h | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/cocos/2d/CCEventCustom.cpp b/cocos/2d/CCEventCustom.cpp index 89e56a3a69..8960b9fd87 100644 --- a/cocos/2d/CCEventCustom.cpp +++ b/cocos/2d/CCEventCustom.cpp @@ -24,14 +24,15 @@ #include "CCEventCustom.h" #include "ccMacros.h" +#include NS_CC_BEGIN -EventCustom::EventCustom(Type type) -: Event(type) +EventCustom::EventCustom(const std::string& eventName) +: Event(std::hash()(eventName)) , _userData(nullptr) { - CCASSERT(type >= TYPE_CUSTOM, "custom type should be greater than TYPE_CUSTOM."); +// CCASSERT(type >= TYPE_CUSTOM, "custom type should be greater than TYPE_CUSTOM."); } NS_CC_END diff --git a/cocos/2d/CCEventCustom.h b/cocos/2d/CCEventCustom.h index 96676cdbfd..c4521e3bbf 100644 --- a/cocos/2d/CCEventCustom.h +++ b/cocos/2d/CCEventCustom.h @@ -33,7 +33,7 @@ class EventCustom : public Event { public: /** Constructor */ - EventCustom(Type type); + EventCustom(const std::string& eventName); /** Set user data */ inline void setUserData(void* data) { _userData = data; }; diff --git a/cocos/2d/CCEventListenerCustom.cpp b/cocos/2d/CCEventListenerCustom.cpp index 3875f61c94..048fede198 100644 --- a/cocos/2d/CCEventListenerCustom.cpp +++ b/cocos/2d/CCEventListenerCustom.cpp @@ -32,10 +32,10 @@ EventListenerCustom::EventListenerCustom() { } -EventListenerCustom* EventListenerCustom::create(Type type, std::function callback) +EventListenerCustom* EventListenerCustom::create(const std::string& eventName, std::function callback) { EventListenerCustom* ret = new EventListenerCustom(); - if (ret && ret->init(type, callback)) + if (ret && ret->init(std::hash()(eventName), callback)) { ret->autorelease(); } @@ -46,7 +46,7 @@ EventListenerCustom* EventListenerCustom::create(Type type, std::functioncallback) +bool EventListenerCustom::init(Type type, std::functioncallback) { bool ret = false; diff --git a/cocos/2d/CCEventListenerCustom.h b/cocos/2d/CCEventListenerCustom.h index 285d257cd3..0bd5bec700 100644 --- a/cocos/2d/CCEventListenerCustom.h +++ b/cocos/2d/CCEventListenerCustom.h @@ -56,7 +56,7 @@ public: * @param eventType The type of the event. * @param callback The callback function when the specified event was emitted. */ - static EventListenerCustom* create(int type, std::function callback); + static EventListenerCustom* create(const std::string& eventName, std::function callback); /// Overrides virtual bool checkAvailable() override; @@ -67,7 +67,7 @@ protected: EventListenerCustom(); /** Initializes event with type and callback function */ - bool init(int type, std::function callback); + bool init(Type type, std::function callback); std::function _onCustomEvent; }; From e6b0134080d5fe64f22d384259a2cc2f1b9b2947 Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 28 Oct 2013 10:49:43 +0800 Subject: [PATCH 32/42] issue #3069: Updating Custom event test. --- .../NewEventDispatcherTest.cpp | 45 +++++++++++++++---- .../NewEventDispatcherTest.h | 1 + 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp index aa45afa24d..1e7c746a56 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp @@ -414,33 +414,61 @@ void CustomEventTest::onEnter() Point origin = Director::getInstance()->getVisibleOrigin(); Size size = Director::getInstance()->getVisibleSize(); - auto statusLabel = LabelTTF::create("No custom event received!", "", 20); + MenuItemFont::setFontSize(20); + + auto statusLabel = LabelTTF::create("No custom event 1 received!", "", 20); statusLabel->setPosition(origin + Point(size.width/2, size.height-90)); addChild(statusLabel); - const int game_custom_event = EventListener::TYPE_CUSTOM + 1; - _listener = EventListenerCustom::create(game_custom_event, [=](EventCustom* event){ - std::string str("Custom event received, "); + _listener = EventListenerCustom::create("game_custom_event1", [=](EventCustom* event){ + std::string str("Custom event 1 received, "); char* buf = static_cast(event->getUserData()); str += buf; str += " times"; statusLabel->setString(str.c_str()); - delete[] buf; }); _eventDispatcher->addEventListenerWithFixedPriority(_listener, 1); - auto sendItem = MenuItemFont::create("Send Custom Event", [=](Object* sender){ + auto sendItem = MenuItemFont::create("Send Custom Event 1", [=](Object* sender){ static int count = 0; ++count; char* buf = new char[10]; sprintf(buf, "%d", count); - EventCustom event(game_custom_event); + EventCustom event("game_custom_event1"); event.setUserData(buf); _eventDispatcher->dispatchEvent(&event); + CC_SAFE_DELETE_ARRAY(buf); }); sendItem->setPosition(origin + Point(size.width/2, size.height/2)); - auto menu = Menu::create(sendItem, nullptr); + + auto statusLabel2 = LabelTTF::create("No custom event 2 received!", "", 20); + statusLabel2->setPosition(origin + Point(size.width/2, size.height-120)); + addChild(statusLabel2); + + _listener2 = EventListenerCustom::create("game_custom_event2", [=](EventCustom* event){ + std::string str("Custom event 2 received, "); + char* buf = static_cast(event->getUserData()); + str += buf; + str += " times"; + statusLabel2->setString(str.c_str()); + }); + + _eventDispatcher->addEventListenerWithFixedPriority(_listener2, 1); + + auto sendItem2 = MenuItemFont::create("Send Custom Event 2", [=](Object* sender){ + static int count = 0; + ++count; + char* buf = new char[10]; + sprintf(buf, "%d", count); + EventCustom event("game_custom_event2"); + event.setUserData(buf); + _eventDispatcher->dispatchEvent(&event); + CC_SAFE_DELETE_ARRAY(buf); + }); + sendItem2->setPosition(origin + Point(size.width/2, size.height/2 - 40)); + + auto menu = Menu::create(sendItem, sendItem2, nullptr); menu->setPosition(Point(0, 0)); menu->setAnchorPoint(Point(0, 0)); addChild(menu, -1); @@ -449,6 +477,7 @@ void CustomEventTest::onEnter() void CustomEventTest::onExit() { _eventDispatcher->removeEventListener(_listener); + _eventDispatcher->removeEventListener(_listener2); EventDispatcherTestDemo::onExit(); } diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h index 9af7022bde..080e623008 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h @@ -64,6 +64,7 @@ public: virtual std::string subtitle() override; private: EventListenerCustom* _listener; + EventListenerCustom* _listener2; }; class LabelKeyboardEventTest : public EventDispatcherTestDemo From 8d11f484a96cee937761254617b61bea2dccabfc Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 28 Oct 2013 10:52:23 +0800 Subject: [PATCH 33/42] issue #3069: Removing unused comments. --- cocos/2d/CCEventCustom.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/cocos/2d/CCEventCustom.cpp b/cocos/2d/CCEventCustom.cpp index 8960b9fd87..29d8afc551 100644 --- a/cocos/2d/CCEventCustom.cpp +++ b/cocos/2d/CCEventCustom.cpp @@ -32,7 +32,6 @@ EventCustom::EventCustom(const std::string& eventName) : Event(std::hash()(eventName)) , _userData(nullptr) { -// CCASSERT(type >= TYPE_CUSTOM, "custom type should be greater than TYPE_CUSTOM."); } NS_CC_END From 8b7d1934c81ac1c59ae46c5a3db91f0d3384d9f7 Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 28 Oct 2013 16:00:01 +0800 Subject: [PATCH 34/42] issue #3069: Using enum class for define Event type and EventListener type. --- cocos/2d/CCEvent.h | 12 +- cocos/2d/CCEventAcceleration.cpp | 2 +- cocos/2d/CCEventCustom.cpp | 3 +- cocos/2d/CCEventCustom.h | 7 +- cocos/2d/CCEventDispatcher.cpp | 190 ++++++++++++++--------- cocos/2d/CCEventDispatcher.h | 22 +-- cocos/2d/CCEventKeyboard.cpp | 5 + cocos/2d/CCEventKeyboard.h | 8 +- cocos/2d/CCEventListener.cpp | 3 +- cocos/2d/CCEventListener.h | 42 +++-- cocos/2d/CCEventListenerAcceleration.cpp | 2 +- cocos/2d/CCEventListenerAcceleration.h | 2 + cocos/2d/CCEventListenerCustom.cpp | 6 +- cocos/2d/CCEventListenerCustom.h | 2 +- cocos/2d/CCEventListenerKeyboard.cpp | 2 +- cocos/2d/CCEventListenerKeyboard.h | 2 + cocos/2d/CCEventListenerTouch.cpp | 4 +- cocos/2d/CCEventListenerTouch.h | 4 + cocos/2d/CCEventTouch.cpp | 5 + cocos/2d/CCEventTouch.h | 6 +- 20 files changed, 197 insertions(+), 132 deletions(-) diff --git a/cocos/2d/CCEvent.h b/cocos/2d/CCEvent.h index eecd7f396c..cd2be49adb 100644 --- a/cocos/2d/CCEvent.h +++ b/cocos/2d/CCEvent.h @@ -41,15 +41,14 @@ class Node; class Event { public: - enum EventType + enum class Type { - TYPE_TOUCH = 1, - TYPE_KEYBOARD = 3, - TYPE_ACCELERATION, - TYPE_CUSTOM + TOUCH, + KEYBOARD, + ACCELERATION, + CUSTOM }; - typedef int Type; protected: /** Constructor */ Event(Type type); @@ -78,6 +77,7 @@ protected: inline void setCurrentTarget(Node* target) { _currentTarget = target; }; Type _type; ///< Event type + bool _isStopped; ///< whether the event has been stopped. Node* _currentTarget; ///< Current target diff --git a/cocos/2d/CCEventAcceleration.cpp b/cocos/2d/CCEventAcceleration.cpp index 7039c03ff8..1a8a075d88 100644 --- a/cocos/2d/CCEventAcceleration.cpp +++ b/cocos/2d/CCEventAcceleration.cpp @@ -27,7 +27,7 @@ NS_CC_BEGIN EventAcceleration::EventAcceleration(Acceleration acc) -: Event(TYPE_ACCELERATION) +: Event(Type::ACCELERATION) , _acc(acc) { } diff --git a/cocos/2d/CCEventCustom.cpp b/cocos/2d/CCEventCustom.cpp index 29d8afc551..65e9d4c2f5 100644 --- a/cocos/2d/CCEventCustom.cpp +++ b/cocos/2d/CCEventCustom.cpp @@ -29,8 +29,9 @@ NS_CC_BEGIN EventCustom::EventCustom(const std::string& eventName) -: Event(std::hash()(eventName)) +: Event(Type::CUSTOM) , _userData(nullptr) +, _eventName(eventName) { } diff --git a/cocos/2d/CCEventCustom.h b/cocos/2d/CCEventCustom.h index c4521e3bbf..4a7d750940 100644 --- a/cocos/2d/CCEventCustom.h +++ b/cocos/2d/CCEventCustom.h @@ -35,14 +35,17 @@ public: /** Constructor */ EventCustom(const std::string& eventName); - /** Set user data */ + /** Sets user data */ inline void setUserData(void* data) { _userData = data; }; - /** Get user data */ + /** Gets user data */ inline void* getUserData() const { return _userData; }; + /** Gets event name */ + inline const std::string& getEventName() const { return _eventName; }; protected: void* _userData; ///< User data + std::string _eventName; }; NS_CC_END diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index e0922b0796..1ebadf9b74 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -24,6 +24,7 @@ #include "CCEventDispatcher.h" #include "CCEvent.h" #include "CCEventTouch.h" +#include "CCEventCustom.h" #include "CCEventListenerTouch.h" #include "CCNode.h" #include "CCDirector.h" @@ -58,6 +59,37 @@ private: NS_CC_BEGIN +static EventListener::ListenerID getListenerID(Event* event) +{ + EventListener::ListenerID ret; + switch (event->getType()) + { + case Event::Type::ACCELERATION: + ret = static_cast(EventListener::Type::ACCELERATION); + break; + case Event::Type::CUSTOM: + { + auto customEvent = static_cast(event); + auto listenerID = std::hash()(customEvent->getEventName()); + ret = static_cast(listenerID); + } + break; + case Event::Type::KEYBOARD: + ret = static_cast(EventListener::Type::KEYBOARD); + break; + case Event::Type::TOUCH: + // Touch listener is very special, it contains two kinds of listeners, EventListenerTouchOneByOne and EventListenerTouchAllAtOnce. + // return UNKNOW instead. + ret = static_cast(EventListener::Type::UNKNOWN); + break; + default: + CCASSERT(false, "Invalid type!"); + break; + } + + return ret; +} + EventDispatcher::EventListenerVector::EventListenerVector() : _sceneGraphListeners(nullptr) , _fixedListeners(nullptr) @@ -90,7 +122,7 @@ bool EventDispatcher::EventListenerVector::empty() const void EventDispatcher::EventListenerVector::push_back(EventListener* listener) { - if (listener->_fixedPriority == 0) + if (listener->getFixedPriority() == 0) { if (_sceneGraphListeners == nullptr) { @@ -196,7 +228,7 @@ void EventDispatcher::pauseTarget(Node* node) auto listeners = listenerIter->second; for (auto& l : *listeners) { - l->_paused = true; + l->setPaused(true); } } } @@ -209,7 +241,7 @@ void EventDispatcher::resumeTarget(Node* node) auto listeners = listenerIter->second; for (auto& l : *listeners) { - l->_paused = false; + l->setPaused(false); } } setDirtyForNode(node); @@ -274,11 +306,11 @@ void EventDispatcher::addEventListener(EventListener* listener) { EventListenerVector* listenerList = nullptr; - auto iter = _listeners.find(listener->_type); + auto iter = _listeners.find(listener->getListenerID()); if (iter == _listeners.end()) { listenerList = new EventListenerVector(); - _listeners.insert(std::make_pair(listener->_type, listenerList)); + _listeners.insert(std::make_pair(listener->getListenerID(), listenerList)); } else { @@ -287,13 +319,13 @@ void EventDispatcher::addEventListener(EventListener* listener) listenerList->push_back(listener); - if (listener->_fixedPriority == 0) + if (listener->getFixedPriority() == 0) { - setDirty(listener->_type, DirtyFlag::SCENE_GRAPH_PRIORITY); + setDirty(listener->getListenerID(), DirtyFlag::SCENE_GRAPH_PRIORITY); } else { - setDirty(listener->_type, DirtyFlag::FIXED_PRITORY); + setDirty(listener->getListenerID(), DirtyFlag::FIXED_PRITORY); } } else @@ -305,17 +337,16 @@ void EventDispatcher::addEventListener(EventListener* listener) void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node) { CCASSERT(listener && node, "Invalid parameters."); - CCASSERT(!listener->_isRegistered, "The listener has been registered."); + CCASSERT(!listener->isRegistered(), "The listener has been registered."); if (!listener->checkAvailable()) return; - listener->_node = node; - listener->_fixedPriority = 0; - + listener->setSceneGraphPriority(node); + listener->setFixedPriority(0); + listener->setRegistered(true); + listener->retain(); - listener->_isRegistered = true; - addEventListener(listener); associateNodeAndEventListener(node, listener); @@ -329,17 +360,18 @@ void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* list void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority) { CCASSERT(listener, "Invalid parameters."); - CCASSERT(!listener->_isRegistered, "The listener has been registered."); + CCASSERT(!listener->isRegistered(), "The listener has been registered."); CCASSERT(fixedPriority != 0, "0 priority is forbidden for fixed priority since it's used for scene graph based priority."); if (!listener->checkAvailable()) return; - listener->_node = nullptr; - listener->_fixedPriority = fixedPriority; + listener->setSceneGraphPriority(nullptr); + listener->setFixedPriority(fixedPriority); + listener->setRegistered(true); + listener->setPaused(false); + listener->retain(); - listener->_isRegistered = true; - listener->_paused = false; addEventListener(listener); } @@ -361,10 +393,10 @@ void EventDispatcher::removeEventListener(EventListener* listener) if (l == listener) { CC_SAFE_RETAIN(l); - l->_isRegistered = false; - if (l->_node != nullptr) + l->setRegistered(false); + if (l->getSceneGraphPriority() != nullptr) { - dissociateNodeAndEventListener(l->_node, l); + dissociateNodeAndEventListener(l->getSceneGraphPriority(), l); } if (_inDispatch == 0) @@ -393,7 +425,7 @@ void EventDispatcher::removeEventListener(EventListener* listener) if (iter->second->empty()) { - _priorityDirtyFlagMap.erase(listener->_type); + _priorityDirtyFlagMap.erase(listener->getListenerID()); auto list = iter->second; iter = _listeners.erase(iter); CC_SAFE_DELETE(list); @@ -426,12 +458,12 @@ void EventDispatcher::setPriority(EventListener* listener, int fixedPriority) auto found = std::find(fixedPriorityListeners->begin(), fixedPriorityListeners->end(), listener); if (found != fixedPriorityListeners->end()) { - CCASSERT(listener->_node == nullptr, "Can't set fixed priority with scene graph based listener."); + CCASSERT(listener->getSceneGraphPriority() == nullptr, "Can't set fixed priority with scene graph based listener."); - if (listener->_fixedPriority != fixedPriority) + if (listener->getFixedPriority() != fixedPriority) { - listener->_fixedPriority = fixedPriority; - setDirty(listener->_type, DirtyFlag::FIXED_PRITORY); + listener->setFixedPriority(fixedPriority); + setDirty(listener->getListenerID(), DirtyFlag::FIXED_PRITORY); } return; } @@ -505,21 +537,23 @@ void EventDispatcher::dispatchEvent(Event* event) DispatchGuard guard(_inDispatch); - if (event->getType() == Event::TYPE_TOUCH) + if (event->getType() == Event::Type::TOUCH) { dispatchTouchEvent(static_cast(event)); return; } - sortEventListeners(event->getType()); + auto listenerID = getListenerID(event); - auto iter = _listeners.find(event->getType()); + sortEventListeners(listenerID); + + auto iter = _listeners.find(listenerID); if (iter != _listeners.end()) { auto listeners = iter->second; auto onEvent = [&event](EventListener* listener) -> bool{ - event->setCurrentTarget(listener->_node); + event->setCurrentTarget(listener->getSceneGraphPriority()); listener->_onEvent(event); return event->isStopped(); }; @@ -527,16 +561,19 @@ void EventDispatcher::dispatchEvent(Event* event) dispatchEventToListeners(listeners, onEvent); } - updateListeners(event->getType()); + updateListeners(event); } void EventDispatcher::dispatchTouchEvent(EventTouch* event) { - sortEventListeners(EventListener::TYPE_TOUCH_ONE_BY_ONE); - sortEventListeners(EventListener::TYPE_TOUCH_ALL_AT_ONCE); + auto touchOneByOneID = static_cast(EventListener::Type::TOUCH_ONE_BY_ONE); + auto touchAllAtOnceID = static_cast(EventListener::Type::TOUCH_ALL_AT_ONCE); - auto oneByOnelisteners = getListeners(EventListener::TYPE_TOUCH_ONE_BY_ONE); - auto allAtOncelisteners = getListeners(EventListener::TYPE_TOUCH_ALL_AT_ONCE); + sortEventListeners(touchOneByOneID); + sortEventListeners(touchAllAtOnceID); + + auto oneByOnelisteners = getListeners(touchOneByOneID); + auto allAtOncelisteners = getListeners(touchAllAtOnceID); // If there aren't any touch listeners, return directly. if (nullptr == oneByOnelisteners && nullptr == allAtOncelisteners) @@ -627,7 +664,7 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) // If the event was stopped, return directly. if (event->isStopped()) { - updateListeners(event->getType()); + updateListeners(event); return true; } @@ -706,7 +743,7 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) // If the event was stopped, return directly. if (event->isStopped()) { - updateListeners(event->getType()); + updateListeners(event); return false; } @@ -720,14 +757,14 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event) } } - updateListeners(event->getType()); + updateListeners(event); } -void EventDispatcher::updateListeners(Event::Type eventType) +void EventDispatcher::updateListeners(Event* event) { - auto onUpdateListeners = [this](EventListener::Type listenerType) + auto onUpdateListeners = [this](EventListener::ListenerID listenerID) { - auto listenersIter = _listeners.find(listenerType); + auto listenersIter = _listeners.find(listenerID); if (listenersIter == _listeners.end()) return; @@ -740,7 +777,7 @@ void EventDispatcher::updateListeners(Event::Type eventType) for (auto iter = sceneGraphPriorityListeners->begin(); iter != sceneGraphPriorityListeners->end();) { auto l = *iter; - if (!l->_isRegistered) + if (!l->isRegistered()) { iter = sceneGraphPriorityListeners->erase(iter); l->release(); @@ -757,7 +794,7 @@ void EventDispatcher::updateListeners(Event::Type eventType) for (auto iter = fixedPriorityListeners->begin(); iter != fixedPriorityListeners->end();) { auto l = *iter; - if (!l->_isRegistered) + if (!l->isRegistered()) { iter = fixedPriorityListeners->erase(iter); l->release(); @@ -791,14 +828,14 @@ void EventDispatcher::updateListeners(Event::Type eventType) } }; - if (eventType == Event::TYPE_TOUCH) + if (event->getType() == Event::Type::TOUCH) { - onUpdateListeners(EventListener::TYPE_TOUCH_ONE_BY_ONE); - onUpdateListeners(EventListener::TYPE_TOUCH_ALL_AT_ONCE); + onUpdateListeners(static_cast(EventListener::Type::TOUCH_ONE_BY_ONE)); + onUpdateListeners(static_cast(EventListener::Type::TOUCH_ALL_AT_ONCE)); } else { - onUpdateListeners(eventType); + onUpdateListeners(getListenerID(event)); } @@ -808,12 +845,13 @@ void EventDispatcher::updateListeners(Event::Type eventType) for (auto& listener : _toAddedListeners) { - auto itr = _listeners.find(listener->_type); + EventListener::ListenerID listenerID = listener->getListenerID(); + auto itr = _listeners.find(listenerID); if (itr == _listeners.end()) { listeners = new EventListenerVector(); - _listeners.insert(std::make_pair(listener->_type, listeners)); + _listeners.insert(std::make_pair(listenerID, listeners)); } else { @@ -822,13 +860,13 @@ void EventDispatcher::updateListeners(Event::Type eventType) listeners->push_back(listener); - if (listener->_fixedPriority == 0) + if (listener->getFixedPriority() == 0) { - setDirty(listener->_type, DirtyFlag::SCENE_GRAPH_PRIORITY); + setDirty(listenerID, DirtyFlag::SCENE_GRAPH_PRIORITY); } else { - setDirty(listener->_type, DirtyFlag::FIXED_PRITORY); + setDirty(listenerID, DirtyFlag::FIXED_PRITORY); } } _toAddedListeners.clear(); @@ -846,7 +884,7 @@ void EventDispatcher::updateDirtyFlagForSceneGraph() { for (auto& l : *iter->second) { - setDirty(l->_type, DirtyFlag::SCENE_GRAPH_PRIORITY); + setDirty(l->getListenerID(), DirtyFlag::SCENE_GRAPH_PRIORITY); } } } @@ -855,11 +893,11 @@ void EventDispatcher::updateDirtyFlagForSceneGraph() } } -void EventDispatcher::sortEventListeners(EventListener::Type eventListenerType) +void EventDispatcher::sortEventListeners(EventListener::ListenerID listenerID) { DirtyFlag dirtyFlag = DirtyFlag::NONE; - auto dirtyIter = _priorityDirtyFlagMap.find(eventListenerType); + auto dirtyIter = _priorityDirtyFlagMap.find(listenerID); if (dirtyIter != _priorityDirtyFlagMap.end()) { dirtyFlag = dirtyIter->second; @@ -869,21 +907,21 @@ void EventDispatcher::sortEventListeners(EventListener::Type eventListenerType) { if ((int)dirtyFlag & (int)DirtyFlag::FIXED_PRITORY) { - sortEventListenersOfFixedPriority(eventListenerType); + sortEventListenersOfFixedPriority(listenerID); } if ((int)dirtyFlag & (int)DirtyFlag::SCENE_GRAPH_PRIORITY) { - sortEventListenersOfSceneGraphPriority(eventListenerType); + sortEventListenersOfSceneGraphPriority(listenerID); } dirtyIter->second = DirtyFlag::NONE; } } -void EventDispatcher::sortEventListenersOfSceneGraphPriority(EventListener::Type eventListenerType) +void EventDispatcher::sortEventListenersOfSceneGraphPriority(EventListener::ListenerID listenerID) { - auto listeners = getListeners(eventListenerType); + auto listeners = getListeners(listenerID); if (listeners == nullptr) return; @@ -898,7 +936,7 @@ void EventDispatcher::sortEventListenersOfSceneGraphPriority(EventListener::Type // After sort: priority < 0, > 0 auto sceneGraphlisteners = listeners->getSceneGraphPriorityListeners(); std::sort(sceneGraphlisteners->begin(), sceneGraphlisteners->end(), [this](const EventListener* l1, const EventListener* l2) { - return _nodePriorityMap[l1->_node] > _nodePriorityMap[l2->_node]; + return _nodePriorityMap[l1->getSceneGraphPriority()] > _nodePriorityMap[l2->getSceneGraphPriority()]; }); #if DUMP_LISTENER_ITEM_PRIORITY_INFO @@ -910,9 +948,9 @@ void EventDispatcher::sortEventListenersOfSceneGraphPriority(EventListener::Type #endif } -void EventDispatcher::sortEventListenersOfFixedPriority(EventListener::Type eventListenerType) +void EventDispatcher::sortEventListenersOfFixedPriority(EventListener::ListenerID listenerID) { - auto listeners = getListeners(eventListenerType); + auto listeners = getListeners(listenerID); if (listeners == nullptr) return; @@ -920,14 +958,14 @@ void EventDispatcher::sortEventListenersOfFixedPriority(EventListener::Type even // After sort: priority < 0, > 0 auto fixedlisteners = listeners->getFixedPriorityListeners(); std::sort(fixedlisteners->begin(), fixedlisteners->end(), [](const EventListener* l1, const EventListener* l2) { - return l1->_fixedPriority < l2->_fixedPriority; + return l1->getFixedPriority() < l2->getFixedPriority(); }); // FIXME: Should use binary search int index = 0; for (auto& listener : *fixedlisteners) { - if (listener->_fixedPriority >= 0) + if (listener->getFixedPriority() >= 0) break; ++index; } @@ -944,9 +982,9 @@ void EventDispatcher::sortEventListenersOfFixedPriority(EventListener::Type even } -EventDispatcher::EventListenerVector* EventDispatcher::getListeners(EventListener::Type eventListenerType) +EventDispatcher::EventListenerVector* EventDispatcher::getListeners(EventListener::ListenerID listenerID) { - auto iter = _listeners.find(eventListenerType); + auto iter = _listeners.find(listenerID); if (iter != _listeners.end()) { return iter->second; @@ -955,9 +993,9 @@ EventDispatcher::EventListenerVector* EventDispatcher::getListeners(EventListene return nullptr; } -void EventDispatcher::removeEventListeners(EventListener::Type eventListenerType) +void EventDispatcher::removeEventListeners(EventListener::ListenerID listenerID) { - auto listenerItemIter = _listeners.find(eventListenerType); + auto listenerItemIter = _listeners.find(listenerID); if (listenerItemIter != _listeners.end()) { auto listeners = listenerItemIter->second; @@ -971,10 +1009,10 @@ void EventDispatcher::removeEventListeners(EventListener::Type eventListenerType for (auto iter = listenerVector->begin(); iter != listenerVector->end();) { auto l = *iter; - l->_isRegistered = false; - if (l->_node != nullptr) + l->setRegistered(false); + if (l->getSceneGraphPriority() != nullptr) { - dissociateNodeAndEventListener(l->_node, l); + dissociateNodeAndEventListener(l->getSceneGraphPriority(), l); } if (_inDispatch == 0) @@ -997,7 +1035,7 @@ void EventDispatcher::removeEventListeners(EventListener::Type eventListenerType listeners->clear(); delete listeners; _listeners.erase(listenerItemIter); - _priorityDirtyFlagMap.erase(eventListenerType); + _priorityDirtyFlagMap.erase(listenerID); } } } @@ -1042,12 +1080,12 @@ void EventDispatcher::setDirtyForNode(Node* node) } } -void EventDispatcher::setDirty(EventListener::Type eventListenerType, DirtyFlag flag) +void EventDispatcher::setDirty(EventListener::ListenerID listenerID, DirtyFlag flag) { - auto iter = _priorityDirtyFlagMap.find(eventListenerType); + auto iter = _priorityDirtyFlagMap.find(listenerID); if (iter == _priorityDirtyFlagMap.end()) { - _priorityDirtyFlagMap.insert(std::make_pair(eventListenerType, flag)); + _priorityDirtyFlagMap.insert(std::make_pair(listenerID, flag)); } else { diff --git a/cocos/2d/CCEventDispatcher.h b/cocos/2d/CCEventDispatcher.h index e3650a1fae..3609c7a591 100644 --- a/cocos/2d/CCEventDispatcher.h +++ b/cocos/2d/CCEventDispatcher.h @@ -74,8 +74,8 @@ public: */ void removeEventListener(EventListener* listener); - /** Removes all listeners with the same event listener type */ - void removeEventListeners(EventListener::Type eventListenerType); + /** Removes all listeners with the same event listener ID */ + void removeEventListeners(EventListener::ListenerID listenerID); /** Removes all listeners */ void removeAllEventListeners(); @@ -144,25 +144,25 @@ private: void addEventListener(EventListener* listener); /** Gets event the listener list for the event listener type. */ - EventListenerVector* getListeners(EventListener::Type eventListenerType); + EventListenerVector* getListeners(EventListener::ListenerID listenerID); /** Update dirty flag */ void updateDirtyFlagForSceneGraph(); /** Sort event listener */ - void sortEventListeners(EventListener::Type eventListenerType); + void sortEventListeners(EventListener::ListenerID listenerID); /** Sorts the listeners of specified type by scene graph priority */ - void sortEventListenersOfSceneGraphPriority(EventListener::Type eventListenerType); + void sortEventListenersOfSceneGraphPriority(EventListener::ListenerID listenerID); /** Sorts the listeners of specified type by fixed priority */ - void sortEventListenersOfFixedPriority(EventListener::Type eventListenerType); + void sortEventListenersOfFixedPriority(EventListener::ListenerID listenerID); /** Updates all listeners * 1) Removes all listener items that have been marked as 'removed' when dispatching event. * 2) Adds all listener items that have been marked as 'added' when dispatching event. */ - void updateListeners(Event::Type eventType); + void updateListeners(Event* event); /** Touch event needs to be processed different with other events since it needs support ALL_AT_ONCE and ONE_BY_NONE mode. */ void dispatchTouchEvent(EventTouch* event); @@ -185,18 +185,18 @@ private: ALL = FIXED_PRITORY | SCENE_GRAPH_PRIORITY }; - /** Sets the dirty flag for a specified listener type */ - void setDirty(EventListener::Type listenerType, DirtyFlag flag); + /** Sets the dirty flag for a specified listener ID */ + void setDirty(EventListener::ListenerID listenerID, DirtyFlag flag); /** Walks though scene graph to get the draw order for each node, it's called before sorting event listener with scene graph priority */ void visitTarget(Node* node); private: /** Listeners map */ - std::unordered_map _listeners; + std::unordered_map _listeners; /** The map of dirty flag */ - std::unordered_map _priorityDirtyFlagMap; + std::unordered_map _priorityDirtyFlagMap; /** The map of node and event listeners */ std::unordered_map*> _nodeListenersMap; diff --git a/cocos/2d/CCEventKeyboard.cpp b/cocos/2d/CCEventKeyboard.cpp index b02312fce1..83bf0a8d0a 100644 --- a/cocos/2d/CCEventKeyboard.cpp +++ b/cocos/2d/CCEventKeyboard.cpp @@ -27,5 +27,10 @@ NS_CC_BEGIN +EventKeyboard::EventKeyboard(KeyCode keyCode, bool isPressed) +: Event(Type::KEYBOARD) +, _keyCode(keyCode) +, _isPressed(isPressed) +{} NS_CC_END diff --git a/cocos/2d/CCEventKeyboard.h b/cocos/2d/CCEventKeyboard.h index b8cf7f8976..e0533e62fd 100644 --- a/cocos/2d/CCEventKeyboard.h +++ b/cocos/2d/CCEventKeyboard.h @@ -196,13 +196,7 @@ public: KEY_SEARCH = 0xFFAA }; - static const char* EVENT_TYPE; - - EventKeyboard(KeyCode keyCode, bool isPressed) - : Event(TYPE_KEYBOARD) - , _keyCode(keyCode) - , _isPressed(isPressed) - {}; + EventKeyboard(KeyCode keyCode, bool isPressed); private: KeyCode _keyCode; diff --git a/cocos/2d/CCEventListener.cpp b/cocos/2d/CCEventListener.cpp index 9040593e0e..b4f9fdd499 100644 --- a/cocos/2d/CCEventListener.cpp +++ b/cocos/2d/CCEventListener.cpp @@ -35,10 +35,11 @@ EventListener::~EventListener() CCLOGINFO("In the destructor of EventListener. %p", this); } -bool EventListener::init(Type t, std::function callback) +bool EventListener::init(Type t, ListenerID listenerID, std::function callback) { _onEvent = callback; _type = t; + _listenerID = listenerID; _isRegistered = false; _paused = true; diff --git a/cocos/2d/CCEventListener.h b/cocos/2d/CCEventListener.h index bcedcec0c8..bbf5121149 100644 --- a/cocos/2d/CCEventListener.h +++ b/cocos/2d/CCEventListener.h @@ -46,22 +46,24 @@ class Node; class EventListener : public Object { public: - enum EventListenerType + enum class Type { - TYPE_TOUCH_ONE_BY_ONE = 1, - TYPE_TOUCH_ALL_AT_ONCE, - TYPE_KEYBOARD, - TYPE_ACCELERATION, - TYPE_CUSTOM + UNKNOWN, + TOUCH_ONE_BY_ONE, + TOUCH_ALL_AT_ONCE, + KEYBOARD, + ACCELERATION, + CUSTOM }; - typedef int Type; + typedef int ListenerID; + protected: /** Constructor */ EventListener(); /** Initializes event with type and callback function */ - bool init(Type t, std::functioncallback); + bool init(Type t, ListenerID listenerID, std::functioncallback); public: /** Destructor */ virtual ~EventListener(); @@ -72,21 +74,33 @@ public: /** Clones the listener, its subclasses have to override this method. */ virtual EventListener* clone() = 0; + inline void setPaused(bool paused) { _paused = paused; }; inline bool isPaused() const { return _paused; }; + + inline void setRegistered(bool registered) { _isRegistered = registered; }; inline bool isRegistered() const { return _isRegistered; }; -protected: + inline Type getType() const { return _type; }; + inline ListenerID getListenerID() const { return _listenerID; }; + + inline void setFixedPriority(int fixedPriority) { _fixedPriority = fixedPriority; }; + inline int getFixedPriority() const { return _fixedPriority; }; + + inline void setSceneGraphPriority(Node* node) { _node = node; }; + inline Node* getSceneGraphPriority() const { return _node; }; + std::function _onEvent; /// Event callback function - Type _type; /// Event type - bool _isRegistered; /// Whether the listener has been added to dispatcher. + +protected: + + Type _type; /// Event listener type + ListenerID _listenerID; /// Event listener ID + bool _isRegistered; /// Whether the listener has been added to dispatcher. // The priority of event listener int _fixedPriority; // The higher the number, the higher the priority, 0 is for scene graph base priority. Node* _node; // scene graph based priority bool _paused; - -private: - friend class EventDispatcher; }; NS_CC_END diff --git a/cocos/2d/CCEventListenerAcceleration.cpp b/cocos/2d/CCEventListenerAcceleration.cpp index d1d5d22206..258af5929e 100644 --- a/cocos/2d/CCEventListenerAcceleration.cpp +++ b/cocos/2d/CCEventListenerAcceleration.cpp @@ -59,7 +59,7 @@ bool EventListenerAcceleration::init(std::functiononAccelerationEvent(&accEvent->_acc, event); }; - if (EventListener::init(TYPE_ACCELERATION, listener)) + if (EventListener::init(Type::ACCELERATION, static_cast(Type::ACCELERATION), listener)) { onAccelerationEvent = callback; return true; diff --git a/cocos/2d/CCEventListenerAcceleration.h b/cocos/2d/CCEventListenerAcceleration.h index 59ce3d8439..213bac646e 100644 --- a/cocos/2d/CCEventListenerAcceleration.h +++ b/cocos/2d/CCEventListenerAcceleration.h @@ -33,6 +33,8 @@ NS_CC_BEGIN class EventListenerAcceleration : public EventListener { public: + static const ListenerID ID = static_cast(Type::ACCELERATION); + static EventListenerAcceleration* create(std::function callback); virtual ~EventListenerAcceleration(); diff --git a/cocos/2d/CCEventListenerCustom.cpp b/cocos/2d/CCEventListenerCustom.cpp index 048fede198..09e657a9fc 100644 --- a/cocos/2d/CCEventListenerCustom.cpp +++ b/cocos/2d/CCEventListenerCustom.cpp @@ -46,7 +46,7 @@ EventListenerCustom* EventListenerCustom::create(const std::string& eventName, s return ret; } -bool EventListenerCustom::init(Type type, std::functioncallback) +bool EventListenerCustom::init(ListenerID listenerId, std::functioncallback) { bool ret = false; @@ -59,7 +59,7 @@ bool EventListenerCustom::init(Type type, std::functioncallb } }; - if (EventListener::init(type, listener)) + if (EventListener::init(EventListener::Type::CUSTOM, listenerId, listener)) { ret = true; } @@ -69,7 +69,7 @@ bool EventListenerCustom::init(Type type, std::functioncallb EventListenerCustom* EventListenerCustom::clone() { EventListenerCustom* ret = new EventListenerCustom(); - if (ret && ret->init(_type, _onCustomEvent)) + if (ret && ret->init(_listenerID, _onCustomEvent)) { ret->autorelease(); } diff --git a/cocos/2d/CCEventListenerCustom.h b/cocos/2d/CCEventListenerCustom.h index 0bd5bec700..eb7e875165 100644 --- a/cocos/2d/CCEventListenerCustom.h +++ b/cocos/2d/CCEventListenerCustom.h @@ -67,7 +67,7 @@ protected: EventListenerCustom(); /** Initializes event with type and callback function */ - bool init(Type type, std::function callback); + bool init(ListenerID listenerId, std::function callback); std::function _onCustomEvent; }; diff --git a/cocos/2d/CCEventListenerKeyboard.cpp b/cocos/2d/CCEventListenerKeyboard.cpp index fb733ed442..c947a43e36 100644 --- a/cocos/2d/CCEventListenerKeyboard.cpp +++ b/cocos/2d/CCEventListenerKeyboard.cpp @@ -88,7 +88,7 @@ bool EventListenerKeyboard::init() } }; - if (EventListener::init(TYPE_KEYBOARD, listener)) + if (EventListener::init(Type::KEYBOARD, static_cast(Type::KEYBOARD), listener)) { return true; } diff --git a/cocos/2d/CCEventListenerKeyboard.h b/cocos/2d/CCEventListenerKeyboard.h index c34d2add0d..6c59f69121 100644 --- a/cocos/2d/CCEventListenerKeyboard.h +++ b/cocos/2d/CCEventListenerKeyboard.h @@ -36,6 +36,8 @@ class Event; class EventListenerKeyboard : public EventListener { public: + static const ListenerID ID = static_cast(Type::KEYBOARD); + static EventListenerKeyboard* create(); /// Overrides diff --git a/cocos/2d/CCEventListenerTouch.cpp b/cocos/2d/CCEventListenerTouch.cpp index e753a113d0..1367645452 100644 --- a/cocos/2d/CCEventListenerTouch.cpp +++ b/cocos/2d/CCEventListenerTouch.cpp @@ -46,7 +46,7 @@ EventListenerTouchOneByOne::~EventListenerTouchOneByOne() bool EventListenerTouchOneByOne::init() { - if (EventListener::init(TYPE_TOUCH_ONE_BY_ONE, nullptr)) + if (EventListener::init(Type::TOUCH_ONE_BY_ONE, static_cast(Type::TOUCH_ONE_BY_ONE), nullptr)) { return true; } @@ -123,7 +123,7 @@ EventListenerTouchAllAtOnce::~EventListenerTouchAllAtOnce() bool EventListenerTouchAllAtOnce::init() { - if (EventListener::init(TYPE_TOUCH_ALL_AT_ONCE, nullptr)) + if (EventListener::init(Type::TOUCH_ALL_AT_ONCE, static_cast(Type::TOUCH_ALL_AT_ONCE), nullptr)) { return true; } diff --git a/cocos/2d/CCEventListenerTouch.h b/cocos/2d/CCEventListenerTouch.h index 09e6e6fe0c..94b0bd65b7 100644 --- a/cocos/2d/CCEventListenerTouch.h +++ b/cocos/2d/CCEventListenerTouch.h @@ -36,6 +36,8 @@ NS_CC_BEGIN class EventListenerTouchOneByOne : public EventListener { public: + static const ListenerID ID = static_cast(Type::TOUCH_ONE_BY_ONE); + static EventListenerTouchOneByOne* create(); virtual ~EventListenerTouchOneByOne(); @@ -67,6 +69,8 @@ private: class EventListenerTouchAllAtOnce : public EventListener { public: + static const ListenerID ID = static_cast(Type::TOUCH_ALL_AT_ONCE); + static EventListenerTouchAllAtOnce* create(); virtual ~EventListenerTouchAllAtOnce(); diff --git a/cocos/2d/CCEventTouch.cpp b/cocos/2d/CCEventTouch.cpp index d0df614973..9cfa0edd8d 100644 --- a/cocos/2d/CCEventTouch.cpp +++ b/cocos/2d/CCEventTouch.cpp @@ -26,5 +26,10 @@ NS_CC_BEGIN +EventTouch::EventTouch() +: Event(Type::TOUCH) +{ + _touches.reserve(MAX_TOUCHES); +} NS_CC_END diff --git a/cocos/2d/CCEventTouch.h b/cocos/2d/CCEventTouch.h index 100d67cf95..3bc18f55ca 100644 --- a/cocos/2d/CCEventTouch.h +++ b/cocos/2d/CCEventTouch.h @@ -46,11 +46,7 @@ public: CANCELLED }; - EventTouch() - : Event(TYPE_TOUCH) - { - _touches.reserve(MAX_TOUCHES); - } + EventTouch(); EventCode getEventCode() { return _eventCode; }; std::vector getTouches() { return _touches; }; From a82932524068dbc3caf1fa4cf55d06d267599dc2 Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 28 Oct 2013 16:00:19 +0800 Subject: [PATCH 35/42] issue #3069: Updating EventDispatcherTest. --- .../Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp index 1e7c746a56..8aa31bb82f 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp @@ -187,7 +187,7 @@ void TouchableSpriteTest::onEnter() auto senderItem = static_cast(sender); senderItem->setString("Only Next item could be clicked"); - _eventDispatcher->removeEventListeners(EventListener::TYPE_TOUCH_ONE_BY_ONE); + _eventDispatcher->removeEventListeners(EventListenerTouchOneByOne::ID); auto nextItem = MenuItemFont::create("Next", [=](Object* sender){ nextCallback(nullptr); @@ -664,5 +664,5 @@ std::string RemoveAndRetainNodeTest::title() std::string RemoveAndRetainNodeTest::subtitle() { - return ""; + return "Sprite should be removed after 5s, add to scene again after 5s"; } \ No newline at end of file From 296978a9bee8ac85e8d2e8512c3d9781bedfe53c Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 29 Oct 2013 14:57:16 +0800 Subject: [PATCH 36/42] issue #3069: Separate 'EventDispatcher::removeEventListeners' to 'removeEventListeners' and 'removeCustomEventListeners'. --- cocos/2d/CCEventDispatcher.cpp | 16 ++++++++++++++-- cocos/2d/CCEventDispatcher.h | 25 ++++++++++++++++--------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index 1ebadf9b74..8d590c033d 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -993,7 +993,7 @@ EventDispatcher::EventListenerVector* EventDispatcher::getListeners(EventListene return nullptr; } -void EventDispatcher::removeEventListeners(EventListener::ListenerID listenerID) +void EventDispatcher::removeEventListenersForListenerID(EventListener::ListenerID listenerID) { auto listenerItemIter = _listeners.find(listenerID); if (listenerItemIter != _listeners.end()) @@ -1040,6 +1040,18 @@ void EventDispatcher::removeEventListeners(EventListener::ListenerID listenerID) } } +void EventDispatcher::removeEventListeners(EventListener::Type listenerType) +{ + CCASSERT(listenerType != EventListener::Type::CUSTOM, "Not support custom event listener type, please use EventDispatcher::removeCustomEventListeners instead."); + + removeEventListenersForListenerID(static_cast(listenerType)); +} + +void EventDispatcher::removeCustomEventListeners(const std::string& customEventName) +{ + removeEventListenersForListenerID(std::hash()(customEventName)); +} + void EventDispatcher::removeAllEventListeners() { std::vector types(_listeners.size()); @@ -1051,7 +1063,7 @@ void EventDispatcher::removeAllEventListeners() for (auto& type : types) { - removeEventListeners(type); + removeEventListenersForListenerID(type); } if (!_inDispatch) diff --git a/cocos/2d/CCEventDispatcher.h b/cocos/2d/CCEventDispatcher.h index 3609c7a591..b8b1aa8efb 100644 --- a/cocos/2d/CCEventDispatcher.h +++ b/cocos/2d/CCEventDispatcher.h @@ -74,8 +74,11 @@ public: */ void removeEventListener(EventListener* listener); - /** Removes all listeners with the same event listener ID */ - void removeEventListeners(EventListener::ListenerID listenerID); + /** Removes all listeners with the same event listener type */ + void removeEventListeners(EventListener::Type listenerType); + + /** Removes all custom listeners with the same event name */ + void removeCustomEventListeners(const std::string& customEventName); /** Removes all listeners */ void removeAllEventListeners(); @@ -95,6 +98,14 @@ public: */ void dispatchEvent(Event* event); + /** Constructor of EventDispatcher */ + EventDispatcher(); + /** Destructor of EventDispatcher */ + ~EventDispatcher(); + +private: + friend class Node; + /** Sets the dirty flag for a node. */ void setDirtyForNode(Node* node); @@ -107,13 +118,6 @@ public: /** Notifys event dispatcher that the node has been deleted. */ void cleanTarget(Node* node); - /** Constructor of EventDispatcher */ - EventDispatcher(); - /** Destructor of EventDispatcher */ - ~EventDispatcher(); - -private: - /** * The vector to store event listeners with scene graph based priority and fixed priority. */ @@ -149,6 +153,9 @@ private: /** Update dirty flag */ void updateDirtyFlagForSceneGraph(); + /** Removes all listeners with the same event listener ID */ + void removeEventListenersForListenerID(EventListener::ListenerID listenerID); + /** Sort event listener */ void sortEventListeners(EventListener::ListenerID listenerID); From 5698dcd0e18788a00b9fb6510224f310cbfb856e Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 29 Oct 2013 14:58:47 +0800 Subject: [PATCH 37/42] issue #3069: Protecting some methods in EventListener, make EventDispatcher as its friend class. --- cocos/2d/CCEventListener.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cocos/2d/CCEventListener.h b/cocos/2d/CCEventListener.h index bbf5121149..ea4734f7a3 100644 --- a/cocos/2d/CCEventListener.h +++ b/cocos/2d/CCEventListener.h @@ -74,6 +74,7 @@ public: /** Clones the listener, its subclasses have to override this method. */ virtual EventListener* clone() = 0; +protected: inline void setPaused(bool paused) { _paused = paused; }; inline bool isPaused() const { return _paused; }; @@ -91,16 +92,16 @@ public: std::function _onEvent; /// Event callback function -protected: - Type _type; /// Event listener type ListenerID _listenerID; /// Event listener ID bool _isRegistered; /// Whether the listener has been added to dispatcher. // The priority of event listener int _fixedPriority; // The higher the number, the higher the priority, 0 is for scene graph base priority. - Node* _node; // scene graph based priority - bool _paused; + Node* _node; // scene graph based priority + bool _paused; // Whether the listener is paused + + friend class EventDispatcher; }; NS_CC_END From cb7a762584dd9fcc800ad31ee616bd6b3eff5f06 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 29 Oct 2013 14:59:05 +0800 Subject: [PATCH 38/42] issue #3069: Updating EventDispatcherTest. --- .../Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp index 8aa31bb82f..bbc041c70b 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp @@ -187,7 +187,7 @@ void TouchableSpriteTest::onEnter() auto senderItem = static_cast(sender); senderItem->setString("Only Next item could be clicked"); - _eventDispatcher->removeEventListeners(EventListenerTouchOneByOne::ID); + _eventDispatcher->removeEventListeners(EventListener::Type::TOUCH_ONE_BY_ONE); auto nextItem = MenuItemFont::create("Next", [=](Object* sender){ nextCallback(nullptr); From 01579e12b0e87a9ae4affa12a376eb60bfed7e0e Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 29 Oct 2013 15:01:39 +0800 Subject: [PATCH 39/42] issue #3069: Remove unused ID. --- cocos/2d/CCEventListenerAcceleration.h | 2 -- cocos/2d/CCEventListenerKeyboard.h | 2 -- cocos/2d/CCEventListenerTouch.h | 2 -- 3 files changed, 6 deletions(-) diff --git a/cocos/2d/CCEventListenerAcceleration.h b/cocos/2d/CCEventListenerAcceleration.h index 213bac646e..59ce3d8439 100644 --- a/cocos/2d/CCEventListenerAcceleration.h +++ b/cocos/2d/CCEventListenerAcceleration.h @@ -33,8 +33,6 @@ NS_CC_BEGIN class EventListenerAcceleration : public EventListener { public: - static const ListenerID ID = static_cast(Type::ACCELERATION); - static EventListenerAcceleration* create(std::function callback); virtual ~EventListenerAcceleration(); diff --git a/cocos/2d/CCEventListenerKeyboard.h b/cocos/2d/CCEventListenerKeyboard.h index 6c59f69121..c34d2add0d 100644 --- a/cocos/2d/CCEventListenerKeyboard.h +++ b/cocos/2d/CCEventListenerKeyboard.h @@ -36,8 +36,6 @@ class Event; class EventListenerKeyboard : public EventListener { public: - static const ListenerID ID = static_cast(Type::KEYBOARD); - static EventListenerKeyboard* create(); /// Overrides diff --git a/cocos/2d/CCEventListenerTouch.h b/cocos/2d/CCEventListenerTouch.h index 94b0bd65b7..80ff176263 100644 --- a/cocos/2d/CCEventListenerTouch.h +++ b/cocos/2d/CCEventListenerTouch.h @@ -36,8 +36,6 @@ NS_CC_BEGIN class EventListenerTouchOneByOne : public EventListener { public: - static const ListenerID ID = static_cast(Type::TOUCH_ONE_BY_ONE); - static EventListenerTouchOneByOne* create(); virtual ~EventListenerTouchOneByOne(); From fbd04a476ab9e151e13627d6da6f906e5f89d469 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 29 Oct 2013 15:02:26 +0800 Subject: [PATCH 40/42] issue #3069: Updating comments in CCEventListenerCustom.h. --- cocos/2d/CCEventListenerCustom.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cocos/2d/CCEventListenerCustom.h b/cocos/2d/CCEventListenerCustom.h index eb7e875165..e2e750ce56 100644 --- a/cocos/2d/CCEventListenerCustom.h +++ b/cocos/2d/CCEventListenerCustom.h @@ -33,21 +33,21 @@ class EventCustom; /** * Usage: - * auto dispatcher = EventDispatcher::getInstance(); + * auto dispatcher = Director::getInstance()->getEventDispatcher(); * Adds a listener: * - * auto callback = [](CustomEvent* event){ do_some_thing(); }; - * auto listener = CustomEventListener::create(callback); + * auto callback = [](EventCustom* event){ do_some_thing(); }; + * auto listener = EventListenerCustom::create(callback); * dispatcher->addEventListenerWithSceneGraphPriority(listener, one_node); * * Dispatchs a custom event: * - * Event event("your_event_type"); + * EventCustom event("your_event_type"); * dispatcher->dispatchEvent(&event); * * Removes a listener * - * dispatcher->removeListener(listener); + * dispatcher->removeEventListener(listener); */ class EventListenerCustom : public EventListener { From bb568e84e83d9c3b6cae993db4a8dfe827547417 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 29 Oct 2013 15:09:13 +0800 Subject: [PATCH 41/42] Updating auto generated binding codes. --- cocos/scripting/auto-generated | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/scripting/auto-generated b/cocos/scripting/auto-generated index 2f3c531665..4fd4e14912 160000 --- a/cocos/scripting/auto-generated +++ b/cocos/scripting/auto-generated @@ -1 +1 @@ -Subproject commit 2f3c5316657e64ec38b8ed3ea6826eb48c46f32c +Subproject commit 4fd4e14912165a2c8a5a6faacda2626035af6f36 From 91932ea9063b56c295edfac9c486d55aed726a4b Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 29 Oct 2013 15:36:43 +0800 Subject: [PATCH 42/42] issue #3069: Fixing compilation errors. --- cocos/2d/platform/android/nativeactivity.cpp | 14 ++++++++------ cocos/2d/platform/linux/CCEGLView.cpp | 3 ++- cocos/2d/platform/win32/CCEGLView.cpp | 3 ++- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/cocos/2d/platform/android/nativeactivity.cpp b/cocos/2d/platform/android/nativeactivity.cpp index fa4fb434d4..5ac17a5ee5 100644 --- a/cocos/2d/platform/android/nativeactivity.cpp +++ b/cocos/2d/platform/android/nativeactivity.cpp @@ -422,18 +422,20 @@ static int32_t handle_key_input(AInputEvent *event) { if (AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_UP) { + auto dispatcher = cocos2d::Director::getInstance()->getEventDispatcher(); + switch (AKeyEvent_getKeyCode(event)) { case AKEYCODE_BACK: { cocos2d::EventKeyboard event(cocos2d::EventKeyboard::KeyCode::KEY_BACKSPACE, false); - cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event); + dispatcher->dispatchEvent(&event); } return 1; case AKEYCODE_MENU: { cocos2d::EventKeyboard event(cocos2d::EventKeyboard::KeyCode::KEY_MENU, false); - cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event); + dispatcher->dispatchEvent(&event); } return 1; default: @@ -629,8 +631,8 @@ void android_main(struct android_app* state) { acc.z = event.acceleration.z/10; acc.timestamp = 0; cocos2d::EventAcceleration accEvent(acc); - - cocos2d::EventDispatcher::getInstance()->dispatchEvent(&accEvent); + auto dispatcher = cocos2d::Director::getInstance()->getEventDispatcher(); + dispatcher->dispatchEvent(&accEvent); } else { // ACONFIGURATION_ORIENTATION_LAND // swap x and y parameters @@ -640,8 +642,8 @@ void android_main(struct android_app* state) { acc.z = event.acceleration.z/10; acc.timestamp = 0; cocos2d::EventAcceleration accEvent(acc); - - cocos2d::EventDispatcher::getInstance()->dispatchEvent(&accEvent); + auto dispatcher = cocos2d::Director::getInstance()->getEventDispatcher(); + dispatcher->dispatchEvent(&accEvent); } } } diff --git a/cocos/2d/platform/linux/CCEGLView.cpp b/cocos/2d/platform/linux/CCEGLView.cpp index 561fedcca8..172c9d796a 100644 --- a/cocos/2d/platform/linux/CCEGLView.cpp +++ b/cocos/2d/platform/linux/CCEGLView.cpp @@ -223,7 +223,8 @@ void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x, void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action); - EventDispatcher::getInstance()->dispatchEvent(&event); + auto dispatcher = Director::getInstance()->getEventDispatcher(); + dispatcher->dispatchEvent(&event); } void EGLViewEventHandler::OnGLFWCharCallback(GLFWwindow *window, unsigned int character) diff --git a/cocos/2d/platform/win32/CCEGLView.cpp b/cocos/2d/platform/win32/CCEGLView.cpp index d5b2ba0202..92bfbe5cf1 100644 --- a/cocos/2d/platform/win32/CCEGLView.cpp +++ b/cocos/2d/platform/win32/CCEGLView.cpp @@ -341,7 +341,8 @@ void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x, void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action); - EventDispatcher::getInstance()->dispatchEvent(&event); + auto dispatcher = Director::getInstance()->getEventDispatcher(); + dispatcher->dispatchEvent(&event); } void EGLViewEventHandler::OnGLFWCharCallback(GLFWwindow *window, unsigned int character)