@@ -12,9 +12,27 @@ var IsPlainObject = require('../../utils/object/IsPlainObject');
1212var JSONFile = require ( './JSONFile.js' ) ;
1313var MultiFile = require ( '../MultiFile.js' ) ;
1414
15+ /**
16+ * @typedef {object } Phaser.Loader.FileTypes.AtlasJSONFileConfig
17+ *
18+ * @property {string } key - The key of the file. Must be unique within both the Loader and the Texture Manager.
19+ * @property {string } [textureURL] - The absolute or relative URL to load the texture image file from.
20+ * @property {string } [textureExtension='png'] - The default file extension to use for the image texture if no url is provided.
21+ * @property {XHRSettingsObject } [textureXhrSettings] - Extra XHR Settings specifically for the texture image file.
22+ * @property {string } [normalMap] - The filename of an associated normal map. It uses the same path and url to load as the texture image.
23+ * @property {string } [atlasURL] - The absolute or relative URL to load the atlas json file from. Or a well formed JSON object to use instead.
24+ * @property {string } [atlasExtension='json'] - The default file extension to use for the atlas json if no url is provided.
25+ * @property {XHRSettingsObject } [atlasXhrSettings] - Extra XHR Settings specifically for the atlas json file.
26+ */
27+
1528/**
1629 * @classdesc
17- * An Atlas JSON File.
30+ * A single JSON based Texture Atlas File suitable for loading by the Loader.
31+ *
32+ * These are created when you use the Phaser.Loader.LoaderPlugin#atlas method and are not typically created directly.
33+ *
34+ * For documentation about what all the arguments and configuration options mean please see Phaser.Loader.LoaderPlugin#atlas.
35+ *
1836 * https://www.codeandweb.com/texturepacker/tutorials/how-to-create-sprite-sheets-for-phaser3?source=photonstorm
1937 *
2038 * @class AtlasJSONFile
@@ -23,12 +41,12 @@ var MultiFile = require('../MultiFile.js');
2341 * @constructor
2442 * @since 3.0.0
2543 *
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 } atlasURL - 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 } [atlasXhrSettings] - Optional atlas file specific XHR settings .
44+ * @param {Phaser.Loader.LoaderPlugin } loader - A reference to the Loader that is responsible for this file .
45+ * @param {( string|Phaser.Loader.FileTypes.AtlasJSONFileConfig) } key - The key to use for this file, or a file configuration object .
46+ * @param {string|string[] } [textureURL] - The absolute or relative URL to load the texture 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" .
47+ * @param {string } [atlasURL] - The absolute or relative URL to load the texture atlas json data file from. If undefined or `null` it will be set to `<key>.json`, i.e. if `key` was "alien" then the URL will be "alien.json" .
48+ * @param {XHRSettingsObject } [textureXhrSettings] - An XHR Settings configuration object for the atlas image file. Used in replacement of the Loaders default XHR Settings .
49+ * @param {XHRSettingsObject } [atlasXhrSettings] - An XHR Settings configuration object for the atlas json file. Used in replacement of the Loaders default XHR Settings .
3250 */
3351var AtlasJSONFile = new Class ( {
3452
@@ -38,19 +56,35 @@ var AtlasJSONFile = new Class({
3856
3957 function AtlasJSONFile ( loader , key , textureURL , atlasURL , textureXhrSettings , atlasXhrSettings )
4058 {
59+ var image ;
60+ var data ;
61+
4162 if ( IsPlainObject ( key ) )
4263 {
4364 var config = key ;
4465
4566 key = GetFastValue ( config , 'key' ) ;
46- textureURL = GetFastValue ( config , 'textureURL' ) ;
47- atlasURL = GetFastValue ( config , 'atlasURL' ) ;
48- textureXhrSettings = GetFastValue ( config , 'textureXhrSettings' ) ;
49- atlasXhrSettings = GetFastValue ( config , 'atlasXhrSettings' ) ;
50- }
5167
52- var image = new ImageFile ( loader , key , textureURL , textureXhrSettings ) ;
53- var data = new JSONFile ( loader , key , atlasURL , atlasXhrSettings ) ;
68+ image = new ImageFile ( loader , {
69+ key : key ,
70+ url : GetFastValue ( config , 'textureURL' ) ,
71+ extension : GetFastValue ( config , 'textureExtension' , 'png' ) ,
72+ normalMap : GetFastValue ( config , 'normalMap' ) ,
73+ xhrSettings : GetFastValue ( config , 'textureXhrSettings' )
74+ } ) ;
75+
76+ data = new JSONFile ( loader , {
77+ key : key ,
78+ url : GetFastValue ( config , 'atlasURL' ) ,
79+ extension : GetFastValue ( config , 'atlasExtension' , 'json' ) ,
80+ xhrSettings : GetFastValue ( config , 'atlasXhrSettings' )
81+ } ) ;
82+ }
83+ else
84+ {
85+ image = new ImageFile ( loader , key , textureURL , textureXhrSettings ) ;
86+ data = new JSONFile ( loader , key , atlasURL , atlasXhrSettings ) ;
87+ }
5488
5589 if ( image . linkFile )
5690 {
@@ -63,6 +97,12 @@ var AtlasJSONFile = new Class({
6397 }
6498 } ,
6599
100+ /**
101+ * Adds this file to its target cache upon successful loading and processing.
102+ *
103+ * @method Phaser.Loader.FileTypes.AtlasJSONFile#addToCache
104+ * @since 3.7.0
105+ */
66106 addToCache : function ( )
67107 {
68108 if ( this . isReadyToProcess ( ) )
@@ -82,23 +122,109 @@ var AtlasJSONFile = new Class({
82122} ) ;
83123
84124/**
85- * Adds a Texture Atlas file to the current load queue.
125+ * Adds a JSON based Texture Atlas, or array of atlases, to the current load queue.
126+ *
127+ * You can call this method from within your Scene's `preload`, along with any other files you wish to load:
128+
129+ * ```javascript
130+ * function preload ()
131+ * {
132+ * this.load.atlas('mainmenu', 'images/MainMenu.png', 'images/MainMenu.json');
133+ * }
134+ * ```
135+ *
136+ * The file is **not** loaded right away. It is added to a queue ready to be loaded either when the loader starts,
137+ * or if it's already running, when the next free load slot becomes available. This happens automatically if you
138+ * are calling this from within the Scene's `preload` method, or a related callback. Because the file is queued
139+ * it means you cannot use the file immediately after calling this method, but must wait for the file to complete.
140+ * The typical flow for a Phaser Scene is that you load assets in the Scene's `preload` method and then when the
141+ * Scene's `create` method is called you are guaranteed that all of those assets are ready for use and have been
142+ * loaded.
143+ *
144+ * If you call this from outside of `preload` then you are responsible for starting the Loader afterwards and monitoring
145+ * its events to know when it's safe to use the asset. Please see the Phaser.Loader.LoaderPlugin class for more details.
146+ *
147+ * Phaser expects the atlas data to be provided in a JSON file, using either the JSON Hash or JSON Array format.
148+ * These files are created by software such as Texture Packer, Shoebox and Adobe Flash / Animate.
149+ * If you are using Texture Packer and have enabled multi-atlas support, then please use the Phaser Multi Atlas loader
150+ * instead of this one.
151+ *
152+ * Phaser can load all common image types: png, jpg, gif and any other format the browser can natively handle.
153+ *
154+ * The key must be a unique String. It is used to add the file to the global Texture Manager upon a successful load.
155+ * The key should be unique both in terms of files being loaded and files already present in the Texture Manager.
156+ * Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file
157+ * then remove it from the Texture Manager first, before loading a new one.
158+ *
159+ * Instead of passing arguments you can pass a configuration object, such as:
160+ *
161+ * ```javascript
162+ * this.load.atlas({
163+ * key: 'mainmenu',
164+ * textureURL: 'images/MainMenu.png',
165+ * atlasURL: 'images/MainMenu.json'
166+ * });
167+ * ```
168+ *
169+ * See the documentation for `Phaser.Loader.FileTypes.AtlasJSONFileConfig` for more details.
170+ *
171+ * Instead of passing a URL for the atlas JSON data you can also pass in a well formed JSON object instead.
172+ *
173+ * Once the atlas has finished loading you can use frames from it as textures for a Game Object by referencing its key:
174+ *
175+ * ```javascript
176+ * this.load.atlas('mainmenu', 'images/MainMenu.png', 'images/MainMenu.json');
177+ * // and later in your game ...
178+ * this.add.image(x, y, 'mainmenu', 'background');
179+ * ```
180+ *
181+ * To get a list of all available frames within an atlas please consult your Texture Atlas software.
182+ *
183+ * If you have specified a prefix in the loader, via `Loader.setPrefix` then this value will be prepended to this files
184+ * key. For example, if the prefix was `MENU.` and the key was `Background` the final key will be `MENU.Background` and
185+ * this is what you would use to retrieve the image from the Texture Manager.
186+ *
187+ * The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it.
188+ *
189+ * 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"
190+ * 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
191+ * this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.
192+ *
193+ * Phaser also supports the automatic loading of associated normal maps. If you have a normal map to go with this image,
194+ * then you can specify it by providing an array as the `url` where the second element is the normal map:
195+ *
196+ * ```javascript
197+ * this.load.atlas('mainmenu', [ 'images/MainMenu.png', 'images/MainMenu-n.png' ], 'images/MainMenu.json');
198+ * ```
199+ *
200+ * Or, if you are using a config object use the `normalMap` property:
201+ *
202+ * ```javascript
203+ * this.load.atlas({
204+ * key: 'mainmenu',
205+ * textureURL: 'images/MainMenu.png',
206+ * normalMap: 'images/MainMenu-n.png',
207+ * atlasURL: 'images/MainMenu.json'
208+ * });
209+ * ```
86210 *
87- * Note: This method will only be available if the Atlas JSON File type has been built into Phaser.
211+ * The normal map file is subject to the same conditions as the image file with regard to the path, baseURL, CORs and XHR Settings.
212+ * Normal maps are a WebGL only feature.
88213 *
89- * The file is **not** loaded immediately after calling this method .
90- * Instead, the file is added to a queue within the Loader, which is processed automatically when the Loader starts .
214+ * Note: The ability to load this type of file will only be available if the Atlas JSON File type has been built into Phaser .
215+ * It is available in the default build but can be excluded from custom builds .
91216 *
92217 * @method Phaser.Loader.LoaderPlugin#atlas
218+ * @fires Phaser.Loader.LoaderPlugin#addFileEvent
93219 * @since 3.0.0
94220 *
95- * @param {string } key - The key of the file within the loader .
96- * @param {string } textureURL - The url to load the texture file from.
97- * @param {string } atlasURL - The url to load the atlas file from.
98- * @param {XHRSettingsObject } [textureXhrSettings] - Optional texture file specific XHR settings .
99- * @param {XHRSettingsObject } [atlasXhrSettings] - Optional atlas file specific XHR settings .
221+ * @param {( string|Phaser.Loader.FileTypes.AtlasJSONFileConfig|Phaser.Loader.FileTypes.AtlasJSONFileConfig[]) } key - The key to use for this file, or a file configuration object, or array of them .
222+ * @param {string|string[] } [ textureURL] - The absolute or relative URL to load the texture 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" .
223+ * @param {string } [ atlasURL] - The absolute or relative URL to load the texture atlas json data file from. If undefined or `null` it will be set to `<key>.json`, i.e. if `key` was "alien" then the URL will be "alien.json" .
224+ * @param {XHRSettingsObject } [textureXhrSettings] - An XHR Settings configuration object for the atlas image file. Used in replacement of the Loaders default XHR Settings .
225+ * @param {XHRSettingsObject } [atlasXhrSettings] - An XHR Settings configuration object for the atlas json file. Used in replacement of the Loaders default XHR Settings .
100226 *
101- * @return {Phaser.Loader.LoaderPlugin } The Loader.
227+ * @return {Phaser.Loader.LoaderPlugin } The Loader instance .
102228 */
103229FileTypesManager . register ( 'atlas' , function ( key , textureURL , atlasURL , textureXhrSettings , atlasXhrSettings )
104230{
0 commit comments