2012-09-10 14:13:55 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2010 Sangwoo Im
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
#include "CCScrollView.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
|
|
|
#define SCROLL_DEACCEL_RATE 0.95f
|
|
|
|
#define SCROLL_DEACCEL_DIST 1.0f
|
|
|
|
#define BOUNCE_DURATION 0.15f
|
|
|
|
#define INSET_RATIO 0.2f
|
2013-02-16 19:36:21 +08:00
|
|
|
#define MOVE_INCH 7.0f/160.0f
|
|
|
|
|
|
|
|
static float convertDistanceFromPointToInch(float pointDis)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
float factor = ( EGLView::sharedOpenGLView()->getScaleX() + EGLView::sharedOpenGLView()->getScaleY() ) / 2;
|
|
|
|
return pointDis * factor / Device::getDPI();
|
2013-02-16 19:36:21 +08:00
|
|
|
}
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ScrollView::ScrollView()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _zoomScale(0.0f)
|
|
|
|
, _minZoomScale(0.0f)
|
|
|
|
, _maxZoomScale(0.0f)
|
|
|
|
, _delegate(NULL)
|
2013-06-20 14:15:53 +08:00
|
|
|
, _direction(kScrollViewDirectionBoth)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _dragging(false)
|
|
|
|
, _container(NULL)
|
|
|
|
, _touchMoved(false)
|
|
|
|
, _bounceable(false)
|
|
|
|
, _clippingToBounds(false)
|
|
|
|
, _touchLength(0.0f)
|
|
|
|
, _touches(NULL)
|
|
|
|
, _minScale(0.0f)
|
|
|
|
, _maxScale(0.0f)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ScrollView::~ScrollView()
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_touches->release();
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ScrollView* ScrollView::create(Size size, Node* container/* = NULL*/)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ScrollView* pRet = new ScrollView();
|
2012-06-18 18:30:07 +08:00
|
|
|
if (pRet && pRet->initWithViewSize(size, container))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ScrollView* ScrollView::create()
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ScrollView* pRet = new ScrollView();
|
2012-06-18 18:30:07 +08:00
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ScrollView::initWithViewSize(Size size, Node *container/* = NULL*/)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
if (Layer::init())
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_container = container;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!this->_container)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
_container = Layer::create();
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_container->ignoreAnchorPointForPosition(false);
|
|
|
|
this->_container->setAnchorPoint(ccp(0.0f, 0.0f));
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this->setViewSize(size);
|
|
|
|
|
|
|
|
setTouchEnabled(true);
|
2013-06-20 14:15:53 +08:00
|
|
|
_touches = new Array();
|
2013-06-15 14:03:30 +08:00
|
|
|
_delegate = NULL;
|
|
|
|
_bounceable = true;
|
|
|
|
_clippingToBounds = true;
|
2013-06-20 14:15:53 +08:00
|
|
|
//_container->setContentSize(SizeZero);
|
|
|
|
_direction = kScrollViewDirectionBoth;
|
2013-06-15 14:03:30 +08:00
|
|
|
_container->setPosition(ccp(0.0f, 0.0f));
|
|
|
|
_touchLength = 0.0f;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
this->addChild(_container);
|
|
|
|
_minScale = _maxScale = 1.0f;
|
2012-06-18 18:30:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ScrollView::init()
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
return this->initWithViewSize(CCSizeMake(200, 200), NULL);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::registerWithTouchDispatcher()
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->getTouchDispatcher()->addTargetedDelegate(this, Layer::getTouchPriority(), false);
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ScrollView::isNodeVisible(Node* node)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
const Point offset = this->getContentOffset();
|
|
|
|
const Size size = this->getViewSize();
|
2012-06-18 18:30:07 +08:00
|
|
|
const float scale = this->getZoomScale();
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect viewRect;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
viewRect = CCRectMake(-offset.x/scale, -offset.y/scale, size.width/scale, size.height/scale);
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
return viewRect.intersectsRect(node->boundingBox());
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::pause(Object* sender)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_container->pauseSchedulerAndActions();
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Object* pObj = NULL;
|
|
|
|
Array* pChildren = _container->getChildren();
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
CCARRAY_FOREACH(pChildren, pObj)
|
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
Node* pChild = static_cast<Node*>(pObj);
|
2012-06-18 18:30:07 +08:00
|
|
|
pChild->pauseSchedulerAndActions();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::resume(Object* sender)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Object* pObj = NULL;
|
|
|
|
Array* pChildren = _container->getChildren();
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
CCARRAY_FOREACH(pChildren, pObj)
|
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
Node* pChild = static_cast<Node*>(pObj);
|
2012-06-18 18:30:07 +08:00
|
|
|
pChild->resumeSchedulerAndActions();
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_container->resumeSchedulerAndActions();
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::setTouchEnabled(bool e)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Layer::setTouchEnabled(e);
|
2012-06-18 18:30:07 +08:00
|
|
|
if (!e)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_dragging = false;
|
|
|
|
_touchMoved = false;
|
|
|
|
_touches->removeAllObjects();
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::setContentOffset(Point offset, bool animated/* = false*/)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
if (animated)
|
|
|
|
{ //animate scrolling
|
|
|
|
this->setContentOffsetInDuration(offset, BOUNCE_DURATION);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ //set the container position directly
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_bounceable)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
const Point minOffset = this->minContainerOffset();
|
|
|
|
const Point maxOffset = this->maxContainerOffset();
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
offset.x = MAX(minOffset.x, MIN(maxOffset.x, offset.x));
|
|
|
|
offset.y = MAX(minOffset.y, MIN(maxOffset.y, offset.y));
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_container->setPosition(offset);
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_delegate != NULL)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_delegate->scrollViewDidScroll(this);
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::setContentOffsetInDuration(Point offset, float dt)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
FiniteTimeAction *scroll, *expire;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
scroll = MoveTo::create(dt, offset);
|
|
|
|
expire = CallFuncN::create(this, callfuncN_selector(ScrollView::stoppedAnimatedScroll));
|
|
|
|
_container->runAction(Sequence::create(scroll, expire, NULL));
|
|
|
|
this->schedule(schedule_selector(ScrollView::performedAnimatedScroll));
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Point ScrollView::getContentOffset()
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _container->getPosition();
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::setZoomScale(float s)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_container->getScale() != s)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point oldCenter, newCenter;
|
|
|
|
Point center;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touchLength == 0.0f)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
center = ccp(_viewSize.width*0.5f, _viewSize.height*0.5f);
|
2012-06-18 18:30:07 +08:00
|
|
|
center = this->convertToWorldSpace(center);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
center = _touchPoint;
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
oldCenter = _container->convertToNodeSpace(center);
|
|
|
|
_container->setScale(MAX(_minScale, MIN(_maxScale, s)));
|
|
|
|
newCenter = _container->convertToWorldSpace(oldCenter);
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
const Point offset = ccpSub(center, newCenter);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_delegate != NULL)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_delegate->scrollViewDidZoom(this);
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
this->setContentOffset(ccpAdd(_container->getPosition(),offset));
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float ScrollView::getZoomScale()
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _container->getScale();
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::setZoomScale(float s, bool animated)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
if (animated)
|
|
|
|
{
|
|
|
|
this->setZoomScaleInDuration(s, BOUNCE_DURATION);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->setZoomScale(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::setZoomScaleInDuration(float s, float dt)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
if (dt > 0)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_container->getScale() != s)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ActionTween *scaleAction;
|
|
|
|
scaleAction = ActionTween::create(dt, "zoomScale", _container->getScale(), s);
|
2012-06-18 18:30:07 +08:00
|
|
|
this->runAction(scaleAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->setZoomScale(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::setViewSize(Size size)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_viewSize = size;
|
2013-06-20 14:15:53 +08:00
|
|
|
Layer::setContentSize(size);
|
2012-06-19 09:43:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Node * ScrollView::getContainer()
|
2012-06-19 09:43:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return this->_container;
|
2012-06-19 09:43:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::setContainer(Node * pContainer)
|
2012-06-19 09:43:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
// Make sure that '_container' has a non-NULL value since there are
|
|
|
|
// lots of logic that use '_container'.
|
2013-04-15 09:52:46 +08:00
|
|
|
if (NULL == pContainer)
|
|
|
|
return;
|
2012-06-19 09:43:53 +08:00
|
|
|
|
2013-04-15 09:52:46 +08:00
|
|
|
this->removeAllChildrenWithCleanup(true);
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_container = pContainer;
|
2012-06-19 09:43:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_container->ignoreAnchorPointForPosition(false);
|
|
|
|
this->_container->setAnchorPoint(ccp(0.0f, 0.0f));
|
2012-06-19 09:43:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
this->addChild(this->_container);
|
2012-06-19 09:43:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
this->setViewSize(this->_viewSize);
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::relocateContainer(bool animated)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point oldPoint, min, max;
|
2012-08-01 15:30:12 +08:00
|
|
|
float newX, newY;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
min = this->minContainerOffset();
|
|
|
|
max = this->maxContainerOffset();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
oldPoint = _container->getPosition();
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
newX = oldPoint.x;
|
|
|
|
newY = oldPoint.y;
|
2013-06-20 14:15:53 +08:00
|
|
|
if (_direction == kScrollViewDirectionBoth || _direction == kScrollViewDirectionHorizontal)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
newX = MAX(newX, min.x);
|
2012-12-05 22:55:06 +08:00
|
|
|
newX = MIN(newX, max.x);
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
if (_direction == kScrollViewDirectionBoth || _direction == kScrollViewDirectionVertical)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
newY = MIN(newY, max.y);
|
|
|
|
newY = MAX(newY, min.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newY != oldPoint.y || newX != oldPoint.x)
|
|
|
|
{
|
|
|
|
this->setContentOffset(ccp(newX, newY), animated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Point ScrollView::maxContainerOffset()
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
return ccp(0.0f, 0.0f);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Point ScrollView::minContainerOffset()
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return ccp(_viewSize.width - _container->getContentSize().width*_container->getScaleX(),
|
|
|
|
_viewSize.height - _container->getContentSize().height*_container->getScaleY());
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::deaccelerateScrolling(float dt)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_dragging)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
this->unschedule(schedule_selector(ScrollView::deaccelerateScrolling));
|
2012-06-18 18:30:07 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float newX, newY;
|
2013-06-20 14:15:53 +08:00
|
|
|
Point maxInset, minInset;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_container->setPosition(ccpAdd(_container->getPosition(), _scrollDistance));
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_bounceable)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
maxInset = _maxInset;
|
|
|
|
minInset = _minInset;
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
maxInset = this->maxContainerOffset();
|
|
|
|
minInset = this->minContainerOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
//check to see if offset lies within the inset bounds
|
2013-06-15 14:03:30 +08:00
|
|
|
newX = MIN(_container->getPosition().x, maxInset.x);
|
2012-06-18 18:30:07 +08:00
|
|
|
newX = MAX(newX, minInset.x);
|
2013-06-15 14:03:30 +08:00
|
|
|
newY = MIN(_container->getPosition().y, maxInset.y);
|
2012-06-18 18:30:07 +08:00
|
|
|
newY = MAX(newY, minInset.y);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
newX = _container->getPosition().x;
|
|
|
|
newY = _container->getPosition().y;
|
2012-12-05 04:32:13 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_scrollDistance = ccpSub(_scrollDistance, ccp(newX - _container->getPosition().x, newY - _container->getPosition().y));
|
|
|
|
_scrollDistance = ccpMult(_scrollDistance, SCROLL_DEACCEL_RATE);
|
2012-06-18 18:30:07 +08:00
|
|
|
this->setContentOffset(ccp(newX,newY));
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if ((fabsf(_scrollDistance.x) <= SCROLL_DEACCEL_DIST &&
|
|
|
|
fabsf(_scrollDistance.y) <= SCROLL_DEACCEL_DIST) ||
|
2012-12-05 04:32:13 +08:00
|
|
|
newY > maxInset.y || newY < minInset.y ||
|
|
|
|
newX > maxInset.x || newX < minInset.x ||
|
2012-06-18 18:30:07 +08:00
|
|
|
newX == maxInset.x || newX == minInset.x ||
|
|
|
|
newY == maxInset.y || newY == minInset.y)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
this->unschedule(schedule_selector(ScrollView::deaccelerateScrolling));
|
2012-06-18 18:30:07 +08:00
|
|
|
this->relocateContainer(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::stoppedAnimatedScroll(Node * node)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
this->unschedule(schedule_selector(ScrollView::performedAnimatedScroll));
|
2013-02-18 09:45:01 +08:00
|
|
|
// After the animation stopped, "scrollViewDidScroll" should be invoked, this could fix the bug of lack of tableview cells.
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_delegate != NULL)
|
2013-02-18 09:45:01 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_delegate->scrollViewDidScroll(this);
|
2013-02-18 09:45:01 +08:00
|
|
|
}
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::performedAnimatedScroll(float dt)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_dragging)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
this->unschedule(schedule_selector(ScrollView::performedAnimatedScroll));
|
2012-06-18 18:30:07 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_delegate != NULL)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_delegate->scrollViewDidScroll(this);
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
const Size& ScrollView::getContentSize() const
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _container->getContentSize();
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::setContentSize(const Size & size)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2012-09-09 22:34:32 +08:00
|
|
|
if (this->getContainer() != NULL)
|
|
|
|
{
|
|
|
|
this->getContainer()->setContentSize(size);
|
2012-09-09 22:46:31 +08:00
|
|
|
this->updateInset();
|
|
|
|
}
|
|
|
|
}
|
2012-09-09 22:34:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::updateInset()
|
2012-09-09 22:46:31 +08:00
|
|
|
{
|
|
|
|
if (this->getContainer() != NULL)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_maxInset = this->maxContainerOffset();
|
|
|
|
_maxInset = ccp(_maxInset.x + _viewSize.width * INSET_RATIO,
|
|
|
|
_maxInset.y + _viewSize.height * INSET_RATIO);
|
|
|
|
_minInset = this->minContainerOffset();
|
|
|
|
_minInset = ccp(_minInset.x - _viewSize.width * INSET_RATIO,
|
|
|
|
_minInset.y - _viewSize.height * INSET_RATIO);
|
2012-09-09 22:46:31 +08:00
|
|
|
}
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
2012-09-09 22:46:31 +08:00
|
|
|
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* make sure all children go to the container
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::addChild(Node * child, int zOrder, int tag)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
child->ignoreAnchorPointForPosition(false);
|
|
|
|
child->setAnchorPoint(ccp(0.0f, 0.0f));
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_container != child) {
|
|
|
|
_container->addChild(child, zOrder, tag);
|
2012-06-18 18:30:07 +08:00
|
|
|
} else {
|
2013-06-20 14:15:53 +08:00
|
|
|
Layer::addChild(child, zOrder, tag);
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::addChild(Node * child, int zOrder)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
this->addChild(child, zOrder, child->getTag());
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::addChild(Node * child)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
this->addChild(child, child->getZOrder(), child->getTag());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clip this view so that outside of the visible bounds can be hidden.
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::beforeDraw()
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_clippingToBounds)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_scissorRestored = false;
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect frame = getViewRect();
|
|
|
|
if (EGLView::sharedOpenGLView()->isScissorEnabled()) {
|
2013-06-15 14:03:30 +08:00
|
|
|
_scissorRestored = true;
|
2013-06-20 14:15:53 +08:00
|
|
|
_parentScissorRect = EGLView::sharedOpenGLView()->getScissorRect();
|
2013-06-15 14:03:30 +08:00
|
|
|
//set the intersection of _parentScissorRect and frame as the new scissor rect
|
|
|
|
if (frame.intersectsRect(_parentScissorRect)) {
|
|
|
|
float x = MAX(frame.origin.x, _parentScissorRect.origin.x);
|
|
|
|
float y = MAX(frame.origin.y, _parentScissorRect.origin.y);
|
|
|
|
float xx = MIN(frame.origin.x+frame.size.width, _parentScissorRect.origin.x+_parentScissorRect.size.width);
|
|
|
|
float yy = MIN(frame.origin.y+frame.size.height, _parentScissorRect.origin.y+_parentScissorRect.size.height);
|
2013-06-20 14:15:53 +08:00
|
|
|
EGLView::sharedOpenGLView()->setScissorInPoints(x, y, xx-x, yy-y);
|
2013-03-29 16:05:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2013-06-20 14:15:53 +08:00
|
|
|
EGLView::sharedOpenGLView()->setScissorInPoints(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
|
2013-03-29 16:05:28 +08:00
|
|
|
}
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* retract what's done in beforeDraw so that there's no side effect to
|
|
|
|
* other nodes.
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::afterDraw()
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_clippingToBounds)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_scissorRestored) {//restore the parent's scissor rect
|
2013-06-20 14:15:53 +08:00
|
|
|
EGLView::sharedOpenGLView()->setScissorInPoints(_parentScissorRect.origin.x, _parentScissorRect.origin.y, _parentScissorRect.size.width, _parentScissorRect.size.height);
|
2013-03-29 16:05:28 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
}
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::visit()
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
// quick return if not visible
|
|
|
|
if (!isVisible())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
kmGLPushMatrix();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_grid && _grid->isActive())
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_grid->beforeDraw();
|
2012-06-18 18:30:07 +08:00
|
|
|
this->transformAncestors();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->transform();
|
|
|
|
this->beforeDraw();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_children)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccArray *arrayData = _children->data;
|
2012-06-18 18:30:07 +08:00
|
|
|
unsigned int i=0;
|
|
|
|
|
|
|
|
// draw children zOrder < 0
|
|
|
|
for( ; i < arrayData->num; i++ )
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Node *child = (Node*)arrayData->arr[i];
|
2012-06-18 18:30:07 +08:00
|
|
|
if ( child->getZOrder() < 0 )
|
|
|
|
{
|
|
|
|
child->visit();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this draw
|
|
|
|
this->draw();
|
|
|
|
|
|
|
|
// draw children zOrder >= 0
|
|
|
|
for( ; i < arrayData->num; i++ )
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Node* child = (Node*)arrayData->arr[i];
|
2012-06-18 18:30:07 +08:00
|
|
|
child->visit();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->afterDraw();
|
2013-06-15 14:03:30 +08:00
|
|
|
if ( _grid && _grid->isActive())
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_grid->afterDraw(this);
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
kmGLPopMatrix();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ScrollView::ccTouchBegan(Touch* touch, Event* event)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
if (!this->isVisible())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect frame = getViewRect();
|
2013-02-16 14:18:13 +08:00
|
|
|
|
2012-06-18 18:30:07 +08:00
|
|
|
//dispatcher does not know about clipping. reject touches outside visible bounds.
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touches->count() > 2 ||
|
|
|
|
_touchMoved ||
|
|
|
|
!frame.containsPoint(_container->convertToWorldSpace(_container->convertTouchToNodeSpace(touch))))
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_touches->containsObject(touch))
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_touches->addObject(touch);
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touches->count() == 1)
|
2012-06-18 18:30:07 +08:00
|
|
|
{ // scrolling
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchPoint = this->convertTouchToNodeSpace(touch);
|
|
|
|
_touchMoved = false;
|
|
|
|
_dragging = true; //dragging started
|
|
|
|
_scrollDistance = ccp(0.0f, 0.0f);
|
|
|
|
_touchLength = 0.0f;
|
|
|
|
}
|
|
|
|
else if (_touches->count() == 2)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
_touchPoint = ccpMidpoint(this->convertTouchToNodeSpace((Touch*)_touches->objectAtIndex(0)),
|
|
|
|
this->convertTouchToNodeSpace((Touch*)_touches->objectAtIndex(1)));
|
|
|
|
_touchLength = ccpDistance(_container->convertTouchToNodeSpace((Touch*)_touches->objectAtIndex(0)),
|
|
|
|
_container->convertTouchToNodeSpace((Touch*)_touches->objectAtIndex(1)));
|
2013-06-15 14:03:30 +08:00
|
|
|
_dragging = false;
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::ccTouchMoved(Touch* touch, Event* event)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
if (!this->isVisible())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touches->containsObject(touch))
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touches->count() == 1 && _dragging)
|
2012-06-18 18:30:07 +08:00
|
|
|
{ // scrolling
|
2013-06-20 14:15:53 +08:00
|
|
|
Point moveDistance, newPoint, maxInset, minInset;
|
|
|
|
Rect frame;
|
2012-08-01 15:30:12 +08:00
|
|
|
float newX, newY;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-02-16 14:18:13 +08:00
|
|
|
frame = getViewRect();
|
2012-10-08 14:12:34 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
newPoint = this->convertTouchToNodeSpace((Touch*)_touches->objectAtIndex(0));
|
2013-06-15 14:03:30 +08:00
|
|
|
moveDistance = ccpSub(newPoint, _touchPoint);
|
2013-02-16 19:36:21 +08:00
|
|
|
|
|
|
|
float dis = 0.0f;
|
2013-06-20 14:15:53 +08:00
|
|
|
if (_direction == kScrollViewDirectionVertical)
|
2013-02-16 19:36:21 +08:00
|
|
|
{
|
|
|
|
dis = moveDistance.y;
|
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
else if (_direction == kScrollViewDirectionHorizontal)
|
2013-02-16 19:36:21 +08:00
|
|
|
{
|
|
|
|
dis = moveDistance.x;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dis = sqrtf(moveDistance.x*moveDistance.x + moveDistance.y*moveDistance.y);
|
|
|
|
}
|
2013-02-17 17:13:06 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_touchMoved && fabs(convertDistanceFromPointToInch(dis)) < MOVE_INCH )
|
2013-02-16 19:36:21 +08:00
|
|
|
{
|
2013-02-17 17:13:06 +08:00
|
|
|
//CCLOG("Invalid movement, distance = [%f, %f], disInch = %f", moveDistance.x, moveDistance.y);
|
2013-02-16 19:36:21 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_touchMoved)
|
2013-02-16 19:36:21 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
moveDistance = PointZero;
|
2013-02-16 19:36:21 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchPoint = newPoint;
|
|
|
|
_touchMoved = true;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
if (frame.containsPoint(this->convertToWorldSpace(newPoint)))
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
switch (_direction)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
case kScrollViewDirectionVertical:
|
2012-06-18 18:30:07 +08:00
|
|
|
moveDistance = ccp(0.0f, moveDistance.y);
|
|
|
|
break;
|
2013-06-20 14:15:53 +08:00
|
|
|
case kScrollViewDirectionHorizontal:
|
2012-06-18 18:30:07 +08:00
|
|
|
moveDistance = ccp(moveDistance.x, 0.0f);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
maxInset = _maxInset;
|
|
|
|
minInset = _minInset;
|
2012-12-05 04:32:13 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
newX = _container->getPosition().x + moveDistance.x;
|
|
|
|
newY = _container->getPosition().y + moveDistance.y;
|
2012-12-05 04:32:13 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_scrollDistance = moveDistance;
|
2012-06-18 18:30:07 +08:00
|
|
|
this->setContentOffset(ccp(newX, newY));
|
|
|
|
}
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
else if (_touches->count() == 2 && !_dragging)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
const float len = ccpDistance(_container->convertTouchToNodeSpace((Touch*)_touches->objectAtIndex(0)),
|
|
|
|
_container->convertTouchToNodeSpace((Touch*)_touches->objectAtIndex(1)));
|
2013-06-15 14:03:30 +08:00
|
|
|
this->setZoomScale(this->getZoomScale()*len/_touchLength);
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::ccTouchEnded(Touch* touch, Event* event)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
if (!this->isVisible())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touches->containsObject(touch))
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touches->count() == 1 && _touchMoved)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
this->schedule(schedule_selector(ScrollView::deaccelerateScrolling));
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_touches->removeObject(touch);
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touches->count() == 0)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_dragging = false;
|
|
|
|
_touchMoved = false;
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ScrollView::ccTouchCancelled(Touch* touch, Event* event)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
if (!this->isVisible())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_touches->removeObject(touch);
|
|
|
|
if (_touches->count() == 0)
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_dragging = false;
|
|
|
|
_touchMoved = false;
|
2012-06-18 18:30:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect ScrollView::getViewRect()
|
2013-02-16 14:18:13 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point screenPos = this->convertToWorldSpace(PointZero);
|
2013-02-16 14:18:13 +08:00
|
|
|
|
|
|
|
float scaleX = this->getScaleX();
|
|
|
|
float scaleY = this->getScaleY();
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
for (Node *p = _parent; p != NULL; p = p->getParent()) {
|
2013-02-16 14:18:13 +08:00
|
|
|
scaleX *= p->getScaleX();
|
|
|
|
scaleY *= p->getScaleY();
|
|
|
|
}
|
2013-04-30 23:17:38 +08:00
|
|
|
|
|
|
|
// Support negative scaling. Not doing so causes intersectsRect calls
|
|
|
|
// (eg: to check if the touch was within the bounds) to return false.
|
2013-06-20 14:15:53 +08:00
|
|
|
// Note, Node::getScale will assert if X and Y scales are different.
|
2013-04-30 23:17:38 +08:00
|
|
|
if(scaleX<0.f) {
|
2013-06-15 14:03:30 +08:00
|
|
|
screenPos.x += _viewSize.width*scaleX;
|
2013-04-30 23:17:38 +08:00
|
|
|
scaleX = -scaleX;
|
|
|
|
}
|
|
|
|
if(scaleY<0.f) {
|
2013-06-15 14:03:30 +08:00
|
|
|
screenPos.y += _viewSize.height*scaleY;
|
2013-04-30 23:17:38 +08:00
|
|
|
scaleY = -scaleY;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
return CCRectMake(screenPos.x, screenPos.y, _viewSize.width*scaleX, _viewSize.height*scaleY);
|
2013-02-16 14:18:13 +08:00
|
|
|
}
|
2012-06-18 18:30:07 +08:00
|
|
|
NS_CC_EXT_END
|