UISystem.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * 获取当前是否有除主界面的其他界面打开
  98. */
  99. isPopPanel(except: string[] = ["game"]){
  100. let count = 0;
  101. for (let panelName in this.curOnPanelDic) {
  102. if(except.indexOf(panelName) != -1){
  103. continue;
  104. }
  105. ++count;
  106. }
  107. return count > 0;
  108. }
  109. /**
  110. * 关闭panel界面
  111. * @param panelName 通常使用this.node.name代替,界面名称,不是全路径
  112. * @param callBack 关闭之后回调
  113. */
  114. closePanel(panelName: string, callBack: Function = null) {
  115. if (this.isPanelClosingArr.indexOf(panelName) != -1 || !this.getCurOnPanel(panelName)) {
  116. return;
  117. }
  118. this.isPanelClosingArr.push(panelName);
  119. let node_panel = this.getCurOnPanel(panelName);
  120. if (!node_panel) {
  121. return;
  122. }
  123. // 是否存在关闭动画
  124. let comp_anim = node_panel.getComponent('EffectOpenAndClose');
  125. if (comp_anim && comp_anim.isOpenCloseEffect) {
  126. comp_anim.hideEffect(this.destroyNode.bind(this, node_panel, panelName, callBack));
  127. } else {
  128. this.destroyNode(node_panel, panelName, callBack);
  129. }
  130. mk.event.emit('close-panel', panelName);
  131. }
  132. private destroyNode(node_panel, panelName, callBack = null) {
  133. node_panel.destroy();
  134. delete this.curOnPanelDic[panelName];
  135. let index = this.isPanelClosingArr.indexOf(panelName);
  136. this.isPanelClosingArr.splice(index, 1);
  137. callBack && callBack();
  138. }
  139. /**
  140. * 关闭所有界面,除了主界面
  141. * @param except 排除的ui
  142. */
  143. closeAllUI(except: string[] = ["game", "guide"]) {
  144. for (let panelName in this.curOnPanelDic) {
  145. if (except && except.length > 0 && except.indexOf(panelName) != -1) {
  146. continue;
  147. }
  148. if (this.isPanelClosingArr.indexOf(panelName) != -1) {
  149. continue;
  150. }
  151. this.isPanelClosingArr.push(panelName);
  152. let node_panel = this.getCurOnPanel(panelName);
  153. if (!node_panel) {
  154. continue;
  155. }
  156. //如果EffectOpenAndClose没有打开关闭动画是无效的
  157. let comp_anim = node_panel.getComponent('EffectOpenAndClose');
  158. if (comp_anim) {
  159. if(comp_anim.isOpenCloseEffect)
  160. {
  161. comp_anim.hideEffect(this.destroyNode.bind(this, node_panel, panelName));
  162. }else{
  163. this.destroyNode(node_panel, panelName);
  164. }
  165. } else {
  166. this.destroyNode(node_panel, panelName);
  167. }
  168. mk.event.emit('close-panel', panelName);
  169. }
  170. mk.ad.destroyNativeAd()
  171. mk.ad.destoryBanner()
  172. }
  173. /** 隐藏界面,不销毁 */
  174. hideUI(node_panel, callBack: Function = null) {
  175. // 是否存在关闭动画
  176. let comp_anim = node_panel.getComponent('EffectOpenAndClose');
  177. if (comp_anim) {
  178. comp_anim.hideEffect(callBack);
  179. } else {
  180. node_panel.active = false;
  181. }
  182. }
  183. }
  184. /** 界面打开操作 */
  185. export enum OpenActionType {
  186. /** 普通不操作 */
  187. normal = 0,
  188. /** 关闭其他 */
  189. closeOther = 1,
  190. /** 关闭上一个 */
  191. closeLast = 2
  192. }