UISystem.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 isPanelOpening: boolean = false;
  12. /** 是否界面正在关闭 */
  13. private isPanelClosing: boolean = false;
  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.isPanelOpening || self.getCurOnPanel(panelName)) {
  36. return;
  37. }
  38. self.isPanelOpening = true;
  39. return new Promise((resolve, reject) => {
  40. mk.loader.load(`${panelUrl}`, cc.Prefab)
  41. .then((prefab) => {
  42. self.isPanelOpening = false;
  43. let node_panel = cc.instantiate(prefab);
  44. self.curOnPanelDic[panelName] = node_panel;
  45. if (self.panelParent) {
  46. self.panelParent = self.node;
  47. }
  48. //如果父节点不存在,就默认把该节点定为panel的父节点
  49. if (!self.panelParent) {
  50. console.error("[UISystem] UI的父节点panelParent为null,请检查脚本挂载情况");
  51. return;
  52. }
  53. if (parent) {
  54. node_panel.parent = parent;
  55. }
  56. else {
  57. self.panelParent.addChild(node_panel);
  58. }
  59. switch (openAction) {
  60. case OpenActionType.normal:
  61. break;
  62. case OpenActionType.closeLast:
  63. if (self.lastOpenPanelName) {
  64. self.closePanel(self.lastOpenPanelName);
  65. }
  66. break;
  67. case OpenActionType.closeOther:
  68. let keys = Object.keys(self.curOnPanelDic).filter((key) => { key != panelName });
  69. for (var i = 0; i < keys.length; i++) {
  70. let key = keys[i];
  71. self.closePanel(key);
  72. }
  73. break;
  74. }
  75. self.lastOpenPanelName = panelName;
  76. //返回panel
  77. resolve(node_panel);
  78. })
  79. .catch((err) => {
  80. self.isPanelOpening = false;
  81. reject(err);
  82. })
  83. });
  84. }
  85. /**
  86. * 获取当前打开的panel界面
  87. * @param panelName 界面名称
  88. */
  89. getCurOnPanel(panelName: string): cc.Node {
  90. return this.curOnPanelDic[panelName];
  91. }
  92. /**
  93. * 关闭panel界面
  94. * @param panelName 通常使用this.node.name代替,界面名称,不是全路径
  95. * @param callBack 关闭之后回调
  96. */
  97. closePanel(panelName: string, callBack: Function = null) {
  98. if (this.isPanelClosing || !this.getCurOnPanel(panelName)) {
  99. return;
  100. }
  101. this.isPanelClosing = true;
  102. let node_panel = this.getCurOnPanel(panelName);
  103. // 是否存在关闭动画
  104. let comp_anim = node_panel.getComponent('EffectOpenAndClose');
  105. if (comp_anim) {
  106. comp_anim.hideEffect(this.destroyNode.bind(this, node_panel, panelName, callBack));
  107. } else {
  108. this.destroyNode(node_panel, panelName, callBack);
  109. }
  110. mk.event.emit('close-panel', panelName);
  111. }
  112. private destroyNode(node_panel, panelName, callBack = null) {
  113. node_panel.destroy();
  114. delete this.curOnPanelDic[panelName];
  115. this.isPanelClosing = false;
  116. callBack && callBack();
  117. }
  118. /**
  119. * 关闭所有界面,除了主界面
  120. * @param except 排除的ui
  121. */
  122. closeAllUI(except: string[] = null) {
  123. for (let panelName in this.curOnPanelDic) {
  124. if (except && except.length > 0 && except.indexOf(panelName) != -1) {
  125. continue;
  126. }
  127. let node_panel = this.getCurOnPanel(panelName);
  128. let comp_anim = node_panel.getComponent('EffectOpenAndClose');
  129. if (comp_anim) {
  130. comp_anim.hideEffect(this.destroyNode.bind(this, node_panel, panelName));
  131. } else {
  132. this.destroyNode(node_panel, panelName);
  133. }
  134. mk.event.emit('close-panel', panelName);
  135. }
  136. this.isPanelClosing = false;
  137. }
  138. }
  139. /** 界面打开操作 */
  140. export enum OpenActionType {
  141. /** 普通不操作 */
  142. normal = 0,
  143. /** 关闭其他 */
  144. closeOther = 1,
  145. /** 关闭上一个 */
  146. closeLast = 2
  147. }