Renamed Texture Parsers to follow conventions elsewhere in the library. Started on SS from Atlas.

This commit is contained in:
photonstorm 2017-04-18 15:31:30 +01:00
parent 88c599cb28
commit 6aa2fb009b
12 changed files with 178 additions and 24 deletions

View file

@ -1,4 +1,4 @@
var CHECKSUM = {
build: 'dd446940-1fd6-11e7-b0f6-9b5f5f41e111'
build: 'bfcbb560-2433-11e7-ac68-e9193d1a804f'
};
module.exports = CHECKSUM;

View file

@ -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
*

View file

@ -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;
}

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -0,0 +1,116 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @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;

View file

@ -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;

View file

@ -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')
};