Skip to content

Commit 7c06670

Browse files
committed
Added Loader.SpriteSheet, now using a new config object rather than a bunch of arguments.
1 parent 426e5af commit 7c06670

8 files changed

Lines changed: 67 additions & 17 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: '618b02c0-e81b-11e6-ac0a-578aa8c24501'
2+
build: '9a80e320-ea9a-11e6-9ca7-bb7438e31195'
33
};
44
module.exports = CHECKSUM;

v3/src/loader/BaseLoader.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ var BaseLoader = function ()
1010
// To finish the loader ...
1111
//
1212
// 3) Progress update
13-
// 4) JSON loader
14-
// 5) XML Loader
15-
// 6) Multi File support (atlas + data)
16-
// 7) Atlas Loader
1713

1814
this.events = new EventDispatcher();
1915

v3/src/loader/File.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var XHRLoader = require('./XHRLoader');
44
var XHRSettings = require('./XHRSettings');
55
var MergeXHRSettings = require('./MergeXHRSettings');
66

7-
var File = function (type, key, url, responseType, xhrSettings)
7+
var File = function (type, key, url, responseType, xhrSettings, config)
88
{
99
// file type (image, json, etc) for sorting within the Loader
1010
this.type = type;
@@ -41,6 +41,9 @@ var File = function (type, key, url, responseType, xhrSettings)
4141
// The actual processed file data
4242
this.data = undefined;
4343

44+
// A config object that can be used by file types to store transitional data
45+
this.config = config || {};
46+
4447
// Multipart file? (i.e. an atlas and its json together)
4548
this.linkFile = undefined;
4649
this.linkType = '';

v3/src/loader/filetypes/ImageFile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var CONST = require('../const');
33
var File = require('../File');
44

5-
var ImageFile = function (key, url, path, xhrSettings)
5+
var ImageFile = function (key, url, path, xhrSettings, options)
66
{
77
if (path === undefined) { path = ''; }
88

@@ -20,7 +20,7 @@ var ImageFile = function (key, url, path, xhrSettings)
2020
url = path.concat(url);
2121
}
2222

23-
File.call(this, 'image', key, url, 'blob', xhrSettings);
23+
File.call(this, 'image', key, url, 'blob', xhrSettings, options);
2424
};
2525

2626
ImageFile.prototype = Object.create(File.prototype);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var ImageFile = require('./ImageFile.js');
2+
3+
// config can include: frameWidth, frameHeight, startFrame, endFrame, margin, spacing
4+
5+
var SpriteSheet = function (key, url, config, path, xhrSettings)
6+
{
7+
var image = new ImageFile(key, url, path, xhrSettings, config);
8+
9+
// Override the File type
10+
image.type = 'spritesheet';
11+
12+
return image;
13+
};
14+
15+
module.exports = SpriteSheet;

v3/src/state/systems/Loader.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var BinaryFile = require('../../loader/filetypes/BinaryFile');
99
var GLSLFile = require('../../loader/filetypes/GLSLFile');
1010
var TextFile = require('../../loader/filetypes/TextFile');
1111
var AtlasJSONFile = require('../../loader/filetypes/AtlasJSONFile');
12+
var SpriteSheet = require('../../loader/filetypes/SpriteSheet');
1213

1314
var Loader = function (state)
1415
{
@@ -68,6 +69,14 @@ Loader.prototype.glsl = function (key, url, xhrSettings)
6869
return this.addFile(file);
6970
};
7071

72+
// config can include: frameWidth, frameHeight, startFrame, endFrame, margin, spacing
73+
Loader.prototype.spritesheet = function (key, url, config, xhrSettings)
74+
{
75+
var file = new SpriteSheet(key, url, config, this.path, xhrSettings);
76+
77+
return this.addFile(file);
78+
};
79+
7180
Loader.prototype.atlas = function (key, textureURL, atlasURL, textureXhrSettings, atlasXhrSettings)
7281
{
7382
// Returns an object with two properties: 'texture' and 'data'
@@ -210,6 +219,10 @@ Loader.prototype.processCallback = function ()
210219
}
211220
break;
212221

