mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-05 11:02:40 +08:00
Merge pull request #402 from knoxHuang/master
Improve access to relevant API Component of
This commit is contained in:
+108
-18
@@ -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 i = 0; i < node._components.length; ++i) {
|
||||
var comp = node._components[i];
|
||||
if (comp instanceof constructor) {
|
||||
return comp;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function findComponents (node, constructor, components) {
|
||||
for (var i = 0; i < node._components.length; ++i) {
|
||||
var comp = node._components[i];
|
||||
if (comp instanceof constructor) {
|
||||
components.push(comp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function findChildComponent (children, constructor) {
|
||||
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) {
|
||||
comp = findChildComponent(node.children, constructor);
|
||||
if (comp) {
|
||||
return comp;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
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) {
|
||||
findChildComponents(node._children, constructor, components);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 supplied type in 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 the components of supplied type in any of its children using depth first search.
|
||||
*
|
||||
* @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
|
||||
|
||||
@@ -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 supplied type in 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 the components of supplied type in any of its children using depth first search.
|
||||
*
|
||||
* @method getComponentsInChildren
|
||||
* @param {Function|String} typeOrClassName
|
||||
* @returns {Component[]}
|
||||
*/
|
||||
getComponentsInChildren: function (typeOrClassName) {
|
||||
return this.node.getComponentsInChildren(typeOrClassName);
|
||||
},
|
||||
|
||||
///**
|
||||
|
||||
@@ -152,6 +152,64 @@ test('activation logic for component', function () {
|
||||
cc.js.unregisterClass(MyComponent, MyComponentBase);
|
||||
});
|
||||
|
||||
test('get self components', function () {
|
||||
|
||||
var MyComponent = cc.Class({
|
||||
name: 'MyComponent',
|
||||
extends: cc.Component
|
||||
});
|
||||
|
||||
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',
|
||||
extends: cc.Component
|
||||
});
|
||||
|
||||
var obj = new cc.Node("New Node");
|
||||
cc.director.getScene().addChild(obj);
|
||||
obj.addComponent(MyComponent);
|
||||
obj.addComponent(MyComponent);
|
||||
obj.addComponent(MyComponent);
|
||||
|
||||
var obj1 = new cc.Node("New Node 1");
|
||||
obj1.parent = obj;
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
test('life cycle logic for component', function () {
|
||||
// my component
|
||||
var MyComponent = cc.Class({
|
||||
|
||||
Reference in New Issue
Block a user