import { BaseUI , UIClass } from "../UI/BaseUI" ; export class UIManager { private static instance : UIManager ; private uiList : BaseUI [ ] = [ ] ; private uiRoot : cc . Node = null ; public static getInstance ( ) { if ( this . instance == null ) { this . instance = new UIManager ( ) ; } return this . instance ; } constructor ( ) { this . uiRoot = cc . find ( "Canvas" ) ; } public openUI < T extends BaseUI > ( uiClass , zOrder ? , callback ? , onProgress ? , ... args ) { if ( this . getUI ( uiClass ) ) { return ; } cc . loader . loadRes ( uiClass . getUrl ( ) , ( completedCount , totalCount , item ) => { if ( onProgress ) { onProgress ( completedCount , totalCount , item ) ; } } , ( error , prefab ) => { if ( error ) { cc . log ( error ) ; return ; } if ( this . getUI ( uiClass ) ) { return ; } let uiNode = cc . instantiate ( prefab ) ; uiNode . parent = this . uiRoot ; if ( zOrder ) { uiNode . zIndex = zOrder ; } let ui = uiNode . getComponent ( uiClass ) as BaseUI ; ui . tag = uiClass ; this . uiList . push ( ui ) ; if ( callback ) { callback ( ui , args ) ; } } ) ; } public closeUI < T extends BaseUI > ( uiClass ) { for ( let i = 0 ; i < this . uiList . length ; ++ i ) { if ( this . uiList [ i ] . tag === uiClass ) { this . uiList [ i ] . node . destroy ( ) ; this . uiList . splice ( i , 1 ) ; return ; } } } public showUI < T extends BaseUI > ( uiClass , callback ? ) { let ui = this . getUI ( uiClass ) ; if ( ui ) { ui . node . active = true ; ui . onShow ( ) ; callback && callback ( ui ) ; } else { this . openUI ( uiClass , 0 , ( ) => { let ui = this . getUI ( uiClass ) ; ui . onShow ( ) ; callback && callback ( ui ) ; } ) ; } } public hideUI < T extends BaseUI > ( uiClass ) { let ui = this . getUI ( uiClass ) ; if ( ui ) { ui . node . active = false ; } } public getUI < T extends BaseUI > ( uiClass ) { for ( let i = 0 ; i < this . uiList . length ; ++ i ) { if ( this . uiList [ i ] . tag === uiClass ) { return this . uiList [ i ] ; } } return null ; } }