axmol/core/ui/UIPageViewIndicator.cpp

295 lines
8.2 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2015 Neo Kim (neo.kim@neofect.com)
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
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/UIPageViewIndicator.h"
#include "2d/CCSprite.h"
#include "base/ccUtils.h"
2021-12-25 10:04:45 +08:00
static const char* CIRCLE_IMAGE =
"iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAQAAADZc7J/AAAA8ElEQVRIx62VyRGCQBBF+6gWRCEmYDIQkhiBCgHhSclC8YqWzOV5oVzKAYZp3r1/"
"9fpbxAIBMTsKrjx5cqVgR0wgLhCRUWOjJiPqD56xoaGPhpRZV/iSEy6crHmw5oIrF9b/lVeMofrJgjlnxlIy/"
"wik+JB+mme8BExbBhm+5CJC2LE2LtSEQoyGWDioBA5CoRIohJtK4CYDxzNEM4GAugR1E9VjVC+"
"SZpXvhCJCrjomESLvc17pDGX7bWmlh6UtpjPVCWy9zaJ0TD7qfm3pwERMz2trRVZk3K3BD/L34AY+dEDCniMVBkPFkT2J/b2/"
"AIV+dRpFLOYoAAAAAElFTkSuQmCC";
2016-06-06 22:32:50 +08:00
static const char* CIRCLE_IMAGE_KEY = "/__circleImage";
NS_AX_BEGIN
2021-12-25 10:04:45 +08:00
namespace
{
static const float SPACE_BETWEEN_INDEX_NODES_DEFAULT = 23;
static const uint8_t INDEX_NODES_OPACITY_DEFAULT = 0.3 * 255;
} // namespace
2021-12-25 10:04:45 +08:00
namespace ui
{
PageViewIndicator* PageViewIndicator::create()
{
2021-12-08 00:11:53 +08:00
PageViewIndicator* node = new PageViewIndicator();
if (node->init())
{
node->autorelease();
return node;
}
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(node);
return nullptr;
}
PageViewIndicator::PageViewIndicator()
2021-12-25 10:04:45 +08:00
: _direction(PageView::Direction::HORIZONTAL)
, _currentIndexNode(nullptr)
, _currentOverlappingIndexNode(nullptr)
, _spaceBetweenIndexNodes(SPACE_BETWEEN_INDEX_NODES_DEFAULT)
, _indexNodesScale(1.0f)
, _indexNodesColor(Color3B::WHITE)
, _indexNodesOpacity(INDEX_NODES_OPACITY_DEFAULT)
, _useDefaultTexture(true)
, _indexNodesTextureFile("")
, _indexNodesTexType(Widget::TextureResType::LOCAL)
{}
2021-12-25 10:04:45 +08:00
PageViewIndicator::~PageViewIndicator() {}
bool PageViewIndicator::init()
{
2016-06-06 22:32:50 +08:00
_currentIndexNode = utils::createSpriteFromBase64Cached(CIRCLE_IMAGE, CIRCLE_IMAGE_KEY);
_currentIndexNode->setVisible(false);
addProtectedChild(_currentIndexNode, 1);
return true;
}
void PageViewIndicator::setDirection(PageView::Direction direction)
{
_direction = direction;
rearrange();
}
void PageViewIndicator::reset(ssize_t numberOfTotalPages)
{
2021-12-25 10:04:45 +08:00
while (_indexNodes.size() < numberOfTotalPages)
{
increaseNumberOfPages();
}
2021-12-25 10:04:45 +08:00
while (_indexNodes.size() > numberOfTotalPages)
{
decreaseNumberOfPages();
}
rearrange();
_currentIndexNode->setVisible(!_indexNodes.empty());
}
void PageViewIndicator::indicate(ssize_t index)
{
if (index < 0 || index >= _indexNodes.size())
{
return;
}
2021-12-25 10:04:45 +08:00
Sprite* oldOverlappingNode = _currentOverlappingIndexNode;
_currentOverlappingIndexNode = _indexNodes.at(index);
2021-12-25 10:04:45 +08:00
if (oldOverlappingNode != _currentOverlappingIndexNode)
{
if (oldOverlappingNode)
oldOverlappingNode->setVisible(true);
_currentOverlappingIndexNode->setVisible(false);
_currentIndexNode->setPosition(_currentOverlappingIndexNode->getPosition());
}
}
void PageViewIndicator::rearrange()
{
2021-12-25 10:04:45 +08:00
if (_indexNodes.empty())
{
return;
}
bool horizontal = (_direction == PageView::Direction::HORIZONTAL);
// Calculate total size
2021-10-23 23:27:14 +08:00
Vec2 indexNodeSize = _indexNodes.at(0)->getContentSize();
2021-12-25 10:04:45 +08:00
float sizeValue = (horizontal ? indexNodeSize.width : indexNodeSize.height);
ssize_t numberOfItems = _indexNodes.size();
2021-12-25 10:04:45 +08:00
float totalSizeValue = sizeValue * numberOfItems + _spaceBetweenIndexNodes * (numberOfItems - 1);
float posValue = -(totalSizeValue / 2) + (sizeValue / 2);
for (auto&& indexNode : _indexNodes)
2021-12-25 10:04:45 +08:00
{
Vec2 position;
2021-12-25 10:04:45 +08:00
if (horizontal)
{
position = Vec2(posValue, indexNodeSize.height / 2.0f);
}
else
{
position = Vec2(indexNodeSize.width / 2.0f, -posValue);
}
indexNode->setPosition(position);
posValue += sizeValue + _spaceBetweenIndexNodes;
}
}
void PageViewIndicator::setSpaceBetweenIndexNodes(float spaceBetweenIndexNodes)
{
2021-12-25 10:04:45 +08:00
if (_spaceBetweenIndexNodes == spaceBetweenIndexNodes)
{
return;
}
_spaceBetweenIndexNodes = spaceBetweenIndexNodes;
rearrange();
}
void PageViewIndicator::setIndexNodesColor(const Color3B& indexNodesColor)
{
_indexNodesColor = indexNodesColor;
2021-12-25 10:04:45 +08:00
for (auto&& indexNode : _indexNodes)
2021-12-25 10:04:45 +08:00
{
indexNode->setColor(indexNodesColor);
}
}
2021-12-25 10:04:45 +08:00
void PageViewIndicator::setIndexNodesOpacity(uint8_t opacity)
{
_indexNodesOpacity = opacity;
for (auto&& indexNode : _indexNodes)
indexNode->setOpacity(opacity);
}
void PageViewIndicator::setIndexNodesScale(float indexNodesScale)
{
2021-12-25 10:04:45 +08:00
if (_indexNodesScale == indexNodesScale)
{
return;
}
_indexNodesScale = indexNodesScale;
2021-12-25 10:04:45 +08:00
_currentIndexNode->setScale(indexNodesScale);
for (auto&& indexNode : _indexNodes)
2021-12-25 10:04:45 +08:00
{
indexNode->setScale(_indexNodesScale);
}
2021-12-25 10:04:45 +08:00
rearrange();
}
2021-12-26 23:26:34 +08:00
void PageViewIndicator::setIndexNodesTexture(std::string_view texName, Widget::TextureResType texType)
{
2021-12-25 10:04:45 +08:00
_useDefaultTexture = false;
_indexNodesTextureFile = texName;
2021-12-25 10:04:45 +08:00
_indexNodesTexType = texType;
switch (texType)
{
2021-12-25 10:04:45 +08:00
case Widget::TextureResType::LOCAL:
_currentIndexNode->setTexture(texName);
for (auto&& indexNode : _indexNodes)
2021-12-25 10:04:45 +08:00
{
indexNode->setTexture(texName);
}
break;
case Widget::TextureResType::PLIST:
_currentIndexNode->setSpriteFrame(texName);
for (auto&& indexNode : _indexNodes)
2021-12-25 10:04:45 +08:00
{
indexNode->setSpriteFrame(texName);
}
break;
default:
break;
}
2021-12-25 10:04:45 +08:00
rearrange();
}
2021-12-25 10:04:45 +08:00
void PageViewIndicator::increaseNumberOfPages()
{
2021-12-25 10:04:45 +08:00
if (_currentOverlappingIndexNode)
{
_currentOverlappingIndexNode->setVisible(true);
_currentOverlappingIndexNode = nullptr;
}
Sprite* indexNode;
2021-12-25 10:04:45 +08:00
if (_useDefaultTexture)
{
indexNode = utils::createSpriteFromBase64(CIRCLE_IMAGE);
}
else
{
switch (_indexNodesTexType)
{
2021-12-25 10:04:45 +08:00
case Widget::TextureResType::LOCAL:
indexNode = Sprite::create(_indexNodesTextureFile);
break;
case Widget::TextureResType::PLIST:
indexNode = Sprite::createWithSpriteFrameName(_indexNodesTextureFile);
break;
default:
break;
}
}
2021-12-25 10:04:45 +08:00
indexNode->setColor(_indexNodesColor);
indexNode->setScale(_indexNodesScale);
indexNode->setOpacity(_indexNodesOpacity);
addProtectedChild(indexNode);
_indexNodes.pushBack(indexNode);
}
void PageViewIndicator::decreaseNumberOfPages()
{
2021-12-25 10:04:45 +08:00
if (_currentOverlappingIndexNode)
{
_currentOverlappingIndexNode->setVisible(true);
_currentOverlappingIndexNode = nullptr;
}
2021-12-25 10:04:45 +08:00
if (_indexNodes.empty())
{
return;
}
removeProtectedChild(*_indexNodes.begin());
_indexNodes.erase(_indexNodes.begin());
}
void PageViewIndicator::clear()
{
2021-12-25 10:04:45 +08:00
if (_currentOverlappingIndexNode)
{
_currentOverlappingIndexNode->setVisible(true);
_currentOverlappingIndexNode = nullptr;
}
for (auto&& indexNode : _indexNodes)
{
removeProtectedChild(indexNode);
}
_indexNodes.clear();
_currentIndexNode->setVisible(false);
}
2021-12-25 10:04:45 +08:00
} // namespace ui
NS_AX_END