EventDispatcher.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. return;
  32. }
  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. let handler:EventHandler;
  130. for (let index = 0; index < handlerList.length; index++) {
  131. handler = handlerList[index];
  132. handler.func.apply(handler.target,[data]);
  133. }
  134. }
  135. /**
  136. * 是否有事件监听
  137. * @param key
  138. */
  139. HasEvent(key:string):boolean{
  140. return this.keyMap.has(key);
  141. }
  142. /**
  143. * 是否包含指定函数事件监听
  144. * @param key
  145. * @param caller
  146. * @param func
  147. */
  148. HasEventHandler(key:string,caller:any,func:Function):boolean{
  149. if(this.keyMap.has(key)==false){
  150. return false;
  151. }
  152. let handlerList:EventHandler[]=this.keyMap.get(key);
  153. let handler:EventHandler;
  154. for (let index = 0; index < handlerList.length; index++) {
  155. handler = handlerList[index];
  156. if(handler.target==caller&&handler.func==func){
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. }
  163. class EventHandler{
  164. key:string;
  165. target:any;
  166. func:Function;
  167. priority:number;
  168. constructor(key:string,target:any,func:Function){
  169. this.key=key;
  170. this.target=target;
  171. this.func=func;
  172. }
  173. Destroy(){
  174. this.target=null;
  175. this.key=null;
  176. this.func=null;
  177. }
  178. }