2014-03-11 17:13:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
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 "ui/UIScrollView.h"
|
2015-06-20 16:28:29 +08:00
|
|
|
#include "2d/CCTweenFunction.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
static const float AUTOSCROLLMAXSPEED = 1000.0f;
|
|
|
|
|
2015-06-20 22:42:37 +08:00
|
|
|
static const float BOUNCE_BACK_DURATION = 0.3f;
|
|
|
|
|
|
|
|
|
|
|
|
static char LOG_BUFFER[256];
|
|
|
|
char* time()
|
|
|
|
{
|
|
|
|
int nBufferLength = 256;
|
|
|
|
// Get current local time
|
|
|
|
struct tm* ptm = NULL;
|
|
|
|
struct timeval tv;
|
|
|
|
{
|
|
|
|
gettimeofday (&tv, NULL);
|
|
|
|
ptm = localtime (&tv.tv_sec);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the date and time, down to a single second.
|
|
|
|
strftime (LOG_BUFFER, nBufferLength, "%Y-%m-%d %H:%M:%S", ptm);
|
|
|
|
size_t nCurrentLength = strlen(LOG_BUFFER);
|
|
|
|
|
|
|
|
// Compute milliseconds from microseconds.
|
|
|
|
long lMilliseconds = tv.tv_usec / 1000;
|
|
|
|
snprintf(LOG_BUFFER + nCurrentLength, nBufferLength - nCurrentLength, ".%03ld", lMilliseconds);
|
|
|
|
return LOG_BUFFER;
|
|
|
|
}
|
|
|
|
|
2015-04-20 01:40:52 +08:00
|
|
|
const Vec2 SCROLLDIR_UP(0.0f, 1.0f);
|
|
|
|
const Vec2 SCROLLDIR_DOWN(0.0f, -1.0f);
|
|
|
|
const Vec2 SCROLLDIR_LEFT(-1.0f, 0.0f);
|
|
|
|
const Vec2 SCROLLDIR_RIGHT(1.0f, 0.0f);
|
2015-04-15 16:37:11 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
IMPLEMENT_CLASS_GUI_INFO(ScrollView)
|
|
|
|
|
|
|
|
ScrollView::ScrollView():
|
|
|
|
_innerContainer(nullptr),
|
2014-05-12 11:08:10 +08:00
|
|
|
_direction(Direction::VERTICAL),
|
2014-03-11 17:13:54 +08:00
|
|
|
_topBoundary(0.0f),
|
|
|
|
_bottomBoundary(0.0f),
|
|
|
|
_leftBoundary(0.0f),
|
|
|
|
_rightBoundary(0.0f),
|
|
|
|
_autoScroll(false),
|
|
|
|
_autoScrollAddUpTime(0.0f),
|
|
|
|
_autoScrollOriginalSpeed(0.0f),
|
|
|
|
_autoScrollAcceleration(-1000.0f),
|
|
|
|
_isAutoScrollSpeedAttenuated(false),
|
|
|
|
_needCheckAutoScrollDestination(false),
|
|
|
|
_bePressed(false),
|
|
|
|
_slidTime(0.0f),
|
|
|
|
_childFocusCancelOffset(5.0f),
|
|
|
|
_bounceEnabled(false),
|
2015-06-20 16:32:39 +08:00
|
|
|
_bouncingBack(false),
|
|
|
|
_bounceBackAttenuate(true),
|
|
|
|
_bounceBackDuration(0),
|
|
|
|
_bounceBackAccumulatedTime(0),
|
2014-03-11 17:13:54 +08:00
|
|
|
_inertiaScrollEnabled(true),
|
|
|
|
_scrollViewEventListener(nullptr),
|
2014-05-12 11:08:10 +08:00
|
|
|
_scrollViewEventSelector(nullptr),
|
|
|
|
_eventCallback(nullptr)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-24 15:51:14 +08:00
|
|
|
setTouchEnabled(true);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ScrollView::~ScrollView()
|
|
|
|
{
|
|
|
|
_scrollViewEventListener = nullptr;
|
|
|
|
_scrollViewEventSelector = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScrollView* ScrollView::create()
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
ScrollView* widget = new (std::nothrow) ScrollView();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-04-15 16:37:11 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void ScrollView::onEnter()
|
|
|
|
{
|
2014-06-13 17:59:23 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
|
|
|
if (_scriptType == kScriptTypeJavascript)
|
|
|
|
{
|
2014-06-18 11:51:52 +08:00
|
|
|
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
|
2014-06-13 17:59:23 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2015-04-15 16:37:11 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
Layout::onEnter();
|
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScrollView::init()
|
|
|
|
{
|
|
|
|
if (Layout::init())
|
|
|
|
{
|
|
|
|
setClippingEnabled(true);
|
|
|
|
_innerContainer->setTouchEnabled(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::initRenderer()
|
|
|
|
{
|
|
|
|
Layout::initRenderer();
|
|
|
|
_innerContainer = Layout::create();
|
2014-10-09 18:28:09 +08:00
|
|
|
_innerContainer->setColor(Color3B(255,255,255));
|
|
|
|
_innerContainer->setOpacity(255);
|
|
|
|
_innerContainer->setCascadeColorEnabled(true);
|
|
|
|
_innerContainer->setCascadeOpacityEnabled(true);
|
2014-05-22 17:49:19 +08:00
|
|
|
addProtectedChild(_innerContainer, 1, 1);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::onSizeChanged()
|
|
|
|
{
|
|
|
|
Layout::onSizeChanged();
|
2014-06-20 10:40:16 +08:00
|
|
|
_topBoundary = _contentSize.height;
|
|
|
|
_rightBoundary = _contentSize.width;
|
2014-06-20 11:18:53 +08:00
|
|
|
Size innerSize = _innerContainer->getContentSize();
|
2014-03-11 17:13:54 +08:00
|
|
|
float orginInnerSizeWidth = innerSize.width;
|
|
|
|
float orginInnerSizeHeight = innerSize.height;
|
2014-06-20 10:40:16 +08:00
|
|
|
float innerSizeWidth = MAX(orginInnerSizeWidth, _contentSize.width);
|
|
|
|
float innerSizeHeight = MAX(orginInnerSizeHeight, _contentSize.height);
|
2014-06-20 14:03:33 +08:00
|
|
|
_innerContainer->setContentSize(Size(innerSizeWidth, innerSizeHeight));
|
2014-06-20 11:18:53 +08:00
|
|
|
_innerContainer->setPosition(Vec2(0, _contentSize.height - _innerContainer->getContentSize().height));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::setInnerContainerSize(const Size &size)
|
|
|
|
{
|
2014-06-20 10:40:16 +08:00
|
|
|
float innerSizeWidth = _contentSize.width;
|
|
|
|
float innerSizeHeight = _contentSize.height;
|
2014-06-20 11:18:53 +08:00
|
|
|
Size originalInnerSize = _innerContainer->getContentSize();
|
2014-06-20 10:40:16 +08:00
|
|
|
if (size.width < _contentSize.width)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
CCLOG("Inner width <= scrollview width, it will be force sized!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
innerSizeWidth = size.width;
|
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
if (size.height < _contentSize.height)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
CCLOG("Inner height <= scrollview height, it will be force sized!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
innerSizeHeight = size.height;
|
|
|
|
}
|
2014-06-20 14:03:33 +08:00
|
|
|
_innerContainer->setContentSize(Size(innerSizeWidth, innerSizeHeight));
|
2015-06-19 11:58:30 +08:00
|
|
|
|
|
|
|
// Scroll children appropriately
|
|
|
|
{
|
|
|
|
float offsetX = originalInnerSize.height - innerSizeHeight;
|
|
|
|
float offsetY = 0;
|
|
|
|
if (_innerContainer->getRightBoundary() <= _contentSize.width)
|
|
|
|
{
|
|
|
|
offsetX = originalInnerSize.width - innerSizeWidth;
|
|
|
|
}
|
|
|
|
if(offsetX != 0 || offsetY != 0)
|
|
|
|
{
|
|
|
|
scrollChildren(offsetX, offsetY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate and set the position of the inner container.
|
|
|
|
Vec2 pos = _innerContainer->getPosition();
|
2014-05-19 17:18:04 +08:00
|
|
|
if (_innerContainer->getLeftBoundary() > 0.0f)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-19 11:58:30 +08:00
|
|
|
pos.x = _innerContainer->getAnchorPoint().x * _innerContainer->getContentSize().width;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
if (_innerContainer->getRightBoundary() < _contentSize.width)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-19 11:58:30 +08:00
|
|
|
pos.x = _contentSize.width - ((1.0f - _innerContainer->getAnchorPoint().x) * _innerContainer->getContentSize().width);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
if (_innerContainer->getPosition().y > 0.0f)
|
|
|
|
{
|
2015-06-19 11:58:30 +08:00
|
|
|
pos.y = _innerContainer->getAnchorPoint().y * _innerContainer->getContentSize().height;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
if (_innerContainer->getTopBoundary() < _contentSize.height)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-19 11:58:30 +08:00
|
|
|
pos.y = _contentSize.height - (1.0f - _innerContainer->getAnchorPoint().y) * _innerContainer->getContentSize().height;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-06-19 11:58:30 +08:00
|
|
|
_innerContainer->setPosition(pos);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Size& ScrollView::getInnerContainerSize() const
|
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
return _innerContainer->getContentSize();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-15 16:37:11 +08:00
|
|
|
|
2014-06-25 16:17:16 +08:00
|
|
|
void ScrollView::addChild(Node* child)
|
|
|
|
{
|
|
|
|
ScrollView::addChild(child, child->getLocalZOrder(), child->getTag());
|
|
|
|
}
|
2015-04-15 16:37:11 +08:00
|
|
|
|
2014-06-25 16:17:16 +08:00
|
|
|
void ScrollView::addChild(Node * child, int localZOrder)
|
|
|
|
{
|
|
|
|
ScrollView::addChild(child, localZOrder, child->getTag());
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
void ScrollView::addChild(Node *child, int zOrder, int tag)
|
|
|
|
{
|
2014-03-13 10:11:41 +08:00
|
|
|
_innerContainer->addChild(child, zOrder, tag);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-15 16:37:11 +08:00
|
|
|
|
2014-06-25 11:27:48 +08:00
|
|
|
void ScrollView::addChild(Node* child, int zOrder, const std::string &name)
|
|
|
|
{
|
|
|
|
_innerContainer->addChild(child, zOrder, name);
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
void ScrollView::removeAllChildren()
|
|
|
|
{
|
|
|
|
removeAllChildrenWithCleanup(true);
|
|
|
|
}
|
2015-04-15 16:37:11 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void ScrollView::removeAllChildrenWithCleanup(bool cleanup)
|
|
|
|
{
|
|
|
|
_innerContainer->removeAllChildrenWithCleanup(cleanup);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::removeChild(Node* child, bool cleanup)
|
|
|
|
{
|
|
|
|
return _innerContainer->removeChild(child, cleanup);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Node*>& ScrollView::getChildren()
|
|
|
|
{
|
|
|
|
return _innerContainer->getChildren();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vector<Node*>& ScrollView::getChildren() const
|
|
|
|
{
|
|
|
|
return _innerContainer->getChildren();
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t ScrollView::getChildrenCount() const
|
|
|
|
{
|
|
|
|
return _innerContainer->getChildrenCount();
|
|
|
|
}
|
2015-04-15 16:37:11 +08:00
|
|
|
|
2014-05-17 15:40:36 +08:00
|
|
|
Node* ScrollView::getChildByTag(int tag) const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _innerContainer->getChildByTag(tag);
|
|
|
|
}
|
2015-04-15 16:37:11 +08:00
|
|
|
|
2014-06-11 16:57:43 +08:00
|
|
|
Node* ScrollView::getChildByName(const std::string& name)const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _innerContainer->getChildByName(name);
|
|
|
|
}
|
2015-04-15 16:37:11 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void ScrollView::moveChildren(float offsetX, float offsetY)
|
|
|
|
{
|
2015-06-19 11:58:30 +08:00
|
|
|
Vec2 position = _innerContainer->getPosition() + Vec2(offsetX, offsetY);
|
|
|
|
_innerContainer->setPosition(position);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::autoScrollChildren(float dt)
|
|
|
|
{
|
|
|
|
float lastTime = _autoScrollAddUpTime;
|
|
|
|
_autoScrollAddUpTime += dt;
|
|
|
|
if (_isAutoScrollSpeedAttenuated)
|
|
|
|
{
|
|
|
|
float nowSpeed = _autoScrollOriginalSpeed + _autoScrollAcceleration * _autoScrollAddUpTime;
|
|
|
|
if (nowSpeed <= 0.0f)
|
|
|
|
{
|
|
|
|
stopAutoScrollChildren();
|
2015-06-20 16:28:29 +08:00
|
|
|
startBounceBackIfNeeded();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
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();
|
2015-06-20 16:28:29 +08:00
|
|
|
startBounceBackIfNeeded();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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();
|
2015-06-20 16:28:29 +08:00
|
|
|
startBounceBackIfNeeded();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!scrollChildren(_autoScrollDir.x * dt * _autoScrollOriginalSpeed, _autoScrollDir.y * dt * _autoScrollOriginalSpeed))
|
|
|
|
{
|
|
|
|
stopAutoScrollChildren();
|
2015-06-20 16:28:29 +08:00
|
|
|
startBounceBackIfNeeded();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-20 16:28:29 +08:00
|
|
|
bool ScrollView::startBounceBackIfNeeded()
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (!_bounceEnabled)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-19 09:02:25 +08:00
|
|
|
|
2015-06-20 22:42:37 +08:00
|
|
|
_bounceBackTargetDelta = getHowMuchOutOfBoundary(Vec2::ZERO);
|
|
|
|
if(_bounceBackTargetDelta == Vec2::ZERO)
|
2015-06-19 20:10:08 +08:00
|
|
|
{
|
2015-06-20 22:42:37 +08:00
|
|
|
return false;
|
2015-06-20 16:28:29 +08:00
|
|
|
}
|
|
|
|
|
2015-06-20 16:32:39 +08:00
|
|
|
_bouncingBack = true;
|
|
|
|
_bounceBackAttenuate = true;
|
|
|
|
_bounceBackStartPosition = _innerContainer->getPosition();
|
|
|
|
_bounceBackDuration = DEFAULT_BOUNCE_BACK_DURATION;
|
|
|
|
_bounceBackAccumulatedTime = 0;
|
2015-06-20 16:28:29 +08:00
|
|
|
|
2015-06-20 22:42:37 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 ScrollView::getHowMuchOutOfBoundary(const Vec2& addition) const
|
|
|
|
{
|
|
|
|
Vec2 result;
|
|
|
|
|
|
|
|
if(_innerContainer->getLeftBoundary() + addition.x > _leftBoundary)
|
2015-06-19 20:10:08 +08:00
|
|
|
{
|
2015-06-20 22:42:37 +08:00
|
|
|
result.x = _leftBoundary - (_innerContainer->getLeftBoundary() + addition.x);
|
|
|
|
}
|
|
|
|
else if(_innerContainer->getRightBoundary() + addition.x < _rightBoundary)
|
|
|
|
{
|
|
|
|
result.x = _rightBoundary - (_innerContainer->getRightBoundary() + addition.x);
|
2015-06-20 16:28:29 +08:00
|
|
|
}
|
|
|
|
|
2015-06-20 22:42:37 +08:00
|
|
|
if(_innerContainer->getTopBoundary() + addition.y < _topBoundary)
|
|
|
|
{
|
|
|
|
result.y = _topBoundary - (_innerContainer->getTopBoundary() + addition.y);
|
|
|
|
}
|
|
|
|
else if(_innerContainer->getBottomBoundary() + addition.y > _bottomBoundary)
|
|
|
|
{
|
|
|
|
result.y = _bottomBoundary - (_innerContainer->getBottomBoundary() + addition.y);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-06-20 16:28:29 +08:00
|
|
|
void ScrollView::processBounceBack(float deltaTime)
|
|
|
|
{
|
2015-06-20 16:32:39 +08:00
|
|
|
_bounceBackAccumulatedTime += deltaTime;
|
|
|
|
float percentage = _bounceBackAccumulatedTime / _bounceBackDuration;
|
2015-06-20 16:28:29 +08:00
|
|
|
if(percentage >= 1)
|
|
|
|
{
|
2015-06-20 16:32:39 +08:00
|
|
|
_innerContainer->setPosition(_bounceBackStartPosition + _bounceBackTargetDelta);
|
|
|
|
_bouncingBack = false;
|
2015-06-19 20:10:08 +08:00
|
|
|
}
|
2015-06-20 16:28:29 +08:00
|
|
|
else
|
2015-06-19 20:10:08 +08:00
|
|
|
{
|
2015-06-20 16:32:39 +08:00
|
|
|
if(_bounceBackAttenuate)
|
2015-06-19 20:10:08 +08:00
|
|
|
{
|
2015-06-20 16:28:29 +08:00
|
|
|
percentage = tweenfunc::expoEaseOut(percentage);
|
2015-06-19 20:10:08 +08:00
|
|
|
}
|
2015-06-20 16:32:39 +08:00
|
|
|
Vec2 moveDelta = _bounceBackTargetDelta * percentage;
|
2015-06-20 16:28:29 +08:00
|
|
|
|
|
|
|
// Dispatch related events
|
|
|
|
if(moveDelta.x > 0)
|
2015-06-19 20:10:08 +08:00
|
|
|
{
|
2015-06-20 16:28:29 +08:00
|
|
|
processScrollEvent(MoveDirection::RIGHT, true);
|
2015-06-19 20:10:08 +08:00
|
|
|
}
|
2015-06-20 16:28:29 +08:00
|
|
|
else if(moveDelta.x < 0)
|
2015-06-19 20:10:08 +08:00
|
|
|
{
|
2015-06-20 16:28:29 +08:00
|
|
|
processScrollEvent(MoveDirection::LEFT, true);
|
2015-06-19 20:10:08 +08:00
|
|
|
}
|
2015-06-20 16:28:29 +08:00
|
|
|
if(moveDelta.y > 0)
|
|
|
|
{
|
|
|
|
processScrollEvent(MoveDirection::TOP, true);
|
|
|
|
}
|
|
|
|
else if(moveDelta.y < 0)
|
|
|
|
{
|
|
|
|
processScrollEvent(MoveDirection::BOTTOM, true);
|
|
|
|
}
|
2015-06-20 16:32:39 +08:00
|
|
|
_innerContainer->setPosition(_bounceBackStartPosition + moveDelta);
|
2015-06-19 20:10:08 +08:00
|
|
|
}
|
2015-06-19 17:32:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScrollView::isOutOfBoundary(MoveDirection dir) const
|
|
|
|
{
|
|
|
|
switch(dir)
|
|
|
|
{
|
|
|
|
case MoveDirection::TOP: return _innerContainer->getTopBoundary() < _topBoundary;
|
|
|
|
case MoveDirection::BOTTOM: return _innerContainer->getBottomBoundary() > _bottomBoundary;
|
|
|
|
case MoveDirection::LEFT: return _innerContainer->getLeftBoundary() > _leftBoundary;
|
|
|
|
case MoveDirection::RIGHT: return _innerContainer->getRightBoundary() < _rightBoundary;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScrollView::isOutOfBoundaryTopOrBottom() const
|
|
|
|
{
|
|
|
|
return isOutOfBoundary(MoveDirection::TOP) || isOutOfBoundary(MoveDirection::BOTTOM);
|
|
|
|
}
|
2015-06-20 16:28:29 +08:00
|
|
|
|
2015-06-19 17:32:32 +08:00
|
|
|
bool ScrollView::isOutOfBoundaryLeftOrRight() const
|
|
|
|
{
|
|
|
|
return isOutOfBoundary(MoveDirection::LEFT) || isOutOfBoundary(MoveDirection::RIGHT);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void ScrollView::startAutoScrollChildrenWithOriginalSpeed(const Vec2& dir, float v, bool attenuated, float acceleration)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
stopAutoScrollChildren();
|
|
|
|
_autoScrollDir = dir;
|
|
|
|
_isAutoScrollSpeedAttenuated = attenuated;
|
|
|
|
_autoScrollOriginalSpeed = v;
|
|
|
|
_autoScroll = true;
|
|
|
|
_autoScrollAcceleration = acceleration;
|
|
|
|
}
|
|
|
|
|
2015-04-15 16:37:11 +08:00
|
|
|
void ScrollView::startAutoScrollChildrenWithDestination(const Vec2& des, float second, bool attenuated)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
_needCheckAutoScrollDestination = false;
|
|
|
|
_autoScrollDestination = des;
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 dis = des - _innerContainer->getPosition();
|
|
|
|
Vec2 dir = dis.getNormalized();
|
2014-03-11 17:13:54 +08:00
|
|
|
float orSpeed = 0.0f;
|
|
|
|
float acceleration = -1000.0f;
|
|
|
|
if (attenuated)
|
|
|
|
{
|
2015-04-15 16:37:11 +08:00
|
|
|
acceleration = (-(2 * dis.getLength())) / (second * second);
|
|
|
|
orSpeed = 2 * dis.getLength() / second;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_needCheckAutoScrollDestination = true;
|
2015-04-15 16:37:11 +08:00
|
|
|
orSpeed = dis.getLength() / second;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
startAutoScrollChildrenWithOriginalSpeed(dir, orSpeed, attenuated, acceleration);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void ScrollView::jumpToDestination(const Vec2 &des)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
float finalOffsetX = des.x;
|
|
|
|
float finalOffsetY = des.y;
|
2015-06-19 20:10:08 +08:00
|
|
|
if (des.y <= 0 && _direction != Direction::HORIZONTAL)
|
|
|
|
{
|
|
|
|
finalOffsetY = MAX(des.y, _contentSize.height - _innerContainer->getContentSize().height);
|
|
|
|
}
|
|
|
|
if (des.x <= 0 && _direction != Direction::VERTICAL)
|
|
|
|
{
|
|
|
|
finalOffsetX = MAX(des.x, _contentSize.width - _innerContainer->getContentSize().width);
|
|
|
|
}
|
2014-05-15 01:07:09 +08:00
|
|
|
_innerContainer->setPosition(Vec2(finalOffsetX, finalOffsetY));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::stopAutoScrollChildren()
|
|
|
|
{
|
|
|
|
_autoScroll = false;
|
|
|
|
_autoScrollOriginalSpeed = 0.0f;
|
|
|
|
_autoScrollAddUpTime = 0.0f;
|
|
|
|
}
|
|
|
|
|
2015-06-19 11:58:30 +08:00
|
|
|
bool ScrollView::checkCustomScrollDestinationLeft(float* touchOffsetX, float* touchOffsetY)
|
|
|
|
{
|
|
|
|
float icLeftPos = _innerContainer->getLeftBoundary();
|
|
|
|
if (icLeftPos + *touchOffsetX >= _autoScrollDestination.x)
|
|
|
|
{
|
|
|
|
*touchOffsetX = _autoScrollDestination.x - icLeftPos;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScrollView::checkCustomScrollDestinationRight(float* touchOffsetX, float* touchOffsetY)
|
|
|
|
{
|
|
|
|
float icRightPos = _innerContainer->getRightBoundary();
|
|
|
|
if (icRightPos + *touchOffsetX <= _autoScrollDestination.x)
|
|
|
|
{
|
|
|
|
*touchOffsetX = _autoScrollDestination.x - icRightPos;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
bool ScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchOffsetY)
|
|
|
|
{
|
|
|
|
bool scrollenabled = true;
|
|
|
|
switch (_direction)
|
|
|
|
{
|
2014-12-16 16:44:04 +08:00
|
|
|
case Direction::VERTICAL:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (_autoScrollDir.y > 0)
|
|
|
|
{
|
2014-05-19 17:18:04 +08:00
|
|
|
float icBottomPos = _innerContainer->getBottomBoundary();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (icBottomPos + *touchOffsetY >= _autoScrollDestination.y)
|
|
|
|
{
|
|
|
|
*touchOffsetY = _autoScrollDestination.y - icBottomPos;
|
|
|
|
scrollenabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-19 17:18:04 +08:00
|
|
|
float icBottomPos = _innerContainer->getBottomBoundary();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (icBottomPos + *touchOffsetY <= _autoScrollDestination.y)
|
|
|
|
{
|
|
|
|
*touchOffsetY = _autoScrollDestination.y - icBottomPos;
|
|
|
|
scrollenabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-12-16 16:44:04 +08:00
|
|
|
case Direction::HORIZONTAL:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (_autoScrollDir.x > 0)
|
|
|
|
{
|
2014-05-19 17:18:04 +08:00
|
|
|
float icLeftPos = _innerContainer->getLeftBoundary();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (icLeftPos + *touchOffsetX >= _autoScrollDestination.x)
|
|
|
|
{
|
|
|
|
*touchOffsetX = _autoScrollDestination.x - icLeftPos;
|
|
|
|
scrollenabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-19 17:18:04 +08:00
|
|
|
float icLeftPos = _innerContainer->getLeftBoundary();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (icLeftPos + *touchOffsetX <= _autoScrollDestination.x)
|
|
|
|
{
|
|
|
|
*touchOffsetX = _autoScrollDestination.x - icLeftPos;
|
|
|
|
scrollenabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::BOTH:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-19 11:58:30 +08:00
|
|
|
if (*touchOffsetY > 0.0f) // up
|
|
|
|
{
|
|
|
|
if (*touchOffsetX > 0.0f) // right
|
|
|
|
{
|
|
|
|
scrollenabled = checkCustomScrollDestinationLeft(touchOffsetX, touchOffsetY);
|
|
|
|
}
|
|
|
|
else if (*touchOffsetX < 0.0f) // left
|
|
|
|
{
|
|
|
|
scrollenabled = checkCustomScrollDestinationRight(touchOffsetX, touchOffsetY);
|
|
|
|
}
|
|
|
|
float icBottomPos = _innerContainer->getBottomBoundary();
|
|
|
|
if (icBottomPos + *touchOffsetY >= _autoScrollDestination.y)
|
|
|
|
{
|
|
|
|
*touchOffsetY = _autoScrollDestination.y - icBottomPos;
|
|
|
|
scrollenabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (*touchOffsetY < 0.0f) // down
|
|
|
|
{
|
|
|
|
if (*touchOffsetX < 0.0f) // left
|
|
|
|
{
|
|
|
|
scrollenabled = checkCustomScrollDestinationRight(touchOffsetX, touchOffsetY);
|
|
|
|
}
|
|
|
|
else if (*touchOffsetX > 0.0f) // right
|
|
|
|
{
|
|
|
|
scrollenabled = checkCustomScrollDestinationLeft(touchOffsetX, touchOffsetY);
|
|
|
|
}
|
|
|
|
float icTopPos = _innerContainer->getTopBoundary();
|
|
|
|
if (icTopPos + *touchOffsetY <= _autoScrollDestination.y)
|
|
|
|
{
|
|
|
|
*touchOffsetY = _autoScrollDestination.y - icTopPos;
|
|
|
|
scrollenabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*touchOffsetX < 0.0f) // left
|
|
|
|
{
|
|
|
|
scrollenabled = checkCustomScrollDestinationRight(touchOffsetX, touchOffsetY);
|
|
|
|
}
|
|
|
|
else if (*touchOffsetX > 0.0f) // right
|
|
|
|
{
|
|
|
|
scrollenabled = checkCustomScrollDestinationLeft(touchOffsetX, touchOffsetY);
|
|
|
|
}
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return scrollenabled;
|
|
|
|
}
|
2015-04-15 16:37:11 +08:00
|
|
|
|
2015-06-19 11:58:30 +08:00
|
|
|
bool ScrollView::processScrollUp(float* offsetYResult, float touchOffsetY)
|
2015-06-18 21:28:04 +08:00
|
|
|
{
|
|
|
|
float icBottomPos = _innerContainer->getBottomBoundary();
|
2015-06-19 18:07:00 +08:00
|
|
|
if (icBottomPos + touchOffsetY < _bottomBoundary)
|
2015-06-18 21:28:04 +08:00
|
|
|
{
|
2015-06-19 18:07:00 +08:00
|
|
|
return true;
|
2015-06-18 21:28:04 +08:00
|
|
|
}
|
2015-06-19 18:07:00 +08:00
|
|
|
else if(!_bounceEnabled)
|
|
|
|
{
|
|
|
|
(*offsetYResult) = _bottomBoundary - icBottomPos;
|
|
|
|
}
|
|
|
|
processScrollEvent(MoveDirection::BOTTOM, false);
|
|
|
|
return false;
|
2015-06-18 21:28:04 +08:00
|
|
|
}
|
|
|
|
|
2015-06-19 11:58:30 +08:00
|
|
|
bool ScrollView::processScrollDown(float* offsetYResult, float touchOffsetY)
|
2015-06-18 21:28:04 +08:00
|
|
|
{
|
2015-06-19 11:31:57 +08:00
|
|
|
float icTopPos = _innerContainer->getTopBoundary();
|
2015-06-19 18:07:00 +08:00
|
|
|
if (icTopPos + touchOffsetY > _topBoundary)
|
2015-06-18 21:28:04 +08:00
|
|
|
{
|
2015-06-19 18:07:00 +08:00
|
|
|
return true;
|
2015-06-18 21:28:04 +08:00
|
|
|
}
|
2015-06-19 18:07:00 +08:00
|
|
|
else if(!_bounceEnabled)
|
|
|
|
{
|
|
|
|
(*offsetYResult) = _topBoundary - icTopPos;
|
|
|
|
}
|
|
|
|
processScrollEvent(MoveDirection::TOP, false);
|
|
|
|
return false;
|
2015-06-18 21:28:04 +08:00
|
|
|
}
|
|
|
|
|
2015-06-19 09:25:46 +08:00
|
|
|
bool ScrollView::processScrollLeft(float* offsetXResult, float touchOffsetX)
|
2015-06-18 21:28:04 +08:00
|
|
|
{
|
|
|
|
float icRightPos = _innerContainer->getRightBoundary();
|
2015-06-19 18:07:00 +08:00
|
|
|
if (icRightPos + touchOffsetX > _rightBoundary)
|
2015-06-18 21:28:04 +08:00
|
|
|
{
|
2015-06-19 18:07:00 +08:00
|
|
|
return true;
|
2015-06-18 21:28:04 +08:00
|
|
|
}
|
2015-06-19 18:07:00 +08:00
|
|
|
else if(!_bounceEnabled)
|
|
|
|
{
|
|
|
|
(*offsetXResult) = _rightBoundary - icRightPos;
|
|
|
|
}
|
|
|
|
processScrollEvent(MoveDirection::RIGHT, false);
|
|
|
|
return false;
|
2015-06-18 21:28:04 +08:00
|
|
|
}
|
|
|
|
|
2015-06-19 11:31:57 +08:00
|
|
|
bool ScrollView::processScrollRight(float* offsetXResult, float touchOffsetX)
|
|
|
|
{
|
|
|
|
float icLeftPos = _innerContainer->getLeftBoundary();
|
2015-06-19 18:07:00 +08:00
|
|
|
if (icLeftPos + touchOffsetX < _leftBoundary)
|
2015-06-19 11:31:57 +08:00
|
|
|
{
|
2015-06-19 18:07:00 +08:00
|
|
|
return true;
|
2015-06-19 11:31:57 +08:00
|
|
|
}
|
2015-06-19 18:07:00 +08:00
|
|
|
else if(!_bounceEnabled)
|
|
|
|
{
|
|
|
|
(*offsetXResult) = _leftBoundary - icLeftPos;
|
|
|
|
}
|
|
|
|
processScrollEvent(MoveDirection::LEFT, false);
|
|
|
|
return false;
|
2015-06-19 11:31:57 +08:00
|
|
|
}
|
|
|
|
|
2015-06-19 09:02:25 +08:00
|
|
|
bool ScrollView::scrollChildren(float touchOffsetX, float touchOffsetY)
|
2015-06-18 21:28:04 +08:00
|
|
|
{
|
2015-06-19 09:25:46 +08:00
|
|
|
processScrollingEvent();
|
2015-06-19 09:02:25 +08:00
|
|
|
|
|
|
|
touchOffsetX = (_direction == Direction::VERTICAL ? 0 : touchOffsetX);
|
|
|
|
touchOffsetY = (_direction == Direction::HORIZONTAL ? 0 : touchOffsetY);
|
2015-06-19 18:07:00 +08:00
|
|
|
if(_bounceEnabled)
|
|
|
|
{
|
|
|
|
// If the position of the inner container is out of the boundary, the offsets should be divided by two.
|
|
|
|
touchOffsetX *= (isOutOfBoundaryLeftOrRight() ? 0.5f : 1);
|
|
|
|
touchOffsetY *= (isOutOfBoundaryTopOrBottom() ? 0.5f : 1);
|
|
|
|
}
|
2015-06-19 09:02:25 +08:00
|
|
|
|
2015-06-18 21:28:04 +08:00
|
|
|
float realOffsetX = touchOffsetX;
|
|
|
|
float realOffsetY = touchOffsetY;
|
|
|
|
|
2015-06-20 22:42:37 +08:00
|
|
|
bool scrollEnabledUpDown = true;
|
|
|
|
bool scrollEnabledLeftRight = true;
|
2015-06-19 09:02:25 +08:00
|
|
|
if (touchOffsetY > 0.0f) // up
|
2015-06-18 21:28:04 +08:00
|
|
|
{
|
2015-06-20 22:42:37 +08:00
|
|
|
scrollEnabledUpDown = processScrollUp(&realOffsetY, touchOffsetY);
|
2015-06-18 21:28:04 +08:00
|
|
|
}
|
|
|
|
else if (touchOffsetY < 0.0f) // down
|
|
|
|
{
|
2015-06-20 22:42:37 +08:00
|
|
|
scrollEnabledUpDown = processScrollDown(&realOffsetY, touchOffsetY);
|
2015-06-18 21:28:04 +08:00
|
|
|
}
|
2015-06-20 22:42:37 +08:00
|
|
|
|
|
|
|
if (touchOffsetX < 0.0f) // left
|
2015-06-18 21:28:04 +08:00
|
|
|
{
|
2015-06-20 22:42:37 +08:00
|
|
|
scrollEnabledLeftRight = processScrollLeft(&realOffsetX, touchOffsetX);
|
|
|
|
}
|
|
|
|
else if (touchOffsetX > 0.0f) // right
|
|
|
|
{
|
|
|
|
scrollEnabledLeftRight = processScrollRight(&realOffsetX, touchOffsetX);
|
2015-06-18 21:28:04 +08:00
|
|
|
}
|
|
|
|
moveChildren(realOffsetX, realOffsetY);
|
2015-06-20 22:42:37 +08:00
|
|
|
return scrollEnabledUpDown || scrollEnabledLeftRight;
|
2015-06-18 21:28:04 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 16:37:11 +08:00
|
|
|
void ScrollView::scrollToBottom(float second, bool attenuated)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-04-15 16:37:11 +08:00
|
|
|
startAutoScrollChildrenWithDestination(Vec2(_innerContainer->getPosition().x, 0.0f), second, attenuated);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 16:37:11 +08:00
|
|
|
void ScrollView::scrollToTop(float second, bool attenuated)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-20 10:40:16 +08:00
|
|
|
startAutoScrollChildrenWithDestination(Vec2(_innerContainer->getPosition().x,
|
2015-04-15 16:37:11 +08:00
|
|
|
_contentSize.height - _innerContainer->getContentSize().height), second, attenuated);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 16:37:11 +08:00
|
|
|
void ScrollView::scrollToLeft(float second, bool attenuated)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-04-15 16:37:11 +08:00
|
|
|
startAutoScrollChildrenWithDestination(Vec2(0.0f, _innerContainer->getPosition().y), second, attenuated);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 16:37:11 +08:00
|
|
|
void ScrollView::scrollToRight(float second, bool attenuated)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
startAutoScrollChildrenWithDestination(Vec2(_contentSize.width - _innerContainer->getContentSize().width,
|
2015-04-15 16:37:11 +08:00
|
|
|
_innerContainer->getPosition().y), second, attenuated);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 16:37:11 +08:00
|
|
|
void ScrollView::scrollToTopLeft(float second, bool attenuated)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
if (_direction != Direction::BOTH)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
CCLOG("Scroll diretion is not both!");
|
|
|
|
return;
|
|
|
|
}
|
2015-04-15 16:37:11 +08:00
|
|
|
startAutoScrollChildrenWithDestination(Vec2(0.0f, _contentSize.height - _innerContainer->getContentSize().height), second, attenuated);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 16:37:11 +08:00
|
|
|
void ScrollView::scrollToTopRight(float second, bool attenuated)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
if (_direction != Direction::BOTH)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
CCLOG("Scroll diretion is not both!");
|
|
|
|
return;
|
|
|
|
}
|
2014-06-20 11:18:53 +08:00
|
|
|
startAutoScrollChildrenWithDestination(Vec2(_contentSize.width - _innerContainer->getContentSize().width,
|
2015-04-15 16:37:11 +08:00
|
|
|
_contentSize.height - _innerContainer->getContentSize().height), second, attenuated);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 16:37:11 +08:00
|
|
|
void ScrollView::scrollToBottomLeft(float second, bool attenuated)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
if (_direction != Direction::BOTH)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
CCLOG("Scroll diretion is not both!");
|
|
|
|
return;
|
|
|
|
}
|
2015-04-15 16:37:11 +08:00
|
|
|
startAutoScrollChildrenWithDestination(Vec2::ZERO, second, attenuated);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 16:37:11 +08:00
|
|
|
void ScrollView::scrollToBottomRight(float second, bool attenuated)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
if (_direction != Direction::BOTH)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
CCLOG("Scroll diretion is not both!");
|
|
|
|
return;
|
|
|
|
}
|
2015-04-15 16:37:11 +08:00
|
|
|
startAutoScrollChildrenWithDestination(Vec2(_contentSize.width - _innerContainer->getContentSize().width, 0.0f), second, attenuated);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 16:37:11 +08:00
|
|
|
void ScrollView::scrollToPercentVertical(float percent, float second, bool attenuated)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
float minY = _contentSize.height - _innerContainer->getContentSize().height;
|
2014-03-11 17:13:54 +08:00
|
|
|
float h = - minY;
|
2015-04-15 16:37:11 +08:00
|
|
|
startAutoScrollChildrenWithDestination(Vec2(_innerContainer->getPosition().x, minY + percent * h / 100.0f), second, attenuated);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 16:37:11 +08:00
|
|
|
void ScrollView::scrollToPercentHorizontal(float percent, float second, bool attenuated)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
float w = _innerContainer->getContentSize().width - _contentSize.width;
|
2015-04-15 16:37:11 +08:00
|
|
|
startAutoScrollChildrenWithDestination(Vec2(-(percent * w / 100.0f), _innerContainer->getPosition().y), second, attenuated);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 16:37:11 +08:00
|
|
|
void ScrollView::scrollToPercentBothDirection(const Vec2& percent, float second, bool attenuated)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
if (_direction != Direction::BOTH)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-20 11:18:53 +08:00
|
|
|
float minY = _contentSize.height - _innerContainer->getContentSize().height;
|
2014-03-11 17:13:54 +08:00
|
|
|
float h = - minY;
|
2014-06-20 11:18:53 +08:00
|
|
|
float w = _innerContainer->getContentSize().width - _contentSize.width;
|
2015-04-15 16:37:11 +08:00
|
|
|
startAutoScrollChildrenWithDestination(Vec2(-(percent.x * w / 100.0f), minY + percent.y * h / 100.0f), second, attenuated);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::jumpToBottom()
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
jumpToDestination(Vec2(_innerContainer->getPosition().x, 0.0f));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::jumpToTop()
|
|
|
|
{
|
2014-06-20 10:40:16 +08:00
|
|
|
jumpToDestination(Vec2(_innerContainer->getPosition().x,
|
2014-06-20 11:18:53 +08:00
|
|
|
_contentSize.height - _innerContainer->getContentSize().height));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::jumpToLeft()
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
jumpToDestination(Vec2(0.0f, _innerContainer->getPosition().y));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::jumpToRight()
|
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
jumpToDestination(Vec2(_contentSize.width - _innerContainer->getContentSize().width, _innerContainer->getPosition().y));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::jumpToTopLeft()
|
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
if (_direction != Direction::BOTH)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
CCLOG("Scroll diretion is not both!");
|
|
|
|
return;
|
|
|
|
}
|
2014-06-20 11:18:53 +08:00
|
|
|
jumpToDestination(Vec2(0.0f, _contentSize.height - _innerContainer->getContentSize().height));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::jumpToTopRight()
|
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
if (_direction != Direction::BOTH)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
CCLOG("Scroll diretion is not both!");
|
|
|
|
return;
|
|
|
|
}
|
2014-06-20 11:18:53 +08:00
|
|
|
jumpToDestination(Vec2(_contentSize.width - _innerContainer->getContentSize().width,
|
|
|
|
_contentSize.height - _innerContainer->getContentSize().height));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::jumpToBottomLeft()
|
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
if (_direction != Direction::BOTH)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
CCLOG("Scroll diretion is not both!");
|
|
|
|
return;
|
|
|
|
}
|
2014-05-15 01:07:09 +08:00
|
|
|
jumpToDestination(Vec2::ZERO);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::jumpToBottomRight()
|
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
if (_direction != Direction::BOTH)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
CCLOG("Scroll diretion is not both!");
|
|
|
|
return;
|
|
|
|
}
|
2014-06-20 11:18:53 +08:00
|
|
|
jumpToDestination(Vec2(_contentSize.width - _innerContainer->getContentSize().width, 0.0f));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::jumpToPercentVertical(float percent)
|
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
float minY = _contentSize.height - _innerContainer->getContentSize().height;
|
2014-03-11 17:13:54 +08:00
|
|
|
float h = - minY;
|
2014-05-15 01:07:09 +08:00
|
|
|
jumpToDestination(Vec2(_innerContainer->getPosition().x, minY + percent * h / 100.0f));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::jumpToPercentHorizontal(float percent)
|
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
float w = _innerContainer->getContentSize().width - _contentSize.width;
|
2014-05-15 01:07:09 +08:00
|
|
|
jumpToDestination(Vec2(-(percent * w / 100.0f), _innerContainer->getPosition().y));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void ScrollView::jumpToPercentBothDirection(const Vec2& percent)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
if (_direction != Direction::BOTH)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-20 11:18:53 +08:00
|
|
|
float minY = _contentSize.height - _innerContainer->getContentSize().height;
|
2014-03-11 17:13:54 +08:00
|
|
|
float h = - minY;
|
2014-06-20 11:18:53 +08:00
|
|
|
float w = _innerContainer->getContentSize().width - _contentSize.width;
|
2014-05-15 01:07:09 +08:00
|
|
|
jumpToDestination(Vec2(-(percent.x * w / 100.0f), minY + percent.y * h / 100.0f));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::startRecordSlidAction()
|
|
|
|
{
|
|
|
|
if (_autoScroll)
|
|
|
|
{
|
|
|
|
stopAutoScrollChildren();
|
|
|
|
}
|
2015-06-20 16:32:39 +08:00
|
|
|
if (_bouncingBack)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-20 16:32:39 +08:00
|
|
|
_bouncingBack = false;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
_slidTime = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::endRecordSlidAction()
|
|
|
|
{
|
2015-06-20 16:28:29 +08:00
|
|
|
if (!startBounceBackIfNeeded() && _inertiaScrollEnabled)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (_slidTime <= 0.016f)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
float totalDis = 0.0f;
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 dir;
|
2014-10-17 14:02:49 +08:00
|
|
|
Vec2 touchEndPositionInNodeSpace = this->convertToNodeSpace(_touchEndPosition);
|
|
|
|
Vec2 touchBeganPositionInNodeSpace = this->convertToNodeSpace(_touchBeganPosition);
|
2014-03-11 17:13:54 +08:00
|
|
|
switch (_direction)
|
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::VERTICAL:
|
2014-10-17 14:02:49 +08:00
|
|
|
totalDis = touchEndPositionInNodeSpace.y - touchBeganPositionInNodeSpace.y;
|
2014-03-11 17:13:54 +08:00
|
|
|
if (totalDis < 0.0f)
|
|
|
|
{
|
|
|
|
dir = SCROLLDIR_DOWN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dir = SCROLLDIR_UP;
|
|
|
|
}
|
|
|
|
break;
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::HORIZONTAL:
|
2014-10-17 14:02:49 +08:00
|
|
|
totalDis = touchEndPositionInNodeSpace.x - touchBeganPositionInNodeSpace.x;
|
2014-03-11 17:13:54 +08:00
|
|
|
if (totalDis < 0.0f)
|
|
|
|
{
|
|
|
|
dir = SCROLLDIR_LEFT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dir = SCROLLDIR_RIGHT;
|
|
|
|
}
|
|
|
|
break;
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::BOTH:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-10-17 14:02:49 +08:00
|
|
|
Vec2 subVector = touchEndPositionInNodeSpace - touchBeganPositionInNodeSpace;
|
2014-03-11 17:13:54 +08:00
|
|
|
totalDis = subVector.getLength();
|
2014-05-01 01:10:18 +08:00
|
|
|
dir = subVector.getNormalized();
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
float orSpeed = MIN(fabs(totalDis)/(_slidTime), AUTOSCROLLMAXSPEED);
|
|
|
|
startAutoScrollChildrenWithOriginalSpeed(dir, orSpeed, true, -1000);
|
|
|
|
_slidTime = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 16:48:49 +08:00
|
|
|
void ScrollView::handlePressLogic(Touch *touch)
|
|
|
|
{
|
2014-03-11 17:13:54 +08:00
|
|
|
startRecordSlidAction();
|
|
|
|
_bePressed = true;
|
|
|
|
}
|
|
|
|
|
2014-06-06 16:48:49 +08:00
|
|
|
void ScrollView::handleMoveLogic(Touch *touch)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-10-17 11:31:00 +08:00
|
|
|
Vec2 touchPositionInNodeSpace = this->convertToNodeSpace(touch->getLocation());
|
|
|
|
Vec2 previousTouchPositionInNodeSpace = this->convertToNodeSpace(touch->getPreviousLocation());
|
|
|
|
Vec2 delta = touchPositionInNodeSpace - previousTouchPositionInNodeSpace;
|
2015-06-19 11:58:30 +08:00
|
|
|
scrollChildren(delta.x, delta.y);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-06-06 16:48:49 +08:00
|
|
|
void ScrollView::handleReleaseLogic(Touch *touch)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
endRecordSlidAction();
|
|
|
|
_bePressed = false;
|
2015-04-15 16:37:11 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
bool ScrollView::onTouchBegan(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
bool pass = Layout::onTouchBegan(touch, unusedEvent);
|
2014-08-15 15:28:28 +08:00
|
|
|
if (!_isInterceptTouch)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-08-15 15:28:28 +08:00
|
|
|
if (_hitted)
|
|
|
|
{
|
|
|
|
handlePressLogic(touch);
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::onTouchMoved(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
Layout::onTouchMoved(touch, unusedEvent);
|
2014-08-15 15:28:28 +08:00
|
|
|
if (!_isInterceptTouch)
|
|
|
|
{
|
|
|
|
handleMoveLogic(touch);
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::onTouchEnded(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
Layout::onTouchEnded(touch, unusedEvent);
|
2014-08-15 15:28:28 +08:00
|
|
|
if (!_isInterceptTouch)
|
|
|
|
{
|
|
|
|
handleReleaseLogic(touch);
|
|
|
|
}
|
|
|
|
_isInterceptTouch = false;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::onTouchCancelled(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
Layout::onTouchCancelled(touch, unusedEvent);
|
2014-08-15 15:28:28 +08:00
|
|
|
if (!_isInterceptTouch)
|
|
|
|
{
|
|
|
|
handleReleaseLogic(touch);
|
|
|
|
}
|
|
|
|
_isInterceptTouch = false;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::update(float dt)
|
|
|
|
{
|
|
|
|
if (_autoScroll)
|
|
|
|
{
|
|
|
|
autoScrollChildren(dt);
|
|
|
|
}
|
2015-06-20 16:32:39 +08:00
|
|
|
if(_bouncingBack)
|
2015-06-20 16:28:29 +08:00
|
|
|
{
|
|
|
|
processBounceBack(dt);
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
recordSlidTime(dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::recordSlidTime(float dt)
|
|
|
|
{
|
|
|
|
if (_bePressed)
|
|
|
|
{
|
|
|
|
_slidTime += dt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 16:48:49 +08:00
|
|
|
void ScrollView::interceptTouchEvent(Widget::TouchEventType event, Widget *sender,Touch* touch)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
Vec2 touchPoint = touch->getLocation();
|
2014-05-22 15:23:13 +08:00
|
|
|
switch (event)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-22 15:23:13 +08:00
|
|
|
case TouchEventType::BEGAN:
|
2014-08-15 15:28:28 +08:00
|
|
|
{
|
|
|
|
_isInterceptTouch = true;
|
2014-06-25 10:45:08 +08:00
|
|
|
_touchBeganPosition = touch->getLocation();
|
2014-06-06 16:48:49 +08:00
|
|
|
handlePressLogic(touch);
|
2014-08-15 15:28:28 +08:00
|
|
|
}
|
|
|
|
break;
|
2014-05-22 15:23:13 +08:00
|
|
|
case TouchEventType::MOVED:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
float offset = (sender->getTouchBeganPosition() - touchPoint).getLength();
|
2014-08-19 15:14:07 +08:00
|
|
|
_touchMovePosition = touch->getLocation();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (offset > _childFocusCancelOffset)
|
|
|
|
{
|
2014-05-06 12:04:52 +08:00
|
|
|
sender->setHighlighted(false);
|
2014-06-06 16:48:49 +08:00
|
|
|
handleMoveLogic(touch);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 15:28:28 +08:00
|
|
|
break;
|
2015-04-15 16:37:11 +08:00
|
|
|
|
2014-05-22 15:23:13 +08:00
|
|
|
case TouchEventType::CANCELED:
|
|
|
|
case TouchEventType::ENDED:
|
2014-08-15 15:28:28 +08:00
|
|
|
{
|
2014-06-25 10:45:08 +08:00
|
|
|
_touchEndPosition = touch->getLocation();
|
2014-06-06 16:48:49 +08:00
|
|
|
handleReleaseLogic(touch);
|
2014-08-15 15:28:28 +08:00
|
|
|
if (sender->isSwallowTouches())
|
|
|
|
{
|
|
|
|
_isInterceptTouch = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 09:25:46 +08:00
|
|
|
void ScrollView::processScrollEvent(MoveDirection dir, bool bounce)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-20 16:28:29 +08:00
|
|
|
CCLOG("processScrollEvent() %d", dir);
|
2015-06-19 09:25:46 +08:00
|
|
|
ScrollviewEventType scrollEventType;
|
|
|
|
EventType eventType;
|
|
|
|
switch(dir) {
|
|
|
|
case MoveDirection::TOP:
|
|
|
|
{
|
|
|
|
scrollEventType = (bounce ? SCROLLVIEW_EVENT_BOUNCE_TOP : SCROLLVIEW_EVENT_SCROLL_TO_TOP);
|
|
|
|
eventType = (bounce ? EventType::BOUNCE_TOP : EventType::SCROLL_TO_TOP);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MoveDirection::BOTTOM:
|
|
|
|
{
|
|
|
|
scrollEventType = (bounce ? SCROLLVIEW_EVENT_BOUNCE_BOTTOM : SCROLLVIEW_EVENT_SCROLL_TO_BOTTOM);
|
|
|
|
eventType = (bounce ? EventType::BOUNCE_BOTTOM : EventType::SCROLL_TO_BOTTOM);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MoveDirection::LEFT:
|
|
|
|
{
|
|
|
|
scrollEventType = (bounce ? SCROLLVIEW_EVENT_BOUNCE_LEFT : SCROLLVIEW_EVENT_SCROLL_TO_LEFT);
|
|
|
|
eventType = (bounce ? EventType::BOUNCE_LEFT : EventType::SCROLL_TO_LEFT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MoveDirection::RIGHT:
|
|
|
|
{
|
|
|
|
scrollEventType = (bounce ? SCROLLVIEW_EVENT_BOUNCE_RIGHT : SCROLLVIEW_EVENT_SCROLL_TO_RIGHT);
|
|
|
|
eventType = (bounce ? EventType::BOUNCE_RIGHT : EventType::SCROLL_TO_RIGHT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dispatchEvent(scrollEventType, eventType);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-06-19 09:25:46 +08:00
|
|
|
void ScrollView::processScrollingEvent()
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-19 09:25:46 +08:00
|
|
|
dispatchEvent(SCROLLVIEW_EVENT_SCROLLING, EventType::SCROLLING);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-06-19 09:25:46 +08:00
|
|
|
void ScrollView::dispatchEvent(ScrollviewEventType scrollEventType, EventType eventType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-19 09:25:46 +08:00
|
|
|
this->retain();
|
|
|
|
if (_scrollViewEventListener && _scrollViewEventSelector)
|
|
|
|
{
|
|
|
|
(_scrollViewEventListener->*_scrollViewEventSelector)(this, scrollEventType);
|
|
|
|
}
|
|
|
|
if (_eventCallback)
|
|
|
|
{
|
|
|
|
_eventCallback(this, eventType);
|
|
|
|
}
|
|
|
|
if (_ccEventCallback)
|
|
|
|
{
|
|
|
|
_ccEventCallback(this, static_cast<int>(eventType));
|
|
|
|
}
|
|
|
|
this->release();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::addEventListenerScrollView(Ref *target, SEL_ScrollViewEvent selector)
|
|
|
|
{
|
|
|
|
_scrollViewEventListener = target;
|
|
|
|
_scrollViewEventSelector = selector;
|
|
|
|
}
|
2015-04-15 16:37:11 +08:00
|
|
|
|
2014-05-14 16:14:28 +08:00
|
|
|
void ScrollView::addEventListener(const ccScrollViewCallback& callback)
|
2014-05-12 11:08:10 +08:00
|
|
|
{
|
|
|
|
_eventCallback = callback;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-05-12 11:08:10 +08:00
|
|
|
void ScrollView::setDirection(Direction dir)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
_direction = dir;
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:48:53 +08:00
|
|
|
ScrollView::Direction ScrollView::getDirection()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::setBounceEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
_bounceEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScrollView::isBounceEnabled() const
|
|
|
|
{
|
|
|
|
return _bounceEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::setInertiaScrollEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
_inertiaScrollEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScrollView::isInertiaScrollEnabled() const
|
|
|
|
{
|
|
|
|
return _inertiaScrollEnabled;
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:48:53 +08:00
|
|
|
Layout* ScrollView::getInnerContainer()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _innerContainer;
|
|
|
|
}
|
|
|
|
|
2014-05-12 18:00:54 +08:00
|
|
|
void ScrollView::setLayoutType(Type type)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
_innerContainer->setLayoutType(type);
|
|
|
|
}
|
|
|
|
|
2014-05-12 18:00:54 +08:00
|
|
|
Layout::Type ScrollView::getLayoutType() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _innerContainer->getLayoutType();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::doLayout()
|
|
|
|
{
|
|
|
|
if (!_doLayoutDirty)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_doLayoutDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ScrollView::getDescription() const
|
|
|
|
{
|
|
|
|
return "ScrollView";
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* ScrollView::createCloneInstance()
|
|
|
|
{
|
|
|
|
return ScrollView::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::copyClonedWidgetChildren(Widget* model)
|
|
|
|
{
|
|
|
|
Layout::copyClonedWidgetChildren(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::copySpecialProperties(Widget *widget)
|
|
|
|
{
|
|
|
|
ScrollView* scrollView = dynamic_cast<ScrollView*>(widget);
|
|
|
|
if (scrollView)
|
|
|
|
{
|
|
|
|
Layout::copySpecialProperties(widget);
|
|
|
|
setInnerContainerSize(scrollView->getInnerContainerSize());
|
|
|
|
setDirection(scrollView->_direction);
|
|
|
|
setBounceEnabled(scrollView->_bounceEnabled);
|
|
|
|
setInertiaScrollEnabled(scrollView->_inertiaScrollEnabled);
|
2014-05-23 15:03:46 +08:00
|
|
|
_scrollViewEventListener = scrollView->_scrollViewEventListener;
|
|
|
|
_scrollViewEventSelector = scrollView->_scrollViewEventSelector;
|
|
|
|
_eventCallback = scrollView->_eventCallback;
|
2014-11-27 16:49:19 +08:00
|
|
|
_ccEventCallback = scrollView->_ccEventCallback;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-15 16:37:11 +08:00
|
|
|
|
2014-05-23 10:59:18 +08:00
|
|
|
Widget* ScrollView::findNextFocusedWidget(cocos2d::ui::Widget::FocusDirection direction, cocos2d::ui::Widget *current)
|
|
|
|
{
|
|
|
|
if (this->getLayoutType() == Layout::Type::VERTICAL
|
2014-12-16 16:44:04 +08:00
|
|
|
|| this->getLayoutType() == Layout::Type::HORIZONTAL)
|
|
|
|
{
|
2014-05-23 10:59:18 +08:00
|
|
|
return _innerContainer->findNextFocusedWidget(direction, current);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Widget::findNextFocusedWidget(direction, current);
|
|
|
|
}
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|