author suo
  module FishingJoy { export class UIUtil { public constructor ( ) { } param contentBox param item param num param col param spacing param lending param offsetX param offsetY return public static setItemGridLayout ( contentBox , item , num , col , spacing = 0 , lending = 0 , offsetX = 0 , offsetY = 0 ) { for ( var i = 0 ; i < num ; i ++ ) { var pos = this . getGridItemPos ( i , item , col , spacing , lending , offsetX , offsetY ) ; item . x = pos [ 0 ] ; item . y = pos [ 1 ] ; contentBox . addChild ( item ) ; } } /**
         * 传入索引 获得该Index Item坐标
         */ public static getGridItemPos ( index , item , col , spacing = 0 , lending = 0 , offsetX = 0 , offsetY = 0 ) { /**列数量*/ var x = ( index % col ) * ( item . width + spacing ) + offsetX ; var y = Math . floor ( index / col ) * ( item . height + lending ) + offsetY ; return [ x , y ] ; } /**
         * 置灰
         */ public static setGray ( target ) { var matrix = [ 0.3086 , 0.6094 , 0.0820 , 0 , 0 , 0.3086 , 0.6094 , 0.0820 , 0 , 0 , 0.3086 , 0.6094 , 0.0820 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ; var stateFilter = new egret . ColorMatrixFilter ( matrix ) ; target . filters = [ stateFilter ] ; } /**
         * 恢复正常
         */ public static setNomarl ( target ) { if ( target ) { target . filters = [ ] ; } } /**
         * 高亮
         */ public static setLight ( target ) { var matrix = [ 1 , 0 , 0 , 0 , 0xff * 0.2 , 0 , 1 , 0 , 0 , 0xe0 * 0.2 , 0 , 0 , 1 , 0 , 0x8d * 0.2 , 0 , 0 , 0 , 1 , 0 ] ; var stateFilter = new egret . ColorMatrixFilter ( matrix ) ; target . filters = [ stateFilter ] ; } /**
         * 变红色
         */ public static setRed ( target ) { var matrix = [ 1 , 0.964 , 0.999 , 0.96 , 0xff * 0.2 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ; var stateFilter = new egret . ColorMatrixFilter ( matrix ) ; target . filters = [ stateFilter ] ; } /**
         * 从显示列表内移除自身
         */ public static removeSelf ( ... rest ) { var len = rest . length ; for ( var i = 0 ; i < len ; i ++ ) { var target = rest [ i ] ; if ( egret . is ( target , "egret.DisplayObject" ) ) { if ( target . parent && target ) { target . parent . removeChild ( target ) ; } } } } /**
         * 格式化金币
         */ public static formatMoneny ( value ) { let str ; if ( value > 1000000000 ) { str = value / 1000000000 + "M" ; } else if ( value > 10000 ) { str = value / 10000 + "W" ; } else if ( value > 1000 ) { str = value / 1000 + "K" ; } else { str = value . toString ( ) ; } return str ; } /**
         * 创建影片剪辑
         */ public static creatMovieClip ( groupName , action = "action" ) { var data = RES . getRes ( groupName + "_json" ) ; var txtr = RES . getRes ( groupName + "_png" ) ; var mcFactory = new egret . MovieClipDataFactory ( data , txtr ) ; return new egret . MovieClip ( mcFactory . generateMovieClipData ( action ) ) ; } /**
         * 播放一次自动从舞台移除
         */ public static movPlayOnce ( box , mov , posX , posY , scaleX = 1 , scaleY = 1 , cb = null , delay ? ) { box . addChild ( mov ) ; mov . x = posX ; mov . y = posY ; mov . scaleX = scaleX ; mov . scaleY = scaleY ; mov . gotoAndPlay ( 1 , 1 ) ; if ( delay ) { FishingJoy . Laya . timer . once ( delay , this , ( ) => { mov . stop ( ) ; if ( mov . parent ) box . removeChild ( mov ) ; if ( cb ) { cb . run ( ) ; } } ) ; } else { mov . once ( egret . MovieClipEvent . COMPLETE , ( ) => { if ( mov ) { mov . stop ( ) ; if ( mov . parent ) box . removeChild ( mov ) ; } if ( cb ) { cb . run ( ) ; } } , this ) ; } } /**
         * 循环加法
         */ public static circleAdd ( value , addNum , maxNum ) { value += addNum ; if ( value > maxNum ) { value -= maxNum + 1 ; } return value ; } /**
         * 创建龙骨
         */ public static creatDragonbones ( groupName , armatureName = "armatureName" ) { let egretFactory = dragonBones . EgretFactory . factory ; if ( egretFactory . getDragonBonesData ( groupName ) == null ) { var dragonbonesData = RES . getRes ( "test_ske_dbbin" ) ; var textureData = RES . getRes ( "test_tex_json" ) ; var texture = RES . getRes ( "test_tex_png" ) ; egretFactory . parseDragonBonesData ( dragonbonesData , groupName ) ; egretFactory . parseTextureAtlasData ( textureData , texture , groupName ) ; } let armatureDisplay = egretFactory . buildArmatureDisplay ( armatureName , groupName ) ; return armatureDisplay ; } /**
         * 是否在边界内
         */ public static inBorder ( x , y ) { if ( x < - 100 || x > uniLib . Global . screenWidth + 100 ) { return false ; } if ( y < - 100 || y > uniLib . Global . screenHeight + 100 ) { return false ; } return true ; } /**
         * 在视图内
         */ public static inView ( x , y ) { if ( x < 0 || x > uniLib . Global . screenWidth ) { return false ; } if ( y < 0 || y > uniLib . Global . screenHeight ) { return false ; } return true ; } /**
         * 在瞄准范围内
         */ public static inAimView ( x , y ) { if ( x < 30 || x > uniLib . Global . screenWidth - 30 ) { return false ; } if ( y < 30 || y > uniLib . Global . screenHeight - 30 ) { return false ; } return true ; } /**
         * 在瞄准范围内
         */ public static inAimView2 ( x , y ) { if ( x < 200 || x > uniLib . Global . screenWidth - 200 ) { return false ; } if ( y < 200 || y > uniLib . Global . screenHeight - 200 ) { return false ; } return true ; } /**
         * 通过角度获得弧度
         */ public static getRadian ( angle ) { return angle * Math . PI / 180 ; } /**
         * 通过弧度获得角度
         */ public static getAngle ( radian ) { return radian * 180 / Math . PI ; } /**
         * 设置对象跳跃动画
         */ public static setJumpTween ( target , JumpMaxY , JumpMinY , durationTime = 1000 , waitTime = MathUtil . random ( 0 , 3000 ) ) { egret . Tween . get ( target ) . to ( { scaleX : 1 , scaleY : 1 , y : JumpMinY } , durationTime , egret . Ease . bounceOut ) . wait ( waitTime ) . call ( ( ) => { egret . Tween . get ( target ) . to ( { scaleX : 0.9 , scaleY : 1.1 , y : JumpMaxY } , durationTime , egret . Ease . bounceIn ) . call ( this . setJumpTween , this , [ target , JumpMaxY , JumpMinY , durationTime ] ) ; } , this ) ; } /**
         * 设置对象呼吸动画
         */ public static setBreatheTween ( target , durationTime ) { egret . Tween . get ( target ) . to ( { alpha : 0 } , durationTime ) . call ( ( ) => { egret . Tween . get ( target ) . to ( { alpha : 1 } , durationTime ) . wait ( 500 ) . call ( this . setBreatheTween , this , [ target , durationTime ] ) ; } , this ) ; } /**
         * 设置对象缩放动画
         */ public static setScaleTween ( target , scaleXGene , scaleYGene , durationTime = 600 ) { egret . Tween . get ( target ) . to ( { scaleX : 1.1 * scaleXGene , scaleY : 1.1 * scaleYGene } , durationTime ) . call ( ( ) => { egret . Tween . get ( target ) . to ( { scaleX : 0.9 * scaleXGene , scaleY : 0.9 * scaleYGene } , durationTime ) . call ( this . setScaleTween , this , [ target , scaleXGene , scaleYGene , durationTime ] ) ; } , this ) ; } /**
         * 设置对象上下移动动画
         */ public static setDownUpTween ( target , MaxY , MinY , durationTime = 1000 ) { egret . Tween . get ( target ) . to ( { y : MaxY } , durationTime ) . call ( ( ) => { egret . Tween . get ( target ) . to ( { y : MinY } , durationTime ) . call ( this . setDownUpTween , this , [ target , MaxY , MinY , durationTime ] ) ; } , this ) ; } /**
         * 局部坐标转全局
         */ public static localToGlobal ( target , localX = 0 , localY = 0 ) { let p = Point . create ( 0 , 0 ) ; p = target . localToGlobal ( localX , localY , p ) ; let x = p . x ; let y = p . y ; Point . release ( p ) ; return [ x , y ] ; } /**
         * 炮台中性点的全局坐标
         */ private static batteryFocusGlobalPoint : { [ seatId ] : [ number , number ] ; } = { } ; /**
         * 获取炮台中性点的全局坐标
         */ static getBatteryFocusGlobalPoint ( battery ) { let point = UIUtil . batteryFocusGlobalPoint [ battery . seatID ] ; if ( ! point ) { let bulletPoint = Point . create ( 0 , 0 ) ; battery . focus . localToGlobal ( 0 , 0 , bulletPoint ) ; point = [ bulletPoint . x , bulletPoint . y ] ; Point . release ( bulletPoint ) ; UIUtil . batteryFocusGlobalPoint [ battery . seatID ] = point ; } return point ; } /**
        * 相对于fish层级 炮台位置和舞台点的角度
        */ public static calculateBatteryRotation ( battery , stageX , stageY ) { let [ x , y ] = UIUtil . getBatteryFocusGlobalPoint ( battery ) ; let rotation = GX . getRadianByPoint ( { x : x , y : y } , { x : stageX , y : stageY } ) ; if ( FishingJoy . Master . instance . seatID > 2 ) { rotation += 180 ; if ( rotation > 360 ) rotation -= 360 ; } return rotation ; } /**
         * 是否能够瞄准
         */ public static isCanAim ( gameObj ) { let [ x , y ] = [ - 1 , - 1 , ] ; if ( gameObj . isAccurateCollider ) { for ( let i = 0 ; i < gameObj . colliderAry . length ; i ++ ) { let collider = gameObj . colliderAry [ i ] ; [ x , y ] = UIUtil . localToGlobal ( collider ) ; if ( UIUtil . inAimView ( x , y ) ) { return true ; } } } else { [ x , y ] = UIUtil . localToGlobal ( gameObj ) ; if ( UIUtil . inAimView ( x , y ) ) { return true ; } } return false ; } /**
         * 相对于fish层局部坐标转全局
         */ public static getGlobePos ( target ) { let p = Point . create ( 0 , 0 ) ; target . localToGlobal ( 0 , 0 , p ) ; if ( Master . instance . isRotation ) { let layer = LayerManager . instance . getLayer ( LAYER . Fish ) ; layer . localToGlobal ( p . x , p . y , p ) ; } let x = p . x ; let y = p . y ; Point . release ( p ) ; return [ x , y ] ; } /**
         * 转换金币文本
         */ public static traGoldLabel ( label ) { if ( game . Config . PublicGoldFont ) { label . font = game . Config . PublicGoldFont ; } } /**
         * 影片剪辑播放完毕自动移除
         */ public static playAndAutoRemove ( mov , playTime , cb ? ) { mov . gotoAndPlay ( 1 , playTime ) ; if ( playTime != - 1 ) { mov . once ( egret . MovieClipEvent . COMPLETE , ( e ) => { let mov = e . target ; if ( mov ) { mov . parent . removeChild ( mov ) ; if ( cb ) cb . run ( ) ; } } , null ) ; } } } }