import { GameUIComponent } from './GameUIComponent'; import { Global } from '../../../utils/Global'; export class GameUIObject { name: string; UID: number; data: els.GameObject; private _components: GameUIComponent[]; private _children: GameUIObject[]; private _parent: GameUIObject; private _numChildren: number = 0; private _componentHash: any = {}; private _transform: els.Transform; constructor() { this._children = []; this._components = []; this._transform = new els.Transform(); } get transform() { return this._transform; } get components() { return this._components; } getProperty(componentName: string, propName: string) { if (!propName) return null; let uiComponents: GameUIComponent[] = this.getComponents(componentName); let uiComponent: GameUIComponent = uiComponents ? uiComponents[0] : null; if (uiComponent) { return uiComponent.propertys[propName]; } return null; } get parent() { return this._parent; } get numChildren() { return this._numChildren; } get children() { return this._children; } contains(child: GameUIObject): boolean{ if (!child) return false; for (var i: number = 0, l: number = this._children.length; i < l; i++){ var _child: GameUIObject = this._children[i]; if (_child.UID === child.UID) return true; } return false; } getChildByName(name: string): GameUIObject[]{ let list: GameUIObject[] = []; for (let i: number = 0, l: number = this.children.length; i < l; i++){ if (this.children[i].name === name) list.push(this.children[i]); } return list; } getChildAt(index: number): GameUIObject{ if (index < 0) return null; return this._children[index]; } getChildIndex(child: GameUIObject): number{ if (!child) return -1; return this._children.indexOf(child); } addChild(child: GameUIObject) { if (!child) return; if (!this.contains(child)) { this._children.push(child); this._numChildren++; } child._parent = this; } removeChild(child: GameUIObject): void{ if (!child) return; var index: number = this._children.indexOf(child); if (index !== -1) { this._children.splice(index, 1); child._parent = null; this._numChildren--; } } removeFromParent(): void{ if (this.parent) { this.parent.removeChild(this); } } addChildAt(child: GameUIObject, index: number): GameUIObject{ if (!child) return null; if (!this.contains(child)) { child._parent = this; this._children.splice(index, 0, child); this._numChildren++; } return child; } removeChildren(): void{ for (var i: number = 0, l: number = this._children.length; i < l; i++){ this._children[i]._parent = null; } this._children.length = 0; this._numChildren = 0; } addComponent(com: GameUIComponent) { if (!com) return; this._components.push(com); } getComponents(name: string): GameUIComponent[] { if (!name || !this.components) return null; var _components: GameUIComponent[] = []; for (var i: number = 0, l: number = this.components.length; i < l; i++) { var component: GameUIComponent = this.components[i]; if (component.name === name) _components.push(component); } return _components; } removeComponent(name: string): void { if (!name || !this.components) return; for (var i: number = this.components.length - 1; i >= 0; i--) { var component: GameUIComponent = this.components[i]; if (component.name === name) { this.components.splice(i, 1); } } } static createInstance(): GameUIObject{ let maxNums: number = Global.currentProject.maxUID; maxNums++; let uiGameObject: GameUIObject = new GameUIObject(); uiGameObject.UID = maxNums; uiGameObject.name = "GameObject"; return uiGameObject; } clone(): GameUIObject{ let gameuiobject: GameUIObject = new GameUIObject(); if (this.transform) Object.assign(gameuiobject.transform, this.transform); let maxNums: number = Global.currentProject.maxUID; maxNums++; gameuiobject.UID = maxNums; gameuiobject.name = this.name; if (this.data) { gameuiobject.data = this.data.clone() as els.GameObject; } //组件 if (this.components) { for (let i: number = 0, l: number = this.components.length; i < l; i++){ let uiComponent: GameUIComponent = this.components[i]; uiComponent.uiGameobject = gameuiobject; gameuiobject.addComponent(uiComponent.clone()); } } //添加子对象 if (this.children) { for (let i: number = 0, l: number = this.children.length; i < l; i++){ let subgameuiobject: GameUIObject = this.children[i]; gameuiobject.addChild(subgameuiobject.clone()); } } return gameuiobject; } }