From 0c065b4be08e8ec2d6211b9ce053d1911b86aef4 Mon Sep 17 00:00:00 2001 From: andyque Date: Thu, 22 May 2014 13:51:44 +0800 Subject: [PATCH] issue #5176, refactor Layout and UIPageView --- cocos/ui/UILayout.cpp | 2 +- cocos/ui/UIPageView.cpp | 84 +++++++++++++++-------------------------- cocos/ui/UIPageView.h | 19 ++++------ 3 files changed, 39 insertions(+), 66 deletions(-) diff --git a/cocos/ui/UILayout.cpp b/cocos/ui/UILayout.cpp index f4c07c3395..9f10d02f56 100644 --- a/cocos/ui/UILayout.cpp +++ b/cocos/ui/UILayout.cpp @@ -729,12 +729,12 @@ void Layout::visit(Renderer *renderer, const Mat4 &parentTransform, bool parentT { ProtectedNode::visit(renderer, parentTransform, parentTransformUpdated); } + doLayout(); } void Layout::sortAllChildren() { Widget::sortAllChildren(); - doLayout(); } void Layout::stencilClippingVisit(Renderer *renderer, const Mat4 &parentTransform, bool parentTransformUpdated) diff --git a/cocos/ui/UIPageView.cpp b/cocos/ui/UIPageView.cpp index 630b0f869b..8317b3d989 100644 --- a/cocos/ui/UIPageView.cpp +++ b/cocos/ui/UIPageView.cpp @@ -80,7 +80,6 @@ bool PageView::init() if (Layout::init()) { setClippingEnabled(true); - setTouchEnabled(true); return true; } return false; @@ -88,14 +87,11 @@ bool PageView::init() void PageView::addWidgetToPage(Widget *widget, ssize_t pageIdx, bool forceCreate) { - if (!widget) - { - return; - } - if (pageIdx < 0) + if (!widget || pageIdx < 0) { return; } + ssize_t pageCount = this->getPageCount(); if (pageIdx < 0 || pageIdx >= pageCount) { @@ -126,45 +122,25 @@ Layout* PageView::createPage() void PageView::addPage(Layout* page) { - if (!page) + if (!page || _pages.contains(page)) { return; } - - if (_pages.contains(page)) - { - 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); - } - page->setPosition(Vec2(getPositionXByIndex(this->getPageCount()), 0)); + addProtectedChild(page); _pages.pushBack(page); - updateBoundaryPages(); + _doLayoutDirty = true; } void PageView::insertPage(Layout* page, int idx) { - if (idx < 0) - { - return; - } - if (!page) - { - return; - } - - if (_pages.contains(page)) + if (idx < 0 || !page || _pages.contains(page)) { return; } + ssize_t pageCount = this->getPageCount(); if (idx >= pageCount) @@ -176,23 +152,9 @@ void PageView::insertPage(Layout* page, int idx) _pages.insert(idx, page); addProtectedChild(page); - page->setPosition(Vec2(getPositionXByIndex(idx), 0)); - 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); - } - ssize_t length = this->getPageCount(); - for (ssize_t i=(idx+1); igetPosition(); - behindPage->setPosition(Vec2(formerPos.x+getSize().width, 0)); - } - updateBoundaryPages(); } + + _doLayoutDirty = true; } void PageView::removePage(Layout* page) @@ -204,9 +166,7 @@ void PageView::removePage(Layout* page) removeProtectedChild(page); _pages.eraseObject(page); - updateAllPagesPosition(); - - updateBoundaryPages(); + _doLayoutDirty = true; } void PageView::removePageAtIndex(ssize_t index) @@ -221,7 +181,6 @@ void PageView::removePageAtIndex(ssize_t index) void PageView::removeAllPages() { - for(auto& node : _pages) { removeProtectedChild(node); @@ -256,8 +215,7 @@ void PageView::onSizeChanged() Layout::onSizeChanged(); _rightBoundary = getSize().width; - updateAllPagesSize(); - updateAllPagesPosition(); + _doLayoutDirty = true; } void PageView::updateAllPagesSize() @@ -272,15 +230,18 @@ void PageView::updateAllPagesSize() void PageView::updateAllPagesPosition() { ssize_t pageCount = this->getPageCount(); + if (pageCount <= 0) { _curPageIdx = 0; return; } + if (_curPageIdx >= pageCount) { _curPageIdx = pageCount-1; } + float pageWidth = getSize().width; for (int i=0; igetLocation()); } +void PageView::doLayout() +{ + if (!_doLayoutDirty) + { + return; + } + + updateAllPagesPosition(); + updateAllPagesSize(); + updateBoundaryPages(); + + + _doLayoutDirty = false; +} + void PageView::movePages(float offset) { for (auto& page : this->getPages()) @@ -582,7 +558,7 @@ Layout* PageView::getPage(ssize_t index) { return nullptr; } - return dynamic_cast(this->getPages().at(index)); + return _pages.at(index); } std::string PageView::getDescription() const diff --git a/cocos/ui/UIPageView.h b/cocos/ui/UIPageView.h index 6f48d23a03..b0da7cd06d 100644 --- a/cocos/ui/UIPageView.h +++ b/cocos/ui/UIPageView.h @@ -40,7 +40,7 @@ CC_DEPRECATED_ATTRIBUTE typedef enum CC_DEPRECATED_ATTRIBUTE typedef void (Ref::*SEL_PageViewEvent)(Ref*, PageViewEventType); #define pagevieweventselector(_SELECTOR)(SEL_PageViewEvent)(&_SELECTOR) -class PageView : public Layout , public UIScrollInterface +class PageView : public Layout { DECLARE_CLASS_GUI_INFO @@ -188,22 +188,19 @@ protected: void updateAllPagesSize(); void updateAllPagesPosition(); + virtual void handlePressLogic(const Vec2 &touchPoint); + virtual void handleMoveLogic(const Vec2 &touchPoint) ; + virtual void handleReleaseLogic(const Vec2 &touchPoint) ; + virtual void interceptTouchEvent(int handleState, Widget* sender, const Vec2 &touchPoint) ; + //overrided functions - virtual void handlePressLogic(const Vec2 &touchPoint) override; - virtual void handleMoveLogic(const Vec2 &touchPoint) override; - virtual void handleReleaseLogic(const Vec2 &touchPoint) override; - virtual void interceptTouchEvent(int handleState, Widget* sender, const Vec2 &touchPoint) override; virtual void checkChildInfo(int handleState, Widget* sender, const Vec2 &touchPoint) override; virtual void onSizeChanged() override; virtual Widget* createCloneInstance() override; virtual void copySpecialProperties(Widget* model) override; virtual void copyClonedWidgetChildren(Widget* model) override; - //do we really need this method??? - virtual void doLayout() override - { - if (!_doLayoutDirty){return;} - _doLayoutDirty = false; - }; + + virtual void doLayout() override; protected: ssize_t _curPageIdx;