@@ -13,48 +13,95 @@ var MultiFile = require('../MultiFile.js');
1313var ParseXMLBitmapFont = require ( '../../gameobjects/bitmaptext/ParseXMLBitmapFont.js' ) ;
1414var XMLFile = require ( './XMLFile.js' ) ;
1515
16+ /**
17+ * @typedef {object } Phaser.Loader.FileTypes.BitmapFontFileConfig
18+ *
19+ * @property {string } key - The key of the file. Must be unique within both the Loader and the Texture Manager.
20+ * @property {string } [textureURL] - The absolute or relative URL to load the texture image file from.
21+ * @property {string } [textureExtension='png'] - The default file extension to use for the image texture if no url is provided.
22+ * @property {XHRSettingsObject } [textureXhrSettings] - Extra XHR Settings specifically for the texture image file.
23+ * @property {string } [normalMap] - The filename of an associated normal map. It uses the same path and url to load as the texture image.
24+ * @property {string } [fontDataURL] - The absolute or relative URL to load the font data xml file from.
25+ * @property {string } [fontDataExtension='xml'] - The default file extension to use for the font data xml if no url is provided.
26+ * @property {XHRSettingsObject } [fontDataXhrSettings] - Extra XHR Settings specifically for the font data xml file.
27+ */
28+
1629/**
1730 * @classdesc
18- * An Bitmap Font File.
31+ * A single Bitmap Font based File suitable for loading by the Loader.
32+ *
33+ * These are created when you use the Phaser.Loader.LoaderPlugin#bitmapFont method and are not typically created directly.
34+ *
35+ * For documentation about what all the arguments and configuration options mean please see Phaser.Loader.LoaderPlugin#bitmapFont.
1936 *
2037 * @class BitmapFontFile
2138 * @extends Phaser.Loader.MultiFile
2239 * @memberOf Phaser.Loader.FileTypes
2340 * @constructor
2441 * @since 3.0.0
2542 *
26- * @param {string } key - The key of the file within the loader .
27- * @param {string } textureURL - The url to load the texture file from .
28- * @param {string } xmlURL - The url to load the atlas file from.
29- * @param {string } path - The path of the file.
30- * @param {XHRSettingsObject } [textureXhrSettings] - Optional texture file specific XHR settings .
31- * @param {XHRSettingsObject } [xmlXhrSettings ] - Optional atlas file specific XHR settings .
43+ * @param {Phaser.Loader.LoaderPlugin } loader - A reference to the Loader that is responsible for this file .
44+ * @param {( string|Phaser.Loader.FileTypes.BitmapFontFileConfig) } key - The key to use for this file, or a file configuration object .
45+ * @param {string|string[] } [textureURL] - The absolute or relative URL to load the font image file from. If undefined or `null` it will be set to `<key>.png`, i.e. if `key` was "alien" then the URL will be "alien.png" .
46+ * @param {string } [fontDataURL] - The absolute or relative URL to load the font xml data file from. If undefined or `null` it will be set to `<key>.xml`, i.e. if `key` was "alien" then the URL will be "alien.xml" .
47+ * @param {XHRSettingsObject } [textureXhrSettings] - An XHR Settings configuration object for the font image file. Used in replacement of the Loaders default XHR Settings .
48+ * @param {XHRSettingsObject } [fontDataXhrSettings ] - An XHR Settings configuration object for the font data xml file. Used in replacement of the Loaders default XHR Settings .
3249 */
3350var BitmapFontFile = new Class ( {
3451
3552 Extends : MultiFile ,
3653
3754 initialize :
3855
39- function BitmapFontFile ( loader , key , textureURL , xmlURL , textureXhrSettings , xmlXhrSettings )
56+ function BitmapFontFile ( loader , key , textureURL , fontDataURL , textureXhrSettings , fontDataXhrSettings )
4057 {
58+ var image ;
59+ var data ;
60+
4161 if ( IsPlainObject ( key ) )
4262 {
4363 var config = key ;
4464
4565 key = GetFastValue ( config , 'key' ) ;
46- textureURL = GetFastValue ( config , 'textureURL' ) ;
47- xmlURL = GetFastValue ( config , 'xmlURL' ) ;
48- textureXhrSettings = GetFastValue ( config , 'textureXhrSettings' ) ;
49- xmlXhrSettings = GetFastValue ( config , 'xmlXhrSettings' ) ;
50- }
5166
52- var image = new ImageFile ( loader , key , textureURL , textureXhrSettings ) ;
53- var data = new XMLFile ( loader , key , xmlURL , xmlXhrSettings ) ;
67+ image = new ImageFile ( loader , {
68+ key : key ,
69+ url : GetFastValue ( config , 'textureURL' ) ,
70+ extension : GetFastValue ( config , 'textureExtension' , 'png' ) ,
71+ normalMap : GetFastValue ( config , 'normalMap' ) ,
72+ xhrSettings : GetFastValue ( config , 'textureXhrSettings' )
73+ } ) ;
74+
75+ data = new XMLFile ( loader , {
76+ key : key ,
77+ url : GetFastValue ( config , 'fontDataURL' ) ,
78+ extension : GetFastValue ( config , 'fontDataExtension' , 'xml' ) ,
79+ xhrSettings : GetFastValue ( config , 'fontDataXhrSettings' )
80+ } ) ;
81+ }
82+ else
83+ {
84+ image = new ImageFile ( loader , key , textureURL , textureXhrSettings ) ;
85+ data = new XMLFile ( loader , key , fontDataURL , fontDataXhrSettings ) ;
86+ }
5487
55- MultiFile . call ( this , loader , 'bitmapfont' , key , [ image , data ] ) ;
88+ if ( image . linkFile )
89+ {
90+ // Image has a normal map
91+ MultiFile . call ( this , loader , 'bitmapfont' , key , [ image , data , image . linkFile ] ) ;
92+ }
93+ else
94+ {
95+ MultiFile . call ( this , loader , 'bitmapfont' , key , [ image , data ] ) ;
96+ }
5697 } ,
5798
99+ /**
100+ * Adds this file to its target cache upon successful loading and processing.
101+ *
102+ * @method Phaser.Loader.FileTypes.BitmapFontFile#addToCache
103+ * @since 3.7.0
104+ */
58105 addToCache : function ( )
59106 {
60107 if ( this . isReadyToProcess ( ) )
@@ -65,7 +112,7 @@ var BitmapFontFile = new Class({
65112 image . addToCache ( ) ;
66113 xml . addToCache ( ) ;
67114
68- this . loader . cacheManager . bitmapFont . add ( image . key , { data : ParseXMLBitmapFont ( xml . data ) , texture : image . key , frame : null } ) ;
115+ this . loader . cacheManager . bitmapFont . add ( image . key , { data : ParseXMLBitmapFont ( xml . data ) , texture : image . key , frame : null } ) ;
69116
70117 this . complete = true ;
71118 }
@@ -74,25 +121,106 @@ var BitmapFontFile = new Class({
74121} ) ;
75122
76123/**
77- * Adds a Bitmap Font file to the current load queue.
124+ * Adds an XML based Bitmap Font, or array of fonts, to the current load queue.
125+ *
126+ * You can call this method from within your Scene's `preload`, along with any other files you wish to load:
127+
128+ * ```javascript
129+ * function preload ()
130+ * {
131+ * this.load.bitmapFont('goldenFont', 'images/GoldFont.png', 'images/GoldFont.xml');
132+ * }
133+ * ```
134+ *
135+ * The file is **not** loaded right away. It is added to a queue ready to be loaded either when the loader starts,
136+ * or if it's already running, when the next free load slot becomes available. This happens automatically if you
137+ * are calling this from within the Scene's `preload` method, or a related callback. Because the file is queued
138+ * it means you cannot use the file immediately after calling this method, but must wait for the file to complete.
139+ * The typical flow for a Phaser Scene is that you load assets in the Scene's `preload` method and then when the
140+ * Scene's `create` method is called you are guaranteed that all of those assets are ready for use and have been
141+ * loaded.
142+ *
143+ * If you call this from outside of `preload` then you are responsible for starting the Loader afterwards and monitoring
144+ * its events to know when it's safe to use the asset. Please see the Phaser.Loader.LoaderPlugin class for more details.
145+ *
146+ * Phaser expects the font data to be provided in an XML file format.
147+ * These files are created by software such as the [Angelcode Bitmap Font Generator](http://www.angelcode.com/products/bmfont/),
148+ * [Littera](http://kvazars.com/littera/) or [Glyph Designer](https://71squared.com/glyphdesigner)
149+ *
150+ * Phaser can load all common image types: png, jpg, gif and any other format the browser can natively handle.
151+ *
152+ * The key must be a unique String. It is used to add the file to the global Texture Manager upon a successful load.
153+ * The key should be unique both in terms of files being loaded and files already present in the Texture Manager.
154+ * Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file
155+ * then remove it from the Texture Manager first, before loading a new one.
156+ *
157+ * Instead of passing arguments you can pass a configuration object, such as:
158+ *
159+ * ```javascript
160+ * this.load.bitmapFont({
161+ * key: 'goldenFont',
162+ * textureURL: 'images/GoldFont.png',
163+ * fontDataURL: 'images/GoldFont.xml'
164+ * });
165+ * ```
166+ *
167+ * See the documentation for `Phaser.Loader.FileTypes.BitmapFontFileConfig` for more details.
168+ *
169+ * Once the atlas has finished loading you can use key of it when creating a Bitmap Text Game Object:
170+ *
171+ * ```javascript
172+ * this.load.bitmapFont('goldenFont', 'images/GoldFont.png', 'images/GoldFont.xml');
173+ * // and later in your game ...
174+ * this.add.bitmapText(x, y, 'goldenFont', 'Hello World');
175+ * ```
176+ *
177+ * If you have specified a prefix in the loader, via `Loader.setPrefix` then this value will be prepended to this files
178+ * key. For example, if the prefix was `MENU.` and the key was `Background` the final key will be `MENU.Background` and
179+ * this is what you would use when creating a Bitmap Text object.
180+ *
181+ * The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it.
182+ *
183+ * If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien"
184+ * and no URL is given then the Loader will set the URL to be "alien.png". It will always add `.png` as the extension, although
185+ * this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.
186+ *
187+ * Phaser also supports the automatic loading of associated normal maps. If you have a normal map to go with this image,
188+ * then you can specify it by providing an array as the `url` where the second element is the normal map:
189+ *
190+ * ```javascript
191+ * this.load.bitmapFont('goldenFont', [ 'images/GoldFont.png', 'images/GoldFont-n.png' ], 'images/GoldFont.xml');
192+ * ```
193+ *
194+ * Or, if you are using a config object use the `normalMap` property:
195+ *
196+ * ```javascript
197+ * this.load.bitmapFont({
198+ * key: 'goldenFont',
199+ * textureURL: 'images/GoldFont.png',
200+ * normalMap: 'images/GoldFont-n.png',
201+ * fontDataURL: 'images/GoldFont.xml'
202+ * });
203+ * ```
78204 *
79- * Note: This method will only be available if the Bitmap Font File type has been built into Phaser.
205+ * The normal map file is subject to the same conditions as the image file with regard to the path, baseURL, CORs and XHR Settings.
206+ * Normal maps are a WebGL only feature.
80207 *
81- * The file is **not** loaded immediately after calling this method .
82- * Instead, the file is added to a queue within the Loader, which is processed automatically when the Loader starts .
208+ * Note: The ability to load this type of file will only be available if the Bitmap Font File type has been built into Phaser .
209+ * It is available in the default build but can be excluded from custom builds .
83210 *
84211 * @method Phaser.Loader.LoaderPlugin#bitmapFont
212+ * @fires Phaser.Loader.LoaderPlugin#addFileEvent
85213 * @since 3.0.0
86214 *
87- * @param {string } key - [description]
88- * @param {string } textureURL - [description]
89- * @param {string } xmlURL - [description]
90- * @param {XHRSettingsObject } [textureXhrSettings] - [description]
91- * @param {XHRSettingsObject } [xmlXhrSettings ] - [description]
215+ * @param {( string|Phaser.Loader.FileTypes.BitmapFontFileConfig|Phaser.Loader.FileTypes.BitmapFontFileConfig[]) } key - The key to use for this file, or a file configuration object, or array of them.
216+ * @param {string|string[] } [ textureURL] - The absolute or relative URL to load the font image file from. If undefined or `null` it will be set to `<key>.png`, i.e. if `key` was "alien" then the URL will be "alien.png".
217+ * @param {string } [fontDataURL] - The absolute or relative URL to load the font xml data file from. If undefined or `null` it will be set to `<key>.xml`, i.e. if `key` was "alien" then the URL will be "alien.xml".
218+ * @param {XHRSettingsObject } [textureXhrSettings] - An XHR Settings configuration object for the font image file. Used in replacement of the Loaders default XHR Settings.
219+ * @param {XHRSettingsObject } [fontDataXhrSettings ] - An XHR Settings configuration object for the font data xml file. Used in replacement of the Loaders default XHR Settings.
92220 *
93- * @return {Phaser.Loader.LoaderPlugin } The Loader.
221+ * @return {Phaser.Loader.LoaderPlugin } The Loader instance .
94222 */
95- FileTypesManager . register ( 'bitmapFont' , function ( key , textureURL , xmlURL , textureXhrSettings , xmlXhrSettings )
223+ FileTypesManager . register ( 'bitmapFont' , function ( key , textureURL , fontDataURL , textureXhrSettings , fontDataXhrSettings )
96224{
97225 var multifile ;
98226
@@ -111,7 +239,7 @@ FileTypesManager.register('bitmapFont', function (key, textureURL, xmlURL, textu
111239 }
112240 else
113241 {
114- multifile = new BitmapFontFile ( this , key , textureURL , xmlURL , textureXhrSettings , xmlXhrSettings ) ;
242+ multifile = new BitmapFontFile ( this , key , textureURL , fontDataURL , textureXhrSettings , fontDataXhrSettings ) ;
115243
116244 this . addFile ( multifile . files ) ;
117245 }
0 commit comments