mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
support requireComponent attribute
This commit is contained in:
+30
-5
@@ -302,6 +302,14 @@ var Node = cc.Class({
|
||||
return null;
|
||||
}
|
||||
}
|
||||
var ReqComp = constructor._requireComponent;
|
||||
if (ReqComp && !this.getComponent(ReqComp)) {
|
||||
var depended = this.addComponent(ReqComp);
|
||||
if (!depended) {
|
||||
// depend conflicts
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@@ -335,12 +343,20 @@ var Node = cc.Class({
|
||||
return cc.error("_addComponentAt: Index out of range");
|
||||
}
|
||||
|
||||
// recheck attributes because script may changed
|
||||
var ctor = comp.constructor;
|
||||
if (ctor._disallowMultiple) {
|
||||
if (!this._checkMultipleComp(ctor)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (ctor._requireComponent) {
|
||||
var depend = this.addComponent(ctor._requireComponent);
|
||||
if (!depend) {
|
||||
// depend conflicts
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
comp.node = this;
|
||||
this._components.splice(index, 0, comp);
|
||||
@@ -361,7 +377,7 @@ var Node = cc.Class({
|
||||
removeComponent: function (component) {
|
||||
if ( !component ) {
|
||||
cc.error('removeComponent: Component must be non-nil');
|
||||
return null;
|
||||
return;
|
||||
}
|
||||
if (typeof component !== 'object') {
|
||||
component = this.getComponent(component);
|
||||
@@ -372,14 +388,22 @@ var Node = cc.Class({
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes all components of cc.ENode.
|
||||
* @method removeAllComponents
|
||||
* @method _getDependComponent
|
||||
* @param {cc.Component} depended
|
||||
* @return {cc.Component}
|
||||
* @private
|
||||
*/
|
||||
removeAllComponents: function () {
|
||||
_getDependComponent: CC_EDITOR && function (depended) {
|
||||
for (var i = 0; i < this._components.length; i++) {
|
||||
var comp = this._components[i];
|
||||
comp.destroy();
|
||||
if (comp !== depended && comp.isValid && !cc.Object._willDestroy(comp)) {
|
||||
var depend = comp.constructor._requireComponent;
|
||||
if (depend && depended instanceof depend) {
|
||||
return comp;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
// do remove component, only used internally
|
||||
@@ -388,6 +412,7 @@ var Node = cc.Class({
|
||||
cc.error('Argument must be non-nil');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(this._objFlags & Destroying)) {
|
||||
var i = this._components.indexOf(component);
|
||||
if (i !== -1) {
|
||||
|
||||
@@ -476,6 +476,13 @@ var Component = cc.Class({
|
||||
// OVERRIDES
|
||||
|
||||
destroy: function () {
|
||||
if (CC_EDITOR) {
|
||||
var depend = this.node._getDependComponent(this);
|
||||
if (depend) {
|
||||
return cc.error("Can't remove '%s' because '%s' depends on it.",
|
||||
cc.js.getClassName(this), cc.js.getClassName(depend));
|
||||
}
|
||||
}
|
||||
if (this._super()) {
|
||||
if (this._enabled && this.node._activeInHierarchy) {
|
||||
callOnEnable(this, false);
|
||||
@@ -553,7 +560,19 @@ var Component = cc.Class({
|
||||
}
|
||||
});
|
||||
|
||||
if (CC_EDITOR || CC_TEST) {
|
||||
/**
|
||||
* Automatically add required component as a dependency.
|
||||
*
|
||||
* @property _requireComponent
|
||||
* @type {Function}
|
||||
* @default null
|
||||
* @static
|
||||
* @readonly
|
||||
* @private
|
||||
*/
|
||||
Component._requireComponent = null;
|
||||
|
||||
if (CC_DEV) {
|
||||
|
||||
// INHERITABLE STATIC MEMBERS
|
||||
|
||||
@@ -634,10 +653,16 @@ if (CC_EDITOR || CC_TEST) {
|
||||
priority: priority
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
// use defineProperty to prevent inherited by sub classes
|
||||
Object.defineProperty(Component, '_registerEditorProps', {
|
||||
value: function (cls, props) {
|
||||
// use defineProperty to prevent inherited by sub classes
|
||||
Object.defineProperty(Component, '_registerEditorProps', {
|
||||
value: function (cls, props) {
|
||||
var reqComp = props.requireComponent;
|
||||
if (reqComp) {
|
||||
cls._requireComponent = reqComp;
|
||||
}
|
||||
if (CC_DEV) {
|
||||
var name = cc.js.getClassName(cls);
|
||||
for (var key in props) {
|
||||
var val = props[key];
|
||||
@@ -677,12 +702,8 @@ if (CC_EDITOR || CC_TEST) {
|
||||
cls._disallowMultiple = cls;
|
||||
break;
|
||||
|
||||
// {Number} menuPriority
|
||||
// the order which the menu item are displayed
|
||||
case 'menuPriority':
|
||||
if (!props.menu) {
|
||||
cc.warn('The editor property "menuPriority" should be used with "menu" in class "%s".', name);
|
||||
}
|
||||
case 'requireComponent':
|
||||
// skip here
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -691,8 +712,8 @@ if (CC_EDITOR || CC_TEST) {
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Component.prototype.__scriptUuid = '';
|
||||
|
||||
|
||||
@@ -401,6 +401,7 @@ if (CC_DEV) {
|
||||
nodeToWorldTransform: 'getNodeToWorlshaderProgramdTransform',
|
||||
worldToNodeTransform: 'getWorldToNodeTransform',
|
||||
nodeToParentTransform: 'getNodeToParentTransform',
|
||||
removeAllComponents: 'removeComponent',
|
||||
getNodeToParentAffineTransform: 'getNodeToParentTransform',
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user