Skip to content

Commit 5980a3b

Browse files
committed
Loader can now natively load XML files via load.xml. Once the XML file has loaded it is parsed via either DOMParser or ActiveXObject and then added to the Cache, where it can be retrieved via cache.getXML(key).
Cache now has support for XML files stored in their own container. You can add them with `cache.addXML` (typically this is done from the Loader automatically for you) and get them with `cache.getXML(key)`. There is also `cache.checkXMLKey(key)`, `cache.checkKeys` and `cache.removeXML(key)`.
1 parent 5a58d6b commit 5980a3b

3 files changed

Lines changed: 141 additions & 15 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ Version 2.1.2 - "Whitebridge" - in development
7878
* StateManager.unlink will null all State-level Phaser properties, such as `game`, `add`, etc. Useful if you never need to return to the State again.
7979
* Cache.removeImage has a new parameter: `removeFromPixi` which is `true` by default. It will remove the image from the Pixi BaseTextureCache as well as from the Phaser Cache. Set to false if you don't want the Pixi cache touched.
8080
* Group.ignoreDestroy boolean will bail out early from any call to `Group.destroy`. Handy if you need to create a global Group that persists across States.
81+
* Loader can now natively load XML files via `load.xml`. Once the XML file has loaded it is parsed via either DOMParser or ActiveXObject and then added to the Cache, where it can be retrieved via `cache.getXML(key)`.
82+
* Cache now has support for XML files stored in their own container. You can add them with `cache.addXML` (typically this is done from the Loader automatically for you) and get them with `cache.getXML(key)`. There is also `cache.checkXMLKey(key)`, `cache.checkKeys` and `cache.removeXML(key)`.
8183

8284

8385
### Updates

