From 1165ba8bc3724cd7f5ae9ca16add8014ba014b4b Mon Sep 17 00:00:00 2001 From: andyque Date: Thu, 22 May 2014 17:49:19 +0800 Subject: [PATCH] issue #5176, readd addChild methods into UIListView --- cocos/ui/UILayout.cpp | 4 +- cocos/ui/UIListView.cpp | 58 ++++++++++++++++--- cocos/ui/UIListView.h | 17 ++---- cocos/ui/UIScrollView.cpp | 2 +- .../UIListViewTest/UIListViewTest.cpp | 12 +++- 5 files changed, 70 insertions(+), 23 deletions(-) diff --git a/cocos/ui/UILayout.cpp b/cocos/ui/UILayout.cpp index 695bd2cc5e..fc5deded8e 100644 --- a/cocos/ui/UILayout.cpp +++ b/cocos/ui/UILayout.cpp @@ -676,7 +676,9 @@ void Layout::addChild(Node * child, int zOrder) void Layout::addChild(Node *child, int zOrder, int tag) { - supplyTheLayoutParameterLackToChild(static_cast(child)); + if (dynamic_cast(child)) { + supplyTheLayoutParameterLackToChild(static_cast(child)); + } Widget::addChild(child, zOrder, tag); _doLayoutDirty = true; } diff --git a/cocos/ui/UIListView.cpp b/cocos/ui/UIListView.cpp index f40e22102e..2133cf0324 100644 --- a/cocos/ui/UIListView.cpp +++ b/cocos/ui/UIListView.cpp @@ -251,7 +251,6 @@ void ListView::pushBackDefaultItem() return; } Widget* newItem = _model->clone(); - _items.pushBack(newItem); remedyLayoutParameter(newItem); addChild(newItem); _refreshViewDirty = true; @@ -264,25 +263,71 @@ void ListView::insertDefaultItem(ssize_t index) return; } Widget* newItem = _model->clone(); + _items.insert(index, newItem); + ScrollView::addChild(newItem); + remedyLayoutParameter(newItem); - addChild(newItem); + _refreshViewDirty = true; } void ListView::pushBackCustomItem(Widget* item) { - _items.pushBack(item); remedyLayoutParameter(item); addChild(item); _refreshViewDirty = true; } + +void ListView::addChild(cocos2d::Node *child) +{ + ListView::addChild(child, child->getZOrder(), child->getTag()); +} + +void ListView::addChild(cocos2d::Node *child, int zOrder) +{ + ListView::addChild(child, zOrder, child->getTag()); +} + +void ListView::addChild(cocos2d::Node *child, int zOrder, int tag) +{ + ScrollView::addChild(child, zOrder, tag); + + Widget* widget = dynamic_cast(child); + if (widget) + { + _items.pushBack(widget); + } + +} + +void ListView::removeChild(cocos2d::Node *child, bool cleaup) +{ + Widget* widget = dynamic_cast(child); + if (widget) { + _items.eraseObject(widget); + } + + ScrollView::removeChild(child, cleaup); +} + +void ListView::removeAllChildren() +{ + this->removeAllChildrenWithCleanup(true); +} + +void ListView::removeAllChildrenWithCleanup(bool cleanup) +{ + ScrollView::removeAllChildrenWithCleanup(cleanup); + _items.clear(); +} void ListView::insertCustomItem(Widget* item, ssize_t index) { _items.insert(index, item); + ScrollView::addChild(item); + remedyLayoutParameter(item); - addChild(item); _refreshViewDirty = true; } @@ -293,8 +338,8 @@ void ListView::removeItem(ssize_t index) { return; } - _items.eraseObject(item); - removeChild(item); + removeChild(item, true); + _refreshViewDirty = true; } @@ -305,7 +350,6 @@ void ListView::removeLastItem() void ListView::removeAllItems() { - _items.clear(); removeAllChildren(); } diff --git a/cocos/ui/UIListView.h b/cocos/ui/UIListView.h index 7dcc07bc97..563f6bfaeb 100644 --- a/cocos/ui/UIListView.h +++ b/cocos/ui/UIListView.h @@ -162,6 +162,12 @@ public: float getItemsMargin(); virtual void sortAllChildren() override; + virtual void addChild(Node * child) override; + virtual void addChild(Node * child, int zOrder) override; + virtual void addChild(Node* child, int zOrder, int tag) override; + virtual void removeAllChildren() override; + virtual void removeAllChildrenWithCleanup(bool cleanup) override; + virtual void removeChild(Node* child, bool cleaup = true) override; ssize_t getCurSelectedIndex() const; @@ -186,18 +192,7 @@ CC_CONSTRUCTOR_ACCESS: virtual bool init() override; protected: - virtual void addChild(Node* child) override{ScrollView::addChild(child);}; - virtual void addChild(Node * child, int zOrder) override{ScrollView::addChild(child, zOrder);}; - virtual void addChild(Node* child, int zOrder, int tag) override{ScrollView::addChild(child, zOrder, tag);}; - virtual void removeChild(Node* widget, bool cleanup = true) override{ScrollView::removeChild(widget, cleanup);}; - virtual void removeAllChildren() override{removeAllChildrenWithCleanup(true);}; - virtual void removeAllChildrenWithCleanup(bool cleanup) override {ScrollView::removeAllChildrenWithCleanup(cleanup);}; - virtual Vector& getChildren() override{return ScrollView::getChildren();}; - virtual const Vector& getChildren() const override{return ScrollView::getChildren();}; - virtual ssize_t getChildrenCount() const override {return ScrollView::getChildrenCount();}; - virtual Node * getChildByTag(int tag) const override {return ScrollView::getChildByTag(tag);}; - virtual Widget* getChildByName(const std::string& name) override {return ScrollView::getChildByName(name);}; void updateInnerContainerSize(); void remedyLayoutParameter(Widget* item); virtual void onSizeChanged() override; diff --git a/cocos/ui/UIScrollView.cpp b/cocos/ui/UIScrollView.cpp index 775d81e0e0..879f428f0a 100644 --- a/cocos/ui/UIScrollView.cpp +++ b/cocos/ui/UIScrollView.cpp @@ -153,7 +153,7 @@ void ScrollView::initRenderer() { Layout::initRenderer(); _innerContainer = Layout::create(); - Layout::addChild(_innerContainer,1,1); + addProtectedChild(_innerContainer, 1, 1); } void ScrollView::onSizeChanged() diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIListViewTest/UIListViewTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIListViewTest/UIListViewTest.cpp index f99d14485e..ce6a001d2e 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIListViewTest/UIListViewTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIListViewTest/UIListViewTest.cpp @@ -57,7 +57,6 @@ bool UIListViewTest_Vertical::init() ListView* listView = ListView::create(); // set list view ex direction listView->setDirection(ui::ScrollView::Direction::VERTICAL); - listView->setTouchEnabled(true); listView->setBounceEnabled(true); listView->setBackGroundImage("cocosui/green_edit.png"); listView->setBackGroundImageScale9Enabled(true); @@ -96,6 +95,12 @@ bool UIListViewTest_Vertical::init() listView->insertDefaultItem(0); } + listView->removeAllChildren(); + + Sprite* testSprite = Sprite::create("cocosui/backtotoppressed.png"); + testSprite->setPosition(Vec2(200,200)); + listView->addChild(testSprite); + // add custom item for (int i = 0; i < count / 4; ++i) { @@ -109,7 +114,7 @@ bool UIListViewTest_Vertical::init() custom_button->setPosition(Vec2(custom_item->getSize().width / 2.0f, custom_item->getSize().height / 2.0f)); custom_item->addChild(custom_button); - listView->pushBackCustomItem(custom_item); + listView->addChild(custom_item); } // insert custom item Vector& items = listView->getItems(); @@ -125,6 +130,7 @@ bool UIListViewTest_Vertical::init() custom_item->setSize(custom_button->getSize()); custom_button->setPosition(Vec2(custom_item->getSize().width / 2.0f, custom_item->getSize().height / 2.0f)); custom_item->addChild(custom_button); + custom_item->setTag(1); listView->insertCustomItem(custom_item, items_count); } @@ -140,7 +146,7 @@ bool UIListViewTest_Vertical::init() } // remove last item - listView->removeLastItem(); + listView->removeChildByTag(1); // remove item by index items_count = items.size();