import { Manager } from "../manager/Manager" ; import { EventApi } from "../../framework/event/EventApi" ; import { ViewZOrder , Config } from "../config/Config" ; import { BUNDLE_RESOURCES , ResourceCacheData } from "../../framework/base/Defines" ; import { i18n } from "../language/LanguageImpl" ; description interface AlertConfig { description tag ? : string | number ; description text ? : string ; description title ? : string ; description confirmString ? : string ; description cancelString ? : string ; description confirmCb ? : ( isOK : boolean ) => void ; description cancelCb ? : ( isOK : boolean ) => void ; description richText ? : string ; description immediatelyCallback ? : boolean ; } class AlertDialog extends cc . Component { description private _closeBtn : cc . Node = null ; description private _content : cc . Node = null ; description private _textContent : cc . Label = null ; description private _richTextContent : cc . RichText = null ; description private _title : cc . Label = null ; description private _confirm : cc . Node = null ; description private _cancel : cc . Node = null ; description private _config : AlertConfig = null ; public get config ( ) { return this . _config ; } onLoad ( ) { this . _content = cc . find ( "content" , this . node ) ; this . _closeBtn = cc . find ( "close" , this . _content ) ; this . _title = cc . find ( "title" , this . _content ) . getComponent ( cc . Label ) ; this . _textContent = cc . find ( "content" , this . _content ) . getComponent ( cc . Label ) ; this . _richTextContent = cc . find ( "richContent" , this . _content ) . getComponent ( cc . RichText ) ; this . _confirm = cc . find ( "confirm" , this . _content ) ; this . _cancel = cc . find ( "cancel" , this . _content ) ; } public show ( config ) { if ( ! config . title ) { config . title = i18n . alert_title ; } if ( ! config . confirmString ) { config . confirmString = i18n . alert_confirm ; } if ( ! config . cancelString ) { config . cancelString = i18n . alert_cancel ; } this . _config = config ; this . writeContent ( config ) ; this . showButton ( config ) ; this . _show ( ) ; } private _show ( ) { if ( this . _content ) { cc . tween ( this . _content ) . set ( { scale : 0.2 } ) . to ( 0.2 , { scale : 1.1 } ) . delay ( 0.05 ) . to ( 0.1 , { scale : 1.0 } ) . start ( ) ; } } description private writeContent ( config ) { if ( config . richText ) { this . _textContent . node . active = false ; this . _richTextContent . node . active = true ; this . _richTextContent . string = config . richText ; } else { this . _textContent . node . active = true ; this . _richTextContent . node . active = false ; if ( config . text ) { this . _textContent . string = config . text ; } else { cc . error ( "请指定提示内容" ) ; this . _textContent . string = "" ; } } if ( config . title ) { this . _title . string = config . title ; } if ( config . confirmString ) { let title = cc . find ( "Background/Label" , this . _confirm ) ; if ( title ) { title . getComponent ( cc . Label ) . string = config . confirmString ; } } if ( config . cancelString ) { let title = cc . find ( "Background/Label" , this . _cancel ) ; if ( title ) { title . getComponent ( cc . Label ) . string = config . cancelString ; } } } description private showButton ( config ) { if ( this . _confirm && this . _cancel && this . _closeBtn ) { this . _closeBtn . on ( cc . Node . EventType . TOUCH_END , this . close . bind ( this ) ) ; if ( config . confirmCb ) { this . _confirm . active = true ; this . _confirm . on ( cc . Node . EventType . TOUCH_END , this . onClick . bind ( this , config . confirmCb , true ) ) ; this . _closeBtn . on ( cc . Node . EventType . TOUCH_END , this . onClick . bind ( this , config . confirmCb , false ) ) ; } else { this . _confirm . active = false ; } if ( config . cancelCb ) { this . _cancel . active = true ; this . _cancel . on ( cc . Node . EventType . TOUCH_END , this . onClick . bind ( this , config . cancelCb , false ) ) ; } else { this . _cancel . active = false ; } if ( this . _confirm . active ) { if ( this . _cancel . active ) { } else { this . _confirm . x = 0 ; } } else { if ( this . _cancel . active ) { this . _cancel . x = 0 ; } else { cc . warn ( "提示框无按钮显示" ) ; } } } } description private close ( ) { this . _close ( null ) ; } private _close ( complete ) { let selt = this ; if ( cc . isValid ( this . _content ) ) { cc . tween ( this . _content ) . to ( 0.2 , { scale : 1.15 } ) . to ( 0.1 , { scale : 0.3 } ) . call ( ( ) => { Manager . alert . finishAlert ( ) ; if ( complete ) complete ( ) ; } ) . start ( ) ; } } private onClick ( cb , isOk ) { if ( this . _config . immediatelyCallback ) { if ( cb ) cb ( isOk ) ; this . _close ( null ) ; } else { this . _close ( ( ) => { if ( cb ) cb ( isOk ) ; } ) ; } } } export default class Alert { private static _instance : Alert = null ; public static Instance ( ) { return this . _instance || ( this . _instance = new Alert ( ) ) ; } private curPanel : cc . Node = null ; private queue : AlertConfig [ ] = [ ] ; private prefab : cc . Prefab = null ; constructor ( ) { Manager . eventDispatcher . addEventListener ( EventApi . AdaptScreenEvent , this . onAdaptScreen , this ) ; } private _isLoadingPrefab = false ; private finishLoadCb = null ; public preLoadPrefab ( ) { this . loadPrefab ( ) ; } private onAdaptScreen ( ) { Manager . resolutionHelper . fullScreenAdapt ( this . curPanel ) ; } description param config public show ( config ) { this . queue . push ( config ) ; this . _show ( config ) ; } description param tag public close ( tag ? ) { if ( tag ) { let j = this . queue . length ; while ( j -- ) { if ( this . queue [ j ] . tag == tag ) { this . queue . splice ( j , 1 ) ; } } if ( this . curPanel ) { let current = this . curPanel . getComponent ( AlertDialog ) . config ; if ( current . tag == tag ) { this . finishAlert ( ) ; } } } else { this . finishAlert ( ) ; } } public closeAll ( ) { this . queue = [ ] ; this . finishAlert ( ) ; } public finishAlert ( ) { if ( this . curPanel ) { this . curPanel . removeFromParent ( ) ; this . curPanel = null ; } let config = this . queue . shift ( ) ; if ( this . queue . length != 0 ) { this . _show ( this . queue [ 0 ] ) ; return this . queue [ 0 ] ; } return config ; } private async _show ( config ) { let finish = await this . loadPrefab ( ) ; if ( finish ) { if ( ! this . curPanel ) { this . curPanel = cc . instantiate ( this . prefab ) ; Manager . resolutionHelper . fullScreenAdapt ( this . curPanel ) ; let dialog = this . curPanel . addComponent ( AlertDialog ) ; let canvas = Manager . uiManager . getCanvas ( ) ; if ( canvas ) { this . curPanel . parent = canvas ; this . curPanel . zIndex = ViewZOrder . Alert ; dialog . show ( config ) ; } } } } private async loadPrefab ( ) { return new Promise < boolean > ( ( resolve , reject ) => { if ( this . _isLoadingPrefab ) { this . finishLoadCb = resolve ; return ; } if ( this . prefab ) { if ( this . finishLoadCb ) { this . finishLoadCb ( true ) ; this . finishLoadCb = null ; } resolve ( true ) ; } else { this . _isLoadingPrefab = true ; Manager . assetManager . load ( BUNDLE_RESOURCES , Config . CommonPrefabs . alert , cc . Prefab , ( finish , total , item ) => { } , ( data ) => { this . _isLoadingPrefab = false ; if ( data && data . data && data . data instanceof cc . Prefab ) { this . prefab = data . data ; if ( this . finishLoadCb ) { this . finishLoadCb ( true ) ; this . finishLoadCb = null ; } resolve ( true ) ; } else { if ( this . finishLoadCb ) { this . finishLoadCb ( false ) ; this . finishLoadCb = null ; } resolve ( false ) ; } } ) ; } } ) ; } }