2015-09-20 20:29:11 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2015 Neo Kim (neo.kim@neofect.com)
|
|
|
|
|
|
|
|
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 "UIPageViewIndicator.h"
|
2015-09-21 15:50:18 +08:00
|
|
|
#include "2d/CCSprite.h"
|
|
|
|
#include "base/ccUtils.h"
|
|
|
|
|
2015-09-22 10:06:18 +08:00
|
|
|
static const char* CIRCLE_IMAGE = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAQAAADZc7J/AAAA8ElEQVRIx62VyRGCQBBF+6gWRCEmYDIQkhiBCgHhSclC8YqWzOV5oVzKAYZp3r1/9fpbxAIBMTsKrjx5cqVgR0wgLhCRUWOjJiPqD56xoaGPhpRZV/iSEy6crHmw5oIrF9b/lVeMofrJgjlnxlIy/wik+JB+mme8BExbBhm+5CJC2LE2LtSEQoyGWDioBA5CoRIohJtK4CYDxzNEM4GAugR1E9VjVC+SZpXvhCJCrjomESLvc17pDGX7bWmlh6UtpjPVCWy9zaJ0TD7qfm3pwERMz2trRVZk3K3BD/L34AY+dEDCniMVBkPFkT2J/b2/AIV+dRpFLOYoAAAAAElFTkSuQmCC";
|
2015-09-20 20:29:11 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-09-21 15:50:18 +08:00
|
|
|
static const float SPACE_BETWEEN_INDEX_NODES_DEFAULT = 23;
|
|
|
|
|
2015-09-20 20:29:11 +08:00
|
|
|
namespace ui {
|
|
|
|
|
2015-09-21 15:50:18 +08:00
|
|
|
PageViewIndicator* PageViewIndicator::create()
|
|
|
|
{
|
|
|
|
PageViewIndicator* node = new (std::nothrow) PageViewIndicator();
|
|
|
|
if (node && node->init())
|
|
|
|
{
|
|
|
|
node->autorelease();
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(node);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PageViewIndicator::PageViewIndicator()
|
2015-09-21 21:11:42 +08:00
|
|
|
: _direction(PageView::Direction::HORIZONTAL)
|
|
|
|
, _currentIndexNode(nullptr)
|
2015-09-21 15:50:18 +08:00
|
|
|
, _spaceBetweenIndexNodes(SPACE_BETWEEN_INDEX_NODES_DEFAULT)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PageViewIndicator::~PageViewIndicator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PageViewIndicator::init()
|
|
|
|
{
|
|
|
|
_currentIndexNode = (Node*) utils::createSpriteFromBase64(CIRCLE_IMAGE);
|
|
|
|
_currentIndexNode->setVisible(false);
|
2015-09-22 10:06:18 +08:00
|
|
|
addProtectedChild(_currentIndexNode, 1);
|
2015-09-21 15:50:18 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-21 21:11:42 +08:00
|
|
|
void PageViewIndicator::setDirection(PageView::Direction direction)
|
|
|
|
{
|
|
|
|
_direction = direction;
|
|
|
|
rearrange();
|
|
|
|
}
|
|
|
|
|
2015-11-15 21:48:42 +08:00
|
|
|
void PageViewIndicator::reset(ssize_t numberOfTotalPages)
|
2015-09-21 15:50:18 +08:00
|
|
|
{
|
2015-09-21 18:42:48 +08:00
|
|
|
while(_indexNodes.size() < numberOfTotalPages)
|
|
|
|
{
|
|
|
|
increaseNumberOfPages();
|
|
|
|
}
|
|
|
|
while(_indexNodes.size() > numberOfTotalPages)
|
|
|
|
{
|
|
|
|
decreaseNumberOfPages();
|
|
|
|
}
|
|
|
|
rearrange();
|
|
|
|
_currentIndexNode->setVisible(!_indexNodes.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageViewIndicator::indicate(ssize_t index)
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= _indexNodes.size())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-09-21 15:50:18 +08:00
|
|
|
_currentIndexNode->setPosition(_indexNodes.at(index)->getPosition());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageViewIndicator::rearrange()
|
2015-09-20 20:29:11 +08:00
|
|
|
{
|
2015-09-21 15:50:18 +08:00
|
|
|
if(_indexNodes.empty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-21 21:11:42 +08:00
|
|
|
bool horizontal = (_direction == PageView::Direction::HORIZONTAL);
|
2015-09-21 15:50:18 +08:00
|
|
|
|
2015-09-21 18:42:48 +08:00
|
|
|
// Calculate total size
|
2015-09-21 15:50:18 +08:00
|
|
|
Size indexNodeSize = _indexNodes.at(0)->getContentSize();
|
|
|
|
float sizeValue = (horizontal ? indexNodeSize.width : indexNodeSize.height);
|
2015-09-20 20:29:11 +08:00
|
|
|
|
2015-09-21 18:42:48 +08:00
|
|
|
ssize_t numberOfItems = _indexNodes.size();
|
|
|
|
float totalSizeValue = sizeValue * numberOfItems + _spaceBetweenIndexNodes * (numberOfItems - 1);
|
|
|
|
|
|
|
|
float posValue = -(totalSizeValue / 2) + (sizeValue / 2);
|
2015-09-21 15:50:18 +08:00
|
|
|
for(auto indexNode : _indexNodes) {
|
|
|
|
Vec2 position;
|
|
|
|
if(horizontal)
|
|
|
|
{
|
|
|
|
position = Vec2(posValue, indexNodeSize.height / 2.0f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-21 21:20:36 +08:00
|
|
|
position = Vec2(indexNodeSize.width / 2.0f, -posValue);
|
2015-09-21 15:50:18 +08:00
|
|
|
}
|
|
|
|
indexNode->setPosition(position);
|
2015-09-21 18:42:48 +08:00
|
|
|
posValue += sizeValue + _spaceBetweenIndexNodes;
|
2015-09-21 15:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageViewIndicator::setSpaceBetweenIndexNodes(float spaceBetweenIndexNodes)
|
|
|
|
{
|
|
|
|
if(_spaceBetweenIndexNodes == spaceBetweenIndexNodes)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_spaceBetweenIndexNodes = spaceBetweenIndexNodes;
|
|
|
|
rearrange();
|
2015-09-20 20:29:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-21 15:50:18 +08:00
|
|
|
void PageViewIndicator::increaseNumberOfPages()
|
2015-09-20 20:29:11 +08:00
|
|
|
{
|
2015-09-21 15:50:18 +08:00
|
|
|
Sprite* indexNode = utils::createSpriteFromBase64(CIRCLE_IMAGE);
|
2015-09-22 10:06:18 +08:00
|
|
|
// indexNode->setOpacity(255 * 0.3f);
|
2015-09-21 15:50:18 +08:00
|
|
|
addProtectedChild(indexNode);
|
|
|
|
_indexNodes.pushBack(indexNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageViewIndicator::decreaseNumberOfPages()
|
|
|
|
{
|
|
|
|
if(_indexNodes.empty())
|
|
|
|
{
|
2015-09-21 18:42:48 +08:00
|
|
|
return;
|
2015-09-21 15:50:18 +08:00
|
|
|
}
|
2015-09-21 18:42:48 +08:00
|
|
|
removeProtectedChild(*_indexNodes.begin());
|
|
|
|
_indexNodes.erase(_indexNodes.begin());
|
2015-09-21 15:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageViewIndicator::clear()
|
|
|
|
{
|
|
|
|
for(auto indexNode : _indexNodes)
|
|
|
|
{
|
|
|
|
removeProtectedChild(indexNode);
|
|
|
|
}
|
|
|
|
_indexNodes.clear();
|
|
|
|
_currentIndexNode->setVisible(false);
|
2015-09-20 20:29:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|