UISystem.ts 6.7 KB

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