axmol/cocos/ui/UIPageView.cpp

307 lines
7.8 KiB
C++
Raw Normal View History

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"
#include "ui/UIPageViewIndicator.h"
2014-03-11 17:13:54 +08:00
NS_CC_BEGIN
namespace ui {
IMPLEMENT_CLASS_GUI_INFO(PageView)
PageView::PageView():
_indicator(nullptr),
_currentPageIndex(-1),
_customScrollThreshold(0.0),
_usingCustomScrollThreshold(false),
2014-03-11 17:13:54 +08:00
_childFocusCancelOffset(5.0f),
_pageViewEventListener(nullptr),
2014-05-12 10:31:52 +08:00
_pageViewEventSelector(nullptr),
_eventCallback(nullptr)
2014-03-11 17:13:54 +08:00
{
}
PageView::~PageView()
{
_pageViewEventListener = nullptr;
_pageViewEventSelector = nullptr;
}
PageView* PageView::create()
{
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()
{
if (ListView::init())
2014-03-11 17:13:54 +08:00
{
_indicator = PageViewIndicator::create();
addProtectedChild(_indicator, 10000);
setDirection(ListView::Direction::HORIZONTAL);
setMagneticType(MagneticType::CENTER);
setScrollBarEnabled(false);
2014-03-11 17:13:54 +08:00
return true;
}
return false;
}
void PageView::addWidgetToPage(Widget *widget, ssize_t pageIdx, bool forceCreate)
{
insertCustomItem(widget, pageIdx);
2014-03-11 17:13:54 +08:00
}
void PageView::addPage(Layout* page)
{
pushBackCustomItem(page);
2014-03-11 17:13:54 +08:00
}
void PageView::insertPage(Layout* page, int idx)
{
insertCustomItem(page, idx);
2014-03-11 17:13:54 +08:00
}
void PageView::removePage(Layout* page)
{
removeItem(getIndex(page));
2014-03-11 17:13:54 +08:00
}
void PageView::removePageAtIndex(ssize_t index)
{
removeItem(index);
2014-03-11 17:13:54 +08:00
}
void PageView::removeAllPages()
{
removeAllItems();
2014-03-11 17:13:54 +08:00
}
void PageView::setCurPageIndex( ssize_t index )
{
setCurrentPageIndex(index);
}
void PageView::setCurrentPageIndex(ssize_t index)
{
jumpToItem(index, Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE);
}
2014-03-11 17:13:54 +08:00
void PageView::scrollToPage(ssize_t idx)
{
scrollToItem(idx, Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE);
2014-03-11 17:13:54 +08:00
}
void PageView::setCustomScrollThreshold(float threshold)
{
CCASSERT(threshold > 0, "Invalid threshold!");
_customScrollThreshold = threshold;
this->setUsingCustomScrollThreshold(true);
}
float PageView::getCustomScrollThreshold()const
{
return _customScrollThreshold;
}
void PageView::setUsingCustomScrollThreshold(bool flag)
{
2014-08-14 14:36:30 +08:00
_usingCustomScrollThreshold = flag;
}
bool PageView::isUsingCustomScrollThreshold()const
{
2014-08-14 14:36:30 +08:00
return _usingCustomScrollThreshold;
}
2014-03-11 17:13:54 +08:00
void PageView::moveInnerContainer(const Vec2& deltaMove, bool canStartBounceBack)
{
ListView::moveInnerContainer(deltaMove, canStartBounceBack);
_currentPageIndex = getIndex(getCenterItemInCurrentView());
_indicator->indicate(_currentPageIndex);
}
2015-09-21 18:40:30 +08:00
void PageView::onItemListChanged()
{
ListView::onItemListChanged();
ssize_t index = getIndex(getCenterItemInCurrentView());
_indicator->reset(_items.size(), index);
}
void PageView::onSizeChanged()
{
ListView::onSizeChanged();
_indicator->setPosition(Vec2(getContentSize() / 2) + Vec2(0, -50));
}
2014-06-06 16:48:49 +08:00
void PageView::handleReleaseLogic(Touch *touch)
2014-03-11 17:13:54 +08:00
{
// Use `ScrollView` method in order to avoid `startMagneticScroll()` by `ListView`.
ScrollView::handleReleaseLogic(touch);
if(_items.empty())
{
return;
}
Vec2 touchMoveVelocity = flattenVectorByDirection(calculateTouchMoveVelocity());
static const float THRESHOLD = 500;
CCLOG("handleReleaseLogic() touchMoveVelocity.length()=%.2f", touchMoveVelocity.length());
if(touchMoveVelocity.length() < THRESHOLD)
{
startMagneticScroll();
}
else
{
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)
{
startMagneticScroll();
}
else
{
if(touchMoveVelocity.x < 0 || touchMoveVelocity.y > 0)
{
++_currentPageIndex;
}
else
{
--_currentPageIndex;
}
_currentPageIndex = MIN(_currentPageIndex, _items.size());
_currentPageIndex = MAX(_currentPageIndex, 0);
scrollToItem(_currentPageIndex, Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE);
}
}
2014-03-11 17:13:54 +08:00
}
void PageView::pageTurningEvent()
{
this->retain();
2014-03-11 17:13:54 +08:00
if (_pageViewEventListener && _pageViewEventSelector)
{
(_pageViewEventListener->*_pageViewEventSelector)(this, PAGEVIEW_EVENT_TURNING);
}
if (_eventCallback)
{
2014-05-12 10:31:52 +08:00
_eventCallback(this,EventType::TURNING);
}
if (_ccEventCallback)
{
_ccEventCallback(this, static_cast<int>(EventType::TURNING));
}
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
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
{
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
{
CCASSERT(false, "This method is obsolete!");
// Temporary code to keep backward compatibility.
static Vector<Layout*> pages;
pages.clear();
for(Widget* widget : getItems())
{
pages.pushBack(dynamic_cast<Layout*>(widget));
}
return pages;
2014-03-11 17:13:54 +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;
}
// Temporary code to keep backward compatibility.
static Vector<Layout*> pages;
pages.clear();
for(Widget* widget : getItems())
{
pages.pushBack(dynamic_cast<Layout*>(widget));
}
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)
{
ListView::copySpecialProperties(widget);
2014-05-23 15:03:46 +08:00
_eventCallback = pageView->_eventCallback;
_ccEventCallback = pageView->_ccEventCallback;
2014-05-23 15:03:46 +08:00
_pageViewEventListener = pageView->_pageViewEventListener;
_pageViewEventSelector = pageView->_pageViewEventSelector;
_usingCustomScrollThreshold = pageView->_usingCustomScrollThreshold;
_customScrollThreshold = pageView->_customScrollThreshold;
2014-03-11 17:13:54 +08:00
}
}
}
NS_CC_END