diff --git a/cocos/2d/CCMenu.cpp b/cocos/2d/CCMenu.cpp index 8d2fb39307..a118b3019b 100644 --- a/cocos/2d/CCMenu.cpp +++ b/cocos/2d/CCMenu.cpp @@ -193,6 +193,12 @@ void Menu::addChild(Node * child, int zOrder, int tag) Layer::addChild(child, zOrder, tag); } +void Menu::addChild(Node * child, int zOrder, const std::string &name) +{ + CCASSERT( dynamic_cast(child) != nullptr, "Menu only supports MenuItem objects as children"); + Layer::addChild(child, zOrder, name); +} + void Menu::onEnter() { Layer::onEnter(); diff --git a/cocos/2d/CCMenu.h b/cocos/2d/CCMenu.h index e4f29e580d..31b9ef6120 100644 --- a/cocos/2d/CCMenu.h +++ b/cocos/2d/CCMenu.h @@ -132,6 +132,7 @@ public: 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 addChild(Node * child, int zOrder, const std::string &name) override; virtual void onEnter() override; virtual void onExit() override; diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index fc9d7a5171..719780e1fb 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -985,20 +985,36 @@ bool Node::doEnumerate(std::string name, std::function callback) * If a class want's to extend the 'addChild' behavior it only needs * to override this method */ -void Node::addChild(Node *child, int zOrder, int tag) +void Node::addChild(Node *child, int localZOrder, int tag) { CCASSERT( child != nullptr, "Argument must be non-nil"); CCASSERT( child->_parent == nullptr, "child already added. It can't be added again"); + addChildHelper(child, localZOrder, tag, "", true); +} + +void Node::addChild(Node* child, int localZOrder, const std::string &name) +{ + CCASSERT(child != nullptr, "Argument must be non-nil"); + CCASSERT(child->_parent == nullptr, "child already added. It can't be added again"); + + addChildHelper(child, localZOrder, INVALID_TAG, name, false); +} + +void Node::addChildHelper(Node* child, int localZOrder, int tag, const std::string &name, bool setTag) +{ if (_children.empty()) { this->childrenAlloc(); } - - this->insertChild(child, zOrder); - - child->_tag = tag; - + + this->insertChild(child, localZOrder); + + if (setTag) + child->setTag(tag); + else + child->setName(name); + child->setParent(this); child->setOrderOfArrival(s_globalOrderOfArrival++); @@ -1014,7 +1030,7 @@ void Node::addChild(Node *child, int zOrder, int tag) } } #endif - + if( _running ) { child->onEnter(); @@ -1038,13 +1054,13 @@ void Node::addChild(Node *child, int zOrder, int tag) void Node::addChild(Node *child, int zOrder) { CCASSERT( child != nullptr, "Argument must be non-nil"); - this->addChild(child, zOrder, child->_tag); + this->addChild(child, zOrder, child->_name); } void Node::addChild(Node *child) { CCASSERT( child != nullptr, "Argument must be non-nil"); - this->addChild(child, child->_localZOrder, child->_tag); + this->addChild(child, child->_localZOrder, child->_name); } void Node::removeFromParent() @@ -1093,6 +1109,22 @@ void Node::removeChildByTag(int tag, bool cleanup/* = true */) } } +void Node::removeChildByName(const std::string &name, bool cleanup) +{ + CCASSERT(name.length() != 0, "Invalid name"); + + Node *child = this->getChildByName(name); + + if (child == nullptr) + { + CCLOG("cocos2d: removeChildByName(name = %s): child not found!", name.c_str()); + } + else + { + this->removeChild(child, cleanup); + } +} + void Node::removeAllChildren() { this->removeAllChildrenWithCleanup(true); diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index c3b9b42e93..b1132995a4 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -677,16 +677,31 @@ public: * @param child A child node * @param zOrder Z order for drawing priority. Please refer to `setLocalZOrder(int)` * @param tag An integer to identify the node easily. Please refer to `setTag(int)` + * + * Please use `addChild(Node* child, int localZOrder, const std::string &name)` instead. */ - virtual void addChild(Node* child, int localZOrder, int tag); + virtual void addChild(Node* child, int localZOrder, int tag); + /** + * Adds a child to the container with z order and tag + * + * If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately. + * + * @param child A child node + * @param zOrder Z order for drawing priority. Please refer to `setLocalZOrder(int)` + * @param name A string to identify the node easily. Please refer to `setName(int)` + * + */ + virtual void addChild(Node* child, int localZOrder, const std::string &name); /** * Gets a child from the container with its tag * * @param tag An identifier to find the child node. * * @return a Node object whose tag equals to the input parameter + * + * Please use `getChildByName()` instead */ - virtual Node * getChildByTag(int tag) const; + virtual Node * getChildByTag(int tag) const; /** * Gets a child from the container with its name * @@ -791,8 +806,17 @@ public: * * @param tag An interger number that identifies a child node * @param cleanup true if all running actions and callbacks on the child node will be cleanup, false otherwise. + * + * Please use `removeChildByName` instead. */ - virtual void removeChildByTag(int tag, bool cleanup = true); + virtual void removeChildByTag(int tag, bool cleanup = true); + /** + * Removes a child from the container by tag value. It will also cleanup all running actions depending on the cleanup parameter + * + * @param name A string that identifies a child node + * @param cleanup true if all running actions and callbacks on the child node will be cleanup, false otherwise. + */ + virtual void removeChildByName(const std::string &name, bool cleanup = true); /** * Removes all children from the container with a cleanup. * @@ -832,16 +856,20 @@ public: * Returns a tag that is used to identify the node easily. * * @return An integer that identifies the node. + * + * Please use `getTag()` instead. */ - virtual int getTag() const; + virtual int getTag() const; /** * Changes the tag that is used to identify the node easily. * * Please refer to getTag for the sample code. * * @param tag A integer that identifies the node. + * + * Please use `setName()` instead. */ - virtual void setTag(int tag); + virtual void setTag(int tag); /** Returns a string that is used to identify the node. * @return A string that identifies the node. @@ -1475,6 +1503,11 @@ protected: virtual void updatePhysicsBodyPosition(Scene* layer); virtual void updatePhysicsBodyRotation(Scene* layer); #endif // CC_USE_PHYSICS + +private: + void addChildHelper(Node* child, int localZOrder, int tag, const std::string &name, bool setTag); + +protected: float _rotationX; ///< rotation on the X-axis float _rotationY; ///< rotation on the Y-axis diff --git a/cocos/2d/CCParallaxNode.cpp b/cocos/2d/CCParallaxNode.cpp index d84a7eabf8..0e83b912ae 100644 --- a/cocos/2d/CCParallaxNode.cpp +++ b/cocos/2d/CCParallaxNode.cpp @@ -93,6 +93,14 @@ void ParallaxNode::addChild(Node * child, int zOrder, int tag) CCASSERT(0,"ParallaxNode: use addChild:z:parallaxRatio:positionOffset instead"); } +void ParallaxNode::addChild(Node * child, int zOrder, const std::string &name) +{ + CC_UNUSED_PARAM(zOrder); + CC_UNUSED_PARAM(child); + CC_UNUSED_PARAM(name); + CCASSERT(0,"ParallaxNode: use addChild:z:parallaxRatio:positionOffset instead"); +} + void ParallaxNode::addChild(Node *child, int z, const Vec2& ratio, const Vec2& offset) { CCASSERT( child != nullptr, "Argument must be non-nil"); @@ -105,7 +113,7 @@ void ParallaxNode::addChild(Node *child, int z, const Vec2& ratio, const Vec2& o pos.y = -pos.y + pos.y * ratio.y + offset.y; child->setPosition(pos); - Node::addChild(child, z, child->getTag()); + Node::addChild(child, z, child->getName()); } void ParallaxNode::removeChild(Node* child, bool cleanup) diff --git a/cocos/2d/CCParallaxNode.h b/cocos/2d/CCParallaxNode.h index a2e80a7f78..df584f2f63 100644 --- a/cocos/2d/CCParallaxNode.h +++ b/cocos/2d/CCParallaxNode.h @@ -65,6 +65,7 @@ public: // Overrides // virtual void addChild(Node * child, int zOrder, int tag) override; + virtual void addChild(Node * child, int zOrder, const std::string &name) override; virtual void removeChild(Node* child, bool cleanup) override; virtual void removeAllChildrenWithCleanup(bool cleanup) override; virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override; diff --git a/cocos/2d/CCParticleBatchNode.cpp b/cocos/2d/CCParticleBatchNode.cpp index ffcf9eba3a..72a0646284 100644 --- a/cocos/2d/CCParticleBatchNode.cpp +++ b/cocos/2d/CCParticleBatchNode.cpp @@ -156,20 +156,40 @@ void ParticleBatchNode::addChild(Node * aChild, int zOrder, int tag) CCASSERT( dynamic_cast(aChild) != nullptr, "CCParticleBatchNode only supports QuadParticleSystems as children"); ParticleSystem* child = static_cast(aChild); CCASSERT( child->getTexture()->getName() == _textureAtlas->getTexture()->getName(), "CCParticleSystem is not using the same texture id"); + + addChildByTagOrName(child, zOrder, tag, "", true); +} + +void ParticleBatchNode::addChild(Node * aChild, int zOrder, const std::string &name) +{ + CCASSERT( aChild != nullptr, "Argument must be non-nullptr"); + CCASSERT( dynamic_cast(aChild) != nullptr, "CCParticleBatchNode only supports QuadParticleSystems as children"); + ParticleSystem* child = static_cast(aChild); + CCASSERT( child->getTexture()->getName() == _textureAtlas->getTexture()->getName(), "CCParticleSystem is not using the same texture id"); + + addChildByTagOrName(child, zOrder, 0, name, false); +} + +void ParticleBatchNode::addChildByTagOrName(ParticleSystem* child, int zOrder, int tag, const std::string &name, bool setTag) +{ // If this is the 1st children, then copy blending function if (_children.empty()) { setBlendFunc(child->getBlendFunc()); } - + CCASSERT( _blendFunc.src == child->getBlendFunc().src && _blendFunc.dst == child->getBlendFunc().dst, "Can't add a ParticleSystem that uses a different blending function"); - + //no lazy sorting, so don't call super addChild, call helper instead - auto pos = addChildHelper(child,zOrder,tag); - + int pos = 0; + if (setTag) + pos = addChildHelper(child, zOrder, tag, "", true); + else + pos = addChildHelper(child, zOrder, 0, name, false); + //get new atlasIndex int atlasIndex = 0; - + if (pos != 0) { ParticleSystem* p = static_cast(_children.at(pos-1)); @@ -179,9 +199,9 @@ void ParticleBatchNode::addChild(Node * aChild, int zOrder, int tag) { atlasIndex = 0; } - + insertChild(child, atlasIndex); - + // update quad info child->setBatchNode(this); } @@ -190,7 +210,7 @@ void ParticleBatchNode::addChild(Node * aChild, int zOrder, int tag) // XXX research whether lazy sorting + freeing current quads and calloc a new block with size of capacity would be faster // XXX or possibly using vertexZ for reordering, that would be fastest // this helper is almost equivalent to Node's addChild, but doesn't make use of the lazy sorting -int ParticleBatchNode::addChildHelper(ParticleSystem* child, int z, int aTag) +int ParticleBatchNode::addChildHelper(ParticleSystem* child, int z, int aTag, const std::string &name, bool setTag) { CCASSERT( child != nullptr, "Argument must be non-nil"); CCASSERT( child->getParent() == nullptr, "child already added. It can't be added again"); @@ -202,7 +222,11 @@ int ParticleBatchNode::addChildHelper(ParticleSystem* child, int z, int aTag) _children.insert(pos, child); - child->setTag(aTag); + if (setTag) + child->setTag(aTag); + else + child->setName(name); + child->_setLocalZOrder(z); child->setParent(this); diff --git a/cocos/2d/CCParticleBatchNode.h b/cocos/2d/CCParticleBatchNode.h index 7318f5d138..21075b2862 100644 --- a/cocos/2d/CCParticleBatchNode.h +++ b/cocos/2d/CCParticleBatchNode.h @@ -95,6 +95,7 @@ public: using Node::addChild; virtual void addChild(Node * child, int zOrder, int tag) override; + virtual void addChild(Node * child, int zOrder, const std::string &name) override; virtual void removeChild(Node* child, bool cleanup) override; virtual void reorderChild(Node * child, int zOrder) override; virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override; @@ -136,7 +137,8 @@ private: void increaseAtlasCapacityTo(ssize_t quantity); int searchNewPositionInChildrenForZ(int z); void getCurrentIndex(int* oldIndex, int* newIndex, Node* child, int z); - int addChildHelper(ParticleSystem* child, int z, int aTag); + int addChildHelper(ParticleSystem* child, int z, int aTag, const std::string &name, bool setTag); + void addChildByTagOrName(ParticleSystem* child, int z, int tag, const std::string &name, bool setTag); void updateBlendFunc(void); /** the texture atlas used for drawing the quads */ TextureAtlas* _textureAtlas; diff --git a/cocos/2d/CCScene.cpp b/cocos/2d/CCScene.cpp index 834a83ffca..25a63f4be2 100644 --- a/cocos/2d/CCScene.cpp +++ b/cocos/2d/CCScene.cpp @@ -111,6 +111,12 @@ void Scene::addChild(Node* child, int zOrder, int tag) addChildToPhysicsWorld(child); } +void Scene::addChild(Node* child, int zOrder, const std::string &name) +{ + Node::addChild(child, zOrder, name); + addChildToPhysicsWorld(child); +} + void Scene::update(float delta) { Node::update(delta); diff --git a/cocos/2d/CCScene.h b/cocos/2d/CCScene.h index 1174d1a9a8..f7a99e9e58 100644 --- a/cocos/2d/CCScene.h +++ b/cocos/2d/CCScene.h @@ -28,6 +28,7 @@ THE SOFTWARE. #ifndef __CCSCENE_H__ #define __CCSCENE_H__ +#include #include "2d/CCNode.h" #include "physics/CCPhysicsWorld.h" @@ -81,6 +82,7 @@ private: #if CC_USE_PHYSICS public: virtual void addChild(Node* child, int zOrder, int tag) override; + virtual void addChild(Node* child, int zOrder, const std::string &name) override; virtual void update(float delta) override; inline PhysicsWorld* getPhysicsWorld() { return _physicsWorld; } static Scene *createWithPhysics(); diff --git a/cocos/2d/CCSprite.cpp b/cocos/2d/CCSprite.cpp index 489a386a71..1192514cca 100644 --- a/cocos/2d/CCSprite.cpp +++ b/cocos/2d/CCSprite.cpp @@ -644,6 +644,27 @@ void Sprite::addChild(Node *child, int zOrder, int tag) Node::addChild(child, zOrder, tag); } +void Sprite::addChild(Node *child, int zOrder, const std::string &name) +{ + CCASSERT(child != nullptr, "Argument must be non-nullptr"); + + if (_batchNode) + { + Sprite* childSprite = dynamic_cast(child); + CCASSERT( childSprite, "CCSprite only supports Sprites as children when using SpriteBatchNode"); + CCASSERT(childSprite->getTexture()->getName() == _textureAtlas->getTexture()->getName(), ""); + //put it in descendants array of batch node + _batchNode->appendChild(childSprite); + + if (!_reorderChildDirty) + { + setReorderChildDirtyRecursively(); + } + } + //CCNode already sets isReorderChildDirty_ so this needs to be after batchNode check + Node::addChild(child, zOrder, name); +} + void Sprite::reorderChild(Node *child, int zOrder) { CCASSERT(child != nullptr, "child must be non null"); diff --git a/cocos/2d/CCSprite.h b/cocos/2d/CCSprite.h index 9fa880f369..b3006f4483 100644 --- a/cocos/2d/CCSprite.h +++ b/cocos/2d/CCSprite.h @@ -414,6 +414,7 @@ public: virtual void reorderChild(Node *child, int zOrder) override; using Node::addChild; virtual void addChild(Node *child, int zOrder, int tag) override; + virtual void addChild(Node *child, int zOrder, const std::string &name) override; virtual void sortAllChildren() override; virtual void setScale(float scale) override; virtual void setPositionZ(float positionZ) override; diff --git a/cocos/2d/CCSpriteBatchNode.cpp b/cocos/2d/CCSpriteBatchNode.cpp index b1aa3a50e0..487a6b328d 100644 --- a/cocos/2d/CCSpriteBatchNode.cpp +++ b/cocos/2d/CCSpriteBatchNode.cpp @@ -182,6 +182,19 @@ void SpriteBatchNode::addChild(Node *child, int zOrder, int tag) appendChild(sprite); } +void SpriteBatchNode::addChild(Node * child, int zOrder, const std::string &name) +{ + CCASSERT(child != nullptr, "child should not be null"); + CCASSERT(dynamic_cast(child) != nullptr, "CCSpriteBatchNode only supports Sprites as children"); + Sprite *sprite = static_cast(child); + // check Sprite is using the same texture id + CCASSERT(sprite->getTexture()->getName() == _textureAtlas->getTexture()->getName(), "CCSprite is not using the same texture id"); + + Node::addChild(child, zOrder, name); + + appendChild(sprite); +} + // override reorderChild void SpriteBatchNode::reorderChild(Node *child, int zOrder) { diff --git a/cocos/2d/CCSpriteBatchNode.h b/cocos/2d/CCSpriteBatchNode.h index 8f9118baec..d544d0472e 100644 --- a/cocos/2d/CCSpriteBatchNode.h +++ b/cocos/2d/CCSpriteBatchNode.h @@ -138,6 +138,7 @@ public: using Node::addChild; virtual void addChild(Node * child, int zOrder, int tag) override; + virtual void addChild(Node * child, int zOrder, const std::string &name) override; virtual void reorderChild(Node *child, int zOrder) override; virtual void removeChild(Node *child, bool cleanup) override; diff --git a/cocos/editor-support/cocostudio/CCBatchNode.cpp b/cocos/editor-support/cocostudio/CCBatchNode.cpp index 6844a43d69..f11e38affc 100644 --- a/cocos/editor-support/cocostudio/CCBatchNode.cpp +++ b/cocos/editor-support/cocostudio/CCBatchNode.cpp @@ -66,16 +66,6 @@ bool BatchNode::init() return ret; } -void BatchNode::addChild(Node *pChild) -{ - Node::addChild(pChild); -} - -void BatchNode::addChild(Node *child, int zOrder) -{ - Node::addChild(child, zOrder); -} - void BatchNode::addChild(Node *child, int zOrder, int tag) { Node::addChild(child, zOrder, tag); @@ -90,6 +80,20 @@ void BatchNode::addChild(Node *child, int zOrder, int tag) } } +void BatchNode::addChild(cocos2d::Node *child, int zOrder, const std::string &name) +{ + Node::addChild(child, zOrder, name); + Armature *armature = dynamic_cast(child); + if (armature != nullptr) + { + armature->setBatchNode(this); + if (_groupCommand == nullptr) + { + _groupCommand = new GroupCommand(); + } + } +} + void BatchNode::removeChild(Node* child, bool cleanup) { Armature *armature = dynamic_cast(child); diff --git a/cocos/editor-support/cocostudio/CCBatchNode.h b/cocos/editor-support/cocostudio/CCBatchNode.h index a49e23f36d..031a939f00 100644 --- a/cocos/editor-support/cocostudio/CCBatchNode.h +++ b/cocos/editor-support/cocostudio/CCBatchNode.h @@ -52,9 +52,9 @@ public: * @js NA */ virtual bool init() override; - virtual void addChild(cocos2d::Node *pChild) override; - virtual void addChild(cocos2d::Node *pChild, int zOrder) override; + using Node::addChild; virtual void addChild(cocos2d::Node *pChild, int zOrder, int tag) override; + virtual void addChild(cocos2d::Node *pChild, int zOrder, const std::string &name) override; virtual void removeChild(cocos2d::Node* child, bool cleanup) override; virtual void visit(cocos2d::Renderer *renderer, const cocos2d::Mat4 &parentTransform, uint32_t parentFlags) override; virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) override; diff --git a/cocos/ui/UILayout.cpp b/cocos/ui/UILayout.cpp index d9b65eb64a..0d4be2801f 100644 --- a/cocos/ui/UILayout.cpp +++ b/cocos/ui/UILayout.cpp @@ -152,16 +152,6 @@ bool Layout::init() } return false; } - -void Layout::addChild(Node *child) -{ - Layout::addChild(child, child->getZOrder(), child->getTag()); -} - -void Layout::addChild(Node * child, int zOrder) -{ - Layout::addChild(child, zOrder, child->getTag()); -} void Layout::addChild(Node *child, int zOrder, int tag) { @@ -172,6 +162,15 @@ void Layout::addChild(Node *child, int zOrder, int tag) _doLayoutDirty = true; } +void Layout::addChild(Node* child, int zOrder, const std::string &name) +{ + if (dynamic_cast(child)) { + supplyTheLayoutParameterLackToChild(static_cast(child)); + } + Widget::addChild(child, zOrder, name); + _doLayoutDirty = true; +} + void Layout::removeChild(Node *child, bool cleanup) { Widget::removeChild(child, cleanup); diff --git a/cocos/ui/UILayout.h b/cocos/ui/UILayout.h index 6c77939185..c19af1c617 100644 --- a/cocos/ui/UILayout.h +++ b/cocos/ui/UILayout.h @@ -232,16 +232,7 @@ public: virtual Type getLayoutType() const; - virtual void addChild(Node * child) override; - /** - * Adds a child to the container with a z-order - * - * If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately. - * - * @param child A child node - * @param zOrder Z order for drawing priority. Please refer to setLocalZOrder(int) - */ - virtual void addChild(Node * child, int zOrder) override; + using Node::addChild; /** * Adds a child to the container with z order and tag * @@ -252,6 +243,7 @@ public: * @param tag A interger to identify the node easily. Please refer to setTag(int) */ virtual void addChild(Node* child, int zOrder, int tag) override; + virtual void addChild(Node* child, int zOrder, const std::string &name) override; virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override; diff --git a/cocos/ui/UIListView.cpp b/cocos/ui/UIListView.cpp index 547d492adc..a795a41d9d 100644 --- a/cocos/ui/UIListView.cpp +++ b/cocos/ui/UIListView.cpp @@ -279,16 +279,6 @@ void ListView::pushBackCustomItem(Widget* 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); @@ -298,7 +288,17 @@ void ListView::addChild(cocos2d::Node *child, int zOrder, int tag) { _items.pushBack(widget); } - +} + +void ListView::addChild(Node* child, int zOrder, const std::string &name) +{ + ScrollView::addChild(child, zOrder, name); + + Widget* widget = dynamic_cast(child); + if (widget) + { + _items.pushBack(widget); + } } void ListView::removeChild(cocos2d::Node *child, bool cleaup) diff --git a/cocos/ui/UIListView.h b/cocos/ui/UIListView.h index 300b301b7c..4635e2182e 100644 --- a/cocos/ui/UIListView.h +++ b/cocos/ui/UIListView.h @@ -163,9 +163,9 @@ public: virtual void doLayout() override; - virtual void addChild(Node * child) override; - virtual void addChild(Node * child, int zOrder) override; + using ScrollView::addChild; virtual void addChild(Node* child, int zOrder, int tag) override; + virtual void addChild(Node* child, int zOrder, const std::string &name) override; virtual void removeAllChildren() override; virtual void removeAllChildrenWithCleanup(bool cleanup) override; virtual void removeChild(Node* child, bool cleaup = true) override; diff --git a/cocos/ui/UIRichText.cpp b/cocos/ui/UIRichText.cpp index 97ca400d27..1f4050039f 100644 --- a/cocos/ui/UIRichText.cpp +++ b/cocos/ui/UIRichText.cpp @@ -371,7 +371,7 @@ void RichText::formarRenderers() Node* l = row->at(j); l->setAnchorPoint(Vec2::ZERO); l->setPosition(Vec2(nextPosX, 0.0f)); - _elementRenderersContainer->addChild(l, 1, (int)j); + _elementRenderersContainer->addChild(l, 1); Size iSize = l->getContentSize(); newContentSizeWidth += iSize.width; newContentSizeHeight = MAX(newContentSizeHeight, iSize.height); @@ -410,7 +410,7 @@ void RichText::formarRenderers() Node* l = row->at(j); l->setAnchorPoint(Vec2::ZERO); l->setPosition(Vec2(nextPosX, nextPosY)); - _elementRenderersContainer->addChild(l, 1, (int)(i*10 + j)); + _elementRenderersContainer->addChild(l, 1); nextPosX += l->getContentSize().width; } } diff --git a/cocos/ui/UIScrollView.cpp b/cocos/ui/UIScrollView.cpp index fe350efe78..4dd4149af2 100644 --- a/cocos/ui/UIScrollView.cpp +++ b/cocos/ui/UIScrollView.cpp @@ -229,21 +229,16 @@ const Size& ScrollView::getInnerContainerSize() const { return _innerContainer->getContentSize(); } - -void ScrollView::addChild(Node *child) -{ - ScrollView::addChild(child, child->getZOrder(), child->getTag()); -} - -void ScrollView::addChild(Node * child, int zOrder) -{ - ScrollView::addChild(child, zOrder, child->getTag()); -} void ScrollView::addChild(Node *child, int zOrder, int tag) { _innerContainer->addChild(child, zOrder, tag); } + +void ScrollView::addChild(Node* child, int zOrder, const std::string &name) +{ + _innerContainer->addChild(child, zOrder, name); +} void ScrollView::removeAllChildren() { diff --git a/cocos/ui/UIScrollView.h b/cocos/ui/UIScrollView.h index 9c6ae494e1..fbd0d684f0 100644 --- a/cocos/ui/UIScrollView.h +++ b/cocos/ui/UIScrollView.h @@ -254,9 +254,9 @@ public: virtual void addEventListener(const ccScrollViewCallback& callback); //all of these functions are related to innerContainer. - virtual void addChild(Node * child) override; - virtual void addChild(Node * child, int zOrder) override; + using Layout::addChild; virtual void addChild(Node* child, int zOrder, int tag) override; + virtual void addChild(Node* child, int zOrder, const std::string &name) override; virtual void removeAllChildren() override; virtual void removeAllChildrenWithCleanup(bool cleanup) override; virtual void removeChild(Node* child, bool cleaup = true) override; diff --git a/extensions/GUI/CCControlExtension/CCScale9Sprite.cpp b/extensions/GUI/CCControlExtension/CCScale9Sprite.cpp index c959fdafcb..43664de763 100644 --- a/extensions/GUI/CCControlExtension/CCScale9Sprite.cpp +++ b/extensions/GUI/CCControlExtension/CCScale9Sprite.cpp @@ -32,19 +32,6 @@ THE SOFTWARE. NS_CC_EXT_BEGIN -enum positions -{ - pCentre = 0, - pTop, - pLeft, - pRight, - pBottom, - pTopRight, - pTopLeft, - pBottomRight, - pBottomLeft -}; - Scale9Sprite::Scale9Sprite() : _spritesGenerated(false) , _spriteFrameRotated(false) @@ -251,48 +238,48 @@ bool Scale9Sprite::updateWithBatchNode(SpriteBatchNode* batchnode, const Rect& o // Centre _centre = Sprite::createWithTexture(_scale9Image->getTexture(), centerbounds); _centre->retain(); - this->addChild(_centre, 0, pCentre); + this->addChild(_centre, 0); // Top _top = Sprite::createWithTexture(_scale9Image->getTexture(), centertopbounds); _top->retain(); - this->addChild(_top, 1, pTop); + this->addChild(_top, 1); // Bottom _bottom = Sprite::createWithTexture(_scale9Image->getTexture(), centerbottombounds); _bottom->retain(); - this->addChild(_bottom, 1, pBottom); + this->addChild(_bottom, 1); // Left _left = Sprite::createWithTexture(_scale9Image->getTexture(), leftcenterbounds); _left->retain(); - this->addChild(_left, 1, pLeft); + this->addChild(_left, 1); // Right _right = Sprite::createWithTexture(_scale9Image->getTexture(), rightcenterbounds); _right->retain(); - this->addChild(_right, 1, pRight); + this->addChild(_right, 1); // Top left _topLeft = Sprite::createWithTexture(_scale9Image->getTexture(), lefttopbounds); _topLeft->retain(); - this->addChild(_topLeft, 2, pTopLeft); + this->addChild(_topLeft, 2); // Top right _topRight = Sprite::createWithTexture(_scale9Image->getTexture(), righttopbounds); _topRight->retain(); - this->addChild(_topRight, 2, pTopRight); + this->addChild(_topRight, 2); // Bottom left _bottomLeft = Sprite::createWithTexture(_scale9Image->getTexture(), leftbottombounds); _bottomLeft->retain(); - this->addChild(_bottomLeft, 2, pBottomLeft); + this->addChild(_bottomLeft, 2); // Bottom right _bottomRight = Sprite::createWithTexture(_scale9Image->getTexture(), rightbottombounds); _bottomRight->retain(); - this->addChild(_bottomRight, 2, pBottomRight); + this->addChild(_bottomRight, 2); } else { // set up transformation of coordinates // to handle the case where the sprite is stored rotated @@ -337,47 +324,47 @@ bool Scale9Sprite::updateWithBatchNode(SpriteBatchNode* batchnode, const Rect& o // Centre _centre = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedcenterbounds, true); _centre->retain(); - this->addChild(_centre, 0, pCentre); + this->addChild(_centre, 0); // Top _top = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedcentertopbounds, true); _top->retain(); - this->addChild(_top, 1, pTop); + this->addChild(_top, 1); // Bottom _bottom = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedcenterbottombounds, true); _bottom->retain(); - this->addChild(_bottom, 1, pBottom); + this->addChild(_bottom, 1); // Left _left = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedleftcenterbounds, true); _left->retain(); - this->addChild(_left, 1, pLeft); + this->addChild(_left, 1); // Right _right = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedrightcenterbounds, true); _right->retain(); - this->addChild(_right, 1, pRight); + this->addChild(_right, 1); // Top left _topLeft = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedlefttopbounds, true); _topLeft->retain(); - this->addChild(_topLeft, 2, pTopLeft); + this->addChild(_topLeft, 2); // Top right _topRight = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedrighttopbounds, true); _topRight->retain(); - this->addChild(_topRight, 2, pTopRight); + this->addChild(_topRight, 2); // Bottom left _bottomLeft = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedleftbottombounds, true); _bottomLeft->retain(); - this->addChild(_bottomLeft, 2, pBottomLeft); + this->addChild(_bottomLeft, 2); // Bottom right _bottomRight = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedrightbottombounds, true); _bottomRight->retain(); - this->addChild(_bottomRight, 2, pBottomRight); + this->addChild(_bottomRight, 2); } this->setContentSize(rect.size); diff --git a/extensions/GUI/CCScrollView/CCScrollView.cpp b/extensions/GUI/CCScrollView/CCScrollView.cpp index c1397370fd..69a1742b40 100644 --- a/extensions/GUI/CCScrollView/CCScrollView.cpp +++ b/extensions/GUI/CCScrollView/CCScrollView.cpp @@ -490,6 +490,18 @@ void ScrollView::addChild(Node * child, int zOrder, int tag) } } +void ScrollView::addChild(Node * child, int zOrder, const std::string &name) +{ + if (_container != child) + { + _container->addChild(child, zOrder, name); + } + else + { + Layer::addChild(child, zOrder, name); + } +} + void ScrollView::beforeDraw() { _beforeDrawCommand.init(_globalZOrder); diff --git a/extensions/GUI/CCScrollView/CCScrollView.h b/extensions/GUI/CCScrollView/CCScrollView.h index 3009619a8b..d26c9240e0 100644 --- a/extensions/GUI/CCScrollView/CCScrollView.h +++ b/extensions/GUI/CCScrollView/CCScrollView.h @@ -229,6 +229,7 @@ public: using Node::addChild; virtual void addChild(Node * child, int zOrder, int tag) override; + virtual void addChild(Node * child, int zOrder, const std::string &name) override; /** * CCActionTweenDelegate