import PrefabManager from "../manager/PrefabManager"; import { Dictionary } from "../tools/General"; import BasePanel from "./BasePanel"; export default class UIMng { private static _ins: UIMng; public static get Ins(): UIMng { if (this._ins == null) { console.log("---------------------------------------------------UIMng"); this._ins = new UIMng(); } return this._ins; } private canvasNode: cc.Node; private get CanvasNode(): cc.Node { if (this.canvasNode == null) { this.canvasNode = cc.find("Canvas/Panel"); } return this.canvasNode; } private canvasNode0: cc.Node; private get CanvasNode0(): cc.Node { if (this.canvasNode0 == null) { this.canvasNode0 = cc.find("Canvas/Panel0"); } return this.canvasNode0; } private canvasNodeA: cc.Node; private get CanvasNodeA(): cc.Node { if (this.canvasNodeA == null) { this.canvasNodeA = cc.find("Canvas/PanelA"); } return this.canvasNodeA; } private panelDic: Dictionary; private panelPath: Dictionary; private id: number = 1; Destroy() { this.canvasNode = null; this.canvasNode0 = null; this.canvasNodeA = null; //if (this.panelDic) // this.panelDic.Clear(); this.panelDic = null; this.panelPath = null; console.log("UIMng Destroy::", this.panelDic + " ID:" + this.id); UIMng._ins = null; } constructor() { //return; this.id = (new Date).getSeconds(); this.panelPath = new Dictionary(); let index: number = 0; for (var item in PanelType) { let isValueProperty = parseInt(item, 10) >= 0; if (isValueProperty) { index++; this.panelPath.Add(PanelType[item.toString()], "ui/" + PanelType[item]); } } this.CanvasNode; console.log("Keys:" + this.panelPath.GetKey()); } /**是否存在页面*/ IsExistPanel(panelType: PanelType) { if (this.panelDic == null) return false; return this.panelDic.ContainKey(panelType); } /** * 获取UI页面 * @param panelType 页面类型 * @param layer 层级 1 Panel层 0 Panel0层(更内一层) 2 PanelA 最高级层 */ GetPanel(panelType: PanelType, layer: number = 1): BasePanel { if (this.panelDic == null) { this.panelDic = new Dictionary(); } let basePanel = this.panelDic.TryGetValue(panelType); if (basePanel == null) { let panel = PrefabManager.LoadPanel(this.panelPath.Item(PanelType[panelType.toString()])); if (layer == 1) this.CanvasNode.addChild(panel); else if (layer == 0) this.CanvasNode0.addChild(panel); else if (layer == 2) this.CanvasNodeA.addChild(panel); basePanel = panel.getComponent(BasePanel); this.panelDic.Add(panelType, basePanel); } return basePanel; } /**动态获取界面 * @param panelType 页面类型 * @param layer 层级 1 Panel层 0 Panel0层(更内一层) 2 PanelA 最高级层 * @param cb 加载完成回调 */ AsyncGetPanel(panelType: PanelType, cb: (panel) => void, layer: number = 1) { if (this.panelDic == null) { this.panelDic = new Dictionary(); } let basePanel = this.panelDic.TryGetValue(panelType); //console.log("UIMng Destroy::", this.panelDic + " ID:" + this.id); if (basePanel == null) { PrefabManager.LoadPanelAsync(this.panelPath.Item(PanelType[panelType.toString()]) , (panel) => { if (layer == 1) this.CanvasNode.addChild(panel); else if (layer == 0) this.CanvasNode0.addChild(panel); else if (layer == 2) this.CanvasNodeA.addChild(panel); //panel.setParent(this.CanvasNode); console.log("ui : "+PanelType[panelType.toString()]); basePanel = panel.getComponent(BasePanel); this.panelDic.Add(panelType, basePanel); cb(basePanel); }) } else { cb(basePanel); } } OnEnter(panelType, param?) { this.GetPanel(panelType).OnEnter(param); } OnExit(panelType, param?) { this.GetPanel(panelType).OnExit(param); } } export enum PanelType { TestPanel, FightRolePanel, FightRoleBPanel, FightPanel, FightResultPanel, MateUnlockPanel, MatePanel, FightUpPanel, WealthPanel, RichPanel, RichPropPanel, RichBookPanel, RichHelpPanel, RichCollectPanel, RichRewardPanel, AdRbPanel, CashProPanel, RedCodePanel, RedCodeListPanel, }