Skip to content

Commit de195fe

Browse files
committed
Now supports multiple atlases per Spine set
1 parent e23cd39 commit de195fe

1 file changed

Lines changed: 72 additions & 143 deletions

File tree

plugins/spine/src/SpineFile.js

Lines changed: 72 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ var SpineFile = new Class({
5353

5454
function SpineFile (loader, key, jsonURL, atlasURL, jsonXhrSettings, atlasXhrSettings)
5555
{
56+
var i;
5657
var json;
5758
var atlas;
59+
var files = [];
60+
var cache = loader.cacheManager.custom.spine;
61+
62+
// atlas can be an array of atlas files, not just a single one
5863

5964
if (IsPlainObject(key))
6065
{
@@ -69,22 +74,60 @@ var SpineFile = new Class({
6974
xhrSettings: GetFastValue(config, 'jsonXhrSettings')
7075
});
7176

72-
atlas = new TextFile(loader, {
73-
key: key,
74-
url: GetFastValue(config, 'atlasURL'),
75-
extension: GetFastValue(config, 'atlasExtension', 'atlas'),
76-
xhrSettings: GetFastValue(config, 'atlasXhrSettings')
77-
});
77+
atlasURL = GetFastValue(config, 'atlasURL');
78+
79+
if (Array.isArray(atlasURL))
80+
{
81+
for (i = 0; i < atlasURL.length; i++)
82+
{
83+
atlas = new TextFile(loader, {
84+
key: key,
85+
url: atlasURL[i],
86+
extension: GetFastValue(config, 'atlasExtension', 'atlas'),
87+
xhrSettings: GetFastValue(config, 'atlasXhrSettings')
88+
});
89+
90+
files.push(atlas);
91+
}
92+
}
93+
else
94+
{
95+
atlas = new TextFile(loader, {
96+
key: key,
97+
url: atlasURL,
98+
extension: GetFastValue(config, 'atlasExtension', 'atlas'),
99+
xhrSettings: GetFastValue(config, 'atlasXhrSettings')
100+
});
101+
102+
files.push(atlas);
103+
}
78104
}
79105
else
80106
{
81107
json = new JSONFile(loader, key, jsonURL, jsonXhrSettings);
82-
atlas = new TextFile(loader, key, atlasURL, atlasXhrSettings);
108+
109+
if (Array.isArray(atlasURL))
110+
{
111+
for (i = 0; i < atlasURL.length; i++)
112+
{
113+
atlas = new TextFile(loader, key + '_' + i, atlasURL[i], atlasXhrSettings);
114+
atlas.cache = cache;
115+
116+
files.push(atlas);
117+
}
118+
}
119+
else
120+
{
121+
atlas = new TextFile(loader, key + '_0', atlasURL, atlasXhrSettings);
122+
atlas.cache = cache;
123+
124+
files.push(atlas);
125+
}
83126
}
84-
85-
atlas.cache = loader.cacheManager.custom.spine;
86127

87-
MultiFile.call(this, loader, 'spine', key, [ json, atlas ]);
128+
files.unshift(json);
129+
130+
MultiFile.call(this, loader, 'spine', key, files);
88131
},
89132

90133
/**
@@ -174,152 +217,38 @@ var SpineFile = new Class({
174217

175218
fileJSON.addToCache();
176219

177-
var fileText = this.files[1];
178-
179-
fileText.addToCache();
220+
var atlasCache;
221+
var atlasKey = '';
222+
var combinedAtlastData = '';
180223

181-
for (var i = 2; i < this.files.length; i++)
224+
for (var i = 1; i < this.files.length; i++)
182225
{
183226
var file = this.files[i];
184227

185-
var key = file.key.substr(4).trim();
228+
if (file.type === 'text')
229+
{
230+
atlasKey = file.key.substr(0, file.key.length - 2);
186231

187-
this.loader.textureManager.addImage(key, file.data);
232+
atlasCache = file.cache;
233+
234+
combinedAtlastData = combinedAtlastData.concat(file.data);
235+
}
236+
else
237+
{
238+
var key = file.key.substr(4).trim();
239+
240+
this.loader.textureManager.addImage(key, file.data);
241+
}
188242

189243
file.pendingDestroy();
190244
}
191245

192-
this.complete = true;
193-
}
194-
}
195-
196-
});
246+
atlasCache.add(atlasKey, combinedAtlastData);
197247

198-
/**
199-
* Adds a Unity YAML based Texture Atlas, or array of atlases, to the current load queue.
200-
*
201-
* You can call this method from within your Scene's `preload`, along with any other files you wish to load:
202-
*
203-
* ```javascript
204-
* function preload ()
205-
* {
206-
* this.load.unityAtlas('mainmenu', 'images/MainMenu.png', 'images/MainMenu.txt');
207-
* }
208-
* ```
209-
*
210-
* The file is **not** loaded right away. It is added to a queue ready to be loaded either when the loader starts,
211-
* or if it's already running, when the next free load slot becomes available. This happens automatically if you
212-
* are calling this from within the Scene's `preload` method, or a related callback. Because the file is queued
213-
* it means you cannot use the file immediately after calling this method, but must wait for the file to complete.
214-
* The typical flow for a Phaser Scene is that you load assets in the Scene's `preload` method and then when the
215-
* Scene's `create` method is called you are guaranteed that all of those assets are ready for use and have been
216-
* loaded.
217-
*
218-
* If you call this from outside of `preload` then you are responsible for starting the Loader afterwards and monitoring
219-
* its events to know when it's safe to use the asset. Please see the Phaser.Loader.LoaderPlugin class for more details.
220-
*
221-
* Phaser expects the atlas data to be provided in a YAML formatted text file as exported from Unity.
222-
*
223-
* Phaser can load all common image types: png, jpg, gif and any other format the browser can natively handle.
224-
*
225-
* The key must be a unique String. It is used to add the file to the global Texture Manager upon a successful load.
226-
* The key should be unique both in terms of files being loaded and files already present in the Texture Manager.
227-
* Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file
228-
* then remove it from the Texture Manager first, before loading a new one.
229-
*
230-
* Instead of passing arguments you can pass a configuration object, such as:
231-
*
232-
* ```javascript
233-
* this.load.unityAtlas({
234-
* key: 'mainmenu',
235-
* textureURL: 'images/MainMenu.png',
236-
* atlasURL: 'images/MainMenu.txt'
237-
* });
238-
* ```
239-
*
240-
* See the documentation for `Phaser.Loader.FileTypes.SpineFileConfig` for more details.
241-
*
242-
* Once the atlas has finished loading you can use frames from it as textures for a Game Object by referencing its key:
243-
*
244-
* ```javascript
245-
* this.load.unityAtlas('mainmenu', 'images/MainMenu.png', 'images/MainMenu.json');
246-
* // and later in your game ...
247-
* this.add.image(x, y, 'mainmenu', 'background');
248-
* ```
249-
*
250-
* To get a list of all available frames within an atlas please consult your Texture Atlas software.
251-
*
252-
* If you have specified a prefix in the loader, via `Loader.setPrefix` then this value will be prepended to this files
253-
* key. For example, if the prefix was `MENU.` and the key was `Background` the final key will be `MENU.Background` and
254-
* this is what you would use to retrieve the image from the Texture Manager.
255-
*
256-
* The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it.
257-
*
258-
* 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"
259-
* 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
260-
* this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.
261-
*
262-
* Phaser also supports the automatic loading of associated normal maps. If you have a normal map to go with this image,
263-
* then you can specify it by providing an array as the `url` where the second element is the normal map:
264-
*
265-
* ```javascript
266-
* this.load.unityAtlas('mainmenu', [ 'images/MainMenu.png', 'images/MainMenu-n.png' ], 'images/MainMenu.txt');
267-
* ```
268-
*
269-
* Or, if you are using a config object use the `normalMap` property:
270-
*
271-
* ```javascript
272-
* this.load.unityAtlas({
273-
* key: 'mainmenu',
274-
* textureURL: 'images/MainMenu.png',
275-
* normalMap: 'images/MainMenu-n.png',
276-
* atlasURL: 'images/MainMenu.txt'
277-
* });
278-
* ```
279-
*
280-
* The normal map file is subject to the same conditions as the image file with regard to the path, baseURL, CORs and XHR Settings.
281-
* Normal maps are a WebGL only feature.
282-
*
283-
* Note: The ability to load this type of file will only be available if the Unity Atlas File type has been built into Phaser.
284-
* It is available in the default build but can be excluded from custom builds.
285-
*
286-
* @method Phaser.Loader.LoaderPlugin#spine
287-
* @fires Phaser.Loader.LoaderPlugin#addFileEvent
288-
* @since 3.16.0
289-
*
290-
* @param {(string|Phaser.Loader.FileTypes.SpineFileConfig|Phaser.Loader.FileTypes.SpineFileConfig[])} key - The key to use for this file, or a file configuration object, or array of them.
291-
* @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".
292-
* @param {string} [atlasURL] - The absolute or relative URL to load the texture atlas data file from. If undefined or `null` it will be set to `<key>.txt`, i.e. if `key` was "alien" then the URL will be "alien.txt".
293-
* @param {XHRSettingsObject} [textureXhrSettings] - An XHR Settings configuration object for the atlas image file. Used in replacement of the Loaders default XHR Settings.
294-
* @param {XHRSettingsObject} [atlasXhrSettings] - An XHR Settings configuration object for the atlas data file. Used in replacement of the Loaders default XHR Settings.
295-
*
296-
* @return {Phaser.Loader.LoaderPlugin} The Loader instance.
297-
FileTypesManager.register('spine', function (key, jsonURL, atlasURL, jsonXhrSettings, atlasXhrSettings)
298-
{
299-
var multifile;
300-
301-
// Supports an Object file definition in the key argument
302-
// Or an array of objects in the key argument
303-
// Or a single entry where all arguments have been defined
304-
305-
if (Array.isArray(key))
306-
{
307-
for (var i = 0; i < key.length; i++)
308-
{
309-
multifile = new SpineFile(this, key[i]);
310-
311-
this.addFile(multifile.files);
248+
this.complete = true;
312249
}
313250
}
314-
else
315-
{
316-
multifile = new SpineFile(this, key, jsonURL, atlasURL, jsonXhrSettings, atlasXhrSettings);
317-
318-
this.addFile(multifile.files);
319-
}
320251

321-
return this;
322252
});
323-
*/
324253

325254
module.exports = SpineFile;

0 commit comments

Comments
 (0)