/**
 * 吐司的显示任务
 */ interface ToastTask { text : string ; duration : number ; } /**
 * 吐司的实体
 */ class ToastObject { private _text ; private _duration ; private _gravity ; private _x = 0 ; private _y = 0 ; param text param duration constructor ( text , duration ) { this . _text = text ; this . _duration = duration ; } /**
     * 显示Toast的方法
     */ show ( ) { if ( Toast . showing ) { let text = this . _text ; let duration = this . _duration ; Toast . TaskArray . push ( { text , duration } ) ; if ( Toast . TaskArray . length > Toast . MAX_ARRAY ) { Toast . TaskArray . shift ( ) ; } return ; } if ( Toast . bgSpriteFrame == null ) { cc . loader . load ( { 'uuid' : 'b43ff3c2-02bb-4874-81f7-f2dea6970f18' } , ( error , result ) => { if ( error ) { cc . error ( error ) ; return ; } Toast . bgSpriteFrame = new cc . SpriteFrame ( result ) ; Toast . bgSpriteFrame . insetTop = 3 ; Toast . bgSpriteFrame . insetBottom = 3 ; Toast . bgSpriteFrame . insetLeft = 4 ; Toast . bgSpriteFrame . insetRight = 4 ; this . show ( ) ; } ) ; return ; } Toast . showing = true ; var canvas = cc . director . getScene ( ) . getComponentInChildren ( cc . Canvas ) ; var width = canvas . node . width ; var height = canvas . node . height ; if ( this . _duration === undefined ) { this . _duration = Toast . LENGTH_SHORT ; } let bgNode = new cc . Node ( ) ; bgNode . opacity = 200 ; let bgSprite = bgNode . addComponent ( cc . Sprite ) ; bgSprite . type = cc . Sprite . Type . SLICED ; let bgLayout = bgNode . addComponent ( cc . Layout ) ; bgLayout . resizeMode = cc . Layout . ResizeMode . CONTAINER ; let textNode = new cc . Node ( ) ; let textLabel = textNode . addComponent ( cc . Label ) ; textLabel . horizontalAlign = cc . Label . HorizontalAlign . CENTER ; textLabel . verticalAlign = cc . Label . VerticalAlign . CENTER ; textLabel . fontSize = 20 ; textLabel . string = this . _text ; let hPadding = textLabel . fontSize / 8 ; let vPadding = 2 ; bgLayout . paddingLeft = hPadding ; bgLayout . paddingRight = hPadding ; bgLayout . paddingTop = vPadding ; bgLayout . paddingBottom = vPadding ; if ( this . _text . length * textLabel . fontSize > width / 3 ) { textNode . width = width / 3 ; textLabel . overflow = cc . Label . Overflow . RESIZE_HEIGHT ; } bgNode . addChild ( textNode ) ; if ( Toast . bgSpriteFrame ) { bgSprite . spriteFrame = Toast . bgSpriteFrame ; } if ( this . _gravity == Toast . CENTER ) { textNode . y = 0 ; textNode . x = 0 ; } else if ( this . _gravity == Toast . TOP ) { textNode . y = textNode . y + ( height / 5 ) * 2 ; } else if ( this . _gravity == Toast . TOP_LEFT ) { textNode . y = textNode . y + ( height / 5 ) * 2 ; textNode . x = textNode . x + ( width / 5 ) ; } else if ( this . _gravity == Toast . LEFT ) { textNode . x = textNode . x + ( width / 5 ) ; } else if ( this . _gravity == Toast . BOTTOM_LEFT ) { textNode . y = textNode . y - ( height / 5 ) * 2 ; textNode . x = textNode . x + ( width / 5 ) ; } else if ( this . _gravity == Toast . BOTTOM ) { textNode . y = textNode . y - ( height / 5 ) * 2 ; } else if ( this . _gravity == Toast . BOTTOM_RIGHT ) { textNode . y = textNode . y - ( height / 5 ) * 2 ; textNode . x = textNode . x - ( width / 5 ) ; } else if ( this . _gravity == Toast . RIGHT ) { textNode . x = textNode . x - ( width / 5 ) ; } else if ( this . _gravity == Toast . TOP_RIGHT ) { textNode . y = textNode . y + ( height / 5 ) * 2 ; textNode . x = textNode . x - ( width / 5 ) ; } else { textNode . y = textNode . y - ( height / 5 ) * 2 ; } textNode . x = textNode . x + this . _x ; textNode . y = textNode . y + this . _y ; canvas . node . addChild ( bgNode ) ; let finished = cc . callFunc ( ( target ) => { bgNode . destroy ( ) ; Toast . showing = false ; if ( Toast . TaskArray . length == 0 ) { } else { let task = Toast . TaskArray . shift ( ) ; Toast . showText ( task . text , task . duration ) ; } } ) ; let action = cc . sequence ( cc . moveBy ( this . _duration , cc . p ( 0 , 0 ) ) , cc . fadeOut ( 0.3 ) , finished ) ; bgNode . runAction ( action ) ; } param gravity param x param y setGravity ( gravity , x , y ) { this . _gravity = gravity ; this . _x = x ; this . _y = y ; } } export default class Toast { /**
     * 时长
     */ static LENGTH_LONG : number = 3.5 ; static LENGTH_SHORT : number = 1 ; /**
     * 位置
     */ static CENTER : number = 0 ; static TOP : number = 1 ; static TOP_LEFT : number = 2 ; static LEFT : number = 3 ; static BOTTOM_LEFT : number = 4 ; static BOTTOM : number = 5 ; static BOTTOM_RIGHT : number = 6 ; static RIGHT : number = 7 ; static TOP_RIGHT : number = 8 ; static bgSpriteFrame : cc . SpriteFrame = null ; static TaskArray : Array < ToastTask > = [ ] ; static showing : boolean = false ; static MAX_ARRAY : number = 5 ; param text param duration static makeText ( text , duration ) { return new ToastObject ( text , duration ) ; } param text param duration static showText ( text , duration ) { Toast . makeText ( text , duration ) . show ( ) ; } }