2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2019-01-15 16:20:22 +00:00
|
|
|
* @copyright 2019 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-07-04 12:23:58 +00:00
|
|
|
var Class = require('../utils/Class');
|
2018-07-03 15:47:53 +00:00
|
|
|
var Clamp = require('../math/Clamp');
|
2016-12-15 17:15:47 +00:00
|
|
|
var Extend = require('../utils/object/Extend');
|
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* @classdesc
|
|
|
|
* A Frame is a section of a Texture.
|
|
|
|
*
|
|
|
|
* @class Frame
|
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.Textures.Texture} texture - The Texture this Frame is a part of.
|
2018-03-20 15:10:19 +00:00
|
|
|
* @param {(integer|string)} name - The name of this Frame. The name is unique within the Texture.
|
2018-02-08 04:01:44 +00:00
|
|
|
* @param {integer} sourceIndex - The index of the TextureSource that this Frame is a part of.
|
|
|
|
* @param {number} x - The x coordinate of the top-left of this Frame.
|
|
|
|
* @param {number} y - The y coordinate of the top-left of this Frame.
|
|
|
|
* @param {number} width - The width of this Frame.
|
|
|
|
* @param {number} height - The height of this Frame.
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
var Frame = new Class({
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
initialize:
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
function Frame (texture, name, sourceIndex, x, y, width, height)
|
|
|
|
{
|
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The Texture this Frame is a part of.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#texture
|
|
|
|
* @type {Phaser.Textures.Texture}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.texture = texture;
|
|
|
|
|
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The name of this Frame.
|
|
|
|
* The name is unique within the Texture.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#name
|
|
|
|
* @type {string}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.name = name;
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* The TextureSource this Frame is part of.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#source
|
|
|
|
* @type {Phaser.Textures.TextureSource}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.source = texture.source[sourceIndex];
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* The index of the TextureSource in the Texture sources array.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#sourceIndex
|
|
|
|
* @type {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.sourceIndex = sourceIndex;
|
|
|
|
|
2018-07-02 12:43:35 +00:00
|
|
|
/**
|
|
|
|
* A reference to the Texture Source WebGL Texture that this Frame is using.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#glTexture
|
|
|
|
* @type {?WebGLTexture}
|
|
|
|
* @default null
|
|
|
|
* @since 3.11.0
|
|
|
|
*/
|
|
|
|
this.glTexture = this.source.glTexture;
|
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* X position within the source image to cut from.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#cutX
|
|
|
|
* @type {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-04-23 22:40:12 +00:00
|
|
|
this.cutX;
|
2017-07-04 12:23:58 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* Y position within the source image to cut from.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#cutY
|
|
|
|
* @type {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-04-23 22:40:12 +00:00
|
|
|
this.cutY;
|
2017-07-04 12:23:58 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The width of the area in the source image to cut.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#cutWidth
|
|
|
|
* @type {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-04-23 22:40:12 +00:00
|
|
|
this.cutWidth;
|
2017-07-04 12:23:58 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The height of the area in the source image to cut.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#cutHeight
|
|
|
|
* @type {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-04-23 22:40:12 +00:00
|
|
|
this.cutHeight;
|
2017-07-04 12:23:58 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The X rendering offset of this Frame, taking trim into account.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#x
|
|
|
|
* @type {integer}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.x = 0;
|
|
|
|
|
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The Y rendering offset of this Frame, taking trim into account.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#y
|
|
|
|
* @type {integer}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.y = 0;
|
|
|
|
|
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The rendering width of this Frame, taking trim into account.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#width
|
|
|
|
* @type {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-04-23 22:40:12 +00:00
|
|
|
this.width;
|
2017-07-04 12:23:58 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The rendering height of this Frame, taking trim into account.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#height
|
|
|
|
* @type {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-04-23 22:40:12 +00:00
|
|
|
this.height;
|
2017-07-04 12:23:58 +00:00
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* Half the width, floored.
|
|
|
|
* Precalculated for the renderer.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#halfWidth
|
|
|
|
* @type {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-04-23 22:40:12 +00:00
|
|
|
this.halfWidth;
|
2017-10-16 21:16:33 +00:00
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* Half the height, floored.
|
|
|
|
* Precalculated for the renderer.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#halfHeight
|
|
|
|
* @type {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-04-23 22:40:12 +00:00
|
|
|
this.halfHeight;
|
2017-10-16 21:16:33 +00:00
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The x center of this frame, floored.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#centerX
|
|
|
|
* @type {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-04-23 22:40:12 +00:00
|
|
|
this.centerX;
|
2017-07-04 12:23:58 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The y center of this frame, floored.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#centerY
|
|
|
|
* @type {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-04-23 22:40:12 +00:00
|
|
|
this.centerY;
|
2017-07-04 12:23:58 +00:00
|
|
|
|
2018-01-19 18:23:25 +00:00
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The horizontal pivot point of this Frame.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#pivotX
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-19 18:23:25 +00:00
|
|
|
this.pivotX = 0;
|
|
|
|
|
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The vertical pivot point of this Frame.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#pivotY
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-19 18:23:25 +00:00
|
|
|
this.pivotY = 0;
|
|
|
|
|
2018-02-09 15:22:15 +00:00
|
|
|
/**
|
|
|
|
* Does this Frame have a custom pivot point?
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#customPivot
|
|
|
|
* @type {boolean}
|
|
|
|
* @default false
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
this.customPivot = false;
|
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* **CURRENTLY UNSUPPORTED**
|
2018-03-20 15:10:19 +00:00
|
|
|
*
|
2018-02-08 04:01:44 +00:00
|
|
|
* Is this frame is rotated or not in the Texture?
|
|
|
|
* Rotation allows you to use rotated frames in texture atlas packing.
|
|
|
|
* It has nothing to do with Sprite rotation.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#rotated
|
|
|
|
* @type {boolean}
|
|
|
|
* @default false
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.rotated = false;
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* Over-rides the Renderer setting.
|
|
|
|
* -1 = use Renderer Setting
|
|
|
|
* 0 = No rounding
|
|
|
|
* 1 = Round
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#autoRound
|
|
|
|
* @type {integer}
|
|
|
|
* @default -1
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.autoRound = -1;
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* Any Frame specific custom data can be stored here.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#customData
|
|
|
|
* @type {object}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-19 18:23:25 +00:00
|
|
|
this.customData = {};
|
|
|
|
|
2018-07-02 22:51:42 +00:00
|
|
|
/**
|
|
|
|
* WebGL UV u0 value.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#u0
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.11.0
|
|
|
|
*/
|
|
|
|
this.u0 = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WebGL UV v0 value.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#v0
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.11.0
|
|
|
|
*/
|
|
|
|
this.v0 = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WebGL UV u1 value.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#u1
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.11.0
|
|
|
|
*/
|
|
|
|
this.u1 = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WebGL UV v1 value.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#v1
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.11.0
|
|
|
|
*/
|
|
|
|
this.v1 = 0;
|
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The un-modified source frame, trim and UV data.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#data
|
|
|
|
* @type {object}
|
|
|
|
* @private
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.data = {
|
|
|
|
cut: {
|
2018-04-23 22:40:12 +00:00
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
w: 0,
|
|
|
|
h: 0,
|
|
|
|
r: 0,
|
|
|
|
b: 0
|
2017-07-04 12:23:58 +00:00
|
|
|
},
|
|
|
|
trim: false,
|
|
|
|
sourceSize: {
|
2018-04-23 22:40:12 +00:00
|
|
|
w: 0,
|
|
|
|
h: 0
|
2017-07-04 12:23:58 +00:00
|
|
|
},
|
|
|
|
spriteSourceSize: {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
2018-04-23 22:40:12 +00:00
|
|
|
w: 0,
|
2018-07-06 11:22:05 +00:00
|
|
|
h: 0,
|
|
|
|
r: 0,
|
|
|
|
b: 0
|
2017-07-04 12:23:58 +00:00
|
|
|
},
|
2018-04-23 22:40:12 +00:00
|
|
|
radius: 0,
|
2017-07-04 12:23:58 +00:00
|
|
|
drawImage: {
|
2018-07-04 16:01:29 +00:00
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 0,
|
|
|
|
height: 0
|
2017-07-04 12:23:58 +00:00
|
|
|
}
|
|
|
|
};
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-04-23 22:40:12 +00:00
|
|
|
this.setSize(width, height, x, y);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the width, height, x and y of this Frame.
|
|
|
|
*
|
|
|
|
* This is called automatically by the constructor
|
|
|
|
* and should rarely be changed on-the-fly.
|
|
|
|
*
|
|
|
|
* @method Phaser.Textures.Frame#setSize
|
2018-05-04 17:51:02 +00:00
|
|
|
* @since 3.7.0
|
2018-04-23 22:40:12 +00:00
|
|
|
*
|
|
|
|
* @param {integer} width - The width of the frame before being trimmed.
|
|
|
|
* @param {integer} height - The height of the frame before being trimmed.
|
|
|
|
* @param {integer} [x=0] - The x coordinate of the top-left of this Frame.
|
|
|
|
* @param {integer} [y=0] - The y coordinate of the top-left of this Frame.
|
|
|
|
*
|
|
|
|
* @return {Phaser.Textures.Frame} This Frame object.
|
|
|
|
*/
|
|
|
|
setSize: function (width, height, x, y)
|
|
|
|
{
|
|
|
|
if (x === undefined) { x = 0; }
|
|
|
|
if (y === undefined) { y = 0; }
|
|
|
|
|
|
|
|
this.cutX = x;
|
|
|
|
this.cutY = y;
|
|
|
|
this.cutWidth = width;
|
|
|
|
this.cutHeight = height;
|
|
|
|
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
|
|
|
|
this.halfWidth = Math.floor(width * 0.5);
|
|
|
|
this.halfHeight = Math.floor(height * 0.5);
|
|
|
|
|
|
|
|
this.centerX = Math.floor(width / 2);
|
|
|
|
this.centerY = Math.floor(height / 2);
|
|
|
|
|
|
|
|
var data = this.data;
|
|
|
|
var cut = data.cut;
|
|
|
|
|
|
|
|
cut.x = x;
|
|
|
|
cut.y = y;
|
|
|
|
cut.w = width;
|
|
|
|
cut.h = height;
|
|
|
|
cut.r = x + width;
|
|
|
|
cut.b = y + height;
|
|
|
|
|
|
|
|
data.sourceSize.w = width;
|
|
|
|
data.sourceSize.h = height;
|
|
|
|
|
|
|
|
data.spriteSourceSize.w = width;
|
|
|
|
data.spriteSourceSize.h = height;
|
|
|
|
|
|
|
|
data.radius = 0.5 * Math.sqrt(width * width + height * height);
|
|
|
|
|
|
|
|
var drawImage = data.drawImage;
|
|
|
|
|
2018-07-04 16:01:29 +00:00
|
|
|
drawImage.x = x;
|
|
|
|
drawImage.y = y;
|
|
|
|
drawImage.width = width;
|
|
|
|
drawImage.height = height;
|
2018-04-23 22:40:12 +00:00
|
|
|
|
|
|
|
return this.updateUVs();
|
2017-07-04 12:23:58 +00:00
|
|
|
},
|
2016-12-06 16:18:28 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* If the frame was trimmed when added to the Texture Atlas, this records the trim and source data.
|
|
|
|
*
|
|
|
|
* @method Phaser.Textures.Frame#setTrim
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} actualWidth - The width of the frame before being trimmed.
|
|
|
|
* @param {number} actualHeight - The height of the frame before being trimmed.
|
|
|
|
* @param {number} destX - The destination X position of the trimmed frame for display.
|
|
|
|
* @param {number} destY - The destination Y position of the trimmed frame for display.
|
|
|
|
* @param {number} destWidth - The destination width of the trimmed frame for display.
|
|
|
|
* @param {number} destHeight - The destination height of the trimmed frame for display.
|
|
|
|
*
|
|
|
|
* @return {Phaser.Textures.Frame} This Frame object.
|
|
|
|
*/
|
2016-12-06 16:18:28 +00:00
|
|
|
setTrim: function (actualWidth, actualHeight, destX, destY, destWidth, destHeight)
|
|
|
|
{
|
2017-01-16 22:43:46 +00:00
|
|
|
var data = this.data;
|
|
|
|
var ss = data.spriteSourceSize;
|
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
// Store actual values
|
|
|
|
|
2017-01-16 22:43:46 +00:00
|
|
|
data.trim = true;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2017-01-16 22:43:46 +00:00
|
|
|
data.sourceSize.w = actualWidth;
|
|
|
|
data.sourceSize.h = actualHeight;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2017-01-16 22:43:46 +00:00
|
|
|
ss.x = destX;
|
|
|
|
ss.y = destY;
|
|
|
|
ss.w = destWidth;
|
|
|
|
ss.h = destHeight;
|
2018-07-06 11:22:05 +00:00
|
|
|
ss.r = destX + destWidth;
|
|
|
|
ss.b = destY + destHeight;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
|
|
|
// Adjust properties
|
|
|
|
this.x = destX;
|
|
|
|
this.y = destY;
|
2017-03-02 02:05:59 +00:00
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
this.width = destWidth;
|
|
|
|
this.height = destHeight;
|
|
|
|
|
2017-10-16 21:16:33 +00:00
|
|
|
this.halfWidth = destWidth * 0.5;
|
|
|
|
this.halfHeight = destHeight * 0.5;
|
|
|
|
|
2017-03-02 02:05:59 +00:00
|
|
|
this.centerX = Math.floor(destWidth / 2);
|
|
|
|
this.centerY = Math.floor(destHeight / 2);
|
|
|
|
|
2017-11-03 10:27:59 +00:00
|
|
|
return this.updateUVs();
|
2016-12-06 16:18:28 +00:00
|
|
|
},
|
|
|
|
|
2018-07-03 15:47:53 +00:00
|
|
|
/**
|
|
|
|
* Takes a crop data object and, based on the rectangular region given, calculates the
|
|
|
|
* required UV coordinates in order to crop this Frame for WebGL and Canvas rendering.
|
|
|
|
*
|
|
|
|
* This is called directly by the Game Object Texture Components `setCrop` method.
|
|
|
|
* Please use that method to crop a Game Object.
|
|
|
|
*
|
2018-07-04 14:18:08 +00:00
|
|
|
* @method Phaser.Textures.Frame#setCropUVs
|
2018-07-03 15:47:53 +00:00
|
|
|
* @since 3.11.0
|
|
|
|
*
|
|
|
|
* @param {object} crop - The crop data object. This is the `GameObject._crop` property.
|
|
|
|
* @param {number} x - The x coordinate to start the crop from. Cannot be negative or exceed the Frame width.
|
|
|
|
* @param {number} y - The y coordinate to start the crop from. Cannot be negative or exceed the Frame height.
|
|
|
|
* @param {number} width - The width of the crop rectangle. Cannot exceed the Frame width.
|
|
|
|
* @param {number} height - The height of the crop rectangle. Cannot exceed the Frame height.
|
2018-07-04 14:18:08 +00:00
|
|
|
* @param {boolean} flipX - Does the parent Game Object have flipX set?
|
|
|
|
* @param {boolean} flipY - Does the parent Game Object have flipY set?
|
2018-07-03 15:47:53 +00:00
|
|
|
*
|
|
|
|
* @return {object} The updated crop data object.
|
|
|
|
*/
|
2018-07-04 14:18:08 +00:00
|
|
|
setCropUVs: function (crop, x, y, width, height, flipX, flipY)
|
2018-07-03 15:47:53 +00:00
|
|
|
{
|
|
|
|
// Clamp the input values
|
|
|
|
|
2018-07-04 16:01:29 +00:00
|
|
|
var cx = this.cutX;
|
|
|
|
var cy = this.cutY;
|
|
|
|
var cw = this.cutWidth;
|
|
|
|
var ch = this.cutHeight;
|
2018-07-05 22:55:12 +00:00
|
|
|
var rw = this.realWidth;
|
|
|
|
var rh = this.realHeight;
|
2018-07-03 15:47:53 +00:00
|
|
|
|
2018-07-05 22:55:12 +00:00
|
|
|
x = Clamp(x, 0, rw);
|
|
|
|
y = Clamp(y, 0, rh);
|
|
|
|
|
|
|
|
width = Clamp(width, 0, rw - x);
|
|
|
|
height = Clamp(height, 0, rh - y);
|
2018-07-04 16:01:29 +00:00
|
|
|
|
2018-07-05 22:01:26 +00:00
|
|
|
var ox = cx + x;
|
|
|
|
var oy = cy + y;
|
2018-07-06 11:14:25 +00:00
|
|
|
var ow = width;
|
|
|
|
var oh = height;
|
2018-07-04 14:18:08 +00:00
|
|
|
|
2018-07-06 11:14:25 +00:00
|
|
|
var data = this.data;
|
2018-07-05 22:55:12 +00:00
|
|
|
|
2018-07-06 11:14:25 +00:00
|
|
|
if (data.trim)
|
2018-07-05 22:55:12 +00:00
|
|
|
{
|
2018-07-06 11:14:25 +00:00
|
|
|
var ss = data.spriteSourceSize;
|
|
|
|
|
2018-07-06 11:33:39 +00:00
|
|
|
// Need to check for intersection between the cut area and the crop area
|
|
|
|
// If there is none, we set UV to be empty, otherwise set it to be the intersection area
|
2018-07-05 22:55:12 +00:00
|
|
|
|
2018-09-27 15:49:52 +00:00
|
|
|
width = Clamp(width, 0, cw - x);
|
|
|
|
height = Clamp(height, 0, ch - y);
|
|
|
|
|
2018-07-06 11:22:05 +00:00
|
|
|
var cropRight = x + width;
|
|
|
|
var cropBottom = y + height;
|
2018-07-06 11:14:25 +00:00
|
|
|
|
2018-07-06 11:22:05 +00:00
|
|
|
var intersects = !(ss.r < x || ss.b < y || ss.x > cropRight || ss.y > cropBottom);
|
2018-07-06 11:14:25 +00:00
|
|
|
|
|
|
|
if (intersects)
|
|
|
|
{
|
2018-07-06 11:22:05 +00:00
|
|
|
var ix = Math.max(ss.x, x);
|
|
|
|
var iy = Math.max(ss.y, y);
|
|
|
|
var iw = Math.min(ss.r, cropRight) - ix;
|
|
|
|
var ih = Math.min(ss.b, cropBottom) - iy;
|
|
|
|
|
|
|
|
ow = iw;
|
|
|
|
oh = ih;
|
2018-07-05 22:55:12 +00:00
|
|
|
|
2018-07-06 11:33:39 +00:00
|
|
|
if (flipX)
|
|
|
|
{
|
|
|
|
ox = cx + (cw - (ix - ss.x) - iw);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ox = cx + (ix - ss.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flipY)
|
|
|
|
{
|
|
|
|
oy = cy + (ch - (iy - ss.y) - ih);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
oy = cy + (iy - ss.y);
|
|
|
|
}
|
|
|
|
|
2018-07-06 11:22:05 +00:00
|
|
|
x = ix;
|
|
|
|
y = iy;
|
2018-07-06 11:14:25 +00:00
|
|
|
|
2018-07-06 11:22:05 +00:00
|
|
|
width = iw;
|
|
|
|
height = ih;
|
2018-07-06 11:14:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ox = 0;
|
|
|
|
oy = 0;
|
|
|
|
ow = 0;
|
|
|
|
oh = 0;
|
|
|
|
}
|
2018-07-05 22:55:12 +00:00
|
|
|
}
|
2018-07-06 11:33:39 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (flipX)
|
|
|
|
{
|
|
|
|
ox = cx + (cw - x - width);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flipY)
|
|
|
|
{
|
|
|
|
oy = cy + (ch - y - height);
|
|
|
|
}
|
|
|
|
}
|
2018-07-05 22:55:12 +00:00
|
|
|
|
2018-07-05 22:01:26 +00:00
|
|
|
var tw = this.source.width;
|
|
|
|
var th = this.source.height;
|
2018-07-04 16:01:29 +00:00
|
|
|
|
2018-07-05 22:01:26 +00:00
|
|
|
// Map the given coordinates into UV space, clamping to the 0-1 range.
|
2018-07-04 16:01:29 +00:00
|
|
|
|
2018-07-05 22:01:26 +00:00
|
|
|
crop.u0 = Math.max(0, ox / tw);
|
|
|
|
crop.v0 = Math.max(0, oy / th);
|
2018-07-06 11:14:25 +00:00
|
|
|
crop.u1 = Math.min(1, (ox + ow) / tw);
|
|
|
|
crop.v1 = Math.min(1, (oy + oh) / th);
|
2018-07-03 15:47:53 +00:00
|
|
|
|
|
|
|
crop.x = x;
|
|
|
|
crop.y = y;
|
2018-07-04 16:01:29 +00:00
|
|
|
|
2018-07-06 14:34:12 +00:00
|
|
|
crop.cx = ox;
|
|
|
|
crop.cy = oy;
|
|
|
|
crop.cw = ow;
|
|
|
|
crop.ch = oh;
|
|
|
|
|
2018-07-06 11:14:25 +00:00
|
|
|
crop.width = width;
|
|
|
|
crop.height = height;
|
2018-07-04 16:01:29 +00:00
|
|
|
|
2018-07-04 14:18:08 +00:00
|
|
|
crop.flipX = flipX;
|
|
|
|
crop.flipY = flipY;
|
2018-07-03 15:47:53 +00:00
|
|
|
|
|
|
|
return crop;
|
|
|
|
},
|
|
|
|
|
2018-07-04 14:18:08 +00:00
|
|
|
/**
|
|
|
|
* Takes a crop data object and recalculates the UVs based on the dimensions inside the crop object.
|
|
|
|
* Called automatically by `setFrame`.
|
|
|
|
*
|
|
|
|
* @method Phaser.Textures.Frame#updateCropUVs
|
|
|
|
* @since 3.11.0
|
|
|
|
*
|
|
|
|
* @param {object} crop - The crop data object. This is the `GameObject._crop` property.
|
|
|
|
* @param {boolean} flipX - Does the parent Game Object have flipX set?
|
|
|
|
* @param {boolean} flipY - Does the parent Game Object have flipY set?
|
|
|
|
*
|
|
|
|
* @return {object} The updated crop data object.
|
|
|
|
*/
|
|
|
|
updateCropUVs: function (crop, flipX, flipY)
|
|
|
|
{
|
|
|
|
return this.setCropUVs(crop, crop.x, crop.y, crop.width, crop.height, flipX, flipY);
|
|
|
|
},
|
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* Updates the internal WebGL UV cache and the drawImage cache.
|
|
|
|
*
|
|
|
|
* @method Phaser.Textures.Frame#updateUVs
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {Phaser.Textures.Frame} This Frame object.
|
|
|
|
*/
|
2016-12-06 16:18:28 +00:00
|
|
|
updateUVs: function ()
|
|
|
|
{
|
2018-01-05 17:27:39 +00:00
|
|
|
var cx = this.cutX;
|
|
|
|
var cy = this.cutY;
|
|
|
|
var cw = this.cutWidth;
|
|
|
|
var ch = this.cutHeight;
|
|
|
|
|
2017-11-03 13:16:21 +00:00
|
|
|
// Canvas data
|
|
|
|
|
|
|
|
var cd = this.data.drawImage;
|
|
|
|
|
2018-07-04 16:01:29 +00:00
|
|
|
cd.width = cw;
|
|
|
|
cd.height = ch;
|
2017-11-03 13:16:21 +00:00
|
|
|
|
|
|
|
// WebGL data
|
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
var tw = this.source.width;
|
|
|
|
var th = this.source.height;
|
2018-03-20 15:10:19 +00:00
|
|
|
|
2018-07-02 22:51:42 +00:00
|
|
|
this.u0 = cx / tw;
|
|
|
|
this.v0 = cy / th;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-07-02 22:51:42 +00:00
|
|
|
this.u1 = (cx + cw) / tw;
|
|
|
|
this.v1 = (cy + ch) / th;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* Updates the internal WebGL UV cache.
|
|
|
|
*
|
|
|
|
* @method Phaser.Textures.Frame#updateUVsInverted
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {Phaser.Textures.Frame} This Frame object.
|
|
|
|
*/
|
2016-12-06 16:18:28 +00:00
|
|
|
updateUVsInverted: function ()
|
|
|
|
{
|
|
|
|
var tw = this.source.width;
|
|
|
|
var th = this.source.height;
|
|
|
|
|
2018-07-02 22:51:42 +00:00
|
|
|
this.u0 = (this.cutX + this.cutHeight) / tw;
|
|
|
|
this.v0 = this.cutY / th;
|
2018-03-20 15:10:19 +00:00
|
|
|
|
2018-07-02 22:51:42 +00:00
|
|
|
this.u1 = this.cutX / tw;
|
|
|
|
this.v1 = (this.cutY + this.cutWidth) / th;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* Clones this Frame into a new Frame object.
|
|
|
|
*
|
|
|
|
* @method Phaser.Textures.Frame#clone
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {Phaser.Textures.Frame} A clone of this Frame.
|
|
|
|
*/
|
2016-12-06 16:18:28 +00:00
|
|
|
clone: function ()
|
|
|
|
{
|
2016-12-15 17:15:47 +00:00
|
|
|
var clone = new Frame(this.texture, this.name, this.sourceIndex);
|
2016-12-06 16:18:28 +00:00
|
|
|
|
|
|
|
clone.cutX = this.cutX;
|
|
|
|
clone.cutY = this.cutY;
|
|
|
|
clone.cutWidth = this.cutWidth;
|
|
|
|
clone.cutHeight = this.cutHeight;
|
|
|
|
|
|
|
|
clone.x = this.x;
|
|
|
|
clone.y = this.y;
|
2017-03-02 02:05:59 +00:00
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
clone.width = this.width;
|
|
|
|
clone.height = this.height;
|
|
|
|
|
2017-10-16 21:16:33 +00:00
|
|
|
clone.halfWidth = this.halfWidth;
|
|
|
|
clone.halfHeight = this.halfHeight;
|
|
|
|
|
2017-03-02 02:05:59 +00:00
|
|
|
clone.centerX = this.centerX;
|
|
|
|
clone.centerY = this.centerY;
|
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
clone.rotated = this.rotated;
|
|
|
|
|
2016-12-15 17:15:47 +00:00
|
|
|
clone.data = Extend(true, clone.data, this.data);
|
2016-12-06 16:18:28 +00:00
|
|
|
|
|
|
|
clone.updateUVs();
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2019-08-07 11:59:23 +00:00
|
|
|
* Destroys this Frame by nulling its reference to the parent Texture and and data objects.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.Frame#destroy
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2016-12-06 16:18:28 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
2018-01-31 03:38:10 +00:00
|
|
|
this.source = null;
|
2019-08-07 11:59:23 +00:00
|
|
|
this.texture = null;
|
|
|
|
this.glTexture = null;
|
|
|
|
this.customData = null;
|
|
|
|
this.data = null;
|
2017-07-04 12:23:58 +00:00
|
|
|
},
|
2016-12-06 16:18:28 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The width of the Frame in its un-trimmed, un-padded state, as prepared in the art package,
|
|
|
|
* before being packed.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#realWidth
|
|
|
|
* @type {number}
|
2018-10-09 12:40:00 +00:00
|
|
|
* @readonly
|
2018-02-08 04:01:44 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2016-12-06 16:18:28 +00:00
|
|
|
realWidth: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.data.sourceSize.w;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The height of the Frame in its un-trimmed, un-padded state, as prepared in the art package,
|
|
|
|
* before being packed.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.Frame#realHeight
|
|
|
|
* @type {number}
|
2018-10-09 12:40:00 +00:00
|
|
|
* @readonly
|
2018-02-08 04:01:44 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2016-12-06 16:18:28 +00:00
|
|
|
realHeight: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.data.sourceSize.h;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2017-01-26 04:06:10 +00:00
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The radius of the Frame (derived from sqrt(w * w + h * h) / 2)
|
2018-03-20 15:10:19 +00:00
|
|
|
*
|
2018-02-08 04:01:44 +00:00
|
|
|
* @name Phaser.Textures.Frame#radius
|
|
|
|
* @type {number}
|
2018-10-09 12:40:00 +00:00
|
|
|
* @readonly
|
2018-02-08 04:01:44 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-01-26 04:06:10 +00:00
|
|
|
radius: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.data.radius;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2017-04-18 14:31:30 +00:00
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* Is the Frame trimmed or not?
|
2018-03-20 15:10:19 +00:00
|
|
|
*
|
2018-02-08 04:01:44 +00:00
|
|
|
* @name Phaser.Textures.Frame#trimmed
|
|
|
|
* @type {boolean}
|
2018-10-09 12:40:00 +00:00
|
|
|
* @readonly
|
2018-02-08 04:01:44 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-04-18 14:31:30 +00:00
|
|
|
trimmed: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.data.trim;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2017-01-16 22:43:46 +00:00
|
|
|
/**
|
2018-02-08 04:01:44 +00:00
|
|
|
* The Canvas drawImage data object.
|
2018-03-20 15:10:19 +00:00
|
|
|
*
|
2018-02-08 04:01:44 +00:00
|
|
|
* @name Phaser.Textures.Frame#canvasData
|
|
|
|
* @type {object}
|
2018-10-09 12:40:00 +00:00
|
|
|
* @readonly
|
2018-02-08 04:01:44 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-01-16 22:43:46 +00:00
|
|
|
canvasData: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.data.drawImage;
|
|
|
|
}
|
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Frame;
|