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 "UIScrollView.h"
|
|
|
|
#include "../../System/UILayer.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
|
|
|
UIScrollView::UIScrollView():
|
2013-09-17 20:34:26 +08:00
|
|
|
_innerContainer(NULL),
|
2013-09-16 20:54:13 +08:00
|
|
|
_direction(SCROLLVIEW_DIR_VERTICAL),
|
|
|
|
_moveDirection(SCROLLVIEW_MOVE_DIR_NONE),
|
|
|
|
_touchStartLocation(0.0f),
|
|
|
|
_touchEndLocation(0.0f),
|
|
|
|
_touchMoveStartLocation(0.0f),
|
|
|
|
_topBoundary(0.0f),
|
|
|
|
_bottomBoundary(0.0f),
|
|
|
|
_leftBoundary(0.0f),
|
|
|
|
_rightBoundary(0.0f),
|
|
|
|
_topEnd(false),
|
|
|
|
_bottomEnd(false),
|
|
|
|
_leftEnd(false),
|
|
|
|
_rightEnd(false),
|
|
|
|
_autoScroll(false),
|
|
|
|
_autoScrollOriginalSpeed(0.0f),
|
|
|
|
_autoScrollAcceleration(600.0f),
|
|
|
|
_bePressed(false),
|
|
|
|
_slidTime(0.0f),
|
|
|
|
_moveChildPoint(Point::ZERO),
|
|
|
|
_childFocusCancelOffset(5.0f),
|
2013-09-17 20:34:26 +08:00
|
|
|
_eventListener(NULL),
|
|
|
|
_eventSelector(NULL)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UIScrollView::~UIScrollView()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UIScrollView* UIScrollView::create()
|
|
|
|
{
|
|
|
|
UIScrollView* widget = new UIScrollView();
|
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::releaseResoures()
|
|
|
|
{
|
|
|
|
setUpdateEnabled(false);
|
|
|
|
removeAllChildren();
|
2013-09-16 15:32:52 +08:00
|
|
|
_renderer->removeAllChildrenWithCleanup(true);
|
|
|
|
_renderer->removeFromParentAndCleanup(true);
|
|
|
|
_renderer->release();
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
Layout::removeChild(_innerContainer);
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-09-16 15:32:52 +08:00
|
|
|
_children->release();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UIScrollView::init()
|
|
|
|
{
|
|
|
|
if (Layout::init())
|
|
|
|
{
|
|
|
|
setUpdateEnabled(true);
|
|
|
|
setTouchEnabled(true);
|
|
|
|
setClippingEnabled(true);
|
2013-09-16 20:54:13 +08:00
|
|
|
_innerContainer->setTouchEnabled(false);
|
2013-09-13 22:20:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::initRenderer()
|
|
|
|
{
|
|
|
|
Layout::initRenderer();
|
2013-09-16 20:54:13 +08:00
|
|
|
_innerContainer = Layout::create();
|
|
|
|
Layout::addChild(_innerContainer);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::onSizeChanged()
|
|
|
|
{
|
|
|
|
Layout::onSizeChanged();
|
2013-09-16 20:54:13 +08:00
|
|
|
_topBoundary = _size.height;
|
|
|
|
_rightBoundary = _size.width;
|
|
|
|
Size innerSize = _innerContainer->getSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
float orginInnerSizeWidth = innerSize.width;
|
|
|
|
float orginInnerSizeHeight = innerSize.height;
|
2013-09-16 15:32:52 +08:00
|
|
|
float innerSizeWidth = MAX(orginInnerSizeWidth, _size.width);
|
|
|
|
float innerSizeHeight = MAX(orginInnerSizeHeight, _size.height);
|
2013-09-16 20:54:13 +08:00
|
|
|
_innerContainer->setSize(Size(innerSizeWidth, innerSizeHeight));
|
|
|
|
_innerContainer->setPosition(Point(0, _size.height - _innerContainer->getSize().height));
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::setInnerContainerSize(const Size &size)
|
|
|
|
{
|
2013-09-16 15:32:52 +08:00
|
|
|
float innerSizeWidth = _size.width;
|
|
|
|
float innerSizeHeight = _size.height;
|
|
|
|
if (size.width < _size.width)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
CCLOG("Inner width <= scrollview width, it will be force sized!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
innerSizeWidth = size.width;
|
|
|
|
}
|
2013-09-16 15:32:52 +08:00
|
|
|
if (size.height < _size.height)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
CCLOG("Inner height <= scrollview height, it will be force sized!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
innerSizeHeight = size.height;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
_innerContainer->setSize(Size(innerSizeWidth, innerSizeHeight));
|
|
|
|
_innerContainer->setPosition(Point(0, _size.height - _innerContainer->getSize().height));
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Size& UIScrollView::getInnerContainerSize() const
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _innerContainer->getSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UIScrollView::addChild(UIWidget* widget)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _innerContainer->addChild(widget);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::removeAllChildren()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_innerContainer->removeAllChildren();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UIScrollView::removeChild(UIWidget* child)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _innerContainer->removeChild(child);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Array* UIScrollView::getChildren()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _innerContainer->getChildren();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::moveChildren(float offset)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_direction)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case SCROLLVIEW_DIR_VERTICAL: // vertical
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_moveChildPoint.x = _innerContainer->getPosition().x;
|
|
|
|
_moveChildPoint.y = _innerContainer->getPosition().y + offset;
|
|
|
|
_innerContainer->setPosition(_moveChildPoint);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SCROLLVIEW_DIR_HORIZONTAL: // horizontal
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_moveChildPoint.x = _innerContainer->getPosition().x + offset;
|
|
|
|
_moveChildPoint.y = _innerContainer->getPosition().y;
|
|
|
|
_innerContainer->setPosition(_moveChildPoint);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::autoScrollChildren(float dt)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_direction)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case SCROLLVIEW_DIR_VERTICAL: // vertical
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_moveDirection)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case SCROLLVIEW_MOVE_DIR_UP: // up
|
|
|
|
{
|
|
|
|
float curDis = getCurAutoScrollDistance(dt);
|
|
|
|
if (curDis <= 0)
|
|
|
|
{
|
|
|
|
curDis = 0;
|
|
|
|
stopAutoScrollChildren();
|
|
|
|
}
|
|
|
|
if (!scrollChildren(curDis))
|
|
|
|
{
|
|
|
|
stopAutoScrollChildren();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCROLLVIEW_MOVE_DIR_DOWN: // down
|
|
|
|
{
|
|
|
|
float curDis = getCurAutoScrollDistance(dt);
|
|
|
|
if (curDis <= 0)
|
|
|
|
{
|
|
|
|
curDis = 0;
|
|
|
|
stopAutoScrollChildren();
|
|
|
|
}
|
|
|
|
if (!scrollChildren(-curDis))
|
|
|
|
{
|
|
|
|
stopAutoScrollChildren();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCROLLVIEW_DIR_HORIZONTAL: // horizontal
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_moveDirection)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case SCROLLVIEW_MOVE_DIR_LEFT: // left
|
|
|
|
{
|
|
|
|
float curDis = getCurAutoScrollDistance(dt);
|
|
|
|
if (curDis <= 0)
|
|
|
|
{
|
|
|
|
curDis = 0;
|
|
|
|
stopAutoScrollChildren();
|
|
|
|
}
|
|
|
|
if (!scrollChildren(-curDis))
|
|
|
|
{
|
|
|
|
stopAutoScrollChildren();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCROLLVIEW_MOVE_DIR_RIGHT: // right
|
|
|
|
{
|
|
|
|
float curDis = getCurAutoScrollDistance(dt);
|
|
|
|
if (curDis <= 0)
|
|
|
|
{
|
|
|
|
curDis = 0;
|
|
|
|
stopAutoScrollChildren();
|
|
|
|
}
|
|
|
|
if (!scrollChildren(curDis))
|
|
|
|
{
|
|
|
|
stopAutoScrollChildren();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::startAutoScrollChildren(float v)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_autoScrollOriginalSpeed = v;
|
|
|
|
_autoScroll = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::stopAutoScrollChildren()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_autoScroll = false;
|
|
|
|
_autoScrollOriginalSpeed = 0.0f;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float UIScrollView::getCurAutoScrollDistance(float time)
|
|
|
|
{
|
|
|
|
float dt = time;
|
2013-09-16 20:54:13 +08:00
|
|
|
_autoScrollOriginalSpeed -= _autoScrollAcceleration*dt;
|
|
|
|
return _autoScrollOriginalSpeed*dt;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UIScrollView::scrollChildren(float touchOffset)
|
|
|
|
{
|
|
|
|
float realOffset = touchOffset;
|
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_direction)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case SCROLLVIEW_DIR_VERTICAL: // vertical
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_moveDirection)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case SCROLLVIEW_MOVE_DIR_UP: // up
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
float icBottomPos = _innerContainer->getBottomInParent();
|
|
|
|
if (icBottomPos + touchOffset >= _bottomBoundary)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
realOffset = _bottomBoundary - icBottomPos;
|
2013-09-13 22:20:20 +08:00
|
|
|
moveChildren(realOffset);
|
2013-09-16 20:54:13 +08:00
|
|
|
_bottomEnd = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
scrollToBottomEvent();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SCROLLVIEW_MOVE_DIR_DOWN: // down
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
float icTopPos = _innerContainer->getTopInParent();
|
|
|
|
if (icTopPos + touchOffset <= _topBoundary)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
realOffset = _topBoundary - icTopPos;
|
2013-09-13 22:20:20 +08:00
|
|
|
moveChildren(realOffset);
|
2013-09-16 20:54:13 +08:00
|
|
|
_topEnd = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
scrollToTopEvent();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
moveChildren(realOffset);
|
2013-09-16 20:54:13 +08:00
|
|
|
_topEnd = false;
|
|
|
|
_bottomEnd = false;
|
2013-09-13 22:20:20 +08:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case SCROLLVIEW_DIR_HORIZONTAL: // horizontal
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_moveDirection)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case SCROLLVIEW_MOVE_DIR_LEFT: // left
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
float icRightPos = _innerContainer->getRightInParent();
|
|
|
|
if (icRightPos + touchOffset <= _rightBoundary)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
realOffset = _rightBoundary - icRightPos;
|
2013-09-13 22:20:20 +08:00
|
|
|
moveChildren(realOffset);
|
2013-09-16 20:54:13 +08:00
|
|
|
_rightEnd = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
scrollToRightEvent();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SCROLLVIEW_MOVE_DIR_RIGHT: // right
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
float icLeftPos = _innerContainer->getLeftInParent();
|
|
|
|
if (icLeftPos + touchOffset >= _leftBoundary)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
realOffset = _leftBoundary - icLeftPos;
|
2013-09-13 22:20:20 +08:00
|
|
|
moveChildren(realOffset);
|
2013-09-16 20:54:13 +08:00
|
|
|
_leftEnd = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
scrollToLeftEvent();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
moveChildren(realOffset);
|
2013-09-16 20:54:13 +08:00
|
|
|
_leftEnd = false;
|
|
|
|
_rightEnd = false;
|
2013-09-13 22:20:20 +08:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::scrollToBottom()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_moveDirection = SCROLLVIEW_MOVE_DIR_UP; // up
|
|
|
|
scrollChildren(_innerContainer->getSize().height);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::scrollToTop()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_moveDirection = SCROLLVIEW_MOVE_DIR_DOWN; // down
|
|
|
|
scrollChildren(-_innerContainer->getSize().height);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::startRecordSlidAction()
|
|
|
|
{
|
2013-09-16 15:32:52 +08:00
|
|
|
if (_children->count() <= 0)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_autoScroll){
|
2013-09-13 22:20:20 +08:00
|
|
|
stopAutoScrollChildren();
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
_bePressed = true;
|
|
|
|
_slidTime = 0.0;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::endRecordSlidAction()
|
|
|
|
{
|
2013-09-16 15:32:52 +08:00
|
|
|
if (_children->count() <= 0)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_slidTime <= 0.016f)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
float totalDis = 0;
|
2013-09-16 20:54:13 +08:00
|
|
|
totalDis = _touchEndLocation-_touchStartLocation;
|
|
|
|
float orSpeed = fabs(totalDis)/(_slidTime);
|
2013-09-13 22:20:20 +08:00
|
|
|
startAutoScrollChildren(orSpeed);
|
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
_bePressed = false;
|
|
|
|
_slidTime = 0.0;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::handlePressLogic(const Point &touchPoint)
|
|
|
|
{
|
2013-09-16 15:32:52 +08:00
|
|
|
Point nsp = _renderer->convertToNodeSpace(touchPoint);
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_direction)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case SCROLLVIEW_DIR_VERTICAL: // vertical
|
2013-09-16 20:54:13 +08:00
|
|
|
_touchMoveStartLocation = nsp.y;
|
|
|
|
_touchStartLocation = nsp.y;
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case SCROLLVIEW_DIR_HORIZONTAL: // horizontal
|
2013-09-16 20:54:13 +08:00
|
|
|
_touchMoveStartLocation = nsp.x;
|
|
|
|
_touchStartLocation = nsp.x;
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
startRecordSlidAction();
|
|
|
|
}
|
|
|
|
|
2013-09-16 15:32:52 +08:00
|
|
|
void UIScrollView::handleMoveLogic(const Point &touchPoint)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 15:32:52 +08:00
|
|
|
Point nsp = _renderer->convertToNodeSpace(touchPoint);
|
2013-09-13 22:20:20 +08:00
|
|
|
float offset = 0.0f;
|
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_direction)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case SCROLLVIEW_DIR_VERTICAL: // vertical
|
|
|
|
{
|
|
|
|
float moveY = nsp.y;
|
2013-09-16 20:54:13 +08:00
|
|
|
offset = moveY - _touchMoveStartLocation;
|
|
|
|
_touchMoveStartLocation = moveY;
|
2013-09-13 22:20:20 +08:00
|
|
|
|
|
|
|
if (offset < 0.0f)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_moveDirection = SCROLLVIEW_MOVE_DIR_DOWN; // down
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else if (offset > 0.0f)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_moveDirection = SCROLLVIEW_MOVE_DIR_UP; // up
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCROLLVIEW_DIR_HORIZONTAL: // horizontal
|
|
|
|
{
|
|
|
|
float moveX = nsp.x;
|
2013-09-16 20:54:13 +08:00
|
|
|
offset = moveX - _touchMoveStartLocation;
|
|
|
|
_touchMoveStartLocation = moveX;
|
2013-09-13 22:20:20 +08:00
|
|
|
|
|
|
|
if (offset < 0)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_moveDirection = SCROLLVIEW_MOVE_DIR_LEFT; // left
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else if (offset > 0)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_moveDirection = SCROLLVIEW_MOVE_DIR_RIGHT; // right
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
scrollChildren(offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::handleReleaseLogic(const Point &touchPoint)
|
|
|
|
{
|
2013-09-16 15:32:52 +08:00
|
|
|
Point nsp = _renderer->convertToNodeSpace(touchPoint);
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_direction)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case SCROLLVIEW_DIR_VERTICAL: // vertical
|
2013-09-16 20:54:13 +08:00
|
|
|
_touchEndLocation = nsp.y;
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SCROLLVIEW_DIR_HORIZONTAL: // horizontal
|
2013-09-16 20:54:13 +08:00
|
|
|
_touchEndLocation = nsp.x;
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
endRecordSlidAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UIScrollView::onTouchBegan(const Point &touchPoint)
|
|
|
|
{
|
|
|
|
bool pass = Layout::onTouchBegan(touchPoint);
|
|
|
|
handlePressLogic(touchPoint);
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::onTouchMoved(const Point &touchPoint)
|
|
|
|
{
|
|
|
|
Layout::onTouchMoved(touchPoint);
|
|
|
|
handleMoveLogic(touchPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::onTouchEnded(const Point &touchPoint)
|
|
|
|
{
|
|
|
|
Layout::onTouchEnded(touchPoint);
|
|
|
|
handleReleaseLogic(touchPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::onTouchCancelled(const Point &touchPoint)
|
|
|
|
{
|
|
|
|
Layout::onTouchCancelled(touchPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::onTouchLongClicked(const Point &touchPoint)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::interceptTouchEvent(int handleState, UIWidget *sender, const Point &touchPoint)
|
|
|
|
{
|
|
|
|
switch (handleState)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
handlePressLogic(touchPoint);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
float offset = 0;
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_direction)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case SCROLLVIEW_DIR_VERTICAL: // vertical
|
|
|
|
offset = fabs(sender->getTouchStartPos().y - touchPoint.y);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCROLLVIEW_DIR_HORIZONTAL: // horizontal
|
|
|
|
offset = fabs(sender->getTouchStartPos().x - touchPoint.x);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
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:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::checkChildInfo(int handleState,UIWidget* sender,const Point &touchPoint)
|
|
|
|
{
|
|
|
|
interceptTouchEvent(handleState, sender, touchPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::scrollToTopEvent()
|
|
|
|
{
|
2013-09-17 20:34:26 +08:00
|
|
|
if (_eventListener && _eventSelector)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-17 20:34:26 +08:00
|
|
|
(_eventListener->*_eventSelector)(this, SCROLLVIEW_EVENT_SCROLL_TO_TOP);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::scrollToBottomEvent()
|
|
|
|
{
|
2013-09-17 20:34:26 +08:00
|
|
|
if (_eventListener && _eventSelector)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-17 20:34:26 +08:00
|
|
|
(_eventListener->*_eventSelector)(this, SCROLLVIEW_EVENT_SCROLL_TO_BOTTOM);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::scrollToLeftEvent()
|
|
|
|
{
|
2013-09-17 20:34:26 +08:00
|
|
|
if (_eventListener && _eventSelector)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-17 20:34:26 +08:00
|
|
|
(_eventListener->*_eventSelector)(this, SCROLLVIEW_EVENT_SCROLL_TO_LEFT);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::scrollToRightEvent()
|
|
|
|
{
|
2013-09-17 20:34:26 +08:00
|
|
|
if (_eventListener && _eventSelector)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-17 20:34:26 +08:00
|
|
|
(_eventListener->*_eventSelector)(this, SCROLLVIEW_EVENT_SCROLL_TO_RIGHT);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 20:34:26 +08:00
|
|
|
void UIScrollView::addEventListener(Object *target, SEL_ScrollViewEvent selector)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-17 20:34:26 +08:00
|
|
|
_eventListener = target;
|
|
|
|
_eventSelector = 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
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollView::setMoveDirection(SCROLLVIEW_MOVE_DIR dir)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_moveDirection = dir;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SCROLLVIEW_MOVE_DIR UIScrollView::getMoveDirection()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _moveDirection;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Layout* UIScrollView::getInnerContainer()
|
|
|
|
{
|
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-09-13 22:20:20 +08:00
|
|
|
NS_CC_EXT_END
|