UNPKG

@senspark/ee

Version:

utility library for cocos creator

272 lines (271 loc) 9.77 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); var assert = require("assert"); var UnselectableComponent_1 = require("./UnselectableComponent"); var _a = cc._decorator, ccclass = _a.ccclass, disallowMultiple = _a.disallowMultiple, executeInEditMode = _a.executeInEditMode, menu = _a.menu, property = _a.property; /** Decorator for nested prefabs. */ exports.nest = function (type) { if (type instanceof Array) { return exports.nestArray(type[0]); } else { return exports.nestSingle(type); } }; exports.nestSingle = function (type) { return function (target, propertyKey) { var internalProperty = propertyKey + "_prefab"; // Prefixed with a dash. // Let cocos handle with the internal property. property({ type: NestedPrefab, displayName: propertyKey, visible: true, })(target, internalProperty, { /** Default value is null */ initializer: function () { return null; }, }); Object.defineProperty(target, propertyKey, { set: function (value) { // No effect. }, get: function () { var prefab = this[internalProperty]; if (prefab === undefined) { return null; } var view = prefab.getView(); if (view === null) { return null; } var component = view.getComponent(type); assert(component !== null); return component; }, configurable: false, enumerable: true, }); }; }; exports.nestArray = function (type) { return function (target, propertyKey) { var internalProperty = propertyKey + "_prefab"; property({ type: [NestedPrefab], displayName: propertyKey, visible: true, })(target, internalProperty, { /** Default value is an empty array. */ initializer: function () { return []; }, }); Object.defineProperty(target, propertyKey, { set: function (value) { // No effect. }, get: function () { var prefabs = this[internalProperty]; if (prefabs === undefined) { return null; } var views = prefabs.map(function (item) { return item.getView(); }); var components = views.map(function (item) { return item === null ? null : item.getComponent(type); }); return components; }, configurable: false, enumerable: true, }); }; }; var NestedPrefab = /** @class */ (function (_super) { __extends(NestedPrefab, _super); function NestedPrefab() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.instantiated = false; _this.view = null; _this._prefab = null; _this.instantiate = true; _this.synchronize = false; return _this; } NestedPrefab_1 = NestedPrefab; NestedPrefab.createNode = function (prefab) { var node = new cc.Node(); var comp = node.addComponent(NestedPrefab_1); comp.prefab = prefab; node.name = prefab.name; var view = comp.getView(); if (view === null) { return null; } node.setContentSize(view.getContentSize()); node.setAnchorPoint(view.getAnchorPoint()); return node; }; Object.defineProperty(NestedPrefab.prototype, "prefab", { get: function () { return this._prefab; }, set: function (value) { if (this._prefab !== null) { if (this.view === null) { if (CC_EDITOR) { throw new Error('Prefab exist but the view is not present.'); } } else { this.instantiated = false; this.view.destroy(); this.view = null; } } this._prefab = value; if (this._prefab !== null) { if (this.instantiateView()) { this.instantiated = true; this.setupView(); } } }, enumerable: true, configurable: true }); NestedPrefab.prototype.onLoad = function () { if (!CC_EDITOR && !this.instantiated && this.instantiate) { if (this.instantiateView()) { this.instantiated = true; this.applySync(); } } }; NestedPrefab.prototype.update = function () { if (!CC_EDITOR) { if (this.instantiate) { assert(this.view !== null, 'View should be present at runtime.'); } return; } if (this.view !== null) { if (!this.view.isValid) { // Object is destroyed but remains referenced. this.view = null; } } if (this.node.childrenCount > 0 && this.node.children[0] !== this.view) { // This node was duplicated. this.node.removeAllChildren(); } if (this.prefab !== null && this.view === null) { if (this.instantiateView()) { this.instantiated = true; this.setupView(); } } this.applySync(); }; /** Creates a view using the current prefab. */ NestedPrefab.prototype.createView = function () { assert(this.prefab !== null); var view = cc.instantiate(this.prefab); return view; }; NestedPrefab.prototype.getView = function () { if (!this.instantiate) { return null; } if (this.prefab === null) { return null; } if (!this.instantiated) { if (this.instantiateView()) { this.instantiated = true; this.applySync(); if (CC_EDITOR) { this.setupView(); } } } return this.view; }; /** Creates a view from the current prefab and add it to this. */ NestedPrefab.prototype.instantiateView = function () { if (this.view !== null) { assert.fail('View already instantiate.'); return false; } if (this.prefab === null) { assert.fail('Prefab is not exist.'); return false; } this.view = this.createView(); this.node.addChild(this.view); return true; }; NestedPrefab.prototype.setupView = function () { if (this.view === null) { assert.fail('View is not present'); return false; } this.freeze(this.view); this.view._objFlags |= cc.Object.Flags.DontSave; return true; }; NestedPrefab.prototype.freeze = function (node) { var _this = this; if (node.getComponent(UnselectableComponent_1.UnselectableComponent) === null) { node.addComponent(UnselectableComponent_1.UnselectableComponent); } var prefab = node.getComponent(NestedPrefab_1); if (prefab !== null) { if (!prefab.instantiate) { node.active = false; } } node.children.forEach(function (child) { return _this.freeze(child); }); }; NestedPrefab.prototype.applySync = function () { var view = this.getView(); if (view !== null && this.synchronize) { this.node.setContentSize(view.getContentSize()); this.node.setAnchorPoint(view.getAnchorPoint()); } }; var NestedPrefab_1; __decorate([ property(cc.Prefab) ], NestedPrefab.prototype, "_prefab", void 0); __decorate([ property({ type: cc.Prefab }) ], NestedPrefab.prototype, "prefab", null); __decorate([ property(cc.Boolean) ], NestedPrefab.prototype, "instantiate", void 0); __decorate([ property(cc.Boolean) ], NestedPrefab.prototype, "synchronize", void 0); NestedPrefab = NestedPrefab_1 = __decorate([ ccclass, disallowMultiple, executeInEditMode, menu('ee/NestedPrefab') ], NestedPrefab); return NestedPrefab; }(cc.Component)); exports.NestedPrefab = NestedPrefab;