import { ResType, WindowName } from "../Global"; import { UIBase } from "./UIBase"; export class UIManager { private static _ins: UIManager = null; static get ins(): UIManager { if (this._ins == null) { this._ins = new UIManager(); } return this._ins; } private _uiRoot: cc.Node; private _uiLayer: cc.Node; get uiLayer(): cc.Node { return this._uiLayer; } private _windowLockLayer: cc.Node; private _windowLayer: cc.Node; private _widgetLayer: cc.Node; get widgetLayer(): cc.Node { return this._widgetLayer; } private _loadingLayer: cc.Node; get loadingLayer(): cc.Node { return this._loadingLayer; } // private _loadingNode: Node; private _tipsLayer: cc.Node; private _adLayer: cc.Node; get adLayer(): cc.Node { return this._adLayer; } private _cache: Map = new Map(); get cache(): Map { return this._cache; } private _adRoot: cc.Node; // setLoadingNode(node: Node): void { // this._loadingNode = node; // } // setAdNode(node: cc.Node) { // this._adRoot = node; // } init(uiRoot: cc.Node): void { //已经进入了GameMain场景 this._uiRoot = uiRoot; this._uiLayer = this._uiRoot.getChildByName("uiLayer"); this._windowLockLayer = this._uiRoot.getChildByName("windowBlcok"); this._windowLockLayer.active = false; // this._windowLockLayer.getComponent(Widget).updateAlignment(); this._windowLayer = this._uiRoot.getChildByName("windowLayer"); this._widgetLayer = this._uiRoot.getChildByName("widgetLayer"); this._loadingLayer = this._uiRoot.getChildByName("loadingLayer"); this._loadingLayer.active = false; this._tipsLayer = this._uiRoot.getChildByName("tipsLayer"); this._adLayer = this._uiRoot.getChildByName("adLayer"); // if (this._loadingNode) { // // this._loadingNode.removeFromParent(); // this._loadingLayer.addChild(this._loadingNode); // this._loadingNode.getChildByName("type1").active = false; // this._loadingNode.getChildByName("type2").active = true; // this._loadingNode.active = false; // } // uiRoot.addChild(this._adRoot);//添加广告节点 } // static get adRoot() { // let _scene: any = cc.director.getScene(); // let _adRoot = _scene.getChildByName('Canvas').getChildByName('adLayer') as cc.Node; // if (_adRoot == null) { // _adRoot = new cc.Node(); // _adRoot.name = 'adLayer'; // _adRoot.parent = _scene.getChildByName('Canvas'); // _adRoot.x = 0; // _adRoot.y = 0; // _adRoot.width = cc.winSize.width; // _adRoot.height = cc.winSize.height; // _adRoot.setSiblingIndex(999); // } // return _adRoot; // } openScene(name: string) { this._loadingLayer.active = true; this._loadingLayer.getChildByName('sceneNode').active = true; this._loadingLayer.getChildByName('windowNode').active = false; cc.resources.loadScene('scenes/' + name, (finish: number, total: number) => { let prg = finish / total; this._loadingLayer.getChildByName('sceneNode').getComponentInChildren(cc.ProgressBar).progress = prg; }, (error, scene: cc.SceneAsset) => { this._loadingLayer.active = false; cc.director.runScene(scene); }); // cc.director.scene } /** * * @param type * @param callBack 参数 view:cc.Node */ loadWindow(type: WindowName, callBack: Function) { if (this._cache.has(type)) { callBack(this._cache.get(type)); } else { this._loadingLayer.active = true; this._loadingLayer.getChildByName('sceneNode').active = false; this._loadingLayer.getChildByName('windowNode').active = true; cc.resources.load(type, cc.Prefab, (error, prefab: cc.Prefab) => { this._loadingLayer.active = false; if (!error) { if (!this._cache.has(type)) { let view = cc.instantiate(prefab); this._cache.set(type, view); callBack(view); } } }); } } openWindow(type: WindowName, openParam: any = null): void { if (this._cache.has(type)) { this.doOpenWindow(this._cache.get(type), openParam); } else { // this._loadingLayer.active = true; // this._loadingLayer.getChildByName('sceneNode').active = false; // this._loadingLayer.getChildByName('windowNode').active = true; // cc.resources.load(type, cc.Prefab, // (finished, total) => { // let prg = finished / total; // }, // (error, prefab: cc.Prefab) => { // //finish // if (!this._cache.has(type)) { // let view = cc.instantiate(prefab); // this._cache.set(type, view); // this._loadingLayer.active = false; // this.doOpenWindow(view, openParam); // } // }); this.loadWindow(type, (view: cc.Node) => { this.doOpenWindow(view, openParam); }) } } private doOpenWindow(view: cc.Node, param: any): void { if (!view.parent) { !this._windowLockLayer.active && (this._windowLockLayer.active = true); let base = view.getComponent(UIBase); base && (base.enableParam = param); this._windowLayer.addChild(view); view.active = true; base && base.onPopUpEffect(); } } closeWindow(type: WindowName, callback: Function = null): void { if (this._cache.has(type)) { let view = this._cache.get(type); // view.active = false; let base = view.getComponent(UIBase); base && base.onBeforeClose(); base.onCloseEffect(() => { let isOnWindowLayer = (view.isValid && view.parent === this._windowLayer) if (isOnWindowLayer) { view.removeFromParent(); view.active = false; } if (isOnWindowLayer && this._windowLayer.children.length == 0) { this._windowLockLayer.active = false; } callback && callback(); }, this) } } getWidgetNode(type: ResType): cc.Node { return this._widgetLayer.getChildByName('widget' + type.toString()); } showTips(str: string): void { // let node = this._tipsLayer.getChildByName('TipsNode'); // node.active = true; // node.getComponentInChildren(Label).string = str; // Tween.stopAllByTarget(node); // tween(node).delay(3).call(this.closeTips.bind(this)).start() } private closeTips(): void { let node = this._tipsLayer.getChildByName('TipsNode'); node.active = false; } // playResFlyEffect }