uiManager.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { _decorator, Component, Node, SpriteComponent, Color, RichTextComponent, find, isValid, Vec3, UITransformComponent } from "cc";
  2. import { resourceUtil } from "./resourceUtil";
  3. import { poolManager } from "./poolManager";
  4. import { constant } from "./constant";
  5. import { tips } from "../ui/common/tips";
  6. const { ccclass, property } = _decorator;
  7. const SHOW_STR_INTERVAL_TIME = 800;
  8. @ccclass("uiManager")
  9. export class uiManager {
  10. private _dictSharedPanel: any = {}
  11. private _dictLoading: any = {}
  12. private _arrPopupDialog: any = []
  13. private _showTipsTime: number = 0
  14. private static _instance: uiManager;
  15. public static get instance() {
  16. if (this._instance) {
  17. return this._instance;
  18. }
  19. this._instance = new uiManager();
  20. return this._instance;
  21. }
  22. /**
  23. * 检查当前界面是否正在展示
  24. * @param panelPath
  25. */
  26. public isDialogVisible(panelPath: string) {
  27. if (!this._dictSharedPanel.hasOwnProperty(panelPath)) {
  28. return false;
  29. }
  30. let panel = this._dictSharedPanel[panelPath];
  31. return isValid(panel) && panel.active && panel.parent;
  32. }
  33. /**
  34. * 显示单例界面
  35. * @param {String} panelPath
  36. * @param {Array} args
  37. * @param {Function} cb 回调函数,创建完毕后回调
  38. */
  39. public showDialog(panelPath: string, args?: any, cb?: Function) {
  40. if (this._dictLoading[panelPath]) {
  41. return;
  42. }
  43. let idxSplit = panelPath.lastIndexOf('/');
  44. let scriptName = panelPath.slice(idxSplit + 1);
  45. if (!args) {
  46. args = [];
  47. }
  48. if (this._dictSharedPanel.hasOwnProperty(panelPath)) {
  49. let panel = this._dictSharedPanel[panelPath];
  50. if (isValid(panel)) {
  51. panel.parent = find("Canvas");
  52. panel.active = true;
  53. let script = panel.getComponent(scriptName);
  54. let script2 = panel.getComponent(scriptName.charAt(0).toUpperCase() + scriptName.slice(1));
  55. if (script && script.show) {
  56. script.show.apply(script, args);
  57. cb && cb(script);
  58. } else if (script2 && script2.show) {
  59. script2.show.apply(script2, args);
  60. cb && cb(script2);
  61. } else {
  62. throw `查找不到脚本文件${scriptName}`;
  63. }
  64. return;
  65. }
  66. }
  67. this._dictLoading[panelPath] = true;
  68. resourceUtil.createUI(panelPath, (err: any, node: any) => {
  69. //判断是否有可能在显示前已经被关掉了?
  70. let isCloseBeforeShow = false;
  71. if (!this._dictLoading[panelPath]) {
  72. //已经被关掉
  73. isCloseBeforeShow = true;
  74. }
  75. this._dictLoading[panelPath] = false;
  76. if (err) {
  77. console.error(err);
  78. return;
  79. }
  80. // node.getComponent(UITransformComponent).priority = constant.ZORDER.DIALOG;
  81. this._dictSharedPanel[panelPath] = node;
  82. let script: any = node.getComponent(scriptName);
  83. let script2: any = node.getComponent(scriptName.charAt(0).toUpperCase() + scriptName.slice(1));
  84. if (script && script.show) {
  85. script.show.apply(script, args);
  86. cb && cb(script);
  87. } else if (script2 && script2.show) {
  88. script2.show.apply(script2, args);
  89. cb && cb(script2);
  90. } else {
  91. throw `查找不到脚本文件${scriptName}`;
  92. }
  93. if (isCloseBeforeShow) {
  94. //如果在显示前又被关闭,则直接触发关闭掉
  95. this.hideDialog(panelPath);
  96. }
  97. });
  98. }
  99. /**
  100. * 隐藏单例界面
  101. * @param {String} panelPath
  102. * @param {fn} callback
  103. */
  104. public hideDialog(panelPath: string, callback?: Function) {
  105. if (this._dictSharedPanel.hasOwnProperty(panelPath)) {
  106. let panel = this._dictSharedPanel[panelPath];
  107. if (panel && isValid(panel)) {
  108. let ani = panel.getComponent('animationUI');
  109. if (ani) {
  110. ani.close(() => {
  111. panel.parent = null;
  112. if (callback && typeof callback === 'function') {
  113. callback();
  114. }
  115. });
  116. } else {
  117. panel.parent = null;
  118. if (callback && typeof callback === 'function') {
  119. callback();
  120. }
  121. }
  122. } else if (callback && typeof callback === 'function') {
  123. callback();
  124. }
  125. }
  126. this._dictLoading[panelPath] = false;
  127. }
  128. /**
  129. * 将弹窗加入弹出窗队列
  130. * @param {string} panelPath
  131. * @param {string} scriptName
  132. * @param {*} param
  133. */
  134. public pushToPopupSeq(panelPath: string, scriptName: string, param: any) {
  135. let popupDialog = {
  136. panelPath: panelPath,
  137. scriptName: scriptName,
  138. param: param,
  139. isShow: false
  140. };
  141. this._arrPopupDialog.push(popupDialog);
  142. this._checkPopupSeq();
  143. }
  144. /**
  145. * 将弹窗加入弹出窗队列
  146. * @param {number} index
  147. * @param {string} panelPath
  148. * @param {string} scriptName
  149. * @param {*} param
  150. */
  151. public insertToPopupSeq(index: number, panelPath: string, param: any) {
  152. let popupDialog = {
  153. panelPath: panelPath,
  154. param: param,
  155. isShow: false
  156. };
  157. this._arrPopupDialog.splice(index, 0, popupDialog);
  158. //this._checkPopupSeq();
  159. }
  160. /**
  161. * 将弹窗从弹出窗队列中移除
  162. * @param {string} panelPath
  163. */
  164. public shiftFromPopupSeq(panelPath: string) {
  165. this.hideDialog(panelPath, () => {
  166. if (this._arrPopupDialog[0] && this._arrPopupDialog[0].panelPath === panelPath) {
  167. this._arrPopupDialog.shift();
  168. this._checkPopupSeq();
  169. }
  170. })
  171. }
  172. /**
  173. * 检查当前是否需要弹窗
  174. */
  175. private _checkPopupSeq() {
  176. if (this._arrPopupDialog.length > 0) {
  177. let first = this._arrPopupDialog[0];
  178. if (!first.isShow) {
  179. this.showDialog(first.panelPath, first.param);
  180. this._arrPopupDialog[0].isShow = true;
  181. }
  182. }
  183. }
  184. /**
  185. * 显示提示
  186. * @param {String} content
  187. * @param {Function} cb
  188. */
  189. public showTips(content: string | number, callback?: Function) {
  190. let str = String(content);
  191. let next = () => {
  192. this._showTipsAni(str, callback);
  193. }
  194. var now = Date.now();
  195. if (now - this._showTipsTime < SHOW_STR_INTERVAL_TIME) {
  196. var spareTime = SHOW_STR_INTERVAL_TIME - (now - this._showTipsTime);
  197. setTimeout(() => {
  198. next();
  199. }, spareTime);
  200. this._showTipsTime = now + spareTime;
  201. } else {
  202. next();
  203. this._showTipsTime = now;
  204. }
  205. }
  206. /**
  207. * 内部函数
  208. * @param {String} content
  209. * @param {Function} cb
  210. */
  211. private _showTipsAni(content: string, callback?: Function) {
  212. resourceUtil.getUIPrefabRes('common/tips', function (err: any, prefab: any) {
  213. if (err) {
  214. return;
  215. }
  216. let tipsNode = poolManager.instance.getNode(prefab, find("Canvas") as Node);
  217. let tipScript = tipsNode.getComponent(tips) as tips;
  218. tipScript.show(content, callback);
  219. });
  220. }
  221. }