phaser/src/textures/Texture.js

236 lines
5.4 KiB
JavaScript
Raw Normal View History

2017-07-04 12:23:58 +00:00
var Class = require('../utils/Class');
var Frame = require('./Frame');
var TextureSource = require('./TextureSource');
/**
* A Texture consists of a source, usually an Image from the Cache, or a Canvas, and a collection
* of Frames. The Frames represent the different areas of the Texture. For example a texture atlas
* may have many Frames, one for each element within the atlas. Where-as a single image would have
* just one frame, that encompasses the whole image.
*
* Textures are managed by the global TextureManager. This is a singleton class that is
* responsible for creating and delivering Textures and their corresponding Frames to Game Objects.
*
* Sprites and other Game Objects get the texture data they need from the TextureManager.
*/
2017-07-04 12:23:58 +00:00
var Texture = new Class({
2017-07-04 12:23:58 +00:00
initialize:
2017-07-04 12:23:58 +00:00
function Texture (manager, key, source, width, height)
{
this.manager = manager;
2017-07-04 12:23:58 +00:00
if (!Array.isArray(source))
{
source = [ source ];
}
2017-07-04 12:23:58 +00:00
this.key = key;
2017-07-04 12:23:58 +00:00
/**
* The source that is used to create the texture.
* Usually an Image, but can also be a Canvas.
*/
this.source = [];
this.dataSource = [];
2017-07-04 12:23:58 +00:00
this.frames = {};
// Any additional data that was set in the source JSON (if any), or any extra data you'd like to store relating to this texture
this.customData = {};
2017-07-04 12:23:58 +00:00
this.firstFrame = '__BASE';
2017-07-04 12:23:58 +00:00
this.frameTotal = 0;
2017-07-04 12:23:58 +00:00
// Load the Sources
for (var i = 0; i < source.length; i++)
{
this.source.push(new TextureSource(this, source[i], width, height));
}
},
add: function (name, sourceIndex, x, y, width, height)
{
var frame = new Frame(this, name, sourceIndex, x, y, width, height);
this.frames[name] = frame;
// Set the first frame of the Texture (other than __BASE)
// This is used to ensure we don't spam the display with entire
// atlases of sprite sheets, but instead just the first frame of them
// should the dev incorrectly specify the frame index
if (this.frameTotal === 1)
{
this.firstFrame = name;
}
this.frameTotal++;
return frame;
},
2017-04-05 16:04:55 +00:00
has: function (name)
{
return (this.frames[name]);
},
get: function (name)
{
2018-01-20 17:44:45 +00:00
if (name === undefined || name === null || (typeof name !== 'string' && typeof name !== 'number'))
{
name = (this.frameTotal === 1) ? '__BASE' : this.firstFrame;
}
var frame = this.frames[name];
if (!frame)
{
console.warn('No Texture.frame found with name ' + name);
return this.frames['__BASE'];
}
else
{
return frame;
}
},
getTextureSourceIndex: function (source)
{
for (var i = 0; i < this.source.length; i++)
{
if (this.source[i] === source)
{
return i;
}
}
return -1;
},
// source = TextureSource object
getFramesFromTextureSource: function (sourceIndex)
{
var out = [];
for (var frameName in this.frames)
{
if (frameName === '__BASE')
{
continue;
}
var frame = this.frames[frameName];
if (frame.sourceIndex === sourceIndex)
{
out.push(frame.name);
}
}
return out;
},
getFrameNames: function (includeBase)
{
if (includeBase === undefined) { includeBase = false; }
var out = Object.keys(this.frames);
if (!includeBase)
{
var idx = out.indexOf('__BASE');
if (idx !== -1)
{
out.splice(idx, 1);
}
}
return out;
},
getSourceImage: function (name)
{
if (name === undefined || name === null || this.frameTotal === 1)
{
name = '__BASE';
}
var frame = this.frames[name];
if (!frame)
{
console.warn('No Texture.frame found with name ' + name);
return this.frames['__BASE'].source.image;
}
else
{
return frame.source.image;
}
},
setDataSource: function (data)
{
if (!Array.isArray(data))
{
data = [ data ];
}
for (var i = 0; i < data.length; i++)
{
var source = this.source[i];
this.dataSource.push(new TextureSource(this, data[i], source.width, source.height));
}
},
2017-05-20 01:16:45 +00:00
setFilter: function (filterMode)
{
var i;
for (i = 0; i < this.source.length; i++)
2017-05-20 01:16:45 +00:00
{
this.source[i].setFilter(filterMode);
}
for (i = 0; i < this.dataSource.length; i++)
{
this.dataSource[i].setFilter(filterMode);
}
2017-07-04 12:23:58 +00:00
},
destroy: function ()
{
var i;
for (i = 0; i < this.source.length; i++)
{
this.source[i].destroy();
}
for (i = 0; i < this.dataSource.length; i++)
{
this.dataSource[i].destroy();
}
for (var frameName in this.frames)
{
var frame = this.frames[frameName];
frame.destroy();
}
this.source = [];
this.dataSource = [];
this.frames = {};
}
2017-07-04 12:23:58 +00:00
});
module.exports = Texture;