import Events , { EventName , preloadEvent } from "../../common/util/Events" ; import RecyclePool from "../../common/util/RecyclePool" ; import Res from "../../common/util/Res" ; import Tool from "../../common/util/Tool" ; import { ParameterData } from "../../constant/BaseConst" ; import { ResUrl } from "../../constant/ResUrl" ; import ParamItem from "./ParamItem" ; const { ccclass , property } = cc . _decorator ; @ ccclass export default class ParamCtr extends cc . Component { @ property ( cc . Node ) ParamContent : cc . Node = null ; @ property ( cc . Node ) SelectPosNode : cc . Node = null ; protected onLoad ( ) { Events . targetOn ( this ) ; } protected onEnable ( ) { let scrollView = this . getComponent ( cc . ScrollView ) ; scrollView . node . off ( cc . Node . EventType . TOUCH_START , scrollView [ '_onTouchBegan' ] , scrollView , true ) ; scrollView . node . off ( cc . Node . EventType . TOUCH_MOVE , scrollView [ '_onTouchMoved' ] , scrollView , true ) ; scrollView . node . off ( cc . Node . EventType . TOUCH_END , scrollView [ '_onTouchEnded' ] , scrollView , true ) ; scrollView . node . off ( cc . Node . EventType . TOUCH_CANCEL , scrollView [ '_onTouchCancelled' ] , scrollView , true ) ; } protected onDestroy ( ) { Events . targetOff ( this ) ; } /**
     * 导入数据
     */ public import ( arr ) { arr . forEach ( ( data ) => { let node = this . getParamItem ( ) ; this . ParamContent . addChild ( node ) ; let pi = node . getComponent ( ParamItem ) ; pi . onInit ( data . param , data . type , data . init ) ; } ) ; } /**
     * 导出数据
     */ public export ( ) { let arr = [ ] ; this . ParamContent . children . forEach ( ( e ) => { let pi = e . getComponent ( ParamItem ) ; let data = { param : pi . paramName , type : pi . type , init : pi . init } ; arr . push ( data ) ; } ) ; return arr ; } public getParamMap ( ) { let map = new Map ( ) ; this . ParamContent . children . forEach ( ( e ) => { let pi = e . getComponent ( ParamItem ) ; map . set ( pi . paramName , pi ) ; } ) ; return map ; } private getParamItem ( ) { let prefab = Res . getLoaded ( ResUrl . PREFAB . PARAM_ITEM ) ; let node = RecyclePool . get ( ParamItem ) || cc . instantiate ( prefab ) ; node . width = this . ParamContent . width ; Tool . updateWidget ( node ) ; return node ; } private putParamItem ( node ) { RecyclePool . put ( ParamItem , node ) ; } public getParamItemByName ( name ) { for ( let i = 0 ; i < this . ParamContent . childrenCount ; i ++ ) { let pi = this . ParamContent . children [ i ] . getComponent ( ParamItem ) ; if ( name === pi . paramName ) { return pi ; } } return null ; } /**
     * 获取一个不重复的参数名
     */ public getParamName ( paramItem , name = 'param' ) { let index = 0 ; let findName = false ; while ( ! findName ) { findName = true ; for ( let i = 0 ; i < this . ParamContent . childrenCount ; i ++ ) { let pi = this . ParamContent . children [ i ] . getComponent ( ParamItem ) ; if ( pi === paramItem ) { continue ; } if ( pi . paramName === `${ name }${ index > 0 ? index : '' }` ) { index ++ ; findName = false ; break ; } } } return `${ name }${ index > 0 ? index : '' }` ; } private onClickAddParam ( event , typeStr ) { Events . emit ( EventName . CLOSE_MENU ) ; let type = parseInt ( typeStr ) ; let node = this . getParamItem ( ) ; this . ParamContent . addChild ( node ) ; let pi = node . getComponent ( ParamItem ) ; pi . onInit ( this . getParamName ( pi ) , type ) ; } private onClickOpenSelect ( ) { Events . emit ( EventName . SHOW_PARAM_ADD , this . SelectPosNode . parent . convertToWorldSpaceAR ( this . SelectPosNode . position ) ) ; } @ preloadEvent ( EventName . PARAM_DELETE ) private onEventParamDelete ( item ) { this . putParamItem ( item . node ) ; } @ preloadEvent ( EventName . PARAM_SELECT ) private onEventParamSelect ( item ) { this . ParamContent . children . forEach ( ( e ) => { let ti = e . getComponent ( ParamItem ) ; ti . select ( ti === item ) ; } ) ; } @ preloadEvent ( EventName . RESIZE ) private onEventResize ( node ) { if ( node !== this . node ) { return ; } Tool . updateWidget ( this . node , this . ParamContent ) ; this . ParamContent . children . forEach ( ( e ) => { e . width = this . ParamContent . width ; Tool . updateWidget ( e ) ; } ) ; } }