diff --git a/extensions/GUI/CCScrollView/CCScrollView.cpp b/extensions/GUI/CCScrollView/CCScrollView.cpp index c06d60daa4..be3be450e8 100644 --- a/extensions/GUI/CCScrollView/CCScrollView.cpp +++ b/extensions/GUI/CCScrollView/CCScrollView.cpp @@ -61,7 +61,7 @@ ScrollView::ScrollView() ScrollView::~ScrollView() { - CC_SAFE_RELEASE(_touches); + } ScrollView* ScrollView::create(Size size, Node* container/* = NULL*/) @@ -111,8 +111,7 @@ bool ScrollView::initWithViewSize(Size size, Node *container/* = NULL*/) setTouchEnabled(true); setTouchMode(Touch::DispatchMode::ONE_BY_ONE); - _touches = new Array(); - _touches->init(); + _touches.reserve(TouchEvent::MAX_TOUCHES); _delegate = NULL; _bounceable = true; @@ -136,11 +135,6 @@ bool ScrollView::init() return this->initWithViewSize(Size(200, 200), NULL); } -//void ScrollView::registerWithTouchDispatcher() -//{ -// Director::getInstance()->getTouchDispatcher()->addTargetedDelegate(this, Layer::getTouchPriority(), false); -//} - bool ScrollView::isNodeVisible(Node* node) { const Point offset = this->getContentOffset(); @@ -189,8 +183,7 @@ void ScrollView::setTouchEnabled(bool e) { _dragging = false; _touchMoved = false; - if(_touches) - _touches->removeAllObjects(); + _touches.clear(); } } @@ -611,19 +604,19 @@ bool ScrollView::onTouchBegan(Touch* touch, Event* event) Rect frame = getViewRect(); //dispatcher does not know about clipping. reject touches outside visible bounds. - if (_touches->count() > 2 || + if (_touches.size() > 2 || _touchMoved || !frame.containsPoint(_container->convertToWorldSpace(_container->convertTouchToNodeSpace(touch)))) { return false; } - if (!_touches->containsObject(touch)) + if (std::find(_touches.begin(), _touches.end(), touch) != _touches.end()) { - _touches->addObject(touch); + _touches.push_back(touch); } - if (_touches->count() == 1) + if (_touches.size() == 1) { // scrolling _touchPoint = this->convertTouchToNodeSpace(touch); _touchMoved = false; @@ -631,13 +624,13 @@ bool ScrollView::onTouchBegan(Touch* touch, Event* event) _scrollDistance = Point(0.0f, 0.0f); _touchLength = 0.0f; } - else if (_touches->count() == 2) + else if (_touches.size() == 2) { - _touchPoint = (this->convertTouchToNodeSpace((Touch*)_touches->getObjectAtIndex(0)).getMidpoint( - this->convertTouchToNodeSpace((Touch*)_touches->getObjectAtIndex(1)))); + _touchPoint = (this->convertTouchToNodeSpace(_touches[0]).getMidpoint( + this->convertTouchToNodeSpace(_touches[1]))); - _touchLength = _container->convertTouchToNodeSpace((Touch*)_touches->getObjectAtIndex(0)).getDistance( - _container->convertTouchToNodeSpace((Touch*)_touches->getObjectAtIndex(1))); + _touchLength = _container->convertTouchToNodeSpace(_touches[0]).getDistance( + _container->convertTouchToNodeSpace(_touches[1])); _dragging = false; } @@ -651,9 +644,9 @@ void ScrollView::onTouchMoved(Touch* touch, Event* event) return; } - if (_touches->containsObject(touch)) + if (std::find(_touches.begin(), _touches.end(), touch) != _touches.end()) { - if (_touches->count() == 1 && _dragging) + if (_touches.size() == 1 && _dragging) { // scrolling Point moveDistance, newPoint, maxInset, minInset; Rect frame; @@ -661,7 +654,7 @@ void ScrollView::onTouchMoved(Touch* touch, Event* event) frame = getViewRect(); - newPoint = this->convertTouchToNodeSpace((Touch*)_touches->getObjectAtIndex(0)); + newPoint = this->convertTouchToNodeSpace(_touches[0]); moveDistance = newPoint - _touchPoint; float dis = 0.0f; @@ -716,10 +709,10 @@ void ScrollView::onTouchMoved(Touch* touch, Event* event) this->setContentOffset(Point(newX, newY)); } } - else if (_touches->count() == 2 && !_dragging) + else if (_touches.size() == 2 && !_dragging) { - const float len = _container->convertTouchToNodeSpace((Touch*)_touches->getObjectAtIndex(0)).getDistance( - _container->convertTouchToNodeSpace((Touch*)_touches->getObjectAtIndex(1))); + const float len = _container->convertTouchToNodeSpace(_touches[0]).getDistance( + _container->convertTouchToNodeSpace(_touches[1])); this->setZoomScale(this->getZoomScale()*len/_touchLength); } } @@ -731,16 +724,19 @@ void ScrollView::onTouchEnded(Touch* touch, Event* event) { return; } - if (_touches->containsObject(touch)) + + auto touchIter = std::find(_touches.begin(), _touches.end(), touch); + + if (touchIter != _touches.end()) { - if (_touches->count() == 1 && _touchMoved) + if (_touches.size() == 1 && _touchMoved) { this->schedule(schedule_selector(ScrollView::deaccelerateScrolling)); } - _touches->removeObject(touch); + _touches.erase(touchIter); } - if (_touches->count() == 0) + if (_touches.size() == 0) { _dragging = false; _touchMoved = false; @@ -753,8 +749,11 @@ void ScrollView::onTouchCancelled(Touch* touch, Event* event) { return; } - _touches->removeObject(touch); - if (_touches->count() == 0) + + auto touchIter = std::find(_touches.begin(), _touches.end(), touch); + _touches.erase(touchIter); + + if (_touches.size() == 0) { _dragging = false; _touchMoved = false; diff --git a/extensions/GUI/CCScrollView/CCScrollView.h b/extensions/GUI/CCScrollView/CCScrollView.h index 2c98bcb192..6c600b247c 100644 --- a/extensions/GUI/CCScrollView/CCScrollView.h +++ b/extensions/GUI/CCScrollView/CCScrollView.h @@ -304,9 +304,9 @@ protected: */ float _touchLength; /** - * UITouch objects to detect multitouch + * Touch objects to detect multitouch */ - Array* _touches; + std::vector _touches; /** * size to clip. Node boundingBox uses contentSize directly. * It's semantically different what it actually means to common scroll views. diff --git a/extensions/GUI/CCScrollView/CCTableView.cpp b/extensions/GUI/CCScrollView/CCTableView.cpp index 79c9b43d94..55c803ffd9 100644 --- a/extensions/GUI/CCScrollView/CCTableView.cpp +++ b/extensions/GUI/CCScrollView/CCTableView.cpp @@ -592,7 +592,7 @@ bool TableView::onTouchBegan(Touch *pTouch, Event *pEvent) bool touchResult = ScrollView::onTouchBegan(pTouch, pEvent); - if(_touches->count() == 1) { + if(_touches.size() == 1) { unsigned int index; Point point;