UISystem.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 gamePanelbUrl: string = './game/prefab';
  12. /** 模块界面预制体路径 */
  13. private modulPanelUrl: string = './module';
  14. /** 是否正在打开界面 */
  15. private isPanelOpening: boolean = false;
  16. /** 是否界面正在关闭 */
  17. private isPanelClosing: boolean = false;
  18. /** 当前打开的UIPanel */
  19. private curOnPanelDic: { [key: string]: cc.Node } = {};
  20. /** 上一个打开的界面名称 */
  21. private lastOpenPanelName: string = "";
  22. onLoad() {
  23. mk.ui = this;
  24. }
  25. start() {
  26. if (!this.panelParent) {
  27. this.panelParent = this.node;
  28. }
  29. }
  30. /**
  31. * 打开panel界面
  32. * @param panelName 界面名称
  33. * @param panelType 界面类型
  34. * @param openAction 打开之后的操(需不需要关闭其他界面)
  35. */
  36. openPanel(panelName: string, panelType: PanelType, openAction: OpenActionType = OpenActionType.normal): Promise<any> {
  37. let panelUrl = this.getUrlByPanelType(panelName, panelType);
  38. let self = this;
  39. if (self.isPanelOpening || self.getCurOnPanel(panelName)) {
  40. return;
  41. }
  42. self.isPanelOpening = true;
  43. return new Promise((resolve, reject) => {
  44. mk.loader.load(`${panelUrl}`, cc.Prefab)
  45. .then((prefab) => {
  46. self.isPanelOpening = false;
  47. let node_panel = cc.instantiate(prefab);
  48. self.curOnPanelDic[panelName] = node_panel;
  49. if (self.panelParent) {
  50. self.panelParent = self.node;
  51. }
  52. //如果父节点不存在,就默认把该节点定为panel的父节点
  53. if (!self.panelParent) {
  54. console.error("[UISystem] UI的父节点panelParent为null,请检查脚本挂载情况");
  55. return;
  56. }
  57. self.panelParent.addChild(node_panel);
  58. switch (openAction) {
  59. case OpenActionType.normal:
  60. break;
  61. case OpenActionType.closeLast:
  62. if (self.lastOpenPanelName) {
  63. self.closePanel(self.lastOpenPanelName);
  64. }
  65. break;
  66. case OpenActionType.closeOther:
  67. let keys = Object.keys(self.curOnPanelDic).filter((key) => { key != panelName });
  68. for (var i = 0; i < keys.length; i++) {
  69. let key = keys[i];
  70. self.closePanel(key);
  71. }
  72. break;
  73. }
  74. self.lastOpenPanelName = panelName;
  75. //返回panel
  76. resolve(node_panel);
  77. })
  78. .catch((err) => {
  79. reject(err);
  80. })
  81. });
  82. }
  83. /**
  84. * 获取当前打开的panel界面
  85. * @param panelName 界面名称
  86. */
  87. getCurOnPanel(panelName: string): cc.Node {
  88. return this.curOnPanelDic[panelName];
  89. }
  90. /**
  91. * 关闭panel界面
  92. * @param panelName 界面名称
  93. * @param callBack 关闭之后回调
  94. */
  95. closePanel(panelName: string, callBack: Function = null) {
  96. if (this.isPanelClosing || !this.getCurOnPanel(panelName)) {
  97. return;
  98. }
  99. this.isPanelClosing = true;
  100. let node_panel = this.getCurOnPanel(panelName);
  101. // 是否存在关闭动画
  102. let comp_anim = node_panel.getComponent('UIOpenAndClose');
  103. if (comp_anim) {
  104. comp_anim.hideEffect(this.destroyNode.bind(this, node_panel, panelName, callBack));
  105. } else {
  106. this.destroyNode(node_panel, panelName, callBack);
  107. }
  108. }
  109. private destroyNode(node_panel, panelName, callBack) {
  110. node_panel.destroy();
  111. delete this.curOnPanelDic[panelName];
  112. this.isPanelClosing = false;
  113. callBack && callBack();
  114. }
  115. /**
  116. * 根据界面类型获取url
  117. * @param panelName 界面名称
  118. * @param panelType 界面类型
  119. */
  120. getUrlByPanelType(pannelName: string, panelType: PanelType): string {
  121. let url: string = "";
  122. switch (panelType) {
  123. case PanelType.game:
  124. url = `${this.gamePanelbUrl}/${pannelName}`
  125. break;
  126. case PanelType.module:
  127. url = `${this.modulPanelUrl}/${pannelName}/${pannelName}`
  128. break;
  129. }
  130. return url;
  131. }
  132. }
  133. /** 界面打开操作 */
  134. export enum OpenActionType {
  135. /** 普通不操作 */
  136. normal = 0,
  137. /** 关闭其他 */
  138. closeOther = 1,
  139. /** 关闭上一个 */
  140. closeLast = 2
  141. }
  142. /** */
  143. export enum PanelType {
  144. /**游戏界面 */
  145. game = 1,
  146. /**模块界面 */
  147. module = 2,
  148. }