/**
 * 工具类
 */ export default class Tool { param arr public static randArray < T > ( arr ) { if ( arr . length <= 0 ) { return null ; } return arr [ this . randInt ( 0 , arr . length - 1 ) ] ; } param min param max public static randInt ( min , max ) { min = Math . ceil ( min ) ; max = Math . floor ( max ) ; if ( min >= max ) { return max ; } return Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min ; } param min param max public static randFloat ( min , max ) { if ( min >= max ) { return max ; } return Math . random ( ) * ( max - min ) + min ; } param min param max param value param includeEdge public static inRange ( min , max , value , includeEdge = true ) { return includeEdge ? value >= min && value <= max : value > min && value < max ; } /**
     * 判断数组中是否有某个元素
     */ public static arrayHas < T > ( arr , ele ) { let idx = arr . findIndex ( ( e ) => { return e === ele ; } ) ; return idx >= 0 ; } /**
     * 根据下标交换数组两个元素位置
     */ public static arraySwap < T > ( arr , idx1 , idx2 ) { if ( idx1 === idx2 || ! this . inRange ( 0 , arr . length - 1 , idx1 ) || ! this . inRange ( 0 , arr . length - 1 , idx2 ) ) { return ; } [ arr [ idx1 ] , arr [ idx2 ] ] = [ arr [ idx2 ] , arr [ idx1 ] ] ; } /**
     * 将元素从fromIdx位置移到toIdx位置，其余元素相对位置不变
     */ public static arrayMove < T > ( arr , fromIdx , toIdx ) { if ( fromIdx === toIdx || ! this . inRange ( 0 , arr . length - 1 , fromIdx ) || ! this . inRange ( 0 , arr . length - 1 , toIdx ) ) { return ; } let from = arr . splice ( fromIdx , 1 ) ; arr . splice ( toIdx , 0 , from [ 0 ] ) ; } param canRepeat returns public static arrayAdd < T > ( arr , ele , canRepeat = false ) { if ( ! canRepeat && this . arrayHas ( arr , ele ) ) { return false ; } arr . push ( ele ) ; return true ; } returns public static arrayDelete < T > ( arr , ele ) { let idx = arr . findIndex ( ( e ) => { return e === ele ; } ) ; if ( idx === - 1 ) { return false ; } arr . splice ( idx , 1 ) ; return true ; } param node param ignoreList public static updateWidget ( node , ... ignoreList ) { node . children . forEach ( ( c ) => { let widget = c . getComponent ( cc . Widget ) ; widget && widget . updateAlignment ( ) ; if ( this . arrayHas ( ignoreList , c ) ) { return ; } this . updateWidget ( c , ... ignoreList ) ; } ) ; } }