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"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
IMPLEMENT_CLASS_GUI_INFO(PageView)
|
|
|
|
|
|
|
|
PageView::PageView():
|
|
|
|
_curPageIdx(0),
|
2014-05-22 09:56:42 +08:00
|
|
|
_touchMoveDirection(TouchDirection::LEFT),
|
2014-03-11 17:13:54 +08:00
|
|
|
_touchStartLocation(0.0f),
|
|
|
|
_touchMoveStartLocation(0.0f),
|
2014-05-15 01:07:09 +08:00
|
|
|
_movePagePoint(Vec2::ZERO),
|
2014-05-22 09:56:42 +08:00
|
|
|
_leftBoundaryChild(nullptr),
|
|
|
|
_rightBoundaryChild(nullptr),
|
2014-03-11 17:13:54 +08:00
|
|
|
_leftBoundary(0.0f),
|
|
|
|
_rightBoundary(0.0f),
|
|
|
|
_isAutoScrolling(false),
|
|
|
|
_autoScrollDistance(0.0f),
|
|
|
|
_autoScrollSpeed(0.0f),
|
2014-05-22 14:09:01 +08:00
|
|
|
_autoScrollDirection(AutoScrollDirection::LEFT),
|
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 PageView();
|
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::onEnter()
|
|
|
|
{
|
|
|
|
Layout::onEnter();
|
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PageView::init()
|
|
|
|
{
|
|
|
|
if (Layout::init())
|
|
|
|
{
|
|
|
|
setClippingEnabled(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::addWidgetToPage(Widget *widget, ssize_t pageIdx, bool forceCreate)
|
|
|
|
{
|
2014-05-22 13:51:44 +08:00
|
|
|
if (!widget || pageIdx < 0)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-05-22 13:51:44 +08:00
|
|
|
|
2014-05-21 18:30:25 +08:00
|
|
|
ssize_t pageCount = this->getPageCount();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (pageIdx < 0 || pageIdx >= pageCount)
|
|
|
|
{
|
|
|
|
if (forceCreate)
|
|
|
|
{
|
|
|
|
if (pageIdx > pageCount)
|
|
|
|
{
|
|
|
|
CCLOG("pageIdx is %d, it will be added as page id [%d]",static_cast<int>(pageIdx),static_cast<int>(pageCount));
|
|
|
|
}
|
|
|
|
Layout* newPage = createPage();
|
|
|
|
newPage->addChild(widget);
|
|
|
|
addPage(newPage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-22 11:43:02 +08:00
|
|
|
Node * page = _pages.at(pageIdx);
|
2014-03-11 17:13:54 +08:00
|
|
|
page->addChild(widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Layout* PageView::createPage()
|
|
|
|
{
|
|
|
|
Layout* newPage = Layout::create();
|
|
|
|
newPage->setSize(getSize());
|
|
|
|
return newPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::addPage(Layout* page)
|
|
|
|
{
|
2014-05-22 13:51:44 +08:00
|
|
|
if (!page || _pages.contains(page))
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-05-22 13:51:44 +08:00
|
|
|
|
2014-05-21 16:07:33 +08:00
|
|
|
|
|
|
|
addProtectedChild(page);
|
2014-05-22 11:43:02 +08:00
|
|
|
_pages.pushBack(page);
|
2014-05-21 16:07:33 +08:00
|
|
|
|
2014-05-22 13:51:44 +08:00
|
|
|
_doLayoutDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::insertPage(Layout* page, int idx)
|
|
|
|
{
|
2014-05-22 13:51:44 +08:00
|
|
|
if (idx < 0 || !page || _pages.contains(page))
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-05-22 13:51:44 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-05-22 11:43:02 +08:00
|
|
|
ssize_t pageCount = this->getPageCount();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (idx >= pageCount)
|
|
|
|
{
|
|
|
|
addPage(page);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-22 11:43:02 +08:00
|
|
|
_pages.insert(idx, page);
|
|
|
|
addProtectedChild(page);
|
2014-05-21 16:40:42 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-05-22 13:51:44 +08:00
|
|
|
|
|
|
|
_doLayoutDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::removePage(Layout* page)
|
|
|
|
{
|
|
|
|
if (!page)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-05-22 11:43:02 +08:00
|
|
|
removeProtectedChild(page);
|
|
|
|
_pages.eraseObject(page);
|
|
|
|
|
2014-05-22 13:51:44 +08:00
|
|
|
_doLayoutDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::removePageAtIndex(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;
|
|
|
|
}
|
2014-05-22 11:43:02 +08:00
|
|
|
Layout* page = _pages.at(index);
|
2014-03-11 17:13:54 +08:00
|
|
|
removePage(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::removeAllPages()
|
|
|
|
{
|
2014-05-22 11:43:02 +08:00
|
|
|
for(auto& node : _pages)
|
2014-05-21 18:30:25 +08:00
|
|
|
{
|
2014-05-22 11:43:02 +08:00
|
|
|
removeProtectedChild(node);
|
2014-05-21 18:30:25 +08:00
|
|
|
}
|
2014-05-22 11:43:02 +08:00
|
|
|
_pages.clear();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::updateBoundaryPages()
|
|
|
|
{
|
2014-05-22 11:43:02 +08:00
|
|
|
if (_pages.size() <= 0)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-22 09:56:42 +08:00
|
|
|
_leftBoundaryChild = nullptr;
|
|
|
|
_rightBoundaryChild = nullptr;
|
2014-03-11 17:13:54 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-05-22 11:43:02 +08:00
|
|
|
_leftBoundaryChild = _pages.at(0);
|
|
|
|
_rightBoundaryChild = _pages.at(this->getPageCount()-1);
|
2014-05-21 18:30:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t PageView::getPageCount()
|
|
|
|
{
|
2014-05-22 11:43:02 +08:00
|
|
|
return _pages.size();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PageView::getPositionXByIndex(ssize_t idx)
|
|
|
|
{
|
2014-05-22 11:43:02 +08:00
|
|
|
return (getSize().width * (idx-_curPageIdx));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::onSizeChanged()
|
|
|
|
{
|
|
|
|
Layout::onSizeChanged();
|
|
|
|
_rightBoundary = getSize().width;
|
2014-05-22 11:43:02 +08:00
|
|
|
|
2014-05-22 13:51:44 +08:00
|
|
|
_doLayoutDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-22 11:43:02 +08:00
|
|
|
void PageView::updateAllPagesSize()
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
Size selfSize = getSize();
|
2014-05-22 11:43:02 +08:00
|
|
|
for (auto& page : _pages)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-22 11:43:02 +08:00
|
|
|
page->setSize(selfSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 11:43:02 +08:00
|
|
|
void PageView::updateAllPagesPosition()
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-21 18:30:25 +08:00
|
|
|
ssize_t pageCount = this->getPageCount();
|
2014-05-22 13:51:44 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (pageCount <= 0)
|
|
|
|
{
|
|
|
|
_curPageIdx = 0;
|
|
|
|
return;
|
|
|
|
}
|
2014-05-22 13:51:44 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_curPageIdx >= pageCount)
|
|
|
|
{
|
|
|
|
_curPageIdx = pageCount-1;
|
|
|
|
}
|
2014-05-22 13:51:44 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
float pageWidth = getSize().width;
|
|
|
|
for (int i=0; i<pageCount; i++)
|
|
|
|
{
|
2014-05-22 11:43:02 +08:00
|
|
|
Layout* page = _pages.at(i);
|
|
|
|
page->setPosition(Vec2((i-_curPageIdx) * pageWidth, 0));
|
2014-05-21 18:30:25 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PageView::scrollToPage(ssize_t idx)
|
|
|
|
{
|
2014-05-21 18:30:25 +08:00
|
|
|
if (idx < 0 || idx >= this->getPageCount())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_curPageIdx = idx;
|
2014-05-22 11:43:02 +08:00
|
|
|
Layout* curPage = _pages.at(idx);
|
2014-03-11 17:13:54 +08:00
|
|
|
_autoScrollDistance = -(curPage->getPosition().x);
|
|
|
|
_autoScrollSpeed = fabs(_autoScrollDistance)/0.2f;
|
2014-05-22 14:09:01 +08:00
|
|
|
_autoScrollDirection = _autoScrollDistance > 0 ? AutoScrollDirection::RIGHT : AutoScrollDirection::LEFT;
|
2014-03-11 17:13:54 +08:00
|
|
|
_isAutoScrolling = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::update(float dt)
|
|
|
|
{
|
|
|
|
if (_isAutoScrolling)
|
|
|
|
{
|
2014-05-22 14:09:01 +08:00
|
|
|
this->autoScroll(dt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::autoScroll(float dt)
|
|
|
|
{
|
|
|
|
switch (_autoScrollDirection)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-22 14:09:01 +08:00
|
|
|
case AutoScrollDirection::LEFT:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
float step = _autoScrollSpeed*dt;
|
|
|
|
if (_autoScrollDistance + step >= 0.0f)
|
|
|
|
{
|
|
|
|
step = -_autoScrollDistance;
|
|
|
|
_autoScrollDistance = 0.0f;
|
|
|
|
_isAutoScrolling = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_autoScrollDistance += step;
|
|
|
|
}
|
|
|
|
scrollPages(-step);
|
|
|
|
if (!_isAutoScrolling)
|
|
|
|
{
|
|
|
|
pageTurningEvent();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2014-05-22 14:09:01 +08:00
|
|
|
case AutoScrollDirection::RIGHT:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
float step = _autoScrollSpeed*dt;
|
|
|
|
if (_autoScrollDistance - step <= 0.0f)
|
|
|
|
{
|
|
|
|
step = _autoScrollDistance;
|
|
|
|
_autoScrollDistance = 0.0f;
|
|
|
|
_isAutoScrolling = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_autoScrollDistance -= step;
|
|
|
|
}
|
|
|
|
scrollPages(step);
|
2014-05-22 14:09:01 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (!_isAutoScrolling)
|
|
|
|
{
|
|
|
|
pageTurningEvent();
|
|
|
|
}
|
2014-05-22 14:09:01 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-05-22 14:09:01 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PageView::onTouchBegan(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
bool pass = Layout::onTouchBegan(touch, unusedEvent);
|
|
|
|
if (_hitted)
|
|
|
|
{
|
|
|
|
handlePressLogic(touch->getLocation());
|
|
|
|
}
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::onTouchMoved(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
_touchMovePos = touch->getLocation();
|
|
|
|
handleMoveLogic(_touchMovePos);
|
|
|
|
Widget* widgetParent = getWidgetParent();
|
|
|
|
if (widgetParent)
|
|
|
|
{
|
2014-05-22 15:23:13 +08:00
|
|
|
widgetParent->passTouchEventToParent(TouchEventType::MOVED,this,_touchMovePos);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
moveEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::onTouchEnded(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
Layout::onTouchEnded(touch, unusedEvent);
|
|
|
|
handleReleaseLogic(_touchEndPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::onTouchCancelled(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
Layout::onTouchCancelled(touch, unusedEvent);
|
|
|
|
handleReleaseLogic(touch->getLocation());
|
|
|
|
}
|
|
|
|
|
2014-05-22 13:51:44 +08:00
|
|
|
void PageView::doLayout()
|
|
|
|
{
|
|
|
|
if (!_doLayoutDirty)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAllPagesPosition();
|
|
|
|
updateAllPagesSize();
|
|
|
|
updateBoundaryPages();
|
|
|
|
|
|
|
|
|
|
|
|
_doLayoutDirty = false;
|
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void PageView::movePages(float offset)
|
|
|
|
{
|
2014-05-21 18:30:25 +08:00
|
|
|
for (auto& page : this->getPages())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
_movePagePoint.x = page->getPosition().x + offset;
|
|
|
|
_movePagePoint.y = page->getPosition().y;
|
|
|
|
page->setPosition(_movePagePoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PageView::scrollPages(float touchOffset)
|
|
|
|
{
|
2014-05-21 18:30:25 +08:00
|
|
|
if (this->getPageCount() <= 0)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-22 09:56:42 +08:00
|
|
|
if (!_leftBoundaryChild || !_rightBoundaryChild)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float realOffset = touchOffset;
|
|
|
|
|
2014-05-22 09:56:42 +08:00
|
|
|
switch (_touchMoveDirection)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-12 10:31:52 +08:00
|
|
|
case TouchDirection::LEFT: // left
|
2014-05-22 09:56:42 +08:00
|
|
|
if (_rightBoundaryChild->getRightInParent() + touchOffset <= _rightBoundary)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-22 09:56:42 +08:00
|
|
|
realOffset = _rightBoundary - _rightBoundaryChild->getRightInParent();
|
2014-03-11 17:13:54 +08:00
|
|
|
movePages(realOffset);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-05-12 10:31:52 +08:00
|
|
|
case TouchDirection::RIGHT: // right
|
2014-05-22 09:56:42 +08:00
|
|
|
if (_leftBoundaryChild->getLeftInParent() + touchOffset >= _leftBoundary)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-22 09:56:42 +08:00
|
|
|
realOffset = _leftBoundary - _leftBoundaryChild->getLeftInParent();
|
2014-03-11 17:13:54 +08:00
|
|
|
movePages(realOffset);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
movePages(realOffset);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void PageView::handlePressLogic(const Vec2 &touchPoint)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 nsp = convertToNodeSpace(touchPoint);
|
2014-03-11 17:13:54 +08:00
|
|
|
_touchMoveStartLocation = nsp.x;
|
|
|
|
_touchStartLocation = nsp.x;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void PageView::handleMoveLogic(const Vec2 &touchPoint)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 nsp = convertToNodeSpace(touchPoint);
|
2014-03-11 17:13:54 +08:00
|
|
|
float offset = 0.0;
|
|
|
|
float moveX = nsp.x;
|
|
|
|
offset = moveX - _touchMoveStartLocation;
|
|
|
|
_touchMoveStartLocation = moveX;
|
|
|
|
if (offset < 0)
|
|
|
|
{
|
2014-05-22 09:56:42 +08:00
|
|
|
_touchMoveDirection = TouchDirection::LEFT;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else if (offset > 0)
|
|
|
|
{
|
2014-05-22 09:56:42 +08:00
|
|
|
_touchMoveDirection = TouchDirection::RIGHT;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
scrollPages(offset);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void PageView::handleReleaseLogic(const Vec2 &touchPoint)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-22 11:43:02 +08:00
|
|
|
if (this->getPageCount() <= 0)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-05-21 18:30:25 +08:00
|
|
|
Widget* curPage = dynamic_cast<Widget*>(this->getPages().at(_curPageIdx));
|
2014-03-11 17:13:54 +08:00
|
|
|
if (curPage)
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 curPagePos = curPage->getPosition();
|
2014-05-22 11:43:02 +08:00
|
|
|
ssize_t pageCount = this->getPageCount();
|
2014-03-11 17:13:54 +08:00
|
|
|
float curPageLocation = curPagePos.x;
|
|
|
|
float pageWidth = getSize().width;
|
|
|
|
float boundary = pageWidth/2.0f;
|
|
|
|
if (curPageLocation <= -boundary)
|
|
|
|
{
|
|
|
|
if (_curPageIdx >= pageCount-1)
|
|
|
|
{
|
|
|
|
scrollPages(-curPageLocation);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scrollToPage(_curPageIdx+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (curPageLocation >= boundary)
|
|
|
|
{
|
|
|
|
if (_curPageIdx <= 0)
|
|
|
|
{
|
|
|
|
scrollPages(-curPageLocation);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scrollToPage(_curPageIdx-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scrollToPage(_curPageIdx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 15:23:13 +08:00
|
|
|
void PageView::passTouchEventToParent(TouchEventType event,Widget* sender, const Vec2 &touchPoint)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-22 15:23:13 +08:00
|
|
|
interceptTouchEvent(event, sender, touchPoint);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-22 15:23:13 +08:00
|
|
|
void PageView::interceptTouchEvent(TouchEventType event, Widget *sender, const Vec2 &touchPoint)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-22 15:23:13 +08:00
|
|
|
switch (event)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-22 15:23:13 +08:00
|
|
|
case TouchEventType::BEGAN:
|
2014-03-11 17:13:54 +08:00
|
|
|
handlePressLogic(touchPoint);
|
|
|
|
break;
|
2014-05-22 15:23:13 +08:00
|
|
|
case TouchEventType::MOVED:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
float offset = 0;
|
|
|
|
offset = fabs(sender->getTouchStartPos().x - touchPoint.x);
|
|
|
|
if (offset > _childFocusCancelOffset)
|
|
|
|
{
|
2014-05-06 12:04:52 +08:00
|
|
|
sender->setHighlighted(false);
|
2014-03-11 17:13:54 +08:00
|
|
|
handleMoveLogic(touchPoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-05-22 15:23:13 +08:00
|
|
|
case TouchEventType::CANCELED:
|
|
|
|
case TouchEventType::ENDED:
|
2014-03-11 17:13:54 +08:00
|
|
|
handleReleaseLogic(touchPoint);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::pageTurningEvent()
|
|
|
|
{
|
|
|
|
if (_pageViewEventListener && _pageViewEventSelector)
|
|
|
|
{
|
|
|
|
(_pageViewEventListener->*_pageViewEventSelector)(this, PAGEVIEW_EVENT_TURNING);
|
|
|
|
}
|
2014-05-12 10:31:52 +08:00
|
|
|
if (_eventCallback) {
|
|
|
|
_eventCallback(this,EventType::TURNING);
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
return _curPageIdx;
|
|
|
|
}
|
|
|
|
|
2014-05-22 11:43:02 +08:00
|
|
|
Vector<Layout*>& PageView::getPages()
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-22 11:43:02 +08:00
|
|
|
return _pages;
|
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;
|
|
|
|
}
|
2014-05-22 13:51:44 +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::copyClonedWidgetChildren(Widget* model)
|
|
|
|
{
|
2014-05-21 16:07:33 +08:00
|
|
|
auto modelPages = static_cast<PageView*>(model)->getPages();
|
2014-03-11 17:13:54 +08:00
|
|
|
for (auto& page : modelPages)
|
|
|
|
{
|
2014-05-22 14:09:01 +08:00
|
|
|
addPage(static_cast<Layout*>(page->clone()));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageView::copySpecialProperties(Widget *widget)
|
|
|
|
{
|
|
|
|
PageView* pageView = dynamic_cast<PageView*>(widget);
|
|
|
|
if (pageView)
|
|
|
|
{
|
|
|
|
Layout::copySpecialProperties(widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|