From 742fb9e22d608bbc7c603ad7e8c65e82a20701b0 Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Tue, 1 Mar 2016 13:07:21 +0800 Subject: [PATCH 1/4] add getComponents, getComponentInChildren, getComponentsInChildren for Components API --- cocos2d/core/CCNode.js | 126 +++++++++++++++++++++---- cocos2d/core/components/CCComponent.js | 55 ++++++++--- test/qunit/unit/test-node.js | 36 +++++++ 3 files changed, 188 insertions(+), 29 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 7e0f4a5540..bb773cb799 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -241,6 +241,63 @@ var _searchMaskParent = function (node) { return null; }; +function getConstructor (typeOrClassName) { + if ( !typeOrClassName ) { + cc.error('getComponent: Type must be non-nil'); + return null; + } + if (typeof typeOrClassName === 'string') { + return JS.getClassByName(typeOrClassName); + } + + return typeOrClassName; +} + +function findComponent (node, constructor) { + for (var c = 0; c < node._components.length; ++c) { + var comp = node._components[c]; + if (comp instanceof constructor) { + return comp; + } + } + return null; +} + +function findComponents (node, constructor, components) { + for (var c = 0; c < node._components.length; ++c) { + var comp = node._components[c]; + if (comp instanceof constructor) { + components.push(comp); + } + } +} + +function findChildComponent (children, constructor) { + for (var c = 0; c < children.length; ++c) { + var node = children[c]; + var comp = findComponent(node, constructor); + if (comp) { + return comp; + } + } + return null; +} + +function findChildComponents (children, constructor, components) { + for (var c = 0; c < children.length; ++c) { + var node = children[c]; + var comp = findComponents(node, constructor, components); + if (comp) { + components.push(comp); + } + else { + comp = findChildComponents(node._children, constructor, components); + if (comp) { + components.push(comp); + } + } + } +} /** * Class of all entities in Cocos Creator scenes. @@ -461,28 +518,61 @@ var Node = cc.Class({ * @returns {Component} */ getComponent: function (typeOrClassName) { - if ( !typeOrClassName ) { - cc.error('getComponent: Type must be non-nil'); - return null; - } - var constructor; - if (typeof typeOrClassName === 'string') { - constructor = JS.getClassByName(typeOrClassName); - } - else { - constructor = typeOrClassName; - } + var constructor = getConstructor(typeOrClassName); if (constructor) { - for (var c = 0; c < this._components.length; ++c) { - var component = this._components[c]; - if (component instanceof constructor) { - return component; - } - } + return findComponent(this, constructor); } return null; }, + /** + * Returns all components of supplied Type in the node. + * + * @method getComponents + * @param {Function|String} typeOrClassName + * @returns {Component[]} + */ + getComponents: function (typeOrClassName) { + var constructor = getConstructor(typeOrClassName), components = []; + if (constructor) { + findComponents(this, constructor, components); + } + + return components; + }, + + /** + * Returns the component of Type, type in the any of its children using depth first search. + * + * @method getComponentInChildren + * @param {Function|String} typeOrClassName + * @returns {Component} + */ + getComponentInChildren: function (typeOrClassName) { + var constructor = getConstructor(typeOrClassName); + if (constructor) { + return findChildComponent(this._children, constructor); + } + + return null; + }, + + /** + * Returns all components of Type, type in the any of its children. + * + * @method getComponentsInChildren + * @param {Function|String} typeOrClassName + * @returns {Component[]} + */ + getComponentsInChildren: function (typeOrClassName) { + var constructor = getConstructor(typeOrClassName), components = []; + if (constructor) { + findChildComponents(this._children, constructor, components); + } + + return components; + }, + _checkMultipleComp: CC_EDITOR && function (ctor) { var err, existing = this.getComponent(ctor._disallowMultiple); if (existing) { @@ -500,7 +590,7 @@ var Node = cc.Class({ }, /** - * Adds a component class to the node. You can also add component to entity by passing in the name of the script. + * Adds a component class to the node. You can also add component to node by passing in the name of the script. * * @method addComponent * @param {Function|String} typeOrClassName - The constructor or the class name of the component to add diff --git a/cocos2d/core/components/CCComponent.js b/cocos2d/core/components/CCComponent.js index 6c602b50bc..46ec4fa32b 100644 --- a/cocos2d/core/components/CCComponent.js +++ b/cocos2d/core/components/CCComponent.js @@ -322,7 +322,7 @@ var Component = cc.Class({ }, /** - * indicates whether this component is enabled and its entity is also active in the hierarchy. + * indicates whether this component is enabled and its node is also active in the hierarchy. * @property enabledInHierarchy * @type {Boolean} * @readOnly @@ -376,7 +376,7 @@ var Component = cc.Class({ lateUpdate: null, /** - * When attaching to an active entity or its entity first activated + * When attaching to an active node or its node first activated * @method onLoad */ onLoad: null, @@ -388,13 +388,13 @@ var Component = cc.Class({ start: null, /** - * Called when this component becomes enabled and its entity becomes active + * Called when this component becomes enabled and its node becomes active * @method onEnable */ onEnable: null, /** - * Called when this component becomes disabled or its entity becomes inactive + * Called when this component becomes disabled or its node becomes inactive * @method onDisable */ onDisable: null, @@ -423,11 +423,11 @@ var Component = cc.Class({ // PUBLIC /** - * Adds a component class to the entity. You can also add component to entity by passing in the name of the + * Adds a component class to the node. You can also add component to node by passing in the name of the * script. * * @method addComponent - * @param {Function|String} typeOrName - the constructor or the class name of the component to add + * @param {Function|String} typeOrTypename - the constructor or the class name of the component to add * @return {Component} - the newly added component */ addComponent: function (typeOrTypename) { @@ -435,15 +435,48 @@ var Component = cc.Class({ }, /** - * Returns the component of supplied type if the entity has one attached, null if it doesn't. You can also get - * component in the entity by passing in the name of the script. + * Returns the component of supplied type if the node has one attached, null if it doesn't. You can also get + * component in the node by passing in the name of the script. * * @method getComponent - * @param {Function|String} typeOrName + * @param {Function|String} typeOrClassName * @return {Component} */ - getComponent: function (typeOrTypename) { - return this.node.getComponent(typeOrTypename); + getComponent: function (typeOrClassName) { + return this.node.getComponent(typeOrClassName); + }, + + /** + * Returns all components of supplied Type in the node. + * + * @method getComponents + * @param {Function|String} typeOrClassName + * @return {Component[]} + */ + getComponents: function (typeOrClassName) { + return this.node.getComponents(typeOrClassName); + }, + + /** + * Returns the component of Type, type in the any of its children using depth first search. + * + * @method getComponentInChildren + * @param {Function|String} typeOrClassName + * @returns {Component} + */ + getComponentInChildren: function (typeOrClassName) { + return this.node.getComponentInChildren(typeOrClassName); + }, + + /** + * Returns all components of Type, type in the any of its children. + * + * @method getComponentsInChildren + * @param {Function|String} typeOrClassName + * @returns {Component[]} + */ + getComponentsInChildren: function (typeOrClassName) { + return this.node.getComponentsInChildren(typeOrClassName); }, ///** diff --git a/test/qunit/unit/test-node.js b/test/qunit/unit/test-node.js index d6fa271bd6..b610507eb7 100644 --- a/test/qunit/unit/test-node.js +++ b/test/qunit/unit/test-node.js @@ -152,6 +152,42 @@ test('activation logic for component', function () { cc.js.unregisterClass(MyComponent, MyComponentBase); }); +test('get component related code', function () { + + var MyComponent = cc.Class({ + name: 'MyComponent', + extends: cc.Component + }); + + var obj = new cc.Node("New Node"); + cc.director.getScene().addChild(obj); + var comp1 = obj.addComponent(MyComponent); + var comp2 = obj.addComponent(MyComponent); + var comp3 = obj.addComponent(MyComponent); + + //-- layer 1 + var obj1 = new cc.Node("New Node 1"); + obj1.parent = obj; + + var obj2 = new cc.Node("New Node 4"); + var comp4 = obj2.addComponent(MyComponent); + obj2.parent = obj; + + //-- layer 2 + var obj3 = new cc.Node("New Node 2"); + var comp5 = obj3.addComponent(MyComponent); + obj3.parent = obj1; + + var obj4 = new cc.Node("New Node 3"); + obj4.parent = obj1; + + ok(obj.getComponents(MyComponent).length === 3, 'getComponents: can get my component array'); + ok(obj1.getComponentInChildren(MyComponent) === comp5, 'getComponentInChildren: can get my component in children'); + ok(obj.getComponentsInChildren(MyComponent).length === 2, 'getComponentsInChildren: can get my components in children'); + + cc.js.unregisterClass(MyComponent); +}); + test('life cycle logic for component', function () { // my component var MyComponent = cc.Class({ From 9d1f281fedb11828279cd795e6a74f45129672ba Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Tue, 1 Mar 2016 14:04:28 +0800 Subject: [PATCH 2/4] fix findChildComponents --- cocos2d/core/CCNode.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index bb773cb799..294df176f2 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -291,10 +291,7 @@ function findChildComponents (children, constructor, components) { components.push(comp); } else { - comp = findChildComponents(node._children, constructor, components); - if (comp) { - components.push(comp); - } + findChildComponents(node._children, constructor, components); } } } From 22e1e644c30f939a0a543fda82ae5f927e95af12 Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Tue, 1 Mar 2016 15:04:31 +0800 Subject: [PATCH 3/4] modify CCNode, CCComponent, tes-node --- cocos2d/core/CCNode.js | 30 +++++++++++------------ cocos2d/core/components/CCComponent.js | 4 ++-- test/qunit/unit/test-node.js | 33 ++++++++++++++++++-------- 3 files changed, 40 insertions(+), 27 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 294df176f2..335eacb759 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -254,8 +254,8 @@ function getConstructor (typeOrClassName) { } function findComponent (node, constructor) { - for (var c = 0; c < node._components.length; ++c) { - var comp = node._components[c]; + for (var i = 0; i < node._components.length; ++i) { + var comp = node._components[i]; if (comp instanceof constructor) { return comp; } @@ -264,8 +264,8 @@ function findComponent (node, constructor) { } function findComponents (node, constructor, components) { - for (var c = 0; c < node._components.length; ++c) { - var comp = node._components[c]; + for (var i = 0; i < node._components.length; ++i) { + var comp = node._components[i]; if (comp instanceof constructor) { components.push(comp); } @@ -273,24 +273,24 @@ function findComponents (node, constructor, components) { } function findChildComponent (children, constructor) { - for (var c = 0; c < children.length; ++c) { - var node = children[c]; + for (var i = 0; i < children.length; ++i) { + var node = children[i]; var comp = findComponent(node, constructor); if (comp) { return comp; } + else if(node.children.length > 0) { + return findChildComponent(node.children, constructor); + } } return null; } function findChildComponents (children, constructor, components) { - for (var c = 0; c < children.length; ++c) { - var node = children[c]; - var comp = findComponents(node, constructor, components); - if (comp) { - components.push(comp); - } - else { + for (var i = 0; i < children.length; ++i) { + var node = children[i]; + findComponents(node, constructor, components); + if (node._children.length > 0 ) { findChildComponents(node._children, constructor, components); } } @@ -539,7 +539,7 @@ var Node = cc.Class({ }, /** - * Returns the component of Type, type in the any of its children using depth first search. + * Returns the component of supplied type in any of its children using depth first search. * * @method getComponentInChildren * @param {Function|String} typeOrClassName @@ -555,7 +555,7 @@ var Node = cc.Class({ }, /** - * Returns all components of Type, type in the any of its children. + * Returns the components of supplied type in any of its children using depth first search. * * @method getComponentsInChildren * @param {Function|String} typeOrClassName diff --git a/cocos2d/core/components/CCComponent.js b/cocos2d/core/components/CCComponent.js index 46ec4fa32b..8046f2204a 100644 --- a/cocos2d/core/components/CCComponent.js +++ b/cocos2d/core/components/CCComponent.js @@ -458,7 +458,7 @@ var Component = cc.Class({ }, /** - * Returns the component of Type, type in the any of its children using depth first search. + * Returns the component of supplied type in any of its children using depth first search. * * @method getComponentInChildren * @param {Function|String} typeOrClassName @@ -469,7 +469,7 @@ var Component = cc.Class({ }, /** - * Returns all components of Type, type in the any of its children. + * Returns the components of supplied type in any of its children using depth first search. * * @method getComponentsInChildren * @param {Function|String} typeOrClassName diff --git a/test/qunit/unit/test-node.js b/test/qunit/unit/test-node.js index b610507eb7..4cc861a855 100644 --- a/test/qunit/unit/test-node.js +++ b/test/qunit/unit/test-node.js @@ -152,7 +152,7 @@ test('activation logic for component', function () { cc.js.unregisterClass(MyComponent, MyComponentBase); }); -test('get component related code', function () { +test('get self components', function () { var MyComponent = cc.Class({ name: 'MyComponent', @@ -161,16 +161,33 @@ test('get component related code', function () { var obj = new cc.Node("New Node"); cc.director.getScene().addChild(obj); - var comp1 = obj.addComponent(MyComponent); - var comp2 = obj.addComponent(MyComponent); - var comp3 = obj.addComponent(MyComponent); + obj.addComponent(MyComponent); + obj.addComponent(MyComponent); + obj.addComponent(MyComponent); + + ok(obj.getComponents(MyComponent).length === 3, 'getComponents: can get my component array'); + + cc.js.unregisterClass(MyComponent); +}); + +test('should not include self component', function () { + var MyComponent = cc.Class({ + name: 'MyComponent', + extends: cc.Component + }); + + var obj = new cc.Node("New Node"); + cc.director.getScene().addChild(obj); + obj.addComponent(MyComponent); + obj.addComponent(MyComponent); //-- layer 1 var obj1 = new cc.Node("New Node 1"); + obj1.addComponent(MyComponent); obj1.parent = obj; var obj2 = new cc.Node("New Node 4"); - var comp4 = obj2.addComponent(MyComponent); + obj2.addComponent(MyComponent); obj2.parent = obj; //-- layer 2 @@ -178,12 +195,8 @@ test('get component related code', function () { var comp5 = obj3.addComponent(MyComponent); obj3.parent = obj1; - var obj4 = new cc.Node("New Node 3"); - obj4.parent = obj1; - - ok(obj.getComponents(MyComponent).length === 3, 'getComponents: can get my component array'); ok(obj1.getComponentInChildren(MyComponent) === comp5, 'getComponentInChildren: can get my component in children'); - ok(obj.getComponentsInChildren(MyComponent).length === 2, 'getComponentsInChildren: can get my components in children'); + ok(obj.getComponentsInChildren(MyComponent).length === 3, 'getComponentsInChildren: can get my components in children'); cc.js.unregisterClass(MyComponent); }); From ea60080ee138a55d9c063c1e3b249635eda27f23 Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Tue, 1 Mar 2016 15:49:20 +0800 Subject: [PATCH 4/4] modify CCNode, tes-node --- cocos2d/core/CCNode.js | 9 ++++++--- test/qunit/unit/test-node.js | 37 ++++++++++++++++++++++-------------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 335eacb759..0ba1336e0d 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -279,8 +279,11 @@ function findChildComponent (children, constructor) { if (comp) { return comp; } - else if(node.children.length > 0) { - return findChildComponent(node.children, constructor); + else if (node.children.length > 0) { + comp = findChildComponent(node.children, constructor); + if (comp) { + return comp; + } } } return null; @@ -290,7 +293,7 @@ function findChildComponents (children, constructor, components) { for (var i = 0; i < children.length; ++i) { var node = children[i]; findComponents(node, constructor, components); - if (node._children.length > 0 ) { + if (node._children.length > 0) { findChildComponents(node._children, constructor, components); } } diff --git a/test/qunit/unit/test-node.js b/test/qunit/unit/test-node.js index 4cc861a855..3f36de982d 100644 --- a/test/qunit/unit/test-node.js +++ b/test/qunit/unit/test-node.js @@ -159,17 +159,36 @@ test('get self components', function () { extends: cc.Component }); - var obj = new cc.Node("New Node"); + var obj = new cc.Node("New Node 1"); cc.director.getScene().addChild(obj); obj.addComponent(MyComponent); obj.addComponent(MyComponent); obj.addComponent(MyComponent); + //-- layer 1 + var obj1 = new cc.Node("New Node 2"); + obj1.parent = obj; + + var obj2 = new cc.Node("New Node 3"); + var comp = obj2.addComponent(MyComponent); + obj2.parent = obj1; + + //-- layer 2 + var obj3 = new cc.Node("New Node 4"); + obj3.parent = obj2; + + var obj4 = new cc.Node("New Node 5"); + obj4.addComponent(MyComponent); + obj4.parent = obj3; + ok(obj.getComponents(MyComponent).length === 3, 'getComponents: can get my component array'); + ok(obj.getComponentInChildren(MyComponent) === comp, 'getComponentInChildren: can get my component in children'); + ok(obj.getComponentsInChildren(MyComponent).length === 2, 'getComponentsInChildren: can get my components in children'); cc.js.unregisterClass(MyComponent); }); + test('should not include self component', function () { var MyComponent = cc.Class({ name: 'MyComponent', @@ -180,23 +199,13 @@ test('should not include self component', function () { cc.director.getScene().addChild(obj); obj.addComponent(MyComponent); obj.addComponent(MyComponent); + obj.addComponent(MyComponent); - //-- layer 1 var obj1 = new cc.Node("New Node 1"); - obj1.addComponent(MyComponent); obj1.parent = obj; - var obj2 = new cc.Node("New Node 4"); - obj2.addComponent(MyComponent); - obj2.parent = obj; - - //-- layer 2 - var obj3 = new cc.Node("New Node 2"); - var comp5 = obj3.addComponent(MyComponent); - obj3.parent = obj1; - - ok(obj1.getComponentInChildren(MyComponent) === comp5, 'getComponentInChildren: can get my component in children'); - ok(obj.getComponentsInChildren(MyComponent).length === 3, 'getComponentsInChildren: can get my components in children'); + ok(obj.getComponentInChildren(MyComponent) === null, 'getComponentInChildren should not include self component'); + ok(obj.getComponentsInChildren(MyComponent).length === 0, 'getComponentsInChildren should not include self component'); cc.js.unregisterClass(MyComponent); });