UIBase.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. this._notifyEventList = new Map<string, Function>();
  49. this._registerEventList = new Map<string, RegisterEvent>();
  50. this.onUILoad();
  51. }
  52. onDestroy() {
  53. this.onUIDestroy();
  54. }
  55. onEnable() {
  56. this.onRegisterEvent(this.node, this.touchEvent, this, false);
  57. this.onShow();
  58. }
  59. onDisable() {
  60. this.unRegisterEvent(this.node, this.touchEvent, this);
  61. this.onHide();
  62. let self = this;
  63. this._notifyEventList.forEach((f, key) => {
  64. EventManager.off(key, f, self);
  65. }, this);
  66. this._notifyEventList.clear();
  67. }
  68. /**注册notice事件,disable的时候会自动移除 */
  69. initEvent(eventName: string, cb: Function) {
  70. EventManager.on(eventName, cb, this);
  71. this._notifyEventList.set(eventName, cb);
  72. }
  73. touchEvent(event) {
  74. event.stopPropagation();
  75. }
  76. start() {
  77. this.onStart();
  78. }
  79. update(dt) {
  80. this.onUpdate(dt);
  81. }
  82. /* ---------------------------------------------------------------------------------- */
  83. onInit(params) {
  84. }
  85. onUILoad() {
  86. }
  87. onUIDestroy() {
  88. }
  89. onShow() {
  90. }
  91. onHide() {
  92. }
  93. onStart() {
  94. }
  95. onUpdate(dt) {
  96. }
  97. onClose() {
  98. }
  99. /**
  100. * 注册touch事件
  101. * @param node
  102. * @param callback
  103. * @param target
  104. * @param playAudio 是否播放音效,默认播放
  105. */
  106. onRegisterEvent(node: cc.Node, callback, target = null, playAudio = true) {
  107. if (!node) {
  108. return;
  109. }
  110. node.on(cc.Node.EventType.TOUCH_END, callback, target);
  111. this._registerEventList.set(node.name, { callback: callback, target: target, playAudio: playAudio });
  112. }
  113. unRegisterEvent(node: cc.Node, callback, target = null) {
  114. node.off(cc.Node.EventType.TOUCH_END, callback, target);
  115. }
  116. unLocalRegisterEvent( target = null) {
  117. this.node.off(cc.Node.EventType.TOUCH_END, this.touchEvent, target);
  118. }
  119. }