From 6aa2fb009b68d0aee1ed97eb47e80309210626cf Mon Sep 17 00:00:00 2001 From: photonstorm Date: Tue, 18 Apr 2017 15:31:30 +0100 Subject: [PATCH] Renamed Texture Parsers to follow conventions elsewhere in the library. Started on SS from Atlas. --- v3/src/checksum.js | 2 +- v3/src/textures/Frame.js | 16 +++ v3/src/textures/TextureManager.js | 23 +++- .../{ImageTextureParser.js => Canvas.js} | 4 +- .../{CanvasTextureParser.js => Image.js} | 4 +- ...JSONArrayTextureParser.js => JSONArray.js} | 4 +- .../{JSONHashTextureParser.js => JSONHash.js} | 4 +- .../{PyxelTextureParser.js => Pyxel.js} | 4 +- ...teSheetTextureParser.js => SpriteSheet.js} | 6 +- .../textures/parsers/SpriteSheetFromAtlas.js | 116 ++++++++++++++++++ ...lingXMLTextureParser.js => StarlingXML.js} | 4 +- v3/src/textures/parsers/index.js | 15 +-- 12 files changed, 178 insertions(+), 24 deletions(-) rename v3/src/textures/parsers/{ImageTextureParser.js => Canvas.js} (88%) rename v3/src/textures/parsers/{CanvasTextureParser.js => Image.js} (87%) rename v3/src/textures/parsers/{JSONArrayTextureParser.js => JSONArray.js} (94%) rename v3/src/textures/parsers/{JSONHashTextureParser.js => JSONHash.js} (94%) rename v3/src/textures/parsers/{PyxelTextureParser.js => Pyxel.js} (95%) rename v3/src/textures/parsers/{SpriteSheetTextureParser.js => SpriteSheet.js} (92%) create mode 100644 v3/src/textures/parsers/SpriteSheetFromAtlas.js rename v3/src/textures/parsers/{StarlingXMLTextureParser.js => StarlingXML.js} (95%) diff --git a/v3/src/checksum.js b/v3/src/checksum.js index 7ddca0186..0e62453ca 100644 --- a/v3/src/checksum.js +++ b/v3/src/checksum.js @@ -1,4 +1,4 @@ var CHECKSUM = { -build: 'dd446940-1fd6-11e7-b0f6-9b5f5f41e111' +build: 'bfcbb560-2433-11e7-ac68-e9193d1a804f' }; module.exports = CHECKSUM; \ No newline at end of file diff --git a/v3/src/textures/Frame.js b/v3/src/textures/Frame.js index a134930c6..424db188d 100644 --- a/v3/src/textures/Frame.js +++ b/v3/src/textures/Frame.js @@ -370,6 +370,22 @@ Object.defineProperties(Frame.prototype, { }, + /** + * Is the Frame trimmed? + * @name Phaser.TextureFrame#trimmed + * @property {boolean} trimmed + */ + trimmed: { + + enumerable: true, + + get: function () + { + return this.data.trim; + } + + }, + /** * Canvas Draw Image data * diff --git a/v3/src/textures/TextureManager.js b/v3/src/textures/TextureManager.js index d6deaf27e..24581a4c2 100644 --- a/v3/src/textures/TextureManager.js +++ b/v3/src/textures/TextureManager.js @@ -170,7 +170,28 @@ TextureManager.prototype = { { var texture = this.create(key, sheet.source.image); - Parser.SpriteSheet(texture, 0, sheet.cutX, sheet.cutY, sheet.cutWidth, sheet.cutHeight, config); + // { + // "filename": "explosion", + // "frame": {"x":2,"y":2,"w":319,"h":312}, = cutX, Y, W, H + // "rotated": false, + // "trimmed": true, + // "spriteSourceSize": {"x":1,"y":6,"w":319,"h":312}, + // "sourceSize": {"w":320,"h":320}, + // "pivot": {"x":0.5,"y":0.5} + // }, + + // If trimmed we need to help the parser adjust + + console.log(sheet); + + if (sheet.trimmed) + { + Parser.SpriteSheetFromAtlas(texture, sheet, config); + } + else + { + Parser.SpriteSheet(texture, 0, sheet.cutX, sheet.cutY, sheet.cutWidth, sheet.cutHeight, config); + } return texture; } diff --git a/v3/src/textures/parsers/ImageTextureParser.js b/v3/src/textures/parsers/Canvas.js similarity index 88% rename from v3/src/textures/parsers/ImageTextureParser.js rename to v3/src/textures/parsers/Canvas.js index 2c15ca593..37b1dd69a 100644 --- a/v3/src/textures/parsers/ImageTextureParser.js +++ b/v3/src/textures/parsers/Canvas.js @@ -13,7 +13,7 @@ * @param {string} key - The key of the Frame within the Texture that the Sprite Sheet is stored in. * @return {Phaser.FrameData} A FrameData object containing the parsed frames. */ -var ImageTextureParser = function (texture, sourceIndex) +var Canvas = function (texture, sourceIndex) { var source = texture.source[sourceIndex]; @@ -22,4 +22,4 @@ var ImageTextureParser = function (texture, sourceIndex) return texture; }; -module.exports = ImageTextureParser; +module.exports = Canvas; diff --git a/v3/src/textures/parsers/CanvasTextureParser.js b/v3/src/textures/parsers/Image.js similarity index 87% rename from v3/src/textures/parsers/CanvasTextureParser.js rename to v3/src/textures/parsers/Image.js index 0a22828c3..d659b5621 100644 --- a/v3/src/textures/parsers/CanvasTextureParser.js +++ b/v3/src/textures/parsers/Image.js @@ -13,7 +13,7 @@ * @param {string} key - The key of the Frame within the Texture that the Sprite Sheet is stored in. * @return {Phaser.FrameData} A FrameData object containing the parsed frames. */ -var CanvasTextureParser = function (texture, sourceIndex) +var Image = function (texture, sourceIndex) { var source = texture.source[sourceIndex]; @@ -22,4 +22,4 @@ var CanvasTextureParser = function (texture, sourceIndex) return texture; }; -module.exports = CanvasTextureParser; +module.exports = Image; diff --git a/v3/src/textures/parsers/JSONArrayTextureParser.js b/v3/src/textures/parsers/JSONArray.js similarity index 94% rename from v3/src/textures/parsers/JSONArrayTextureParser.js rename to v3/src/textures/parsers/JSONArray.js index 9e7786478..7050ef40d 100644 --- a/v3/src/textures/parsers/JSONArrayTextureParser.js +++ b/v3/src/textures/parsers/JSONArray.js @@ -13,7 +13,7 @@ * @param {object} json - The JSON data from the Texture Atlas. Must be in Array format. * @return {Phaser.FrameData} A FrameData object containing the parsed frames. */ -var JSONArrayTextureParser = function (texture, sourceIndex, json) +var JSONArray = function (texture, sourceIndex, json) { // Malformed? if (!json['frames']) @@ -60,4 +60,4 @@ var JSONArrayTextureParser = function (texture, sourceIndex, json) return texture; }; -module.exports = JSONArrayTextureParser; +module.exports = JSONArray; diff --git a/v3/src/textures/parsers/JSONHashTextureParser.js b/v3/src/textures/parsers/JSONHash.js similarity index 94% rename from v3/src/textures/parsers/JSONHashTextureParser.js rename to v3/src/textures/parsers/JSONHash.js index ecd979aca..5ea35d694 100644 --- a/v3/src/textures/parsers/JSONHashTextureParser.js +++ b/v3/src/textures/parsers/JSONHash.js @@ -13,7 +13,7 @@ * @param {object} json - The JSON data from the Texture Atlas. Must be in JSON Hash format. * @return {Phaser.FrameData} A FrameData object containing the parsed frames. */ -var JSONHashTextureParser = function (texture, sourceIndex, json) +var JSONHash = function (texture, sourceIndex, json) { // Malformed? if (!json['frames']) @@ -60,4 +60,4 @@ var JSONHashTextureParser = function (texture, sourceIndex, json) return texture; }; -module.exports = JSONHashTextureParser; +module.exports = JSONHash; diff --git a/v3/src/textures/parsers/PyxelTextureParser.js b/v3/src/textures/parsers/Pyxel.js similarity index 95% rename from v3/src/textures/parsers/PyxelTextureParser.js rename to v3/src/textures/parsers/Pyxel.js index 80f50d07c..80cb16bd2 100644 --- a/v3/src/textures/parsers/PyxelTextureParser.js +++ b/v3/src/textures/parsers/Pyxel.js @@ -13,7 +13,7 @@ * @param {object} json - The JSON data from the Texture Atlas. Must be in Pyxel JSON format. * @return {Phaser.FrameData} A FrameData object containing the parsed frames. */ -var PyxelTextureParser = function (texture, json) +var Pyxel = function (texture, json) { // Malformed? There are a few keys to check here. var signature = [ 'layers', 'tilewidth', 'tileheight', 'tileswide', 'tileshigh' ]; @@ -62,4 +62,4 @@ var PyxelTextureParser = function (texture, json) return data; }; -module.exports = PyxelTextureParser; +module.exports = Pyxel; diff --git a/v3/src/textures/parsers/SpriteSheetTextureParser.js b/v3/src/textures/parsers/SpriteSheet.js similarity index 92% rename from v3/src/textures/parsers/SpriteSheetTextureParser.js rename to v3/src/textures/parsers/SpriteSheet.js index ddcb3553a..23da632c3 100644 --- a/v3/src/textures/parsers/SpriteSheetTextureParser.js +++ b/v3/src/textures/parsers/SpriteSheet.js @@ -21,7 +21,7 @@ var GetObjectValue = require('../../utils/object/GetObjectValue'); * @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here. * @return {Phaser.FrameData} A FrameData object containing the parsed frames. */ -var SpriteSheetTextureParser = function (texture, sourceIndex, x, y, width, height, config) +var SpriteSheet = function (texture, sourceIndex, x, y, width, height, config) { var frameWidth = GetObjectValue(config, 'frameWidth', null); var frameHeight = GetObjectValue(config, 'frameHeight', frameWidth); @@ -29,7 +29,7 @@ var SpriteSheetTextureParser = function (texture, sourceIndex, x, y, width, heig // If missing we can't proceed if (frameWidth === null) { - throw new Error('TextureManager.SpriteSheetTextureParser: Invalid frameWidth given.'); + throw new Error('TextureManager.SpriteSheet: Invalid frameWidth given.'); } // Add in a __BASE entry (for the entire atlas) @@ -99,4 +99,4 @@ var SpriteSheetTextureParser = function (texture, sourceIndex, x, y, width, heig return texture; }; -module.exports = SpriteSheetTextureParser; +module.exports = SpriteSheet; diff --git a/v3/src/textures/parsers/SpriteSheetFromAtlas.js b/v3/src/textures/parsers/SpriteSheetFromAtlas.js new file mode 100644 index 000000000..b17d22d62 --- /dev/null +++ b/v3/src/textures/parsers/SpriteSheetFromAtlas.js @@ -0,0 +1,116 @@ +/** +* @author Richard Davey +* @copyright 2016 Photon Storm Ltd. +* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} +*/ + +var GetObjectValue = require('../../utils/object/GetObjectValue'); + +/** +* Parse a Sprite Sheet and extracts the frame data from it. +* +* @class Phaser.TextureParser.SpriteSheet +* @static +* @param {Phaser.Texture} texture - The parent Texture. +* @param {string} key - The key of the Frame within the Texture that the Sprite Sheet is stored in. +* @param {number} frameWidth - The fixed width of each frame. +* @param {number} frameHeight - The fixed height of each frame. +* @param {number} [startFrame=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one Texture. +* @param {number} [endFrame=-1] - The total number of frames to extract from the Sprite Sheet. The default value of -1 means "extract all frames". +* @param {number} [margin=0] - If the frames have been drawn with a margin, specify the amount here. +* @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here. +* @return {Phaser.FrameData} A FrameData object containing the parsed frames. +*/ +var SpriteSheetFromAtlas = function (texture, frame, config) +{ + var frameWidth = GetObjectValue(config, 'frameWidth', null); + var frameHeight = GetObjectValue(config, 'frameHeight', frameWidth); + + // If missing we can't proceed + if (!frameWidth) + { + throw new Error('TextureManager.SpriteSheetFromAtlas: Invalid frameWidth given.'); + } + + // Add in a __BASE entry (for the entire atlas) + // var source = texture.source[0]; + // texture.add('__BASE', 0, 0, 0, source.width, source.height); + + var startFrame = GetObjectValue(config, 'startFrame', 0); + var endFrame = GetObjectValue(config, 'endFrame', -1); + var margin = GetObjectValue(config, 'margin', 0); + var spacing = GetObjectValue(config, 'spacing', 0); + + var x = frame.cutX; + var y = frame.cutY; + var cutWidth = frame.cutWidth; + var cutHeight = frame.cutHeight; + var sheetWidth = frame.realWidth; + var sheetHeight = frame.realHeight; + + var row = Math.floor((sheetWidth - margin) / (frameWidth + spacing)); + var column = Math.floor((sheetHeight - margin) / (frameHeight + spacing)); + var total = row * column; + + console.log('split sheet into rows/cols:', row, column, 'total frames:', total); + + if (startFrame > total || startFrame < -total) + { + startFrame = 0; + } + + if (startFrame < 0) + { + // Allow negative skipframes. + startFrame = total + startFrame; + } + + if (endFrame !== -1) + { + total = startFrame + (endFrame + 1); + } + + var fx = margin; + var fy = margin; + var ax = 0; + var ay = 0; + var sheetFrame; + + for (var i = 0; i < total; i++) + { + ax = 0; + ay = 0; + + var w = fx + frameWidth; + var h = fy + frameHeight; + + if (w > width) + { + ax = w - width; + } + + if (h > height) + { + ay = h - height; + } + + sheetFrame = texture.add(i, sourceIndex, x + fx, y + fy, frameWidth - ax, frameHeight - ay); + + // sheetFrame.setTrim(sheetWidth, sheetHeight, ) + + // setTrim: function (actualWidth, actualHeight, destX, destY, destWidth, destHeight) + + + fx += frameWidth + spacing; + + if (fx + frameWidth > width) + { + fx = margin; + fy += frameHeight + spacing; + } + } + + return texture; +}; + +module.exports = SpriteSheetFromAtlas; diff --git a/v3/src/textures/parsers/StarlingXMLTextureParser.js b/v3/src/textures/parsers/StarlingXML.js similarity index 95% rename from v3/src/textures/parsers/StarlingXMLTextureParser.js rename to v3/src/textures/parsers/StarlingXML.js index e30a3436d..169dfc54e 100644 --- a/v3/src/textures/parsers/StarlingXMLTextureParser.js +++ b/v3/src/textures/parsers/StarlingXML.js @@ -13,7 +13,7 @@ * @param {object} xml - The XML data from the Texture Atlas. Must be in Starling XML format. * @return {Phaser.FrameData} A FrameData object containing the parsed frames. */ -var StarlingXMLTextureParser = function (texture, xml) +var StarlingXML = function (texture, xml) { // Malformed? if (!xml.getElementsByTagName('TextureAtlas')) @@ -72,4 +72,4 @@ var StarlingXMLTextureParser = function (texture, xml) }; -module.exports = StarlingXMLTextureParser; +module.exports = StarlingXML; diff --git a/v3/src/textures/parsers/index.js b/v3/src/textures/parsers/index.js index 4db0a30e8..e809195f2 100644 --- a/v3/src/textures/parsers/index.js +++ b/v3/src/textures/parsers/index.js @@ -1,10 +1,11 @@ module.exports = { - Canvas: require('./CanvasTextureParser'), - Image: require('./ImageTextureParser'), - SpriteSheet: require('./SpriteSheetTextureParser'), - JSONArray: require('./JSONArrayTextureParser'), - JSONHash: require('./JSONHashTextureParser'), - StarlingXML: require('./StarlingXMLTextureParser'), - Pyxel: require('./PyxelTextureParser') + Canvas: require('./Canvas'), + Image: require('./Image'), + JSONArray: require('./JSONArray'), + JSONHash: require('./JSONHash'), + Pyxel: require('./Pyxel'), + SpriteSheet: require('./SpriteSheet'), + SpriteSheetFromAtlas: require('./SpriteSheetFromAtlas'), + StarlingXML: require('./StarlingXML') };