123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import { Component } from "cc";
- export class EventManager {
-
- private _events = new Map<string, { id: string, func: Function, target: any }[]>();
-
- private _targets = new Set<string>();
-
- private _onceEvents = new Map<string, { id: string, func: Function, target: any }[]>();
-
- private _onceTargets = new Set<string>();
- constructor() {
- this._events.clear();
- this._targets.clear();
- this._onceEvents.clear();
- this._onceTargets.clear();
- }
-
- addEvent(name: string, callback: (...val: any[]) => void, target: any, uuid: string = null) {
- if (!name || !callback || !target) {
- console.error("添加事件失败name,callback或者target为空.");
- return false;
- }
- if (!this._events.has(name)) {
- this._events.set(name, []);
- }
- if (!uuid) {
- uuid = target.node.uuid;
- }
- this._events.get(name).push({ id: uuid, func: callback.bind(target), target: target });
- if (!this._targets.has(uuid)) {
- this._targets.add(uuid);
- }
- return true;
- }
-
- onceEvent(name: string, callback: (...val: any[]) => void, target: any, uuid: string = null) {
- if (!name || !callback || !target) {
- console.error("添加事件失败name,callback或者target为空.");
- return false;
- }
- if (!this._onceEvents.has(name)) {
- this._onceEvents.set(name, []);
- }
- if (!uuid) {
- uuid = target.node.uuid;
- }
- this._onceEvents.get(name).push({ id: uuid, func: callback.bind(target), target: target });
-
- if (!this._onceTargets.has(uuid)) {
- this._onceTargets.add(uuid);
- }
- return true;
- }
-
- fireEvent(name: string, ...val: any[]) {
- let event = this._events.get(name);
- if (event) {
- for (let i = event.length - 1; i >= 0; --i) {
- if (event[i].target instanceof Component) {
- if (event[i].target.isValid) {
- event[i].func(...val);
- } else {
- event.splice(i, 1);
- }
- } else {
- event[i].func(...val);
- }
- }
- }
- let onceEvent = this._onceEvents.get(name);
- if (onceEvent) {
- for (let i = onceEvent.length - 1; i >= 0; --i) {
- if (onceEvent[i].target instanceof Component) {
- if (onceEvent[i].target.isValid) {
- onceEvent[i].func(...val);
- }
- } else {
- onceEvent[i].func(...val);
- }
- }
- this._onceEvents.delete(name);
- }
- }
-
- removeEvent(target: Component | string) {
- let uuid = (typeof target === "string") ? target : target?.node?.uuid;
- if (this._targets.has(uuid)) {
- this._targets.delete(uuid);
- this._events.forEach((events, name) => {
- for (let i = events.length - 1; i >= 0; --i) {
- if (events[i].id === uuid) {
- events.splice(i, 1);
- }
- }
- if (events.length === 0) {
- this._events.delete(name);
- }
- });
- }
- if (this._onceTargets.has(uuid)) {
- this._onceTargets.delete(uuid);
- this._onceEvents.forEach((events, name) => {
- for (let i = events.length - 1; i >= 0; --i) {
- if (events[i].id === uuid) {
- events.splice(i, 1);
- }
- }
- if (events.length === 0) {
- this._onceEvents.delete(name);
- }
- });
- }
- }
-
- removeAll() {
- this._events.clear();
- this._targets.clear();
- this._onceEvents.clear();
- this._onceTargets.clear();
- }
- }
- export let EventMgr = new EventManager;
|