2013-09-13 22:20:20 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +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.
|
|
|
|
****************************************************************************/
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "gui/UIPageView.h"
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-02-24 18:56:45 +08:00
|
|
|
namespace ui {
|
2014-03-04 16:51:35 +08:00
|
|
|
|
|
|
|
IMPLEMENT_CLASS_GUI_INFO(PageView)
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
PageView::PageView():
|
2013-09-16 20:54:13 +08:00
|
|
|
_curPageIdx(0),
|
|
|
|
_touchMoveDir(PAGEVIEW_TOUCHLEFT),
|
|
|
|
_touchStartLocation(0.0f),
|
|
|
|
_touchMoveStartLocation(0.0f),
|
2013-12-23 15:02:52 +08:00
|
|
|
_movePagePoint(Point::ZERO),
|
2013-11-14 11:37:46 +08:00
|
|
|
_leftChild(nullptr),
|
|
|
|
_rightChild(nullptr),
|
2013-09-16 20:54:13 +08:00
|
|
|
_leftBoundary(0.0f),
|
|
|
|
_rightBoundary(0.0f),
|
|
|
|
_isAutoScrolling(false),
|
|
|
|
_autoScrollDistance(0.0f),
|
|
|
|
_autoScrollSpeed(0.0f),
|
|
|
|
_autoScrollDir(0),
|
|
|
|
_childFocusCancelOffset(5.0f),
|
2013-11-14 11:37:46 +08:00
|
|
|
_pageViewEventListener(nullptr),
|
|
|
|
_pageViewEventSelector(nullptr)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
PageView::~PageView()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
_pages.clear();
|
2013-11-14 11:37:46 +08:00
|
|
|
_pageViewEventListener = nullptr;
|
|
|
|
_pageViewEventSelector = nullptr;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
PageView* PageView::create()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
PageView* widget = new PageView();
|
2013-09-13 22:20:20 +08:00
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
2013-11-14 11:37:46 +08:00
|
|
|
return nullptr;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2014-01-09 09:58:27 +08:00
|
|
|
|
|
|
|
void PageView::onEnter()
|
|
|
|
{
|
|
|
|
Layout::onEnter();
|
2014-01-09 19:00:47 +08:00
|
|
|
scheduleUpdate();
|
2014-01-09 09:58:27 +08:00
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
bool PageView::init()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
if (Layout::init())
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
setClippingEnabled(true);
|
2013-11-06 16:04:06 +08:00
|
|
|
setTouchEnabled(true);
|
2013-09-13 22:20:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
void PageView::addWidgetToPage(Widget *widget, ssize_t pageIdx, bool forceCreate)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
if (!widget)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
if (pageIdx < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-28 14:34:52 +08:00
|
|
|
ssize_t pageCount = _pages.size();
|
2013-09-13 22:20:20 +08:00
|
|
|
if (pageIdx < 0 || pageIdx >= pageCount)
|
|
|
|
{
|
|
|
|
if (forceCreate)
|
|
|
|
{
|
|
|
|
if (pageIdx > pageCount)
|
|
|
|
{
|
2014-01-05 11:59:32 +08:00
|
|
|
CCLOG("pageIdx is %d, it will be added as page id [%d]",static_cast<int>(pageIdx),static_cast<int>(pageCount));
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
Layout* newPage = createPage();
|
2013-09-13 22:20:20 +08:00
|
|
|
newPage->addChild(widget);
|
|
|
|
addPage(newPage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Layout * page = _pages.at(pageIdx);
|
|
|
|
page->addChild(widget);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Layout* PageView::createPage()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Layout* newPage = Layout::create();
|
2013-09-13 22:20:20 +08:00
|
|
|
newPage->setSize(getSize());
|
|
|
|
return newPage;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::addPage(Layout* page)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
if (!page)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (page->getWidgetType() != WidgetTypeContainer)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
if (_pages.contains(page))
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
Size pSize = page->getSize();
|
|
|
|
Size pvSize = getSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
if (!pSize.equals(pvSize))
|
|
|
|
{
|
|
|
|
CCLOG("page size does not match pageview size, it will be force sized!");
|
|
|
|
page->setSize(pvSize);
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
page->setPosition(Point(getPositionXByIndex(_pages.size()), 0));
|
|
|
|
_pages.pushBack(page);
|
2013-09-13 22:20:20 +08:00
|
|
|
addChild(page);
|
|
|
|
updateBoundaryPages();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::insertPage(Layout* page, int idx)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
if (idx < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!page)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (page->getWidgetType() != WidgetTypeContainer)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
if (_pages.contains(page))
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
ssize_t pageCount = _pages.size();
|
2013-09-13 22:20:20 +08:00
|
|
|
if (idx >= pageCount)
|
|
|
|
{
|
|
|
|
addPage(page);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
_pages.insert(idx, page);
|
|
|
|
page->setPosition(Point(getPositionXByIndex(idx), 0));
|
2013-09-13 22:20:20 +08:00
|
|
|
addChild(page);
|
2013-12-23 15:02:52 +08:00
|
|
|
Size pSize = page->getSize();
|
|
|
|
Size pvSize = getSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
if (!pSize.equals(pvSize))
|
|
|
|
{
|
|
|
|
CCLOG("page size does not match pageview size, it will be force sized!");
|
|
|
|
page->setSize(pvSize);
|
|
|
|
}
|
2013-12-28 14:34:52 +08:00
|
|
|
ssize_t length = _pages.size();
|
|
|
|
for (ssize_t i=(idx+1); i<length; i++){
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* behindPage = _pages.at(i);
|
|
|
|
Point formerPos = behindPage->getPosition();
|
|
|
|
behindPage->setPosition(Point(formerPos.x+getSize().width, 0));
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
updateBoundaryPages();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::removePage(Layout* page)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
if (!page)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
removeChild(page);
|
|
|
|
updateChildrenPosition();
|
|
|
|
updateBoundaryPages();
|
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
void PageView::removePageAtIndex(ssize_t index)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-28 14:34:52 +08:00
|
|
|
if (index < 0 || index >= _pages.size())
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
Layout* page = _pages.at(index);
|
|
|
|
removePage(page);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-11-13 20:01:57 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::removeAllPages()
|
2013-11-13 20:01:57 +08:00
|
|
|
{
|
|
|
|
removeAllChildren();
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::updateBoundaryPages()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
if (_pages.size() <= 0)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-14 11:37:46 +08:00
|
|
|
_leftChild = nullptr;
|
|
|
|
_rightChild = nullptr;
|
2013-11-06 16:04:06 +08:00
|
|
|
return;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
_leftChild = _pages.at(0);
|
|
|
|
_rightChild = _pages.at(_pages.size()-1);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
float PageView::getPositionXByIndex(ssize_t idx)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return (getSize().width*(idx-_curPageIdx));
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
|
|
|
|
void PageView::addChild(Node *child)
|
|
|
|
{
|
|
|
|
Layout::addChild(child);
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::addChild(Node * child, int zOrder)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Layout::addChild(child, zOrder);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::addChild(Node *child, int zOrder, int tag)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Layout::addChild(child, zOrder, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::removeChild(Node *child, bool cleanup)
|
|
|
|
{
|
|
|
|
if (_pages.contains(static_cast<Layout*>(child)))
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
_pages.eraseObject(static_cast<Layout*>(child));
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
Layout::removeChild(child, cleanup);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::onSizeChanged()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Layout::onSizeChanged();
|
2013-09-16 20:54:13 +08:00
|
|
|
_rightBoundary = getSize().width;
|
2013-09-13 22:20:20 +08:00
|
|
|
updateChildrenSize();
|
|
|
|
updateChildrenPosition();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::updateChildrenSize()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Size selfSize = getSize();
|
2013-12-26 16:17:52 +08:00
|
|
|
for (auto& page : _pages)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
page->setSize(selfSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::updateChildrenPosition()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-28 14:34:52 +08:00
|
|
|
ssize_t pageCount = _pages.size();
|
2013-09-13 22:20:20 +08:00
|
|
|
if (pageCount <= 0)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_curPageIdx = 0;
|
2013-09-13 22:20:20 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_curPageIdx >= pageCount)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_curPageIdx = pageCount-1;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
float pageWidth = getSize().width;
|
|
|
|
for (int i=0; i<pageCount; i++)
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Layout* page = _pages.at(i);
|
|
|
|
page->setPosition(Point((i-_curPageIdx)*pageWidth, 0));
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::removeAllChildren()
|
2013-12-27 16:01:03 +08:00
|
|
|
{
|
|
|
|
removeAllChildrenWithCleanup(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::removeAllChildrenWithCleanup(bool cleanup)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
_pages.clear();
|
2013-12-27 16:01:03 +08:00
|
|
|
Layout::removeAllChildrenWithCleanup(cleanup);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
void PageView::scrollToPage(ssize_t idx)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-28 14:34:52 +08:00
|
|
|
if (idx < 0 || idx >= _pages.size())
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
_curPageIdx = idx;
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* curPage = _pages.at(idx);
|
2013-09-16 20:54:13 +08:00
|
|
|
_autoScrollDistance = -(curPage->getPosition().x);
|
|
|
|
_autoScrollSpeed = fabs(_autoScrollDistance)/0.2f;
|
|
|
|
_autoScrollDir = _autoScrollDistance > 0 ? 1 : 0;
|
|
|
|
_isAutoScrolling = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::update(float dt)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_isAutoScrolling)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_autoScrollDir)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
float step = _autoScrollSpeed*dt;
|
|
|
|
if (_autoScrollDistance + step >= 0.0f)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
step = -_autoScrollDistance;
|
|
|
|
_autoScrollDistance = 0.0f;
|
|
|
|
_isAutoScrolling = false;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_autoScrollDistance += step;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
scrollPages(-step);
|
2013-11-06 16:04:06 +08:00
|
|
|
if (!_isAutoScrolling)
|
|
|
|
{
|
|
|
|
pageTurningEvent();
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
float step = _autoScrollSpeed*dt;
|
|
|
|
if (_autoScrollDistance - step <= 0.0f)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
step = _autoScrollDistance;
|
|
|
|
_autoScrollDistance = 0.0f;
|
|
|
|
_isAutoScrolling = false;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_autoScrollDistance -= step;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
scrollPages(step);
|
2013-11-06 16:04:06 +08:00
|
|
|
if (!_isAutoScrolling)
|
|
|
|
{
|
|
|
|
pageTurningEvent();
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
bool PageView::onTouchBegan(Touch *touch, Event *unusedEvent)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
bool pass = Layout::onTouchBegan(touch, unusedEvent);
|
|
|
|
if (_hitted)
|
|
|
|
{
|
|
|
|
handlePressLogic(touch->getLocation());
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::onTouchMoved(Touch *touch, Event *unusedEvent)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
_touchMovePos = touch->getLocation();
|
|
|
|
handleMoveLogic(_touchMovePos);
|
|
|
|
Widget* widgetParent = getWidgetParent();
|
|
|
|
if (widgetParent)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
widgetParent->checkChildInfo(1,this,_touchMovePos);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
moveEvent();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::onTouchEnded(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
Layout::onTouchEnded(touch, unusedEvent);
|
|
|
|
handleReleaseLogic(_touchEndPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::onTouchCancelled(Touch *touch, Event *unusedEvent)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Layout::onTouchCancelled(touch, unusedEvent);
|
|
|
|
handleReleaseLogic(touch->getLocation());
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::movePages(float offset)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-26 16:17:52 +08:00
|
|
|
for (auto& page : _pages)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-26 16:17:52 +08:00
|
|
|
_movePagePoint.x = page->getPosition().x + offset;
|
|
|
|
_movePagePoint.y = page->getPosition().y;
|
|
|
|
page->setPosition(_movePagePoint);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
bool PageView::scrollPages(float touchOffset)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
if (_pages.size() <= 0)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_leftChild || !_rightChild)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float realOffset = touchOffset;
|
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_touchMoveDir)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case PAGEVIEW_TOUCHLEFT: // left
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_rightChild->getRightInParent() + touchOffset <= _rightBoundary)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
realOffset = _rightBoundary - _rightChild->getRightInParent();
|
2013-09-13 22:20:20 +08:00
|
|
|
movePages(realOffset);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PAGEVIEW_TOUCHRIGHT: // right
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_leftChild->getLeftInParent() + touchOffset >= _leftBoundary)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
realOffset = _leftBoundary - _leftChild->getLeftInParent();
|
2013-09-13 22:20:20 +08:00
|
|
|
movePages(realOffset);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
movePages(realOffset);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::handlePressLogic(const Point &touchPoint)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Point nsp = convertToNodeSpace(touchPoint);
|
2013-09-16 20:54:13 +08:00
|
|
|
_touchMoveStartLocation = nsp.x;
|
|
|
|
_touchStartLocation = nsp.x;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::handleMoveLogic(const Point &touchPoint)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Point nsp = convertToNodeSpace(touchPoint);
|
2013-09-13 22:20:20 +08:00
|
|
|
float offset = 0.0;
|
|
|
|
float moveX = nsp.x;
|
2013-09-16 20:54:13 +08:00
|
|
|
offset = moveX - _touchMoveStartLocation;
|
|
|
|
_touchMoveStartLocation = moveX;
|
2013-09-13 22:20:20 +08:00
|
|
|
if (offset < 0)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_touchMoveDir = PAGEVIEW_TOUCHLEFT;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else if (offset > 0)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_touchMoveDir = PAGEVIEW_TOUCHRIGHT;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
scrollPages(offset);
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::handleReleaseLogic(const Point &touchPoint)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
if (_pages.size() <= 0)
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* curPage = _pages.at(_curPageIdx);
|
2013-09-13 22:20:20 +08:00
|
|
|
if (curPage)
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Point curPagePos = curPage->getPosition();
|
2013-12-28 14:34:52 +08:00
|
|
|
ssize_t pageCount = _pages.size();
|
2013-09-13 22:20:20 +08:00
|
|
|
float curPageLocation = curPagePos.x;
|
|
|
|
float pageWidth = getSize().width;
|
|
|
|
float boundary = pageWidth/2.0f;
|
|
|
|
if (curPageLocation <= -boundary)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_curPageIdx >= pageCount-1)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
scrollPages(-curPageLocation);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
scrollToPage(_curPageIdx+1);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (curPageLocation >= boundary)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_curPageIdx <= 0)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
scrollPages(-curPageLocation);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
scrollToPage(_curPageIdx-1);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
scrollToPage(_curPageIdx);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::checkChildInfo(int handleState,Widget* sender, const Point &touchPoint)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
interceptTouchEvent(handleState, sender, touchPoint);
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::interceptTouchEvent(int handleState, Widget *sender, const Point &touchPoint)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
switch (handleState)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
handlePressLogic(touchPoint);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
float offset = 0;
|
|
|
|
offset = fabs(sender->getTouchStartPos().x - touchPoint.x);
|
2013-09-16 20:54:13 +08:00
|
|
|
if (offset > _childFocusCancelOffset)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
sender->setFocused(false);
|
|
|
|
handleMoveLogic(touchPoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
handleReleaseLogic(touchPoint);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2014-01-03 19:14:17 +08:00
|
|
|
handleReleaseLogic(touchPoint);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::pageTurningEvent()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-08 18:43:06 +08:00
|
|
|
if (_pageViewEventListener && _pageViewEventSelector)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-08 18:43:06 +08:00
|
|
|
(_pageViewEventListener->*_pageViewEventSelector)(this, PAGEVIEW_EVENT_TURNING);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void PageView::addEventListenerPageView(Ref *target, SEL_PageViewEvent selector)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-08 18:43:06 +08:00
|
|
|
_pageViewEventListener = target;
|
|
|
|
_pageViewEventSelector = selector;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
ssize_t PageView::getCurPageIndex() const
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _curPageIdx;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Vector<Layout*>& PageView::getPages()
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
|
|
|
return _pages;
|
|
|
|
}
|
2013-11-13 20:01:57 +08:00
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
Layout* PageView::getPage(ssize_t index)
|
2013-11-13 20:01:57 +08:00
|
|
|
{
|
2013-12-28 14:34:52 +08:00
|
|
|
if (index < 0 || index >= _pages.size())
|
2013-11-13 20:01:57 +08:00
|
|
|
{
|
2013-11-14 11:37:46 +08:00
|
|
|
return nullptr;
|
2013-11-13 20:01:57 +08:00
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
return _pages.at(index);
|
2013-11-13 20:01:57 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
std::string PageView::getDescription() const
|
2013-09-17 17:59:20 +08:00
|
|
|
{
|
|
|
|
return "PageView";
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* PageView::createCloneInstance()
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
return PageView::create();
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::copyClonedWidgetChildren(Widget* model)
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-12-27 17:22:51 +08:00
|
|
|
auto& modelPages = static_cast<PageView*>(model)->getPages();
|
2013-12-26 16:17:52 +08:00
|
|
|
for (auto& page : modelPages)
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
addPage(dynamic_cast<Layout*>(page->clone()));
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void PageView::copySpecialProperties(Widget *widget)
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
PageView* pageView = dynamic_cast<PageView*>(widget);
|
2013-11-06 16:04:06 +08:00
|
|
|
if (pageView)
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Layout::copySpecialProperties(widget);
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
}
|
|
|
|
|
2014-02-24 18:56:45 +08:00
|
|
|
NS_CC_END
|