UIMgr.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { _decorator, Component, instantiate, isValid, Node, Prefab } from 'cc'
  2. import { Debug } from '../util/Debug'
  3. import { UIType } from '../enum/UIType'
  4. import { EventMgr } from './EventMgr'
  5. import { EventType } from '../enum/EventType'
  6. import { ResMgr } from './ResMgr'
  7. import { UIBase } from '../scriptBase/UIBase'
  8. import { Bundle } from '../enum/Bundle'
  9. import { DtoUI } from '../dto/DtoUI'
  10. const { ccclass, property } = _decorator
  11. const Tag: string = 'UIMgr'
  12. @ccclass('Manager/UIMgr')
  13. export class UIMgr extends Component {
  14. private static _inst: UIMgr = null
  15. private static _node: Node = null
  16. @property(Node)
  17. private popUpMask: Node = null
  18. private static _openedUI: Object = {}
  19. private static _popUpStack: string[] = []
  20. private static _toast: Node = null
  21. protected onLoad(): void {
  22. globalThis.UIMgr = UIMgr
  23. UIMgr._inst = this
  24. UIMgr._node = this.node
  25. UIMgr.init()
  26. }
  27. private static init(): void {
  28. EventMgr.on(EventType.OpenUI, this.open, this)
  29. EventMgr.on(EventType.CloseUI, this.close, this)
  30. }
  31. public static async open(uiName: string, data?: unknown, dtoUI?: DtoUI) {
  32. if (this.isOpened(uiName)) {
  33. Debug.Warn(Tag, `${uiName}已经打开`)
  34. return
  35. }
  36. let uiBundle: Bundle = dtoUI?.uiBundle ?? Bundle.UI
  37. let isUILoaded: boolean = ResMgr.isLoaded(uiBundle, uiName)
  38. if (!isUILoaded) {
  39. await ResMgr.loadRes(uiBundle, uiName)
  40. }
  41. let uiPrefab: Prefab = ResMgr.getRes(uiBundle, uiName)
  42. if (!uiPrefab) {
  43. Debug.Error(Tag, `${uiName}不存在`)
  44. return
  45. }
  46. let uiNode: Node = instantiate(uiPrefab)
  47. let ui: UIBase = uiNode.getComponent(UIBase)
  48. Debug.Log(Tag, `打开${uiName}`)
  49. let uiType: UIType = dtoUI?.uiType ?? ui.uiType
  50. let uiParent: Node = this._node.children[uiType]
  51. uiParent.addChild(uiNode)
  52. ui.onOpen(data)
  53. if (uiType === UIType.PopUp) {
  54. let idx: number = uiNode.getSiblingIndex()
  55. this._inst.popUpMask.active = true
  56. this._inst.popUpMask.setSiblingIndex(idx - 1)
  57. this._popUpStack.push(uiName)
  58. }
  59. if (uiType === UIType.Toast) {
  60. isValid(this._toast) && this._toast.destroy()
  61. this._toast = uiNode
  62. } else {
  63. this._openedUI[uiName] = ui
  64. }
  65. }
  66. public static close(uiName: string, data?: unknown): void {
  67. if (!this.isOpened(uiName)) return
  68. let ui: UIBase = this._openedUI[uiName]
  69. Debug.Log(Tag, `关闭${uiName}`)
  70. let uiType: UIType = ui.uiType
  71. if (uiType === UIType.PopUp) {
  72. let idx: number = this._popUpStack.indexOf(uiName)
  73. this._popUpStack.splice(idx, 1)
  74. if (this._popUpStack.length <= 0) {
  75. this._inst.popUpMask.active = false
  76. } else {
  77. let topWindowName: string = this._popUpStack[this._popUpStack.length - 1]
  78. let topWindow: UIBase = this._openedUI[topWindowName]
  79. let topWindowIdx: number = topWindow.node.getSiblingIndex()
  80. this._inst.popUpMask.setSiblingIndex(Math.max(0, topWindowIdx))
  81. }
  82. }
  83. ui.onClose(data)
  84. delete this._openedUI[uiName]
  85. ui.node.destroy()
  86. }
  87. public static get(uiName: string): UIBase {
  88. return this._openedUI[uiName]
  89. }
  90. public static isOpened(uiName: string): boolean {
  91. return this._openedUI.hasOwnProperty(uiName)
  92. }
  93. }