axmol/cocos/ui/UIPageViewIndicator.cpp

295 lines
8.2 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2015 Neo Kim (neo.kim@neofect.com)
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "ui/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";
2019-11-23 20:27:39 +08:00
static const char* CIRCLE_IMAGE_KEY = "/__circleImage";
NS_CC_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
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
PageViewIndicator* PageViewIndicator::create()
{
2021-12-08 00:11:53 +08:00
PageViewIndicator* node = new PageViewIndicator();
if (node->init())
2019-11-23 20:27:39 +08:00
{
node->autorelease();
return node;
}
CC_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)
{}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PageViewIndicator::~PageViewIndicator() {}
2019-11-23 20:27:39 +08:00
bool PageViewIndicator::init()
{
_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)
2019-11-23 20:27:39 +08:00
{
increaseNumberOfPages();
}
2021-12-25 10:04:45 +08:00
while (_indexNodes.size() > numberOfTotalPages)
2019-11-23 20:27:39 +08:00
{
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;
2019-11-23 20:27:39 +08:00
_currentOverlappingIndexNode = _indexNodes.at(index);
2021-12-25 10:04:45 +08:00
if (oldOverlappingNode != _currentOverlappingIndexNode)
{
if (oldOverlappingNode)
2019-11-23 20:27:39 +08:00
oldOverlappingNode->setVisible(true);
_currentOverlappingIndexNode->setVisible(false);
_currentIndexNode->setPosition(_currentOverlappingIndexNode->getPosition());
}
}
void PageViewIndicator::rearrange()
{
2021-12-25 10:04:45 +08:00
if (_indexNodes.empty())
2019-11-23 20:27:39 +08:00
{
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);
2019-11-23 20:27:39 +08:00
ssize_t numberOfItems = _indexNodes.size();
2021-12-25 10:04:45 +08:00
float totalSizeValue = sizeValue * numberOfItems + _spaceBetweenIndexNodes * (numberOfItems - 1);
2019-11-23 20:27:39 +08:00
float posValue = -(totalSizeValue / 2) + (sizeValue / 2);
2021-12-25 10:04:45 +08:00
for (auto& indexNode : _indexNodes)
{
2019-11-23 20:27:39 +08:00
Vec2 position;
2021-12-25 10:04:45 +08:00
if (horizontal)
2019-11-23 20:27:39 +08:00
{
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)
2019-11-23 20:27:39 +08:00
{
return;
}
_spaceBetweenIndexNodes = spaceBetweenIndexNodes;
rearrange();
}
void PageViewIndicator::setIndexNodesColor(const Color3B& indexNodesColor)
{
_indexNodesColor = indexNodesColor;
2021-12-25 10:04:45 +08:00
for (auto& indexNode : _indexNodes)
{
2019-11-23 20:27:39 +08:00
indexNode->setColor(indexNodesColor);
}
}
2021-12-25 10:04:45 +08:00
void PageViewIndicator::setIndexNodesOpacity(uint8_t opacity)
{
2019-11-23 20:27:39 +08:00
_indexNodesOpacity = opacity;
2021-12-25 10:04:45 +08:00
for (auto& indexNode : _indexNodes)
2019-11-23 20:27:39 +08:00
indexNode->setOpacity(opacity);
}
void PageViewIndicator::setIndexNodesScale(float indexNodesScale)
{
2021-12-25 10:04:45 +08:00
if (_indexNodesScale == indexNodesScale)
2019-11-23 20:27:39 +08:00
{
return;
}
_indexNodesScale = indexNodesScale;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_currentIndexNode->setScale(indexNodesScale);
2021-12-25 10:04:45 +08:00
for (auto& indexNode : _indexNodes)
{
2019-11-23 20:27:39 +08:00
indexNode->setScale(_indexNodesScale);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
rearrange();
}
void PageViewIndicator::setIndexNodesTexture(const std::string& texName, Widget::TextureResType texType)
{
2021-12-25 10:04:45 +08:00
_useDefaultTexture = false;
2019-11-23 20:27:39 +08:00
_indexNodesTextureFile = texName;
2021-12-25 10:04:45 +08:00
_indexNodesTexType = texType;
2019-11-23 20:27:39 +08:00
switch (texType)
{
2021-12-25 10:04:45 +08:00
case Widget::TextureResType::LOCAL:
_currentIndexNode->setTexture(texName);
for (auto& indexNode : _indexNodes)
{
indexNode->setTexture(texName);
}
break;
case Widget::TextureResType::PLIST:
_currentIndexNode->setSpriteFrame(texName);
for (auto& indexNode : _indexNodes)
{
indexNode->setSpriteFrame(texName);
}
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
rearrange();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void PageViewIndicator::increaseNumberOfPages()
{
2021-12-25 10:04:45 +08:00
if (_currentOverlappingIndexNode)
{
2019-11-23 20:27:39 +08:00
_currentOverlappingIndexNode->setVisible(true);
_currentOverlappingIndexNode = nullptr;
}
Sprite* indexNode;
2021-12-25 10:04:45 +08:00
if (_useDefaultTexture)
2019-11-23 20:27:39 +08:00
{
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;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +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)
{
2019-11-23 20:27:39 +08:00
_currentOverlappingIndexNode->setVisible(true);
_currentOverlappingIndexNode = nullptr;
}
2021-12-25 10:04:45 +08:00
if (_indexNodes.empty())
2019-11-23 20:27:39 +08:00
{
return;
}
removeProtectedChild(*_indexNodes.begin());
_indexNodes.erase(_indexNodes.begin());
}
void PageViewIndicator::clear()
{
2021-12-25 10:04:45 +08:00
if (_currentOverlappingIndexNode)
{
2019-11-23 20:27:39 +08:00
_currentOverlappingIndexNode->setVisible(true);
_currentOverlappingIndexNode = nullptr;
}
2021-12-25 10:04:45 +08:00
for (auto& indexNode : _indexNodes)
2019-11-23 20:27:39 +08:00
{
removeProtectedChild(indexNode);
}
_indexNodes.clear();
_currentIndexNode->setVisible(false);
}
2021-12-25 10:04:45 +08:00
} // namespace ui
2019-11-23 20:27:39 +08:00
NS_CC_END