phaser/src/gameobjects/components/Origin.js

198 lines
5.6 KiB
JavaScript
Raw Normal View History

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}
*/
2018-02-01 00:04:45 +00:00
/**
* Provides methods used for getting and setting the origin of a Game Object.
* Values are normalized, given in the range 0 to 1.
* Display values contain the calculated pixel values.
* Should be applied as a mixin and not used directly.
2018-03-27 12:52:58 +00:00
*
2018-02-01 00:04:45 +00:00
* @name Phaser.GameObjects.Components.Origin
* @since 3.0.0
*/
var Origin = {
/**
* A property indicating that a Game Object has this component.
2018-03-27 12:52:58 +00:00
*
* @name Phaser.GameObjects.Components.Origin#_originComponent
* @type {boolean}
* @private
* @default true
* @since 3.2.0
*/
_originComponent: true,
2018-02-01 00:04:45 +00:00
/**
* The horizontal origin of this Game Object.
* The origin maps the relationship between the size and position of the Game Object.
* The default value is 0.5, meaning all Game Objects are positioned based on their center.
* Setting the value to 0 means the position now relates to the left of the Game Object.
2018-03-27 12:52:58 +00:00
*
2018-02-01 00:04:45 +00:00
* @name Phaser.GameObjects.Components.Origin#originX
* @type {float}
* @default 0.5
* @since 3.0.0
*/
originX: 0.5,
2018-02-01 00:04:45 +00:00
/**
* The vertical origin of this Game Object.
* The origin maps the relationship between the size and position of the Game Object.
* The default value is 0.5, meaning all Game Objects are positioned based on their center.
* Setting the value to 0 means the position now relates to the top of the Game Object.
2018-03-27 12:52:58 +00:00
*
2018-02-01 00:04:45 +00:00
* @name Phaser.GameObjects.Components.Origin#originY
* @type {float}
* @default 0.5
* @since 3.0.0
*/
originY: 0.5,
2018-02-01 01:36:52 +00:00
// private + read only
_displayOriginX: 0,
_displayOriginY: 0,
2018-02-01 00:04:45 +00:00
/**
* The horizontal display origin of this Game Object.
* The origin is a normalized value between 0 and 1.
* The displayOrigin is a pixel value, based on the size of the Game Object combined with the origin.
2018-03-27 12:52:58 +00:00
*
2018-02-01 00:04:45 +00:00
* @name Phaser.GameObjects.Components.Origin#displayOriginX
* @type {float}
* @since 3.0.0
*/
displayOriginX: {
get: function ()
{
return this._displayOriginX;
},
set: function (value)
{
this._displayOriginX = value;
this.originX = value / this.width;
}
},
2018-02-01 00:04:45 +00:00
/**
* The vertical display origin of this Game Object.
* The origin is a normalized value between 0 and 1.
* The displayOrigin is a pixel value, based on the size of the Game Object combined with the origin.
2018-03-27 12:52:58 +00:00
*
2018-02-01 00:04:45 +00:00
* @name Phaser.GameObjects.Components.Origin#displayOriginY
* @type {float}
* @since 3.0.0
*/
displayOriginY: {
get: function ()
{
return this._displayOriginY;
},
set: function (value)
{
this._displayOriginY = value;
this.originY = value / this.height;
}
},
2018-02-01 00:04:45 +00:00
/**
* Sets the origin of this Game Object.
*
* The values are given in the range 0 to 1.
2018-03-27 12:52:58 +00:00
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.Origin#setOrigin
2018-02-01 00:04:45 +00:00
* @since 3.0.0
*
* @param {number} [x=0.5] - The horizontal origin value.
2018-03-27 12:52:58 +00:00
* @param {number} [y=x] - The vertical origin value. If not defined it will be set to the value of `x`.
*
2018-02-01 00:04:45 +00:00
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
*/
setOrigin: function (x, y)
{
if (x === undefined) { x = 0.5; }
if (y === undefined) { y = x; }
this.originX = x;
this.originY = y;
return this.updateDisplayOrigin();
},
2018-02-09 15:21:49 +00:00
/**
* Sets the origin of this Game Object based on the Pivot values in its Frame.
2018-03-27 12:52:58 +00:00
*
2018-02-09 15:21:49 +00:00
* @method Phaser.GameObjects.Components.Origin#setOriginFromFrame
* @since 3.0.0
2018-03-27 12:52:58 +00:00
*
2018-02-09 15:21:49 +00:00
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
*/
setOriginFromFrame: function ()
{
if (!this.frame || !this.frame.customPivot)
{
return this.setOrigin();
}
else
{
this.originX = this.frame.pivotX;
this.originY = this.frame.pivotY;
}
return this.updateDisplayOrigin();
},
2018-02-01 00:04:45 +00:00
/**
* Sets the display origin of this Game Object.
* The difference between this and setting the origin is that you can use pixel values for setting the display origin.
2018-03-27 12:52:58 +00:00
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.Origin#setDisplayOrigin
2018-02-01 00:04:45 +00:00
* @since 3.0.0
*
* @param {number} [x=0] - The horizontal display origin value.
2018-03-27 12:52:58 +00:00
* @param {number} [y=x] - The vertical display origin value. If not defined it will be set to the value of `x`.
*
2018-02-01 00:04:45 +00:00
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
*/
setDisplayOrigin: function (x, y)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = x; }
this.displayOriginX = x;
this.displayOriginY = y;
2017-08-17 05:26:25 +00:00
return this;
},
2018-02-01 00:04:45 +00:00
/**
* Updates the Display Origin cached values internally stored on this Game Object.
* You don't usually call this directly, but it is exposed for edge-cases where you may.
2018-03-27 12:52:58 +00:00
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.Origin#updateDisplayOrigin
2018-02-01 00:04:45 +00:00
* @since 3.0.0
2018-03-27 12:52:58 +00:00
*
2018-02-01 00:04:45 +00:00
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
*/
updateDisplayOrigin: function ()
2017-08-17 05:26:25 +00:00
{
2017-10-11 11:05:09 +00:00
this._displayOriginX = Math.round(this.originX * this.width);
this._displayOriginY = Math.round(this.originY * this.height);
2017-08-17 05:26:25 +00:00
return this;
}
};
module.exports = Origin;