FguiView.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { gameMethod } from "../../../common/gameMethod";
  2. import { WindowEvent } from "../../../data/const/EventConst";
  3. import { AudioConst } from "../../../data/const/TypeConst";
  4. import { ViewZOrder } from "../../../data/const/ViewZOrder";
  5. import GameDataCenter from "../../../data/GameDataCenter";
  6. import EventMng from "../../../manager/EventMng";
  7. import { LoadingAssetItem, ResCollector } from "../../compment/ResCollector";
  8. import Context from "../Context";
  9. import FguiMgr from "../FguiMgr";
  10. import { ILateUpdate } from "../Interface/ILateUpdate";
  11. import { IUpdate } from "../Interface/IUpdate";
  12. import { FguiViewModel } from "./FguiViewModel";
  13. import { IUIController } from "./IUIController";
  14. export interface FguiClass<T extends FguiView> {
  15. new(): T;
  16. /**
  17. * fgui的包名
  18. */
  19. getPkgName(): string;
  20. /**
  21. * fgui包内的UI名字
  22. */
  23. getPrefabName(): string;
  24. /**
  25. * 类名,用于给UI命名
  26. */
  27. getViewName(): string;
  28. }
  29. export enum ViewType {
  30. None,
  31. /** 非全屏or半屏 */
  32. Part,
  33. /** 全屏 */
  34. Full,
  35. }
  36. export abstract class FguiView implements IUpdate, ILateUpdate {
  37. //#region 静态函数
  38. /**
  39. * fgui的包名
  40. */
  41. protected static pkgName;
  42. /**
  43. * fgui包内的UI名字
  44. */
  45. protected static prefabName;
  46. /**
  47. * 类名,用于给UI命名
  48. */
  49. protected static viewName;
  50. public static getPkgName(): string {
  51. return this.pkgName;
  52. }
  53. public static getPrefabName(): string {
  54. return this.prefabName;
  55. }
  56. public static getViewName(): string {
  57. return this.viewName;
  58. }
  59. /** 页面类型 */
  60. public viewType: ViewType = ViewType.None;
  61. /** 全屏时隐藏页面次数 */
  62. public fullHideCount: number = 0;
  63. public addFullHideCount(num: number) {
  64. this.fullHideCount += num;
  65. if (this.fullHideCount <= 0) {
  66. this.fullHideCount = 0;
  67. this.Panel.visible = true;
  68. this.OnDisplay(true);
  69. } else {
  70. this.Panel.visible = false;
  71. this.OnDisplay(false);
  72. }
  73. }
  74. public zOrder: number;
  75. public uiPkgName: string;
  76. public getZOrder(): number {
  77. return this.zOrder;
  78. }
  79. public OnDisplay(visible: boolean) {
  80. this._uiController.OnDisplay(visible);
  81. }
  82. /**
  83. * 打开界面的动画是否结束
  84. */
  85. public isFinishOpenAni: boolean;
  86. public getOpenAniState(): boolean {
  87. return this.isFinishOpenAni;
  88. }
  89. //#endregion
  90. private _context: Context;
  91. get Context(): Context {
  92. return this._context;
  93. }
  94. private _viewModel: FguiViewModel;
  95. private _uiController: IUIController;
  96. private _panel: fgui.GComponent;
  97. public get Panel(): fgui.GComponent {
  98. return this._panel;
  99. }
  100. public get Controller(): IUIController {
  101. return this._uiController;
  102. }
  103. initForward(panel: fgui.GComponent, context: Context): void {
  104. this._panel = panel;
  105. this._context = context;
  106. this._viewModel = this.createVM(panel);
  107. let controller = this.CreateCtrl();
  108. controller[Context.CONTEXT_KEY] = this._context;
  109. controller[FguiViewModel.VIEW_MODEL_KEY] = this._viewModel;
  110. this._uiController = controller;
  111. }
  112. /**
  113. * 初始化,资源加载完成后回调
  114. */
  115. init(): void {
  116. this._uiController.OnInited();
  117. }
  118. /**
  119. * 显示,被打开时回调
  120. * @param intent 上下文
  121. */
  122. show(intent?: any): void {
  123. //避免注册没有被注销
  124. this._uiController.offEvent()
  125. this._uiController.RemoveTimers();
  126. this._uiController.RemoveAllClick();
  127. if (this.zOrder == ViewZOrder.Pop) {
  128. //默认打开动画、音效
  129. GameDataCenter.audio.playEffect(AudioConst.effect_open)
  130. this.isFinishOpenAni = false
  131. let animNode = this._panel.node
  132. animNode.anchorX = 0.5
  133. animNode.anchorY = 0.5
  134. cc.Tween.stopAllByTarget(animNode)
  135. animNode.scale = 0.9
  136. cc.tween(animNode).to(0.1, { scale: 1 })
  137. .call(() => {
  138. this.isFinishOpenAni = true
  139. }).start()
  140. } else {
  141. this.isFinishOpenAni = true
  142. }
  143. this._panel.node.active = true;
  144. this._uiController.ThirdPkgs.forEach(element => {
  145. FguiMgr.Instance.AddPkgCount(element);
  146. });
  147. this._uiController.OnShow(intent);
  148. }
  149. /**
  150. * 隐藏,被关闭时回调
  151. */
  152. hide(isDispose?: boolean): void {
  153. if (this.zOrder == ViewZOrder.Pop) {
  154. //默认关闭动画、音效
  155. GameDataCenter.audio.playEffect(AudioConst.effect_open)
  156. }
  157. this._uiController.offEvent()
  158. this._uiController.OnHide(isDispose);
  159. setTimeout(() => {
  160. this._uiController.ThirdPkgs.forEach(element => {
  161. FguiMgr.Instance.DelPkgCount(element);
  162. });
  163. }, 1000);
  164. this._panel.node.active = false;
  165. this._uiController.RemoveTimers();
  166. this._uiController.RemoveAllClick();
  167. EventMng.emit(WindowEvent.CLOSE_UI, this.Panel.node.name + "View");
  168. }
  169. /**
  170. * 释放节点,资源被卸载时回调
  171. */
  172. Dispose(): void {
  173. this._uiController.OnDestroy();
  174. this._panel = null;
  175. }
  176. //#region 接口实现
  177. OnUpdate(elapseTime: number): void {
  178. this._uiController.OnUpdate(elapseTime);
  179. }
  180. OnLateUpdate(elapseTime: number): void {
  181. this._uiController.OnLateUpdate(elapseTime);
  182. }
  183. //#endregion
  184. protected abstract createVM(panel: fgui.GComponent): FguiViewModel;
  185. protected abstract CreateCtrl(): IUIController;
  186. }