UIMask.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // UI遮罩层
  2. import { GUIEvent, NetworkEvent } from "../../../data/const/EventConst";
  3. import { ViewZOrder } from "../../../data/const/ViewZOrder";
  4. import EventMng from "../../../manager/EventMng";
  5. import { uiCommon } from "../../../utils/UICommon";
  6. const { ccclass, menu, property } = cc._decorator;
  7. @ccclass
  8. @menu("UI/persistNode/UIMask")
  9. export default class UIMask extends cc.Component {
  10. @property(cc.Node)
  11. uiMask: cc.Node = null
  12. @property(cc.Node)
  13. netWaitMask: cc.Node = null
  14. @property(cc.Node)
  15. emptyMask: cc.Node = null
  16. protected onEnable() {
  17. cc.game.addPersistRootNode(this.node)
  18. this.node.zIndex = ViewZOrder.MASK
  19. this.netWaitMask.active = false;
  20. this.uiMask.setContentSize(cc.winSize)
  21. this.node.x = cc.winSize.width / 2
  22. this.node.y = cc.winSize.height / 2
  23. EventMng.on(GUIEvent.SHOW_MASK, this.showUiLoadMask, this)
  24. EventMng.on(NetworkEvent.NETWAIT_MASK, this.showNetWaitMask, this)
  25. EventMng.on(GUIEvent.SHOW_EMPTY_MASK, this.showEmptyMask, this)
  26. uiCommon.onRegisterEvent(this.uiMask, this.onClick, this)
  27. this.uiMask.active = false
  28. }
  29. protected onDisable() {
  30. EventMng.off(GUIEvent.SHOW_MASK, this.showUiLoadMask, this)
  31. EventMng.off(NetworkEvent.NETWAIT_MASK, this.showNetWaitMask, this)
  32. EventMng.off(GUIEvent.SHOW_EMPTY_MASK, this.showEmptyMask, this)
  33. uiCommon.unRegisterEvent(this.uiMask)
  34. }
  35. uiload: Map<string, boolean> = new Map()
  36. /**
  37. * @param loadUi 加载的UI名称
  38. * @param loading 加载状态
  39. * @returns
  40. */
  41. showUiLoadMask(loadUi: string, loading: boolean) {
  42. if (loading) {
  43. this.uiload.set(loadUi, true)
  44. this.uiMask.active = loading
  45. this.scheduleOnce(() => {
  46. this.uiMask.getChildByName("imgBg").active = true
  47. let animNode = this.uiMask.getChildByName("imgBg").getComponent(cc.Animation)
  48. animNode.play()
  49. }, 0.5)
  50. } else {
  51. this.uiload.set(loadUi, false)
  52. let loadComplelte = true
  53. this.uiload.forEach((loading) => {
  54. if (loading == true) loadComplelte = false
  55. })
  56. if (!loadComplelte) return
  57. this.uiload.clear()
  58. this.uiMask.active = false
  59. this.unscheduleAllCallbacks()
  60. this.uiMask.getChildByName("imgBg").active = false
  61. }
  62. }
  63. showNetWaitMask(show: boolean = false) {
  64. this.netWaitMask.active = show;
  65. }
  66. onClick() {
  67. console.warn("ui遮罩层被点击")
  68. }
  69. showEmptyMask(show: boolean = false) {
  70. this.emptyMask.active = show;
  71. }
  72. }