UIMng.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { ViewZorder } from "../constant/ViewZOrder";
  2. import UIBase, { UIClass } from "./UIBase";
  3. import gameEventManager from "../../gameLogic/utrl/gameEventManager";
  4. import {EVENT_TYPE} from "../../gameLogic/utrl/gameEnum";
  5. import EventManager from "../event/EventManager";
  6. import UIAirdropView from "../../ui/uiview/Interface/UIAirdropView";
  7. import {Log, LOG_TAG} from "../log/Log";
  8. const needCheckLockUI = ['UISignView','UIShopView','UIAirdropView','UICrazyAwardView','UIWelfareView']
  9. export default class UIMng {
  10. private static instance: UIMng;
  11. private uiList: UIBase[] = [];
  12. public static getInstance(): UIMng {
  13. if (this.instance == null) {
  14. this.instance = new UIMng();
  15. }
  16. return this.instance;
  17. }
  18. UILock = false;
  19. private constructor() {
  20. }
  21. setLock(flag :boolean){
  22. if (!flag){
  23. EventManager.emit(EVENT_TYPE.unLockUI)
  24. }
  25. this.UILock = flag;
  26. }
  27. /**
  28. * 打开UI
  29. * @param uiClass
  30. * @param zOrder
  31. * @param callback 打开完毕回调函数
  32. * @param onProgress 打开过程进度函数
  33. * @param args 传入到UI的参数
  34. */
  35. public openUI<T extends UIBase>(uiClass: UIClass<T>, zOrder: number = ViewZorder.UI, callback?: Function, onProgress?: Function, ...args: any[]) {
  36. if (needCheckLockUI.includes(uiClass.getName())) {
  37. //特殊处理,非强制弹窗ui检查lock ,未来可优化写法
  38. if (this.UILock) {
  39. Log.log(LOG_TAG.DEBUG,`锁未被释放:${uiClass.getName()}`)
  40. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS, false);
  41. return;
  42. }
  43. }
  44. if (this.getUI(uiClass)) {
  45. console.error(`UIMng OpenUI 1: ui ${uiClass.getName()} is already exist, please check`);
  46. return;
  47. }
  48. cc.resources.load(uiClass.getUrl(), (completedCount: number, totalCount: number, item: any) => {
  49. onProgress && onProgress(completedCount, totalCount, item);
  50. }, (error: Error, prefab: cc.Prefab) => {
  51. if (error) {
  52. console.error(`UIMng OpenUI: load ui error: ${error}`);
  53. return;
  54. }
  55. if (this.getUI(uiClass)) {
  56. console.error(`UIMng OpenUI 2: ui ${uiClass.getName()} is already exist, please check`);
  57. return;
  58. }
  59. let uiNode: cc.Node = cc.instantiate(prefab);
  60. let ui = uiNode.getComponent(uiClass) as UIBase;
  61. if (!ui) {
  62. console.error(`${uiClass.getUrl()}没有绑定UI脚本!!!`);
  63. return;
  64. }
  65. ui.init(args);
  66. // let uiRoot = cc.director.getScene().getChildByName('UIRoot');
  67. let uiRoot = cc.director.getScene().getChildByName('Canvas');;
  68. if (!uiRoot) {
  69. console.error(`当前场景${cc.director.getScene().name}Canvas!!!`);
  70. return;
  71. }
  72. uiNode.parent = uiRoot;
  73. uiNode.zIndex = zOrder;
  74. this.uiList.push(ui);
  75. ui.tag = uiClass;
  76. callback && callback(ui);
  77. });
  78. }
  79. /**
  80. * 清除依赖资源
  81. * @param prefabUrl
  82. */
  83. private clearDependsRes(prefabUrl) {
  84. let deps = cc.loader.getDependsRecursively(prefabUrl);
  85. // console.log(`UIMng clearDependsRes: release ${prefabUrl} depends resources `, deps);
  86. deps.forEach((item) => {
  87. // todo:排除公共资源,然后清理
  88. // if (item.indexOf('common') === -1) {
  89. // cc.loader.release(item);
  90. // }
  91. });
  92. }
  93. public closeUI<T extends UIBase>(uiClass: UIClass<T>) {
  94. for (let i = 0; i < this.uiList.length; ++i) {
  95. if (this.uiList[i].tag === uiClass) {
  96. if (cc.isValid(this.uiList[i].node)) {
  97. this.uiList[i].node.destroy();
  98. this.clearDependsRes(uiClass.getUrl());
  99. }
  100. this.uiList.splice(i, 1);
  101. return;
  102. }
  103. }
  104. }
  105. public closeAllUI() {
  106. if (this.uiList.length == 0) {
  107. return;
  108. }
  109. this.closeUI(this.uiList[0].tag);
  110. while (this.uiList.length > 0) {
  111. this.closeUI(this.uiList[0].tag);
  112. }
  113. }
  114. public showUI<T extends UIBase>(uiClass: UIClass<T>, callback?: Function) {
  115. let ui = this.getUI(uiClass);
  116. if (!ui) {
  117. console.error(`UIMng showUI: ui ${uiClass.getName()} not exist`);
  118. return;
  119. }
  120. ui.node.active = true;
  121. }
  122. public hideUI<T extends UIBase>(uiClass: UIClass<T>) {
  123. let ui = this.getUI(uiClass);
  124. if (ui) {
  125. ui.node.active = false;
  126. }
  127. }
  128. public getUI<T extends UIBase>(uiClass: UIClass<T>): UIBase {
  129. for (let i = 0; i < this.uiList.length; ++i) {
  130. if (this.uiList[i].tag === uiClass) {
  131. return this.uiList[i];
  132. }
  133. }
  134. return null;
  135. }
  136. public isShowing<T extends UIBase>(uiClass: UIClass<T>) {
  137. let ui = this.getUI(uiClass);
  138. if (!ui) {
  139. return false;
  140. }
  141. return ui.node.active;
  142. }
  143. }