From 530f82ee27fcb6a733eabc8b603bf165629ee44b Mon Sep 17 00:00:00 2001 From: CaiWenzhi Date: Thu, 26 Dec 2013 14:57:30 +0800 Subject: [PATCH 1/2] Add refresh layout method --- cocos/gui/UILayout.cpp | 5 +++++ cocos/gui/UILayout.h | 2 ++ cocos/gui/UIListView.cpp | 5 +++++ cocos/gui/UIListView.h | 2 ++ 4 files changed, 14 insertions(+) diff --git a/cocos/gui/UILayout.cpp b/cocos/gui/UILayout.cpp index aaa41c7fd7..29031e2a29 100644 --- a/cocos/gui/UILayout.cpp +++ b/cocos/gui/UILayout.cpp @@ -709,6 +709,11 @@ LayoutType Layout::getLayoutType() const { return _layoutType; } + +void Layout::requestDoLayout() +{ + _doLayoutDirty = true; +} void Layout::doLayout() { diff --git a/cocos/gui/UILayout.h b/cocos/gui/UILayout.h index a4f7add36e..0feff5447e 100644 --- a/cocos/gui/UILayout.h +++ b/cocos/gui/UILayout.h @@ -212,6 +212,8 @@ public: virtual void visit(); virtual void sortAllChildren() override; + + void requestDoLayout(); protected: //override "init" method of widget. virtual bool init() override; diff --git a/cocos/gui/UIListView.cpp b/cocos/gui/UIListView.cpp index 301093beee..a6cedbb287 100644 --- a/cocos/gui/UIListView.cpp +++ b/cocos/gui/UIListView.cpp @@ -362,6 +362,11 @@ void ListView::setDirection(SCROLLVIEW_DIR dir) } ScrollView::setDirection(dir); } + +void ListView::requestRefreshView() +{ + _refreshViewDirty = true; +} void ListView::refreshView() { diff --git a/cocos/gui/UIListView.h b/cocos/gui/UIListView.h index b7c18c7f0b..0eac270d6e 100644 --- a/cocos/gui/UIListView.h +++ b/cocos/gui/UIListView.h @@ -165,6 +165,8 @@ public: virtual std::string getDescription() const override; + void requestRefreshView(); + protected: virtual void addChild(Node* child) override{ScrollView::addChild(child);}; virtual void addChild(Node * child, int zOrder) override{ScrollView::addChild(child, zOrder);}; From 067af2fb938b52c3fa7a259888afef55cd451962 Mon Sep 17 00:00:00 2001 From: CaiWenzhi Date: Thu, 26 Dec 2013 16:17:52 +0800 Subject: [PATCH 2/2] optimize loop --- cocos/gui/UIHelper.cpp | 23 ++++++++++------------- cocos/gui/UILayout.cpp | 24 ++++++++++-------------- cocos/gui/UIListView.cpp | 13 ++++--------- cocos/gui/UIPageView.cpp | 22 ++++++++-------------- cocos/gui/UIWidget.cpp | 7 ++++--- 5 files changed, 36 insertions(+), 53 deletions(-) diff --git a/cocos/gui/UIHelper.cpp b/cocos/gui/UIHelper.cpp index 814a4bdf53..2d9264cb4a 100644 --- a/cocos/gui/UIHelper.cpp +++ b/cocos/gui/UIHelper.cpp @@ -38,7 +38,7 @@ Widget* UIHelper::seekWidgetByTag(Widget* root, int tag) { return root; } - Vector arrayRootChildren = root->getChildren(); + const auto& arrayRootChildren = root->getChildren(); int length = arrayRootChildren.size(); for (int i=0;i arrayRootChildren = root->getChildren(); - int length = arrayRootChildren.size(); - for (int i=0;igetChildren(); + for (auto& subWidget : arrayRootChildren) { - Widget* child = static_cast(arrayRootChildren.at(i)); + Widget* child = static_cast(subWidget); Widget* res = seekWidgetByName(child,name); if (res != nullptr) { @@ -82,11 +81,10 @@ Widget* UIHelper::seekWidgetByRelativeName(Widget *root, const char *name) { return nullptr; } - Vector arrayRootChildren = root->getChildren(); - int length = arrayRootChildren.size(); - for (int i=0;igetChildren(); + for (auto& subWidget : arrayRootChildren) { - Widget* child = static_cast(arrayRootChildren.at(i)); + Widget* child = static_cast(subWidget); RelativeLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)); if (layoutParameter && strcmp(layoutParameter->getRelativeName(), name) == 0) { @@ -107,11 +105,10 @@ Widget* UIHelper::seekActionWidgetByActionTag(Widget* root, int tag) { return root; } - Vector arrayRootChildren = root->getChildren(); - int length = arrayRootChildren.size(); - for (int i=0;igetChildren(); + for (auto& subWidget : arrayRootChildren) { - Widget* child = static_cast(arrayRootChildren.at(i)); + Widget* child = static_cast(subWidget); Widget* res = seekActionWidgetByActionTag(child,tag); if (res != nullptr) { diff --git a/cocos/gui/UILayout.cpp b/cocos/gui/UILayout.cpp index 29031e2a29..f63ddb2c0d 100644 --- a/cocos/gui/UILayout.cpp +++ b/cocos/gui/UILayout.cpp @@ -727,12 +727,12 @@ void Layout::doLayout() break; case LAYOUT_LINEAR_VERTICAL: { - int length = _widgetChildren.size(); Size layoutSize = getSize(); float topBoundary = layoutSize.height; - for (int i=0; i(_widgetChildren.at(i)); + Widget* child = static_cast(subWidget); LinearLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); if (layoutParameter) @@ -767,12 +767,11 @@ void Layout::doLayout() } case LAYOUT_LINEAR_HORIZONTAL: { - int length = _widgetChildren.size(); Size layoutSize = getSize(); float leftBoundary = 0.0f; - for (int i=0; i(_widgetChildren.at(i)); + Widget* child = static_cast(subWidget); LinearLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); if (layoutParameter) @@ -807,22 +806,19 @@ void Layout::doLayout() } case LAYOUT_RELATIVE: { - int length = _widgetChildren.size(); - int unlayoutChildCount = length; + int unlayoutChildCount = _widgetChildren.size(); Size layoutSize = getSize(); - - for (int i=0; i(_widgetChildren.at(i)); + Widget* child = static_cast(subWidget); RelativeLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)); layoutParameter->_put = false; } - while (unlayoutChildCount > 0) { - for (int i=0; i(_widgetChildren.at(i)); + Widget* child = static_cast(subWidget); RelativeLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)); if (layoutParameter) diff --git a/cocos/gui/UIListView.cpp b/cocos/gui/UIListView.cpp index a6cedbb287..5d608ac6dc 100644 --- a/cocos/gui/UIListView.cpp +++ b/cocos/gui/UIListView.cpp @@ -90,9 +90,8 @@ void ListView::updateInnerContainerSize() { int length = _items.size(); float totalHeight = (length - 1) * _itemsMargin; - for (int i=0; igetSize().height; } float finalWidth = _size.width; @@ -104,9 +103,8 @@ void ListView::updateInnerContainerSize() { int length = _items.size(); float totalWidth = (length - 1) * _itemsMargin; - for (int i=0; igetSize().width; } float finalWidth = totalWidth; @@ -446,12 +444,9 @@ Widget* ListView::createCloneInstance() void ListView::copyClonedWidgetChildren(Widget* model) { - Vector arrayItems = getItems(); - - int length = arrayItems.size(); - for (int i=0; iclone()); } } diff --git a/cocos/gui/UIPageView.cpp b/cocos/gui/UIPageView.cpp index 784fc31b8f..dcc0dbf593 100644 --- a/cocos/gui/UIPageView.cpp +++ b/cocos/gui/UIPageView.cpp @@ -181,7 +181,7 @@ void PageView::insertPage(Layout* page, int idx) page->setSize(pvSize); } int length = _pages.size(); - for (int i=(idx+1); igetPosition(); behindPage->setPosition(Point(formerPos.x+getSize().width, 0)); @@ -268,10 +268,8 @@ void PageView::onSizeChanged() void PageView::updateChildrenSize() { Size selfSize = getSize(); - int length = _pages.size(); - for (long i=0; isetSize(selfSize); } } @@ -410,13 +408,11 @@ void PageView::onTouchCancelled(Touch *touch, Event *unusedEvent) void PageView::movePages(float offset) { - int length = _pages.size(); - for (int i = 0; i < length; i++) + for (auto& page : _pages) { - Widget* child = _pages.at(i); - _movePagePoint.x = child->getPosition().x + offset; - _movePagePoint.y = child->getPosition().y; - child->setPosition(_movePagePoint); + _movePagePoint.x = page->getPosition().x + offset; + _movePagePoint.y = page->getPosition().y; + page->setPosition(_movePagePoint); } } @@ -606,11 +602,9 @@ Widget* PageView::createCloneInstance() void PageView::copyClonedWidgetChildren(Widget* model) { - Vector modelPages = dynamic_cast(model)->getPages(); - int length = modelPages.size(); - for (int i=0; i(model)->getPages(); + for (auto& page : modelPages) { - Layout* page = modelPages.at(i); addPage(dynamic_cast(page->clone())); } } diff --git a/cocos/gui/UIWidget.cpp b/cocos/gui/UIWidget.cpp index 58b3284119..495821cd99 100644 --- a/cocos/gui/UIWidget.cpp +++ b/cocos/gui/UIWidget.cpp @@ -977,10 +977,11 @@ Widget* Widget::createCloneInstance() void Widget::copyClonedWidgetChildren(Widget* model) { - int length = model->getChildren().size(); - for (int i=0; igetChildren(); + + for (auto& subWidget : modelChildren) { - Widget* child = static_cast(model->getChildren().at(i)); + Widget* child = static_cast(subWidget); addChild(child->clone()); } }