namespace NT { /** Represents the configuration for a bitmap font. These are typically created and stored in a fonts file. */ class BitmapFontConfig { /** The name of this font. */ public name : string ; /** The font file path of this bitmap font. */ public fontFile : string ; param json public static fromJson ( json ) { let config = new BitmapFontConfig ( ) ; if ( json . name !== undefined ) { config . name = String ( json . name ) ; } if ( json . fontFile !== undefined ) { config . fontFile = String ( json . fontFile ) ; } else { throw new Error ( "Cannot create a bitmap font without a font file." ) ; } return config ; } } /** A manager for bitmap fonts. */ export class BitmapFontManager { private static _configLoaded : boolean = false ; private static _fonts : { [ name ] : BitmapFont ; } = { } ; /** Hide the constructor to prevent instantiation. */ private constructor ( ) { } param message public static onMessage ( message ) { if ( message . code === MESSAGE_ASSET_LOADER_ASSET_LOADED + "assets/fonts/fonts.json" ) { Message . unsubscribeCallback ( MESSAGE_ASSET_LOADER_ASSET_LOADED + "assets/fonts/fonts.json" , BitmapFontManager . onMessage ) ; BitmapFontManager . processFontAsset ( message . context as JsonAsset ) ; } } /** Indicates if this manager is loaded. */ public static get isLoaded ( ) : boolean { if ( BitmapFontManager . _configLoaded ) { let keys = Object . keys ( BitmapFontManager . _fonts ) ; for ( let key of keys ) { if ( ! BitmapFontManager . _fonts [ key ] . isLoaded ) { console . debug ( "Font " + key + " is still loading..." ) ; return false ; } } console . debug ( "All fonts are loaded" ) ; return true ; } return false ; } param name public static getFont ( name ) { if ( BitmapFontManager . _fonts [ name ] === undefined ) { throw new Error ( "A font named " + name + " does not exist." ) ; } return BitmapFontManager . _fonts [ name ] ; } /**
         * Loads registered fonts.
         */ public static load ( ) { let asset = AssetManager . getAsset ( "assets/fonts/fonts.json" ) ; if ( asset !== undefined ) { BitmapFontManager . processFontAsset ( asset as JsonAsset ) ; } else { Message . subscribeCallback ( MESSAGE_ASSET_LOADER_ASSET_LOADED + "assets/fonts/fonts.json" , BitmapFontManager . onMessage ) ; } } private static processFontAsset ( asset ) { let fonts = asset . Data . bitmapFonts ; if ( fonts ) { for ( let font of fonts ) { let f = BitmapFontConfig . fromJson ( font ) ; BitmapFontManager . _fonts [ font . name ] = new BitmapFont ( font . name , font . fontFile ) ; BitmapFontManager . _fonts [ font . name ] . load ( ) ; } } BitmapFontManager . _configLoaded = true ; } } }