Skip to content

Commit 6aa2fb0

Browse files
committed
Renamed Texture Parsers to follow conventions elsewhere in the library. Started on SS from Atlas.
1 parent 88c599c commit 6aa2fb0

12 files changed

Lines changed: 178 additions & 24 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: 'dd446940-1fd6-11e7-b0f6-9b5f5f41e111'
2+
build: 'bfcbb560-2433-11e7-ac68-e9193d1a804f'
33
};
44
module.exports = CHECKSUM;

v3/src/textures/Frame.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,22 @@ Object.defineProperties(Frame.prototype, {
370370

371371
},
372372

373+
/**
374+
* Is the Frame trimmed?
375+
* @name Phaser.TextureFrame#trimmed
376+
* @property {boolean} trimmed
377+
*/
378+
trimmed: {
379+
380+
enumerable: true,
381+
382+
get: function ()
383+
{
384+
return this.data.trim;
385+
}
386+
387+
},
388+
373389
/**
374390
* Canvas Draw Image data
375391
*

v3/src/textures/TextureManager.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,28 @@ TextureManager.prototype = {
170170
{
171171
var texture = this.create(key, sheet.source.image);
172172

173-
Parser.SpriteSheet(texture, 0, sheet.cutX, sheet.cutY, sheet.cutWidth, sheet.cutHeight, config);
173+
// {
174+
// "filename": "explosion",
175+
// "frame": {"x":2,"y":2,"w":319,"h":312}, = cutX, Y, W, H
176+
// "rotated": false,
177+
// "trimmed": true,
178+
// "spriteSourceSize": {"x":1,"y":6,"w":319,"h":312},
179+
// "sourceSize": {"w":320,"h":320},
180+
// "pivot": {"x":0.5,"y":0.5}
181+
// },
182+
183+
// If trimmed we need to help the parser adjust
184+
185+
console.log(sheet);
186+
187+
if (sheet.trimmed)
188+
{
189+
Parser.SpriteSheetFromAtlas(texture, sheet, config);
190+
}
191+
else
192+
{
193+
Parser.SpriteSheet(texture, 0, sheet.cutX, sheet.cutY, sheet.cutWidth, sheet.cutHeight, config);
194+
}
174195

175196
return texture;
176197
}

v3/src/textures/parsers/ImageTextureParser.js renamed to v3/src/textures/parsers/Canvas.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @param {string} key - The key of the Frame within the Texture that the Sprite Sheet is stored in.
1414
* @return {Phaser.FrameData} A FrameData object containing the parsed frames.
1515
*/
16-
var ImageTextureParser = function (texture, sourceIndex)
16+
var Canvas = function (texture, sourceIndex)
1717
{
1818
var source = texture.source[sourceIndex];
1919

@@ -22,4 +22,4 @@ var ImageTextureParser = function (texture, sourceIndex)
2222
return texture;
2323
};
2424

25-
module.exports = ImageTextureParser;
25+
module.exports = Canvas;

v3/src/textures/parsers/CanvasTextureParser.js renamed to v3/src/textures/parsers/Image.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @param {string} key - The key of the Frame within the Texture that the Sprite Sheet is stored in.
1414
* @return {Phaser.FrameData} A FrameData object containing the parsed frames.
1515
*/
16-
var CanvasTextureParser = function (texture, sourceIndex)
16+
var Image = function (texture, sourceIndex)
1717
{
1818
var source = texture.source[sourceIndex];
1919

@@ -22,4 +22,4 @@ var CanvasTextureParser = function (texture, sourceIndex)
2222
return texture;
2323
};
2424

25-
module.exports = CanvasTextureParser;
25+
module.exports = Image;

v3/src/textures/parsers/JSONArrayTextureParser.js renamed to v3/src/textures/parsers/JSONArray.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @param {object} json - The JSON data from the Texture Atlas. Must be in Array format.
1414
* @return {Phaser.FrameData} A FrameData object containing the parsed frames.
1515
*/
16-
var JSONArrayTextureParser = function (texture, sourceIndex, json)
16+
var JSONArray = function (texture, sourceIndex, json)
1717
{
1818
// Malformed?
1919
if (!json['frames'])
@@ -60,4 +60,4 @@ var JSONArrayTextureParser = function (texture, sourceIndex, json)
6060
return texture;
6161
};
6262

63-
module.exports = JSONArrayTextureParser;
63+
module.exports = JSONArray;

v3/src/textures/parsers/JSONHashTextureParser.js renamed to v3/src/textures/parsers/JSONHash.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @param {object} json - The JSON data from the Texture Atlas. Must be in JSON Hash format.
1414
* @return {Phaser.FrameData} A FrameData object containing the parsed frames.
1515
*/
16-
var JSONHashTextureParser = function (texture, sourceIndex, json)
16+
var JSONHash = function (texture, sourceIndex, json)
1717
{
1818
// Malformed?
1919
if (!json['frames'])
@@ -60,4 +60,4 @@ var JSONHashTextureParser = function (texture, sourceIndex, json)
6060
return texture;
6161
};
6262

63-
module.exports = JSONHashTextureParser;
63+
module.exports = JSONHash;

v3/src/textures/parsers/PyxelTextureParser.js renamed to v3/src/textures/parsers/Pyxel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @param {object} json - The JSON data from the Texture Atlas. Must be in Pyxel JSON format.
1414
* @return {Phaser.FrameData} A FrameData object containing the parsed frames.
1515
*/
16-
var PyxelTextureParser = function (texture, json)
16+
var Pyxel = function (texture, json)
1717
{
1818
// Malformed? There are a few keys to check here.
1919
var signature = [ 'layers', 'tilewidth', 'tileheight', 'tileswide', 'tileshigh' ];
@@ -62,4 +62,4 @@ var PyxelTextureParser = function (texture, json)
6262
return data;
6363
};
6464

65-
module.exports = PyxelTextureParser;
65+
module.exports = Pyxel;

v3/src/textures/parsers/SpriteSheetTextureParser.js renamed to v3/src/textures/parsers/SpriteSheet.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ var GetObjectValue = require('../../utils/object/GetObjectValue');
2121
* @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
2222
* @return {Phaser.FrameData} A FrameData object containing the parsed frames.
2323
*/
24-
var SpriteSheetTextureParser = function (texture, sourceIndex, x, y, width, height, config)
24+
var SpriteSheet = function (texture, sourceIndex, x, y, width, height, config)
2525
{
2626
var frameWidth = GetObjectValue(config, 'frameWidth', null);
2727
var frameHeight = GetObjectValue(config, 'frameHeight', frameWidth);
2828

2929
// If missing we can't proceed
3030
if (frameWidth === null)
3131
{
32-
throw new Error('TextureManager.SpriteSheetTextureParser: Invalid frameWidth given.');
32+
throw new Error('TextureManager.SpriteSheet: Invalid frameWidth given.');
3333
}
3434

3535
// Add in a __BASE entry (for the entire atlas)
@@ -99,4 +99,4 @@ var SpriteSheetTextureParser = function (texture, sourceIndex, x, y, width, heig
9999
return texture;
100100
};
101101

102-
module.exports = SpriteSheetTextureParser;
102+
module.exports = SpriteSheet;
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2016 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var GetObjectValue = require('../../utils/object/GetObjectValue');
8+
9+
/**
10+
* Parse a Sprite Sheet and extracts the frame data from it.
11+
*
12+
* @class Phaser.TextureParser.SpriteSheet
13+
* @static
14+
* @param {Phaser.Texture} texture - The parent Texture.
15+
* @param {string} key - The key of the Frame within the Texture that the Sprite Sheet is stored in.
16+
* @param {number} frameWidth - The fixed width of each frame.
17+
* @param {number} frameHeight - The fixed height of each frame.
18+
* @param {number} [startFrame=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one Texture.
19+
* @param {number} [endFrame=-1] - The total number of frames to extract from the Sprite Sheet. The default value of -1 means "extract all frames".
20+
* @param {number} [margin=0] - If the frames have been drawn with a margin, specify the amount here.
21+
* @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
22+
* @return {Phaser.FrameData} A FrameData object containing the parsed frames.
23+
*/
24+
var SpriteSheetFromAtlas = function (texture, frame, config)
25+
{
26+
var frameWidth = GetObjectValue(config, 'frameWidth', null);
27+
var frameHeight = GetObjectValue(config, 'frameHeight', frameWidth);
28+
29+
// If missing we can't proceed
30+
if (!frameWidth)
31+
{
32+
throw new Error('TextureManager.SpriteSheetFromAtlas: Invalid frameWidth given.');
33+
}
34+
35+
// Add in a __BASE entry (for the entire atlas)
36+
// var source = texture.source[0];
37+
// texture.add('__BASE', 0, 0, 0, source.width, source.height);
38+
39+
var startFrame = GetObjectValue(config, 'startFrame', 0);
40+
var endFrame = GetObjectValue(config, 'endFrame', -1);
41+
var margin = GetObjectValue(config, 'margin', 0);
42+
var spacing = GetObjectValue(config, 'spacing', 0);
43+
44+
var x = frame.cutX;
45+
var y = frame.cutY;
46+
var cutWidth = frame.cutWidth;
47+
var cutHeight = frame.cutHeight;
48+
var sheetWidth = frame.realWidth;
49+
var sheetHeight = frame.realHeight;
50+
51+
var row = Math.floor((sheetWidth - margin) / (frameWidth + spacing));
52+
var column = Math.floor((sheetHeight - margin) / (frameHeight + spacing));
53+
var total = row * column;
54+
55+
console.log('split sheet into rows/cols:', row, column, 'total frames:', total);
56+
57+
if (startFrame > total || startFrame < -total)
58+
{
59+
startFrame = 0;
60+
}
61+
62+
if (startFrame < 0)
63+
{
64+
// Allow negative skipframes.
65+
startFrame = total + startFrame;
66+
}
67+
68+
if (endFrame !== -1)
69+
{
70+
total = startFrame + (endFrame + 1);
71+
}
72+
73+
var fx = margin;
74+
var fy = margin;
75+
var ax = 0;
76+
var ay = 0;
77+
var sheetFrame;
78+
79+
for (var i = 0; i < total; i++)
80+
{
81+
ax = 0;
82+
ay = 0;
83+
84+
var w = fx + frameWidth;
85+
var h = fy + frameHeight;
86+
87+
if (w > width)
88+
{
89+
ax = w - width;
90+
}
91+
92+
if (h > height)
93+
{
94+
ay = h - height;
95+
}
96+
97+
sheetFrame = texture.add(i, sourceIndex, x + fx, y + fy, frameWidth - ax, frameHeight - ay);
98+
99+
// sheetFrame.setTrim(sheetWidth, sheetHeight, )
100+
101+
// setTrim: function (actualWidth, actualHeight, destX, destY, destWidth, destHeight)
102+
103+
104+
fx += frameWidth + spacing;
105+
106+
if (fx + frameWidth > width)
107+
{
108+
fx = margin;
109+
fy += frameHeight + spacing;
110+
}
111+
}
112+
113+
return texture;
114+
};
115+
116+
module.exports = SpriteSheetFromAtlas;

0 commit comments

Comments
 (0)