axmol/core/ui/UIScrollView.cpp

1593 lines
45 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-08-08 18:02:17 +08:00
https://axys1.github.io/
2019-11-23 20:27:39 +08:00
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"
#include "base/CCDirector.h"
#include "base/ccUtils.h"
#include "platform/CCDevice.h"
#include "ui/UIScrollViewBar.h"
#include "2d/CCTweenFunction.h"
#include "2d/CCCamera.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
static const int NUMBER_OF_GATHERED_TOUCHES_FOR_MOVE_SPEED = 5;
2021-12-25 10:04:45 +08:00
static const float OUT_OF_BOUNDARY_BREAKING_FACTOR = 0.05f;
static const float BOUNCE_BACK_DURATION = 1.0f;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
#define MOVE_INCH 7.0f / 160.0f
2019-11-23 20:27:39 +08:00
static float convertDistanceFromPointToInch(const Vec2& dis)
{
2021-12-25 10:04:45 +08:00
auto glview = Director::getInstance()->getOpenGLView();
int dpi = Device::getDPI();
2019-11-23 20:27:39 +08:00
float distance = Vec2(dis.x * glview->getScaleX() / dpi, dis.y * glview->getScaleY() / dpi).getLength();
return distance;
}
2021-12-25 10:04:45 +08:00
namespace ui
{
2019-11-23 20:27:39 +08:00
IMPLEMENT_CLASS_GUI_INFO(ScrollView)
2021-12-25 10:04:45 +08:00
ScrollView::ScrollView()
: _innerContainer(nullptr)
, _direction(Direction::VERTICAL)
, _topBoundary(0.0f)
, _bottomBoundary(0.0f)
, _leftBoundary(0.0f)
, _rightBoundary(0.0f)
, _bePressed(false)
, _childFocusCancelOffsetInInch(MOVE_INCH)
, _touchMovePreviousTimestamp(0)
, _touchTotalTimeThreshold(0.5f)
, _scrolling(false)
, _autoScrolling(false)
, _autoScrollAttenuate(true)
, _autoScrollTotalTime(0)
, _autoScrollAccumulatedTime(0)
, _autoScrollCurrentlyOutOfBoundary(false)
, _autoScrollBraking(false)
, _inertiaScrollEnabled(true)
, _bounceEnabled(false)
, _outOfBoundaryAmount(Vec2::ZERO)
, _outOfBoundaryAmountDirty(true)
, _scrollBarEnabled(true)
, _verticalScrollBar(nullptr)
, _horizontalScrollBar(nullptr)
, _scrollViewEventListener(nullptr)
, _eventCallback(nullptr)
2019-11-23 20:27:39 +08:00
{
setTouchEnabled(true);
_propagateTouchEvents = false;
}
ScrollView::~ScrollView()
{
2021-12-25 10:04:45 +08:00
_verticalScrollBar = nullptr;
_horizontalScrollBar = nullptr;
2019-11-23 20:27:39 +08:00
_scrollViewEventListener = nullptr;
}
ScrollView* ScrollView::create()
{
2021-12-08 00:11:53 +08:00
ScrollView* widget = new ScrollView();
if (widget->init())
2019-11-23 20:27:39 +08:00
{
widget->autorelease();
return widget;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(widget);
2019-11-23 20:27:39 +08:00
return nullptr;
}
void ScrollView::onEnter()
{
Layout::onEnter();
scheduleUpdate();
}
void ScrollView::onExit()
{
Layout::onExit();
stopOverallScroll();
}
bool ScrollView::init()
{
if (Layout::init())
{
setClippingEnabled(true);
_innerContainer->setTouchEnabled(false);
2021-12-25 10:04:45 +08:00
if (_scrollBarEnabled)
2019-11-23 20:27:39 +08:00
{
initScrollBar();
}
return true;
}
return false;
}
void ScrollView::initRenderer()
{
Layout::initRenderer();
_innerContainer = Layout::create();
2021-12-25 10:04:45 +08:00
_innerContainer->setColor(Color3B(255, 255, 255));
2019-11-23 20:27:39 +08:00
_innerContainer->setOpacity(255);
_innerContainer->setCascadeColorEnabled(true);
_innerContainer->setCascadeOpacityEnabled(true);
addProtectedChild(_innerContainer, 1, 1);
}
void ScrollView::onSizeChanged()
{
Layout::onSizeChanged();
2021-12-25 10:04:45 +08:00
_topBoundary = _contentSize.height;
_rightBoundary = _contentSize.width;
Vec2 innerSize = _innerContainer->getContentSize();
float orginInnerSizeWidth = innerSize.width;
2019-11-23 20:27:39 +08:00
float orginInnerSizeHeight = innerSize.height;
2021-12-25 10:04:45 +08:00
float innerSizeWidth = MAX(orginInnerSizeWidth, _contentSize.width);
float innerSizeHeight = MAX(orginInnerSizeHeight, _contentSize.height);
2021-10-23 23:27:14 +08:00
_innerContainer->setContentSize(Vec2(innerSizeWidth, innerSizeHeight));
2019-11-23 20:27:39 +08:00
setInnerContainerPosition(Vec2(0.0f, _contentSize.height - _innerContainer->getContentSize().height));
if (_verticalScrollBar != nullptr)
{
_verticalScrollBar->onScrolled(getHowMuchOutOfBoundary());
}
if (_horizontalScrollBar != nullptr)
{
_horizontalScrollBar->onScrolled(getHowMuchOutOfBoundary());
}
}
2021-12-25 10:04:45 +08:00
void ScrollView::setInnerContainerSize(const Vec2& size)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
float innerSizeWidth = _contentSize.width;
float innerSizeHeight = _contentSize.height;
2021-10-23 23:27:14 +08:00
Vec2 originalInnerSize = _innerContainer->getContentSize();
if (size.width >= innerSizeWidth)
2019-11-23 20:27:39 +08:00
{
innerSizeWidth = size.width;
}
if (size.height >= innerSizeHeight)
2019-11-23 20:27:39 +08:00
{
innerSizeHeight = size.height;
}
2021-10-23 23:27:14 +08:00
_innerContainer->setContentSize(Vec2(innerSizeWidth, innerSizeHeight));
2019-11-23 20:27:39 +08:00
// Calculate and set the position of the inner container.
2021-12-25 10:04:45 +08:00
Vec2 pos = _innerContainer->getPosition();
const auto boundingBox = _innerContainer->getBoundingBox();
2021-12-25 10:04:45 +08:00
const auto rBoundary = boundingBox.origin.x + boundingBox.size.width;
const auto bBoundary = boundingBox.origin.y;
if (rBoundary < _contentSize.width)
{
// When the right border does not fit to the width,
// consider left-side fitting or right-side fitting.
const auto lBoundary = boundingBox.origin.x;
2021-12-25 10:04:45 +08:00
if (rBoundary - lBoundary > _contentSize.width)
{
// The width of _innerContainer is greater than _contentSize.width,use right-side fitting.
2021-12-25 10:04:45 +08:00
pos.x = _contentSize.width -
(1.f - _innerContainer->getAnchorPoint().x) * _innerContainer->getContentSize().width;
}
2021-12-25 10:04:45 +08:00
else
{
// or use left-side fitting.
pos.x = _innerContainer->getAnchorPoint().x * _innerContainer->getContentSize().width;
}
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
if (bBoundary > 0.f)
{
// Same as horizontal direction.
const auto tBoundary = boundingBox.origin.y + boundingBox.size.height;
2021-12-25 10:04:45 +08:00
if (tBoundary - bBoundary > _contentSize.height)
{
pos.y = _innerContainer->getAnchorPoint().y * _innerContainer->getContentSize().height;
}
2021-12-25 10:04:45 +08:00
else
{
pos.y = _contentSize.height -
(1.0f - _innerContainer->getAnchorPoint().y) * _innerContainer->getContentSize().height;
}
2019-11-23 20:27:39 +08:00
}
// In the vertical direction, if the _innerContainer initially contains content larger than _contentSize.height,
// this will result in bottom alignment.Obviously this is not what I want,
// so here is the rule : when the height of _innerContainer and _contentSize are equal,
// if the position of _innerContainer needs to be changed at this point,align _innerContainer to the top.
2021-12-25 10:04:45 +08:00
if (Direction::VERTICAL == _direction && originalInnerSize.height == _contentSize.height)
{
pos.y = _contentSize.height - innerSizeHeight;
}
2019-11-23 20:27:39 +08:00
setInnerContainerPosition(pos);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
updateScrollBar(Vec2::ZERO);
}
2021-10-23 23:27:14 +08:00
const Vec2& ScrollView::getInnerContainerSize() const
2019-11-23 20:27:39 +08:00
{
return _innerContainer->getContentSize();
}
2021-12-25 10:04:45 +08:00
void ScrollView::setInnerContainerPosition(const Vec2& position)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (position == _innerContainer->getPosition())
2019-11-23 20:27:39 +08:00
{
return;
}
_innerContainer->setPosition(position);
_outOfBoundaryAmountDirty = true;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Process bouncing events
2021-12-25 10:04:45 +08:00
if (_bounceEnabled)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
for (int direction = (int)MoveDirection::TOP; direction < (int)MoveDirection::RIGHT; ++direction)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (isOutOfBoundary((MoveDirection)direction))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
processScrollEvent((MoveDirection)direction, true);
2019-11-23 20:27:39 +08:00
}
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
this->retain();
if (_eventCallback)
{
_eventCallback(this, EventType::CONTAINER_MOVED);
}
if (_ccEventCallback)
{
_ccEventCallback(this, static_cast<int>(EventType::CONTAINER_MOVED));
}
this->release();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
const Vec2& ScrollView::getInnerContainerPosition() const
{
return _innerContainer->getPosition();
}
void ScrollView::addChild(Node* child)
{
ScrollView::addChild(child, child->getLocalZOrder(), child->getTag());
}
2021-12-25 10:04:45 +08:00
void ScrollView::addChild(Node* child, int localZOrder)
2019-11-23 20:27:39 +08:00
{
ScrollView::addChild(child, localZOrder, child->getTag());
}
2021-12-25 10:04:45 +08:00
void ScrollView::addChild(Node* child, int zOrder, int tag)
2019-11-23 20:27:39 +08:00
{
child->setGlobalZOrder(_globalZOrder);
_innerContainer->addChild(child, zOrder, tag);
}
void ScrollView::addChild(Node* child, int zOrder, std::string_view name)
2019-11-23 20:27:39 +08:00
{
child->setGlobalZOrder(_globalZOrder);
_innerContainer->addChild(child, zOrder, name);
}
void ScrollView::removeAllChildren()
{
removeAllChildrenWithCleanup(true);
}
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();
}
Node* ScrollView::getChildByTag(int tag) const
{
return _innerContainer->getChildByTag(tag);
}
Node* ScrollView::getChildByName(std::string_view name) const
2019-11-23 20:27:39 +08:00
{
return _innerContainer->getChildByName(name);
}
void ScrollView::moveInnerContainer(const Vec2& deltaMove, bool canStartBounceBack)
{
Vec2 adjustedMove = flattenVectorByDirection(deltaMove);
setInnerContainerPosition(getInnerContainerPosition() + adjustedMove);
Vec2 outOfBoundary = getHowMuchOutOfBoundary();
updateScrollBar(outOfBoundary);
2021-12-25 10:04:45 +08:00
if (_bounceEnabled && canStartBounceBack)
2019-11-23 20:27:39 +08:00
{
startBounceBackIfNeeded();
}
}
void ScrollView::updateScrollBar(const Vec2& outOfBoundary)
{
2021-12-25 10:04:45 +08:00
if (_verticalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_verticalScrollBar->onScrolled(outOfBoundary);
}
2021-12-25 10:04:45 +08:00
if (_horizontalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_horizontalScrollBar->onScrolled(outOfBoundary);
}
}
Vec2 ScrollView::calculateTouchMoveVelocity() const
{
float totalTime = 0;
for (auto&& timeDelta : _touchMoveTimeDeltas)
2019-11-23 20:27:39 +08:00
{
totalTime += timeDelta;
}
2021-12-25 10:04:45 +08:00
if (totalTime == 0 || totalTime >= _touchTotalTimeThreshold)
2019-11-23 20:27:39 +08:00
{
return Vec2::ZERO;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Vec2 totalMovement;
for (auto&& displacement : _touchMoveDisplacements)
2019-11-23 20:27:39 +08:00
{
totalMovement += displacement;
}
return totalMovement / totalTime;
}
void ScrollView::startInertiaScroll(const Vec2& touchMoveVelocity)
{
const float MOVEMENT_FACTOR = 0.7f;
2021-12-25 10:04:45 +08:00
Vec2 inertiaTotalMovement = touchMoveVelocity * MOVEMENT_FACTOR;
2019-11-23 20:27:39 +08:00
startAttenuatingAutoScroll(inertiaTotalMovement, touchMoveVelocity);
}
bool ScrollView::startBounceBackIfNeeded()
{
if (!_bounceEnabled)
{
return false;
}
Vec2 bounceBackAmount = getHowMuchOutOfBoundary();
2021-12-25 10:04:45 +08:00
if (fltEqualZero(bounceBackAmount))
2019-11-23 20:27:39 +08:00
{
return false;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
startAutoScroll(bounceBackAmount, BOUNCE_BACK_DURATION, true);
return true;
}
Vec2 ScrollView::flattenVectorByDirection(const Vec2& vector)
{
Vec2 result = vector;
2021-12-25 10:04:45 +08:00
result.x = (_direction == Direction::VERTICAL ? 0 : result.x);
result.y = (_direction == Direction::HORIZONTAL ? 0 : result.y);
2019-11-23 20:27:39 +08:00
return result;
}
Vec2 ScrollView::getHowMuchOutOfBoundary(const Vec2& addition)
{
2021-12-25 10:04:45 +08:00
if (addition == Vec2::ZERO && !_outOfBoundaryAmountDirty)
2019-11-23 20:27:39 +08:00
{
return _outOfBoundaryAmount;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Vec2 outOfBoundaryAmount(Vec2::ZERO);
2021-12-25 10:04:45 +08:00
if (_innerContainer->getLeftBoundary() + addition.x > _leftBoundary)
2019-11-23 20:27:39 +08:00
{
outOfBoundaryAmount.x = _leftBoundary - (_innerContainer->getLeftBoundary() + addition.x);
}
2021-12-25 10:04:45 +08:00
else if (_innerContainer->getRightBoundary() + addition.x < _rightBoundary)
2019-11-23 20:27:39 +08:00
{
outOfBoundaryAmount.x = _rightBoundary - (_innerContainer->getRightBoundary() + addition.x);
}
2021-12-25 10:04:45 +08:00
if (_innerContainer->getTopBoundary() + addition.y < _topBoundary)
2019-11-23 20:27:39 +08:00
{
outOfBoundaryAmount.y = _topBoundary - (_innerContainer->getTopBoundary() + addition.y);
}
2021-12-25 10:04:45 +08:00
else if (_innerContainer->getBottomBoundary() + addition.y > _bottomBoundary)
2019-11-23 20:27:39 +08:00
{
outOfBoundaryAmount.y = _bottomBoundary - (_innerContainer->getBottomBoundary() + addition.y);
}
2021-12-25 10:04:45 +08:00
if (addition == Vec2::ZERO)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_outOfBoundaryAmount = outOfBoundaryAmount;
2019-11-23 20:27:39 +08:00
_outOfBoundaryAmountDirty = false;
}
return outOfBoundaryAmount;
}
bool ScrollView::isOutOfBoundary(MoveDirection dir)
{
Vec2 outOfBoundary = getHowMuchOutOfBoundary();
2021-12-25 10:04:45 +08:00
switch (dir)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
case MoveDirection::TOP:
return outOfBoundary.y > 0;
case MoveDirection::BOTTOM:
return outOfBoundary.y < 0;
case MoveDirection::LEFT:
return outOfBoundary.x < 0;
case MoveDirection::RIGHT:
return outOfBoundary.x > 0;
2019-11-23 20:27:39 +08:00
}
return false;
}
bool ScrollView::isOutOfBoundary()
{
return !fltEqualZero(getHowMuchOutOfBoundary());
}
void ScrollView::startAutoScrollToDestination(const Vec2& destination, float timeInSec, bool attenuated)
{
startAutoScroll(destination - _innerContainer->getPosition(), timeInSec, attenuated);
}
static float calculateAutoScrollTimeByInitialSpeed(float initialSpeed)
{
// Calculate the time from the initial speed according to quintic polynomial.
float time = sqrtf(sqrtf(initialSpeed / 5));
return time;
}
void ScrollView::startAttenuatingAutoScroll(const Vec2& deltaMove, const Vec2& initialVelocity)
{
float time = calculateAutoScrollTimeByInitialSpeed(initialVelocity.length());
startAutoScroll(deltaMove, time, true);
}
void ScrollView::startAutoScroll(const Vec2& deltaMove, float timeInSec, bool attenuated)
{
Vec2 adjustedDeltaMove = flattenVectorByDirection(deltaMove);
2021-12-25 10:04:45 +08:00
_autoScrolling = true;
_autoScrollTargetDelta = adjustedDeltaMove;
_autoScrollAttenuate = attenuated;
_autoScrollStartPosition = _innerContainer->getPosition();
_autoScrollTotalTime = timeInSec;
_autoScrollAccumulatedTime = 0;
_autoScrollBraking = false;
2019-11-23 20:27:39 +08:00
_autoScrollBrakingStartPosition = Vec2::ZERO;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// If the destination is also out of boundary of same side, start brake from beginning.
Vec2 currentOutOfBoundary = getHowMuchOutOfBoundary();
if (!fltEqualZero(currentOutOfBoundary))
{
_autoScrollCurrentlyOutOfBoundary = true;
2021-12-25 10:04:45 +08:00
Vec2 afterOutOfBoundary = getHowMuchOutOfBoundary(adjustedDeltaMove);
if (currentOutOfBoundary.x * afterOutOfBoundary.x > 0 || currentOutOfBoundary.y * afterOutOfBoundary.y > 0)
2019-11-23 20:27:39 +08:00
{
_autoScrollBraking = true;
}
}
}
void ScrollView::stopScroll()
{
if (_scrolling)
{
if (_verticalScrollBar != nullptr)
{
_verticalScrollBar->onTouchEnded();
}
if (_horizontalScrollBar != nullptr)
{
_horizontalScrollBar->onTouchEnded();
}
_scrolling = false;
_bePressed = false;
startBounceBackIfNeeded();
dispatchEvent(EventType::SCROLLING_ENDED);
}
}
void ScrollView::stopAutoScroll()
{
if (_autoScrolling)
{
if (_verticalScrollBar != nullptr)
{
_verticalScrollBar->onTouchEnded();
}
if (_horizontalScrollBar != nullptr)
{
_horizontalScrollBar->onTouchEnded();
}
2021-12-25 10:04:45 +08:00
_autoScrolling = false;
_autoScrollAttenuate = true;
_autoScrollTotalTime = 0;
2019-11-23 20:27:39 +08:00
_autoScrollAccumulatedTime = 0;
dispatchEvent(EventType::AUTOSCROLL_ENDED);
}
}
void ScrollView::stopOverallScroll()
{
stopScroll();
stopAutoScroll();
}
bool ScrollView::isNecessaryAutoScrollBrake()
{
2021-12-25 10:04:45 +08:00
if (_autoScrollBraking)
2019-11-23 20:27:39 +08:00
{
return true;
}
2021-12-25 10:04:45 +08:00
if (isOutOfBoundary())
2019-11-23 20:27:39 +08:00
{
// It just went out of boundary.
2021-12-25 10:04:45 +08:00
if (!_autoScrollCurrentlyOutOfBoundary)
2019-11-23 20:27:39 +08:00
{
_autoScrollCurrentlyOutOfBoundary = true;
2021-12-25 10:04:45 +08:00
_autoScrollBraking = true;
_autoScrollBrakingStartPosition = getInnerContainerPosition();
2019-11-23 20:27:39 +08:00
return true;
}
}
else
{
_autoScrollCurrentlyOutOfBoundary = false;
}
return false;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
float ScrollView::getAutoScrollStopEpsilon() const
{
return FLT_EPSILON;
}
bool ScrollView::fltEqualZero(const Vec2& point) const
{
return (fabsf(point.x) <= 0.0001f && fabsf(point.y) <= 0.0001f);
}
void ScrollView::processAutoScrolling(float deltaTime)
{
// Make auto scroll shorter if it needs to deaccelerate.
float brakingFactor = (isNecessaryAutoScrollBrake() ? OUT_OF_BOUNDARY_BREAKING_FACTOR : 1);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Elapsed time
_autoScrollAccumulatedTime += deltaTime * (1 / brakingFactor);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Calculate the progress percentage
float percentage = MIN(1, _autoScrollAccumulatedTime / _autoScrollTotalTime);
2021-12-25 10:04:45 +08:00
if (_autoScrollAttenuate)
2019-11-23 20:27:39 +08:00
{
// Use quintic(5th degree) polynomial
percentage = tweenfunc::quintEaseOut(percentage);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Calculate the new position
Vec2 newPosition = _autoScrollStartPosition + (_autoScrollTargetDelta * percentage);
2021-12-25 10:04:45 +08:00
bool reachedEnd = std::abs(percentage - 1) <= this->getAutoScrollStopEpsilon();
2019-11-23 20:27:39 +08:00
if (reachedEnd)
{
newPosition = _autoScrollStartPosition + _autoScrollTargetDelta;
}
2021-12-25 10:04:45 +08:00
if (_bounceEnabled)
2019-11-23 20:27:39 +08:00
{
// The new position is adjusted if out of boundary
newPosition = _autoScrollBrakingStartPosition + (newPosition - _autoScrollBrakingStartPosition) * brakingFactor;
}
else
{
// Don't let go out of boundary
2021-12-25 10:04:45 +08:00
Vec2 moveDelta = newPosition - getInnerContainerPosition();
2019-11-23 20:27:39 +08:00
Vec2 outOfBoundary = getHowMuchOutOfBoundary(moveDelta);
if (!fltEqualZero(outOfBoundary))
{
newPosition += outOfBoundary;
reachedEnd = true;
}
}
// Finish auto scroll if it ended
2021-12-25 10:04:45 +08:00
if (reachedEnd)
2019-11-23 20:27:39 +08:00
{
_autoScrolling = false;
dispatchEvent(EventType::AUTOSCROLL_ENDED);
}
moveInnerContainer(newPosition - getInnerContainerPosition(), reachedEnd);
}
2021-12-25 10:04:45 +08:00
void ScrollView::jumpToDestination(const Vec2& des)
2019-11-23 20:27:39 +08:00
{
_autoScrolling = false;
moveInnerContainer(des - getInnerContainerPosition(), true);
}
void ScrollView::scrollChildren(const Vec2& deltaMove)
{
Vec2 realMove = deltaMove;
2021-12-25 10:04:45 +08:00
if (_bounceEnabled)
2019-11-23 20:27:39 +08:00
{
// If the position of the inner container is out of the boundary, the offsets should be divided by two.
Vec2 outOfBoundary = getHowMuchOutOfBoundary();
realMove.x *= (outOfBoundary.x == 0 ? 1 : 0.5f);
realMove.y *= (outOfBoundary.y == 0 ? 1 : 0.5f);
}
2021-12-25 10:04:45 +08:00
if (!_bounceEnabled)
2019-11-23 20:27:39 +08:00
{
Vec2 outOfBoundary = getHowMuchOutOfBoundary(realMove);
realMove += outOfBoundary;
}
2021-12-25 10:04:45 +08:00
bool scrolledToLeft = false;
bool scrolledToRight = false;
bool scrolledToTop = false;
2019-11-23 20:27:39 +08:00
bool scrolledToBottom = false;
2021-12-25 10:04:45 +08:00
if (realMove.y > 0.0f) // up
2019-11-23 20:27:39 +08:00
{
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + realMove.y >= _bottomBoundary)
{
scrolledToBottom = true;
}
}
2021-12-25 10:04:45 +08:00
else if (realMove.y < 0.0f) // down
2019-11-23 20:27:39 +08:00
{
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos + realMove.y <= _topBoundary)
{
scrolledToTop = true;
}
}
2021-12-25 10:04:45 +08:00
if (realMove.x < 0.0f) // left
2019-11-23 20:27:39 +08:00
{
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos + realMove.x <= _rightBoundary)
{
scrolledToRight = true;
}
}
2021-12-25 10:04:45 +08:00
else if (realMove.x > 0.0f) // right
2019-11-23 20:27:39 +08:00
{
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos + realMove.x >= _leftBoundary)
{
scrolledToLeft = true;
}
}
moveInnerContainer(realMove, false);
2021-12-25 10:04:45 +08:00
if (realMove.x != 0 || realMove.y != 0)
2019-11-23 20:27:39 +08:00
{
processScrollingEvent();
}
2021-12-25 10:04:45 +08:00
if (scrolledToBottom)
2019-11-23 20:27:39 +08:00
{
processScrollEvent(MoveDirection::BOTTOM, false);
}
2021-12-25 10:04:45 +08:00
if (scrolledToTop)
2019-11-23 20:27:39 +08:00
{
processScrollEvent(MoveDirection::TOP, false);
}
2021-12-25 10:04:45 +08:00
if (scrolledToLeft)
2019-11-23 20:27:39 +08:00
{
processScrollEvent(MoveDirection::LEFT, false);
}
2021-12-25 10:04:45 +08:00
if (scrolledToRight)
2019-11-23 20:27:39 +08:00
{
processScrollEvent(MoveDirection::RIGHT, false);
}
}
void ScrollView::scrollToBottom(float timeInSec, bool attenuated)
{
startAutoScrollToDestination(Vec2(_innerContainer->getPosition().x, 0.0f), timeInSec, attenuated);
}
void ScrollView::scrollToTop(float timeInSec, bool attenuated)
{
2021-12-25 10:04:45 +08:00
startAutoScrollToDestination(
Vec2(_innerContainer->getPosition().x, _contentSize.height - _innerContainer->getContentSize().height),
timeInSec, attenuated);
2019-11-23 20:27:39 +08:00
}
void ScrollView::scrollToLeft(float timeInSec, bool attenuated)
{
startAutoScrollToDestination(Vec2(0.0f, _innerContainer->getPosition().y), timeInSec, attenuated);
}
void ScrollView::scrollToRight(float timeInSec, bool attenuated)
{
2021-12-25 10:04:45 +08:00
startAutoScrollToDestination(
Vec2(_contentSize.width - _innerContainer->getContentSize().width, _innerContainer->getPosition().y), timeInSec,
attenuated);
2019-11-23 20:27:39 +08:00
}
void ScrollView::scrollToTopLeft(float timeInSec, bool attenuated)
{
if (_direction != Direction::BOTH)
{
2022-07-16 10:43:05 +08:00
AXLOG("Scroll direction is not both!");
2019-11-23 20:27:39 +08:00
return;
}
2021-12-25 10:04:45 +08:00
startAutoScrollToDestination(Vec2(0.0f, _contentSize.height - _innerContainer->getContentSize().height), timeInSec,
attenuated);
2019-11-23 20:27:39 +08:00
}
void ScrollView::scrollToTopRight(float timeInSec, bool attenuated)
{
if (_direction != Direction::BOTH)
{
2022-07-16 10:43:05 +08:00
AXLOG("Scroll direction is not both!");
2019-11-23 20:27:39 +08:00
return;
}
startAutoScrollToDestination(Vec2(_contentSize.width - _innerContainer->getContentSize().width,
2021-12-25 10:04:45 +08:00
_contentSize.height - _innerContainer->getContentSize().height),
timeInSec, attenuated);
2019-11-23 20:27:39 +08:00
}
void ScrollView::scrollToBottomLeft(float timeInSec, bool attenuated)
{
if (_direction != Direction::BOTH)
{
2022-07-16 10:43:05 +08:00
AXLOG("Scroll direction is not both!");
2019-11-23 20:27:39 +08:00
return;
}
startAutoScrollToDestination(Vec2::ZERO, timeInSec, attenuated);
}
void ScrollView::scrollToBottomRight(float timeInSec, bool attenuated)
{
if (_direction != Direction::BOTH)
{
2022-07-16 10:43:05 +08:00
AXLOG("Scroll direction is not both!");
2019-11-23 20:27:39 +08:00
return;
}
2021-12-25 10:04:45 +08:00
startAutoScrollToDestination(Vec2(_contentSize.width - _innerContainer->getContentSize().width, 0.0f), timeInSec,
attenuated);
2019-11-23 20:27:39 +08:00
}
void ScrollView::scrollToPercentVertical(float percent, float timeInSec, bool attenuated)
{
float minY = _contentSize.height - _innerContainer->getContentSize().height;
2021-12-25 10:04:45 +08:00
float h = -minY;
startAutoScrollToDestination(Vec2(_innerContainer->getPosition().x, minY + percent * h / 100.0f), timeInSec,
attenuated);
2019-11-23 20:27:39 +08:00
}
void ScrollView::scrollToPercentHorizontal(float percent, float timeInSec, bool attenuated)
{
float w = _innerContainer->getContentSize().width - _contentSize.width;
2021-12-25 10:04:45 +08:00
startAutoScrollToDestination(Vec2(-(percent * w / 100.0f), _innerContainer->getPosition().y), timeInSec,
attenuated);
2019-11-23 20:27:39 +08:00
}
void ScrollView::scrollToPercentBothDirection(const Vec2& percent, float timeInSec, bool attenuated)
{
if (_direction != Direction::BOTH)
{
return;
}
float minY = _contentSize.height - _innerContainer->getContentSize().height;
2021-12-25 10:04:45 +08:00
float h = -minY;
float w = _innerContainer->getContentSize().width - _contentSize.width;
2019-11-23 20:27:39 +08:00
startAutoScrollToDestination(Vec2(-(percent.x * w / 100.0f), minY + percent.y * h / 100.0f), timeInSec, attenuated);
}
2021-12-25 10:04:45 +08:00
float ScrollView::getScrolledPercentVertical() const
{
2019-11-23 20:27:39 +08:00
const float minY = getContentSize().height - getInnerContainerSize().height;
2021-12-25 10:04:45 +08:00
return (1.f - getInnerContainerPosition().y / minY) * 100.f;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
float ScrollView::getScrolledPercentHorizontal() const
{
2019-11-23 20:27:39 +08:00
const float minX = getContentSize().width - getInnerContainerSize().width;
return getInnerContainerPosition().x / minX * 100.f;
}
2021-12-25 10:04:45 +08:00
Vec2 ScrollView::getScrolledPercentBothDirection() const
{
2019-11-23 20:27:39 +08:00
return {getScrolledPercentHorizontal(), getScrolledPercentVertical()};
}
void ScrollView::jumpToBottom()
{
jumpToDestination(Vec2(_innerContainer->getPosition().x, 0.0f));
}
void ScrollView::jumpToTop()
{
2021-12-25 10:04:45 +08:00
jumpToDestination(
Vec2(_innerContainer->getPosition().x, _contentSize.height - _innerContainer->getContentSize().height));
2019-11-23 20:27:39 +08:00
}
void ScrollView::jumpToLeft()
{
jumpToDestination(Vec2(0.0f, _innerContainer->getPosition().y));
}
void ScrollView::jumpToRight()
{
2021-12-25 10:04:45 +08:00
jumpToDestination(
Vec2(_contentSize.width - _innerContainer->getContentSize().width, _innerContainer->getPosition().y));
2019-11-23 20:27:39 +08:00
}
void ScrollView::jumpToTopLeft()
{
if (_direction != Direction::BOTH)
{
2022-07-16 10:43:05 +08:00
AXLOG("Scroll direction is not both!");
2019-11-23 20:27:39 +08:00
return;
}
jumpToDestination(Vec2(0.0f, _contentSize.height - _innerContainer->getContentSize().height));
}
void ScrollView::jumpToTopRight()
{
if (_direction != Direction::BOTH)
{
2022-07-16 10:43:05 +08:00
AXLOG("Scroll direction is not both!");
2019-11-23 20:27:39 +08:00
return;
}
jumpToDestination(Vec2(_contentSize.width - _innerContainer->getContentSize().width,
_contentSize.height - _innerContainer->getContentSize().height));
}
void ScrollView::jumpToBottomLeft()
{
if (_direction != Direction::BOTH)
{
2022-07-16 10:43:05 +08:00
AXLOG("Scroll direction is not both!");
2019-11-23 20:27:39 +08:00
return;
}
jumpToDestination(Vec2::ZERO);
}
void ScrollView::jumpToBottomRight()
{
if (_direction != Direction::BOTH)
{
2022-07-16 10:43:05 +08:00
AXLOG("Scroll direction is not both!");
2019-11-23 20:27:39 +08:00
return;
}
jumpToDestination(Vec2(_contentSize.width - _innerContainer->getContentSize().width, 0.0f));
}
void ScrollView::jumpToPercentVertical(float percent)
{
float minY = _contentSize.height - _innerContainer->getContentSize().height;
2021-12-25 10:04:45 +08:00
float h = -minY;
2019-11-23 20:27:39 +08:00
jumpToDestination(Vec2(_innerContainer->getPosition().x, minY + percent * h / 100.0f));
}
void ScrollView::jumpToPercentHorizontal(float percent)
{
float w = _innerContainer->getContentSize().width - _contentSize.width;
jumpToDestination(Vec2(-(percent * w / 100.0f), _innerContainer->getPosition().y));
}
void ScrollView::jumpToPercentBothDirection(const Vec2& percent)
{
if (_direction != Direction::BOTH)
{
return;
}
float minY = _contentSize.height - _innerContainer->getContentSize().height;
2021-12-25 10:04:45 +08:00
float h = -minY;
float w = _innerContainer->getContentSize().width - _contentSize.width;
2019-11-23 20:27:39 +08:00
jumpToDestination(Vec2(-(percent.x * w / 100.0f), minY + percent.y * h / 100.0f));
}
bool ScrollView::calculateCurrAndPrevTouchPoints(Touch* touch, Vec3* currPt, Vec3* prevPt)
{
2021-12-25 10:04:45 +08:00
if (nullptr == _hittedByCamera || false == hitTest(touch->getLocation(), _hittedByCamera, currPt) ||
2019-11-23 20:27:39 +08:00
false == hitTest(touch->getPreviousLocation(), _hittedByCamera, prevPt))
{
return false;
}
return true;
}
void ScrollView::gatherTouchMove(const Vec2& delta)
{
2021-12-25 10:04:45 +08:00
while (_touchMoveDisplacements.size() >= NUMBER_OF_GATHERED_TOUCHES_FOR_MOVE_SPEED)
2019-11-23 20:27:39 +08:00
{
_touchMoveDisplacements.pop_front();
_touchMoveTimeDeltas.pop_front();
}
_touchMoveDisplacements.emplace_back(delta);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
long long timestamp = utils::getTimeInMilliseconds();
_touchMoveTimeDeltas.emplace_back((timestamp - _touchMovePreviousTimestamp) / 1000.0f);
2019-11-23 20:27:39 +08:00
_touchMovePreviousTimestamp = timestamp;
}
void ScrollView::handlePressLogic(Touch* /*touch*/)
{
2021-12-25 10:04:45 +08:00
_bePressed = true;
2019-11-23 20:27:39 +08:00
_autoScrolling = false;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Clear gathered touch move information
{
_touchMovePreviousTimestamp = utils::getTimeInMilliseconds();
_touchMoveDisplacements.clear();
_touchMoveTimeDeltas.clear();
}
2021-12-25 10:04:45 +08:00
if (_verticalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_verticalScrollBar->onTouchBegan();
}
2021-12-25 10:04:45 +08:00
if (_horizontalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_horizontalScrollBar->onTouchBegan();
}
}
2021-12-25 10:04:45 +08:00
void ScrollView::handleMoveLogic(Touch* touch)
2019-11-23 20:27:39 +08:00
{
if (!_bePressed)
return;
Vec3 currPt, prevPt;
2021-12-25 10:04:45 +08:00
if (!calculateCurrAndPrevTouchPoints(touch, &currPt, &prevPt))
2019-11-23 20:27:39 +08:00
{
return;
}
Vec3 delta3 = currPt - prevPt;
Vec2 delta(delta3.x, delta3.y);
scrollChildren(delta);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Gather touch move information for speed calculation
gatherTouchMove(delta);
}
2021-12-25 10:04:45 +08:00
void ScrollView::handleReleaseLogic(Touch* touch)
2019-11-23 20:27:39 +08:00
{
if (!_bePressed)
return;
// Gather the last touch information when released
{
Vec3 currPt, prevPt;
2021-12-25 10:04:45 +08:00
if (calculateCurrAndPrevTouchPoints(touch, &currPt, &prevPt))
2019-11-23 20:27:39 +08:00
{
Vec3 delta3 = currPt - prevPt;
Vec2 delta(delta3.x, delta3.y);
gatherTouchMove(delta);
}
}
_bePressed = false;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
bool bounceBackStarted = startBounceBackIfNeeded();
2021-12-25 10:04:45 +08:00
if (!bounceBackStarted && _inertiaScrollEnabled)
2019-11-23 20:27:39 +08:00
{
Vec2 touchMoveVelocity = calculateTouchMoveVelocity();
2021-12-25 10:04:45 +08:00
if (touchMoveVelocity != Vec2::ZERO)
2019-11-23 20:27:39 +08:00
{
startInertiaScroll(touchMoveVelocity);
}
}
2021-12-25 10:04:45 +08:00
if (_verticalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_verticalScrollBar->onTouchEnded();
}
2021-12-25 10:04:45 +08:00
if (_horizontalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_horizontalScrollBar->onTouchEnded();
}
2021-12-25 10:04:45 +08:00
if (_scrolling)
{
2019-11-23 20:27:39 +08:00
processScrollingEndedEvent();
}
}
2021-12-25 10:04:45 +08:00
bool ScrollView::onTouchBegan(Touch* touch, Event* unusedEvent)
2019-11-23 20:27:39 +08:00
{
bool pass = Layout::onTouchBegan(touch, unusedEvent);
if (!_isInterceptTouch)
{
if (_hitted)
{
handlePressLogic(touch);
}
}
return pass;
}
2021-12-25 10:04:45 +08:00
void ScrollView::onTouchMoved(Touch* touch, Event* unusedEvent)
2019-11-23 20:27:39 +08:00
{
Layout::onTouchMoved(touch, unusedEvent);
if (!_isInterceptTouch)
{
handleMoveLogic(touch);
}
}
2021-12-25 10:04:45 +08:00
void ScrollView::onTouchEnded(Touch* touch, Event* unusedEvent)
2019-11-23 20:27:39 +08:00
{
Layout::onTouchEnded(touch, unusedEvent);
if (!_isInterceptTouch)
{
handleReleaseLogic(touch);
}
_isInterceptTouch = false;
}
2021-12-25 10:04:45 +08:00
void ScrollView::onTouchCancelled(Touch* touch, Event* unusedEvent)
2019-11-23 20:27:39 +08:00
{
Layout::onTouchCancelled(touch, unusedEvent);
if (!_isInterceptTouch)
{
handleReleaseLogic(touch);
}
_isInterceptTouch = false;
}
void ScrollView::update(float dt)
{
if (_autoScrolling)
{
processAutoScrolling(dt);
}
}
2021-12-25 10:04:45 +08:00
void ScrollView::interceptTouchEvent(Widget::TouchEventType event, Widget* sender, Touch* touch)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (!_touchEnabled)
2019-11-23 20:27:39 +08:00
{
Layout::interceptTouchEvent(event, sender, touch);
return;
}
2021-12-25 10:04:45 +08:00
if (_direction == Direction::NONE)
2019-11-23 20:27:39 +08:00
return;
Vec2 touchPoint = touch->getLocation();
switch (event)
{
2021-12-25 10:04:45 +08:00
case TouchEventType::BEGAN:
{
_isInterceptTouch = true;
_touchBeganPosition = touch->getLocation();
handlePressLogic(touch);
}
break;
case TouchEventType::MOVED:
{
_touchMovePosition = touch->getLocation();
// calculates move offset in points
float offsetInInch = 0;
switch (_direction)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
case Direction::HORIZONTAL:
offsetInInch =
convertDistanceFromPointToInch(Vec2(std::abs(sender->getTouchBeganPosition().x - touchPoint.x), 0.0f));
break;
case Direction::VERTICAL:
offsetInInch =
convertDistanceFromPointToInch(Vec2(0.0f, std::abs(sender->getTouchBeganPosition().y - touchPoint.y)));
break;
case Direction::BOTH:
offsetInInch = convertDistanceFromPointToInch(sender->getTouchBeganPosition() - touchPoint);
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
if (offsetInInch > _childFocusCancelOffsetInInch)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
sender->setHighlighted(false);
handleMoveLogic(touch);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
}
break;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
case TouchEventType::CANCELED:
case TouchEventType::ENDED:
{
_touchEndPosition = touch->getLocation();
handleReleaseLogic(touch);
if (sender->isSwallowTouches())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_isInterceptTouch = false;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
}
break;
2019-11-23 20:27:39 +08:00
}
}
void ScrollView::processScrollEvent(MoveDirection dir, bool bounce)
{
EventType eventType;
2021-12-25 10:04:45 +08:00
switch (dir)
{
case MoveDirection::TOP:
{
eventType = (bounce ? EventType::BOUNCE_TOP : EventType::SCROLL_TO_TOP);
break;
}
case MoveDirection::BOTTOM:
{
eventType = (bounce ? EventType::BOUNCE_BOTTOM : EventType::SCROLL_TO_BOTTOM);
break;
}
case MoveDirection::LEFT:
{
eventType = (bounce ? EventType::BOUNCE_LEFT : EventType::SCROLL_TO_LEFT);
break;
}
case MoveDirection::RIGHT:
{
eventType = (bounce ? EventType::BOUNCE_RIGHT : EventType::SCROLL_TO_RIGHT);
break;
}
2019-11-23 20:27:39 +08:00
}
dispatchEvent(eventType);
}
void ScrollView::processScrollingEvent()
{
2021-12-25 10:04:45 +08:00
if (!_scrolling)
{
2019-11-23 20:27:39 +08:00
_scrolling = true;
dispatchEvent(EventType::SCROLLING_BEGAN);
}
dispatchEvent(EventType::SCROLLING);
}
2021-12-25 10:04:45 +08:00
void ScrollView::processScrollingEndedEvent()
{
2019-11-23 20:27:39 +08:00
_scrolling = false;
dispatchEvent(EventType::SCROLLING_ENDED);
}
void ScrollView::dispatchEvent(EventType eventType)
{
2021-12-25 10:04:45 +08:00
this->retain();
if (_eventCallback)
{
_eventCallback(this, eventType);
}
if (_ccEventCallback)
{
_ccEventCallback(this, static_cast<int>(eventType));
}
this->release();
2019-11-23 20:27:39 +08:00
}
void ScrollView::addEventListener(const ccScrollViewCallback& callback)
{
_eventCallback = callback;
}
void ScrollView::setDirection(Direction dir)
{
_direction = dir;
2021-12-25 10:04:45 +08:00
if (_scrollBarEnabled)
2019-11-23 20:27:39 +08:00
{
removeScrollBar();
initScrollBar();
}
}
2021-12-25 10:04:45 +08:00
ScrollView::Direction ScrollView::getDirection() const
2019-11-23 20:27:39 +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;
}
void ScrollView::setScrollBarEnabled(bool enabled)
{
2021-12-25 10:04:45 +08:00
if (_scrollBarEnabled == enabled)
2019-11-23 20:27:39 +08:00
{
return;
}
2021-12-25 10:04:45 +08:00
if (_scrollBarEnabled)
2019-11-23 20:27:39 +08:00
{
removeScrollBar();
}
_scrollBarEnabled = enabled;
2021-12-25 10:04:45 +08:00
if (_scrollBarEnabled)
2019-11-23 20:27:39 +08:00
{
initScrollBar();
}
}
bool ScrollView::isScrollBarEnabled() const
{
return _scrollBarEnabled;
}
void ScrollView::setScrollBarPositionFromCorner(const Vec2& positionFromCorner)
{
2021-12-25 10:04:45 +08:00
if (_direction != Direction::HORIZONTAL)
2019-11-23 20:27:39 +08:00
{
setScrollBarPositionFromCornerForVertical(positionFromCorner);
}
2021-12-25 10:04:45 +08:00
if (_direction != Direction::VERTICAL)
2019-11-23 20:27:39 +08:00
{
setScrollBarPositionFromCornerForHorizontal(positionFromCorner);
}
}
void ScrollView::setScrollBarPositionFromCornerForVertical(const Vec2& positionFromCorner)
{
2022-07-16 10:43:05 +08:00
AXASSERT(_scrollBarEnabled, "Scroll bar should be enabled!");
AXASSERT(_direction != Direction::HORIZONTAL, "Scroll view doesn't have a vertical scroll bar!");
2019-11-23 20:27:39 +08:00
_verticalScrollBar->setPositionFromCorner(positionFromCorner);
}
Vec2 ScrollView::getScrollBarPositionFromCornerForVertical() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(_scrollBarEnabled, "Scroll bar should be enabled!");
AXASSERT(_direction != Direction::HORIZONTAL, "Scroll view doesn't have a vertical scroll bar!");
2019-11-23 20:27:39 +08:00
return _verticalScrollBar->getPositionFromCorner();
}
void ScrollView::setScrollBarPositionFromCornerForHorizontal(const Vec2& positionFromCorner)
{
2022-07-16 10:43:05 +08:00
AXASSERT(_scrollBarEnabled, "Scroll bar should be enabled!");
AXASSERT(_direction != Direction::VERTICAL, "Scroll view doesn't have a horizontal scroll bar!");
2019-11-23 20:27:39 +08:00
_horizontalScrollBar->setPositionFromCorner(positionFromCorner);
}
Vec2 ScrollView::getScrollBarPositionFromCornerForHorizontal() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(_scrollBarEnabled, "Scroll bar should be enabled!");
AXASSERT(_direction != Direction::VERTICAL, "Scroll view doesn't have a horizontal scroll bar!");
2019-11-23 20:27:39 +08:00
return _horizontalScrollBar->getPositionFromCorner();
}
void ScrollView::setScrollBarWidth(float width)
{
2022-07-16 10:43:05 +08:00
AXASSERT(_scrollBarEnabled, "Scroll bar should be enabled!");
2021-12-25 10:04:45 +08:00
if (_verticalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_verticalScrollBar->setWidth(width);
}
2021-12-25 10:04:45 +08:00
if (_horizontalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_horizontalScrollBar->setWidth(width);
}
}
float ScrollView::getScrollBarWidth() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(_scrollBarEnabled, "Scroll bar should be enabled!");
2021-12-25 10:04:45 +08:00
if (_verticalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
return _verticalScrollBar->getWidth();
}
2021-12-25 10:04:45 +08:00
else if (_horizontalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
return _horizontalScrollBar->getWidth();
}
return 0;
}
void ScrollView::setScrollBarColor(const Color3B& color)
{
2022-07-16 10:43:05 +08:00
AXASSERT(_scrollBarEnabled, "Scroll bar should be enabled!");
2021-12-25 10:04:45 +08:00
if (_verticalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_verticalScrollBar->setColor(color);
}
2021-12-25 10:04:45 +08:00
if (_horizontalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_horizontalScrollBar->setColor(color);
}
}
const Color3B& ScrollView::getScrollBarColor() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(_scrollBarEnabled, "Scroll bar should be enabled!");
2021-12-25 10:04:45 +08:00
if (_verticalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
return _verticalScrollBar->getColor();
}
2021-12-25 10:04:45 +08:00
else if (_horizontalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
return _horizontalScrollBar->getColor();
}
return Color3B::WHITE;
}
void ScrollView::setScrollBarOpacity(uint8_t opacity)
{
2022-07-16 10:43:05 +08:00
AXASSERT(_scrollBarEnabled, "Scroll bar should be enabled!");
2021-12-25 10:04:45 +08:00
if (_verticalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_verticalScrollBar->setOpacity(opacity);
}
2021-12-25 10:04:45 +08:00
if (_horizontalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_horizontalScrollBar->setOpacity(opacity);
}
}
uint8_t ScrollView::getScrollBarOpacity() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(_scrollBarEnabled, "Scroll bar should be enabled!");
2021-12-25 10:04:45 +08:00
if (_verticalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
return _verticalScrollBar->getOpacity();
}
2021-12-25 10:04:45 +08:00
else if (_horizontalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
return _horizontalScrollBar->getOpacity();
}
return -1;
}
void ScrollView::setScrollBarAutoHideEnabled(bool autoHideEnabled)
{
2022-07-16 10:43:05 +08:00
AXASSERT(_scrollBarEnabled, "Scroll bar should be enabled!");
2021-12-25 10:04:45 +08:00
if (_verticalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_verticalScrollBar->setAutoHideEnabled(autoHideEnabled);
}
2021-12-25 10:04:45 +08:00
if (_horizontalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_horizontalScrollBar->setAutoHideEnabled(autoHideEnabled);
}
}
bool ScrollView::isScrollBarAutoHideEnabled() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(_scrollBarEnabled, "Scroll bar should be enabled!");
2021-12-25 10:04:45 +08:00
if (_verticalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
return _verticalScrollBar->isAutoHideEnabled();
}
2021-12-25 10:04:45 +08:00
else if (_horizontalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
return _horizontalScrollBar->isAutoHideEnabled();
}
return false;
}
void ScrollView::setScrollBarAutoHideTime(float autoHideTime)
{
2022-07-16 10:43:05 +08:00
AXASSERT(_scrollBarEnabled, "Scroll bar should be enabled!");
2021-12-25 10:04:45 +08:00
if (_verticalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_verticalScrollBar->setAutoHideTime(autoHideTime);
}
2021-12-25 10:04:45 +08:00
if (_horizontalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
_horizontalScrollBar->setAutoHideTime(autoHideTime);
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
float ScrollView::getScrollBarAutoHideTime() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(_scrollBarEnabled, "Scroll bar should be enabled!");
2021-12-25 10:04:45 +08:00
if (_verticalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
return _verticalScrollBar->getAutoHideTime();
}
2021-12-25 10:04:45 +08:00
else if (_horizontalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
return _horizontalScrollBar->getAutoHideTime();
}
return 0;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void ScrollView::setTouchTotalTimeThreshold(float touchTotalTimeThreshold)
{
_touchTotalTimeThreshold = touchTotalTimeThreshold;
}
float ScrollView::getTouchTotalTimeThreshold() const
{
return _touchTotalTimeThreshold;
}
2021-12-25 10:04:45 +08:00
Layout* ScrollView::getInnerContainer() const
2019-11-23 20:27:39 +08:00
{
return _innerContainer;
}
void ScrollView::setLayoutType(Type type)
{
_innerContainer->setLayoutType(type);
}
Layout::Type ScrollView::getLayoutType() const
{
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);
}
2021-12-25 10:04:45 +08:00
void ScrollView::copySpecialProperties(Widget* widget)
2019-11-23 20:27:39 +08:00
{
ScrollView* scrollView = dynamic_cast<ScrollView*>(widget);
if (scrollView)
{
Layout::copySpecialProperties(widget);
setDirection(scrollView->_direction);
setInnerContainerPosition(scrollView->getInnerContainerPosition());
setInnerContainerSize(scrollView->getInnerContainerSize());
2021-12-25 10:04:45 +08:00
_topBoundary = scrollView->_topBoundary;
_bottomBoundary = scrollView->_bottomBoundary;
_leftBoundary = scrollView->_leftBoundary;
_rightBoundary = scrollView->_rightBoundary;
_bePressed = scrollView->_bePressed;
_childFocusCancelOffsetInInch = scrollView->_childFocusCancelOffsetInInch;
_touchMoveDisplacements = scrollView->_touchMoveDisplacements;
_touchMoveTimeDeltas = scrollView->_touchMoveTimeDeltas;
_touchMovePreviousTimestamp = scrollView->_touchMovePreviousTimestamp;
_scrolling = scrollView->_scrolling;
_autoScrolling = scrollView->_autoScrolling;
_autoScrollAttenuate = scrollView->_autoScrollAttenuate;
_autoScrollStartPosition = scrollView->_autoScrollStartPosition;
_autoScrollTargetDelta = scrollView->_autoScrollTargetDelta;
_autoScrollTotalTime = scrollView->_autoScrollTotalTime;
_autoScrollAccumulatedTime = scrollView->_autoScrollAccumulatedTime;
2019-11-23 20:27:39 +08:00
_autoScrollCurrentlyOutOfBoundary = scrollView->_autoScrollCurrentlyOutOfBoundary;
2021-12-25 10:04:45 +08:00
_autoScrollBraking = scrollView->_autoScrollBraking;
_autoScrollBrakingStartPosition = scrollView->_autoScrollBrakingStartPosition;
2019-11-23 20:27:39 +08:00
setInertiaScrollEnabled(scrollView->_inertiaScrollEnabled);
setBounceEnabled(scrollView->_bounceEnabled);
_scrollViewEventListener = scrollView->_scrollViewEventListener;
2021-12-25 10:04:45 +08:00
_eventCallback = scrollView->_eventCallback;
_ccEventCallback = scrollView->_ccEventCallback;
2019-11-23 20:27:39 +08:00
setScrollBarEnabled(scrollView->isScrollBarEnabled());
2021-12-25 10:04:45 +08:00
if (isScrollBarEnabled())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (_direction != Direction::HORIZONTAL)
2019-11-23 20:27:39 +08:00
{
setScrollBarPositionFromCornerForVertical(scrollView->getScrollBarPositionFromCornerForVertical());
}
2021-12-25 10:04:45 +08:00
if (_direction != Direction::VERTICAL)
2019-11-23 20:27:39 +08:00
{
setScrollBarPositionFromCornerForHorizontal(scrollView->getScrollBarPositionFromCornerForHorizontal());
}
setScrollBarWidth(scrollView->getScrollBarWidth());
setScrollBarColor(scrollView->getScrollBarColor());
setScrollBarAutoHideEnabled(scrollView->isScrollBarAutoHideEnabled());
setScrollBarAutoHideTime(scrollView->getScrollBarAutoHideTime());
}
}
}
void ScrollView::initScrollBar()
{
2021-12-25 10:04:45 +08:00
if (_direction != Direction::HORIZONTAL && _verticalScrollBar == nullptr)
2019-11-23 20:27:39 +08:00
{
_verticalScrollBar = ScrollViewBar::create(this, Direction::VERTICAL);
addProtectedChild(_verticalScrollBar, 2);
}
2021-12-25 10:04:45 +08:00
if (_direction != Direction::VERTICAL && _horizontalScrollBar == nullptr)
2019-11-23 20:27:39 +08:00
{
_horizontalScrollBar = ScrollViewBar::create(this, Direction::HORIZONTAL);
addProtectedChild(_horizontalScrollBar, 2);
}
}
void ScrollView::removeScrollBar()
{
2021-12-25 10:04:45 +08:00
if (_verticalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
removeProtectedChild(_verticalScrollBar);
_verticalScrollBar = nullptr;
}
2021-12-25 10:04:45 +08:00
if (_horizontalScrollBar != nullptr)
2019-11-23 20:27:39 +08:00
{
removeProtectedChild(_horizontalScrollBar);
_horizontalScrollBar = nullptr;
}
}
2022-08-08 18:02:17 +08:00
Widget* ScrollView::findNextFocusedWidget(ax::ui::Widget::FocusDirection direction, ax::ui::Widget* current)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (this->getLayoutType() == Layout::Type::VERTICAL || this->getLayoutType() == Layout::Type::HORIZONTAL)
2019-11-23 20:27:39 +08:00
{
return _innerContainer->findNextFocusedWidget(direction, current);
}
else
{
return Widget::findNextFocusedWidget(direction, current);
}
}
2021-12-25 10:04:45 +08:00
} // namespace ui
2019-11-23 20:27:39 +08:00
NS_AX_END