UISystem.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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): 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. self.panelParent.addChild(node_panel);
  54. switch (openAction) {
  55. case OpenActionType.normal:
  56. break;
  57. case OpenActionType.closeLast:
  58. if (self.lastOpenPanelName) {
  59. self.closePanel(self.lastOpenPanelName);
  60. }
  61. break;
  62. case OpenActionType.closeOther:
  63. let keys = Object.keys(self.curOnPanelDic).filter((key) => { key != panelName });
  64. for (var i = 0; i < keys.length; i++) {
  65. let key = keys[i];
  66. self.closePanel(key);
  67. }
  68. break;
  69. }
  70. self.lastOpenPanelName = panelName;
  71. //返回panel
  72. resolve(node_panel);
  73. })
  74. .catch((err) => {
  75. reject(err);
  76. })
  77. });
  78. }
  79. /**
  80. * 获取当前打开的panel界面
  81. * @param panelName 界面名称
  82. */
  83. getCurOnPanel(panelName: string): cc.Node {
  84. return this.curOnPanelDic[panelName];
  85. }
  86. /**
  87. * 关闭panel界面
  88. * @param panelName 界面名称
  89. * @param callBack 关闭之后回调
  90. */
  91. closePanel(panelName: string, callBack: Function = null) {
  92. if (this.isPanelClosing || !this.getCurOnPanel(panelName)) {
  93. return;
  94. }
  95. this.isPanelClosing = true;
  96. let node_panel = this.getCurOnPanel(panelName);
  97. // 是否存在关闭动画
  98. let comp_anim = node_panel.getComponent('UIOpenAndClose');
  99. if (comp_anim) {
  100. comp_anim.hideEffect(this.destroyNode.bind(this, node_panel, panelName, callBack));
  101. } else {
  102. this.destroyNode(node_panel, panelName, callBack);
  103. }
  104. }
  105. private destroyNode(node_panel, panelName, callBack) {
  106. node_panel.destroy();
  107. delete this.curOnPanelDic[panelName];
  108. this.isPanelClosing = false;
  109. callBack && callBack();
  110. }
  111. }
  112. /** 界面打开操作 */
  113. export enum OpenActionType {
  114. /** 普通不操作 */
  115. normal = 0,
  116. /** 关闭其他 */
  117. closeOther = 1,
  118. /** 关闭上一个 */
  119. closeLast = 2
  120. }