2014-03-11 17:13:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
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/UIPageView.h"
|
2015-09-21 15:50:18 +08:00
|
|
|
#include "ui/UIPageViewIndicator.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
IMPLEMENT_CLASS_GUI_INFO(PageView)
|
|
|
|
|
|
|
|
PageView::PageView():
|
2015-09-21 15:50:18 +08:00
|
|
|
_indicator(nullptr),
|
2015-09-21 21:21:28 +08:00
|
|
|
_indicatorPositionAsAnchorPoint(Vec2(0.5f, 0.1f)),
|
2015-09-21 20:46:10 +08:00
|
|
|
_currentPageIndex(-1),
|
2014-03-11 17:13:54 +08:00
|
|
|
_childFocusCancelOffset(5.0f),
|
|
|
|
_pageViewEventListener(nullptr),
|
2014-05-12 10:31:52 +08:00
|
|
|
_pageViewEventSelector(nullptr),
|
2014-10-30 22:25:46 +08:00
|
|
|
_eventCallback(nullptr)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PageView::~PageView()
|
|
|
|
{
|
|
|
|
_pageViewEventListener = nullptr;
|
|
|
|
_pageViewEventSelector = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PageView* PageView::create()
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PageView* widget = new (std::nothrow) PageView();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PageView::init()
|
|
|
|
{
|
2015-09-16 17:13:58 +08:00
|
|
|
if (ListView::init())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-09-21 21:12:25 +08:00
|
|
|
setDirection(Direction::HORIZONTAL);
|
2015-09-16 17:13:58 +08:00
|
|
|
setMagneticType(MagneticType::CENTER);
|
2015-11-10 20:03:51 +08:00
|
|
|
setScrollBarEnabled(false);
|
2014-03-11 17:13:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-21 21:12:25 +08:00
|
|
|
void PageView::setDirection(PageView::Direction direction)
|
|
|
|
{
|
|
|
|
ListView::setDirection(direction);
|
|
|
|
if(direction == Direction::HORIZONTAL)
|
|
|
|
{
|
|
|
|
_indicatorPositionAsAnchorPoint = Vec2(0.5f, 0.1f);
|
|
|
|
}
|
|
|
|
else if(direction == Direction::VERTICAL)
|
|
|
|
{
|
|
|
|
_indicatorPositionAsAnchorPoint = Vec2(0.1f, 0.5f);
|
|
|
|
}
|
2015-10-28 15:34:30 +08:00
|
|
|
|
|
|
|
if(_indicator != nullptr)
|
|
|
|
{
|
|
|
|
_indicator->setDirection(direction);
|
|
|
|
refreshIndicatorPosition();
|
|
|
|
}
|
2015-09-21 21:12:25 +08:00
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void PageView::addWidgetToPage(Widget *widget, ssize_t pageIdx, bool forceCreate)
|
|
|
|
{
|
2015-11-10 20:03:51 +08:00
|
|
|
insertCustomItem(widget, pageIdx);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-11-10 20:30:43 +08:00
|
|
|
void PageView::addPage(Widget* page)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-11-10 20:03:51 +08:00
|
|
|
pushBackCustomItem(page);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-11-10 20:30:43 +08:00
|
|
|
void PageView::insertPage(Widget* page, int idx)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-11-10 20:03:51 +08:00
|
|
|
insertCustomItem(page, idx);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-11-10 20:30:43 +08:00
|
|
|
void PageView::removePage(Widget* page)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-11-10 20:03:51 +08:00
|
|
|
removeItem(getIndex(page));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::removePageAtIndex(ssize_t index)
|
|
|
|
{
|
2015-11-10 20:03:51 +08:00
|
|
|
removeItem(index);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::removeAllPages()
|
|
|
|
{
|
2015-11-10 20:03:51 +08:00
|
|
|
removeAllItems();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 17:02:02 +08:00
|
|
|
void PageView::setCurPageIndex( ssize_t index )
|
|
|
|
{
|
2015-09-21 20:46:10 +08:00
|
|
|
setCurrentPageIndex(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::setCurrentPageIndex(ssize_t index)
|
|
|
|
{
|
|
|
|
jumpToItem(index, Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE);
|
2015-07-15 17:02:02 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
void PageView::scrollToPage(ssize_t idx)
|
|
|
|
{
|
2015-10-28 15:54:39 +08:00
|
|
|
scrollToItem(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::scrollToItem(ssize_t itemIndex)
|
|
|
|
{
|
|
|
|
ListView::scrollToItem(itemIndex, Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-08-14 11:14:24 +08:00
|
|
|
void PageView::setCustomScrollThreshold(float threshold)
|
|
|
|
{
|
2015-11-10 20:11:11 +08:00
|
|
|
CCLOG("PageView::setCustomScrollThreshold() has no effect!");
|
2014-08-14 11:14:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PageView::getCustomScrollThreshold()const
|
|
|
|
{
|
2015-11-10 20:11:11 +08:00
|
|
|
return 0;
|
2014-08-14 11:14:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::setUsingCustomScrollThreshold(bool flag)
|
|
|
|
{
|
2015-11-10 20:11:11 +08:00
|
|
|
CCLOG("PageView::setUsingCustomScrollThreshold() has no effect!");
|
2014-08-14 11:14:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PageView::isUsingCustomScrollThreshold()const
|
|
|
|
{
|
2015-11-10 20:11:11 +08:00
|
|
|
return false;
|
2014-08-14 11:14:24 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2015-09-21 20:46:10 +08:00
|
|
|
void PageView::moveInnerContainer(const Vec2& deltaMove, bool canStartBounceBack)
|
|
|
|
{
|
|
|
|
ListView::moveInnerContainer(deltaMove, canStartBounceBack);
|
|
|
|
_currentPageIndex = getIndex(getCenterItemInCurrentView());
|
2015-10-28 15:34:30 +08:00
|
|
|
if(_indicator != nullptr)
|
|
|
|
{
|
|
|
|
_indicator->indicate(_currentPageIndex);
|
|
|
|
}
|
2015-09-21 20:46:10 +08:00
|
|
|
}
|
|
|
|
|
2015-09-21 18:40:30 +08:00
|
|
|
void PageView::onItemListChanged()
|
|
|
|
{
|
|
|
|
ListView::onItemListChanged();
|
|
|
|
ssize_t index = getIndex(getCenterItemInCurrentView());
|
2015-10-28 15:34:30 +08:00
|
|
|
if(_indicator != nullptr)
|
|
|
|
{
|
|
|
|
_indicator->reset(_items.size(), index);
|
|
|
|
}
|
2015-09-21 18:40:30 +08:00
|
|
|
}
|
|
|
|
|
2015-09-21 15:50:18 +08:00
|
|
|
void PageView::onSizeChanged()
|
|
|
|
{
|
|
|
|
ListView::onSizeChanged();
|
2015-09-21 21:03:05 +08:00
|
|
|
refreshIndicatorPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::refreshIndicatorPosition()
|
|
|
|
{
|
2015-10-28 15:34:30 +08:00
|
|
|
if(_indicator != nullptr)
|
|
|
|
{
|
|
|
|
const Size& contentSize = getContentSize();
|
|
|
|
float posX = contentSize.width * _indicatorPositionAsAnchorPoint.x;
|
|
|
|
float posY = contentSize.height * _indicatorPositionAsAnchorPoint.y;
|
|
|
|
_indicator->setPosition(Vec2(posX, posY));
|
|
|
|
}
|
2015-09-21 15:50:18 +08:00
|
|
|
}
|
|
|
|
|
2014-06-06 16:48:49 +08:00
|
|
|
void PageView::handleReleaseLogic(Touch *touch)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-09-21 20:46:10 +08:00
|
|
|
// Use `ScrollView` method in order to avoid `startMagneticScroll()` by `ListView`.
|
2015-09-20 14:55:24 +08:00
|
|
|
ScrollView::handleReleaseLogic(touch);
|
|
|
|
|
2015-09-21 20:46:10 +08:00
|
|
|
if(_items.empty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-20 14:55:24 +08:00
|
|
|
Vec2 touchMoveVelocity = flattenVectorByDirection(calculateTouchMoveVelocity());
|
2015-09-21 20:46:10 +08:00
|
|
|
|
2015-09-21 21:21:28 +08:00
|
|
|
static const float DEFAULT_THRESHOLD = 500;
|
|
|
|
if(touchMoveVelocity.length() < DEFAULT_THRESHOLD)
|
2015-09-20 14:55:24 +08:00
|
|
|
{
|
|
|
|
startMagneticScroll();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-21 20:46:10 +08:00
|
|
|
Widget* currentPage = getItem(_currentPageIndex);
|
|
|
|
Vec2 destination = calculateItemDestination(Vec2::ANCHOR_MIDDLE, currentPage, Vec2::ANCHOR_MIDDLE);
|
|
|
|
Vec2 deltaToCurrentpage;
|
|
|
|
deltaToCurrentpage = destination - getInnerContainerPosition();
|
|
|
|
deltaToCurrentpage = flattenVectorByDirection(deltaToCurrentpage);
|
|
|
|
|
|
|
|
if(touchMoveVelocity.x * deltaToCurrentpage.x > 0 || touchMoveVelocity.y * deltaToCurrentpage.y > 0)
|
2015-09-20 14:55:24 +08:00
|
|
|
{
|
2015-09-21 20:46:10 +08:00
|
|
|
startMagneticScroll();
|
2015-09-20 14:55:24 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-21 20:46:10 +08:00
|
|
|
if(touchMoveVelocity.x < 0 || touchMoveVelocity.y > 0)
|
|
|
|
{
|
|
|
|
++_currentPageIndex;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--_currentPageIndex;
|
|
|
|
}
|
|
|
|
_currentPageIndex = MIN(_currentPageIndex, _items.size());
|
|
|
|
_currentPageIndex = MAX(_currentPageIndex, 0);
|
2015-10-28 15:54:39 +08:00
|
|
|
scrollToItem(_currentPageIndex);
|
2015-09-20 14:55:24 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::pageTurningEvent()
|
|
|
|
{
|
2014-08-01 20:58:42 +08:00
|
|
|
this->retain();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_pageViewEventListener && _pageViewEventSelector)
|
|
|
|
{
|
|
|
|
(_pageViewEventListener->*_pageViewEventSelector)(this, PAGEVIEW_EVENT_TURNING);
|
|
|
|
}
|
2014-12-16 16:44:04 +08:00
|
|
|
if (_eventCallback)
|
|
|
|
{
|
2014-05-12 10:31:52 +08:00
|
|
|
_eventCallback(this,EventType::TURNING);
|
|
|
|
}
|
2014-11-27 16:49:19 +08:00
|
|
|
if (_ccEventCallback)
|
|
|
|
{
|
|
|
|
_ccEventCallback(this, static_cast<int>(EventType::TURNING));
|
|
|
|
}
|
2014-08-01 20:58:42 +08:00
|
|
|
this->release();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::addEventListenerPageView(Ref *target, SEL_PageViewEvent selector)
|
|
|
|
{
|
|
|
|
_pageViewEventListener = target;
|
|
|
|
_pageViewEventSelector = selector;
|
|
|
|
}
|
2014-05-12 10:31:52 +08:00
|
|
|
|
2014-05-14 16:14:28 +08:00
|
|
|
void PageView::addEventListener(const ccPageViewCallback& callback)
|
2014-05-12 10:31:52 +08:00
|
|
|
{
|
|
|
|
_eventCallback = callback;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
ssize_t PageView::getCurPageIndex() const
|
|
|
|
{
|
2015-11-10 20:03:51 +08:00
|
|
|
Widget* widget = ListView::getCenterItemInCurrentView();
|
|
|
|
return getIndex(widget);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-22 11:43:02 +08:00
|
|
|
Vector<Layout*>& PageView::getPages()
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-11-10 20:03:51 +08:00
|
|
|
CCASSERT(false, "This method is obsolete!");
|
2015-09-16 17:59:56 +08:00
|
|
|
|
|
|
|
// Temporary code to keep backward compatibility.
|
|
|
|
static Vector<Layout*> pages;
|
2015-11-10 20:03:51 +08:00
|
|
|
pages.clear();
|
|
|
|
for(Widget* widget : getItems())
|
|
|
|
{
|
|
|
|
pages.pushBack(dynamic_cast<Layout*>(widget));
|
|
|
|
}
|
|
|
|
return pages;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-09-16 17:59:56 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
Layout* PageView::getPage(ssize_t index)
|
|
|
|
{
|
2014-05-21 18:30:25 +08:00
|
|
|
if (index < 0 || index >= this->getPages().size())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-09-16 17:59:56 +08:00
|
|
|
|
|
|
|
// Temporary code to keep backward compatibility.
|
|
|
|
static Vector<Layout*> pages;
|
2015-11-10 20:03:51 +08:00
|
|
|
pages.clear();
|
|
|
|
for(Widget* widget : getItems())
|
|
|
|
{
|
|
|
|
pages.pushBack(dynamic_cast<Layout*>(widget));
|
|
|
|
}
|
2015-09-16 17:59:56 +08:00
|
|
|
return pages.at(index);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string PageView::getDescription() const
|
|
|
|
{
|
|
|
|
return "PageView";
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* PageView::createCloneInstance()
|
|
|
|
{
|
|
|
|
return PageView::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::copySpecialProperties(Widget *widget)
|
|
|
|
{
|
|
|
|
PageView* pageView = dynamic_cast<PageView*>(widget);
|
|
|
|
if (pageView)
|
|
|
|
{
|
2015-09-16 17:13:58 +08:00
|
|
|
ListView::copySpecialProperties(widget);
|
2014-05-23 15:03:46 +08:00
|
|
|
_eventCallback = pageView->_eventCallback;
|
2014-11-27 16:49:19 +08:00
|
|
|
_ccEventCallback = pageView->_ccEventCallback;
|
2014-05-23 15:03:46 +08:00
|
|
|
_pageViewEventListener = pageView->_pageViewEventListener;
|
|
|
|
_pageViewEventSelector = pageView->_pageViewEventSelector;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 21:12:25 +08:00
|
|
|
void PageView::setIndicatorEnabled(bool enabled)
|
|
|
|
{
|
2015-10-28 15:34:30 +08:00
|
|
|
if(enabled == (_indicator != nullptr))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!enabled)
|
|
|
|
{
|
|
|
|
removeProtectedChild(_indicator);
|
|
|
|
_indicator = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_indicator = PageViewIndicator::create();
|
|
|
|
addProtectedChild(_indicator, 10000);
|
|
|
|
setIndicatorSelectedIndexColor(Color3B(100, 100, 255));
|
|
|
|
refreshIndicatorPosition();
|
|
|
|
}
|
2015-09-21 21:12:25 +08:00
|
|
|
}
|
|
|
|
|
2015-09-21 21:03:05 +08:00
|
|
|
void PageView::setIndicatorPositionAsAnchorPoint(const Vec2& positionAsAnchorPoint)
|
|
|
|
{
|
|
|
|
_indicatorPositionAsAnchorPoint = positionAsAnchorPoint;
|
|
|
|
refreshIndicatorPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vec2& PageView::getIndicatorPositionAsAnchorPoint() const
|
|
|
|
{
|
|
|
|
return _indicatorPositionAsAnchorPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::setIndicatorPosition(const Vec2& position)
|
|
|
|
{
|
2015-10-28 15:34:30 +08:00
|
|
|
if(_indicator != nullptr)
|
|
|
|
{
|
|
|
|
const Size& contentSize = getContentSize();
|
|
|
|
_indicatorPositionAsAnchorPoint.x = position.x / contentSize.width;
|
|
|
|
_indicatorPositionAsAnchorPoint.y = position.y / contentSize.height;
|
|
|
|
_indicator->setPosition(position);
|
|
|
|
}
|
2015-09-21 21:03:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Vec2& PageView::getIndicatorPosition() const
|
|
|
|
{
|
2015-10-28 15:34:30 +08:00
|
|
|
CCASSERT(_indicator != nullptr, "");
|
2015-09-21 21:03:05 +08:00
|
|
|
return _indicator->getPosition();
|
|
|
|
}
|
|
|
|
|
2015-09-22 10:06:18 +08:00
|
|
|
void PageView::setIndicatorSpaceBetweenIndexNodes(float spaceBetweenIndexNodes)
|
|
|
|
{
|
2015-10-28 15:34:30 +08:00
|
|
|
if(_indicator != nullptr)
|
|
|
|
{
|
|
|
|
_indicator->setSpaceBetweenIndexNodes(spaceBetweenIndexNodes);
|
|
|
|
}
|
2015-09-22 10:06:18 +08:00
|
|
|
}
|
|
|
|
float PageView::getIndicatorSpaceBetweenIndexNodes() const
|
|
|
|
{
|
2015-10-28 15:34:30 +08:00
|
|
|
CCASSERT(_indicator != nullptr, "");
|
2015-09-22 10:06:18 +08:00
|
|
|
return _indicator->getSpaceBetweenIndexNodes();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::setIndicatorSelectedIndexColor(const Color3B& color)
|
|
|
|
{
|
2015-10-28 15:34:30 +08:00
|
|
|
if(_indicator != nullptr)
|
|
|
|
{
|
|
|
|
_indicator->setSelectedIndexColor(color);
|
|
|
|
}
|
2015-09-22 10:06:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Color3B& PageView::getIndicatorSelectedIndexColor() const
|
|
|
|
{
|
2015-10-28 15:34:30 +08:00
|
|
|
CCASSERT(_indicator != nullptr, "");
|
2015-09-22 10:06:18 +08:00
|
|
|
return _indicator->getSelectedIndexColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|