LayerManager.ts 5.8 KB

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