UIBase.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import EventManager from "../event/EventManager";
  2. export interface UIClass<T extends UIBase> {
  3. new(): T;
  4. getUrl(): string;
  5. getName(): string;
  6. }
  7. interface RegisterEvent {
  8. callback: Function,
  9. target?: any,
  10. playAudio?: boolean,
  11. }
  12. const PREFAB_UI_DIR = 'prefab/';
  13. const { ccclass, property } = cc._decorator;
  14. @ccclass
  15. export default abstract class UIBase extends cc.Component {
  16. protected static prefabUrl;
  17. protected static className;
  18. protected mTag: any;
  19. public get tag(): any {
  20. return this.mTag;
  21. }
  22. public set tag(value: any) {
  23. this.mTag = value;
  24. }
  25. /**
  26. * 得到prefab的路径,相对于resources目录
  27. */
  28. public static getUrl(): string {
  29. return PREFAB_UI_DIR + this.prefabUrl;
  30. }
  31. /**
  32. * 类名,用于给UI命名
  33. */
  34. public static getName(): string {
  35. return this.className;
  36. }
  37. /**通知事件列表 */
  38. private _notifyEventList: Map<string, Function>;
  39. /**点击事件列表 */
  40. private _registerEventList: Map<string, RegisterEvent>;
  41. /* ----------------------------- 以下方法不能在子类重写 ----------------------------- */
  42. /**初始化函数,在onLoad之前被调用,params为打开ui是传入的不定参数数组 */
  43. init(params) {
  44. this.onInit(params);
  45. }
  46. /**onLoad 会在组件被首次加载的时候被回调。且优先于任何start */
  47. onLoad() {
  48. console.log('zh:UIBase onLoad')
  49. this._notifyEventList = new Map<string, Function>();
  50. this._registerEventList = new Map<string, RegisterEvent>();
  51. this.onUILoad();
  52. }
  53. onDestroy() {
  54. this.onUIDestroy();
  55. }
  56. onEnable() {
  57. this.onRegisterEvent(this.node, this.touchEvent, this, false);
  58. this.onShow();
  59. }
  60. onDisable() {
  61. this.unRegisterEvent(this.node, this.touchEvent, this);
  62. this.onHide();
  63. let self = this;
  64. this._notifyEventList.forEach((f, key) => {
  65. EventManager.off(key, f, self);
  66. }, this);
  67. this._notifyEventList.clear();
  68. }
  69. /**注册notice事件,disable的时候会自动移除 */
  70. initEvent(eventName: string, cb: Function) {
  71. EventManager.on(eventName, cb, this);
  72. this._notifyEventList.set(eventName, cb);
  73. }
  74. touchEvent(event) {
  75. event.stopPropagation();
  76. }
  77. start() {
  78. this.onStart();
  79. }
  80. update(dt) {
  81. this.onUpdate(dt);
  82. }
  83. /* ---------------------------------------------------------------------------------- */
  84. onInit(params) {
  85. }
  86. onUILoad() {
  87. }
  88. onUIDestroy() {
  89. }
  90. onShow() {
  91. }
  92. onHide() {
  93. }
  94. onStart() {
  95. }
  96. onUpdate(dt) {
  97. }
  98. onClose() {
  99. }
  100. /**
  101. * 注册touch事件
  102. * @param node
  103. * @param callback
  104. * @param target
  105. * @param playAudio 是否播放音效,默认播放
  106. */
  107. onRegisterEvent(node: cc.Node, callback, target = null, playAudio = true) {
  108. if (!node) {
  109. return;
  110. }
  111. node.on(cc.Node.EventType.TOUCH_END, callback, target);
  112. this._registerEventList.set(node.name, { callback: callback, target: target, playAudio: playAudio });
  113. }
  114. unRegisterEvent(node: cc.Node, callback, target = null) {
  115. node.off(cc.Node.EventType.TOUCH_END, callback, target);
  116. }
  117. unLocalRegisterEvent( target = null) {
  118. this.node.off(cc.Node.EventType.TOUCH_END, this.touchEvent, target);
  119. }
  120. }