src/loader/Cache.js

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ Phaser.Cache = function (game) {
5555
*/
5656
this._json = {};
5757

58+
/**
59+
* @property {object} _xml - XML key-value container.
60+
* @private
61+
*/
62+
this._xml = {};
63+
5864
/**
5965
* @property {object} _physics - Physics data key-value container.
6066
* @private
@@ -109,6 +115,7 @@ Phaser.Cache = function (game) {
109115
this._cacheMap[Phaser.Cache.BITMAPDATA] = this._bitmapDatas;
110116
this._cacheMap[Phaser.Cache.BITMAPFONT] = this._bitmapFont;
111117
this._cacheMap[Phaser.Cache.JSON] = this._json;
118+
this._cacheMap[Phaser.Cache.XML] = this._xml;
112119

113120
};
114121

@@ -178,6 +185,12 @@ Phaser.Cache.BITMAPFONT = 10;
178185
*/
179186
Phaser.Cache.JSON = 11;
180187

188+
/**
189+
* @constant
190+
* @type {number}
191+
*/
192+
Phaser.Cache.XML = 12;
193+
181194
Phaser.Cache.prototype = {
182195

183196
/**
@@ -408,16 +421,30 @@ Phaser.Cache.prototype = {
408421
* Add a new json object into the cache.
409422
*
410423
* @method Phaser.Cache#addJSON
411-
* @param {string} key - Asset key for the text data.
412-
* @param {string} url - URL of this text data file.
413-
* @param {object} data - Extra text data.
424+
* @param {string} key - Asset key for the json data.
425+
* @param {string} url - URL of this json data file.
426+
* @param {object} data - Extra json data.
414427
*/
415428
addJSON: function (key, url, data) {
416429

417430
this._json[key] = { url: url, data: data };
418431

419432
},
420433

434+
/**
435+
* Add a new xml object into the cache.
436+
*
437+
* @method Phaser.Cache#addXML
438+
* @param {string} key - Asset key for the xml file.
439+
* @param {string} url - URL of this xml file.
440+
* @param {object} data - Extra text data.
441+
*/
442+
addXML: function (key, url, data) {
443+
444+
this._xml[key] = { url: url, data: data };
445+
446+
},
447+
421448
/**
422449
* Adds an Image file into the Cache. The file must have already been loaded, typically via Phaser.Loader.
423450
*
@@ -679,7 +706,7 @@ Phaser.Cache.prototype = {
679706
* Checks if the given key exists in the Canvas Cache.
680707
*
681708
* @method Phaser.Cache#checkCanvasKey
682-
* @param {string} key - Asset key of the image to check is in the Cache.
709+
* @param {string} key - Asset key of the canvas to check is in the Cache.
683710
* @return {boolean} True if the key exists, otherwise false.
684711
*/
685712
checkCanvasKey: function (key) {
@@ -718,7 +745,7 @@ Phaser.Cache.prototype = {
718745
* Checks if the given key exists in the Sound Cache.
719746
*
720747
* @method Phaser.Cache#checkSoundKey
721-
* @param {string} key - Asset key of the image to check is in the Cache.
748+
* @param {string} key - Asset key of the sound file to check is in the Cache.
722749
* @return {boolean} True if the key exists, otherwise false.
723750
*/
724751
checkSoundKey: function (key) {
@@ -731,7 +758,7 @@ Phaser.Cache.prototype = {
731758
* Checks if the given key exists in the Text Cache.
732759
*
733760
* @method Phaser.Cache#checkTextKey
734-
* @param {string} key - Asset key of the image to check is in the Cache.
761+
* @param {string} key - Asset key of the text file to check is in the Cache.
735762
* @return {boolean} True if the key exists, otherwise false.
736763
*/
737764
checkTextKey: function (key) {
@@ -744,7 +771,7 @@ Phaser.Cache.prototype = {
744771
* Checks if the given key exists in the Physics Cache.
745772
*
746773
* @method Phaser.Cache#checkPhysicsKey
747-
* @param {string} key - Asset key of the image to check is in the Cache.
774+
* @param {string} key - Asset key of the physics data file to check is in the Cache.
748775
* @return {boolean} True if the key exists, otherwise false.
749776
*/
750777
checkPhysicsKey: function (key) {
@@ -757,7 +784,7 @@ Phaser.Cache.prototype = {
757784
* Checks if the given key exists in the Tilemap Cache.
758785
*
759786
* @method Phaser.Cache#checkTilemapKey
760-
* @param {string} key - Asset key of the image to check is in the Cache.
787+
* @param {string} key - Asset key of the Tilemap to check is in the Cache.
761788
* @return {boolean} True if the key exists, otherwise false.
762789
*/
763790
checkTilemapKey: function (key) {
@@ -770,7 +797,7 @@ Phaser.Cache.prototype = {
770797
* Checks if the given key exists in the Binary Cache.
771798
*
772799
* @method Phaser.Cache#checkBinaryKey
773-
* @param {string} key - Asset key of the image to check is in the Cache.
800+
* @param {string} key - Asset key of the binary file to check is in the Cache.
774801
* @return {boolean} True if the key exists, otherwise false.
775802
*/
776803
checkBinaryKey: function (key) {
@@ -783,7 +810,7 @@ Phaser.Cache.prototype = {
783810
* Checks if the given key exists in the BitmapData Cache.
784811
*
785812
* @method Phaser.Cache#checkBitmapDataKey
786-
* @param {string} key - Asset key of the image to check is in the Cache.
813+
* @param {string} key - Asset key of the BitmapData to check is in the Cache.
787814
* @return {boolean} True if the key exists, otherwise false.
788815
*/
789816
checkBitmapDataKey: function (key) {
@@ -796,7 +823,7 @@ Phaser.Cache.prototype = {
796823
* Checks if the given key exists in the BitmapFont Cache.
797824
*
798825
* @method Phaser.Cache#checkBitmapFontKey
799-
* @param {string} key - Asset key of the image to check is in the Cache.
826+
* @param {string} key - Asset key of the BitmapFont to check is in the Cache.
800827
* @return {boolean} True if the key exists, otherwise false.
801828
*/
802829
checkBitmapFontKey: function (key) {
@@ -809,7 +836,7 @@ Phaser.Cache.prototype = {
809836
* Checks if the given key exists in the JSON Cache.
810837
*
811838
* @method Phaser.Cache#checkJSONKey
812-
* @param {string} key - Asset key of the image to check is in the Cache.
839+
* @param {string} key - Asset key of the JSON file to check is in the Cache.
813840
* @return {boolean} True if the key exists, otherwise false.
814841
*/
815842
checkJSONKey: function (key) {
@@ -818,6 +845,19 @@ Phaser.Cache.prototype = {
818845

819846
},
820847

848+
/**
849+
* Checks if the given key exists in the XML Cache.
850+
*
851+
* @method Phaser.Cache#checkXMLKey
852+
* @param {string} key - Asset key of the XML file to check is in the Cache.
853+
* @return {boolean} True if the key exists, otherwise false.
854+
*/
855+
checkXMLKey: function (key) {
856+
857+
return this.checkKey(Phaser.Cache.XML, key);
858+
859+
},
860+
821861
/**
822862
* Get image data by key.
823863
*
@@ -1107,6 +1147,26 @@ Phaser.Cache.prototype = {
11071147

11081148
},
11091149

1150+
/**
1151+
* Get a XML object by key from the cache.
1152+
*
1153+
* @method Phaser.Cache#getXML
1154+
* @param {string} key - Asset key of the XML object to retrieve from the Cache.
1155+
* @return {object} The XML object.
1156+
*/
1157+
getXML: function (key) {
1158+
1159+
if (this._xml[key])
1160+
{
1161+
return this._xml[key].data;
1162+
}
1163+
else
1164+
{
1165+
console.warn('Phaser.Cache.getXML: Invalid key: "' + key + '"');
1166+
}
1167+
1168+
},
1169+
11101170
/**
11111171
* Get binary data by key.
11121172
*
@@ -1183,6 +1243,10 @@ Phaser.Cache.prototype = {
11831243
case Phaser.Cache.JSON:
11841244
array = this._json;
11851245
break;
1246+
1247+
case Phaser.Cache.XML:
1248+
array = this._xml;
1249+
break;
11861250
}
11871251

11881252
if (!array)
@@ -1264,6 +1328,16 @@ Phaser.Cache.prototype = {
12641328
delete this._json[key];
12651329
},
12661330

1331+
/**
1332+
* Removes a xml object from the cache.
1333+
*
1334+
* @method Phaser.Cache#removeXML
1335+
* @param {string} key - Key of the asset you want to remove.
1336+
*/
1337+
removeXML: function (key) {
1338+
delete this._xml[key];
1339+
},
1340+
12671341
/**
12681342
* Removes a physics data file from the cache.
12691343
*
@@ -1349,6 +1423,11 @@ Phaser.Cache.prototype = {
13491423
delete this._json[item];
13501424
}
13511425

1426+
for (var item in this._xml)
1427+
{
1428+
delete this._xml[item];
1429+
}
1430+
13521431
for (var item in this._textures)
13531432
{
13541433
delete this._textures[item];

src/loader/Loader.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,32 @@ Phaser.Loader.prototype = {
499499

500500
},
501501

502+
/**
503+
* Add an XML file to the Loader.
504+
*
505+
* @method Phaser.Loader#xml
506+
* @param {string} key - Unique asset key of the xml file.
507+
* @param {string} url - URL of the xml file.
508+
* @param {boolean} [overwrite=false] - If an unloaded file with a matching key already exists in the queue, this entry will overwrite it.
509+
* @return {Phaser.Loader} This Loader instance.
510+
*/
511+
xml: function (key, url, overwrite) {
512+
513+
if (typeof overwrite === "undefined") { overwrite = false; }
514+
515+
if (overwrite)
516+
{
517+
this.replaceInFileList('xml', key, url);
518+
}
519+
else
520+
{
521+
this.addToFileList('xml', key, url);
522+
}
523+
524+
return this;
525+
526+
},
527+
502528
/**
503529
* Add a JavaScript file to the Loader. Once loaded the JavaScript file will be automatically turned into a script tag (and executed), so be careful what you load!
504530
* You can also specify a callback. This will be executed as soon as the script tag has been created.
@@ -1042,6 +1068,10 @@ Phaser.Loader.prototype = {
10421068
this.json(file.key, file.url, file.overwrite);
10431069
break;
10441070

1071+
case "xml":
1072+
this.xml(file.key, file.url, file.overwrite);
1073+
break;
1074+
10451075
case "script":
10461076
this.script(file.key, file.url, file.callback, pack.callbackContext);
10471077
break;
@@ -1153,7 +1183,7 @@ Phaser.Loader.prototype = {
11531183
var file = this._fileList[this._fileIndex];
11541184
var _this = this;
11551185

1156-
this.onFileStart.dispatch(this.progress, file.key);
1186+
this.onFileStart.dispatch(this.progress, file.key, file.url);
11571187

11581188
// Image or Data?
11591189
switch (file.type)
@@ -1259,6 +1289,11 @@ Phaser.Loader.prototype = {
12591289

12601290
break;
12611291

1292+
case 'xml':
1293+
1294+
this.xhrLoad(this._fileIndex, this.baseURL + file.url, 'text', 'xmlLoadComplete', 'dataLoadError');
1295+
break;
1296+
12621297
case 'tilemap':
12631298

12641299
if (file.format === Phaser.Tilemap.TILED_JSON)
@@ -1609,6 +1644,12 @@ Phaser.Loader.prototype = {
16091644
*/
16101645
xmlLoadComplete: function (index) {
16111646

1647+
if (this._xhr.responseType !== '' && this._xhr.responseType !== 'text')
1648+
{
1649+
console.warn('Invalid XML Response Type', this._fileList[index]);
1650+
console.warn(this._xhr);
1651+
}
1652+
16121653
var data = this._xhr.responseText;
16131654
var xml;
16141655

@@ -1639,14 +1680,18 @@ Phaser.Loader.prototype = {
16391680
var file = this._fileList[index];
16401681
file.loaded = true;
16411682

1642-
if (file.type == 'bitmapfont')
1683+
if (file.type === 'bitmapfont')
16431684
{
16441685
this.game.cache.addBitmapFont(file.key, file.url, file.data, xml, file.xSpacing, file.ySpacing);
16451686
}
1646-
else if (file.type == 'textureatlas')
1687+
else if (file.type === 'textureatlas')
16471688
{
16481689
this.game.cache.addTextureAtlas(file.key, file.url, file.data, xml, file.format);
16491690
}
1691+
else if (file.type === 'xml')
1692+
{
1693+
this.game.cache.addXML(file.key, file.url, xml);
1694+
}
16501695

16511696
this.nextFile(index, true);
16521697

0 commit comments

Comments
 (0)