axmol/cocos/gui/UIScrollView.cpp

1615 lines
54 KiB
C++
Raw Normal View History

2013-09-13 22:20:20 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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.
****************************************************************************/
#include "gui/UIScrollView.h"
namespace gui {
2013-09-13 22:20:20 +08:00
2013-11-06 16:04:06 +08:00
#define AUTOSCROLLMAXSPEED 1000.0f
const cocos2d::Point SCROLLDIR_UP = cocos2d::Point(0.0f, 1.0f);
const cocos2d::Point SCROLLDIR_DOWN = cocos2d::Point(0.0f, -1.0f);
const cocos2d::Point SCROLLDIR_LEFT = cocos2d::Point(-1.0f, 0.0f);
const cocos2d::Point SCROLLDIR_RIGHT = cocos2d::Point(1.0f, 0.0f);
2013-09-13 22:20:20 +08:00
UIScrollView::UIScrollView():
2013-11-14 11:37:46 +08:00
_innerContainer(nullptr),
2013-09-16 20:54:13 +08:00
_direction(SCROLLVIEW_DIR_VERTICAL),
2013-11-06 16:04:06 +08:00
_touchBeganPoint(cocos2d::Point::ZERO),
_touchMovedPoint(cocos2d::Point::ZERO),
_touchEndedPoint(cocos2d::Point::ZERO),
_touchMovingPoint(cocos2d::Point::ZERO),
_autoScrollDir(cocos2d::Point::ZERO),
2013-09-16 20:54:13 +08:00
_topBoundary(0.0f),
_bottomBoundary(0.0f),
_leftBoundary(0.0f),
_rightBoundary(0.0f),
2013-11-06 16:04:06 +08:00
_bounceTopBoundary(0.0f),
_bounceBottomBoundary(0.0f),
_bounceLeftBoundary(0.0f),
_bounceRightBoundary(0.0f),
2013-09-16 20:54:13 +08:00
_autoScroll(false),
2013-11-06 16:04:06 +08:00
_autoScrollAddUpTime(0.0f),
2013-09-16 20:54:13 +08:00
_autoScrollOriginalSpeed(0.0f),
2013-11-06 16:04:06 +08:00
_autoScrollAcceleration(-1000.0f),
_isAutoScrollSpeedAttenuated(false),
_needCheckAutoScrollDestination(false),
_autoScrollDestination(cocos2d::Point::ZERO),
2013-09-16 20:54:13 +08:00
_bePressed(false),
_slidTime(0.0f),
2013-11-06 16:04:06 +08:00
_moveChildPoint(cocos2d::Point::ZERO),
2013-09-16 20:54:13 +08:00
_childFocusCancelOffset(5.0f),
2013-11-06 16:04:06 +08:00
_leftBounceNeeded(false),
_topBounceNeeded(false),
_rightBounceNeeded(false),
_bottomBounceNeeded(false),
_bounceEnabled(false),
_bouncing(false),
_bounceDir(cocos2d::Point::ZERO),
_bounceOriginalSpeed(0.0f),
_inertiaScrollEnabled(true),
2013-11-14 11:37:46 +08:00
_scrollViewEventListener(nullptr),
_scrollViewEventSelector(nullptr)
2013-09-13 22:20:20 +08:00
{
}
UIScrollView::~UIScrollView()
{
2013-11-14 11:37:46 +08:00
_scrollViewEventListener = nullptr;
_scrollViewEventSelector = nullptr;
2013-09-13 22:20:20 +08:00
}
UIScrollView* UIScrollView::create()
{
UIScrollView* widget = new UIScrollView();
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
2013-11-14 11:37:46 +08:00
return nullptr;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
bool UIScrollView::init()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (UILayout::init())
{
setUpdateEnabled(true);
setTouchEnabled(true);
setClippingEnabled(true);
_innerContainer->setTouchEnabled(false);
return true;
}
return false;
}
void UIScrollView::initRenderer()
{
UILayout::initRenderer();
_innerContainer = UILayout::create();
UILayout::addChild(_innerContainer);
}
void UIScrollView::onSizeChanged()
{
UILayout::onSizeChanged();
_topBoundary = _size.height;
_rightBoundary = _size.width;
float bounceBoundaryParameterX = _size.width / 3.0f;
float bounceBoundaryParameterY = _size.height / 3.0f;
_bounceTopBoundary = _size.height - bounceBoundaryParameterY;
_bounceBottomBoundary = bounceBoundaryParameterY;
_bounceLeftBoundary = bounceBoundaryParameterX;
_bounceRightBoundary = _size.width - bounceBoundaryParameterX;
cocos2d::Size innerSize = _innerContainer->getSize();
float orginInnerSizeWidth = innerSize.width;
float orginInnerSizeHeight = innerSize.height;
float innerSizeWidth = MAX(orginInnerSizeWidth, _size.width);
float innerSizeHeight = MAX(orginInnerSizeHeight, _size.height);
_innerContainer->setSize(cocos2d::Size(innerSizeWidth, innerSizeHeight));
_innerContainer->setPosition(cocos2d::Point(0, _size.height - _innerContainer->getSize().height));
}
void UIScrollView::setInnerContainerSize(const cocos2d::Size &size)
{
float innerSizeWidth = _size.width;
float innerSizeHeight = _size.height;
cocos2d::Size originalInnerSize = _innerContainer->getSize();
if (size.width < _size.width)
{
CCLOG("Inner width <= scrollview width, it will be force sized!");
}
else
{
innerSizeWidth = size.width;
}
if (size.height < _size.height)
{
CCLOG("Inner height <= scrollview height, it will be force sized!");
}
else
{
innerSizeHeight = size.height;
}
_innerContainer->setSize(cocos2d::Size(innerSizeWidth, innerSizeHeight));
switch (_direction)
{
case SCROLLVIEW_DIR_VERTICAL:
{
cocos2d::Size newInnerSize = _innerContainer->getSize();
float offset = originalInnerSize.height - newInnerSize.height;
scrollChildren(0.0f, offset);
break;
}
case SCROLLVIEW_DIR_HORIZONTAL:
{
if (_innerContainer->getRightInParent() <= _size.width)
{
cocos2d::Size newInnerSize = _innerContainer->getSize();
float offset = originalInnerSize.width - newInnerSize.width;
scrollChildren(offset, 0.0f);
}
break;
}
case SCROLLVIEW_DIR_BOTH:
{
cocos2d::Size newInnerSize = _innerContainer->getSize();
float offsetY = originalInnerSize.height - newInnerSize.height;
float offsetX = 0.0f;
if (_innerContainer->getRightInParent() <= _size.width)
{
offsetX = originalInnerSize.width - newInnerSize.width;
}
scrollChildren(offsetX, offsetY);
break;
}
default:
break;
}
}
const cocos2d::Size& UIScrollView::getInnerContainerSize() const
{
return _innerContainer->getSize();
}
bool UIScrollView::addChild(UIWidget* widget)
{
return _innerContainer->addChild(widget);
}
void UIScrollView::removeAllChildren()
{
_innerContainer->removeAllChildren();
}
bool UIScrollView::removeChild(UIWidget* child)
{
return _innerContainer->removeChild(child);
}
cocos2d::Array* UIScrollView::getChildren()
{
return _innerContainer->getChildren();
}
void UIScrollView::moveChildren(float offsetX, float offsetY)
{
_moveChildPoint = _innerContainer->getPosition() + cocos2d::Point(offsetX, offsetY);
_innerContainer->setPosition(_moveChildPoint);
}
void UIScrollView::autoScrollChildren(float dt)
{
float lastTime = _autoScrollAddUpTime;
_autoScrollAddUpTime += dt;
if (_isAutoScrollSpeedAttenuated)
{
float nowSpeed = _autoScrollOriginalSpeed + _autoScrollAcceleration * _autoScrollAddUpTime;
if (nowSpeed <= 0.0f)
{
stopAutoScrollChildren();
checkNeedBounce();
}
else
{
float timeParam = lastTime * 2 + dt;
float offset = (_autoScrollOriginalSpeed + _autoScrollAcceleration * timeParam * 0.5f) * dt;
float offsetX = offset * _autoScrollDir.x;
float offsetY = offset * _autoScrollDir.y;
if (!scrollChildren(offsetX, offsetY))
{
stopAutoScrollChildren();
checkNeedBounce();
}
}
}
else
{
if (_needCheckAutoScrollDestination)
{
float xOffset = _autoScrollDir.x * dt * _autoScrollOriginalSpeed;
float yOffset = _autoScrollDir.y * dt * _autoScrollOriginalSpeed;
bool notDone = checkCustomScrollDestination(&xOffset, &yOffset);
bool scrollCheck = scrollChildren(xOffset, yOffset);
if (!notDone || !scrollCheck)
{
stopAutoScrollChildren();
checkNeedBounce();
}
}
else
{
if (!scrollChildren(_autoScrollDir.x * dt * _autoScrollOriginalSpeed, _autoScrollDir.y * dt * _autoScrollOriginalSpeed))
{
stopAutoScrollChildren();
checkNeedBounce();
}
}
}
}
void UIScrollView::bounceChildren(float dt)
{
if (_bounceOriginalSpeed <= 0.0f)
{
stopBounceChildren();
}
if (!bounceScrollChildren(_bounceDir.x * dt * _bounceOriginalSpeed, _bounceDir.y * dt * _bounceOriginalSpeed))
{
stopBounceChildren();
}
}
bool UIScrollView::checkNeedBounce()
{
if (!_bounceEnabled)
{
return false;
}
checkBounceBoundary();
if (_topBounceNeeded || _bottomBounceNeeded || _leftBounceNeeded || _rightBounceNeeded)
{
if (_topBounceNeeded && _leftBounceNeeded)
{
cocos2d::Point scrollVector = cocos2d::Point(0.0f, _size.height) - cocos2d::Point(_innerContainer->getLeftInParent(), _innerContainer->getTopInParent());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
startBounceChildren(orSpeed);
}
else if (_topBounceNeeded && _rightBounceNeeded)
{
cocos2d::Point scrollVector = cocos2d::Point(_size.width, _size.height) - cocos2d::Point(_innerContainer->getRightInParent(), _innerContainer->getTopInParent());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
startBounceChildren(orSpeed);
}
else if (_bottomBounceNeeded && _leftBounceNeeded)
{
cocos2d::Point scrollVector = cocos2d::Point::ZERO - cocos2d::Point(_innerContainer->getLeftInParent(), _innerContainer->getBottomInParent());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
startBounceChildren(orSpeed);
}
else if (_bottomBounceNeeded && _rightBounceNeeded)
{
cocos2d::Point scrollVector = cocos2d::Point(_size.width, 0.0f) - cocos2d::Point(_innerContainer->getRightInParent(), _innerContainer->getBottomInParent());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
startBounceChildren(orSpeed);
}
else if (_topBounceNeeded)
{
cocos2d::Point scrollVector = cocos2d::Point(0.0f, _size.height) - cocos2d::Point(0.0f, _innerContainer->getTopInParent());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
startBounceChildren(orSpeed);
}
else if (_bottomBounceNeeded)
{
cocos2d::Point scrollVector = cocos2d::Point::ZERO - cocos2d::Point(0.0f, _innerContainer->getBottomInParent());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
startBounceChildren(orSpeed);
}
else if (_leftBounceNeeded)
{
cocos2d::Point scrollVector = cocos2d::Point::ZERO - cocos2d::Point(_innerContainer->getLeftInParent(), 0.0f);
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
startBounceChildren(orSpeed);
}
else if (_rightBounceNeeded)
{
cocos2d::Point scrollVector = cocos2d::Point(_size.width, 0.0f) - cocos2d::Point(_innerContainer->getRightInParent(), 0.0f);
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
startBounceChildren(orSpeed);
}
return true;
}
return false;
}
void UIScrollView::checkBounceBoundary()
{
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos > _bottomBoundary)
{
scrollToBottomEvent();
_bottomBounceNeeded = true;
}
else
{
_bottomBounceNeeded = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos < _topBoundary)
{
scrollToTopEvent();
_topBounceNeeded = true;
}
else
{
_topBounceNeeded = false;
}
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos < _rightBoundary)
{
scrollToRightEvent();
_rightBounceNeeded = true;
}
else
{
_rightBounceNeeded = false;
}
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos > _leftBoundary)
{
scrollToLeftEvent();
_leftBounceNeeded = true;
}
else
{
_leftBounceNeeded = false;
}
}
void UIScrollView::startBounceChildren(float v)
{
_bounceOriginalSpeed = v;
_bouncing = true;
}
void UIScrollView::stopBounceChildren()
{
_bouncing = false;
_bounceOriginalSpeed = 0.0f;
_leftBounceNeeded = false;
_rightBounceNeeded = false;
_topBounceNeeded = false;
_bottomBounceNeeded = false;
}
void UIScrollView::startAutoScrollChildrenWithOriginalSpeed(const cocos2d::Point& dir, float v, bool attenuated, float acceleration)
{
stopAutoScrollChildren();
_autoScrollDir = dir;
_isAutoScrollSpeedAttenuated = attenuated;
_autoScrollOriginalSpeed = v;
_autoScroll = true;
_autoScrollAcceleration = acceleration;
}
void UIScrollView::startAutoScrollChildrenWithDestination(const cocos2d::Point& des, float time, bool attenuated)
{
_needCheckAutoScrollDestination = false;
_autoScrollDestination = des;
cocos2d::Point dis = des - _innerContainer->getPosition();
cocos2d::Point dir = dis.normalize();
float orSpeed = 0.0f;
float acceleration = -1000.0f;
if (attenuated)
{
acceleration = (-(2 * dis.getLength())) / (time * time);
orSpeed = 2 * dis.getLength() / time;
}
else
{
_needCheckAutoScrollDestination = true;
orSpeed = dis.getLength() / time;
}
startAutoScrollChildrenWithOriginalSpeed(dir, orSpeed, attenuated, acceleration);
}
void UIScrollView::jumpToDestination(const cocos2d::Point &des)
{
float finalOffsetX = des.x;
float finalOffsetY = des.y;
switch (_direction)
{
case SCROLLVIEW_DIR_VERTICAL:
if (des.y <= 0)
{
finalOffsetY = MAX(des.y, _size.height - _innerContainer->getSize().height);
}
break;
case SCROLLVIEW_DIR_HORIZONTAL:
if (des.x <= 0)
{
finalOffsetX = MAX(des.x, _size.width - _innerContainer->getSize().width);
}
break;
case SCROLLVIEW_DIR_BOTH:
if (des.y <= 0)
{
finalOffsetY = MAX(des.y, _size.height - _innerContainer->getSize().height);
}
if (des.x <= 0)
{
finalOffsetX = MAX(des.x, _size.width - _innerContainer->getSize().width);
}
break;
default:
break;
}
_innerContainer->setPosition(cocos2d::Point(finalOffsetX, finalOffsetY));
}
void UIScrollView::stopAutoScrollChildren()
{
_autoScroll = false;
_autoScrollOriginalSpeed = 0.0f;
_autoScrollAddUpTime = 0.0f;
}
bool UIScrollView::bounceScrollChildren(float touchOffsetX, float touchOffsetY)
{
bool scrollenabled = true;
if (touchOffsetX > 0.0f && touchOffsetY > 0.0f) //first quadrant //bounce to top-right
{
float realOffsetX = touchOffsetX;
float realOffsetY = touchOffsetY;
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + realOffsetX >= _rightBoundary)
{
realOffsetX = _rightBoundary - icRightPos;
bounceRightEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY >= _topBoundary)
{
realOffsetY = _topBoundary - icTopPos;
bounceTopEvent();
scrollenabled = false;
}
moveChildren(realOffsetX, realOffsetY);
}
else if(touchOffsetX < 0.0f && touchOffsetY > 0.0f) //second quadrant //bounce to top-left
{
float realOffsetX = touchOffsetX;
float realOffsetY = touchOffsetY;
float icLefrPos = _innerContainer->getLeftInParent();
if (icLefrPos + realOffsetX <= _leftBoundary)
{
realOffsetX = _leftBoundary - icLefrPos;
bounceLeftEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY >= _topBoundary)
{
realOffsetY = _topBoundary - icTopPos;
bounceTopEvent();
scrollenabled = false;
}
moveChildren(realOffsetX, realOffsetY);
}
else if (touchOffsetX < 0.0f && touchOffsetY < 0.0f) //third quadrant //bounce to bottom-left
{
float realOffsetX = touchOffsetX;
float realOffsetY = touchOffsetY;
float icLefrPos = _innerContainer->getLeftInParent();
if (icLefrPos + realOffsetX <= _leftBoundary)
{
realOffsetX = _leftBoundary - icLefrPos;
bounceLeftEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY <= _bottomBoundary)
{
realOffsetY = _bottomBoundary - icBottomPos;
bounceBottomEvent();
scrollenabled = false;
}
moveChildren(realOffsetX, realOffsetY);
}
else if (touchOffsetX > 0.0f && touchOffsetY < 0.0f) //forth quadrant //bounce to bottom-right
{
float realOffsetX = touchOffsetX;
float realOffsetY = touchOffsetY;
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + realOffsetX >= _rightBoundary)
{
realOffsetX = _rightBoundary - icRightPos;
bounceRightEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY <= _bottomBoundary)
{
realOffsetY = _bottomBoundary - icBottomPos;
bounceBottomEvent();
scrollenabled = false;
}
moveChildren(realOffsetX, realOffsetY);
}
else if (touchOffsetX == 0.0f && touchOffsetY > 0.0f) // bounce to top
{
float realOffsetY = touchOffsetY;
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY >= _topBoundary)
{
realOffsetY = _topBoundary - icTopPos;
bounceTopEvent();
scrollenabled = false;
}
moveChildren(0.0f, realOffsetY);
}
else if (touchOffsetX == 0.0f && touchOffsetY < 0.0f) //bounce to bottom
{
float realOffsetY = touchOffsetY;
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY <= _bottomBoundary)
{
realOffsetY = _bottomBoundary - icBottomPos;
bounceBottomEvent();
scrollenabled = false;
}
moveChildren(0.0f, realOffsetY);
}
else if (touchOffsetX > 0.0f && touchOffsetY == 0.0f) //bounce to right
{
float realOffsetX = touchOffsetX;
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + realOffsetX >= _rightBoundary)
{
realOffsetX = _rightBoundary - icRightPos;
bounceRightEvent();
scrollenabled = false;
}
moveChildren(realOffsetX, 0.0f);
}
else if (touchOffsetX < 0.0f && touchOffsetY == 0.0f) //bounce to left
{
float realOffsetX = touchOffsetX;
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + realOffsetX <= _leftBoundary)
{
realOffsetX = _leftBoundary - icLeftPos;
bounceLeftEvent();
scrollenabled = false;
}
moveChildren(realOffsetX, 0.0f);
}
return scrollenabled;
}
bool UIScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchOffsetY)
{
bool scrollenabled = true;
switch (_direction)
{
case SCROLLVIEW_DIR_VERTICAL: // vertical
{
if (_autoScrollDir.y > 0)
{
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + *touchOffsetY >= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icBottomPos;
scrollenabled = false;
}
}
else
{
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + *touchOffsetY <= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icBottomPos;
scrollenabled = false;
}
}
break;
}
case SCROLLVIEW_DIR_HORIZONTAL: // horizontal
{
if (_autoScrollDir.x > 0)
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + *touchOffsetX >= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icLeftPos;
scrollenabled = false;
}
}
else
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + *touchOffsetX <= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icLeftPos;
scrollenabled = false;
}
}
break;
}
case SCROLLVIEW_DIR_BOTH:
{
if (*touchOffsetX > 0.0f && *touchOffsetY > 0.0f) // up right
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + *touchOffsetX >= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icLeftPos;
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + *touchOffsetY >= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icBottomPos;
scrollenabled = false;
}
}
else if (*touchOffsetX < 0.0f && *touchOffsetY > 0.0f) // up left
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + *touchOffsetX <= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icRightPos;
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + *touchOffsetY >= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icBottomPos;
scrollenabled = false;
}
}
else if (*touchOffsetX < 0.0f && *touchOffsetY < 0.0f) // down left
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + *touchOffsetX <= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icRightPos;
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + *touchOffsetY <= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icTopPos;
scrollenabled = false;
}
}
else if (*touchOffsetX > 0.0f && *touchOffsetY < 0.0f) // down right
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + *touchOffsetX >= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icLeftPos;
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + *touchOffsetY <= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icTopPos;
scrollenabled = false;
}
}
else if (*touchOffsetX == 0.0f && *touchOffsetY > 0.0f) // up
{
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + *touchOffsetY >= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icBottomPos;
scrollenabled = false;
}
}
else if (*touchOffsetX < 0.0f && *touchOffsetY == 0.0f) // left
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + *touchOffsetX <= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icRightPos;
scrollenabled = false;
}
}
else if (*touchOffsetX == 0.0f && *touchOffsetY < 0.0f) // down
{
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + *touchOffsetY <= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icTopPos;
scrollenabled = false;
}
}
else if (*touchOffsetX > 0.0f && *touchOffsetY == 0.0f) // right
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + *touchOffsetX >= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icLeftPos;
scrollenabled = false;
}
}
break;
}
default:
break;
}
return scrollenabled;
}
2013-09-13 22:20:20 +08:00
2013-11-06 16:04:06 +08:00
bool UIScrollView::scrollChildren(float touchOffsetX, float touchOffsetY)
{
bool scrollenabled = true;
scrollingEvent();
switch (_direction)
{
case SCROLLVIEW_DIR_VERTICAL: // vertical
{
float realOffset = touchOffsetY;
if (_bounceEnabled)
{
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bounceBottomBoundary)
{
realOffset = _bounceBottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _bounceTopBoundary)
{
realOffset = _bounceTopBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else
{
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bottomBoundary)
{
realOffset = _bottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _topBoundary)
{
realOffset = _topBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
moveChildren(0.0f, realOffset);
break;
}
case SCROLLVIEW_DIR_HORIZONTAL: // horizontal
{
float realOffset = touchOffsetX;
if (_bounceEnabled)
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _bounceRightBoundary)
{
realOffset = _bounceRightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _bounceLeftBoundary)
{
realOffset = _bounceLeftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
}
else
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _rightBoundary)
{
realOffset = _rightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _leftBoundary)
{
realOffset = _leftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
}
moveChildren(realOffset, 0.0f);
break;
}
case SCROLLVIEW_DIR_BOTH:
{
float realOffsetX = touchOffsetX;
float realOffsetY = touchOffsetY;
if (_bounceEnabled)
{
if (touchOffsetX > 0.0f && touchOffsetY > 0.0f) // up right
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _bounceLeftBoundary)
{
realOffsetX = _bounceLeftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bounceBottomBoundary)
{
realOffsetY = _bounceBottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY > 0.0f) // up left
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _bounceRightBoundary)
{
realOffsetX = _bounceRightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bounceBottomBoundary)
{
realOffsetY = _bounceBottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY < 0.0f) // down left
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _bounceRightBoundary)
{
realOffsetX = _bounceRightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _bounceTopBoundary)
{
realOffsetY = _bounceTopBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX > 0.0f && touchOffsetY < 0.0f) // down right
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _bounceLeftBoundary)
{
realOffsetX = _bounceLeftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _bounceTopBoundary)
{
realOffsetY = _bounceTopBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX == 0.0f && touchOffsetY > 0.0f) // up
{
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bounceBottomBoundary)
{
realOffsetY = _bounceBottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY == 0.0f) // left
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _bounceRightBoundary)
{
realOffsetX = _bounceRightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
}
else if (touchOffsetX == 0.0f && touchOffsetY < 0.0f) // down
{
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _bounceTopBoundary)
{
realOffsetY = _bounceTopBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX > 0.0f && touchOffsetY == 0.0f) // right
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _bounceLeftBoundary)
{
realOffsetX = _bounceLeftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
}
}
else
{
if (touchOffsetX > 0.0f && touchOffsetY > 0.0f) // up right
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _leftBoundary)
{
realOffsetX = _leftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bottomBoundary)
{
realOffsetY = _bottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY > 0.0f) // up left
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _rightBoundary)
{
realOffsetX = _rightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bottomBoundary)
{
realOffsetY = _bottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY < 0.0f) // down left
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _rightBoundary)
{
realOffsetX = _rightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _topBoundary)
{
realOffsetY = _topBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX > 0.0f && touchOffsetY < 0.0f) // down right
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _leftBoundary)
{
realOffsetX = _leftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _topBoundary)
{
realOffsetY = _topBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX == 0.0f && touchOffsetY > 0.0f) // up
{
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bottomBoundary)
{
realOffsetY = _bottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY == 0.0f) // left
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _rightBoundary)
{
realOffsetX = _rightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
}
else if (touchOffsetX == 0.0f && touchOffsetY < 0.0f) // down
{
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _topBoundary)
{
realOffsetY = _topBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX > 0.0f && touchOffsetY == 0.0f) // right
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _leftBoundary)
{
realOffsetX = _leftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
}
}
moveChildren(realOffsetX, realOffsetY);
break;
}
default:
break;
}
return scrollenabled;
}
void UIScrollView::scrollToBottom(float time, bool attenuated)
{
startAutoScrollChildrenWithDestination(cocos2d::Point(_innerContainer->getPosition().x, 0.0f), time, attenuated);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::scrollToTop(float time, bool attenuated)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
startAutoScrollChildrenWithDestination(cocos2d::Point(_innerContainer->getPosition().x, _size.height - _innerContainer->getSize().height), time, attenuated);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::scrollToLeft(float time, bool attenuated)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
startAutoScrollChildrenWithDestination(cocos2d::Point(0.0f, _innerContainer->getPosition().y), time, attenuated);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::scrollToRight(float time, bool attenuated)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
startAutoScrollChildrenWithDestination(cocos2d::Point(_size.width - _innerContainer->getSize().width, _innerContainer->getPosition().y), time, attenuated);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::scrollToTopLeft(float time, bool attenuated)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (_direction != SCROLLVIEW_DIR_BOTH)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
CCLOG("Scroll diretion is not both!");
return;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
startAutoScrollChildrenWithDestination(cocos2d::Point(0.0f, _size.height - _innerContainer->getSize().height), time, attenuated);
}
void UIScrollView::scrollToTopRight(float time, bool attenuated)
{
if (_direction != SCROLLVIEW_DIR_BOTH)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
CCLOG("Scroll diretion is not both!");
return;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
startAutoScrollChildrenWithDestination(cocos2d::Point(_size.width - _innerContainer->getSize().width, _size.height - _innerContainer->getSize().height), time, attenuated);
}
void UIScrollView::scrollToBottomLeft(float time, bool attenuated)
{
if (_direction != SCROLLVIEW_DIR_BOTH)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
CCLOG("Scroll diretion is not both!");
return;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
startAutoScrollChildrenWithDestination(cocos2d::Point::ZERO, time, attenuated);
}
void UIScrollView::scrollToBottomRight(float time, bool attenuated)
{
if (_direction != SCROLLVIEW_DIR_BOTH)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
CCLOG("Scroll diretion is not both!");
return;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
startAutoScrollChildrenWithDestination(cocos2d::Point(_size.width - _innerContainer->getSize().width, 0.0f), time, attenuated);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::scrollToPercentVertical(float percent, float time, bool attenuated)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
float minY = _size.height - _innerContainer->getSize().height;
float h = - minY;
startAutoScrollChildrenWithDestination(cocos2d::Point(_innerContainer->getPosition().x, minY + percent * h / 100.0f), time, attenuated);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::scrollToPercentHorizontal(float percent, float time, bool attenuated)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
float w = _innerContainer->getSize().width - _size.width;
startAutoScrollChildrenWithDestination(cocos2d::Point(-(percent * w / 100.0f), _innerContainer->getPosition().y), time, attenuated);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::scrollToPercentBothDirection(const cocos2d::Point& percent, float time, bool attenuated)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (_direction != SCROLLVIEW_DIR_BOTH)
{
return;
}
float minY = _size.height - _innerContainer->getSize().height;
float h = - minY;
float w = _innerContainer->getSize().width - _size.width;
startAutoScrollChildrenWithDestination(cocos2d::Point(-(percent.x * w / 100.0f), minY + percent.y * h / 100.0f), time, attenuated);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::jumpToBottom()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
jumpToDestination(cocos2d::Point(_innerContainer->getPosition().x, 0.0f));
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::jumpToTop()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
jumpToDestination(cocos2d::Point(_innerContainer->getPosition().x, _size.height - _innerContainer->getSize().height));
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::jumpToLeft()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
jumpToDestination(cocos2d::Point(0.0f, _innerContainer->getPosition().y));
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::jumpToRight()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
jumpToDestination(cocos2d::Point(_size.width - _innerContainer->getSize().width, _innerContainer->getPosition().y));
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::jumpToTopLeft()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (_direction != SCROLLVIEW_DIR_BOTH)
{
CCLOG("Scroll diretion is not both!");
return;
}
jumpToDestination(cocos2d::Point(0.0f, _size.height - _innerContainer->getSize().height));
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::jumpToTopRight()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (_direction != SCROLLVIEW_DIR_BOTH)
{
CCLOG("Scroll diretion is not both!");
return;
}
jumpToDestination(cocos2d::Point(_size.width - _innerContainer->getSize().width, _size.height - _innerContainer->getSize().height));
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::jumpToBottomLeft()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (_direction != SCROLLVIEW_DIR_BOTH)
{
CCLOG("Scroll diretion is not both!");
return;
}
jumpToDestination(cocos2d::Point::ZERO);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::jumpToBottomRight()
{
if (_direction != SCROLLVIEW_DIR_BOTH)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
CCLOG("Scroll diretion is not both!");
return;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
jumpToDestination(cocos2d::Point(_size.width - _innerContainer->getSize().width, 0.0f));
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::jumpToPercentVertical(float percent)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
float minY = _size.height - _innerContainer->getSize().height;
float h = - minY;
jumpToDestination(cocos2d::Point(_innerContainer->getPosition().x, minY + percent * h / 100.0f));
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::jumpToPercentHorizontal(float percent)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
float w = _innerContainer->getSize().width - _size.width;
jumpToDestination(cocos2d::Point(-(percent * w / 100.0f), _innerContainer->getPosition().y));
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::jumpToPercentBothDirection(const cocos2d::Point& percent)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (_direction != SCROLLVIEW_DIR_BOTH)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-11-06 16:04:06 +08:00
float minY = _size.height - _innerContainer->getSize().height;
float h = - minY;
float w = _innerContainer->getSize().width - _size.width;
jumpToDestination(cocos2d::Point(-(percent.x * w / 100.0f), minY + percent.y * h / 100.0f));
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::startRecordSlidAction()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (_autoScroll)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
stopAutoScrollChildren();
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
if (_bouncing)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
stopBounceChildren();
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
_slidTime = 0.0f;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::endRecordSlidAction()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (!checkNeedBounce() && _inertiaScrollEnabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (_slidTime <= 0.016f)
{
return;
}
float totalDis = 0.0f;
cocos2d::Point dir;
switch (_direction)
{
case SCROLLVIEW_DIR_VERTICAL:
totalDis = _touchEndedPoint.y - _touchBeganPoint.y;
if (totalDis < 0.0f)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dir = SCROLLDIR_DOWN;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
else
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dir = SCROLLDIR_UP;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
break;
case SCROLLVIEW_DIR_HORIZONTAL:
totalDis = _touchEndedPoint.x - _touchBeganPoint.x;
if (totalDis < 0.0f)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dir = SCROLLDIR_LEFT;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
else
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dir = SCROLLDIR_RIGHT;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
break;
case SCROLLVIEW_DIR_BOTH:
{
cocos2d::Point subVector = _touchEndedPoint - _touchBeganPoint;
totalDis = subVector.getLength();
dir = subVector.normalize();
break;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
default:
break;
}
float orSpeed = MIN(fabs(totalDis)/(_slidTime), AUTOSCROLLMAXSPEED);
startAutoScrollChildrenWithOriginalSpeed(dir, orSpeed, true, -1000);
_slidTime = 0.0f;
2013-09-13 22:20:20 +08:00
}
}
2013-11-06 16:04:06 +08:00
void UIScrollView::handlePressLogic(const cocos2d::Point &touchPoint)
{
_touchBeganPoint = _renderer->convertToNodeSpace(touchPoint);
_touchMovingPoint = _touchBeganPoint;
startRecordSlidAction();
_bePressed = true;
}
void UIScrollView::handleMoveLogic(const cocos2d::Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
_touchMovedPoint = _renderer->convertToNodeSpace(touchPoint);
cocos2d::Point delta = _touchMovedPoint - _touchMovingPoint;
_touchMovingPoint = _touchMovedPoint;
2013-09-16 20:54:13 +08:00
switch (_direction)
2013-09-13 22:20:20 +08:00
{
case SCROLLVIEW_DIR_VERTICAL: // vertical
2013-11-06 16:04:06 +08:00
{
scrollChildren(0.0f, delta.y);
2013-09-13 22:20:20 +08:00
break;
2013-11-06 16:04:06 +08:00
}
2013-09-13 22:20:20 +08:00
case SCROLLVIEW_DIR_HORIZONTAL: // horizontal
2013-11-06 16:04:06 +08:00
{
scrollChildren(delta.x, 0.0f);
2013-09-13 22:20:20 +08:00
break;
2013-11-06 16:04:06 +08:00
}
case SCROLLVIEW_DIR_BOTH: // both
{
scrollChildren(delta.x, delta.y);
break;
}
2013-09-13 22:20:20 +08:00
default:
break;
}
2013-11-06 16:04:06 +08:00
}
void UIScrollView::handleReleaseLogic(const cocos2d::Point &touchPoint)
{
_touchEndedPoint = _renderer->convertToNodeSpace(touchPoint);
2013-09-13 22:20:20 +08:00
endRecordSlidAction();
2013-11-06 16:04:06 +08:00
_bePressed = false;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
bool UIScrollView::onTouchBegan(const cocos2d::Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
bool pass = UILayout::onTouchBegan(touchPoint);
2013-09-13 22:20:20 +08:00
handlePressLogic(touchPoint);
return pass;
}
2013-11-06 16:04:06 +08:00
void UIScrollView::onTouchMoved(const cocos2d::Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
UILayout::onTouchMoved(touchPoint);
2013-09-13 22:20:20 +08:00
handleMoveLogic(touchPoint);
}
2013-11-06 16:04:06 +08:00
void UIScrollView::onTouchEnded(const cocos2d::Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
UILayout::onTouchEnded(touchPoint);
2013-09-13 22:20:20 +08:00
handleReleaseLogic(touchPoint);
}
2013-11-06 16:04:06 +08:00
void UIScrollView::onTouchCancelled(const cocos2d::Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
UILayout::onTouchCancelled(touchPoint);
handleReleaseLogic(touchPoint);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::onTouchLongClicked(const cocos2d::Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
}
void UIScrollView::update(float dt)
{
2013-09-16 20:54:13 +08:00
if (_autoScroll)
2013-09-13 22:20:20 +08:00
{
autoScrollChildren(dt);
}
2013-11-06 16:04:06 +08:00
if (_bouncing)
{
bounceChildren(dt);
}
2013-09-13 22:20:20 +08:00
recordSlidTime(dt);
}
void UIScrollView::recordSlidTime(float dt)
{
2013-09-16 20:54:13 +08:00
if (_bePressed)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_slidTime += dt;
2013-09-13 22:20:20 +08:00
}
}
2013-11-06 16:04:06 +08:00
void UIScrollView::interceptTouchEvent(int handleState, UIWidget *sender, const cocos2d::Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
switch (handleState)
{
case 0:
handlePressLogic(touchPoint);
break;
case 1:
{
2013-11-06 16:04:06 +08:00
float offset = (sender->getTouchStartPos() - touchPoint).getLength();
2013-09-16 20:54:13 +08:00
if (offset > _childFocusCancelOffset)
2013-09-13 22:20:20 +08:00
{
sender->setFocused(false);
handleMoveLogic(touchPoint);
}
}
break;
case 2:
handleReleaseLogic(touchPoint);
break;
case 3:
2013-11-06 16:04:06 +08:00
handleReleaseLogic(touchPoint);
2013-09-13 22:20:20 +08:00
break;
}
}
2013-11-06 16:04:06 +08:00
void UIScrollView::checkChildInfo(int handleState,UIWidget* sender,const cocos2d::Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
interceptTouchEvent(handleState, sender, touchPoint);
}
void UIScrollView::scrollToTopEvent()
{
2013-11-08 18:43:06 +08:00
if (_scrollViewEventListener && _scrollViewEventSelector)
2013-09-13 22:20:20 +08:00
{
2013-11-08 18:43:06 +08:00
(_scrollViewEventListener->*_scrollViewEventSelector)(this, SCROLLVIEW_EVENT_SCROLL_TO_TOP);
2013-09-13 22:20:20 +08:00
}
}
void UIScrollView::scrollToBottomEvent()
{
2013-11-08 18:43:06 +08:00
if (_scrollViewEventListener && _scrollViewEventSelector)
2013-09-13 22:20:20 +08:00
{
2013-11-08 18:43:06 +08:00
(_scrollViewEventListener->*_scrollViewEventSelector)(this, SCROLLVIEW_EVENT_SCROLL_TO_BOTTOM);
2013-09-13 22:20:20 +08:00
}
}
void UIScrollView::scrollToLeftEvent()
{
2013-11-08 18:43:06 +08:00
if (_scrollViewEventListener && _scrollViewEventSelector)
2013-09-13 22:20:20 +08:00
{
2013-11-08 18:43:06 +08:00
(_scrollViewEventListener->*_scrollViewEventSelector)(this, SCROLLVIEW_EVENT_SCROLL_TO_LEFT);
2013-09-13 22:20:20 +08:00
}
}
void UIScrollView::scrollToRightEvent()
{
2013-11-08 18:43:06 +08:00
if (_scrollViewEventListener && _scrollViewEventSelector)
2013-09-13 22:20:20 +08:00
{
2013-11-08 18:43:06 +08:00
(_scrollViewEventListener->*_scrollViewEventSelector)(this, SCROLLVIEW_EVENT_SCROLL_TO_RIGHT);
2013-09-13 22:20:20 +08:00
}
}
2013-11-06 16:04:06 +08:00
void UIScrollView::scrollingEvent()
{
2013-11-08 18:43:06 +08:00
if (_scrollViewEventListener && _scrollViewEventSelector)
2013-11-06 16:04:06 +08:00
{
2013-11-08 18:43:06 +08:00
(_scrollViewEventListener->*_scrollViewEventSelector)(this, SCROLLVIEW_EVENT_SCROLLING);
2013-11-06 16:04:06 +08:00
}
}
void UIScrollView::bounceTopEvent()
{
2013-11-08 18:43:06 +08:00
if (_scrollViewEventListener && _scrollViewEventSelector)
2013-11-06 16:04:06 +08:00
{
2013-11-08 18:43:06 +08:00
(_scrollViewEventListener->*_scrollViewEventSelector)(this, SCROLLVIEW_EVENT_BOUNCE_TOP);
2013-11-06 16:04:06 +08:00
}
}
void UIScrollView::bounceBottomEvent()
{
2013-11-08 18:43:06 +08:00
if (_scrollViewEventListener && _scrollViewEventSelector)
2013-11-06 16:04:06 +08:00
{
2013-11-08 18:43:06 +08:00
(_scrollViewEventListener->*_scrollViewEventSelector)(this, SCROLLVIEW_EVENT_BOUNCE_BOTTOM);
2013-11-06 16:04:06 +08:00
}
}
void UIScrollView::bounceLeftEvent()
{
2013-11-08 18:43:06 +08:00
if (_scrollViewEventListener && _scrollViewEventSelector)
2013-11-06 16:04:06 +08:00
{
2013-11-08 18:43:06 +08:00
(_scrollViewEventListener->*_scrollViewEventSelector)(this, SCROLLVIEW_EVENT_BOUNCE_LEFT);
2013-11-06 16:04:06 +08:00
}
}
void UIScrollView::bounceRightEvent()
{
2013-11-08 18:43:06 +08:00
if (_scrollViewEventListener && _scrollViewEventSelector)
2013-11-06 16:04:06 +08:00
{
2013-11-08 18:43:06 +08:00
(_scrollViewEventListener->*_scrollViewEventSelector)(this, SCROLLVIEW_EVENT_BOUNCE_RIGHT);
2013-11-06 16:04:06 +08:00
}
}
2013-11-08 18:43:06 +08:00
void UIScrollView::addEventListenerScrollView(cocos2d::Object *target, SEL_ScrollViewEvent selector)
2013-09-13 22:20:20 +08:00
{
2013-11-08 18:43:06 +08:00
_scrollViewEventListener = target;
_scrollViewEventSelector = selector;
2013-09-13 22:20:20 +08:00
}
void UIScrollView::setDirection(SCROLLVIEW_DIR dir)
{
2013-09-16 20:54:13 +08:00
_direction = dir;
2013-09-13 22:20:20 +08:00
}
SCROLLVIEW_DIR UIScrollView::getDirection()
{
2013-09-16 20:54:13 +08:00
return _direction;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIScrollView::setBounceEnabled(bool enabled)
{
_bounceEnabled = enabled;
}
bool UIScrollView::isBounceEnabled() const
{
return _bounceEnabled;
}
void UIScrollView::setInertiaScrollEnabled(bool enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
_inertiaScrollEnabled = enabled;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
bool UIScrollView::isInertiaScrollEnabled() const
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
return _inertiaScrollEnabled;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
UILayout* UIScrollView::getInnerContainer()
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
return _innerContainer;
2013-09-13 22:20:20 +08:00
}
2013-09-23 19:16:20 +08:00
void UIScrollView::setLayoutType(LayoutType type)
2013-09-13 22:20:20 +08:00
{
2013-09-23 19:16:20 +08:00
_innerContainer->setLayoutType(type);
2013-09-13 22:20:20 +08:00
}
2013-09-23 19:16:20 +08:00
LayoutType UIScrollView::getLayoutType() const
2013-09-13 22:20:20 +08:00
{
2013-09-23 19:16:20 +08:00
return _innerContainer->getLayoutType();
}
void UIScrollView::doLayout()
{
_innerContainer->doLayout();
2013-09-13 22:20:20 +08:00
}
2013-09-17 17:59:20 +08:00
const char* UIScrollView::getDescription() const
{
return "ScrollView";
}
2013-11-06 16:04:06 +08:00
UIWidget* UIScrollView::createCloneInstance()
{
return UIScrollView::create();
}
void UIScrollView::copyClonedWidgetChildren(UIWidget* model)
{
UILayout::copyClonedWidgetChildren(model);
}
void UIScrollView::copySpecialProperties(UIWidget *widget)
{
UIScrollView* scrollView = dynamic_cast<UIScrollView*>(widget);
if (scrollView)
{
UILayout::copySpecialProperties(widget);
setInnerContainerSize(scrollView->getInnerContainerSize());
setDirection(scrollView->_direction);
setBounceEnabled(scrollView->_bounceEnabled);
setInertiaScrollEnabled(scrollView->_inertiaScrollEnabled);
}
}
}