2020-10-18 12:31:45 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2010 Sangwoo Im
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +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:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "ScrollView.h"
|
|
|
|
#include "platform/Device.h"
|
|
|
|
#include "2d/ActionInstant.h"
|
|
|
|
#include "2d/ActionInterval.h"
|
|
|
|
#include "2d/ActionTween.h"
|
|
|
|
#include "base/Director.h"
|
|
|
|
#include "base/EventDispatcher.h"
|
|
|
|
#include "renderer/Renderer.h"
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_EXT_BEGIN
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#define SCROLL_DEACCEL_RATE 0.95f
|
|
|
|
#define SCROLL_DEACCEL_DIST 1.0f
|
|
|
|
#define BOUNCE_DURATION 0.15f
|
|
|
|
#define INSET_RATIO 0.2f
|
|
|
|
#define MOVE_INCH 7.0f / 160.0f
|
|
|
|
#define BOUNCE_BACK_FACTOR 0.35f
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
static float convertDistanceFromPointToInch(float pointDis)
|
|
|
|
{
|
2023-12-01 09:04:51 +08:00
|
|
|
auto glView = Director::getInstance()->getGLView();
|
2022-09-05 14:13:52 +08:00
|
|
|
float factor = (glView->getScaleX() + glView->getScaleY()) / 2;
|
2020-10-18 12:31:45 +08:00
|
|
|
return pointDis * factor / Device::getDPI();
|
|
|
|
}
|
|
|
|
|
|
|
|
ScrollView::ScrollView()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _delegate(nullptr)
|
|
|
|
, _direction(Direction::BOTH)
|
|
|
|
, _dragging(false)
|
|
|
|
, _container(nullptr)
|
|
|
|
, _touchMoved(false)
|
|
|
|
, _bounceable(false)
|
|
|
|
, _clippingToBounds(false)
|
|
|
|
, _touchLength(0.0f)
|
|
|
|
, _minScale(0.0f)
|
|
|
|
, _maxScale(0.0f)
|
|
|
|
, _scissorRestored(false)
|
|
|
|
, _touchListener(nullptr)
|
|
|
|
, _animatedScrollAction(nullptr)
|
|
|
|
{}
|
|
|
|
|
|
|
|
ScrollView::~ScrollView() {}
|
|
|
|
|
|
|
|
ScrollView* ScrollView::create(Size size, Node* container /* = nullptr*/)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
ScrollView* pRet = new ScrollView();
|
|
|
|
if (pRet->initWithViewSize(size, container))
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(pRet);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScrollView* ScrollView::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
ScrollView* pRet = new ScrollView();
|
|
|
|
if (pRet->init())
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(pRet);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ScrollView::initWithViewSize(Size size, Node* container /* = nullptr*/)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
if (Layer::init())
|
|
|
|
{
|
|
|
|
_container = container;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (!this->_container)
|
|
|
|
{
|
|
|
|
_container = Layer::create();
|
|
|
|
_container->setIgnoreAnchorPointForPosition(false);
|
|
|
|
_container->setAnchorPoint(Vec2(0.0f, 0.0f));
|
|
|
|
}
|
|
|
|
|
|
|
|
this->setViewSize(size);
|
|
|
|
|
|
|
|
setTouchEnabled(true);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
_touches.reserve(EventTouch::MAX_TOUCHES);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_delegate = nullptr;
|
|
|
|
_bounceable = true;
|
2020-10-18 12:31:45 +08:00
|
|
|
_clippingToBounds = true;
|
|
|
|
//_container->setContentSize(Size::ZERO);
|
2021-12-25 10:04:45 +08:00
|
|
|
_direction = Direction::BOTH;
|
2020-10-18 12:31:45 +08:00
|
|
|
_container->setPosition(0.0f, 0.0f);
|
|
|
|
_touchLength = 0.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
this->addChild(_container);
|
|
|
|
_minScale = _maxScale = 1.0f;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScrollView::init()
|
|
|
|
{
|
|
|
|
return this->initWithViewSize(Size(200, 200), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScrollView::isNodeVisible(Node* node)
|
|
|
|
{
|
|
|
|
const Vec2 offset = this->getContentOffset();
|
2021-12-25 10:04:45 +08:00
|
|
|
const Size size = this->getViewSize();
|
|
|
|
const float scale = this->getZoomScale();
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
Rect viewRect;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
viewRect = Rect(-offset.x / scale, -offset.y / scale, size.width / scale, size.height / scale);
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
return viewRect.intersectsRect(node->getBoundingBox());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::pause(Ref* /*sender*/)
|
|
|
|
{
|
|
|
|
_container->pause();
|
|
|
|
|
|
|
|
auto& children = _container->getChildren();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& child : children)
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
child->pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::resume(Ref* /*sender*/)
|
|
|
|
{
|
|
|
|
auto& children = _container->getChildren();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& child : children)
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
child->resume();
|
|
|
|
}
|
|
|
|
|
|
|
|
_container->resume();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScrollView::isTouchEnabled() const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return _touchListener != nullptr;
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::setTouchEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
_eventDispatcher->removeEventListener(_touchListener);
|
|
|
|
_touchListener = nullptr;
|
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
_touchListener = EventListenerTouchOneByOne::create();
|
|
|
|
_touchListener->setSwallowTouches(true);
|
2022-07-16 10:43:05 +08:00
|
|
|
_touchListener->onTouchBegan = AX_CALLBACK_2(ScrollView::onTouchBegan, this);
|
|
|
|
_touchListener->onTouchMoved = AX_CALLBACK_2(ScrollView::onTouchMoved, this);
|
|
|
|
_touchListener->onTouchEnded = AX_CALLBACK_2(ScrollView::onTouchEnded, this);
|
|
|
|
_touchListener->onTouchCancelled = AX_CALLBACK_2(ScrollView::onTouchCancelled, this);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(_touchListener, this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_dragging = false;
|
2020-10-18 12:31:45 +08:00
|
|
|
_touchMoved = false;
|
|
|
|
_touches.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::setSwallowTouches(bool needSwallow)
|
|
|
|
{
|
|
|
|
if (_touchListener != nullptr)
|
|
|
|
{
|
|
|
|
_touchListener->setSwallowTouches(needSwallow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ScrollView::setContentOffset(Vec2 offset, bool animated /* = false*/)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
if (animated)
|
2021-12-25 10:04:45 +08:00
|
|
|
{ // animate scrolling
|
2020-10-18 12:31:45 +08:00
|
|
|
this->setContentOffsetInDuration(offset, BOUNCE_DURATION);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2020-10-18 12:31:45 +08:00
|
|
|
else
|
2021-12-25 10:04:45 +08:00
|
|
|
{ // set the container position directly
|
2020-10-18 12:31:45 +08:00
|
|
|
if (!_bounceable)
|
|
|
|
{
|
|
|
|
const Vec2 minOffset = this->minContainerOffset();
|
|
|
|
const Vec2 maxOffset = this->maxContainerOffset();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
offset.x = MAX(minOffset.x, MIN(maxOffset.x, offset.x));
|
|
|
|
offset.y = MAX(minOffset.y, MIN(maxOffset.y, offset.y));
|
|
|
|
}
|
|
|
|
|
|
|
|
_container->setPosition(offset);
|
|
|
|
|
|
|
|
if (_delegate != nullptr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_delegate->scrollViewDidScroll(this);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::setContentOffsetInDuration(Vec2 offset, float dt)
|
|
|
|
{
|
|
|
|
FiniteTimeAction *scroll, *expire;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (_animatedScrollAction)
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
stopAnimatedContentOffset();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
scroll = MoveTo::create(dt, offset);
|
2022-07-16 10:43:05 +08:00
|
|
|
expire = CallFuncN::create(AX_CALLBACK_1(ScrollView::stoppedAnimatedScroll, this));
|
2020-10-18 12:31:45 +08:00
|
|
|
_animatedScrollAction = _container->runAction(Sequence::create(scroll, expire, nullptr));
|
|
|
|
_animatedScrollAction->retain();
|
2022-07-16 10:43:05 +08:00
|
|
|
this->schedule(AX_SCHEDULE_SELECTOR(ScrollView::performedAnimatedScroll));
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ScrollView::stopAnimatedContentOffset()
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
stopAction(_animatedScrollAction);
|
|
|
|
_animatedScrollAction->release();
|
|
|
|
_animatedScrollAction = nullptr;
|
|
|
|
stoppedAnimatedScroll(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 ScrollView::getContentOffset()
|
|
|
|
{
|
|
|
|
return _container->getPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::setZoomScale(float s)
|
|
|
|
{
|
|
|
|
if (_container->getScale() != s)
|
|
|
|
{
|
|
|
|
Vec2 oldCenter, newCenter;
|
|
|
|
Vec2 center;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (_touchLength == 0.0f)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
center.set(_viewSize.width * 0.5f, _viewSize.height * 0.5f);
|
2020-10-18 12:31:45 +08:00
|
|
|
center = this->convertToWorldSpace(center);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
center = _touchPoint;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
oldCenter = _container->convertToNodeSpace(center);
|
|
|
|
_container->setScale(MAX(_minScale, MIN(_maxScale, s)));
|
|
|
|
newCenter = _container->convertToWorldSpace(oldCenter);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
const Vec2 offset = center - newCenter;
|
|
|
|
if (_delegate != nullptr)
|
|
|
|
{
|
|
|
|
_delegate->scrollViewDidZoom(this);
|
|
|
|
}
|
|
|
|
this->setContentOffset(_container->getPosition() + offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float ScrollView::getZoomScale()
|
|
|
|
{
|
|
|
|
return _container->getScale();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::setZoomScale(float s, bool animated)
|
|
|
|
{
|
|
|
|
if (animated)
|
|
|
|
{
|
|
|
|
this->setZoomScaleInDuration(s, BOUNCE_DURATION);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->setZoomScale(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::setZoomScaleInDuration(float s, float dt)
|
|
|
|
{
|
|
|
|
if (dt > 0)
|
|
|
|
{
|
|
|
|
if (_container->getScale() != s)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ActionTween* scaleAction;
|
2020-10-18 12:31:45 +08:00
|
|
|
scaleAction = ActionTween::create(dt, "zoomScale", _container->getScale(), s);
|
|
|
|
this->runAction(scaleAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->setZoomScale(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void ScrollView::updateTweenAction(float value, std::string_view /*key*/)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
this->setZoomScale(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::setViewSize(Size size)
|
|
|
|
{
|
|
|
|
_viewSize = size;
|
|
|
|
Layer::setContentSize(size);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* ScrollView::getContainer()
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
return this->_container;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ScrollView::setContainer(Node* pContainer)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
// Make sure that '_container' has a non-nullptr value since there are
|
|
|
|
// lots of logic that use '_container'.
|
|
|
|
if (nullptr == pContainer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->removeAllChildrenWithCleanup(true);
|
|
|
|
this->_container = pContainer;
|
|
|
|
|
|
|
|
this->_container->setIgnoreAnchorPointForPosition(false);
|
|
|
|
this->_container->setAnchorPoint(Vec2(0.0f, 0.0f));
|
|
|
|
|
|
|
|
this->addChild(this->_container);
|
|
|
|
|
|
|
|
this->setViewSize(this->_viewSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScrollView::hasVisibleParents() const
|
|
|
|
{
|
|
|
|
auto parent = this->getParent();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto c = parent; c != nullptr; c = c->getParent())
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!c->isVisible())
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::relocateContainer(bool animated)
|
|
|
|
{
|
|
|
|
Vec2 oldPoint, min, max;
|
|
|
|
float newX, newY;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
min = this->minContainerOffset();
|
|
|
|
max = this->maxContainerOffset();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
oldPoint = _container->getPosition();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
newX = oldPoint.x;
|
|
|
|
newY = oldPoint.y;
|
2020-10-18 12:31:45 +08:00
|
|
|
if (_direction == Direction::BOTH || _direction == Direction::HORIZONTAL)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
newX = MAX(newX, min.x);
|
|
|
|
newX = MIN(newX, max.x);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_direction == Direction::BOTH || _direction == Direction::VERTICAL)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
newY = MIN(newY, max.y);
|
|
|
|
newY = MAX(newY, min.y);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (newY != oldPoint.y || newX != oldPoint.x)
|
|
|
|
{
|
|
|
|
this->setContentOffset(Vec2(newX, newY), animated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 ScrollView::maxContainerOffset()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Point anchorPoint = _container->isIgnoreAnchorPointForPosition() ? Point::ZERO : _container->getAnchorPoint();
|
2020-10-18 12:31:45 +08:00
|
|
|
float contW = _container->getContentSize().width * _container->getScaleX();
|
|
|
|
float contH = _container->getContentSize().height * _container->getScaleY();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
return Vec2(anchorPoint.x * contW, anchorPoint.y * contH);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 ScrollView::minContainerOffset()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Point anchorPoint = _container->isIgnoreAnchorPointForPosition() ? Point::ZERO : _container->getAnchorPoint();
|
2020-10-18 12:31:45 +08:00
|
|
|
float contW = _container->getContentSize().width * _container->getScaleX();
|
|
|
|
float contH = _container->getContentSize().height * _container->getScaleY();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
return Vec2(_viewSize.width - (1 - anchorPoint.x) * contW, _viewSize.height - (1 - anchorPoint.y) * contH);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::deaccelerateScrolling(float /*dt*/)
|
|
|
|
{
|
|
|
|
if (_dragging)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
this->unschedule(AX_SCHEDULE_SELECTOR(ScrollView::deaccelerateScrolling));
|
2020-10-18 12:31:45 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
float newX, newY;
|
|
|
|
Vec2 maxInset, minInset;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
_container->setPosition(_container->getPosition() + _scrollDistance);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (_bounceable)
|
|
|
|
{
|
|
|
|
maxInset = _maxInset;
|
|
|
|
minInset = _minInset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
maxInset = this->maxContainerOffset();
|
|
|
|
minInset = this->minContainerOffset();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
newX = _container->getPosition().x;
|
|
|
|
newY = _container->getPosition().y;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_scrollDistance = _scrollDistance * SCROLL_DEACCEL_RATE;
|
|
|
|
this->setContentOffset(Vec2(newX, newY));
|
|
|
|
|
|
|
|
if ((fabsf(_scrollDistance.x) <= SCROLL_DEACCEL_DIST && fabsf(_scrollDistance.y) <= SCROLL_DEACCEL_DIST) ||
|
|
|
|
((_direction == Direction::BOTH || _direction == Direction::VERTICAL) &&
|
|
|
|
(newY >= maxInset.y || newY <= minInset.y)) ||
|
|
|
|
((_direction == Direction::BOTH || _direction == Direction::HORIZONTAL) &&
|
|
|
|
(newX >= maxInset.x || newX <= minInset.x)))
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
this->unschedule(AX_SCHEDULE_SELECTOR(ScrollView::deaccelerateScrolling));
|
2020-10-18 12:31:45 +08:00
|
|
|
this->relocateContainer(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ScrollView::stoppedAnimatedScroll(Node* /*node*/)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
this->unschedule(AX_SCHEDULE_SELECTOR(ScrollView::performedAnimatedScroll));
|
2021-12-25 10:04:45 +08:00
|
|
|
// After the animation stopped, "scrollViewDidScroll" should be invoked, this could fix the bug of lack of tableview
|
|
|
|
// cells.
|
2020-10-18 12:31:45 +08:00
|
|
|
if (_delegate != nullptr)
|
|
|
|
{
|
|
|
|
_delegate->scrollViewDidScroll(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::performedAnimatedScroll(float /*dt*/)
|
|
|
|
{
|
|
|
|
if (_dragging)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
this->unschedule(AX_SCHEDULE_SELECTOR(ScrollView::performedAnimatedScroll));
|
2020-10-18 12:31:45 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_delegate != nullptr)
|
|
|
|
{
|
|
|
|
_delegate->scrollViewDidScroll(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Size& ScrollView::getContentSize() const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return _container->getContentSize();
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ScrollView::setContentSize(const Size& size)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
if (this->getContainer() != nullptr)
|
|
|
|
{
|
|
|
|
this->getContainer()->setContentSize(size);
|
2021-12-25 10:04:45 +08:00
|
|
|
this->updateInset();
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::updateInset()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (this->getContainer() != nullptr)
|
|
|
|
{
|
|
|
|
_maxInset = this->maxContainerOffset();
|
|
|
|
_maxInset.set(_maxInset.x + _viewSize.width * INSET_RATIO, _maxInset.y + _viewSize.height * INSET_RATIO);
|
|
|
|
_minInset = this->minContainerOffset();
|
|
|
|
_minInset.set(_minInset.x - _viewSize.width * INSET_RATIO, _minInset.y - _viewSize.height * INSET_RATIO);
|
|
|
|
}
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* make sure all children go to the container
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void ScrollView::addChild(Node* child, int zOrder, int tag)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_container != child)
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
_container->addChild(child, zOrder, tag);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
Layer::addChild(child, zOrder, tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::removeChild(Node* node, bool cleanup)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_container != node)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
_container->removeChild(node, cleanup);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Layer::removeChild(node, cleanup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::removeAllChildrenWithCleanup(bool cleanup)
|
|
|
|
{
|
|
|
|
_container->removeAllChildrenWithCleanup(cleanup);
|
|
|
|
Layer::removeAllChildrenWithCleanup(cleanup);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::removeAllChildren()
|
|
|
|
{
|
|
|
|
removeAllChildrenWithCleanup(true);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void ScrollView::addChild(Node* child, int zOrder, std::string_view name)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
if (_container != child)
|
|
|
|
{
|
|
|
|
_container->addChild(child, zOrder, name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Layer::addChild(child, zOrder, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::beforeDraw()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// TODO: minggo
|
|
|
|
// ScrollView don't support drawing in 3D space
|
|
|
|
// _beforeDrawCommand.init(_globalZOrder);
|
2022-07-16 10:43:05 +08:00
|
|
|
// _beforeDrawCommand.func = AX_CALLBACK_0(ScrollView::onBeforeDraw, this);
|
2021-12-25 10:04:45 +08:00
|
|
|
// Director::getInstance()->getRenderer()->addCommand(&_beforeDrawCommand);
|
|
|
|
}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* clip this view so that outside of the visible bounds can be hidden.
|
|
|
|
*/
|
|
|
|
void ScrollView::onBeforeDraw()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// TODO: minggo
|
|
|
|
// if (_clippingToBounds)
|
|
|
|
// {
|
|
|
|
// _scissorRestored = false;
|
|
|
|
// Rect frame = getViewRect();
|
2023-12-01 09:04:51 +08:00
|
|
|
// auto glView = Director::getInstance()->getGLView();
|
2021-12-25 10:04:45 +08:00
|
|
|
//
|
2022-09-05 14:13:52 +08:00
|
|
|
// if (glView->getVR() == nullptr) {
|
|
|
|
// if (glView->isScissorEnabled()) {
|
2021-12-25 10:04:45 +08:00
|
|
|
// _scissorRestored = true;
|
2022-09-05 14:13:52 +08:00
|
|
|
// _parentScissorRect = glView->getScissorRect();
|
2021-12-25 10:04:45 +08:00
|
|
|
// //set the intersection of _parentScissorRect and frame as the new scissor rect
|
|
|
|
// if (frame.intersectsRect(_parentScissorRect)) {
|
|
|
|
// float x = MAX(frame.origin.x, _parentScissorRect.origin.x);
|
|
|
|
// float y = MAX(frame.origin.y, _parentScissorRect.origin.y);
|
|
|
|
// float xx = MIN(frame.origin.x + frame.size.width, _parentScissorRect.origin.x +
|
|
|
|
// _parentScissorRect.size.width); float yy = MIN(frame.origin.y + frame.size.height,
|
2022-09-05 14:13:52 +08:00
|
|
|
// _parentScissorRect.origin.y + _parentScissorRect.size.height); glView->setScissorInPoints(x,
|
2021-12-25 10:04:45 +08:00
|
|
|
// y, xx - x, yy - y);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// glEnable(GL_SCISSOR_TEST);
|
2022-09-05 14:13:52 +08:00
|
|
|
// glView->setScissorInPoints(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
|
2021-12-25 10:04:45 +08:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::afterDraw()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// TODO: minggo
|
|
|
|
// _afterDrawCommand.init(_globalZOrder);
|
2022-07-16 10:43:05 +08:00
|
|
|
// _afterDrawCommand.func = AX_CALLBACK_0(ScrollView::onAfterDraw, this);
|
2021-12-25 10:04:45 +08:00
|
|
|
// Director::getInstance()->getRenderer()->addCommand(&_afterDrawCommand);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* retract what's done in beforeDraw so that there's no side effect to
|
|
|
|
* other nodes.
|
|
|
|
*/
|
|
|
|
void ScrollView::onAfterDraw()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// TODO:minggo
|
|
|
|
// if (_clippingToBounds)
|
|
|
|
// {
|
2023-12-01 09:04:51 +08:00
|
|
|
// auto glView = Director::getInstance()->getGLView();
|
2022-09-05 14:13:52 +08:00
|
|
|
// if (glView->getVR() == nullptr) {
|
2021-12-25 10:04:45 +08:00
|
|
|
// if (_scissorRestored) {//restore the parent's scissor rect
|
2022-09-05 14:13:52 +08:00
|
|
|
// glView->setScissorInPoints(_parentScissorRect.origin.x, _parentScissorRect.origin.y,
|
2021-12-25 10:04:45 +08:00
|
|
|
// _parentScissorRect.size.width, _parentScissorRect.size.height);
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// glDisable(GL_SCISSOR_TEST);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ScrollView::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
// quick return if not visible
|
|
|
|
if (!isVisible())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t flags = processParentFlags(parentTransform, parentFlags);
|
|
|
|
|
|
|
|
// IMPORTANT:
|
|
|
|
// To ease the migration to v3.0, we still support the Mat4 stack,
|
|
|
|
// but it is deprecated and your code should not rely on it
|
|
|
|
Director* director = Director::getInstance();
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(nullptr != director, "Director is null when setting matrix stack");
|
2020-10-18 12:31:45 +08:00
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
|
|
|
|
|
|
|
|
this->beforeDraw();
|
|
|
|
bool visibleByCamera = isVisitableByVisitingCamera();
|
|
|
|
|
|
|
|
if (!_children.empty())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
// draw children zOrder < 0
|
|
|
|
for (; i < _children.size(); i++)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* child = _children.at(i);
|
|
|
|
if (child->getLocalZOrder() < 0)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
child->visit(renderer, _modelViewTransform, flags);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// this draw
|
2020-10-18 12:31:45 +08:00
|
|
|
if (visibleByCamera)
|
|
|
|
this->draw(renderer, _modelViewTransform, flags);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
// draw children zOrder >= 0
|
2021-12-25 10:04:45 +08:00
|
|
|
for (; i < _children.size(); i++)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* child = _children.at(i);
|
|
|
|
child->visit(renderer, _modelViewTransform, flags);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (visibleByCamera)
|
|
|
|
{
|
|
|
|
this->draw(renderer, _modelViewTransform, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->afterDraw();
|
|
|
|
|
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScrollView::onTouchBegan(Touch* touch, Event* /*event*/)
|
|
|
|
{
|
|
|
|
if (!this->isVisible() || !this->hasVisibleParents())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
Rect frame = getViewRect();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// dispatcher does not know about clipping. reject touches outside visible bounds.
|
|
|
|
if (_touches.size() > 2 || _touchMoved || !frame.containsPoint(touch->getLocation()))
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::find(_touches.begin(), _touches.end(), touch) == _touches.end())
|
|
|
|
{
|
2022-08-09 09:54:53 +08:00
|
|
|
_touches.emplace_back(touch);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_touches.size() == 1)
|
2021-12-25 10:04:45 +08:00
|
|
|
{ // scrolling
|
|
|
|
_touchPoint = this->convertTouchToNodeSpace(touch);
|
|
|
|
_touchMoved = false;
|
|
|
|
_dragging = true; // dragging started
|
2020-10-18 12:31:45 +08:00
|
|
|
_scrollDistance.setZero();
|
2021-12-25 10:04:45 +08:00
|
|
|
_touchLength = 0.0f;
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
else if (_touches.size() == 2)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_touchPoint =
|
|
|
|
(this->convertTouchToNodeSpace(_touches[0]).getMidpoint(this->convertTouchToNodeSpace(_touches[1])));
|
|
|
|
|
|
|
|
_touchLength = _container->convertTouchToNodeSpace(_touches[0])
|
|
|
|
.getDistance(_container->convertTouchToNodeSpace(_touches[1]));
|
|
|
|
|
|
|
|
_dragging = false;
|
|
|
|
}
|
2020-10-18 12:31:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::onTouchMoved(Touch* touch, Event* /*event*/)
|
|
|
|
{
|
|
|
|
if (!this->isVisible())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::find(_touches.begin(), _touches.end(), touch) != _touches.end())
|
|
|
|
{
|
|
|
|
if (_touches.size() == 1 && _dragging)
|
2021-12-25 10:04:45 +08:00
|
|
|
{ // scrolling
|
2020-10-18 12:31:45 +08:00
|
|
|
Vec2 moveDistance, newPoint;
|
2021-12-25 10:04:45 +08:00
|
|
|
Rect frame;
|
2020-10-18 12:31:45 +08:00
|
|
|
float newX, newY;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
frame = getViewRect();
|
|
|
|
|
|
|
|
newPoint = this->convertTouchToNodeSpace(_touches[0]);
|
|
|
|
moveDistance = newPoint - _touchPoint;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
float dis = 0.0f;
|
|
|
|
if (_direction == Direction::VERTICAL)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
dis = moveDistance.y;
|
2020-10-18 12:31:45 +08:00
|
|
|
float pos = _container->getPosition().y;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!(minContainerOffset().y <= pos && pos <= maxContainerOffset().y))
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
moveDistance.y *= BOUNCE_BACK_FACTOR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_direction == Direction::HORIZONTAL)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
dis = moveDistance.x;
|
2020-10-18 12:31:45 +08:00
|
|
|
float pos = _container->getPosition().x;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!(minContainerOffset().x <= pos && pos <= maxContainerOffset().x))
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
moveDistance.x *= BOUNCE_BACK_FACTOR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
dis = sqrtf(moveDistance.x * moveDistance.x + moveDistance.y * moveDistance.y);
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
float pos = _container->getPosition().y;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!(minContainerOffset().y <= pos && pos <= maxContainerOffset().y))
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
moveDistance.y *= BOUNCE_BACK_FACTOR;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
pos = _container->getPosition().x;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!(minContainerOffset().x <= pos && pos <= maxContainerOffset().x))
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
moveDistance.x *= BOUNCE_BACK_FACTOR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_touchMoved && fabs(convertDistanceFromPointToInch(dis)) < MOVE_INCH)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
// AXLOG("Invalid movement, distance = [%f, %f], disInch = %f", moveDistance.x, moveDistance.y);
|
2020-10-18 12:31:45 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (!_touchMoved)
|
|
|
|
{
|
|
|
|
moveDistance.setZero();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
_touchPoint = newPoint;
|
|
|
|
_touchMoved = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (_dragging)
|
|
|
|
{
|
|
|
|
switch (_direction)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case Direction::VERTICAL:
|
|
|
|
moveDistance.set(0.0f, moveDistance.y);
|
|
|
|
break;
|
|
|
|
case Direction::HORIZONTAL:
|
|
|
|
moveDistance.set(moveDistance.x, 0.0f);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
newX = _container->getPosition().x + moveDistance.x;
|
|
|
|
newY = _container->getPosition().y + moveDistance.y;
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
_scrollDistance = moveDistance;
|
|
|
|
this->setContentOffset(Vec2(newX, newY));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_touches.size() == 2 && !_dragging)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const float len = _container->convertTouchToNodeSpace(_touches[0])
|
|
|
|
.getDistance(_container->convertTouchToNodeSpace(_touches[1]));
|
|
|
|
this->setZoomScale(this->getZoomScale() * len / _touchLength);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::onTouchEnded(Touch* touch, Event* /*event*/)
|
|
|
|
{
|
|
|
|
if (!this->isVisible())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
auto touchIter = std::find(_touches.begin(), _touches.end(), touch);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (touchIter != _touches.end())
|
|
|
|
{
|
|
|
|
if (_touches.size() == 1 && _touchMoved)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
this->schedule(AX_SCHEDULE_SELECTOR(ScrollView::deaccelerateScrolling));
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
_touches.erase(touchIter);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
if (_touches.size() == 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_dragging = false;
|
2020-10-18 12:31:45 +08:00
|
|
|
_touchMoved = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollView::onTouchCancelled(Touch* touch, Event* /*event*/)
|
|
|
|
{
|
|
|
|
if (!this->isVisible())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
auto touchIter = std::find(_touches.begin(), _touches.end(), touch);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (touchIter == _touches.end())
|
2020-10-18 12:31:45 +08:00
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
_touches.erase(touchIter);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (_touches.size() == 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_dragging = false;
|
2020-10-18 12:31:45 +08:00
|
|
|
_touchMoved = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect ScrollView::getViewRect()
|
|
|
|
{
|
|
|
|
Vec2 screenPos = this->convertToWorldSpace(Vec2::ZERO);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
float scaleX = this->getScaleX();
|
|
|
|
float scaleY = this->getScaleY();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (Node* p = _parent; p != nullptr; p = p->getParent())
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
scaleX *= p->getScaleX();
|
|
|
|
scaleY *= p->getScaleY();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Support negative scaling. Not doing so causes intersectsRect calls
|
|
|
|
// (eg: to check if the touch was within the bounds) to return false.
|
|
|
|
// Note, Node::getScale will assert if X and Y scales are different.
|
2021-12-25 10:04:45 +08:00
|
|
|
if (scaleX < 0.f)
|
|
|
|
{
|
|
|
|
screenPos.x += _viewSize.width * scaleX;
|
2020-10-18 12:31:45 +08:00
|
|
|
scaleX = -scaleX;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (scaleY < 0.f)
|
|
|
|
{
|
|
|
|
screenPos.y += _viewSize.height * scaleY;
|
2020-10-18 12:31:45 +08:00
|
|
|
scaleY = -scaleY;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
return Rect(screenPos.x, screenPos.y, _viewSize.width * scaleX, _viewSize.height * scaleY);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_EXT_END
|