UISystem.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. const { ccclass, property } = cc._decorator;
  2. @ccclass
  3. /**
  4. * UI管理类
  5. * @author 冯聪
  6. */
  7. export default class UISystem extends cc.Component {
  8. /** panel父节点 */
  9. private panelParent: cc.Node = null;
  10. /** 是否正在打开界面 */
  11. private isPanelOpeningArr = [];
  12. /** 是否界面正在关闭 */
  13. private isPanelClosingArr = [];
  14. /** 当前打开的UIPanel */
  15. private curOnPanelDic: { [key: string]: cc.Node } = {};
  16. /** 上一个打开的界面名称 */
  17. private lastOpenPanelName: string = "";
  18. private registerCallBack: Map<any, Map<string, Function>> = new Map<any, Map<string,Function>>();
  19. onLoad() {
  20. mk.ui = this;
  21. this.registerCallBack.clear();
  22. }
  23. start() {
  24. if (!this.panelParent) {
  25. this.panelParent = this.node;
  26. }
  27. }
  28. /**
  29. * 打开panel界面
  30. * @param panelUrl 界面名称全路径
  31. * @param openAction 打开之后的操(需不需要关闭其他界面)
  32. */
  33. openPanel(panelUrl: string, openAction: OpenActionType = OpenActionType.normal, parent: cc.Node = null): Promise<any> {
  34. let arr = panelUrl.split("/");
  35. let panelName = arr[arr.length - 1];
  36. let self = this;
  37. if (self.isPanelOpeningArr.indexOf(panelName) != -1 || self.getCurOnPanel(panelName)) {
  38. return;
  39. }
  40. self.isPanelOpeningArr.push(panelName);
  41. return new Promise((resolve, reject) => {
  42. mk.loader.load(`${panelUrl}`, cc.Prefab)
  43. .then((prefab) => {
  44. let node_panel = cc.instantiate(prefab);
  45. self.curOnPanelDic[panelName] = node_panel;
  46. if (self.panelParent) {
  47. self.panelParent = self.node;
  48. }
  49. //如果父节点不存在,就默认把该节点定为panel的父节点
  50. if (!self.panelParent) {
  51. console.error("[UISystem] UI的父节点panelParent为null,请检查脚本挂载情况");
  52. let index = self.isPanelOpeningArr.indexOf(panelName);
  53. self.isPanelOpeningArr.splice(index, 1);
  54. return;
  55. }
  56. if (parent) {
  57. node_panel.parent = parent;
  58. }
  59. else {
  60. self.panelParent.addChild(node_panel);
  61. }
  62. switch (openAction) {
  63. case OpenActionType.normal:
  64. break;
  65. case OpenActionType.closeLast:
  66. if (self.lastOpenPanelName) {
  67. self.closePanel(self.lastOpenPanelName);
  68. }
  69. break;
  70. case OpenActionType.closeOther:
  71. let keys = Object.keys(self.curOnPanelDic).filter((key) => { return key != panelName });
  72. for (var i = 0; i < keys.length; i++) {
  73. let key = keys[i];
  74. self.closePanel(key);
  75. }
  76. break;
  77. }
  78. self.lastOpenPanelName = panelName;
  79. //返回panel
  80. resolve(node_panel);
  81. let index = self.isPanelOpeningArr.indexOf(panelName);
  82. self.isPanelOpeningArr.splice(index, 1);
  83. })
  84. .catch((err) => {
  85. let index = self.isPanelOpeningArr.indexOf(panelName);
  86. self.isPanelOpeningArr.splice(index, 1);
  87. reject(err);
  88. })
  89. });
  90. }
  91. /**
  92. * 获取当前打开的panel界面
  93. * @param panelName 界面名称
  94. */
  95. getCurOnPanel(panelName: string): cc.Node {
  96. return this.curOnPanelDic[panelName];
  97. }
  98. /**
  99. * 关闭panel界面
  100. * @param panelName 通常使用this.node.name代替,界面名称,不是全路径
  101. * @param callBack 关闭之后回调
  102. */
  103. closePanel(panelName: string, callBack: Function = null) {
  104. if (this.isPanelClosingArr.indexOf(panelName) != -1 || !this.getCurOnPanel(panelName)) {
  105. return;
  106. }
  107. this.isPanelClosingArr.push(panelName);
  108. let node_panel = this.getCurOnPanel(panelName);
  109. if (!node_panel) {
  110. return;
  111. }
  112. // 是否存在关闭动画
  113. let comp_anim = node_panel.getComponent('EffectOpenAndClose');
  114. if (comp_anim && comp_anim.isOpenCloseEffect) {
  115. comp_anim.hideEffect(this.destroyNode.bind(this, node_panel, panelName, callBack));
  116. } else {
  117. this.destroyNode(node_panel, panelName, callBack);
  118. }
  119. mk.event.emit('close-panel', panelName);
  120. }
  121. private destroyNode(node_panel, panelName, callBack = null) {
  122. node_panel.destroy();
  123. delete this.curOnPanelDic[panelName];
  124. let index = this.isPanelClosingArr.indexOf(panelName);
  125. this.isPanelClosingArr.splice(index, 1);
  126. callBack && callBack();
  127. }
  128. /**
  129. * 关闭所有界面,除了主界面
  130. * @param except 排除的ui
  131. */
  132. closeAllUI(except: string[] = ["game", "guide"]) {
  133. for (let panelName in this.curOnPanelDic) {
  134. if (this.isPanelClosingArr.indexOf(panelName) != -1) {
  135. continue;
  136. }
  137. this.isPanelClosingArr.push(panelName);
  138. if (except && except.length > 0 && except.indexOf(panelName) != -1) {
  139. continue;
  140. }
  141. let node_panel = this.getCurOnPanel(panelName);
  142. if (!node_panel) {
  143. continue;
  144. }
  145. let comp_anim = node_panel.getComponent('EffectOpenAndClose');
  146. if (comp_anim) {
  147. comp_anim.hideEffect(this.destroyNode.bind(this, node_panel, panelName));
  148. } else {
  149. this.destroyNode(node_panel, panelName);
  150. }
  151. mk.event.emit('close-panel', panelName);
  152. }
  153. mk.ad.destroyNativeAd()
  154. mk.ad.destoryBanner()
  155. }
  156. registerRefreshEvent(target:any, callBack: Function = null, name: string = "default")
  157. {
  158. let value = this.registerCallBack.get(target);
  159. if(value)
  160. {
  161. value.set(name, callBack);
  162. }else{
  163. let m = new Map<string, Function>();
  164. m.set(name, callBack);
  165. this.registerCallBack.set(target, m);
  166. }
  167. }
  168. invokeRefreshEventByName(name: string)
  169. {
  170. this.registerCallBack.forEach((value, key)=>{
  171. value.forEach((callBack, k)=>{
  172. if(k === name)
  173. {
  174. callBack && callBack();
  175. }
  176. })
  177. });
  178. }
  179. removeRefreshEvent(target: any)
  180. {
  181. let value = this.registerCallBack.get(target);
  182. if(value)
  183. {
  184. this.registerCallBack.delete(target);
  185. }
  186. }
  187. }
  188. /** 界面打开操作 */
  189. export enum OpenActionType {
  190. /** 普通不操作 */
  191. normal = 0,
  192. /** 关闭其他 */
  193. closeOther = 1,
  194. /** 关闭上一个 */
  195. closeLast = 2
  196. }