axmol/core/ui/UIPageView.cpp

486 lines
12 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-10-01 16:24:52 +08:00
https://axmolengine.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/UIPageView.h"
#include "ui/UIPageViewIndicator.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
namespace ui
{
2019-11-23 20:27:39 +08:00
IMPLEMENT_CLASS_GUI_INFO(PageView)
2021-12-25 10:04:45 +08:00
PageView::PageView()
: _indicator(nullptr)
, _indicatorPositionAsAnchorPoint(Vec2(0.5f, 0.1f))
, _currentPageIndex(-1)
, _childFocusCancelOffset(5.0f)
, _eventCallback(nullptr)
, _autoScrollStopEpsilon(0.001f)
, _previousPageIndex(-1)
, _isTouchBegin(false)
{}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PageView::~PageView() {}
2019-11-23 20:27:39 +08:00
PageView* PageView::create()
{
2021-12-08 00:11:53 +08:00
PageView* widget = new PageView();
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;
}
bool PageView::init()
{
if (ListView::init())
{
setDirection(Direction::HORIZONTAL);
setMagneticType(MagneticType::CENTER);
setScrollBarEnabled(false);
return true;
}
return false;
}
void PageView::doLayout()
{
2021-12-25 10:04:45 +08:00
if (!_innerContainerDoLayoutDirty)
2019-11-23 20:27:39 +08:00
{
return;
}
ListView::doLayout();
2021-12-25 10:04:45 +08:00
if (_indicator != nullptr)
2019-11-23 20:27:39 +08:00
{
_currentPageIndex = getIndex(getCenterItemInCurrentView());
_indicator->indicate(_currentPageIndex);
}
_innerContainerDoLayoutDirty = false;
}
void PageView::setDirection(PageView::Direction direction)
{
ListView::setDirection(direction);
2021-12-25 10:04:45 +08:00
if (direction == Direction::HORIZONTAL)
2019-11-23 20:27:39 +08:00
{
_indicatorPositionAsAnchorPoint = Vec2(0.5f, 0.1f);
}
2021-12-25 10:04:45 +08:00
else if (direction == Direction::VERTICAL)
2019-11-23 20:27:39 +08:00
{
_indicatorPositionAsAnchorPoint = Vec2(0.1f, 0.5f);
}
2021-12-25 10:04:45 +08:00
if (_indicator != nullptr)
2019-11-23 20:27:39 +08:00
{
_indicator->setDirection(direction);
refreshIndicatorPosition();
}
}
void PageView::addPage(Widget* page)
{
pushBackCustomItem(page);
}
void PageView::insertPage(Widget* page, int idx)
{
insertCustomItem(page, idx);
}
void PageView::removePage(Widget* page)
{
removeItem(getIndex(page));
}
void PageView::removePageAtIndex(ssize_t index)
{
removeItem(index);
}
void PageView::removeAllPages()
{
removeAllItems();
}
ssize_t PageView::getCurrentPageIndex()
{
2021-12-25 10:04:45 +08:00
// The _currentPageIndex is lazy calculated
if (_innerContainerDoLayoutDirty)
{
2019-11-23 20:27:39 +08:00
_currentPageIndex = getIndex(getCenterItemInCurrentView());
}
return _currentPageIndex;
}
void PageView::setCurrentPageIndex(ssize_t index)
{
jumpToItem(index, Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE);
}
void PageView::scrollToPage(ssize_t idx)
{
scrollToItem(idx);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void PageView::scrollToPage(ssize_t idx, float time)
{
scrollToItem(idx, time);
}
void PageView::scrollToItem(ssize_t itemIndex)
{
2021-12-25 10:04:45 +08:00
if (_innerContainerDoLayoutDirty)
{
2019-11-23 20:27:39 +08:00
this->forceDoLayout();
}
ListView::scrollToItem(itemIndex, Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE);
}
void PageView::scrollToItem(ssize_t itemIndex, float time)
{
2021-12-25 10:04:45 +08:00
if (_innerContainerDoLayoutDirty)
{
2019-11-23 20:27:39 +08:00
this->forceDoLayout();
}
ListView::scrollToItem(itemIndex, Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE, time >= 0 ? time : _scrollTime);
}
void PageView::setAutoScrollStopEpsilon(float epsilon)
{
_autoScrollStopEpsilon = epsilon;
}
void PageView::moveInnerContainer(const Vec2& deltaMove, bool canStartBounceBack)
{
ListView::moveInnerContainer(deltaMove, canStartBounceBack);
_currentPageIndex = getIndex(getCenterItemInCurrentView());
2021-12-25 10:04:45 +08:00
if (_indicator != nullptr)
2019-11-23 20:27:39 +08:00
{
_indicator->indicate(_currentPageIndex);
}
}
void PageView::onItemListChanged()
{
ListView::onItemListChanged();
2021-12-25 10:04:45 +08:00
if (_indicator != nullptr)
2019-11-23 20:27:39 +08:00
{
_indicator->reset(_items.size());
}
}
void PageView::onSizeChanged()
{
ListView::onSizeChanged();
refreshIndicatorPosition();
}
void PageView::refreshIndicatorPosition()
{
2021-12-25 10:04:45 +08:00
if (_indicator != nullptr)
2019-11-23 20:27:39 +08:00
{
2021-10-23 23:27:14 +08:00
const Vec2& contentSize = getContentSize();
2021-12-25 10:04:45 +08:00
float posX = contentSize.width * _indicatorPositionAsAnchorPoint.x;
float posY = contentSize.height * _indicatorPositionAsAnchorPoint.y;
2019-11-23 20:27:39 +08:00
_indicator->setPosition(Vec2(posX, posY));
}
}
2021-12-25 10:04:45 +08:00
void PageView::handlePressLogic(Touch* touch)
2019-11-23 20:27:39 +08:00
{
ListView::handlePressLogic(touch);
2021-12-25 10:04:45 +08:00
if (!_isTouchBegin)
{
_currentPageIndex = getIndex(getCenterItemInCurrentView());
2019-11-23 20:27:39 +08:00
_previousPageIndex = _currentPageIndex;
2021-12-25 10:04:45 +08:00
_isTouchBegin = true;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
void PageView::handleReleaseLogic(Touch* touch)
2019-11-23 20:27:39 +08:00
{
// Use `ScrollView` method in order to avoid `startMagneticScroll()` by `ListView`.
ScrollView::handleReleaseLogic(touch);
2021-12-25 10:04:45 +08:00
if (_items.empty())
2019-11-23 20:27:39 +08:00
{
return;
}
Vec2 touchMoveVelocity = flattenVectorByDirection(calculateTouchMoveVelocity());
static const float INERTIA_THRESHOLD = 500;
2021-12-25 10:04:45 +08:00
if (touchMoveVelocity.length() < INERTIA_THRESHOLD)
2019-11-23 20:27:39 +08:00
{
startMagneticScroll();
}
else
{
// Handle paging by inertia force.
2021-12-25 10:04:45 +08:00
Widget* currentPage = getItem(_currentPageIndex);
Vec2 destination = calculateItemDestination(Vec2::ANCHOR_MIDDLE, currentPage, Vec2::ANCHOR_MIDDLE);
2019-11-23 20:27:39 +08:00
Vec2 deltaToCurrentpage = destination - getInnerContainerPosition();
2021-12-25 10:04:45 +08:00
deltaToCurrentpage = flattenVectorByDirection(deltaToCurrentpage);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// If the direction of displacement to current page and the direction of touch are same, just start magnetic
// scroll to the current page. Otherwise, move to the next page of touch direction.
if (touchMoveVelocity.x * deltaToCurrentpage.x > 0 || touchMoveVelocity.y * deltaToCurrentpage.y > 0)
2019-11-23 20:27:39 +08:00
{
startMagneticScroll();
}
else
{
2021-12-25 10:04:45 +08:00
if (touchMoveVelocity.x < 0 || touchMoveVelocity.y > 0)
2019-11-23 20:27:39 +08:00
{
++_currentPageIndex;
}
else
{
--_currentPageIndex;
}
_currentPageIndex = MIN(_currentPageIndex, _items.size() - 1);
_currentPageIndex = MAX(_currentPageIndex, 0);
scrollToItem(_currentPageIndex);
}
}
}
float PageView::getAutoScrollStopEpsilon() const
{
return _autoScrollStopEpsilon;
}
void PageView::pageTurningEvent()
{
this->retain();
if (_eventCallback)
{
2021-12-25 10:04:45 +08:00
_eventCallback(this, EventType::TURNING);
2019-11-23 20:27:39 +08:00
}
if (_ccEventCallback)
{
_ccEventCallback(this, static_cast<int>(EventType::TURNING));
}
_isTouchBegin = false;
this->release();
}
void PageView::addEventListener(const ccPageViewCallback& callback)
{
2021-12-25 10:04:45 +08:00
_eventCallback = callback;
ccScrollViewCallback scrollViewCallback = [this](Object* /*ref*/, ScrollView::EventType type) -> void {
2021-12-25 10:04:45 +08:00
if (type == ScrollView::EventType::AUTOSCROLL_ENDED && _previousPageIndex != _currentPageIndex)
{
2019-11-23 20:27:39 +08:00
pageTurningEvent();
}
};
this->addEventListener(scrollViewCallback);
}
std::string PageView::getDescription() const
{
return "PageView";
}
Widget* PageView::createCloneInstance()
{
return PageView::create();
}
2021-12-25 10:04:45 +08:00
void PageView::copySpecialProperties(Widget* widget)
2019-11-23 20:27:39 +08:00
{
PageView* pageView = dynamic_cast<PageView*>(widget);
if (pageView)
{
ListView::copySpecialProperties(widget);
2021-12-25 10:04:45 +08:00
_eventCallback = pageView->_eventCallback;
_ccEventCallback = pageView->_ccEventCallback;
_currentPageIndex = pageView->_currentPageIndex;
_previousPageIndex = pageView->_previousPageIndex;
_childFocusCancelOffset = pageView->_childFocusCancelOffset;
_autoScrollStopEpsilon = pageView->_autoScrollStopEpsilon;
2019-11-23 20:27:39 +08:00
_indicatorPositionAsAnchorPoint = pageView->_indicatorPositionAsAnchorPoint;
2021-12-25 10:04:45 +08:00
_isTouchBegin = pageView->_isTouchBegin;
2019-11-23 20:27:39 +08:00
}
}
void PageView::setIndicatorEnabled(bool enabled)
{
2021-12-25 10:04:45 +08:00
if (enabled == (_indicator != nullptr))
2019-11-23 20:27:39 +08:00
{
return;
}
2021-12-25 10:04:45 +08:00
if (!enabled)
2019-11-23 20:27:39 +08:00
{
removeProtectedChild(_indicator);
_indicator = nullptr;
}
else
{
_indicator = PageViewIndicator::create();
_indicator->setDirection(getDirection());
addProtectedChild(_indicator, 10000);
setIndicatorSelectedIndexColor(Color3B(100, 100, 255));
refreshIndicatorPosition();
}
}
void PageView::setIndicatorPositionAsAnchorPoint(const Vec2& positionAsAnchorPoint)
{
_indicatorPositionAsAnchorPoint = positionAsAnchorPoint;
refreshIndicatorPosition();
}
const Vec2& PageView::getIndicatorPositionAsAnchorPoint() const
{
return _indicatorPositionAsAnchorPoint;
}
void PageView::setIndicatorPosition(const Vec2& position)
{
2021-12-25 10:04:45 +08:00
if (_indicator != nullptr)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
const Vec2& contentSize = getContentSize();
2019-11-23 20:27:39 +08:00
_indicatorPositionAsAnchorPoint.x = position.x / contentSize.width;
_indicatorPositionAsAnchorPoint.y = position.y / contentSize.height;
_indicator->setPosition(position);
}
}
const Vec2& PageView::getIndicatorPosition() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(_indicator != nullptr, "");
2019-11-23 20:27:39 +08:00
return _indicator->getPosition();
}
void PageView::setIndicatorSpaceBetweenIndexNodes(float spaceBetweenIndexNodes)
{
2021-12-25 10:04:45 +08:00
if (_indicator != nullptr)
2019-11-23 20:27:39 +08:00
{
_indicator->setSpaceBetweenIndexNodes(spaceBetweenIndexNodes);
}
}
float PageView::getIndicatorSpaceBetweenIndexNodes() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(_indicator != nullptr, "");
2019-11-23 20:27:39 +08:00
return _indicator->getSpaceBetweenIndexNodes();
}
void PageView::setIndicatorSelectedIndexColor(const Color3B& color)
{
2021-12-25 10:04:45 +08:00
if (_indicator != nullptr)
2019-11-23 20:27:39 +08:00
{
_indicator->setSelectedIndexColor(color);
}
}
const Color3B& PageView::getIndicatorSelectedIndexColor() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(_indicator != nullptr, "");
2019-11-23 20:27:39 +08:00
return _indicator->getSelectedIndexColor();
}
void PageView::setIndicatorIndexNodesColor(const Color3B& color)
{
2021-12-25 10:04:45 +08:00
if (_indicator != nullptr)
2019-11-23 20:27:39 +08:00
{
_indicator->setIndexNodesColor(color);
}
}
const Color3B& PageView::getIndicatorIndexNodesColor() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(_indicator != nullptr, "");
2019-11-23 20:27:39 +08:00
return _indicator->getIndexNodesColor();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void PageView::setIndicatorSelectedIndexOpacity(uint8_t opacity)
{
2021-12-25 10:04:45 +08:00
if (_indicator != nullptr)
2019-11-23 20:27:39 +08:00
{
_indicator->setSelectedIndexOpacity(opacity);
}
}
uint8_t PageView::getIndicatorSelectedIndexOpacity() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(_indicator != nullptr, "");
2019-11-23 20:27:39 +08:00
return _indicator->getSelectedIndexOpacity();
}
void PageView::setIndicatorIndexNodesOpacity(uint8_t opacity)
{
2021-12-25 10:04:45 +08:00
if (_indicator != nullptr)
2019-11-23 20:27:39 +08:00
{
_indicator->setIndexNodesOpacity(opacity);
}
}
uint8_t PageView::getIndicatorIndexNodesOpacity() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(_indicator != nullptr, "");
2019-11-23 20:27:39 +08:00
return _indicator->getIndexNodesOpacity();
}
void PageView::setIndicatorIndexNodesScale(float indexNodesScale)
{
2021-12-25 10:04:45 +08:00
if (_indicator != nullptr)
2019-11-23 20:27:39 +08:00
{
_indicator->setIndexNodesScale(indexNodesScale);
_indicator->indicate(_currentPageIndex);
}
}
float PageView::getIndicatorIndexNodesScale() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(_indicator != nullptr, "");
2019-11-23 20:27:39 +08:00
return _indicator->getIndexNodesScale();
}
void PageView::setIndicatorIndexNodesTexture(std::string_view texName, Widget::TextureResType texType)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (_indicator != nullptr)
2019-11-23 20:27:39 +08:00
{
_indicator->setIndexNodesTexture(texName, texType);
_indicator->indicate(_currentPageIndex);
}
}
2021-12-25 10:04:45 +08:00
void PageView::remedyLayoutParameter(Widget* item)
2019-11-23 20:27:39 +08:00
{
item->setContentSize(this->getContentSize());
ListView::remedyLayoutParameter(item);
}
2021-12-25 10:04:45 +08:00
} // namespace ui
2019-11-23 20:27:39 +08:00
NS_AX_END