UIBase.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { AudioConst } from "../../data/const/TypeConst";
  2. import GameDataCenter from "../../data/GameDataCenter";
  3. import EventMng from "../../manager/EventMng";
  4. import { uiCommon } from "../../utils/UICommon";
  5. export interface UIClass<T extends UIBase> {
  6. new(): T;
  7. getUrl(): string;
  8. getName(): string;
  9. getPop(): BASE_POP;
  10. getZindex(): number;
  11. /** 预加载页面所需资源 */
  12. onCollectRes(): void;
  13. }
  14. // 弹出方式
  15. export enum BASE_POP {
  16. MAIN = "MAIN",//主场景
  17. UI = "UI", //除了MAIN以外的其他非弹窗UI
  18. UI_HALF = "UI_HALF",// 与UI类似,但不展示底部按钮,只显示顶部资源栏
  19. FULL = "FULL",// 盖住菜单界面的窗口
  20. POP = "POP", // 弹窗
  21. MemuBottom = "MemuBottom", // 主界面的底部菜单栏ui
  22. Battle = "Battle" // 战斗弹窗
  23. }
  24. interface RegisterEvent {
  25. callback: Function,
  26. target?: any,
  27. playAudio?: boolean,
  28. }
  29. const PREFAB_UI_DIR = 'prefab/';
  30. const { ccclass, property } = cc._decorator;
  31. @ccclass
  32. export default abstract class UIBase extends cc.Component {
  33. protected static prefabUrl;
  34. protected static className;
  35. static onCollectRes() {
  36. }
  37. protected mTag: any;
  38. public get tag(): any {
  39. return this.mTag;
  40. }
  41. public set tag(value: any) {
  42. this.mTag = value;
  43. }
  44. /**
  45. * 得到prefab的路径,相对于resources目录
  46. */
  47. public static getUrl(): string {
  48. return this.prefabUrl;
  49. //return PREFAB_UI_DIR + this.prefabUrl;
  50. }
  51. /**
  52. * 类名,用于给UI命名
  53. */
  54. public static getName(): string {
  55. return this.className;
  56. }
  57. protected static pop: BASE_POP = BASE_POP.UI
  58. public static getPop(): BASE_POP {
  59. return this.pop
  60. }
  61. protected static _zindex: number
  62. public static getZindex(): number {
  63. return this._zindex
  64. }
  65. public belong: any = null
  66. public showAnim: boolean = true
  67. public bgm: AudioConst = AudioConst.empty
  68. /** 是否注册防穿透事件 */
  69. protected isRegisterStopEvent: boolean = true;
  70. /**通知事件列表 */
  71. private _notifyEventList: Map<string, Function>;
  72. /**点击事件列表 */
  73. private _registerEventList: Map<string, RegisterEvent>;
  74. /* ----------------------------- 以下方法不能在子类重写 ----------------------------- */
  75. /**初始化函数,在onLoad之前被调用,params为打开ui是传入的不定参数数组 */
  76. init(params) {
  77. this.onInit(params);
  78. }
  79. /**onLoad 会在组件被首次加载的时候被回调。且优先于任何start */
  80. onLoad() {
  81. this._notifyEventList = new Map<string, Function>();
  82. // this._registerEventList = new Map<string, RegisterEvent>();
  83. this.onUILoad();
  84. }
  85. onDestroy() {
  86. this.onUIDestroy();
  87. }
  88. onEnable() {
  89. // if (this.isRegisterStopEvent) {
  90. // this.onRegisterEvent(this.node, this.touchEvent, this);
  91. // }
  92. if (this.bgm != AudioConst.empty) {
  93. GameDataCenter.audio.playMusic(this.bgm)
  94. }
  95. this.onShow();
  96. }
  97. onDisable() {
  98. if (this.isRegisterStopEvent) {
  99. this.unRegisterEvent(this.node);
  100. }
  101. this.onHide();
  102. try {
  103. let self = this;
  104. this._notifyEventList.forEach((f, key) => {
  105. // console.log("_notifyEventList key = "+key)
  106. EventMng.off(key, f, self);
  107. }, this);
  108. this._notifyEventList.clear();
  109. } catch (error) {
  110. console.error("onDisable->" + error)
  111. }
  112. if (this.registerCount > 0) {
  113. console.error(this.name + " 按钮注册后没off掉啊喂 \n(╯-_-)╯╧╧ ")
  114. }
  115. }
  116. /**注册notice事件,disable的时候会自动移除 */
  117. initEvent(eventName: string, cb: Function) {
  118. EventMng.on(eventName, cb, this);
  119. this._notifyEventList.set(eventName, cb);
  120. }
  121. private touchEvent(event) {
  122. event.stopPropagation();
  123. }
  124. start() {
  125. // this.onStart(); // 改在UiMng中,在init和添加到父节点之后触发
  126. }
  127. update(dt) {
  128. this.onUpdate(dt);
  129. }
  130. /* ---------------------------------------------------------------------------------- */
  131. onInit(params) {
  132. }
  133. onUILoad() {
  134. }
  135. onUIDestroy() {
  136. }
  137. onShow() {
  138. }
  139. onHide() {
  140. }
  141. onStart(isFirstTime: boolean) {
  142. }
  143. onUpdate(dt: number) {
  144. }
  145. onClose() {
  146. }
  147. registerCount: number = 0
  148. /**
  149. * 注册touch事件
  150. * @param node
  151. * @param callback
  152. * @param target
  153. * @param playAudio 是否播放音效,默认播放
  154. */
  155. onRegisterEvent(node: cc.Node, callback, target = null, params: any = [], audio: AudioConst = AudioConst.effect_click) {
  156. this.registerCount++
  157. uiCommon.onRegisterEvent(node, callback, target, params, audio)
  158. }
  159. unRegisterEvent(node: cc.Node) {
  160. this.registerCount--
  161. uiCommon.unRegisterEvent(node)
  162. }
  163. }