From 365419a8b20db5a8e848614325926fc1c4c9997b Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 16 Sep 2013 23:35:16 +0800 Subject: [PATCH 1/2] issue #2087: [dispatcher] Fix box2d_test crash. --- .../CCLayer.cpp | 18 ++++++++++++------ .../layers_scenes_transitions_nodes/CCLayer.h | 3 ++- .../TestCpp/Classes/Box2DTestBed/Box2dView.cpp | 8 ++++---- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index fc8dd27d63..6179bfd934 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -98,8 +98,13 @@ Layer *Layer::create() /// Touch and Accelerometer related -void Layer::onRegisterTouchListener() -{ +void Layer::addTouchListener() +{ + if (_touchListener != nullptr) + return; + + auto dispatcher = EventDispatcher::getInstance(); + if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE ) { // Register Touch Event @@ -110,7 +115,7 @@ void Layer::onRegisterTouchListener() listener->onTouchesEnded = CC_CALLBACK_2(Layer::onTouchesEnded, this); listener->onTouchesCancelled = CC_CALLBACK_2(Layer::onTouchesCancelled, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); _touchListener = listener; } else @@ -124,7 +129,7 @@ void Layer::onRegisterTouchListener() listener->onTouchEnded = CC_CALLBACK_2(Layer::onTouchEnded, this); listener->onTouchCancelled = CC_CALLBACK_2(Layer::onTouchCancelled, this); - EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); + dispatcher->addEventListenerWithSceneGraphPriority(listener, this); _touchListener = listener; } } @@ -170,7 +175,7 @@ void Layer::setTouchEnabled(bool enabled) { if (enabled) { - this->onRegisterTouchListener(); + this->addTouchListener(); } else { @@ -309,7 +314,7 @@ void Layer::onEnter() // since events are propagated in reverse order if (_touchEnabled) { - this->onRegisterTouchListener(); + this->addTouchListener(); } // then iterate over all the children @@ -348,6 +353,7 @@ void Layer::onEnterTransitionDidFinish() if (_accelerometerEnabled) { auto dispatcher = EventDispatcher::getInstance(); + dispatcher->removeEventListener(_accelerationListener); _accelerationListener = AccelerationEventListener::create(CC_CALLBACK_2(Layer::onAcceleration, this)); dispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this); } diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h index ca75eb3dd0..ffd4eafe2c 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h @@ -113,7 +113,6 @@ public: @since v0.8.0 */ CC_DEPRECATED_ATTRIBUTE virtual void registerWithTouchDispatcher() final {}; - virtual void onRegisterTouchListener(); /** whether or not it will receive Touch events. You can enable / disable touch events with this property. @@ -180,6 +179,8 @@ public: virtual void onEnterTransitionDidFinish() override; protected: + void addTouchListener(); + bool _touchEnabled; bool _accelerometerEnabled; bool _keyboardEnabled; diff --git a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp index b35b38048e..d99f5e6f3c 100644 --- a/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp +++ b/samples/Cpp/TestCpp/Classes/Box2DTestBed/Box2dView.cpp @@ -53,6 +53,7 @@ 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); @@ -90,7 +91,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, 0); + EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 1); _touchListener = listener; return true; } @@ -185,6 +186,7 @@ bool Box2DView::initWithEntryID(int entryId) m_entry = g_testEntries + entryId; m_test = m_entry->createFcn(); + setTouchMode(Touch::DispatchMode::ONE_BY_ONE); // Removes Touch Event Listener EventDispatcher::getInstance()->removeEventListener(_touchListener); @@ -197,7 +199,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::getInstance()->addEventListenerWithFixedPriority(listener, -10); _touchListener = listener; return true; @@ -268,8 +270,6 @@ void Box2DView::onTouchEnded(Touch* touch, Event* event) log("Box2DView::onTouchEnded, pos: %f,%f -> %f,%f", touchLocation.x, touchLocation.y, nodePosition.x, nodePosition.y); m_test->MouseUp(b2Vec2(nodePosition.x,nodePosition.y)); - -// EventDispatcher::getInstance()->setPriorityWithFixedValue(_touchEventId, -1); } // void Box2DView::accelerometer(UIAccelerometer* accelerometer, Acceleration* acceleration) From 650e6ad6fcca01f1371521f84ec509633deed937 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 17 Sep 2013 10:46:31 +0800 Subject: [PATCH 2/2] issue #2087: [dispatcher] Updating MenuTest. --- .../Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp | 17 +++++++---------- samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h | 2 -- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp index 8b0e698c47..e8081a2563 100644 --- a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp +++ b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp @@ -573,19 +573,16 @@ RemoveMenuItemWhenMove::RemoveMenuItemWhenMove() setTouchEnabled(true); setTouchMode(Touch::DispatchMode::ONE_BY_ONE); -} - -void RemoveMenuItemWhenMove::onRegisterTouchListener() -{ + // Register Touch Event - auto listener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE); - listener->setSwallowTouches(false); + _touchListener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE); + _touchListener->setSwallowTouches(false); - listener->onTouchBegan = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchBegan, this); - listener->onTouchMoved = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchMoved, this); + _touchListener->onTouchBegan = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchBegan, this); + _touchListener->onTouchMoved = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchMoved, this); + + EventDispatcher::getInstance()->addEventListenerWithFixedPriority(_touchListener, -100); - EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, -100); - _touchListener = listener; } void RemoveMenuItemWhenMove::goBack(Object *pSender) diff --git a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h index af97bf66aa..a6384d0195 100644 --- a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h +++ b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.h @@ -107,8 +107,6 @@ public: RemoveMenuItemWhenMove(); ~RemoveMenuItemWhenMove(); - virtual void onRegisterTouchListener() override; -// virtual void registerWithTouchDispatcher(void); virtual bool onTouchBegan(Touch *touch, Event *event); virtual void onTouchMoved(Touch *touch, Event *event);