You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
4.9 KiB
124 lines
4.9 KiB
import { _decorator, EventTarget} from "cc";
|
|
import { PREVIEW } from "cc/env";
|
|
import InstanceT from "../ExUtils/InstanceT";
|
|
|
|
|
|
/**
|
|
* 简易信息处理类
|
|
*/
|
|
@InstanceT.DecorateInstance()
|
|
export default class MesMgr extends EventTarget {
|
|
/** 单例 */
|
|
private static readonly instance: MesMgr;
|
|
//
|
|
private constructor() { super(); }
|
|
|
|
/**
|
|
* 检查 EventDispatcher 对象是否为特定事件类型注册了任何侦听器。
|
|
* @param type 事件的类型。
|
|
* @return 如果指定类型的侦听器已注册,则值为 true;否则,值为 false。
|
|
*/
|
|
public static hasListener(type: string | number): boolean {
|
|
return this.instance.hasEventListener(type.toString());
|
|
}
|
|
|
|
/**
|
|
* 派发事件。
|
|
* @param type 事件类型。
|
|
* @param value1 (可选)回调数据。<b>注意:</b>如果是需要传递多个参数 p1,p2,p3,...可以使用数组结构如:[p1,p2,p3,...] ;如果需要回调单个参数 p ,且 p 是一个数组,则需要使用结构如:[p],其他的单个参数 p ,可以直接传入参数 p。
|
|
* @return 此事件类型是否有侦听者,如果有侦听者则值为 true,否则值为 false。
|
|
*/
|
|
public static event(type: string | number, value1?: any, value2?: any, value3?: any, value4?: any,value5?: any):void {
|
|
return this.instance.emit(type.toString(), value1,value2,value3,value4,value5);
|
|
}
|
|
|
|
/**
|
|
* 使用 EventDispatcher 对象注册指定类型的事件侦听器对象,以使侦听器能够接收事件通知。
|
|
* @param type 事件的类型。
|
|
//* @param caller 事件侦听函数的执行域。
|
|
* @param listener 事件侦听函数。
|
|
* @param args (可选)事件侦听函数的回调参数。
|
|
* @return 此 EventDispatcher 对象。
|
|
*/
|
|
public static on(type: string | number, listener:(...any: any[]) => void, target?: any, useCapture?: boolean) {
|
|
return this.instance.on(type.toString(), listener,target,useCapture);
|
|
}
|
|
|
|
/**
|
|
* 使用 EventDispatcher 对象注册指定类型的事件侦听器对象,以使侦听器能够接收事件通知,此侦听事件响应一次后自动移除。
|
|
* @param type 事件的类型。
|
|
* @param target 事件侦听函数的执行域。
|
|
* @param callback 事件侦听函数。
|
|
* @return 此 EventDispatcher 对象。
|
|
*/
|
|
public static once(type: string | number, callback: (arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any) => void, target?: any) {
|
|
//
|
|
return this.instance.once(type.toString(), callback ,target);
|
|
}
|
|
|
|
/**
|
|
* 从 EventDispatcher 对象中删除侦听器。
|
|
* @param type 事件的类型。
|
|
* @param caller 事件侦听函数的执行域。
|
|
* @param listener 事件侦听函数。
|
|
* @param onceOnly (可选)如果值为 true ,则只移除通过 once 方法添加的侦听器。
|
|
* @return 此 EventDispatcher 对象。
|
|
*/
|
|
public static off(type: string | number,listener: (...any: any[]) => void, target?:any): void {
|
|
//
|
|
return this.instance.off(type.toString(), listener,target);
|
|
}
|
|
|
|
/**
|
|
* 从 EventDispatcher 对象中删除指定事件类型的所有侦听器。
|
|
* @param type (可选)事件类型,如果值为 null,则移除本对象所有类型的侦听器。
|
|
* @return 此 EventDispatcher 对象。
|
|
*/
|
|
public static offAll(type?: string | number):void {
|
|
//
|
|
return this.instance.removeAll(type.toString());
|
|
}
|
|
|
|
/**
|
|
* 移除caller为target的所有事件监听
|
|
* @param caller caller对象
|
|
*/
|
|
public static offAllCaller(caller: any): void {
|
|
//
|
|
return this.instance.targetOff(caller);
|
|
}
|
|
|
|
/**
|
|
* 检测指定事件类型是否是鼠标事件。
|
|
* @param type 事件的类型。
|
|
* @return 如果是鼠标事件,则值为 true;否则,值为 false。
|
|
*/
|
|
public static isMouseEvent(type: string | number): void {
|
|
//
|
|
// return this.instance.isMouseEvent(type.toString());
|
|
}
|
|
|
|
/**执行事件类型 如有多个 只执行其中第一个 返回携带的参数 */
|
|
public static ExecuteType(type: string, value1?: any, value2?: any, value3?: any, value4?: any):any {
|
|
let callTable = this.instance["_callbackTable"];
|
|
if(!callTable){return null;}
|
|
let typeData = callTable[type];
|
|
if(!typeData){return null;}
|
|
let callBack: any[] = typeData["callbackInfos"] || []
|
|
for (let i = 0; i < callBack.length; i++) {
|
|
const func = callBack[i].callback;
|
|
const target = callBack[i].target;
|
|
if(target){
|
|
return func.call(target, value1, value2, value3, value4);
|
|
}else{
|
|
return func(value1, value2, value3, value4);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|
|
if(PREVIEW){
|
|
window["MesManager"] = MesMgr;
|
|
}
|