phaser/src/textures/TextureManager.js

1610 lines
53 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2023-01-02 17:36:27 +00:00
* @copyright 2013-2023 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
2017-10-11 16:05:59 +00:00
var CanvasPool = require('../display/canvas/CanvasPool');
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');
var CONST = require('../const');
2022-09-27 22:39:23 +00:00
var DynamicTexture = require('./DynamicTexture');
var EventEmitter = require('eventemitter3');
var Events = require('./events');
var Frame = require('./Frame');
var GameEvents = require('../core/events');
var GenerateTexture = require('../create/GenerateTexture');
2017-07-04 12:23:58 +00:00
var GetValue = require('../utils/object/GetValue');
2022-09-28 19:57:42 +00:00
var ImageGameObject = require('../gameobjects/image/Image');
var IsPlainObject = require('../utils/object/IsPlainObject');
2017-07-04 12:23:58 +00:00
var Parser = require('./parsers');
2022-09-28 18:53:49 +00:00
var Rectangle = require('../geom/rectangle/Rectangle');
2017-07-04 12:23:58 +00:00
var Texture = require('./Texture');
2018-03-19 21:57:46 +00:00
/**
* @callback EachTextureCallback
*
* @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
*/
/**
2018-02-08 04:01:44 +00:00
* @classdesc
2022-09-28 18:09:35 +00:00
* When Phaser boots it will create an instance of this Texture Manager class.
2018-02-08 04:01:44 +00:00
*
2022-09-28 18:09:35 +00:00
* It is a global manager that handles all textures in your game. You can access it from within
* a Scene via the `this.textures` property.
2018-02-08 04:01:44 +00:00
*
2022-09-28 18:09:35 +00:00
* Its role is as a manager for all textures that your game uses. It can create, update and remove
* textures globally, as well as parse texture data from external files, such as sprite sheets
* and texture atlases.
*
* Sprites and other texture-based Game Objects get their texture data directly from this class.
2018-02-08 04:01:44 +00:00
*
* @class TextureManager
2018-03-28 14:04:09 +00:00
* @extends Phaser.Events.EventEmitter
2018-10-10 09:49:13 +00:00
* @memberof Phaser.Textures
2018-02-08 04:01:44 +00:00
* @constructor
* @since 3.0.0
*
* @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
Extends: EventEmitter,
2017-07-04 12:23:58 +00:00
initialize:
2017-07-04 12:23:58 +00:00
function TextureManager (game)
{
EventEmitter.call(this);
2018-02-08 04:01:44 +00:00
/**
2022-09-28 18:09:35 +00:00
* The Game that the Texture Manager belongs to.
*
* A game will only ever have one instance of a Texture Manager.
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;
2018-02-08 04:01:44 +00:00
/**
2022-09-28 18:09:35 +00:00
* The internal name of this manager.
2018-02-08 04:01:44 +00:00
*
* @name Phaser.Textures.TextureManager#name
* @type {string}
2022-09-28 18:09:35 +00:00
* @readonly
2018-02-08 04:01:44 +00:00
* @since 3.0.0
*/
this.name = 'TextureManager';
2018-02-08 04:01:44 +00:00
/**
2022-09-28 18:09:35 +00:00
* This object contains all Textures that belong to this Texture Manager.
*
* Textures are identified by string-based keys, which are used as the property
* within this object. Therefore, you can access any texture directly from this
* object without any iteration.
*
* You should not typically modify this object directly, but instead use the
* methods provided by the Texture Manager to add and remove entries from it.
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 = {};
2018-02-08 04:01:44 +00:00
/**
* The temporary canvas element used to save the pixel data of an arbitrary texture
* during the `TextureManager.getPixel` and `getPixelAlpha` methods.
2018-02-08 04:01:44 +00:00
*
* @name Phaser.Textures.TextureManager#_tempCanvas
* @type {HTMLCanvasElement}
* @private
* @since 3.0.0
*/
this._tempCanvas = CanvasPool.create2D(this);
2018-02-08 04:01:44 +00:00
/**
* The 2d context of the `_tempCanvas` element.
2018-02-08 04:01:44 +00:00
*
* @name Phaser.Textures.TextureManager#_tempContext
* @type {CanvasRenderingContext2D}
* @private
* @since 3.0.0
*/
this._tempContext = this._tempCanvas.getContext('2d', { willReadFrequently: true });
2018-02-08 04:01:44 +00:00
/**
* An internal tracking value used for emitting the 'READY' event after all of
* the managers in the game have booted.
2018-02-08 04:01:44 +00:00
*
* @name Phaser.Textures.TextureManager#_pending
2020-11-23 10:22:13 +00:00
* @type {number}
2018-02-08 04:01:44 +00:00
* @private
* @default 0
* @since 3.0.0
*/
this._pending = 0;
2022-09-28 18:53:49 +00:00
/**
* An Image Game Object that belongs to this Texture Manager.
*
* Used as a drawing stamp within Dynamic Textures.
*
* This is not part of the display list and doesn't render.
*
* @name Phaser.Textures.TextureManager#stamp
* @type {Phaser.GameObjects.Image}
* @readonly
* @since 3.60.0
*/
2022-09-28 19:57:42 +00:00
this.stamp;
2022-09-28 18:53:49 +00:00
/**
* The crop Rectangle as used by the Stamp when it needs to crop itself.
*
* @name Phaser.Textures.TextureManager#stampCrop
* @type {Phaser.Geom.Rectangle}
* @since 3.60.0
*/
this.stampCrop = new Rectangle();
/**
* If this flag is `true` then the Texture Manager will never emit any
* warnings to the console log that report missing textures.
*
* @name Phaser.Textures.TextureManager#silentWarnings
* @type {boolean}
* @default false
* @since 3.60.0
*/
this.silentWarnings = false;
game.events.once(GameEvents.BOOT, this.boot, this);
},
2018-02-08 04:01:44 +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
* @private
2018-02-08 04:01:44 +00:00
* @since 3.0.0
*/
boot: function ()
{
this._pending = 3;
2019-01-18 13:41:43 +00:00
this.on(Events.LOAD, this.updatePending, this);
this.on(Events.ERROR, this.updatePending, this);
var config = this.game.config;
this.addBase64('__DEFAULT', config.defaultImage);
this.addBase64('__MISSING', config.missingImage);
this.addBase64('__WHITE', config.whiteImage);
this.game.events.once(GameEvents.DESTROY, this.destroy, this);
this.game.events.once(GameEvents.SYSTEM_READY, function (scene)
{
this.stamp = new ImageGameObject(scene).setOrigin(0);
}, this);
2017-07-04 12:23:58 +00:00
},
2018-02-08 04:01:44 +00:00
/**
* After 'onload' or 'onerror' invoked twice, emit 'ready' event.
2018-02-08 04:01:44 +00:00
*
* @method Phaser.Textures.TextureManager#updatePending
* @private
2018-02-08 04:01:44 +00:00
* @since 3.0.0
*/
updatePending: function ()
{
this._pending--;
if (this._pending === 0)
{
2019-01-18 13:41:43 +00:00
this.off(Events.LOAD);
this.off(Events.ERROR);
2019-01-18 13:41:43 +00:00
this.emit(Events.READY);
}
},
/**
* Checks the given texture key and throws a console.warn if the key is already in use, then returns false.
2022-08-19 15:44:55 +00:00
*
* If you wish to avoid the console.warn then use `TextureManager.exists` instead.
*
* @method Phaser.Textures.TextureManager#checkKey
2018-05-04 17:51:02 +00:00
* @since 3.7.0
*
* @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))
{
if (!this.silentWarnings)
{
// eslint-disable-next-line no-console
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
* @fires Phaser.Textures.Events#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
{
if (!this.silentWarnings)
{
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
2023-10-12 14:21:07 +00:00
var textureKey = key.key;
if (this.list.hasOwnProperty(textureKey))
2018-04-23 18:11:47 +00:00
{
key.destroy();
2023-10-12 14:21:07 +00:00
this.emit(Events.REMOVE, textureKey);
this.emit(Events.REMOVE_KEY + textureKey);
2018-04-23 18:11:47 +00:00
}
return this;
2018-04-23 22:52:57 +00:00
},
2018-04-23 18:11:47 +00:00
/**
* Removes a key from the Texture Manager but does not destroy the Texture that was using the key.
*
* @method Phaser.Textures.TextureManager#removeKey
* @since 3.17.0
*
* @param {string} key - The key to remove from the texture list.
*
* @return {Phaser.Textures.TextureManager} The Texture Manager.
*/
removeKey: function (key)
{
if (this.list.hasOwnProperty(key))
{
delete this.list[key];
}
return this;
},
2018-02-08 04:01:44 +00:00
/**
* Adds a new Texture to the Texture Manager created from the given Base64 encoded data.
*
2020-10-03 09:15:19 +00:00
* It works by creating an `Image` DOM object, then setting the `src` attribute to
* the given base64 encoded data. As a result, the process is asynchronous by its nature,
* so be sure to listen for the events this method dispatches before using the texture.
*
2018-02-08 04:01:44 +00:00
* @method Phaser.Textures.TextureManager#addBase64
* @fires Phaser.Textures.Events#ADD
* @fires Phaser.Textures.Events#ERROR
* @fires Phaser.Textures.Events#LOAD
2018-02-08 04:01:44 +00:00
* @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.
*
* @return {this} This Texture Manager instance.
2018-02-08 04:01:44 +00:00
*/
addBase64: function (key, data)
{
if (this.checkKey(key))
{
var _this = this;
var image = new Image();
image.onerror = function ()
{
_this.emit(Events.ERROR, key);
};
image.onload = function ()
{
var texture = _this.create(key, image);
2018-03-19 21:57:46 +00:00
Parser.Image(texture, 0);
_this.emit(Events.ADD, key, texture);
_this.emit(Events.ADD_KEY + key, texture);
_this.emit(Events.LOAD, key, texture);
};
image.src = data;
}
return this;
},
2018-07-27 10:19:12 +00:00
/**
* Gets an existing texture frame and converts it into a base64 encoded image and returns the base64 data.
*
2018-07-27 10:19:12 +00:00
* You can also provide the image type and encoder options.
*
2019-07-08 14:24:12 +00:00
* This will only work with bitmap based texture frames, such as those created from Texture Atlases.
* It will not work with GL Texture objects, such as Shaders, or Render Textures. For those please
* see the WebGL Snapshot function instead.
2018-07-27 10:19:12 +00:00
*
* @method Phaser.Textures.TextureManager#getBase64
* @since 3.12.0
*
* @param {string} key - The unique string-based key of the Texture.
2020-11-23 10:32:00 +00:00
* @param {(string|number)} [frame] - The string-based name, or integer based index, of the Frame to get from the Texture.
2020-02-04 14:56:28 +00:00
* @param {string} [type='image/png'] - A DOMString indicating the image format. The default format type is image/png.
* @param {number} [encoderOptions=0.92] - A Number between 0 and 1 indicating the image quality to use for image formats that use lossy compression such as image/jpeg and image/webp. If this argument is anything else, the default value for image quality is used. The default value is 0.92. Other arguments are ignored.
*
2018-07-27 10:19:12 +00:00
* @return {string} The base64 encoded data, or an empty string if the texture frame could not be found.
*/
getBase64: function (key, frame, type, encoderOptions)
2018-07-27 08:43:12 +00:00
{
2018-07-27 10:19:12 +00:00
if (type === undefined) { type = 'image/png'; }
if (encoderOptions === undefined) { encoderOptions = 0.92; }
2018-07-27 08:43:12 +00:00
var data = '';
var textureFrame = this.getFrame(key, frame);
2019-07-15 14:42:25 +00:00
if (textureFrame && (textureFrame.source.isRenderTexture || textureFrame.source.isGLTexture))
{
if (!this.silentWarnings)
{
console.warn('Cannot getBase64 from WebGL Texture');
}
2019-07-15 14:42:25 +00:00
}
else if (textureFrame)
2018-07-27 08:43:12 +00:00
{
var cd = textureFrame.canvasData;
var canvas = CanvasPool.create2D(this, cd.width, cd.height);
var ctx = canvas.getContext('2d', { willReadFrequently: true });
2018-07-27 08:43:12 +00:00
if (cd.width > 0 && cd.height > 0)
{
ctx.drawImage(
textureFrame.source.image,
cd.x,
cd.y,
cd.width,
cd.height,
0,
0,
cd.width,
cd.height
);
}
2018-07-27 08:43:12 +00:00
2018-07-27 10:19:12 +00:00
data = canvas.toDataURL(type, encoderOptions);
2018-07-27 08:43:12 +00:00
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
* @fires Phaser.Textures.Events#ADD
2018-02-08 04:01:44 +00:00
* @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.
2018-12-13 09:17:02 +00:00
* @param {HTMLImageElement|HTMLCanvasElement} [dataSource] - An optional data Image element.
2018-02-08 04:01:44 +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
*/
addImage: function (key, source, dataSource)
{
var texture = null;
2018-03-19 21:57:46 +00:00
if (this.checkKey(key))
{
texture = this.create(key, source);
Parser.Image(texture, 0);
if (dataSource)
{
texture.setDataSource(dataSource);
}
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
return texture;
},
/**
* Takes a WebGL Texture and creates a Phaser Texture from it, which is added to the Texture Manager using the given key.
*
* This allows you to then use the Texture as a normal texture for texture based Game Objects like Sprites.
*
* If the `width` and `height` arguments are omitted, but the WebGL Texture was created by Phaser's WebGL Renderer
* and has `glTexture.width` and `glTexture.height` properties, these values will be used instead.
*
* This is a WebGL only feature.
*
* @method Phaser.Textures.TextureManager#addGLTexture
* @fires Phaser.Textures.Events#ADD
* @since 3.19.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {WebGLTexture} glTexture - The source Render Texture.
* @param {number} [width] - The new width of the Texture. Read from `glTexture.width` if omitted.
* @param {number} [height] - The new height of the Texture. Read from `glTexture.height` if omitted.
*
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
*/
addGLTexture: function (key, glTexture)
{
var texture = null;
if (this.checkKey(key))
{
var width = glTexture.width;
var height = glTexture.height;
texture = this.create(key, glTexture, width, height);
texture.add('__BASE', 0, 0, 0, width, height);
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
return texture;
},
/**
* Adds a Compressed Texture to this Texture Manager.
*
* The texture should typically have been loaded via the `CompressedTextureFile` loader,
* in order to prepare the correct data object this method requires.
*
* You can optionally also pass atlas data to this method, in which case a texture atlas
* will be generated from the given compressed texture, combined with the atlas data.
*
* @method Phaser.Textures.TextureManager#addCompressedTexture
* @fires Phaser.Textures.Events#ADD
* @since 3.60.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {Phaser.Types.Textures.CompressedTextureData} textureData - The Compressed Texture data object.
* @param {object} [atlasData] - Optional Texture Atlas data.
*
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
*/
addCompressedTexture: function (key, textureData, atlasData)
{
var texture = null;
if (this.checkKey(key))
{
texture = this.create(key, textureData);
texture.add('__BASE', 0, 0, 0, textureData.width, textureData.height);
if (atlasData)
{
if (Array.isArray(atlasData))
{
for (var i = 0; i < atlasData.length; i++)
{
Parser.JSONHash(texture, i, atlasData[i]);
}
}
else
{
Parser.JSONHash(texture, 0, atlasData);
}
}
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
return texture;
},
2018-08-02 11:34:01 +00:00
/**
* Adds a Render Texture to the Texture Manager using the given key.
* This allows you to then use the Render Texture as a normal texture for texture based Game Objects like Sprites.
*
* @method Phaser.Textures.TextureManager#addRenderTexture
* @fires Phaser.Textures.Events#ADD
2018-08-02 11:34:01 +00:00
* @since 3.12.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {Phaser.GameObjects.RenderTexture} renderTexture - The source Render Texture.
*
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
*/
addRenderTexture: function (key, renderTexture)
{
var texture = null;
if (this.checkKey(key))
{
texture = this.create(key, renderTexture);
texture.add('__BASE', 0, 0, 0, renderTexture.width, renderTexture.height);
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
2018-08-02 11:34:01 +00:00
}
2018-08-02 11:34:01 +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.
*
2018-02-08 13:45:53 +00:00
* Generated textures consist of a Canvas element to which the texture data is drawn.
*
2020-02-04 17:16:19 +00:00
* Generates a texture based on the given Create configuration object.
*
2020-02-04 17:16:19 +00:00
* The texture is drawn using a fixed-size indexed palette of 16 colors, where the hex value in the
* data cells map to a single color. For example, if the texture config looked like this:
*
* ```javascript
* var star = [
* '.....828.....',
* '....72227....',
* '....82228....',
* '...7222227...',
* '2222222222222',
* '8222222222228',
* '.72222222227.',
* '..787777787..',
* '..877777778..',
* '.78778887787.',
* '.27887.78872.',
* '.787.....787.'
* ];
*
2020-02-04 17:16:19 +00:00
* this.textures.generate('star', { data: star, pixelWidth: 4 });
* ```
*
2020-02-04 17:16:19 +00:00
* Then it would generate a texture that is 52 x 48 pixels in size, because each cell of the data array
* represents 1 pixel multiplied by the `pixelWidth` value. The cell values, such as `8`, maps to color
* number 8 in the palette. If a cell contains a period character `.` then it is transparent.
*
2020-02-04 17:16:19 +00:00
* The default palette is Arne16, but you can specify your own using the `palette` property.
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.
2020-02-04 17:16:19 +00:00
* @param {Phaser.Types.Create.GenerateTextureConfig} config - The configuration object needed to generate the texture.
2018-02-08 04:01:44 +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
*/
generate: function (key, config)
{
if (this.checkKey(key))
{
var canvas = CanvasPool.create(this, 1, 1);
config.canvas = canvas;
GenerateTexture(config);
return this.addCanvas(key, canvas);
}
else
{
return null;
}
},
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.
2020-11-23 10:22:13 +00:00
* @param {number} [width=256] - The width of the Canvas element.
* @param {number} [height=256] - The height of the Canvas element.
2018-02-08 04:01:44 +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
*/
createCanvas: function (key, width, height)
{
if (width === undefined) { width = 256; }
if (height === undefined) { height = 256; }
if (this.checkKey(key))
{
var canvas = CanvasPool.create(this, width, height, CONST.CANVAS, true);
return this.addCanvas(key, canvas);
}
return null;
},
2018-02-08 04:01:44 +00:00
/**
* Creates a new Canvas Texture object from an existing Canvas element
* and adds it to this Texture Manager, unless `skipCache` is true.
2018-02-08 04:01:44 +00:00
*
* @method Phaser.Textures.TextureManager#addCanvas
* @fires Phaser.Textures.Events#ADD
2018-02-08 04:01:44 +00:00
* @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.
* @param {boolean} [skipCache=false] - Skip adding this Texture into the Cache?
2018-02-08 04:01:44 +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
*/
addCanvas: function (key, source, skipCache)
{
if (skipCache === undefined) { skipCache = false; }
var texture = null;
2018-03-19 21:57:46 +00:00
if (skipCache)
{
texture = new CanvasTexture(this, key, source, source.width, source.height);
}
else if (this.checkKey(key))
{
texture = new CanvasTexture(this, key, source, source.width, source.height);
this.list[key] = texture;
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
return texture;
},
2022-09-28 18:09:35 +00:00
/**
* Creates a Dynamic Texture instance and adds itself to this Texture Manager.
*
* A Dynamic Texture is a special texture that allows you to draw textures, frames and most kind of
* Game Objects directly to it.
*
* You can take many complex objects and draw them to this one texture, which can then be used as the
* base texture for other Game Objects, such as Sprites. Should you then update this texture, all
* Game Objects using it will instantly be updated as well, reflecting the changes immediately.
*
* It's a powerful way to generate dynamic textures at run-time that are WebGL friendly and don't invoke
* expensive GPU uploads on each change.
*
* See the methods available on the `DynamicTexture` class for more details.
*
* Optionally, you can also pass a Dynamic Texture instance to this method to have
* it added to the Texture Manager.
*
2022-09-28 18:09:35 +00:00
* @method Phaser.Textures.TextureManager#addDynamicTexture
* @fires Phaser.Textures.Events#ADD
* @since 3.60.0
*
* @param {(string|Phaser.Textures.DynamicTexture)} key - The string-based key of this Texture. Must be unique within the Texture Manager. Or, a DynamicTexture instance.
* @param {number} [width=256] - The width of this Dynamic Texture in pixels. Defaults to 256 x 256. Ignored if an instance is passed as the key.
* @param {number} [height=256] - The height of this Dynamic Texture in pixels. Defaults to 256 x 256. Ignored if an instance is passed as the key.
2022-09-28 18:09:35 +00:00
*
* @return {?Phaser.Textures.DynamicTexture} The Dynamic Texture that was created, or `null` if the key is already in use.
*/
2022-09-27 22:39:23 +00:00
addDynamicTexture: function (key, width, height)
{
var texture = null;
if (typeof(key) === 'string' && !this.exists(key))
2022-09-27 22:39:23 +00:00
{
texture = new DynamicTexture(this, key, width, height);
}
else
{
texture = key;
key = texture.key;
}
2022-09-27 22:39:23 +00:00
if (this.checkKey(key))
{
2022-09-27 22:39:23 +00:00
this.list[key] = texture;
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
else
{
texture = null;
}
2022-09-27 22:39:23 +00:00
return texture;
},
2018-02-08 04:01:44 +00:00
/**
* Adds a Texture Atlas to this Texture Manager.
*
* In Phaser terminology, a Texture Atlas is a combination of an atlas image and a JSON data file,
* such as those exported by applications like Texture Packer.
*
2018-02-08 13:45:53 +00:00
* 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
*
* As of Phaser 3.60 you can use this method to add a atlas data to an existing Phaser Texture.
*
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.
* @param {(HTMLImageElement|HTMLImageElement[]|Phaser.Textures.Texture)} source - The source Image element/s, or a Phaser Texture.
* @param {(object|object[])} data - The Texture Atlas data/s.
2018-12-13 09:17:02 +00:00
* @param {HTMLImageElement|HTMLCanvasElement|HTMLImageElement[]|HTMLCanvasElement[]} [dataSource] - An optional data Image element.
2018-02-08 04:01:44 +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)
{
2018-02-09 15:22:55 +00:00
// New Texture Packer format?
if (Array.isArray(data.textures) || Array.isArray(data.frames))
{
2018-05-04 13:32:13 +00:00
return this.addAtlasJSONArray(key, source, data, dataSource);
}
else
{
2018-05-04 13:32:13 +00:00
return this.addAtlasJSONHash(key, source, data, dataSource);
}
},
2018-02-08 04:01:44 +00:00
/**
2018-02-08 13:45:53 +00:00
* Adds a Texture Atlas to this Texture Manager.
*
* In Phaser terminology, a Texture Atlas is a combination of an atlas image and a JSON data file,
* such as those exported by applications like Texture Packer.
*
2018-02-08 13:45:53 +00:00
* The frame data of the atlas must be stored in an Array within the JSON.
*
2018-02-08 13:45:53 +00:00
* This is known as a JSON Array in software such as Texture Packer.
2018-02-08 04:01:44 +00:00
*
* As of Phaser 3.60 you can use this method to add a atlas data to an existing Phaser Texture.
*
2018-02-08 04:01:44 +00:00
* @method Phaser.Textures.TextureManager#addAtlasJSONArray
* @fires Phaser.Textures.Events#ADD
2018-02-08 04:01:44 +00:00
* @since 3.0.0
*
2018-02-08 13:45:53 +00:00
* @param {string} key - The unique string-based key of the Texture.
* @param {(HTMLImageElement|HTMLImageElement[]|Phaser.Textures.Texture)} source - The source Image element/s, or a Phaser Texture.
* @param {(object|object[])} data - The Texture Atlas data/s.
2018-12-13 09:17:02 +00:00
* @param {HTMLImageElement|HTMLCanvasElement|HTMLImageElement[]|HTMLCanvasElement[]} [dataSource] - An optional data Image element.
2018-02-08 04:01:44 +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)
{
var texture = null;
if (source instanceof Texture)
{
key = source.key;
texture = source;
}
else if (this.checkKey(key))
{
texture = this.create(key, source);
}
if (texture)
{
2018-05-04 13:32:13 +00:00
// Multi-Atlas?
if (Array.isArray(data))
{
var singleAtlasFile = (data.length === 1); // multi-pack with one atlas file for all images
// !! Assumes the textures are in the same order in the source array as in the json data !!
for (var i = 0; i < texture.source.length; i++)
{
var atlasData = singleAtlasFile ? data[0] : data[i];
Parser.JSONArray(texture, i, atlasData);
}
}
else
{
Parser.JSONArray(texture, 0, data);
}
2018-05-04 13:32:13 +00:00
if (dataSource)
{
texture.setDataSource(dataSource);
}
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
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.
*
* In Phaser terminology, a Texture Atlas is a combination of an atlas image and a JSON data file,
* such as those exported by applications like Texture Packer.
*
2018-02-08 13:45:53 +00:00
* The frame data of the atlas must be stored in an Object within the JSON.
*
2018-02-08 13:45:53 +00:00
* This is known as a JSON Hash in software such as Texture Packer.
2018-02-08 04:01:44 +00:00
*
* As of Phaser 3.60 you can use this method to add a atlas data to an existing Phaser Texture.
*
2018-02-08 04:01:44 +00:00
* @method Phaser.Textures.TextureManager#addAtlasJSONHash
* @fires Phaser.Textures.Events#ADD
2018-02-08 04:01:44 +00:00
* @since 3.0.0
*
2018-02-08 13:45:53 +00:00
* @param {string} key - The unique string-based key of the Texture.
* @param {(HTMLImageElement|HTMLImageElement[]|Phaser.Textures.Texture)} source - The source Image element/s, or a Phaser Texture.
* @param {(object|object[])} data - The Texture Atlas data/s.
2018-12-13 09:17:02 +00:00
* @param {HTMLImageElement|HTMLCanvasElement|HTMLImageElement[]|HTMLCanvasElement[]} [dataSource] - An optional data Image element.
2018-02-08 04:01:44 +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)
{
var texture = null;
if (source instanceof Texture)
{
key = source.key;
texture = source;
}
else if (this.checkKey(key))
{
texture = this.create(key, source);
}
if (texture)
{
if (Array.isArray(data))
{
for (var i = 0; i < data.length; i++)
{
Parser.JSONHash(texture, i, data[i]);
}
}
else
{
Parser.JSONHash(texture, 0, data);
}
2018-05-04 13:32:13 +00:00
if (dataSource)
{
texture.setDataSource(dataSource);
}
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
2018-05-04 13:32:13 +00:00
}
return texture;
},
/**
* Adds a Texture Atlas to this Texture Manager.
*
* In Phaser terminology, a Texture Atlas is a combination of an atlas image and a data file,
* such as those exported by applications like Texture Packer.
*
* The frame data of the atlas must be stored in an XML file.
*
* As of Phaser 3.60 you can use this method to add a atlas data to an existing Phaser Texture.
2018-05-04 13:32:13 +00:00
*
* @method Phaser.Textures.TextureManager#addAtlasXML
* @fires Phaser.Textures.Events#ADD
2018-05-04 13:32:13 +00:00
* @since 3.7.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {(HTMLImageElement|Phaser.Textures.Texture)} source - The source Image element, or a Phaser Texture.
2018-05-04 13:32:13 +00:00
* @param {object} data - The Texture Atlas XML data.
2018-12-13 09:17:02 +00:00
* @param {HTMLImageElement|HTMLCanvasElement|HTMLImageElement[]|HTMLCanvasElement[]} [dataSource] - An optional data Image element.
2018-05-04 13:32:13 +00:00
*
* @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 (source instanceof Texture)
{
key = source.key;
texture = source;
}
else if (this.checkKey(key))
2018-05-04 13:32:13 +00:00
{
texture = this.create(key, source);
}
if (texture)
{
2018-05-04 13:32:13 +00:00
Parser.AtlasXML(texture, 0, data);
if (dataSource)
{
texture.setDataSource(dataSource);
}
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
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.
*
* In Phaser terminology, a Texture Atlas is a combination of an atlas image and a data file,
* such as those exported by applications like Texture Packer or Unity.
*
* The frame data of the atlas must be stored in a Unity YAML file.
*
* As of Phaser 3.60 you can use this method to add a atlas data to an existing Phaser Texture.
2018-02-08 04:01:44 +00:00
*
* @method Phaser.Textures.TextureManager#addUnityAtlas
* @fires Phaser.Textures.Events#ADD
2018-02-08 04:01:44 +00:00
* @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-12-13 09:17:02 +00:00
* @param {HTMLImageElement|HTMLCanvasElement|HTMLImageElement[]|HTMLCanvasElement[]} [dataSource] - An optional data Image element.
2018-02-08 04:01:44 +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)
{
var texture = null;
if (source instanceof Texture)
{
key = source.key;
texture = source;
}
else if (this.checkKey(key))
{
2018-04-23 22:52:57 +00:00
texture = this.create(key, source);
}
if (texture)
{
Parser.UnityYAML(texture, 0, data);
2018-05-04 13:32:13 +00:00
if (dataSource)
{
texture.setDataSource(dataSource);
}
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
return texture;
},
/**
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. This is different to a Texture Atlas, created by tools such as
* Texture Packer, and more akin with the fixed-frame exports you get from apps like Aseprite or old arcade
* games.
*
* As of Phaser 3.60 you can use this method to add a sprite sheet to an existing Phaser Texture.
2018-02-08 04:01:44 +00:00
*
* @method Phaser.Textures.TextureManager#addSpriteSheet
* @fires Phaser.Textures.Events#ADD
2018-02-08 04:01:44 +00:00
* @since 3.0.0
*
* @param {string} key - The unique string-based key of the Texture. Give an empty string if you provide a Phaser Texture as the 2nd argument.
* @param {(HTMLImageElement|Phaser.Textures.Texture)} source - The source Image element, or a Phaser Texture.
2019-05-09 11:38:45 +00:00
* @param {Phaser.Types.Textures.SpriteSheetConfig} config - The configuration object for this Sprite Sheet.
* @param {HTMLImageElement|HTMLCanvasElement} [dataSource] - An optional data Image element.
2018-02-08 04:01:44 +00:00
*
* @return {?Phaser.Textures.Texture} The Texture that was created or updated, or `null` if the key is already in use.
2018-02-08 04:01:44 +00:00
*/
addSpriteSheet: function (key, source, config, dataSource)
{
var texture = null;
if (source instanceof Texture)
{
key = source.key;
texture = source;
}
else if (this.checkKey(key))
{
texture = this.create(key, source);
}
if (texture)
{
var width = texture.source[0].width;
var height = texture.source[0].height;
Parser.SpriteSheet(texture, 0, 0, 0, width, height, config);
if (dataSource)
{
texture.setDataSource(dataSource);
}
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
return texture;
},
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
* @fires Phaser.Textures.Events#ADD
2018-02-08 04:01:44 +00:00
* @since 3.0.0
*
2018-02-08 13:45:53 +00:00
* @param {string} key - The unique string-based key of the Texture.
2019-05-09 11:38:45 +00:00
* @param {Phaser.Types.Textures.SpriteSheetFromAtlasConfig} config - The configuration object for this Sprite Sheet.
2018-02-08 04:01:44 +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)
{
if (!this.checkKey(key))
{
return null;
}
var atlasKey = GetValue(config, 'atlas', null);
var atlasFrame = GetValue(config, 'frame', null);
2017-04-11 16:22:22 +00:00
if (!atlasKey || !atlasFrame)
{
return;
}
var atlas = this.get(atlasKey);
var sheet = atlas.get(atlasFrame);
if (sheet)
{
var texture = this.create(key, sheet.source.image);
if (sheet.trimmed)
{
2017-11-03 13:15:58 +00:00
// If trimmed we need to help the parser adjust
Parser.SpriteSheetFromAtlas(texture, sheet, config);
}
else
{
Parser.SpriteSheet(texture, 0, sheet.cutX, sheet.cutY, sheet.cutWidth, sheet.cutHeight, config);
}
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
return texture;
}
},
/**
* Creates a texture from an array of colour data.
*
* This is only available in WebGL mode.
*
* If the dimensions provided are powers of two, the resulting texture
* will be automatically set to wrap by the WebGL Renderer.
*
* @method Phaser.Textures.TextureManager#addUint8Array
* @fires Phaser.Textures.Events#ADD
* @since 3.80.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {Uint8Array} data - The color data for the texture.
* @param {number} width - The width of the texture.
* @param {number} height - The height of the texture.
*
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
*/
addUint8Array: function (key, data, width, height)
{
if (
!this.checkKey(key) ||
data.length / 4 !== width * height
)
{
return null;
}
var texture = this.create(key, data, width, height);
texture.add('__BASE', 0, 0, 0, width, height);
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
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.
* @param {(HTMLImageElement|HTMLCanvasElement|HTMLImageElement[]|HTMLCanvasElement[]|Phaser.Renderer.WebGL.Wrappers.WebGLTextureWrapper)} source - An array of sources that are used to create the texture. Usually Images, but can also be a Canvas.
2024-01-12 17:56:30 +00:00
* @param {number} [width] - The width of the Texture. This is optional and automatically derived from the source images.
* @param {number} [height] - The height of the Texture. This is optional and automatically derived from the source images.
2018-02-08 04:01:44 +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)
{
var texture = null;
if (this.checkKey(key))
{
texture = new Texture(this, key, source, width, height);
this.list[key] = texture;
}
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
*/
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 an instance of a Texture, it will return the instance.
*
* If the key is an instance of a Frame, it will return the frames parent Texture instance.
*
* Finally, if the key is given, but not found, and not a Texture or Frame instance, it will return the `__MISSING` Texture.
2018-02-08 04:01:44 +00:00
*
* @method Phaser.Textures.TextureManager#get
* @since 3.0.0
*
* @param {(string|Phaser.Textures.Texture|Phaser.Textures.Frame)} key - The unique string-based key of the Texture, or a Texture, or Frame instance.
2018-02-08 04:01:44 +00:00
*
* @return {Phaser.Textures.Texture} The Texture matching the given key.
2018-02-08 04:01:44 +00:00
*/
get: function (key)
{
if (key === undefined) { key = '__DEFAULT'; }
if (this.list[key])
{
return this.list[key];
}
else if (key instanceof Texture)
{
return key;
}
else if (key instanceof Frame)
{
return key.texture;
}
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.
2020-11-23 10:32:00 +00:00
* @param {(string|number)} 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
*/
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.
2020-11-23 10:32:00 +00:00
* @param {(string|number)} [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
*/
getFrame: function (key, frame)
{
if (this.list[key])
{
return this.list[key].get(frame);
}
},
/**
* Parses the 'key' parameter and returns a Texture Frame instance.
*
* It can accept the following formats:
*
* 1) A string
2022-09-26 22:18:12 +00:00
* 2) An array where the elements are: [ key, [frame] ]
* 3) An object with the properties: { key, [frame] }
* 4) A Texture instance - which returns the default frame from the Texture
* 5) A Frame instance - returns itself
*
* @method Phaser.Textures.TextureManager#parseFrame
* @since 3.60.0
*
2022-09-26 22:18:12 +00:00
* @param {(string|array|object|Phaser.Textures.Texture|Phaser.Textures.Frame)} key - The key to be parsed.
*
* @return {Phaser.Textures.Frame} A Texture Frame object, if found, or undefined if not.
*/
parseFrame: function (key)
{
if (!key)
{
return undefined;
}
else if (typeof key === 'string')
{
return this.getFrame(key);
}
else if (Array.isArray(key) && key.length === 2)
{
return this.getFrame(key[0], key[1]);
}
else if (IsPlainObject(key))
{
return this.getFrame(key.key, key.frame);
}
else if (key instanceof Texture)
{
return key.get();
}
else if (key instanceof Frame)
{
return key;
}
},
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.
2022-08-23 17:38:51 +00:00
* The output array will exclude the `__DEFAULT`, `__MISSING`, and `__WHITE` 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
*/
getTextureKeys: function ()
{
var output = [];
for (var key in this.list)
{
2022-08-23 17:38:51 +00:00
if (key !== '__DEFAULT' && key !== '__MISSING' && key !== '__WHITE')
{
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
*
2020-11-23 10:22:13 +00:00
* @param {number} x - The x coordinate of the pixel within the Texture.
* @param {number} y - The y coordinate of the pixel within the Texture.
2018-02-08 13:45:53 +00:00
* @param {string} key - The unique string-based key of the Texture.
2020-11-23 10:32:00 +00:00
* @param {(string|number)} [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
*/
getPixel: function (x, y, key, frame)
{
var textureFrame = this.getFrame(key, frame);
if (textureFrame)
{
// Adjust for trim (if not trimmed x and y are just zero)
x -= textureFrame.x;
y -= textureFrame.y;
var data = textureFrame.data.cut;
x += data.x;
y += data.y;
if (x >= data.x && x < data.r && y >= data.y && y < data.b)
{
var ctx = this._tempContext;
ctx.clearRect(0, 0, 1, 1);
ctx.drawImage(textureFrame.source.image, x, y, 1, 1, 0, 0, 1, 1);
var rgb = ctx.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
*
2020-11-23 10:22:13 +00:00
* @param {number} x - The x coordinate of the pixel within the Texture.
* @param {number} y - The y coordinate of the pixel within the Texture.
2018-06-08 16:50:26 +00:00
* @param {string} key - The unique string-based key of the Texture.
2020-11-23 10:32:00 +00:00
* @param {(string|number)} [frame] - The string or index of the Frame.
2018-06-08 16:50:26 +00:00
*
2020-11-23 10:22:13 +00:00
* @return {number} A value between 0 and 255, or `null` if the coordinates were out of bounds.
2018-06-08 16:50:26 +00:00
*/
getPixelAlpha: function (x, y, key, frame)
{
var textureFrame = this.getFrame(key, frame);
if (textureFrame)
2018-06-08 16:50:26 +00:00
{
// Adjust for trim (if not trimmed x and y are just zero)
x -= textureFrame.x;
y -= textureFrame.y;
2018-06-08 16:50:26 +00:00
var data = textureFrame.data.cut;
2018-06-08 16:50:26 +00:00
x += data.x;
y += data.y;
2018-06-08 16:50:26 +00:00
if (x >= data.x && x < data.r && y >= data.y && y < data.b)
{
var ctx = this._tempContext;
ctx.clearRect(0, 0, 1, 1);
ctx.drawImage(textureFrame.source.image, x, y, 1, 1, 0, 0, 1, 1);
var rgb = ctx.getImageData(0, 0, 1, 1);
return rgb.data[3];
}
2018-06-08 16:50:26 +00:00
}
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
*
* @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.
2020-11-23 10:32:00 +00:00
* @param {(string|number)} [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
*/
setTexture: function (gameObject, key, frame)
{
if (this.list[key])
{
gameObject.texture = this.list[key];
gameObject.frame = gameObject.texture.get(frame);
}
return gameObject;
},
/**
* Changes the key being used by a Texture to the new key provided.
*
* The old key is removed, allowing it to be re-used.
*
* Game Objects are linked to Textures by a reference to the Texture object, so
* all existing references will be retained.
*
* @method Phaser.Textures.TextureManager#renameTexture
* @since 3.12.0
*
* @param {string} currentKey - The current string-based key of the Texture you wish to rename.
* @param {string} newKey - The new unique string-based key to use for the Texture.
*
* @return {boolean} `true` if the Texture key was successfully renamed, otherwise `false`.
*/
renameTexture: function (currentKey, newKey)
{
var texture = this.get(currentKey);
if (texture && currentKey !== newKey)
{
texture.key = newKey;
this.list[newKey] = texture;
delete this.list[currentKey];
return true;
}
return false;
},
/**
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)
{
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);
}
},
2022-09-28 21:43:20 +00:00
/**
* Resets the internal Stamp object, ready for drawing and returns it.
*
* @method Phaser.Textures.TextureManager#resetStamp
* @since 3.60.0
*
* @param {number} [alpha=1] - The alpha to use.
* @param {number} [tint=0xffffff] - WebGL only. The tint color to use.
*
* @return {Phaser.GameObjects.Image} A reference to the Stamp Game Object.
*/
resetStamp: function (alpha, tint)
{
if (alpha === undefined) { alpha = 1; }
if (tint === undefined) { tint = 0xffffff; }
var stamp = this.stamp;
stamp.setCrop();
stamp.setPosition(0);
stamp.setAngle(0);
2022-10-03 22:15:40 +00:00
stamp.setScale(1);
2022-09-28 21:43:20 +00:00
stamp.setAlpha(alpha);
stamp.setTint(tint);
2023-10-12 14:21:07 +00:00
stamp.setTexture('__WHITE');
2022-09-28 21:43:20 +00:00
return stamp;
},
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
*/
destroy: function ()
{
for (var texture in this.list)
{
this.list[texture].destroy();
}
this.list = {};
2022-09-28 18:53:49 +00:00
this.stamp.destroy();
this.game = null;
2022-09-28 18:53:49 +00:00
this.stamp = null;
CanvasPool.remove(this._tempCanvas);
}
2017-07-04 12:23:58 +00:00
});
module.exports = TextureManager;