FguiViewCtrl.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { AudioConst } from "../../../data/const/TypeConst";
  2. import GameDataCenter from "../../../data/GameDataCenter";
  3. import EventMng from "../../../manager/EventMng";
  4. import { ResCollector } from "../../compment/ResCollector";
  5. import Context from "../Context";
  6. import { FguiViewModel } from "./FguiViewModel";
  7. import { IUIController } from "./IUIController";
  8. export interface BtnEvent {
  9. btn: fairygui.GObject,
  10. func: Function
  11. }
  12. export abstract class FguiViewCtrl<T extends FguiViewModel> implements IUIController {
  13. ThirdPkgs: string[] = [];
  14. /**通知事件列表 */
  15. private _notifyEventList: Map<string, Function> = new Map<string, Function>;
  16. /** 定时器集合 */
  17. private m_Timers: Set<NodeJS.Timer> = new Set<NodeJS.Timer>();
  18. protected get Context(): Context {
  19. return this[Context.CONTEXT_KEY] as Context;
  20. }
  21. protected get VM(): T {
  22. return this[FguiViewModel.VIEW_MODEL_KEY] as T;
  23. }
  24. /**
  25. * UI初始化或者一些无法通过代码生成绑定的节点
  26. * 只执行一次
  27. */
  28. abstract OnInited(): void;
  29. abstract OnShow(intent?: any): void;
  30. abstract OnHide(isDispose?: boolean): void;
  31. OnDisplay(visible: boolean): void {
  32. }
  33. onCollectRes(resCollector: ResCollector, param: any): void {
  34. }
  35. GetThirdPkgs(): string[] {
  36. return this.ThirdPkgs;
  37. }
  38. OnUpdate(elapseTime: number): void {
  39. }
  40. OnLateUpdate(elapseTime: number): void {
  41. }
  42. OnDestroy(): void {
  43. }
  44. offEvent() {
  45. try {
  46. let self = this;
  47. this._notifyEventList.forEach((f, key) => {
  48. EventMng.off(key, f, self);
  49. }, this);
  50. this._notifyEventList.clear();
  51. } catch (error) {
  52. console.error("onDisable->" + error)
  53. }
  54. }
  55. /**注册notice事件,disable的时候会自动移除 */
  56. initEvent(eventName: string, cb: Function) {
  57. EventMng.on(eventName, cb, this);
  58. this._notifyEventList.set(eventName, cb);
  59. }
  60. /**
  61. * 增加定时器
  62. * @param callback
  63. * @param delta 单位毫秒ms
  64. */
  65. AddTimer(callback: Function, delta: number) {
  66. let timer = setInterval(() => {
  67. callback.apply(this);
  68. }, delta);
  69. this.m_Timers.add(timer);
  70. return timer;
  71. }
  72. /** 移除所有定时器 */
  73. RemoveTimers() {
  74. this.m_Timers.forEach(element => {
  75. clearInterval(element);
  76. });
  77. this.m_Timers.clear();
  78. }
  79. /**
  80. * 一次性定时器
  81. * @param callback
  82. * @param delta 单位毫秒ms
  83. */
  84. AddTimerOnce(callback: Function, delta: number) {
  85. let timer = setInterval(() => {
  86. clearInterval(timer)
  87. this.m_Timers.delete(timer);
  88. callback();
  89. }, delta);
  90. this.m_Timers.add(timer);
  91. return timer
  92. }
  93. /** 按钮点击注册器 */
  94. private btnClicks: BtnEvent[] = [];
  95. RegisterClick(btn: fairygui.GObject, func: Function, params: any = [], audio: AudioConst = AudioConst.effect_click) {
  96. let target = this
  97. let call = (event: any) => {
  98. GameDataCenter.audio.playEffect(audio)
  99. func.apply(target, [event, params])
  100. }
  101. btn.onClick(call, this);
  102. this.btnClicks.push({ btn: btn, func: call });
  103. }
  104. RemoveAllClick() {
  105. this.btnClicks.forEach(element => {
  106. element.btn.offClick(element.func, this);
  107. });
  108. this.btnClicks = []
  109. }
  110. public CloseUI() {
  111. }
  112. }