/**
 * 事件名
 */ export enum EventName { /** 调节节点大小 */ RESIZE , /** 删除参数 */ PARAM_DELETE , /** 参数名更改 */ PARAM_NAME_CHANGED , /** 选中ParamItem */ PARAM_SELECT , /** 设置状态机视图的显示 */ SET_CUR_STATE_MACHINE , /** 状态名更改 */ STATE_NAME_CHANGED , /** AnyState改变坐标 */ ANY_STATE_MOVE , /** 状态机名更改 */ STATE_MACHINE_NAME_CHANGED , /** 父状态机节点改变坐标 */ UP_STATE_MACHINE_MOVE , /** 选择连线连向子状态机内部的状态 */ LINE_TO_MACHINE_STATE , /** 删除line */ LINE_DELETE , /** 新增transition */ TRANSITION_ADD , /** 删除transition */ TRANSITION_DELETE , /** 点击按钮选中TransitionItem */ TRANSITION_SELECT , /** 点击按钮选中ConditionItem */ CONDITION_SELECT , /** 选中multiplier参数 */ MULTIPLIER_SELECT , /** 隐藏inspector显示的内容 */ INSPECTOR_HIDE , /** 显示unit信息 */ INSPECTOR_SHOW_UNIT , /** 显示line信息 */ INSPECTOR_SHOW_LINE , /** 关闭菜单层 */ CLOSE_MENU , /** 显示状态机界面右键菜单 */ SHOW_RIGHT_MENU , /** 显示连线目标状态机内部所有状态选择界面 */ SHOW_LINE_TO_List , /** 显示添加参数时选择界面 */ SHOW_PARAM_ADD , /** 显示condition的参数选择界面 */ SHOW_PARAM_SELECT , /** 显示condition的logic选项 */ SHOW_LOGIC , /** 显示multiplier选择界面 */ SHOW_MULTIPLIER } ; param event export function preloadEvent ( event ) { return function ( target , funcName , desc ) { let arr = Events . classMap . get ( target . constructor ) ; if ( arr === undefined ) { arr = [ ] ; Events . classMap . set ( target . constructor , arr ) ; } else { let find = arr . find ( ( e ) => { return e . event === event && e . funcName === funcName ; } ) ; if ( find ) { cc . error ( `event: ${ EventName [ event ] } 重复载入` ) ; return ; } } arr . push ( { event : event , funcName : funcName } ) ; } ; } /**
 * 监听函数类型
 */ type Listener = ( arg : any ) => void ; /**
 * 事件收发管理类
 */ export default class Events { /**
     * 存储监听事件、监听函数与监听对象
     */ private static _eventsMap : Map < EventName , Map < Object , Listener [ ] > > = new Map ( ) ; /**
     * 存储构造函数、监听事件、监听函数名，用于实例化时注册事件
     */ public static classMap : Map < Function , Array < { event : EventName ; funcName : string ; } > > = new Map ( ) ; param target param onSuper public static targetOn ( target , onSuper = true ) { if ( onSuper ) { this . classMap . forEach ( ( value , key ) => { if ( target instanceof key ) { value . forEach ( ( e ) => { this . on ( e . event , target [ e . funcName ] , target ) ; } ) ; } } ) ; } else { let arr = this . classMap . get ( target . constructor ) ; if ( arr ) { arr . forEach ( ( e ) => { this . on ( e . event , target [ e . funcName ] , target ) ; } ) ; } } } param event param listener param target public static on ( event , listener , target ) { if ( ! listener || ! target ) { cc . error ( `event: ${ EventName [ event ] } listener或target不能为空` ) ; return ; } let map = this . _eventsMap . get ( event ) ; let list = [ ] ; if ( map === undefined ) { map = new Map ( ) ; map . set ( target , list ) ; this . _eventsMap . set ( event , map ) ; } else { list = map . get ( target ) ; if ( list === undefined ) { list = [ ] ; map . set ( target , list ) ; } else { let result = list . find ( ( e ) => { return e === listener ; } ) ; if ( result ) { cc . error ( `event: ${ EventName [ event ] } 重复注册` ) ; return ; } } } list . push ( listener ) ; } param event param listener param target public static off ( event , listener , target ) { if ( ! listener || ! target ) { cc . error ( `event: ${ EventName [ event ] } listener或target不能为空` ) ; return ; } let map = this . _eventsMap . get ( event ) ; if ( map === undefined ) { cc . error ( `event: ${ EventName [ event ] } 未注册该事件` ) ; return ; } let list = map . get ( target ) ; if ( list === undefined ) { cc . error ( `event: ${ EventName [ event ] } target上未注册该事件` ) ; return ; } let index = list . findIndex ( ( e ) => { return e === listener ; } ) ; if ( index < 0 ) { cc . error ( `event: ${ EventName [ event ] } target上未以该listener注册该事件` ) ; return ; } list . splice ( index , 1 ) ; if ( list . length <= 0 ) { map . delete ( target ) ; map . size <= 0 && this . _eventsMap . delete ( event ) ; } } param target public static targetOff ( target ) { if ( ! target ) { cc . error ( `event: ${ target } target不能为空` ) ; return ; } this . _eventsMap . forEach ( ( map , event ) => { map . delete ( target ) ; map . size <= 0 && this . _eventsMap . delete ( event ) ; } ) ; } param event param args public static emit ( event , ... args ) { let map = this . _eventsMap . get ( event ) ; if ( map === undefined ) { cc . warn ( `event: ${ EventName [ event ] } 未注册该事件` ) ; return ; } map . forEach ( ( list , target ) => { list . forEach ( ( listener ) => { listener . call ( target , ... args ) ; } ) ; } ) ; } }