2017-11-03 10:27:02 +00:00
|
|
|
var GetFastValue = require('../../utils/object/GetFastValue');
|
2017-02-04 05:36:06 +00:00
|
|
|
|
2017-04-18 14:31:30 +00:00
|
|
|
var SpriteSheet = function (texture, sourceIndex, x, y, width, height, config)
|
2016-12-06 16:18:28 +00:00
|
|
|
{
|
2017-11-03 10:27:02 +00:00
|
|
|
var frameWidth = GetFastValue(config, 'frameWidth', null);
|
|
|
|
var frameHeight = GetFastValue(config, 'frameHeight', frameWidth);
|
2017-02-04 05:36:06 +00:00
|
|
|
|
|
|
|
// If missing we can't proceed
|
|
|
|
if (frameWidth === null)
|
|
|
|
{
|
2017-04-18 14:31:30 +00:00
|
|
|
throw new Error('TextureManager.SpriteSheet: Invalid frameWidth given.');
|
2017-02-04 05:36:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-27 22:38:34 +00:00
|
|
|
// Add in a __BASE entry (for the entire atlas)
|
|
|
|
var source = texture.source[sourceIndex];
|
|
|
|
|
|
|
|
texture.add('__BASE', sourceIndex, 0, 0, source.width, source.height);
|
|
|
|
|
2017-11-03 10:27:02 +00:00
|
|
|
var startFrame = GetFastValue(config, 'startFrame', 0);
|
|
|
|
var endFrame = GetFastValue(config, 'endFrame', -1);
|
|
|
|
var margin = GetFastValue(config, 'margin', 0);
|
|
|
|
var spacing = GetFastValue(config, 'spacing', 0);
|
2016-12-06 16:18:28 +00:00
|
|
|
|
|
|
|
var row = Math.floor((width - margin) / (frameWidth + spacing));
|
|
|
|
var column = Math.floor((height - margin) / (frameHeight + spacing));
|
|
|
|
var total = row * column;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
texture.add(i, sourceIndex, x + fx, y + fy, frameWidth - ax, frameHeight - ay);
|
|
|
|
|
|
|
|
fx += frameWidth + spacing;
|
|
|
|
|
|
|
|
if (fx + frameWidth > width)
|
|
|
|
{
|
|
|
|
fx = margin;
|
|
|
|
fy += frameHeight + spacing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
};
|
|
|
|
|
2017-04-18 14:31:30 +00:00
|
|
|
module.exports = SpriteSheet;
|