UIMng.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import PrefabManager from "../manager/PrefabManager";
  2. import { Dictionary } from "../tools/General";
  3. import BasePanel from "./BasePanel";
  4. export default class UIMng {
  5. private static _ins: UIMng;
  6. public static get Ins(): UIMng {
  7. if (this._ins == null) {
  8. console.log("---------------------------------------------------UIMng");
  9. this._ins = new UIMng();
  10. }
  11. return this._ins;
  12. }
  13. private canvasNode: cc.Node;
  14. private get CanvasNode(): cc.Node {
  15. if (this.canvasNode == null) {
  16. this.canvasNode = cc.find("Canvas/Panel");
  17. }
  18. return this.canvasNode;
  19. }
  20. private canvasNode0: cc.Node;
  21. private get CanvasNode0(): cc.Node {
  22. if (this.canvasNode0 == null) {
  23. this.canvasNode0 = cc.find("Canvas/Panel0");
  24. }
  25. return this.canvasNode0;
  26. }
  27. private canvasNodeA: cc.Node;
  28. private get CanvasNodeA(): cc.Node {
  29. if (this.canvasNodeA == null) {
  30. this.canvasNodeA = cc.find("Canvas/PanelA");
  31. }
  32. return this.canvasNodeA;
  33. }
  34. private panelDic: Dictionary<PanelType, BasePanel>;
  35. private panelPath: Dictionary<PanelType, string>;
  36. private id: number = 1;
  37. Destroy() {
  38. this.canvasNode = null;
  39. this.canvasNode0 = null;
  40. this.canvasNodeA = null;
  41. //if (this.panelDic)
  42. // this.panelDic.Clear();
  43. this.panelDic = null;
  44. this.panelPath = null;
  45. console.log("UIMng Destroy::", this.panelDic + " ID:" + this.id);
  46. UIMng._ins = null;
  47. }
  48. constructor() {
  49. //return;
  50. this.id = (new Date).getSeconds();
  51. this.panelPath = new Dictionary();
  52. let index: number = 0;
  53. for (var item in PanelType) {
  54. let isValueProperty = parseInt(item, 10) >= 0;
  55. if (isValueProperty) {
  56. index++;
  57. this.panelPath.Add(PanelType[item.toString()], "ui/" + PanelType[item]);
  58. }
  59. }
  60. this.CanvasNode;
  61. console.log("Keys:" + this.panelPath.GetKey());
  62. }
  63. /**是否存在页面*/
  64. IsExistPanel(panelType: PanelType) {
  65. if (this.panelDic == null)
  66. return false;
  67. return this.panelDic.ContainKey(panelType);
  68. }
  69. /**
  70. * 获取UI页面
  71. * @param panelType 页面类型
  72. * @param layer 层级 1 Panel层 0 Panel0层(更内一层) 2 PanelA 最高级层
  73. */
  74. GetPanel(panelType: PanelType, layer: number = 1): BasePanel {
  75. if (this.panelDic == null) {
  76. this.panelDic = new Dictionary<PanelType, BasePanel>();
  77. }
  78. let basePanel = this.panelDic.TryGetValue(panelType);
  79. if (basePanel == null) {
  80. let panel = PrefabManager.LoadPanel(this.panelPath.Item(PanelType[panelType.toString()]));
  81. if (layer == 1)
  82. this.CanvasNode.addChild(panel);
  83. else if (layer == 0)
  84. this.CanvasNode0.addChild(panel);
  85. else if (layer == 2)
  86. this.CanvasNodeA.addChild(panel);
  87. basePanel = panel.getComponent(BasePanel);
  88. this.panelDic.Add(panelType, basePanel);
  89. }
  90. return basePanel;
  91. }
  92. /**动态获取界面
  93. * @param panelType 页面类型
  94. * @param layer 层级 1 Panel层 0 Panel0层(更内一层) 2 PanelA 最高级层
  95. * @param cb 加载完成回调
  96. */
  97. AsyncGetPanel(panelType: PanelType, cb: (panel) => void, layer: number = 1) {
  98. if (this.panelDic == null) {
  99. this.panelDic = new Dictionary<PanelType, BasePanel>();
  100. }
  101. let basePanel = this.panelDic.TryGetValue(panelType);
  102. //console.log("UIMng Destroy::", this.panelDic + " ID:" + this.id);
  103. if (basePanel == null) {
  104. PrefabManager.LoadPanelAsync(this.panelPath.Item(PanelType[panelType.toString()])
  105. , (panel) => {
  106. if (layer == 1)
  107. this.CanvasNode.addChild(panel);
  108. else if (layer == 0)
  109. this.CanvasNode0.addChild(panel);
  110. else if (layer == 2)
  111. this.CanvasNodeA.addChild(panel);
  112. //panel.setParent(this.CanvasNode);
  113. console.log("ui : "+PanelType[panelType.toString()]);
  114. basePanel = panel.getComponent(BasePanel);
  115. this.panelDic.Add(panelType, basePanel);
  116. cb(basePanel);
  117. })
  118. } else {
  119. cb(basePanel);
  120. }
  121. }
  122. OnEnter(panelType, param?) {
  123. this.GetPanel(panelType).OnEnter(param);
  124. }
  125. OnExit(panelType, param?) {
  126. this.GetPanel(panelType).OnExit(param);
  127. }
  128. }
  129. export enum PanelType {
  130. TestPanel,
  131. FightRolePanel,
  132. FightRoleBPanel,
  133. FightPanel,
  134. FightResultPanel,
  135. MateUnlockPanel,
  136. MatePanel,
  137. FightUpPanel,
  138. WealthPanel,
  139. RichPanel,
  140. RichPropPanel,
  141. RichBookPanel,
  142. RichHelpPanel,
  143. RichCollectPanel,
  144. RichRewardPanel,
  145. AdRbPanel,
  146. CashProPanel,
  147. RedCodePanel,
  148. RedCodeListPanel,
  149. }