From 262b54eef2cccb4d4b825f262b5c7ac3640b72b3 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 25 Oct 2013 16:06:52 +0800 Subject: [PATCH] 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