EventDispatcher.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { _decorator, Component, Node } from 'cc';
  2. import { IEventDispatcher } from './IEventDispatcher';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * 事件分发器
  6. */
  7. export class EventDispatcher implements IEventDispatcher {
  8. /**
  9. * 对象已经注册的处理器
  10. */
  11. private callerMap:Map<any,EventHandler[]>=new Map<any,EventHandler[]>();
  12. /**
  13. * 事件派发器上所监听的处理器
  14. */
  15. private keyMap:Map<string,EventHandler[]>=new Map<string,EventHandler[]>();
  16. /**
  17. * 添加事件
  18. * @param key
  19. * @param caller
  20. * @param func
  21. * @param priority 优先级(数字越小优先级越高)
  22. */
  23. AddEvent(key:string,caller:any,func:Function,priority:number):void{
  24. let handlerList:EventHandler[];
  25. let handler:EventHandler;
  26. if(this.keyMap.has(key)){
  27. handlerList=this.keyMap.get(key);
  28. for (const iterator of handlerList) {
  29. if(iterator.target==caller&&iterator.func==func){
  30. console.error("重复添加同一个事件监听:"+key+" "+caller+" "+func);
  31. }
  32. return;
  33. }
  34. }else{
  35. handlerList=[];
  36. this.keyMap.set(key,handlerList);
  37. }
  38. handler=new EventHandler(key,caller,func);
  39. handlerList.push(handler);
  40. //按照优先级排序
  41. handlerList.sort((a,b)=>a.priority - priority);
  42. //处理器关联处理
  43. if(this.callerMap.has(caller)){
  44. handlerList=this.callerMap.get(caller);
  45. for (const iterator of handlerList) {
  46. if(iterator.key==key&&iterator.func==func)
  47. {
  48. console.error("事件系统 处理器关联错误:"+key+" "+caller+" "+func);
  49. }
  50. }
  51. }else{
  52. handlerList=[];
  53. this.callerMap.set(caller,handlerList);
  54. }
  55. handlerList.push(handler);
  56. }
  57. /**
  58. * 删除事件监听
  59. * @param key
  60. * @param caller
  61. * @param func
  62. */
  63. RemoveEvent(key:string,caller:any,func:Function):void{
  64. if(this.keyMap.has(key)==false){
  65. return;
  66. }
  67. let handlerList:EventHandler[]=this.keyMap.get(key);
  68. let handler:EventHandler;
  69. let deleteHandler:EventHandler;
  70. //删除
  71. for (let index = 0; index < handlerList.length; index++) {
  72. handler = handlerList[index];
  73. if(handler.target==caller&&handler.func==func){
  74. deleteHandler=handler;
  75. handlerList.splice(index,1);
  76. break;
  77. }
  78. }
  79. handlerList=this.callerMap.get(caller);
  80. //删除
  81. for (let index = 0; index < handlerList.length; index++) {
  82. handler = handlerList[index];
  83. if(handler.key==key&&handler.func==func){
  84. deleteHandler=handler;
  85. handlerList.splice(index,1);
  86. break;
  87. }
  88. }
  89. //销毁处理器
  90. deleteHandler.Destroy();
  91. }
  92. /**
  93. * 删除指定对象所有的事件处理
  94. * @param caller
  95. */
  96. RemoveEventByCaller(caller:any):void{
  97. let handlerList:EventHandler[]=this.callerMap.get(caller);
  98. let handler:EventHandler;
  99. //逐个删除
  100. while (handlerList.length) {
  101. handler=handlerList[0];
  102. this.RemoveEvent(handler.key,handler.target,handler.func);
  103. }
  104. //删除空列表
  105. this.callerMap.delete(caller);
  106. }
  107. /**
  108. * 删除所有事件监听
  109. */
  110. RemoveAllEvent():void{
  111. this.keyMap.forEach(handlerList => {
  112. handlerList.forEach(handler => {
  113. handler.Destroy();
  114. });
  115. });
  116. this.keyMap.clear();
  117. this.callerMap.clear();
  118. }
  119. /**
  120. * 派发事件
  121. * @param key
  122. * @param data
  123. */
  124. DispatchEvent(key:string,data?:any):void{
  125. if(this.keyMap.has(key)==false){
  126. return;
  127. }
  128. let handlerList:EventHandler[]=this.keyMap.get(key);
  129. handlerList.forEach(handler => {
  130. handler.func.apply(handler.target,data);
  131. });
  132. }
  133. /**
  134. * 是否有事件监听
  135. * @param key
  136. */
  137. HasEvent(key:string):boolean{
  138. return this.keyMap.has(key);
  139. }
  140. /**
  141. * 是否包含指定函数事件监听
  142. * @param key
  143. * @param caller
  144. * @param func
  145. */
  146. HasEventHandler(key:string,caller:any,func:Function):boolean{
  147. if(this.keyMap.has(key)==false){
  148. return false;
  149. }
  150. let handlerList:EventHandler[]=this.keyMap.get(key);
  151. let handler:EventHandler;
  152. for (let index = 0; index < handlerList.length; index++) {
  153. handler = handlerList[index];
  154. if(handler.target==caller&&handler.func==func){
  155. return true;
  156. }
  157. }
  158. return false;
  159. }
  160. }
  161. class EventHandler{
  162. key:string;
  163. target:any;
  164. func:Function;
  165. priority:number;
  166. constructor(key:string,target:any,func:Function){
  167. this.key=key;
  168. this.target=target;
  169. this.func=func;
  170. }
  171. Destroy(){
  172. this.target=null;
  173. this.key=null;
  174. this.func=null;
  175. }
  176. }