222+
case 'spritesheet':
223+
textures.addSpriteSheet(file.key, file.data, file.config);
224+
break;
225+
213226
case 'json':
214227
cache.json.add(file.key, file.data);
215228
break;

v3/src/textures/TextureManager.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,31 @@ TextureManager.prototype = {
9999
return texture;
100100
},
101101

102-
addSpriteSheet: function (key, source, frameWidth, frameHeight, startFrame, endFrame, margin, spacing)
102+
/**
103+
* [addSpriteSheet description]
104+
* @param {[type]} key [description]
105+
* @param {[type]} source [description]
106+
* @param {[type]} config [description]
107+
* @param {number} config.frameWidth - The fixed width of each frame.
108+
* @param {number} [config.frameHeight] - The fixed height of each frame. If not set it will use the frameWidth as the height.
109+
* @param {number} [config.startFrame=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one Texture.
110+
* @param {number} [config.endFrame=-1] - The total number of frames to extract from the Sprite Sheet. The default value of -1 means "extract all frames".
111+
* @param {number} [config.margin=0] - If the frames have been drawn with a margin, specify the amount here.
112+
* @param {number} [config.spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
113+
*/
114+
addSpriteSheet: function (key, source, config)
103115
{
104116
var texture = this.create(key, source);
105117

106118
var width = texture.source[0].width;
107119
var height = texture.source[0].height;
108120

109-
Parser.SpriteSheet(texture, 0, 0, 0, width, height, frameWidth, frameHeight, startFrame, endFrame, margin, spacing);
121+
Parser.SpriteSheet(texture, 0, 0, 0, width, height, config);
110122

111123
return texture;
112124
},
113125

114-
addSpriteSheetFromAtlas: function (key, atlasKey, atlasFrame, frameWidth, frameHeight, startFrame, endFrame, margin, spacing)
126+
addSpriteSheetFromAtlas: function (key, atlasKey, atlasFrame, config)
115127
{
116128
var atlas = this.get(atlasKey);
117129
var sheet = atlas.get(atlasFrame);
@@ -120,7 +132,7 @@ TextureManager.prototype = {
120132
{
121133
var texture = this.create(key, sheet.source.image);
122134

123-
Parser.SpriteSheet(texture, 0, sheet.cutX, sheet.cutY, sheet.cutWidth, sheet.cutHeight, frameWidth, frameHeight, startFrame, endFrame, margin, spacing);
135+
Parser.SpriteSheet(texture, 0, sheet.cutX, sheet.cutY, sheet.cutWidth, sheet.cutHeight, config);
124136

125137
return texture;
126138
}

v3/src/textures/parsers/SpriteSheetTextureParser.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var GetObjectValue = require('../../utils/GetObjectValue');
8+
79
/**
810
* Parse a Sprite Sheet and extracts the frame data from it.
911
*
@@ -19,12 +21,21 @@
1921
* @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
2022
* @return {Phaser.FrameData} A FrameData object containing the parsed frames.
2123
*/
22-
var SpriteSheetTextureParser = function (texture, sourceIndex, x, y, width, height, frameWidth, frameHeight, startFrame, endFrame, margin, spacing)
24+
var SpriteSheetTextureParser = function (texture, sourceIndex, x, y, width, height, config)
2325
{
24-
if (startFrame === undefined) { startFrame = 0; }
25-
if (endFrame === undefined) { endFrame = -1; }
26-
if (margin === undefined) { margin = 0; }
27-
if (spacing === undefined) { spacing = 0; }
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 === null)
31+
{
32+
throw new Error('TextureManager.SpriteSheetTextureParser: Invalid frameWidth given.');
33+
}
34+
35+
var startFrame = GetObjectValue(config, 'startFrame', 0);
36+
var endFrame = GetObjectValue(config, 'endFrame', -1);
37+
var margin = GetObjectValue(config, 'margin', 0);
38+
var spacing = GetObjectValue(config, 'spacing', 0);
2839

2940
var row = Math.floor((width - margin) / (frameWidth + spacing));
3041
var column = Math.floor((height - margin) / (frameHeight + spacing));

0 commit comments

Comments
 (0)