axmol/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.cpp

573 lines
14 KiB
C++
Raw Normal View History

2013-09-13 22:20:20 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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 "UIPageView.h"
NS_CC_EXT_BEGIN
UIPageView::UIPageView():
2013-09-16 20:54:13 +08:00
_curPageIdx(0),
_pages(NULL),
_touchMoveDir(PAGEVIEW_TOUCHLEFT),
_touchStartLocation(0.0f),
_touchEndLocation(0.0f),
_touchMoveStartLocation(0.0f),
_movePagePoint(Point::ZERO),
_leftChild(NULL),
_rightChild(NULL),
_leftBoundary(0.0f),
_rightBoundary(0.0f),
_isAutoScrolling(false),
_autoScrollDistance(0.0f),
_autoScrollSpeed(0.0f),
_autoScrollDir(0),
_childFocusCancelOffset(5.0f),
_pageTurningListener(NULL),
_pageTurningSelector(NULL)
2013-09-13 22:20:20 +08:00
{
}
UIPageView::~UIPageView()
{
2013-09-16 20:54:13 +08:00
_pages->release();
2013-09-13 22:20:20 +08:00
}
UIPageView* UIPageView::create()
{
UIPageView* widget = new UIPageView();
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return NULL;
}
bool UIPageView::init()
{
if (Layout::init())
{
2013-09-16 20:54:13 +08:00
_pages = CCArray::create();
_pages->retain();
2013-09-13 22:20:20 +08:00
setClippingEnabled(true);
setUpdateEnabled(true);
return true;
}
return false;
}
void UIPageView::addWidgetToPage(UIWidget *widget, int pageIdx, bool forceCreate)
{
if (!widget)
{
return;
}
2013-09-16 20:54:13 +08:00
int pageCount = _pages->count();
2013-09-13 22:20:20 +08:00
if (pageIdx < 0 || pageIdx >= pageCount)
{
if (forceCreate)
{
if (pageIdx > pageCount)
{
CCLOG("pageIdx is %d, it will be added as page id [%d]",pageIdx,pageCount);
}
Layout* newPage = createPage();
newPage->addChild(widget);
addPage(newPage);
}
}
else
{
2013-09-16 20:54:13 +08:00
Layout * page = dynamic_cast<Layout*>(_pages->getObjectAtIndex(pageIdx));
2013-09-13 22:20:20 +08:00
if (page)
{
page->addChild(widget);
}
}
}
Layout* UIPageView::createPage()
{
Layout* newPage = Layout::create();
newPage->setSize(getSize());
return newPage;
}
void UIPageView::addPage(Layout* page)
{
if (!page)
{
return;
}
if (page->getWidgetType() != WidgetTypeContainer)
{
return;
}
2013-09-16 20:54:13 +08:00
if (_pages->containsObject(page))
2013-09-13 22:20:20 +08:00
{
return;
}
Size pSize = page->getSize();
Size pvSize = getSize();
if (!pSize.equals(pvSize))
{
CCLOG("page size does not match pageview size, it will be force sized!");
page->setSize(pvSize);
}
2013-09-16 20:54:13 +08:00
page->setPosition(Point(getPositionXByIndex(_pages->count()), 0));
_pages->addObject(page);
2013-09-13 22:20:20 +08:00
addChild(page);
updateBoundaryPages();
}
void UIPageView::insertPage(Layout* page, int idx)
{
if (idx < 0)
{
return;
}
if (!page)
{
return;
}
if (page->getWidgetType() != WidgetTypeContainer)
{
return;
}
2013-09-16 20:54:13 +08:00
if (_pages->containsObject(page))
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 20:54:13 +08:00
int pageCount = _pages->count();
2013-09-13 22:20:20 +08:00
if (idx >= pageCount)
{
addPage(page);
}
else
{
2013-09-16 20:54:13 +08:00
_pages->insertObject(page, idx);
2013-09-13 22:20:20 +08:00
page->setPosition(Point(getPositionXByIndex(idx), 0));
addChild(page);
Size pSize = page->getSize();
Size pvSize = getSize();
if (!pSize.equals(pvSize))
{
CCLOG("page size does not match pageview size, it will be force sized!");
page->setSize(pvSize);
}
2013-09-16 20:54:13 +08:00
ccArray* arrayPages = _pages->data;
2013-09-13 22:20:20 +08:00
int length = arrayPages->num;
for (int i=(idx+1); i<length; i++) {
UIWidget* behindPage = dynamic_cast<UIWidget*>(arrayPages->arr[i]);
Point formerPos = behindPage->getPosition();
behindPage->setPosition(Point(formerPos.x+getSize().width, 0));
}
updateBoundaryPages();
}
}
void UIPageView::removePage(Layout* page)
{
if (!page)
{
return;
}
removeChild(page);
updateChildrenPosition();
updateBoundaryPages();
}
void UIPageView::removePageAtIndex(int index)
{
2013-09-16 20:54:13 +08:00
if (index < 0 || index >= (int)(_pages->count()))
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 20:54:13 +08:00
Layout* page = dynamic_cast<Layout*>(_pages->getObjectAtIndex(index));
2013-09-13 22:20:20 +08:00
if (page)
{
removePage(page);
}
}
void UIPageView::updateBoundaryPages()
{
2013-09-16 20:54:13 +08:00
if (_pages->count() <= 0)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_leftChild = NULL;
_rightChild = NULL;
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
_leftChild = dynamic_cast<UIWidget*>(_pages->getObjectAtIndex(0));
_rightChild = dynamic_cast<UIWidget*>(_pages->getLastObject());
2013-09-13 22:20:20 +08:00
}
float UIPageView::getPositionXByIndex(int idx)
{
2013-09-16 20:54:13 +08:00
return (getSize().width*(idx-_curPageIdx));
2013-09-13 22:20:20 +08:00
}
bool UIPageView::addChild(UIWidget* widget)
{
return Layout::addChild(widget);
}
bool UIPageView::removeChild(UIWidget* widget)
{
2013-09-16 20:54:13 +08:00
if (_pages->containsObject(widget))
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_pages->removeObject(widget);
2013-09-13 22:20:20 +08:00
return Layout::removeChild(widget);
}
return false;
}
void UIPageView::onSizeChanged()
{
Layout::onSizeChanged();
2013-09-16 20:54:13 +08:00
_rightBoundary = getSize().width;
2013-09-13 22:20:20 +08:00
updateChildrenSize();
updateChildrenPosition();
}
void UIPageView::updateChildrenSize()
{
2013-09-16 20:54:13 +08:00
if (!_pages)
2013-09-13 22:20:20 +08:00
{
return;
}
Size selfSize = getSize();
2013-09-16 20:54:13 +08:00
for (unsigned int i=0; i<_pages->count(); i++)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
Layout* page = dynamic_cast<Layout*>(_pages->getObjectAtIndex(i));
2013-09-13 22:20:20 +08:00
page->setSize(selfSize);
}
}
void UIPageView::updateChildrenPosition()
{
2013-09-16 20:54:13 +08:00
if (!_pages)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 20:54:13 +08:00
int pageCount = _pages->data->num;
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;
2013-09-16 20:54:13 +08:00
ccArray* arrayPages = _pages->data;
2013-09-13 22:20:20 +08:00
for (int i=0; i<pageCount; i++)
{
Layout* page = dynamic_cast<Layout*>(arrayPages->arr[i]);
2013-09-16 20:54:13 +08:00
page->setPosition(Point((i-_curPageIdx)*pageWidth, 0));
2013-09-13 22:20:20 +08:00
}
}
void UIPageView::removeAllChildren()
{
2013-09-16 20:54:13 +08:00
_pages->removeAllObjects();
2013-09-13 22:20:20 +08:00
Layout::removeAllChildren();
}
void UIPageView::scrollToPage(int idx)
{
2013-09-16 20:54:13 +08:00
if (idx < 0 || idx >= (int)(_pages->count()))
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 20:54:13 +08:00
_curPageIdx = idx;
UIWidget* curPage = dynamic_cast<UIWidget*>(_pages->getObjectAtIndex(idx));
_autoScrollDistance = -(curPage->getPosition().x);
_autoScrollSpeed = fabs(_autoScrollDistance)/0.2f;
_autoScrollDir = _autoScrollDistance > 0 ? 1 : 0;
_isAutoScrolling = true;
2013-09-13 22:20:20 +08:00
}
void UIPageView::update(float dt)
{
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
pageTurningEvent();
}
else
{
2013-09-16 20:54:13 +08:00
_autoScrollDistance += step;
2013-09-13 22:20:20 +08:00
}
scrollPages(-step);
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
pageTurningEvent();
}
else
{
2013-09-16 20:54:13 +08:00
_autoScrollDistance -= step;
2013-09-13 22:20:20 +08:00
}
scrollPages(step);
break;
}
default:
break;
}
}
}
bool UIPageView::onTouchBegan(const Point &touchPoint)
{
bool pass = Layout::onTouchBegan(touchPoint);
handlePressLogic(touchPoint);
return pass;
}
void UIPageView::onTouchMoved(const Point &touchPoint)
{
2013-09-16 15:32:52 +08:00
_touchMovePos.x = touchPoint.x;
_touchMovePos.y = touchPoint.y;
2013-09-13 22:20:20 +08:00
handleMoveLogic(touchPoint);
2013-09-16 15:32:52 +08:00
if (_widgetParent)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_widgetParent->checkChildInfo(1,this,touchPoint);
2013-09-13 22:20:20 +08:00
}
moveEvent();
if (!hitTest(touchPoint))
{
setFocused(false);
onTouchEnded(touchPoint);
}
}
void UIPageView::onTouchEnded(const Point &touchPoint)
{
Layout::onTouchEnded(touchPoint);
handleReleaseLogic(touchPoint);
}
void UIPageView::movePages(float offset)
{
2013-09-16 20:54:13 +08:00
ccArray* arrayPages = _pages->data;
2013-09-13 22:20:20 +08:00
int length = arrayPages->num;
for (int i = 0; i < length; i++)
{
UIWidget* child = (UIWidget*)(arrayPages->arr[i]);
2013-09-16 20:54:13 +08:00
_movePagePoint.x = child->getPosition().x + offset;
_movePagePoint.y = child->getPosition().y;
child->setPosition(_movePagePoint);
2013-09-13 22:20:20 +08:00
}
}
bool UIPageView::scrollPages(float touchOffset)
{
2013-09-16 20:54:13 +08:00
if (_pages->count() <= 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;
}
void UIPageView::onTouchCancelled(const Point &touchPoint)
{
Layout::onTouchCancelled(touchPoint);
}
void UIPageView::handlePressLogic(const Point &touchPoint)
{
2013-09-16 15:32:52 +08:00
Point nsp = _renderer->convertToNodeSpace(touchPoint);
2013-09-16 20:54:13 +08:00
_touchMoveStartLocation = nsp.x;
_touchStartLocation = nsp.x;
2013-09-13 22:20:20 +08:00
}
void UIPageView::handleMoveLogic(const Point &touchPoint)
{
2013-09-16 15:32:52 +08:00
Point nsp = _renderer->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);
}
void UIPageView::handleReleaseLogic(const Point &touchPoint)
{
2013-09-16 20:54:13 +08:00
UIWidget* curPage = dynamic_cast<UIWidget*>(_pages->getObjectAtIndex(_curPageIdx));
2013-09-13 22:20:20 +08:00
if (curPage)
{
Point curPagePos = curPage->getPosition();
2013-09-16 20:54:13 +08:00
int pageCount = _pages->count();
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
}
}
}
void UIPageView::checkChildInfo(int handleState,UIWidget* sender, const Point &touchPoint)
{
interceptTouchEvent(handleState, sender, touchPoint);
}
void UIPageView::interceptTouchEvent(int handleState, UIWidget *sender, const Point &touchPoint)
{
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:
break;
}
}
void UIPageView::pageTurningEvent()
{
2013-09-16 20:54:13 +08:00
if (_pageTurningListener && _pageTurningSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
(_pageTurningListener->*_pageTurningSelector)(this);
2013-09-13 22:20:20 +08:00
}
}
void UIPageView::addPageTurningEvent(Object *target, SEL_PageViewPageTurningEvent selector)
{
2013-09-16 20:54:13 +08:00
_pageTurningListener = target;
_pageTurningSelector = selector;
2013-09-13 22:20:20 +08:00
}
int UIPageView::getCurPageIndex() const
{
2013-09-16 20:54:13 +08:00
return _curPageIdx;
2013-09-13 22:20:20 +08:00
}
2013-09-17 17:59:20 +08:00
const char* UIPageView::getDescription() const
{
return "PageView";
}
2013-09-13 22:20:20 +08:00
NS_CC_EXT_END