From 997a5a36c180846a120e3897b559f6d9e8330c88 Mon Sep 17 00:00:00 2001 From: James Chen Date: Sat, 14 Sep 2013 09:02:49 +0800 Subject: [PATCH] [dispatcher] Renaming 'registerEventListenerXXX' to 'addEventListenerXXX'. --- cocos2dx/base_nodes/CCNode.cpp | 13 ++-- cocos2dx/base_nodes/CCNode.h | 7 +- .../event_dispatcher/CCEventDispatcher.cpp | 63 +++++++++-------- cocos2dx/event_dispatcher/CCEventDispatcher.h | 29 ++++---- .../CCLayer.cpp | 69 ++++++++----------- .../layers_scenes_transitions_nodes/CCLayer.h | 8 +-- .../CocoStudio/Components/CCInputDelegate.cpp | 36 ++++++---- .../CocoStudio/Components/CCInputDelegate.h | 6 +- .../Cpp/HelloCpp/Classes/HelloWorldScene.cpp | 14 ++-- .../Classes/Box2DTestBed/Box2dView.cpp | 9 ++- .../TestCpp/Classes/Box2DTestBed/Box2dView.h | 1 - .../CocosDenshionTest/CocosDenshionTest.cpp | 2 +- .../CocoStudioArmatureTest/ArmatureScene.cpp | 6 +- .../Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp | 6 +- .../NewEventDispatcherTest.cpp | 6 +- .../PerformanceTouchesTest.cpp | 9 +-- .../Classes/TextInputTest/TextInputTest.cpp | 2 +- .../TestCpp/Classes/TouchesTest/Paddle.cpp | 5 +- samples/Cpp/TestCpp/Classes/controller.cpp | 2 +- .../cocos2d_specifics.cpp.REMOVED.git-id | 2 +- .../javascript/bindings/cocos2d_specifics.hpp | 2 +- 21 files changed, 138 insertions(+), 159 deletions(-) diff --git a/cocos2dx/base_nodes/CCNode.cpp b/cocos2dx/base_nodes/CCNode.cpp index c4e499ed16..bdc65afa1f 100644 --- a/cocos2dx/base_nodes/CCNode.cpp +++ b/cocos2dx/base_nodes/CCNode.cpp @@ -176,9 +176,9 @@ Node::~Node() CC_SAFE_DELETE(_componentContainer); - for (auto iter = _eventIds.begin(); iter != _eventIds.end(); ++iter) + for (auto iter = _eventlisteners.begin(); iter != _eventlisteners.end(); ++iter) { - EventDispatcher::getInstance()->unregisterEventListener(*iter); + EventDispatcher::getInstance()->removeEventListener(*iter); } } @@ -1301,14 +1301,9 @@ void Node::updateEventPriorityIndex() _eventPriority = ++_globalEventPriorityIndex; } -void Node::addEventId(int eventId) +void Node::associateEventListener(EventListener* listener) { - _eventIds.insert(eventId); -} - -void Node::removeEventId(int eventId) -{ - _eventIds.erase(eventId); + _eventlisteners.insert(listener); } // NodeRGBA diff --git a/cocos2dx/base_nodes/CCNode.h b/cocos2dx/base_nodes/CCNode.h index 469ecc391c..034845672e 100644 --- a/cocos2dx/base_nodes/CCNode.h +++ b/cocos2dx/base_nodes/CCNode.h @@ -76,6 +76,8 @@ bool nodeComparisonLess(const RCPtr& pp1, const RCPtr& pp2); bool nodeComparisonLess(Object* p1, Object* p2); #endif +class EventListener; + /** @brief Node is the main element. Anything that gets drawn or contains things that get drawn is a Node. The most popular Nodes are: Scene, Layer, Sprite, Menu. @@ -1372,11 +1374,10 @@ private: friend class Director; friend class EventDispatcher; - void addEventId(int eventId); - void removeEventId(int eventId); + void associateEventListener(EventListener* listener); static void resetEventPriorityIndex(); - std::set _eventIds; + std::set _eventlisteners; protected: diff --git a/cocos2dx/event_dispatcher/CCEventDispatcher.cpp b/cocos2dx/event_dispatcher/CCEventDispatcher.cpp index 3812a03880..391f66f02c 100644 --- a/cocos2dx/event_dispatcher/CCEventDispatcher.cpp +++ b/cocos2dx/event_dispatcher/CCEventDispatcher.cpp @@ -51,8 +51,6 @@ private: int& _count; }; -int _eventId = 0; - } NS_CC_BEGIN @@ -94,15 +92,14 @@ void EventDispatcher::registerEventListenerWithItem(EventListenerItem* item) listenerList->push_front(item); } -int EventDispatcher::registerEventListenerWithSceneGraphPriority(EventListener* listener, Node* node) +void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node) { CCASSERT(!listener->_isRegistered, "The listener has been registered."); if (!listener->checkAvaiable()) - return 0; - + return; + EventListenerItem* item = new EventListenerItem(); - item->id = ++_eventId; item->node = node; item->fixedPriority = 0; item->listener = listener; @@ -112,20 +109,17 @@ int EventDispatcher::registerEventListenerWithSceneGraphPriority(EventListener* registerEventListenerWithItem(item); _eventNodes.push_back(node); - node->addEventId(item->id); - - return item->id; + node->associateEventListener(listener); } -int EventDispatcher::registerEventListenerWithFixedPriority(EventListener* listener, int fixedPriority) +void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority) { CCASSERT(!listener->_isRegistered, "The listener has been registered."); if (!listener->checkAvaiable()) - return 0; - + return; + EventListenerItem* item = new EventListenerItem(); - item->id = ++_eventId; item->node = nullptr; item->fixedPriority = fixedPriority; item->listener = listener; @@ -133,29 +127,29 @@ int EventDispatcher::registerEventListenerWithFixedPriority(EventListener* liste item->listener->_isRegistered = true; registerEventListenerWithItem(item); - - return item->id; } -void EventDispatcher::unregisterEventListener(int listenerId) +void EventDispatcher::removeEventListener(EventListener* listener) { - if (_listeners == nullptr || listenerId <= 0) + if (_listeners == nullptr || listener == nullptr) return; for (auto iter = _listeners->begin(); iter != _listeners->end();) { for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter) { - if ((*itemIter)->id == listenerId) + if ((*itemIter)->listener == listener) { - (*itemIter)->id = 0; - if (_inDispatch == 0) { (*itemIter)->listener->release(); delete (*itemIter); iter->second->remove(*itemIter); } + else + { + (*itemIter)->listener = nullptr; + } break; } @@ -179,9 +173,9 @@ void EventDispatcher::unregisterEventListener(int listenerId) } } -void EventDispatcher::setPriorityWithSceneGraph(int listenerId, Node* node) +void EventDispatcher::setPriorityWithSceneGraph(EventListener* listener, Node* node) { - if (_listeners == nullptr || listenerId <= 0) + if (_listeners == nullptr || listener == nullptr || node == nullptr) return; for (auto iter = _listeners->begin(); iter != _listeners->end(); ++iter) @@ -189,7 +183,7 @@ void EventDispatcher::setPriorityWithSceneGraph(int listenerId, Node* node) for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter) { auto item = *itemIter; - if (item->id == listenerId) + if (item->listener == listener) { // FIXME: fixed priority --> scene graph's priority. item->fixedPriority = 0; @@ -200,9 +194,9 @@ void EventDispatcher::setPriorityWithSceneGraph(int listenerId, Node* node) } } -void EventDispatcher::setPriorityWithFixedValue(int listenerId, int fixedPriority) +void EventDispatcher::setPriorityWithFixedValue(EventListener* listener, int fixedPriority) { - if (_listeners == nullptr || listenerId <= 0) + if (_listeners == nullptr || listener == nullptr) return; for (auto iter = _listeners->begin(); iter != _listeners->end(); ++iter) @@ -210,7 +204,7 @@ void EventDispatcher::setPriorityWithFixedValue(int listenerId, int fixedPriorit for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter) { auto item = *itemIter; - if (item->id == listenerId) + if (item->listener == listener) { // FIXME: scene graph's priority --> fixed priority. item->fixedPriority = fixedPriority; @@ -308,7 +302,7 @@ void EventDispatcher::dispatchTouchEvent(TouchEvent* event) for (; oneByOneIter != oneByOnelisteners.end(); ++oneByOneIter) { // Skip if the listener was removed. - if ((*oneByOneIter)->id == 0) + if ((*oneByOneIter)->listener == nullptr) continue; event->setCurrentTarget((*oneByOneIter)->node); @@ -396,7 +390,7 @@ void EventDispatcher::dispatchTouchEvent(TouchEvent* event) for (auto allInOneIter = allInOnelisteners.begin(); allInOneIter != allInOnelisteners.end(); ++allInOneIter) { // Skip if the listener was removed. - if ((*allInOneIter)->id == 0) + if ((*allInOneIter)->listener == nullptr) continue; event->setCurrentTarget((*allInOneIter)->node); @@ -455,7 +449,7 @@ void EventDispatcher::removeUnregisteredListeners() while ( listenerItemIter != _listeners->end()) { auto removeIterBegin = std::remove_if(listenerItemIter->second->begin(), listenerItemIter->second->end(), [](const EventListenerItem* item){ - return item->id == 0; + return item->listener == nullptr; }); for (auto iter = removeIterBegin; iter != listenerItemIter->second->end(); ++iter) @@ -577,4 +571,15 @@ void EventDispatcher::removeAllListeners() _listeners = nullptr; } +void EventDispatcher::setEnabled(bool isEnabled) +{ + _isEnabled = isEnabled; +} + + +bool EventDispatcher::isEnabled() const +{ + return _isEnabled; +} + NS_CC_END diff --git a/cocos2dx/event_dispatcher/CCEventDispatcher.h b/cocos2dx/event_dispatcher/CCEventDispatcher.h index 08be317ce6..0fa51c6614 100644 --- a/cocos2dx/event_dispatcher/CCEventDispatcher.h +++ b/cocos2dx/event_dispatcher/CCEventDispatcher.h @@ -55,41 +55,39 @@ public: static EventDispatcher* getInstance(); /** Registers a callback function for an specified event with the priority of scene graph. - * @return The unique ID for the listener. */ - int registerEventListenerWithSceneGraphPriority(EventListener* listener, Node* node); + void addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node); /** Registers a callback function for an specified event with the fixed priority. - * @return The unique ID for the listener. */ - int registerEventListenerWithFixedPriority(EventListener* listener, int fixedPriority); + void addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority); /** Unregisters a callback function by the unique ID. */ - void unregisterEventListener(int listenerId); + void removeEventListener(EventListener* listener); + /** Removes listeners by event type */ + void removeListenersForEventType(const std::string& eventType); + + /** Removes all listeners */ + void removeAllListeners(); + /** Sets listener's priority with node's draw order. */ - void setPriorityWithSceneGraph(int listenerId, Node* node); + void setPriorityWithSceneGraph(EventListener* listener, Node* node); /** Sets listener's priority with fixed value. */ - void setPriorityWithFixedValue(int listenerId, int fixedPriority); + void setPriorityWithFixedValue(EventListener* listener, int fixedPriority); /** Whether to enable dispatching events */ - void setEnabled(bool isEnabled) { _isEnabled = isEnabled; }; + void setEnabled(bool isEnabled); /** Checks whether dispatching events is enabled */ - bool isEnabled() const { return _isEnabled; }; + bool isEnabled() const; /** Dispatches the event * Also removes all EventListeners marked for deletion from the * event dispatcher list. */ void dispatchEvent(Event* event); - - /** Removes listeners by event type */ - void removeListenersForEventType(const std::string& eventType); - - /** Removes all listeners */ - void removeAllListeners(); public: /** Destructor of EventDispatcher */ @@ -98,7 +96,6 @@ public: private: struct EventListenerItem { - int id; int fixedPriority; // The higher the number, the higher the priority Node* node; // Weak reference. EventListener* listener; diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index 90577ff4bd..81038d3193 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -52,9 +52,9 @@ Layer::Layer() , _keyboardEnabled(false) , _touchMode(Touch::DispatchMode::ALL_AT_ONCE) , _swallowsTouches(true) -, _touchListenerId(0) -, _keyboardListenerId(0) -, _accelerationId(0) +, _touchListener(nullptr) +, _keyboardListener(nullptr) +, _accelerationListener(nullptr) { _ignoreAnchorPointForPosition = true; setAnchorPoint(Point(0.5f, 0.5f)); @@ -97,22 +97,9 @@ Layer *Layer::create() } /// Touch and Accelerometer related -// -//void Layer::registerWithTouchDispatcher() -//{ -// TouchDispatcher* pDispatcher = Director::getInstance()->getTouchDispatcher(); -// -// if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE ) { -// pDispatcher->addStandardDelegate(this, 0); -// } else { -// pDispatcher->addTargetedDelegate(this, _touchPriority, _swallowsTouches); -// } -//} void Layer::onRegisterTouchListener() -{ - EventDispatcher::getInstance()->unregisterEventListener(_touchListenerId); - +{ if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE ) { // Register Touch Event auto listener = TouchEventListener::create(Touch::DispatchMode::ALL_AT_ONCE); @@ -122,7 +109,8 @@ void Layer::onRegisterTouchListener() listener->onTouchesEnded = CC_CALLBACK_2(Layer::onTouchesEnded, this); listener->onTouchesCancelled = CC_CALLBACK_2(Layer::onTouchesCancelled, this); - _touchListenerId = EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _touchListener = listener; } else { // Register Touch Event auto listener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE); @@ -133,7 +121,8 @@ void Layer::onRegisterTouchListener() listener->onTouchEnded = CC_CALLBACK_2(Layer::onTouchEnded, this); listener->onTouchCancelled = CC_CALLBACK_2(Layer::onTouchCancelled, this); - _touchListenerId = EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _touchListener = listener; } } @@ -182,10 +171,8 @@ void Layer::setTouchEnabled(bool enabled) } else { - // have problems? -// Director::getInstance()->getTouchDispatcher()->removeDelegate(this); - EventDispatcher::getInstance()->unregisterEventListener(_touchListenerId); - _touchListenerId = 0; + EventDispatcher::getInstance()->removeEventListener(_touchListener); + _touchListener = nullptr; } } } @@ -248,12 +235,13 @@ void Layer::setAccelerometerEnabled(bool enabled) if (_running) { auto dispatcher = EventDispatcher::getInstance(); - dispatcher->unregisterEventListener(_accelerationId); + dispatcher->removeEventListener(_accelerationListener); + _accelerationListener = nullptr; if (enabled) { - auto listener = AccelerationEventListener::create(CC_CALLBACK_2(Layer::onAcceleration, this)); - dispatcher->registerEventListenerWithSceneGraphPriority(listener, this); + _accelerationListener = AccelerationEventListener::create(CC_CALLBACK_2(Layer::onAcceleration, this)); + dispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this); } } } @@ -295,8 +283,9 @@ void Layer::setKeyboardEnabled(bool enabled) { _keyboardEnabled = enabled; - EventDispatcher::getInstance()->unregisterEventListener(_keyboardListenerId); - _keyboardListenerId = 0; + auto dispatcher = EventDispatcher::getInstance(); + dispatcher->removeEventListener(_keyboardListener); + _keyboardListener = nullptr; if (enabled) { @@ -304,7 +293,8 @@ void Layer::setKeyboardEnabled(bool enabled) listener->onKeyPressed = CC_CALLBACK_2(Layer::onKeyPressed, this); listener->onKeyReleased = CC_CALLBACK_2(Layer::onKeyReleased, this); - _keyboardListenerId = EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); + _keyboardListener = listener; } } } @@ -326,9 +316,9 @@ void Layer::onEnter() if (_accelerometerEnabled) { auto dispatcher = EventDispatcher::getInstance(); - dispatcher->unregisterEventListener(_accelerationId); + dispatcher->removeEventListener(_accelerationListener); auto listener = AccelerationEventListener::create(CC_CALLBACK_2(Layer::onAcceleration, this)); - dispatcher->registerEventListenerWithSceneGraphPriority(listener, this); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); } } @@ -336,16 +326,16 @@ void Layer::onExit() { auto dispatcher = EventDispatcher::getInstance(); - dispatcher->unregisterEventListener(_touchListenerId); - _touchListenerId = 0; + dispatcher->removeEventListener(_touchListener); + _touchListener = nullptr; // remove this layer from the delegates who concern Accelerometer Sensor - dispatcher->unregisterEventListener(_accelerationId); - _accelerationId = 0; + dispatcher->removeEventListener(_accelerationListener); + _accelerationListener = nullptr; // remove this layer from the delegates who concern the keypad msg - dispatcher->unregisterEventListener(_keyboardListenerId); - _keyboardListenerId = 0; + dispatcher->removeEventListener(_keyboardListener); + _keyboardListener = nullptr; Node::onExit(); } @@ -355,9 +345,8 @@ void Layer::onEnterTransitionDidFinish() if (_accelerometerEnabled) { auto dispatcher = EventDispatcher::getInstance(); - dispatcher->unregisterEventListener(_accelerationId); - auto listener = AccelerationEventListener::create(CC_CALLBACK_2(Layer::onAcceleration, this)); - dispatcher->registerEventListenerWithSceneGraphPriority(listener, this); + _accelerationListener = AccelerationEventListener::create(CC_CALLBACK_2(Layer::onAcceleration, this)); + dispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this); } Node::onEnterTransitionDidFinish(); diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h index c38f18f0dc..7bf5063037 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h @@ -120,8 +120,6 @@ public: virtual bool isTouchEnabled() const; virtual void setTouchEnabled(bool value); - int getTouchListenerId() const { return _touchListenerId; }; - virtual void setTouchMode(Touch::DispatchMode mode); virtual Touch::DispatchMode getTouchMode() const; @@ -182,9 +180,9 @@ protected: bool _touchEnabled; bool _accelerometerEnabled; bool _keyboardEnabled; - int _touchListenerId; - int _keyboardListenerId; - int _accelerationId; + EventListener* _touchListener; + EventListener* _keyboardListener; + EventListener* _accelerationListener; private: Touch::DispatchMode _touchMode; bool _swallowsTouches; diff --git a/extensions/CocoStudio/Components/CCInputDelegate.cpp b/extensions/CocoStudio/Components/CCInputDelegate.cpp index 836ed41fc3..1005e85c24 100644 --- a/extensions/CocoStudio/Components/CCInputDelegate.cpp +++ b/extensions/CocoStudio/Components/CCInputDelegate.cpp @@ -28,11 +28,11 @@ NS_CC_EXT_BEGIN InputDelegate::InputDelegate(void) : _touchEnabled(false) -, _touchListenerId(0) +, _touchListener(nullptr) , _accelerometerEnabled(false) -, _accelerometerListenerId(0) +, _accelerometerListener(nullptr) , _keypadEnabled(false) -, _keyboardListenerId(0) +, _keyboardListener(nullptr) , _touchPriority(0) , _touchMode(Touch::DispatchMode::ALL_AT_ONCE) { @@ -42,9 +42,9 @@ InputDelegate::InputDelegate(void) InputDelegate::~InputDelegate(void) { auto dispatcher = EventDispatcher::getInstance(); - dispatcher->unregisterEventListener(_touchListenerId); - dispatcher->unregisterEventListener(_keyboardListenerId); - dispatcher->unregisterEventListener(_accelerometerListenerId); + dispatcher->removeEventListener(_touchListener); + dispatcher->removeEventListener(_keyboardListener); + dispatcher->removeEventListener(_accelerometerListener); } bool InputDelegate::onTouchBegan(Touch *pTouch, Event *pEvent) @@ -107,9 +107,7 @@ void InputDelegate::setTouchEnabled(bool enabled) { _touchEnabled = enabled; if (enabled) - { - EventDispatcher::getInstance()->unregisterEventListener(_touchListenerId); - + { if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE ) { // Register Touch Event auto listener = TouchEventListener::create(Touch::DispatchMode::ALL_AT_ONCE); @@ -119,7 +117,8 @@ void InputDelegate::setTouchEnabled(bool enabled) listener->onTouchesEnded = CC_CALLBACK_2(InputDelegate::onTouchesEnded, this); listener->onTouchesCancelled = CC_CALLBACK_2(InputDelegate::onTouchesCancelled, this); - _touchListenerId = EventDispatcher::getInstance()->registerEventListenerWithFixedPriority(listener, 0); + EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 0); + _touchListener = listener; } else { // Register Touch Event auto listener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE); @@ -130,12 +129,13 @@ void InputDelegate::setTouchEnabled(bool enabled) listener->onTouchEnded = CC_CALLBACK_2(InputDelegate::onTouchEnded, this); listener->onTouchCancelled = CC_CALLBACK_2(InputDelegate::onTouchCancelled, this); - _touchListenerId = EventDispatcher::getInstance()->registerEventListenerWithFixedPriority(listener, _touchPriority); + EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, _touchPriority); + _touchListener = listener; } } else { - EventDispatcher::getInstance()->unregisterEventListener(_touchListenerId); + EventDispatcher::getInstance()->removeEventListener(_touchListener); } } } @@ -190,11 +190,14 @@ void InputDelegate::setAccelerometerEnabled(bool enabled) _accelerometerEnabled = enabled; auto dispatcher = EventDispatcher::getInstance(); - dispatcher->unregisterEventListener(_accelerometerListenerId); + dispatcher->removeEventListener(_accelerometerListener); + _accelerometerListener = nullptr; + if (enabled) { auto listener = AccelerationEventListener::create(CC_CALLBACK_2(InputDelegate::onAcceleration, this)); - _accelerometerListenerId = dispatcher->registerEventListenerWithFixedPriority(listener, 0); + dispatcher->addEventListenerWithFixedPriority(listener, 0); + _accelerometerListener = listener; } } } @@ -210,13 +213,16 @@ void InputDelegate::setKeypadEnabled(bool enabled) { _keypadEnabled = enabled; + EventDispatcher::getInstance()->removeEventListener(_keyboardListener); + if (enabled) { auto listener = KeyboardEventListener::create(); listener->onKeyPressed = CC_CALLBACK_2(InputDelegate::onKeyPressed, this); listener->onKeyReleased = CC_CALLBACK_2(InputDelegate::onKeyReleased, this); - _keyboardListenerId = EventDispatcher::getInstance()->registerEventListenerWithFixedPriority(listener, 0); + EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 0); + _keyboardListener = listener; } } } diff --git a/extensions/CocoStudio/Components/CCInputDelegate.h b/extensions/CocoStudio/Components/CCInputDelegate.h index c39ef172c9..ec6744df55 100644 --- a/extensions/CocoStudio/Components/CCInputDelegate.h +++ b/extensions/CocoStudio/Components/CCInputDelegate.h @@ -124,11 +124,11 @@ public: protected: bool _touchEnabled; - int _touchListenerId; + EventListener* _touchListener; bool _accelerometerEnabled; - int _accelerometerListenerId; + EventListener* _accelerometerListener; bool _keypadEnabled; - int _keyboardListenerId; + EventListener* _keyboardListener; private: int _touchPriority; Touch::DispatchMode _touchMode; diff --git a/samples/Cpp/HelloCpp/Classes/HelloWorldScene.cpp b/samples/Cpp/HelloCpp/Classes/HelloWorldScene.cpp index 272d151eb1..140256d24b 100644 --- a/samples/Cpp/HelloCpp/Classes/HelloWorldScene.cpp +++ b/samples/Cpp/HelloCpp/Classes/HelloWorldScene.cpp @@ -98,21 +98,21 @@ bool HelloWorld::init() // dispatcher->removeAllListeners(); }; - - int layerTouchId = dispatcher->registerEventListenerWithSceneGraphPriority(layerTouchListener, this); - int layerTouchId2 = dispatcher->registerEventListenerWithSceneGraphPriority(layerTouchListener->clone(), this); + auto layerTouchListener2 = layerTouchListener->clone(); + dispatcher->addEventListenerWithSceneGraphPriority(layerTouchListener, this); + dispatcher->addEventListenerWithSceneGraphPriority(layerTouchListener2, this); auto spriteTouchListener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE); spriteTouchListener->setSwallowTouches(true); spriteTouchListener->onTouchBegan = [=](Touch* touch, Event* evt){ CCLOG("Touch sprite.... began... %d, drawOrder = %d", sprite->getZOrder(), sprite->getEventPriority()); - dispatcher->unregisterEventListener(layerTouchId); - dispatcher->unregisterEventListener(layerTouchId2); + dispatcher->removeEventListener(layerTouchListener); + dispatcher->removeEventListener(layerTouchListener2); return false; }; - dispatcher->registerEventListenerWithSceneGraphPriority(spriteTouchListener, sprite); + dispatcher->addEventListenerWithSceneGraphPriority(spriteTouchListener, sprite); for (int i = 0; i < 10; ++i) { @@ -160,7 +160,7 @@ bool HelloWorld::init() spriteItemTouchListener->setSwallowTouches(true); - dispatcher->registerEventListenerWithSceneGraphPriority(spriteItemTouchListener, sprite1); + dispatcher->addEventListenerWithSceneGraphPriority(spriteItemTouchListener, sprite1); // EventDispatcher::getInstance()->registerEventListener(TouchEvent::EVENT_TYPE, [=](Event* evt){ diff --git a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp index 92668b6d1a..c9e92f67dc 100644 --- a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp +++ b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp @@ -87,8 +87,8 @@ bool MenuLayer::initWithEntryID(int entryId) listener->onTouchBegan = CC_CALLBACK_2(MenuLayer::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(MenuLayer::onTouchMoved, this); - _touchListenerId = EventDispatcher::getInstance()->registerEventListenerWithFixedPriority(listener, 0); - + EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 0); + _touchListener = listener; return true; } @@ -185,8 +185,6 @@ bool Box2DView::initWithEntryID(int entryId) // Register Touch Event -// EventDispatcher::getInstance()->unregisterEventListener(this->getTouchEventId()); - auto listener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE); listener->setSwallowTouches(true); @@ -194,7 +192,8 @@ bool Box2DView::initWithEntryID(int entryId) listener->onTouchMoved = CC_CALLBACK_2(Box2DView::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(Box2DView::onTouchEnded, this); - _touchListenerId = EventDispatcher::getInstance()->registerEventListenerWithFixedPriority(listener, 10); + EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 10); + _touchListener = listener; return true; } diff --git a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.h b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.h index 30a51be3e4..5f3e785535 100644 --- a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.h +++ b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.h @@ -18,7 +18,6 @@ public: void nextCallback(Object* sender); void backCallback(Object* sender); -// virtual void registerWithTouchDispatcher(); virtual bool onTouchBegan(Touch* touch, Event* event); virtual void onTouchMoved(Touch* touch, Event* event); diff --git a/samples/Cpp/TestCpp/Classes/CocosDenshionTest/CocosDenshionTest.cpp b/samples/Cpp/TestCpp/Classes/CocosDenshionTest/CocosDenshionTest.cpp index e17a84db03..0fb59b1b89 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()->registerEventListenerWithSceneGraphPriority(listener, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); } diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp index 3e5d94c1eb..b3ef8c75d1 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp @@ -448,7 +448,7 @@ void TestParticleDisplay::onEnter() listener->onTouchBegan = CC_CALLBACK_2(TestParticleDisplay::onTouchBegan, this); - EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); } std::string TestParticleDisplay::title() @@ -503,7 +503,7 @@ void TestUseMutiplePicture::onEnter() listener->onTouchBegan = CC_CALLBACK_2(TestUseMutiplePicture::onTouchBegan, this); - EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); } std::string TestUseMutiplePicture::title() @@ -651,7 +651,7 @@ void TestArmatureNesting::onEnter() listener->onTouchBegan = CC_CALLBACK_2(TestArmatureNesting::onTouchBegan, this); - EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); } diff --git a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp index ee0d9b6573..8b0e698c47 100644 --- a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp +++ b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp @@ -577,9 +577,6 @@ RemoveMenuItemWhenMove::RemoveMenuItemWhenMove() void RemoveMenuItemWhenMove::onRegisterTouchListener() { - // Unregister touch event - EventDispatcher::getInstance()->unregisterEventListener(_touchListenerId); - // Register Touch Event auto listener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE); listener->setSwallowTouches(false); @@ -587,7 +584,8 @@ void RemoveMenuItemWhenMove::onRegisterTouchListener() listener->onTouchBegan = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchMoved, this); - _touchListenerId = EventDispatcher::getInstance()->registerEventListenerWithFixedPriority(listener, -100); + EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, -100); + _touchListener = listener; } void RemoveMenuItemWhenMove::goBack(Object *pSender) diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp index 56c88808c7..054b9856b0 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp @@ -177,9 +177,9 @@ void TouchableSpriteTest::onEnter() } }; - dispatcher->registerEventListenerWithSceneGraphPriority(listener1, sprite1); - dispatcher->registerEventListenerWithSceneGraphPriority(listener1->clone(), sprite2); - dispatcher->registerEventListenerWithSceneGraphPriority(listener1->clone(), sprite3); + dispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1); + dispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2); + dispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite3); } std::string TouchableSpriteTest::title() diff --git a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp index 21cb67701f..45c475cadf 100644 --- a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp +++ b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp @@ -100,7 +100,8 @@ void TouchesPerformTest1::onEnter() listener->onTouchEnded = CC_CALLBACK_2(TouchesPerformTest1::onTouchEnded, this); listener->onTouchCancelled = CC_CALLBACK_2(TouchesPerformTest1::onTouchCancelled, this); - EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + _touchListener = listener; } std::string TouchesPerformTest1::title() @@ -108,12 +109,6 @@ std::string TouchesPerformTest1::title() return "Targeted touches"; } -//void TouchesPerformTest1::registerWithTouchDispatcher() -//{ -// auto director = Director::getInstance(); -// director->getTouchDispatcher()->addTargetedDelegate(this, 0, true); -//} - bool TouchesPerformTest1::onTouchBegan(Touch* touch, Event* event) { numberOfTouchesB++; diff --git a/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp b/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp index ba423a90ab..ff0aadb032 100644 --- a/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp +++ b/samples/Cpp/TestCpp/Classes/TextInputTest/TextInputTest.cpp @@ -134,7 +134,7 @@ KeyboardNotificationLayer::KeyboardNotificationLayer() listener->onTouchBegan = CC_CALLBACK_2(KeyboardNotificationLayer::onTouchBegan, this); listener->onTouchEnded = CC_CALLBACK_2(KeyboardNotificationLayer::onTouchEnded, this); - EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); } //void KeyboardNotificationLayer::registerWithTouchDispatcher() diff --git a/samples/Cpp/TestCpp/Classes/TouchesTest/Paddle.cpp b/samples/Cpp/TestCpp/Classes/TouchesTest/Paddle.cpp index 90d82bbaad..c576147f45 100644 --- a/samples/Cpp/TestCpp/Classes/TouchesTest/Paddle.cpp +++ b/samples/Cpp/TestCpp/Classes/TouchesTest/Paddle.cpp @@ -37,9 +37,6 @@ void Paddle::onEnter() { Sprite::onEnter(); -// auto director = Director::getInstance(); -// director->getTouchDispatcher()->addTargetedDelegate(this, 0, true); - // Register Touch Event auto listener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE); listener->setSwallowTouches(true); @@ -48,7 +45,7 @@ void Paddle::onEnter() listener->onTouchMoved = CC_CALLBACK_2(Paddle::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(Paddle::onTouchEnded, this); - EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); } void Paddle::onExit() diff --git a/samples/Cpp/TestCpp/Classes/controller.cpp b/samples/Cpp/TestCpp/Classes/controller.cpp index 68c7d7d74a..f3ad2162a7 100644 --- a/samples/Cpp/TestCpp/Classes/controller.cpp +++ b/samples/Cpp/TestCpp/Classes/controller.cpp @@ -133,7 +133,7 @@ TestController::TestController() listener->onTouchBegan = CC_CALLBACK_2(TestController::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(TestController::onTouchMoved, this); - EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this); + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); } TestController::~TestController() diff --git a/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id b/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id index b9c29b151e..9ac6e60f7a 100644 --- a/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id +++ b/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id @@ -1 +1 @@ -b81aba469a4fa7d6ef527533671627541d195e73 \ No newline at end of file +60d103435762fea41e137a0d126bc9a94ec71dc3 \ No newline at end of file diff --git a/scripting/javascript/bindings/cocos2d_specifics.hpp b/scripting/javascript/bindings/cocos2d_specifics.hpp index db83b15be9..9668d213e0 100644 --- a/scripting/javascript/bindings/cocos2d_specifics.hpp +++ b/scripting/javascript/bindings/cocos2d_specifics.hpp @@ -218,7 +218,7 @@ private: typedef std::pair TouchDelegatePair; static TouchDelegateMap sTouchDelegateMap; bool _needUnroot; - int _listenerId; + TouchEventListener* _touchListener; };