LayerManager.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. //UI类型
  8. export enum LayerType {
  9. Game = 0, //游戏层
  10. UI, //UI层
  11. Notice, //公告层
  12. Guide //引导层
  13. }
  14. //UI配置结构体
  15. export interface LayerConf {
  16. bundle?: string; //BundleName
  17. url: string; //资源路径
  18. type: LayerType; //UI类型
  19. anim?: boolean; //是否使用动画
  20. cache?: boolean; //是否缓存该UI的资源(该UI动态加载的资源不会缓存)
  21. bottom?: boolean; //是否是最底层UI(不受切换影响)
  22. preload?: boolean; //是否预加载
  23. special?: boolean; //是否是特殊UI
  24. //destroy?: boolean; //关闭时是否销毁
  25. }
  26. //UI数据
  27. export interface LayerInfo {
  28. id: number;
  29. view: BaseView;
  30. args: any[];
  31. res: Asset;
  32. callback: Function;
  33. active: Boolean;
  34. //是否加载中
  35. bLoading: Boolean;
  36. }
  37. export class LayerManager {
  38. private _game_layer: BaseLayer = null;
  39. private _ui_layer: BaseLayer = null;
  40. private _notice_layer: BaseLayer = null;
  41. private _guide_layer: BaseLayer = null;
  42. private _layer_conf: { [key: number]: LayerConf } = {};
  43. private _class_uuid = StringUtil.getUUID(32);
  44. constructor(root: Node, conf: any) {
  45. this._layer_conf = conf;
  46. //预加载资源
  47. for (let id in this._layer_conf) {
  48. let conf = this._layer_conf[id];
  49. (conf.preload) && LoadQueue.pushPreload(1, conf.bundle, conf.url, Prefab);
  50. //conf.destroy = (conf.destroy) ? conf.destroy : true;
  51. }
  52. this._game_layer = new BaseLayer("Game_Layer", this._layer_conf);
  53. root.addChild(this._game_layer);
  54. this._ui_layer = new BaseLayer("UI_Layer", this._layer_conf, true);
  55. root.addChild(this._ui_layer);
  56. this._notice_layer = new BaseLayer("Notice_Layer", this._layer_conf);
  57. root.addChild(this._notice_layer);
  58. this._guide_layer = new BaseLayer("Guide_Layer", this._layer_conf);
  59. root.addChild(this._guide_layer);
  60. EventMgr.addEvent("_Framewrok_Show_GameLayer", (value: boolean) => {
  61. // this._game_layer.showLayerUI(value);
  62. }, this, this._class_uuid);
  63. }
  64. /**
  65. * 打开UI
  66. * @param id UIID
  67. * @param callback UI打开成功回调
  68. * @param args 打开UI的可传参数
  69. */
  70. open(id: number, callback: Function = null, ...args: any[]) {
  71. // let openState = GameUtil.checkSystemOpen(id)
  72. // if (!openState.state) {
  73. // Framework.tips.setTips(openState.tips);
  74. // return
  75. // }
  76. let conf = this._layer_conf[id];
  77. // Framework.tips.setTips('huidhkaj shdgk ')
  78. if (conf) {
  79. //完全打开需要去检测引导手指
  80. let mCallback = () => {
  81. // Framework.event.fireEvent(GameEvent.GuideClick_Update_Finger, GuideManager.needShowFinger());
  82. if (callback) {
  83. callback();
  84. }
  85. };
  86. if (conf.type === LayerType.Game) {
  87. this._game_layer.open(id, mCallback, args);
  88. } else if (conf.type === LayerType.UI) {
  89. this._ui_layer.open(id, mCallback, args);
  90. } else if (conf.type === LayerType.Notice) {
  91. this._notice_layer.open(id, mCallback, args);
  92. } else if (conf.type === LayerType.Guide) {
  93. this._guide_layer.open(id, mCallback, args);
  94. }
  95. return true;
  96. }
  97. console.error("未找到对应UI:" + id);
  98. return false;
  99. }
  100. /**
  101. * 关闭UI
  102. * @param ui 待关闭的UI,该值可为id也可为this
  103. */
  104. close(ui: number | BaseView) {
  105. if (ui) {
  106. let id = (typeof ui != "number") ? ui.view_id : ui;
  107. let conf = this._layer_conf[id];
  108. if (conf) {
  109. if (conf.type === LayerType.Game) {
  110. this._game_layer.close(ui);
  111. } else if (conf.type === LayerType.UI) {
  112. this._ui_layer.close(ui);
  113. } else if (conf.type === LayerType.Notice) {
  114. this._notice_layer.close(ui);
  115. } else if (conf.type === LayerType.Guide) {
  116. this._guide_layer.close(ui);
  117. }
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. ShowUI(ui, state, callback: Function = null, ...args: any[]) {
  124. this._ui_layer.ShowUI(ui, state, callback, args);
  125. }
  126. /**
  127. * 获取UI对应的UIView
  128. * @param id UIID
  129. */
  130. getUIView(id: number) {
  131. let conf = this._layer_conf[id];
  132. if (conf) {
  133. if (conf.type === LayerType.Game) {
  134. return this._game_layer.getUIView(id);
  135. } else if (conf.type === LayerType.UI) {
  136. return this._ui_layer.getUIView(id);
  137. } else if (conf.type === LayerType.Notice) {
  138. return this._notice_layer.getUIView(id);
  139. } else if (conf.type === LayerType.Guide) {
  140. return this._guide_layer.getUIView(id);
  141. }
  142. }
  143. console.error("未找到对应UI:" + id);
  144. return null;
  145. }
  146. /**
  147. * 关闭所有UI
  148. */
  149. closeAllUI(withoutID: number = -1) {
  150. EventMgr.fireEvent("_Framewrok_Show_GameLayer", true);
  151. this._ui_layer.closeAll(withoutID);
  152. }
  153. closeAllGame(withoutID: number = -1) {
  154. this._game_layer.closeAll(withoutID);
  155. }
  156. }