LayerManager.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { Asset, Node, Prefab } from "cc";
  2. import { EventMgr } from "../event/EventManager";
  3. import { LoadQueue } from "../res/LoadQueue";
  4. import { StringUtil } from "../util/StringUtil";
  5. import { BaseLayer } from "./BaseLayer";
  6. import { BaseView } from "./BaseView";
  7. import { LayerConf, LayerType } from "../config/LayerConf";
  8. //UI数据
  9. export interface LayerInfo {
  10. id: number;
  11. view: BaseView;
  12. args: any[];
  13. res: Asset;
  14. callback: Function;
  15. active: Boolean;
  16. //是否加载中
  17. bLoading: Boolean;
  18. }
  19. export class LayerManager {
  20. private _game_layer: BaseLayer = null;
  21. private _ui_layer: BaseLayer = null;
  22. private _notice_layer: BaseLayer = null;
  23. private _guide_layer: BaseLayer = null;
  24. private _layer_conf: { [key: number]: LayerConf } = {};
  25. private _class_uuid = StringUtil.getUUID(32);
  26. constructor(root: Node, conf: any) {
  27. this._layer_conf = conf;
  28. //预加载资源
  29. for (let id in this._layer_conf) {
  30. let conf = this._layer_conf[id];
  31. (conf.preload) && LoadQueue.pushPreload(1, conf.bundle, conf.url, Prefab);
  32. //conf.destroy = (conf.destroy) ? conf.destroy : true;
  33. }
  34. this._game_layer = new BaseLayer("Game_Layer", this._layer_conf);
  35. root.addChild(this._game_layer);
  36. this._ui_layer = new BaseLayer("UI_Layer", this._layer_conf, true);
  37. root.addChild(this._ui_layer);
  38. this._notice_layer = new BaseLayer("Notice_Layer", this._layer_conf);
  39. root.addChild(this._notice_layer);
  40. this._guide_layer = new BaseLayer("Guide_Layer", this._layer_conf);
  41. root.addChild(this._guide_layer);
  42. EventMgr.addEvent("_Framewrok_Show_GameLayer", (value: boolean) => {
  43. // this._game_layer.showLayerUI(value);
  44. }, this, this._class_uuid);
  45. }
  46. /**
  47. * 打开UI
  48. * @param id UIID
  49. * @param callback UI打开成功回调
  50. * @param args 打开UI的可传参数
  51. */
  52. open(id: number, callback: Function = null, ...args: any[]) {
  53. // let openState = GameUtil.checkSystemOpen(id)
  54. // if (!openState.state) {
  55. // Framework.tips.setTips(openState.tips);
  56. // return
  57. // }
  58. let conf = this._layer_conf[id];
  59. // Framework.tips.setTips('huidhkaj shdgk ')
  60. if (conf) {
  61. //完全打开需要去检测引导手指
  62. let mCallback = () => {
  63. // Framework.event.fireEvent(GameEvent.GuideClick_Update_Finger, GuideManager.needShowFinger());
  64. if (callback) {
  65. callback();
  66. }
  67. };
  68. console.log('conf.type', conf.type)
  69. if (conf.type === LayerType.Game) {
  70. this._game_layer.open(id, mCallback, args);
  71. } else if (conf.type === LayerType.UI) {
  72. this._ui_layer.open(id, mCallback, args);
  73. } else if (conf.type === LayerType.Notice) {
  74. this._notice_layer.open(id, mCallback, args);
  75. } else if (conf.type === LayerType.Guide) {
  76. this._guide_layer.open(id, mCallback, args);
  77. }
  78. return true;
  79. }
  80. console.error("未找到对应UI:" + id);
  81. return false;
  82. }
  83. /**
  84. * 关闭UI
  85. * @param ui 待关闭的UI,该值可为id也可为this
  86. */
  87. close(ui: number | BaseView) {
  88. if (ui) {
  89. let id = (typeof ui != "number") ? ui.view_id : ui;
  90. let conf = this._layer_conf[id];
  91. if (conf) {
  92. if (conf.type === LayerType.Game) {
  93. this._game_layer.close(ui);
  94. } else if (conf.type === LayerType.UI) {
  95. this._ui_layer.close(ui);
  96. } else if (conf.type === LayerType.Notice) {
  97. this._notice_layer.close(ui);
  98. } else if (conf.type === LayerType.Guide) {
  99. this._guide_layer.close(ui);
  100. }
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. ShowUI(ui, state, callback: Function = null, ...args: any[]) {
  107. this._ui_layer.ShowUI(ui, state, callback, args);
  108. }
  109. /**
  110. * 获取UI对应的UIView
  111. * @param id UIID
  112. */
  113. getUIView(id: number) {
  114. let conf = this._layer_conf[id];
  115. if (conf) {
  116. if (conf.type === LayerType.Game) {
  117. return this._game_layer.getUIView(id);
  118. } else if (conf.type === LayerType.UI) {
  119. return this._ui_layer.getUIView(id);
  120. } else if (conf.type === LayerType.Notice) {
  121. return this._notice_layer.getUIView(id);
  122. } else if (conf.type === LayerType.Guide) {
  123. return this._guide_layer.getUIView(id);
  124. }
  125. }
  126. console.error("未找到对应UI:" + id);
  127. return null;
  128. }
  129. /**
  130. * 关闭所有UI
  131. */
  132. closeAllUI(withoutID: number = -1) {
  133. EventMgr.fireEvent("_Framewrok_Show_GameLayer", true);
  134. this._ui_layer.closeAll(withoutID);
  135. }
  136. closeAllGame(withoutID: number = -1) {
  137. this._game_layer.closeAll(withoutID);
  138. }
  139. }