import BasicEvent from "../Basic/BasicEvent"; import BasicObject from "../Basic/BasicObject"; import Tool from "../Tool/Tool"; /** * 事件管理器 - 逻辑层 */ export default class EventManager { /**事件名划分事件列表 */ private static nameEvent: Map>; /**事件对象划分事件列表 */ private static targetEvent: Map>; /**事件对象列表 */ private static eventTarget: Map>; /**是否允许事件传递 */ private static isEventPropagation: Map; private constructor () { } /**创建 */ public static create (): void { Tool.log("生成事件管理器"); this.nameEvent = new Map>(); this.targetEvent = new Map>(); this.eventTarget = new Map>(); this.isEventPropagation = new Map(); } /**销毁 */ public static destroy () { Tool.log("销毁事件管理器"); this.nameEvent.forEach((value: BasicEvent[], key: string) => { for (let index: number = 0; index < value.length; index++) { value[index] = null; } }); this.nameEvent = null; this.targetEvent = null; this.eventTarget = null; this.isEventPropagation = null; } /**注册事件 */ public static onEvent (event: string, target: any, callback: Function, priority?: number, once?: boolean): void { let basicEvent: BasicEvent = new BasicEvent(); basicEvent.event = event; basicEvent.target = target; basicEvent.callback = callback; basicEvent.priority = priority || 0; basicEvent.once = once || false; basicEvent.stopPropagation = () => { this.isEventPropagation.set(basicEvent.event, false); }; // 添加事件 let nameEvent: Array = []; if (this.nameEvent.has(basicEvent.event) == false) { this.nameEvent.set(basicEvent.event, []); } nameEvent = this.nameEvent.get(basicEvent.event); nameEvent.push(basicEvent); this.nameEvent.set(basicEvent.event, nameEvent); let targetEvent: Array = []; if (this.targetEvent.has(basicEvent.target) == false) { this.targetEvent.set(basicEvent.target, []); } targetEvent = this.targetEvent.get(basicEvent.target); targetEvent.push(basicEvent); this.targetEvent.set(basicEvent.target, targetEvent); let eventTarget: Array = []; if (this.eventTarget.has(target.id) == false) { this.eventTarget.set(target.id, []); } eventTarget = this.eventTarget.get(target.id); if (eventTarget.includes(target) == false) { eventTarget.push(target); this.eventTarget.set(target.id, eventTarget); } // 排序优化 if (nameEvent.length > 1) { // 优先级排序 nameEvent.sort((a: BasicEvent, b: BasicEvent): any => { return b.priority - a.priority; }); let temp: Array = []; for (let i = 0; i < nameEvent.length; i++) { for (let j = 0; j < eventTarget.length; j++) { if (nameEvent[i].target == eventTarget[j]) { temp.push(eventTarget[j]); eventTarget.splice(j, 1); break; } } } eventTarget = temp.concat(eventTarget); this.eventTarget.set(target.id, eventTarget); } } /**注销事件 */ public static offEvent (event: string, target: any, callback: Function): void { let nameEvent: Array = this.nameEvent.get(event); if (nameEvent == null) { return; } for (let j: number = 0; j < nameEvent.length; j++) { let basicEvent: BasicEvent = nameEvent[j]; if (callback == basicEvent.callback && basicEvent.target.id == target.id) { nameEvent.splice(j, 1); j--; let targetEvent: Array = this.targetEvent.get(basicEvent.target); if (targetEvent != null) { for (let k: number = 0; k < targetEvent.length; k++) { if (event == targetEvent[k].event && callback == basicEvent.callback || targetEvent[k].target == null) { targetEvent.splice(k, 1); k--; } } this.targetEvent.set(basicEvent.target, targetEvent); } } } } /**注销事件 */ public static offEventByTarget (target: any): void { let targetEvent: Array = this.targetEvent.get(target); if (targetEvent == null) { return; } for (let j: number = 0; j < targetEvent.length; j++) { let basicEvent: BasicEvent = targetEvent[j]; let nameEvent: Array = this.nameEvent.get(basicEvent.event); if (nameEvent != null) { for (let k: number = 0; k < nameEvent.length; k++) { if (basicEvent.target == nameEvent[k].target || nameEvent[k].target == null) { nameEvent.splice(k, 1); k--; } } this.nameEvent.set(basicEvent.event, nameEvent); } } this.targetEvent.delete(target); this.eventTarget.delete(target.id); } /**发送事件 */ public static sendEvent (event: string, ...args: any[]): void { if (this.nameEvent == null ||this.nameEvent.has(event) == false) { return; } this.isEventPropagation.set(event, true); let nameEvent: Array = this.nameEvent.get(event).concat([]); if (nameEvent != null) { for (let index: number = 0; index < nameEvent.length; index++) { if (this.isEventPropagation.get(event) == false) { return; } let basicEvent: BasicEvent = nameEvent[index]; if (basicEvent.target != null && (basicEvent.target.isEnabled == true || basicEvent.target.enabled == true)) { let _args = [].concat(args).concat([basicEvent]); basicEvent.callback.apply(basicEvent.target, _args); if (basicEvent.once == true) { this.offEvent(basicEvent.event, basicEvent.target, basicEvent.callback); } } } } } /**发送事件 */ public static sendEventByTargetID (event: string, id: string, ...args: any[]): void { if (this.nameEvent == null ||this.nameEvent.has(event) == false) { return; } this.isEventPropagation.set(event, true); let eventTarget: Array = this.eventTarget.get(id); for (let i in eventTarget) { let targetEvent: Array = this.targetEvent.get(eventTarget[i]); if (targetEvent != null) { for (let index: number = 0; index < targetEvent.length; index++) { let basicEvent: BasicEvent = targetEvent[index]; if (event == basicEvent.event && (basicEvent.target.isEnabled == true || basicEvent.target.enabled == true)) { let _args = [].concat(args).concat([basicEvent]); basicEvent.callback.apply(basicEvent.target, _args); if (basicEvent.once == true) { this.offEvent(basicEvent.event, basicEvent.target, basicEvent.callback); } break; } } } } } }