diff --git a/src/textures/TextureManager.js b/src/textures/TextureManager.js index f50e8d9cc..1ad18032b 100644 --- a/src/textures/TextureManager.js +++ b/src/textures/TextureManager.js @@ -627,10 +627,10 @@ var TextureManager = new Class({ /** * Adds a Texture Atlas to this Texture Manager, where the atlas data is given - * in the Starling XML format. + * in the XML format. * - * @method Phaser.Textures.TextureManager#addAtlasStarlingXML - * @since 3.0.0 + * @method Phaser.Textures.TextureManager#addAtlasXML + * @since 3.7.0 * * @param {string} key - The unique string-based key of the Texture. * @param {HTMLImageElement} source - The source Image element. @@ -638,64 +638,15 @@ var TextureManager = new Class({ * * @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use. */ - addAtlasStarlingXML: function (key, source, data) + addAtlasXML: function (key, source, data) { var texture = null; if (this.checkKey(key)) { texture = this.create(key, source); - - if (Array.isArray(data)) - { - for (var i = 0; i < data.length; i++) - { - Parser.StarlingXML(texture, i, data[i]); - } - } - else - { - Parser.StarlingXML(texture, 0, data); - } - - this.emit('addtexture', key, texture); - } - - return texture; - }, - - /** - * Adds a Texture Atlas to this Texture Manager, where the atlas data is given - * in the Pyxel JSON format. - * - * @method Phaser.Textures.TextureManager#addAtlasPyxel - * @since 3.0.0 - * - * @param {string} key - The unique string-based key of the Texture. - * @param {HTMLImageElement} source - The source Image element. - * @param {object} data - The Texture Atlas XML data. - * - * @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use. - */ - addAtlasPyxel: function (key, source, data) - { - var texture = null; - - if (this.checkKey(key)) - { - texture = this.create(key, source); - - if (Array.isArray(data)) - { - for (var i = 0; i < data.length; i++) - { - Parser.Pyxel(texture, i, data[i]); - } - } - else - { - Parser.Pyxel(texture, 0, data); - } + + Parser.AtlasXML(texture, 0, data); this.emit('addtexture', key, texture); } diff --git a/src/textures/parsers/AtlasXML.js b/src/textures/parsers/AtlasXML.js new file mode 100644 index 000000000..799fcc225 --- /dev/null +++ b/src/textures/parsers/AtlasXML.js @@ -0,0 +1,75 @@ +/** + * @author Richard Davey + * @copyright 2018 Photon Storm Ltd. + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} + */ + +/** + * Parses an XML Texture Atlas object and adds all the Frames into a Texture. + * + * @function Phaser.Textures.Parsers.AtlasXML + * @memberOf Phaser.Textures.Parsers + * @private + * @since 3.7.0 + * + * @param {Phaser.Textures.Texture} texture - The Texture to add the Frames to. + * @param {integer} sourceIndex - The index of the TextureSource. + * @param {*} xml - The XML data. + * + * @return {Phaser.Textures.Texture} The Texture modified by this parser. + */ +var AtlasXML = function (texture, sourceIndex, xml) +{ + // Malformed? + if (!xml.getElementsByTagName('TextureAtlas')) + { + console.warn('Invalid Texture Atlas XML given'); + return; + } + + // Add in a __BASE entry (for the entire atlas) + var source = texture.source[sourceIndex]; + + texture.add('__BASE', sourceIndex, 0, 0, source.width, source.height); + + // By this stage frames is a fully parsed array + var frames = xml.getElementsByTagName('SubTexture'); + + var newFrame; + + for (var i = 0; i < frames.length; i++) + { + var frame = frames[i].attributes; + + var name = frame.name.value; + var x = parseInt(frame.x.value, 10); + var y = parseInt(frame.y.value, 10); + var width = parseInt(frame.width.value, 10); + var height = parseInt(frame.height.value, 10); + + // The frame values are the exact coordinates to cut the frame out of the atlas from + newFrame = texture.add(name, sourceIndex, x, y, width, height); + + // These are the original (non-trimmed) sprite values + if (frame.frameX) + { + frameX = Math.abs(parseInt(frame.frameX.value, 10)); + frameY = Math.abs(parseInt(frame.frameY.value, 10)); + frameWidth = parseInt(frame.frameWidth.value, 10); + frameHeight = parseInt(frame.frameHeight.value, 10); + + newFrame.setTrim( + width, + height, + frameX, + frameY, + frameWidth, + frameHeight + ); + } + } + + return texture; +}; + +module.exports = AtlasXML; diff --git a/src/textures/parsers/Pyxel.js b/src/textures/parsers/Pyxel.js deleted file mode 100644 index 8060c0974..000000000 --- a/src/textures/parsers/Pyxel.js +++ /dev/null @@ -1,69 +0,0 @@ -/** - * @author Richard Davey - * @copyright 2018 Photon Storm Ltd. - * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} - */ - -/** - * Parses a Pyxel JSON object and adds the Frames to a Texture. - * - * @function Phaser.Textures.Parsers.Pyxel - * @memberOf Phaser.Textures.Parsers - * @private - * @since 3.0.0 - * - * @param {Phaser.Textures.Texture} texture - The Texture to add the Frames to. - * @param {object} json - The JSON data. - * - * @return {Phaser.Textures.Texture} The Texture modified by this parser. - */ -var Pyxel = function (texture, json) -{ - // Malformed? There are a few keys to check here. - var signature = [ 'layers', 'tilewidth', 'tileheight', 'tileswide', 'tileshigh' ]; - - signature.forEach(function (key) - { - if (!json[key]) - { - // console.warn('Phaser.AnimationParser.JSONDataPyxel: Invalid Pyxel Tilemap JSON given, missing "' + key + '" key.'); - // console.log(json); - return; - } - }); - - // For this purpose, I only care about parsing tilemaps with a single layer. - if (json['layers'].length !== 1) - { - // console.warn('Phaser.AnimationParser.JSONDataPyxel: Too many layers, this parser only supports flat Tilemaps.'); - // console.log(json); - return; - } - - var data = new Phaser.FrameData(); - - var tileheight = json['tileheight']; - var tilewidth = json['tilewidth']; - - var frames = json['layers'][0]['tiles']; - var newFrame; - - for (var i = 0; i < frames.length; i++) - { - newFrame = data.addFrame(new Phaser.Frame( - i, - frames[i].x, - frames[i].y, - tilewidth, - tileheight, - 'frame_' + i // No names are included in pyxel tilemap data. - )); - - // No trim data is included. - newFrame.setTrim(false); - } - - return data; -}; - -module.exports = Pyxel; diff --git a/src/textures/parsers/StarlingXML.js b/src/textures/parsers/StarlingXML.js deleted file mode 100644 index 862cc36da..000000000 --- a/src/textures/parsers/StarlingXML.js +++ /dev/null @@ -1,79 +0,0 @@ -/** - * @author Richard Davey - * @copyright 2018 Photon Storm Ltd. - * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} - */ - -/** - * Parses a Starling XML object and adds all the Frames into a Texture. - * - * @function Phaser.Textures.Parsers.StarlingXML - * @memberOf Phaser.Textures.Parsers - * @private - * @since 3.0.0 - * - * @param {Phaser.Textures.Texture} texture - The Texture to add the Frames to. - * @param {*} xml - The XML data. - * - * @return {Phaser.Textures.Texture} The Texture modified by this parser. - */ -var StarlingXML = function (texture, xml) -{ - // Malformed? - if (!xml.getElementsByTagName('TextureAtlas')) - { - // console.warn("Phaser.AnimationParser.XMLData: Invalid Texture Atlas XML given, missing tag"); - return; - } - - // Let's create some frames then - var data = new Phaser.FrameData(); - var frames = xml.getElementsByTagName('SubTexture'); - var newFrame; - - var name; - var frame; - var x; - var y; - var width; - var height; - var frameX; - var frameY; - var frameWidth; - var frameHeight; - - for (var i = 0; i < frames.length; i++) - { - frame = frames[i].attributes; - - name = frame.name.value; - x = parseInt(frame.x.value, 10); - y = parseInt(frame.y.value, 10); - width = parseInt(frame.width.value, 10); - height = parseInt(frame.height.value, 10); - - frameX = null; - frameY = null; - - if (frame.frameX) - { - frameX = Math.abs(parseInt(frame.frameX.value, 10)); - frameY = Math.abs(parseInt(frame.frameY.value, 10)); - frameWidth = parseInt(frame.frameWidth.value, 10); - frameHeight = parseInt(frame.frameHeight.value, 10); - } - - newFrame = data.addFrame(new Phaser.Frame(i, x, y, width, height, name)); - - // Trimmed? - if (frameX !== null || frameY !== null) - { - newFrame.setTrim(true, width, height, frameX, frameY, frameWidth, frameHeight); - } - } - - return data; - -}; - -module.exports = StarlingXML; diff --git a/src/textures/parsers/index.js b/src/textures/parsers/index.js index 8aaeb0d8c..7baf9c3c7 100644 --- a/src/textures/parsers/index.js +++ b/src/textures/parsers/index.js @@ -10,14 +10,13 @@ module.exports = { + AtlasXML: require('./AtlasXML'), Canvas: require('./Canvas'), Image: require('./Image'), JSONArray: require('./JSONArray'), JSONHash: require('./JSONHash'), - Pyxel: require('./Pyxel'), SpriteSheet: require('./SpriteSheet'), SpriteSheetFromAtlas: require('./SpriteSheetFromAtlas'), - StarlingXML: require('./StarlingXML'), UnityYAML: require('./UnityYAML') };