EventManager.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import BasicEvent from "../Basic/BasicEvent";
  2. import BasicObject from "../Basic/BasicObject";
  3. import Tool from "../Tool/Tool";
  4. /**
  5. * 事件管理器 - 逻辑层
  6. */
  7. export default class EventManager {
  8. /**事件名划分事件列表 */
  9. private static nameEvent: Map<string, Array<BasicEvent>>;
  10. /**事件对象划分事件列表 */
  11. private static targetEvent: Map<any, Array<BasicEvent>>;
  12. /**事件对象列表 */
  13. private static eventTarget: Map<any, Array<BasicObject>>;
  14. /**是否允许事件传递 */
  15. private static isEventPropagation: Map<any, boolean>;
  16. private constructor () {
  17. }
  18. /**创建 */
  19. public static create (): void {
  20. Tool.log("生成事件管理器");
  21. this.nameEvent = new Map<string, Array<BasicEvent>>();
  22. this.targetEvent = new Map<any, Array<BasicEvent>>();
  23. this.eventTarget = new Map<any, Array<BasicObject>>();
  24. this.isEventPropagation = new Map<any, boolean>();
  25. }
  26. /**销毁 */
  27. public static destroy () {
  28. Tool.log("销毁事件管理器");
  29. this.nameEvent.forEach((value: BasicEvent[], key: string) => {
  30. for (let index: number = 0; index < value.length; index++) {
  31. value[index] = null;
  32. }
  33. });
  34. this.nameEvent = null;
  35. this.targetEvent = null;
  36. this.eventTarget = null;
  37. this.isEventPropagation = null;
  38. }
  39. /**注册事件 */
  40. public static onEvent (event: string, target: any, callback: Function, priority?: number, once?: boolean): void {
  41. let basicEvent: BasicEvent = new BasicEvent();
  42. basicEvent.event = event;
  43. basicEvent.target = target;
  44. basicEvent.callback = callback;
  45. basicEvent.priority = priority || 0;
  46. basicEvent.once = once || false;
  47. basicEvent.stopPropagation = () => {
  48. this.isEventPropagation.set(basicEvent.event, false);
  49. };
  50. // 添加事件
  51. let nameEvent: Array<BasicEvent> = [];
  52. if (this.nameEvent.has(basicEvent.event) == false) {
  53. this.nameEvent.set(basicEvent.event, []);
  54. }
  55. nameEvent = this.nameEvent.get(basicEvent.event);
  56. nameEvent.push(basicEvent);
  57. this.nameEvent.set(basicEvent.event, nameEvent);
  58. let targetEvent: Array<BasicEvent> = [];
  59. if (this.targetEvent.has(basicEvent.target) == false) {
  60. this.targetEvent.set(basicEvent.target, []);
  61. }
  62. targetEvent = this.targetEvent.get(basicEvent.target);
  63. targetEvent.push(basicEvent);
  64. this.targetEvent.set(basicEvent.target, targetEvent);
  65. let eventTarget: Array<BasicObject> = [];
  66. if (this.eventTarget.has(target.id) == false) {
  67. this.eventTarget.set(target.id, []);
  68. }
  69. eventTarget = this.eventTarget.get(target.id);
  70. if (eventTarget.includes(target) == false) {
  71. eventTarget.push(target);
  72. this.eventTarget.set(target.id, eventTarget);
  73. }
  74. // 排序优化
  75. if (nameEvent.length > 1) {
  76. // 优先级排序
  77. nameEvent.sort((a: BasicEvent, b: BasicEvent): any => {
  78. return b.priority - a.priority;
  79. });
  80. let temp: Array<BasicObject> = [];
  81. for (let i = 0; i < nameEvent.length; i++) {
  82. for (let j = 0; j < eventTarget.length; j++) {
  83. if (nameEvent[i].target == eventTarget[j]) {
  84. temp.push(eventTarget[j]);
  85. eventTarget.splice(j, 1);
  86. break;
  87. }
  88. }
  89. }
  90. eventTarget = temp.concat(eventTarget);
  91. this.eventTarget.set(target.id, eventTarget);
  92. }
  93. }
  94. /**注销事件 */
  95. public static offEvent (event: string, target: any, callback: Function): void {
  96. let nameEvent: Array<BasicEvent> = this.nameEvent.get(event);
  97. if (nameEvent == null) {
  98. return;
  99. }
  100. for (let j: number = 0; j < nameEvent.length; j++) {
  101. let basicEvent: BasicEvent = nameEvent[j];
  102. if (callback == basicEvent.callback && basicEvent.target.id == target.id) {
  103. nameEvent.splice(j, 1);
  104. j--;
  105. let targetEvent: Array<BasicEvent> = this.targetEvent.get(basicEvent.target);
  106. if (targetEvent != null) {
  107. for (let k: number = 0; k < targetEvent.length; k++) {
  108. if (event == targetEvent[k].event && callback == basicEvent.callback || targetEvent[k].target == null) {
  109. targetEvent.splice(k, 1);
  110. k--;
  111. }
  112. }
  113. this.targetEvent.set(basicEvent.target, targetEvent);
  114. }
  115. }
  116. }
  117. }
  118. /**注销事件 */
  119. public static offEventByTarget (target: any): void {
  120. let targetEvent: Array<BasicEvent> = this.targetEvent.get(target);
  121. if (targetEvent == null) {
  122. return;
  123. }
  124. for (let j: number = 0; j < targetEvent.length; j++) {
  125. let basicEvent: BasicEvent = targetEvent[j];
  126. let nameEvent: Array<BasicEvent> = this.nameEvent.get(basicEvent.event);
  127. if (nameEvent != null) {
  128. for (let k: number = 0; k < nameEvent.length; k++) {
  129. if (basicEvent.target == nameEvent[k].target || nameEvent[k].target == null) {
  130. nameEvent.splice(k, 1);
  131. k--;
  132. }
  133. }
  134. this.nameEvent.set(basicEvent.event, nameEvent);
  135. }
  136. }
  137. this.targetEvent.delete(target);
  138. this.eventTarget.delete(target.id);
  139. }
  140. /**发送事件 */
  141. public static sendEvent (event: string, ...args: any[]): void {
  142. if (this.nameEvent == null ||this.nameEvent.has(event) == false) {
  143. return;
  144. }
  145. this.isEventPropagation.set(event, true);
  146. let nameEvent: Array<BasicEvent> = this.nameEvent.get(event).concat([]);
  147. if (nameEvent != null) {
  148. for (let index: number = 0; index < nameEvent.length; index++) {
  149. if (this.isEventPropagation.get(event) == false) {
  150. return;
  151. }
  152. let basicEvent: BasicEvent = nameEvent[index];
  153. if (basicEvent.target != null && (basicEvent.target.isEnabled == true || basicEvent.target.enabled == true)) {
  154. let _args = [].concat(args).concat([basicEvent]);
  155. basicEvent.callback.apply(basicEvent.target, _args);
  156. if (basicEvent.once == true) {
  157. this.offEvent(basicEvent.event, basicEvent.target, basicEvent.callback);
  158. }
  159. }
  160. }
  161. }
  162. }
  163. /**发送事件 */
  164. public static sendEventByTargetID (event: string, id: string, ...args: any[]): void {
  165. if (this.nameEvent == null ||this.nameEvent.has(event) == false) {
  166. return;
  167. }
  168. this.isEventPropagation.set(event, true);
  169. let eventTarget: Array<BasicObject> = this.eventTarget.get(id);
  170. for (let i in eventTarget) {
  171. let targetEvent: Array<BasicEvent> = this.targetEvent.get(eventTarget[i]);
  172. if (targetEvent != null) {
  173. for (let index: number = 0; index < targetEvent.length; index++) {
  174. let basicEvent: BasicEvent = targetEvent[index];
  175. if (event == basicEvent.event && (basicEvent.target.isEnabled == true || basicEvent.target.enabled == true)) {
  176. let _args = [].concat(args).concat([basicEvent]);
  177. basicEvent.callback.apply(basicEvent.target, _args);
  178. if (basicEvent.once == true) {
  179. this.offEvent(basicEvent.event, basicEvent.target, basicEvent.callback);
  180. }
  181. break;
  182. }
  183. }
  184. }
  185. }
  186. }
  187. }