2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-10-11 16:05:59 +00:00
|
|
|
var CanvasPool = require('../display/canvas/CanvasPool');
|
2018-04-23 22:42:42 +00:00
|
|
|
var CanvasTexture = require('./CanvasTexture');
|
2017-07-04 12:23:58 +00:00
|
|
|
var Class = require('../utils/Class');
|
2017-10-11 16:05:59 +00:00
|
|
|
var Color = require('../display/color/Color');
|
2018-04-23 22:42:42 +00:00
|
|
|
var CONST = require('../const');
|
2018-01-18 14:01:29 +00:00
|
|
|
var EventEmitter = require('eventemitter3');
|
2017-06-15 00:34:05 +00:00
|
|
|
var GenerateTexture = require('../create/GenerateTexture');
|
2017-07-04 12:23:58 +00:00
|
|
|
var GetValue = require('../utils/object/GetValue');
|
|
|
|
var Parser = require('./parsers');
|
|
|
|
var Texture = require('./Texture');
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-03-19 21:57:46 +00:00
|
|
|
/**
|
|
|
|
* @callback EachTextureCallback
|
|
|
|
*
|
2018-06-23 08:17:10 +00:00
|
|
|
* @param {Phaser.Textures.Texture} texture - Each texture in Texture Manager.
|
2018-03-28 15:00:19 +00:00
|
|
|
* @param {...*} [args] - Additional arguments that will be passed to the callback, after the child.
|
2018-03-19 21:57:46 +00:00
|
|
|
*/
|
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* @classdesc
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Access it via `scene.textures`.
|
|
|
|
*
|
|
|
|
* @class TextureManager
|
2018-03-28 14:04:09 +00:00
|
|
|
* @extends Phaser.Events.EventEmitter
|
2018-02-08 04:01:44 +00:00
|
|
|
* @memberOf Phaser.Textures
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-23 08:17:10 +00:00
|
|
|
* @param {Phaser.Game} game - The Phaser.Game instance this Texture Manager belongs to.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
var TextureManager = new Class({
|
2017-01-19 23:20:36 +00:00
|
|
|
|
2018-01-18 14:01:29 +00:00
|
|
|
Extends: EventEmitter,
|
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
initialize:
|
2017-02-07 12:43:20 +00:00
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
function TextureManager (game)
|
|
|
|
{
|
2018-01-18 14:01:29 +00:00
|
|
|
EventEmitter.call(this);
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-06-23 08:17:10 +00:00
|
|
|
* The Game that this TextureManager belongs to.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureManager#game
|
|
|
|
* @type {Phaser.Game}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.game = game;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-06-23 08:17:10 +00:00
|
|
|
* The name of this manager.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureManager#name
|
|
|
|
* @type {string}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-18 05:16:52 +00:00
|
|
|
this.name = 'TextureManager';
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-06-23 08:17:10 +00:00
|
|
|
* An object that has all of textures that Texture Manager creates.
|
|
|
|
* Textures are assigned to keys so we can access to any texture that this object has directly by key value without iteration.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureManager#list
|
|
|
|
* @type {object}
|
|
|
|
* @default {}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.list = {};
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-06-23 08:17:10 +00:00
|
|
|
* The temporary canvas element to save an pixel data of an arbitrary texture in getPixel() and getPixelAlpha() method.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureManager#_tempCanvas
|
|
|
|
* @type {HTMLCanvasElement}
|
|
|
|
* @private
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-16 11:44:45 +00:00
|
|
|
this._tempCanvas = CanvasPool.create2D(this, 1, 1);
|
2018-02-08 04:01:44 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-23 08:17:10 +00:00
|
|
|
* The context of the temporary canvas element made to save an pixel data in getPixel() and getPixelAlpha() method.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureManager#_tempContext
|
|
|
|
* @type {CanvasRenderingContext2D}
|
|
|
|
* @private
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-16 11:44:45 +00:00
|
|
|
this._tempContext = this._tempCanvas.getContext('2d');
|
2018-01-18 05:16:52 +00:00
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-06-23 08:17:10 +00:00
|
|
|
* An counting value used for emitting 'ready' event after all of managers in game is loaded.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureManager#_pending
|
|
|
|
* @type {integer}
|
|
|
|
* @private
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-18 14:01:29 +00:00
|
|
|
this._pending = 0;
|
|
|
|
|
2018-01-18 05:16:52 +00:00
|
|
|
game.events.once('boot', this.boot, this);
|
2017-09-16 20:07:42 +00:00
|
|
|
},
|
2017-07-16 11:44:45 +00:00
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-06-23 08:17:10 +00:00
|
|
|
* The Boot Handler called by Phaser.Game when it first starts up.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#boot
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-16 20:07:42 +00:00
|
|
|
boot: function ()
|
|
|
|
{
|
2018-01-18 14:01:29 +00:00
|
|
|
this._pending = 2;
|
|
|
|
|
|
|
|
this.on('onload', this.updatePending, this);
|
|
|
|
this.on('onerror', this.updatePending, this);
|
|
|
|
|
2017-09-16 20:07:42 +00:00
|
|
|
this.addBase64('__DEFAULT', this.game.config.defaultImage);
|
|
|
|
this.addBase64('__MISSING', this.game.config.missingImage);
|
2018-01-31 03:38:10 +00:00
|
|
|
|
|
|
|
this.game.events.once('destroy', this.destroy, this);
|
2017-07-04 12:23:58 +00:00
|
|
|
},
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-06-23 08:17:10 +00:00
|
|
|
* After 'onload' or 'onerror' invoked twice, emit 'ready' event.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#updatePending
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-18 14:01:29 +00:00
|
|
|
updatePending: function ()
|
|
|
|
{
|
|
|
|
this._pending--;
|
|
|
|
|
|
|
|
if (this._pending === 0)
|
|
|
|
{
|
|
|
|
this.off('onload');
|
|
|
|
this.off('onerror');
|
|
|
|
|
|
|
|
this.game.events.emit('ready');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
/**
|
|
|
|
* Checks the given texture key and throws a console.warn if the key is already in use, then returns false.
|
2018-05-15 11:51:50 +00:00
|
|
|
* If you wish to avoid the console.warn then use `TextureManager.exists` instead.
|
2018-04-23 22:42:42 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#checkKey
|
2018-05-04 17:51:02 +00:00
|
|
|
* @since 3.7.0
|
2018-04-23 22:42:42 +00:00
|
|
|
*
|
|
|
|
* @param {string} key - The texture key to check.
|
|
|
|
*
|
|
|
|
* @return {boolean} `true` if it's safe to use the texture key, otherwise `false`.
|
|
|
|
*/
|
|
|
|
checkKey: function (key)
|
|
|
|
{
|
|
|
|
if (this.exists(key))
|
|
|
|
{
|
2018-04-23 22:52:57 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2018-04-23 22:42:42 +00:00
|
|
|
console.error('Texture key already in use: ' + key);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2018-04-23 18:11:47 +00:00
|
|
|
/**
|
|
|
|
* Removes a Texture from the Texture Manager and destroys it. This will immediately
|
|
|
|
* clear all references to it from the Texture Manager, and if it has one, destroy its
|
|
|
|
* WebGLTexture. This will emit a `removetexture` event.
|
|
|
|
*
|
|
|
|
* Note: If you have any Game Objects still using this texture they will start throwing
|
|
|
|
* errors the next time they try to render. Make sure that removing the texture is the final
|
|
|
|
* step when clearing down to avoid this.
|
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#remove
|
2018-05-04 17:51:02 +00:00
|
|
|
* @since 3.7.0
|
2018-04-23 18:11:47 +00:00
|
|
|
*
|
|
|
|
* @param {(string|Phaser.Textures.Texture)} key - The key of the Texture to remove, or a reference to it.
|
|
|
|
*
|
|
|
|
* @return {Phaser.Textures.TextureManager} The Texture Manager.
|
|
|
|
*/
|
2018-04-23 22:52:57 +00:00
|
|
|
remove: function (key)
|
|
|
|
{
|
2018-04-23 18:11:47 +00:00
|
|
|
if (typeof key === 'string')
|
|
|
|
{
|
|
|
|
if (this.exists(key))
|
|
|
|
{
|
|
|
|
key = this.get(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-23 22:52:57 +00:00
|
|
|
console.warn('No texture found matching key: ' + key);
|
2018-04-23 18:11:47 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// By this point key should be a Texture, if not, the following fails anyway
|
|
|
|
if (this.list.hasOwnProperty(key.key))
|
|
|
|
{
|
|
|
|
delete this.list[key.key];
|
|
|
|
|
|
|
|
key.destroy();
|
|
|
|
|
|
|
|
this.emit('removetexture', key.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
2018-04-23 22:52:57 +00:00
|
|
|
},
|
2018-04-23 18:11:47 +00:00
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* Adds a new Texture to the Texture Manager created from the given Base64 encoded data.
|
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#addBase64
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-03-20 16:15:49 +00:00
|
|
|
* @param {*} data - The Base64 encoded data.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2017-02-07 12:43:20 +00:00
|
|
|
addBase64: function (key, data)
|
|
|
|
{
|
2018-04-23 22:42:42 +00:00
|
|
|
if (this.checkKey(key))
|
|
|
|
{
|
|
|
|
var _this = this;
|
2018-01-18 14:01:29 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
var image = new Image();
|
2017-02-07 12:43:20 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
image.onerror = function ()
|
|
|
|
{
|
|
|
|
_this.emit('onerror', key);
|
|
|
|
};
|
2018-01-18 14:01:29 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
image.onload = function ()
|
|
|
|
{
|
|
|
|
var texture = _this.create(key, image);
|
2018-03-19 21:57:46 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
Parser.Image(texture, 0);
|
2018-01-18 14:01:29 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
_this.emit('addtexture', key, texture);
|
2018-04-20 17:57:23 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
_this.emit('onload', key, texture);
|
|
|
|
};
|
2017-02-07 12:43:20 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
image.src = data;
|
|
|
|
}
|
2017-02-07 12:43:20 +00:00
|
|
|
},
|
|
|
|
|
2018-07-27 08:43:12 +00:00
|
|
|
getBase64: function (key, frame)
|
|
|
|
{
|
|
|
|
var data = '';
|
|
|
|
|
|
|
|
var textureFrame = this.getFrame(key, frame);
|
|
|
|
|
|
|
|
if (textureFrame)
|
|
|
|
{
|
|
|
|
var cd = textureFrame.canvasData;
|
|
|
|
|
|
|
|
var canvas = CanvasPool.create2D(this, cd.width, cd.height);
|
|
|
|
var ctx = canvas.getContext('2d');
|
|
|
|
|
|
|
|
ctx.drawImage(
|
|
|
|
textureFrame.source.image,
|
|
|
|
cd.x,
|
|
|
|
cd.y,
|
|
|
|
cd.width,
|
|
|
|
cd.height,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
cd.width,
|
|
|
|
cd.height
|
|
|
|
);
|
|
|
|
|
|
|
|
data = canvas.toDataURL('image/png').replace(/^data:image\/(png|jpg);base64,/, '');
|
|
|
|
|
|
|
|
CanvasPool.remove(canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* Adds a new Texture to the Texture Manager created from the given Image element.
|
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#addImage
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-03-21 14:41:51 +00:00
|
|
|
* @param {HTMLImageElement} source - The source Image element.
|
|
|
|
* @param {HTMLImageElement} [dataSource] - An optional data Image element.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-04-23 22:42:42 +00:00
|
|
|
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2018-01-29 23:38:27 +00:00
|
|
|
addImage: function (key, source, dataSource)
|
2016-12-06 16:18:28 +00:00
|
|
|
{
|
2018-04-23 22:42:42 +00:00
|
|
|
var texture = null;
|
2018-03-19 21:57:46 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
if (this.checkKey(key))
|
2018-01-29 23:38:27 +00:00
|
|
|
{
|
2018-04-23 22:42:42 +00:00
|
|
|
texture = this.create(key, source);
|
|
|
|
|
|
|
|
Parser.Image(texture, 0);
|
2018-01-29 23:38:27 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
if (dataSource)
|
|
|
|
{
|
|
|
|
texture.setDataSource(dataSource);
|
|
|
|
}
|
2018-04-20 17:57:23 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
this.emit('addtexture', key, texture);
|
|
|
|
}
|
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
return texture;
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Creates a new Texture using the given config values.
|
|
|
|
* Generated textures consist of a Canvas element to which the texture data is drawn.
|
|
|
|
* See the Phaser.Create function for the more direct way to create textures.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#generate
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-06-23 08:17:10 +00:00
|
|
|
* @param {object} config - The configuration object needed to generate the texture.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-04-23 22:42:42 +00:00
|
|
|
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2017-06-15 00:34:05 +00:00
|
|
|
generate: function (key, config)
|
|
|
|
{
|
2018-04-23 22:42:42 +00:00
|
|
|
if (this.checkKey(key))
|
|
|
|
{
|
|
|
|
var canvas = CanvasPool.create(this, 1, 1);
|
2017-06-15 00:34:05 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
config.canvas = canvas;
|
2017-06-15 00:34:05 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
GenerateTexture(config);
|
2017-06-15 00:34:05 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
return this.addCanvas(key, canvas);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2017-06-15 00:34:05 +00:00
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Creates a new Texture using a blank Canvas element of the size given.
|
|
|
|
*
|
|
|
|
* Canvas elements are automatically pooled and calling this method will
|
|
|
|
* extract a free canvas from the CanvasPool, or create one if none are available.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#createCanvas
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-04-23 22:42:42 +00:00
|
|
|
* @param {integer} [width=256]- The width of the Canvas element.
|
|
|
|
* @param {integer} [height=256] - The height of the Canvas element.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-04-23 22:42:42 +00:00
|
|
|
* @return {?Phaser.Textures.CanvasTexture} The Canvas Texture that was created, or `null` if the key is already in use.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2017-03-14 16:37:32 +00:00
|
|
|
createCanvas: function (key, width, height)
|
|
|
|
{
|
|
|
|
if (width === undefined) { width = 256; }
|
|
|
|
if (height === undefined) { height = 256; }
|
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
if (this.checkKey(key))
|
|
|
|
{
|
|
|
|
var canvas = CanvasPool.create(this, width, height, CONST.CANVAS, true);
|
2017-03-14 16:37:32 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
return this.addCanvas(key, canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2017-03-14 16:37:32 +00:00
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-04-23 22:42:42 +00:00
|
|
|
* Creates a new Canvas Texture object from an existing Canvas element and adds
|
2018-02-08 13:45:53 +00:00
|
|
|
* it to this Texture Manager.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#addCanvas
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
|
|
|
* @param {HTMLCanvasElement} source - The Canvas element to form the base of the new Texture.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-04-23 22:42:42 +00:00
|
|
|
* @return {?Phaser.Textures.CanvasTexture} The Canvas Texture that was created, or `null` if the key is already in use.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2016-12-06 16:18:28 +00:00
|
|
|
addCanvas: function (key, source)
|
|
|
|
{
|
2018-04-23 22:42:42 +00:00
|
|
|
var texture = null;
|
2018-03-19 21:57:46 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
if (this.checkKey(key))
|
|
|
|
{
|
|
|
|
texture = new CanvasTexture(this, key, source, source.width, source.height);
|
|
|
|
|
|
|
|
this.list[key] = texture;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
this.emit('addtexture', key, texture);
|
|
|
|
}
|
2018-04-20 17:57:23 +00:00
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
return texture;
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Adds a new Texture Atlas to this Texture Manager.
|
|
|
|
* It can accept either JSON Array or JSON Hash formats, as exported by Texture Packer and similar software.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#addAtlas
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-03-21 14:41:51 +00:00
|
|
|
* @param {HTMLImageElement} source - The source Image element.
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {object} data - The Texture Atlas data.
|
2018-05-04 13:32:13 +00:00
|
|
|
* @param {HTMLImageElement} [dataSource] - An optional data Image element.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-04-23 22:42:42 +00:00
|
|
|
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2018-05-04 13:32:13 +00:00
|
|
|
addAtlas: function (key, source, data, dataSource)
|
2016-12-07 10:50:10 +00:00
|
|
|
{
|
2018-02-09 15:22:55 +00:00
|
|
|
// New Texture Packer format?
|
|
|
|
if (Array.isArray(data.textures) || Array.isArray(data.frames))
|
2016-12-07 10:50:10 +00:00
|
|
|
{
|
2018-05-04 13:32:13 +00:00
|
|
|
return this.addAtlasJSONArray(key, source, data, dataSource);
|
2016-12-07 10:50:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-04 13:32:13 +00:00
|
|
|
return this.addAtlasJSONHash(key, source, data, dataSource);
|
2016-12-07 10:50:10 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Adds a Texture Atlas to this Texture Manager.
|
|
|
|
* The frame data of the atlas must be stored in an Array within the JSON.
|
|
|
|
* This is known as a JSON Array in software such as Texture Packer.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#addAtlasJSONArray
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-05-03 13:19:40 +00:00
|
|
|
* @param {(HTMLImageElement|HTMLImageElement[])} source - The source Image element/s.
|
|
|
|
* @param {(object|object[])} data - The Texture Atlas data/s.
|
2018-05-04 13:32:13 +00:00
|
|
|
* @param {HTMLImageElement} [dataSource] - An optional data Image element.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-04-23 22:42:42 +00:00
|
|
|
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2018-05-04 13:32:13 +00:00
|
|
|
addAtlasJSONArray: function (key, source, data, dataSource)
|
2016-12-06 16:18:28 +00:00
|
|
|
{
|
2018-04-23 22:42:42 +00:00
|
|
|
var texture = null;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
if (this.checkKey(key))
|
2016-12-06 16:18:28 +00:00
|
|
|
{
|
2018-04-23 22:42:42 +00:00
|
|
|
texture = this.create(key, source);
|
2018-04-20 17:57:23 +00:00
|
|
|
|
2018-05-04 13:32:13 +00:00
|
|
|
// Multi-Atlas?
|
2018-04-23 22:42:42 +00:00
|
|
|
if (Array.isArray(data))
|
2016-12-06 16:18:28 +00:00
|
|
|
{
|
2018-04-23 22:42:42 +00:00
|
|
|
var singleAtlasFile = (data.length === 1); // multi-pack with one atlas file for all images
|
|
|
|
|
2018-05-03 13:19:40 +00:00
|
|
|
// !! Assumes the textures are in the same order in the source array as in the json data !!
|
2018-04-23 22:42:42 +00:00
|
|
|
for (var i = 0; i < texture.source.length; i++)
|
|
|
|
{
|
|
|
|
var atlasData = singleAtlasFile ? data[0] : data[i];
|
2018-04-20 17:57:23 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
Parser.JSONArray(texture, i, atlasData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Parser.JSONArray(texture, 0, data);
|
2016-12-06 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 13:32:13 +00:00
|
|
|
if (dataSource)
|
|
|
|
{
|
|
|
|
texture.setDataSource(dataSource);
|
|
|
|
}
|
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
this.emit('addtexture', key, texture);
|
|
|
|
}
|
2018-04-20 17:57:23 +00:00
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
return texture;
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Adds a Texture Atlas to this Texture Manager.
|
|
|
|
* The frame data of the atlas must be stored in an Object within the JSON.
|
|
|
|
* This is known as a JSON Hash in software such as Texture Packer.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#addAtlasJSONHash
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-03-21 14:41:51 +00:00
|
|
|
* @param {HTMLImageElement} source - The source Image element.
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {object} data - The Texture Atlas data.
|
2018-05-04 13:32:13 +00:00
|
|
|
* @param {HTMLImageElement} [dataSource] - An optional data Image element.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-04-23 22:42:42 +00:00
|
|
|
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2018-05-04 13:32:13 +00:00
|
|
|
addAtlasJSONHash: function (key, source, data, dataSource)
|
2016-12-06 16:18:28 +00:00
|
|
|
{
|
2018-04-23 22:42:42 +00:00
|
|
|
var texture = null;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
if (this.checkKey(key))
|
2016-12-06 16:18:28 +00:00
|
|
|
{
|
2018-04-23 22:42:42 +00:00
|
|
|
texture = this.create(key, source);
|
|
|
|
|
|
|
|
if (Array.isArray(data))
|
2016-12-06 16:18:28 +00:00
|
|
|
{
|
2018-04-23 22:42:42 +00:00
|
|
|
for (var i = 0; i < data.length; i++)
|
|
|
|
{
|
|
|
|
Parser.JSONHash(texture, i, data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Parser.JSONHash(texture, 0, data);
|
2016-12-06 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 13:32:13 +00:00
|
|
|
if (dataSource)
|
|
|
|
{
|
|
|
|
texture.setDataSource(dataSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.emit('addtexture', key, texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a Texture Atlas to this Texture Manager, where the atlas data is given
|
|
|
|
* in the XML format.
|
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#addAtlasXML
|
|
|
|
* @since 3.7.0
|
|
|
|
*
|
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
|
|
|
* @param {HTMLImageElement} source - The source Image element.
|
|
|
|
* @param {object} data - The Texture Atlas XML data.
|
|
|
|
* @param {HTMLImageElement} [dataSource] - An optional data Image element.
|
|
|
|
*
|
|
|
|
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
|
|
|
|
*/
|
|
|
|
addAtlasXML: function (key, source, data, dataSource)
|
|
|
|
{
|
|
|
|
var texture = null;
|
|
|
|
|
|
|
|
if (this.checkKey(key))
|
|
|
|
{
|
|
|
|
texture = this.create(key, source);
|
|
|
|
|
|
|
|
Parser.AtlasXML(texture, 0, data);
|
|
|
|
|
|
|
|
if (dataSource)
|
|
|
|
{
|
|
|
|
texture.setDataSource(dataSource);
|
|
|
|
}
|
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
this.emit('addtexture', key, texture);
|
|
|
|
}
|
2018-04-20 17:57:23 +00:00
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
return texture;
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Adds a Unity Texture Atlas to this Texture Manager.
|
|
|
|
* The data must be in the form of a Unity YAML file.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#addUnityAtlas
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-03-21 14:41:51 +00:00
|
|
|
* @param {HTMLImageElement} source - The source Image element.
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {object} data - The Texture Atlas data.
|
2018-05-04 13:32:13 +00:00
|
|
|
* @param {HTMLImageElement} [dataSource] - An optional data Image element.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-04-23 22:42:42 +00:00
|
|
|
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2018-05-04 13:32:13 +00:00
|
|
|
addUnityAtlas: function (key, source, data, dataSource)
|
2017-08-10 04:17:02 +00:00
|
|
|
{
|
2018-04-23 22:42:42 +00:00
|
|
|
var texture = null;
|
2017-08-10 04:17:02 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
if (this.checkKey(key))
|
|
|
|
{
|
2018-04-23 22:52:57 +00:00
|
|
|
texture = this.create(key, source);
|
2017-08-10 04:17:02 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
Parser.UnityYAML(texture, 0, data);
|
|
|
|
|
2018-05-04 13:32:13 +00:00
|
|
|
if (dataSource)
|
|
|
|
{
|
|
|
|
texture.setDataSource(dataSource);
|
|
|
|
}
|
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
this.emit('addtexture', key, texture);
|
|
|
|
}
|
2018-04-20 17:57:23 +00:00
|
|
|
|
2017-08-10 04:17:02 +00:00
|
|
|
return texture;
|
|
|
|
},
|
|
|
|
|
2018-04-16 15:37:07 +00:00
|
|
|
/**
|
|
|
|
* @typedef {object} SpriteSheetConfig
|
|
|
|
*
|
|
|
|
* @property {integer} frameWidth - The fixed width of each frame.
|
|
|
|
* @property {integer} [frameHeight] - The fixed height of each frame. If not set it will use the frameWidth as the height.
|
|
|
|
* @property {integer} [startFrame=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one Texture.
|
|
|
|
* @property {integer} [endFrame=-1] - The total number of frames to extract from the Sprite Sheet. The default value of -1 means "extract all frames".
|
|
|
|
* @property {integer} [margin=0] - If the frames have been drawn with a margin, specify the amount here.
|
|
|
|
* @property {integer} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
|
|
|
|
*/
|
|
|
|
|
2017-02-04 05:36:06 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Adds a Sprite Sheet to this Texture Manager.
|
2018-03-19 21:57:46 +00:00
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* In Phaser terminology a Sprite Sheet is a texture containing different frames, but each frame is the exact
|
|
|
|
* same size and cannot be trimmed or rotated.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#addSpriteSheet
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-03-21 14:41:51 +00:00
|
|
|
* @param {HTMLImageElement} source - The source Image element.
|
2018-04-16 15:37:07 +00:00
|
|
|
* @param {SpriteSheetConfig} config - The configuration object for this Sprite Sheet.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-04-23 22:42:42 +00:00
|
|
|
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2017-02-04 05:36:06 +00:00
|
|
|
addSpriteSheet: function (key, source, config)
|
2016-12-06 16:18:28 +00:00
|
|
|
{
|
2018-04-23 22:42:42 +00:00
|
|
|
var texture = null;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
if (this.checkKey(key))
|
|
|
|
{
|
|
|
|
texture = this.create(key, source);
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
var width = texture.source[0].width;
|
|
|
|
var height = texture.source[0].height;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
Parser.SpriteSheet(texture, 0, 0, 0, width, height, config);
|
|
|
|
|
|
|
|
this.emit('addtexture', key, texture);
|
|
|
|
}
|
2018-04-20 17:57:23 +00:00
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
return texture;
|
|
|
|
},
|
|
|
|
|
2018-04-16 15:37:07 +00:00
|
|
|
/**
|
|
|
|
* @typedef {object} SpriteSheetFromAtlasConfig
|
|
|
|
*
|
|
|
|
* @property {string} atlas - The key of the Texture Atlas in which this Sprite Sheet can be found.
|
|
|
|
* @property {string} frame - The key of the Texture Atlas Frame in which this Sprite Sheet can be found.
|
|
|
|
* @property {integer} frameWidth - The fixed width of each frame.
|
|
|
|
* @property {integer} [frameHeight] - The fixed height of each frame. If not set it will use the frameWidth as the height.
|
|
|
|
* @property {integer} [startFrame=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one Texture.
|
|
|
|
* @property {integer} [endFrame=-1] - The total number of frames to extract from the Sprite Sheet. The default value of -1 means "extract all frames".
|
|
|
|
* @property {integer} [margin=0] - If the frames have been drawn with a margin, specify the amount here.
|
|
|
|
* @property {integer} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
|
|
|
|
*/
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Adds a Sprite Sheet to this Texture Manager, where the Sprite Sheet exists as a Frame within a Texture Atlas.
|
2018-03-19 21:57:46 +00:00
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* In Phaser terminology a Sprite Sheet is a texture containing different frames, but each frame is the exact
|
|
|
|
* same size and cannot be trimmed or rotated.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#addSpriteSheetFromAtlas
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-04-16 15:37:07 +00:00
|
|
|
* @param {SpriteSheetFromAtlasConfig} config - The configuration object for this Sprite Sheet.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-04-23 22:42:42 +00:00
|
|
|
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2017-04-11 16:22:22 +00:00
|
|
|
addSpriteSheetFromAtlas: function (key, config)
|
2016-12-06 16:18:28 +00:00
|
|
|
{
|
2018-04-23 22:42:42 +00:00
|
|
|
if (!this.checkKey(key))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-04-26 15:03:14 +00:00
|
|
|
var atlasKey = GetValue(config, 'atlas', null);
|
|
|
|
var atlasFrame = GetValue(config, 'frame', null);
|
2017-04-11 16:22:22 +00:00
|
|
|
|
|
|
|
if (!atlasKey || !atlasFrame)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
var atlas = this.get(atlasKey);
|
|
|
|
var sheet = atlas.get(atlasFrame);
|
|
|
|
|
|
|
|
if (sheet)
|
|
|
|
{
|
|
|
|
var texture = this.create(key, sheet.source.image);
|
|
|
|
|
2017-04-18 14:31:30 +00:00
|
|
|
if (sheet.trimmed)
|
|
|
|
{
|
2017-11-03 13:15:58 +00:00
|
|
|
// If trimmed we need to help the parser adjust
|
2017-04-18 14:31:30 +00:00
|
|
|
Parser.SpriteSheetFromAtlas(texture, sheet, config);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Parser.SpriteSheet(texture, 0, sheet.cutX, sheet.cutY, sheet.cutWidth, sheet.cutHeight, config);
|
|
|
|
}
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-04-20 17:57:23 +00:00
|
|
|
this.emit('addtexture', key, texture);
|
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Creates a new Texture using the given source and dimensions.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#create
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-03-21 14:41:51 +00:00
|
|
|
* @param {HTMLImageElement} source - The source Image element.
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {integer} width - The width of the Texture.
|
|
|
|
* @param {integer} height - The height of the Texture.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-04-23 22:42:42 +00:00
|
|
|
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2017-05-10 23:36:11 +00:00
|
|
|
create: function (key, source, width, height)
|
2016-12-06 16:18:28 +00:00
|
|
|
{
|
2018-04-23 22:42:42 +00:00
|
|
|
var texture = null;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
if (this.checkKey(key))
|
|
|
|
{
|
|
|
|
texture = new Texture(this, key, source, width, height);
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-04-23 22:42:42 +00:00
|
|
|
this.list[key] = texture;
|
|
|
|
}
|
2018-04-20 17:57:23 +00:00
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
return texture;
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Checks the given key to see if a Texture using it exists within this Texture Manager.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#exists
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @return {boolean} Returns `true` if a Texture matching the given key exists in this Texture Manager.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2016-12-06 16:18:28 +00:00
|
|
|
exists: function (key)
|
|
|
|
{
|
|
|
|
return (this.list.hasOwnProperty(key));
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Returns a Texture from the Texture Manager that matches the given key.
|
|
|
|
* If the key is undefined it will return the `__DEFAULT` Texture.
|
|
|
|
* If the key is given, but not found, it will return the `__MISSING` Texture.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#get
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @return {Phaser.Textures.Texture} The Texture that was created.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2016-12-06 16:18:28 +00:00
|
|
|
get: function (key)
|
|
|
|
{
|
|
|
|
if (key === undefined) { key = '__DEFAULT'; }
|
|
|
|
|
|
|
|
if (this.list[key])
|
|
|
|
{
|
|
|
|
return this.list[key];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return this.list['__MISSING'];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Takes a Texture key and Frame name and returns a clone of that Frame if found.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#cloneFrame
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-03-20 15:10:19 +00:00
|
|
|
* @param {(string|integer)} frame - The string or index of the Frame to be cloned.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @return {Phaser.Textures.Frame} A Clone of the given Frame.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2016-12-06 16:18:28 +00:00
|
|
|
cloneFrame: function (key, frame)
|
|
|
|
{
|
|
|
|
if (this.list[key])
|
|
|
|
{
|
|
|
|
return this.list[key].get(frame).clone();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Takes a Texture key and Frame name and returns a reference to that Frame, if found.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#getFrame
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-07-11 11:33:40 +00:00
|
|
|
* @param {(string|integer)} [frame] - The string-based name, or integer based index, of the Frame to get from the Texture.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @return {Phaser.Textures.Frame} A Texture Frame object.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2016-12-06 16:18:28 +00:00
|
|
|
getFrame: function (key, frame)
|
|
|
|
{
|
|
|
|
if (this.list[key])
|
|
|
|
{
|
|
|
|
return this.list[key].get(frame);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Returns an array with all of the keys of all Textures in this Texture Manager.
|
|
|
|
* The output array will exclude the `__DEFAULT` and `__MISSING` keys.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#getTextureKeys
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @return {string[]} An array containing all of the Texture keys stored in this Texture Manager.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2018-01-19 16:29:55 +00:00
|
|
|
getTextureKeys: function ()
|
|
|
|
{
|
|
|
|
var output = [];
|
|
|
|
|
|
|
|
for (var key in this.list)
|
|
|
|
{
|
|
|
|
if (key !== '__DEFAULT' && key !== '__MISSING')
|
|
|
|
{
|
|
|
|
output.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Given a Texture and an `x` and `y` coordinate this method will return a new
|
|
|
|
* Color object that has been populated with the color and alpha values of the pixel
|
|
|
|
* at that location in the Texture.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#getPixel
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {integer} x - The x coordinate of the pixel within the Texture.
|
|
|
|
* @param {integer} y - The y coordinate of the pixel within the Texture.
|
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-03-20 15:10:19 +00:00
|
|
|
* @param {(string|integer)} frame - The string or index of the Frame.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-03-20 14:36:03 +00:00
|
|
|
* @return {?Phaser.Display.Color} A Color object populated with the color values of the requested pixel,
|
2018-02-08 13:45:53 +00:00
|
|
|
* or `null` if the coordinates were out of bounds.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2017-07-16 11:44:45 +00:00
|
|
|
getPixel: function (x, y, key, frame)
|
|
|
|
{
|
|
|
|
var textureFrame = this.getFrame(key, frame);
|
|
|
|
|
|
|
|
if (textureFrame)
|
|
|
|
{
|
|
|
|
var source = textureFrame.source.image;
|
|
|
|
|
|
|
|
if (x >= 0 && x <= source.width && y >= 0 && y <= source.height)
|
|
|
|
{
|
|
|
|
x += textureFrame.cutX;
|
|
|
|
y += textureFrame.cutY;
|
|
|
|
|
|
|
|
// if (textureFrame.trimmed)
|
|
|
|
// {
|
2018-02-16 18:07:49 +00:00
|
|
|
// x -= this.sprite.texture.trim.x;
|
|
|
|
// y -= this.sprite.texture.trim.y;
|
2017-07-16 11:44:45 +00:00
|
|
|
// }
|
|
|
|
|
|
|
|
var context = this._tempContext;
|
|
|
|
|
|
|
|
context.clearRect(0, 0, 1, 1);
|
|
|
|
context.drawImage(source, x, y, 1, 1, 0, 0, 1, 1);
|
|
|
|
|
|
|
|
var rgb = context.getImageData(0, 0, 1, 1);
|
|
|
|
|
|
|
|
return new Color(rgb.data[0], rgb.data[1], rgb.data[2], rgb.data[3]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2018-06-08 16:50:26 +00:00
|
|
|
/**
|
|
|
|
* Given a Texture and an `x` and `y` coordinate this method will return a value between 0 and 255
|
|
|
|
* corresponding to the alpha value of the pixel at that location in the Texture. If the coordinate
|
|
|
|
* is out of bounds it will return null.
|
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#getPixelAlpha
|
|
|
|
* @since 3.10.0
|
|
|
|
*
|
|
|
|
* @param {integer} x - The x coordinate of the pixel within the Texture.
|
|
|
|
* @param {integer} y - The y coordinate of the pixel within the Texture.
|
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
|
|
|
* @param {(string|integer)} frame - The string or index of the Frame.
|
|
|
|
*
|
|
|
|
* @return {integer} A value between 0 and 255, or `null` if the coordinates were out of bounds.
|
|
|
|
*/
|
|
|
|
getPixelAlpha: function (x, y, key, frame)
|
|
|
|
{
|
|
|
|
var textureFrame = this.getFrame(key, frame);
|
|
|
|
|
|
|
|
if (textureFrame)
|
|
|
|
{
|
|
|
|
var source = textureFrame.source.image;
|
|
|
|
|
|
|
|
if (x >= 0 && x <= source.width && y >= 0 && y <= source.height)
|
|
|
|
{
|
|
|
|
x += textureFrame.cutX;
|
|
|
|
y += textureFrame.cutY;
|
|
|
|
|
|
|
|
var context = this._tempContext;
|
|
|
|
|
|
|
|
context.clearRect(0, 0, 1, 1);
|
|
|
|
context.drawImage(source, x, y, 1, 1, 0, 0, 1, 1);
|
|
|
|
|
|
|
|
var rgb = context.getImageData(0, 0, 1, 1);
|
|
|
|
|
|
|
|
return rgb.data[3];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Sets the given Game Objects `texture` and `frame` properties so that it uses
|
|
|
|
* the Texture and Frame specified in the `key` and `frame` arguments to this method.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#setTexture
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-23 08:17:10 +00:00
|
|
|
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object the texture would be set on.
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {string} key - The unique string-based key of the Texture.
|
2018-03-20 15:10:19 +00:00
|
|
|
* @param {(string|integer)} frame - The string or index of the Frame.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-02-08 13:45:53 +00:00
|
|
|
* @return {Phaser.GameObjects.GameObject} The Game Object the texture was set on.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2016-12-06 16:18:28 +00:00
|
|
|
setTexture: function (gameObject, key, frame)
|
|
|
|
{
|
|
|
|
if (this.list[key])
|
|
|
|
{
|
|
|
|
gameObject.texture = this.list[key];
|
|
|
|
gameObject.frame = gameObject.texture.get(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
return gameObject;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Passes all Textures to the given callback.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#each
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 21:57:46 +00:00
|
|
|
* @param {EachTextureCallback} callback - The callback function to be sent the Textures.
|
2018-02-08 13:45:53 +00:00
|
|
|
* @param {object} scope - The value to use as `this` when executing the callback.
|
2018-03-28 15:00:19 +00:00
|
|
|
* @param {...*} [args] - Additional arguments that will be passed to the callback, after the child.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2018-02-08 13:45:53 +00:00
|
|
|
each: function (callback, scope)
|
2016-12-06 16:18:28 +00:00
|
|
|
{
|
|
|
|
var args = [ null ];
|
|
|
|
|
|
|
|
for (var i = 1; i < arguments.length; i++)
|
|
|
|
{
|
|
|
|
args.push(arguments[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var texture in this.list)
|
|
|
|
{
|
|
|
|
args[0] = this.list[texture];
|
|
|
|
|
2018-02-08 13:45:53 +00:00
|
|
|
callback.apply(scope, args);
|
2016-12-06 16:18:28 +00:00
|
|
|
}
|
2018-01-31 03:38:10 +00:00
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-02-08 13:45:53 +00:00
|
|
|
* Destroys the Texture Manager and all Textures stored within it.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureManager#destroy
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-31 03:38:10 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
for (var texture in this.list)
|
|
|
|
{
|
|
|
|
this.list[texture].destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.list = {};
|
|
|
|
|
|
|
|
this.game = null;
|
2018-04-23 22:42:42 +00:00
|
|
|
|
|
|
|
CanvasPool.remove(this._tempCanvas);
|
2016-12-06 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
});
|
2016-12-06 16:18:28 +00:00
|
|
|
|
|
|
|
module.exports = TextureManager;
|