PanelMgr.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import CacheMgr from "./CacheMgr";
  2. import LoadMgr from "./LoadMgr";
  3. import Emit from "./Emit/Emit";
  4. import {EventCode} from "./Emit/EmitData";
  5. import LayerPanel from "./Layer/LayerPanel";
  6. const {ccclass, property} = cc._decorator;
  7. @ccclass
  8. export default class PanelMgr extends cc.Component {
  9. public static INS: PanelMgr
  10. @property(
  11. {
  12. type: [cc.Node],
  13. tooltip: "只要将Game中的场景layer按照顺序赋值即可, 如果存在修改,需要到PannerMgr.ts中修改枚举变量 Layer,也是需要按照绑定顺序"
  14. }
  15. )
  16. public layers: cc.Node[] = []
  17. //当前正在Loading 的面板
  18. private LoadingList: Map<string, number> = new Map<string, number>()
  19. //当前打开的面板数组
  20. private openList: Map<string, cc.Node> = new Map<string, cc.Node>()
  21. //当前关闭但是未摧毁的面板,存储在这里,下次打开该面板的时候,就会使用这里的面板
  22. private hideList: Map<string, cc.Node> = new Map<string, cc.Node>()
  23. onLoad() {
  24. PanelMgr.INS = this
  25. console.log('PanelMgr初始化完成')
  26. Emit.instance().emit(EventCode.PanelMgrInitOK)
  27. }
  28. /**
  29. * @param param{
  30. * layer : 在哪一个容器打开页面
  31. * panel: 打开面板
  32. * call : 打开成功回调 可选
  33. * param: 传递给下一个面板的参数
  34. * }
  35. */
  36. openPanel(param: openParam) {
  37. let layer = this.layers[param.layer]
  38. console.log('param:',param,this.layers)
  39. if (!layer) {
  40. return
  41. }
  42. //加载分包
  43. let urlInfo = param.panel.getUrl()
  44. if (this.LoadingList.has(urlInfo.name)) {
  45. return;
  46. }
  47. if (this.openList.has(param.panel.getUrl().name)) {
  48. return;
  49. }
  50. this.LoadingList.set(urlInfo.name, 1) //添加一个加载标识, 防止重复添加
  51. //todo mask
  52. let openPanelWay = () => {
  53. let way = () => {
  54. let panel: cc.Node = null
  55. //判断有没有旧的panel可用,有的话就不重新实例化了
  56. if (this.hideList.has(urlInfo.name)) {
  57. panel = this.hideList.get(urlInfo.name)
  58. panel.parent = layer
  59. panel.active = false
  60. // this.scheduleOnce(() => {
  61. this.openList.set(urlInfo.name, panel)
  62. this.showPanel(panel, param.param)
  63. this.LoadingList.delete(urlInfo.name)
  64. if (this.LoadingList.size == 0) {
  65. //todo mask
  66. }
  67. if (param.call) {
  68. param.call()
  69. }
  70. // }, 0)
  71. } else {
  72. LoadMgr.loadPrefab(urlInfo.name, LoadMgr.getBundle(urlInfo.bundle)).then((prefab: cc.Prefab) => {
  73. panel = cc.instantiate(prefab)
  74. panel.parent = layer
  75. panel.active = false
  76. this.openList.set(urlInfo.name, panel)
  77. panel.getComponent(LayerPanel).initUI()
  78. this.showPanel(panel, param.param)
  79. this.LoadingList.delete(urlInfo.name)
  80. if (this.LoadingList.size == 0) {
  81. //todo mask
  82. }
  83. if (param.call) {
  84. param.call()
  85. }
  86. })
  87. }
  88. }
  89. if (LoadMgr.judgeBundleLoad(urlInfo.name)) {
  90. way()
  91. } else {
  92. LoadMgr.loadBundle_Single(urlInfo.bundle).then(() => {
  93. way()
  94. })
  95. }
  96. }
  97. //没有配置立即准备打开目标panel
  98. openPanelWay()
  99. }
  100. private showPanel(panel: cc.Node, param: any) {
  101. let script = panel.getComponent(LayerPanel)
  102. script.show(param)
  103. panel.active = true
  104. }
  105. /**
  106. *
  107. * @param panel 需要关闭的面板
  108. * @param destroy 是否需要彻底销毁这个面板
  109. */
  110. closePanel(panel: typeof LayerPanel, destroy = true) {
  111. let node = this.openList.get(panel.getUrl().name)
  112. if (!node) {
  113. return
  114. }
  115. node.getComponent(LayerPanel).hide() //这里可以做清除代码
  116. node.getComponent(LayerPanel).unscheduleAllCallbacks() //取消所有定时器
  117. if (panel.getUrl().name == "endView") { //如果是endView的化 ,需要同步数据
  118. console.log('zh:close endView ,需要同步数据')
  119. CacheMgr.updateData();
  120. }
  121. node.parent = null
  122. this.openList.delete(panel.getUrl().name)
  123. if (destroy) {
  124. node.getComponent(LayerPanel).onDestroyDo() //这里可以做清除代码
  125. node.destroy()
  126. } else {
  127. this.hideList.set(panel.getUrl().name, node)
  128. }
  129. }
  130. getPanel(panel: typeof LayerPanel): cc.Node {
  131. return this.openList.get(panel.getUrl().name)
  132. }
  133. }
  134. export enum Layer {
  135. gameLayer,
  136. gameInfoLayer,
  137. otherLayer,
  138. nativeLayer,
  139. }
  140. export enum View {
  141. endView,
  142. gameView,
  143. homeView,
  144. }
  145. export interface openParam {
  146. layer: Layer,
  147. panel: typeof LayerPanel,
  148. call?: Function,
  149. param?: any
  150. }