phaser/src/gameobjects/components/GetBounds.js

354 lines
14 KiB
JavaScript
Raw Normal View History

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
*/
var Rectangle = require('../../geom/rectangle/Rectangle');
var RotateAround = require('../../math/RotateAround');
var Vector2 = require('../../math/Vector2');
2018-02-01 00:04:45 +00:00
/**
* Provides methods used for obtaining the bounds of a Game Object.
* Should be applied as a mixin and not used directly.
2018-03-20 14:56:31 +00:00
*
2019-02-12 12:48:41 +00:00
* @namespace Phaser.GameObjects.Components.GetBounds
2018-02-01 00:04:45 +00:00
* @since 3.0.0
*/
var GetBounds = {
2016-12-07 01:40:56 +00:00
/**
* Processes the bounds output vector before returning it.
*
* @method Phaser.GameObjects.Components.GetBounds#prepareBoundsOutput
* @private
* @since 3.18.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {(Phaser.Math.Vector2|object)} output - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
*/
prepareBoundsOutput: function (output, includeParent)
{
if (includeParent === undefined) { includeParent = false; }
if (this.rotation !== 0)
{
RotateAround(output, this.x, this.y, this.rotation);
}
if (includeParent && this.parentContainer)
{
var parentMatrix = this.parentContainer.getBoundsTransformMatrix();
parentMatrix.transformPoint(output.x, output.y, output);
}
return output;
},
2018-02-01 00:04:45 +00:00
/**
* Gets the center coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
2018-03-20 14:56:31 +00:00
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.GetBounds#getCenter
2018-02-01 00:04:45 +00:00
* @since 3.0.0
*
2018-03-27 12:52:58 +00:00
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
2018-03-20 14:56:31 +00:00
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
2018-02-01 00:04:45 +00:00
*/
getCenter: function (output)
{
if (output === undefined) { output = new Vector2(); }
output.x = this.x - (this.displayWidth * this.originX) + (this.displayWidth / 2);
output.y = this.y - (this.displayHeight * this.originY) + (this.displayHeight / 2);
return output;
},
2018-02-01 00:04:45 +00:00
/**
* Gets the top-left corner coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
2018-03-20 14:56:31 +00:00
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.GetBounds#getTopLeft
2018-02-01 00:04:45 +00:00
* @since 3.0.0
*
2018-03-27 12:52:58 +00:00
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
2018-03-20 14:56:31 +00:00
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
2018-03-20 14:56:31 +00:00
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
2018-02-01 00:04:45 +00:00
*/
getTopLeft: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = this.x - (this.displayWidth * this.originX);
output.y = this.y - (this.displayHeight * this.originY);
return this.prepareBoundsOutput(output, includeParent);
},
/**
* Gets the top-center coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
*
* @method Phaser.GameObjects.Components.GetBounds#getTopCenter
* @since 3.18.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
*/
getTopCenter: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = (this.x - (this.displayWidth * this.originX)) + (this.displayWidth / 2);
output.y = this.y - (this.displayHeight * this.originY);
return this.prepareBoundsOutput(output, includeParent);
},
2018-02-01 00:04:45 +00:00
/**
* Gets the top-right corner coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
2018-03-20 14:56:31 +00:00
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.GetBounds#getTopRight
2018-02-01 00:04:45 +00:00
* @since 3.0.0
*
2018-03-27 12:52:58 +00:00
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
2018-03-20 14:56:31 +00:00
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
2018-03-20 14:56:31 +00:00
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
2018-02-01 00:04:45 +00:00
*/
getTopRight: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
output.y = this.y - (this.displayHeight * this.originY);
return this.prepareBoundsOutput(output, includeParent);
},
/**
* Gets the left-center coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
*
* @method Phaser.GameObjects.Components.GetBounds#getLeftCenter
* @since 3.18.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
*/
getLeftCenter: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = this.x - (this.displayWidth * this.originX);
output.y = (this.y - (this.displayHeight * this.originY)) + (this.displayHeight / 2);
return this.prepareBoundsOutput(output, includeParent);
},
/**
* Gets the right-center coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
*
* @method Phaser.GameObjects.Components.GetBounds#getRightCenter
* @since 3.18.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
*/
getRightCenter: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
output.y = (this.y - (this.displayHeight * this.originY)) + (this.displayHeight / 2);
return this.prepareBoundsOutput(output, includeParent);
},
2018-02-01 00:04:45 +00:00
/**
* Gets the bottom-left corner coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
2018-03-20 14:56:31 +00:00
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.GetBounds#getBottomLeft
2018-02-01 00:04:45 +00:00
* @since 3.0.0
*
2018-03-27 12:52:58 +00:00
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
2018-03-20 14:56:31 +00:00
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
2018-03-20 14:56:31 +00:00
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
2018-02-01 00:04:45 +00:00
*/
getBottomLeft: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = this.x - (this.displayWidth * this.originX);
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
return this.prepareBoundsOutput(output, includeParent);
},
/**
* Gets the bottom-center coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
*
* @method Phaser.GameObjects.Components.GetBounds#getBottomCenter
* @since 3.18.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
*/
getBottomCenter: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = (this.x - (this.displayWidth * this.originX)) + (this.displayWidth / 2);
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
return this.prepareBoundsOutput(output, includeParent);
},
2018-02-01 00:04:45 +00:00
/**
* Gets the bottom-right corner coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
2018-03-20 14:56:31 +00:00
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.GetBounds#getBottomRight
2018-02-01 00:04:45 +00:00
* @since 3.0.0
*
2018-03-27 12:52:58 +00:00
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
2018-03-20 14:56:31 +00:00
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
2018-03-20 14:56:31 +00:00
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
2018-02-01 00:04:45 +00:00
*/
getBottomRight: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
return this.prepareBoundsOutput(output, includeParent);
},
2018-02-01 00:04:45 +00:00
/**
* Gets the bounds of this Game Object, regardless of origin.
* The values are stored and returned in a Rectangle, or Rectangle-like, object.
2018-03-20 14:56:31 +00:00
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.GetBounds#getBounds
2018-02-01 00:04:45 +00:00
* @since 3.0.0
*
* @generic {Phaser.Geom.Rectangle} O - [output,$return]
2018-03-27 12:52:58 +00:00
*
2018-03-20 14:56:31 +00:00
* @param {(Phaser.Geom.Rectangle|object)} [output] - An object to store the values in. If not provided a new Rectangle will be created.
*
* @return {(Phaser.Geom.Rectangle|object)} The values stored in the output object.
2018-02-01 00:04:45 +00:00
*/
getBounds: function (output)
2016-12-07 01:40:56 +00:00
{
if (output === undefined) { output = new Rectangle(); }
2016-12-07 01:40:56 +00:00
// We can use the output object to temporarily store the x/y coords in:
var TLx, TLy, TRx, TRy, BLx, BLy, BRx, BRy;
// Instead of doing a check if parent container is
// defined per corner we only do it once.
if (this.parentContainer)
{
2018-04-12 13:25:46 +00:00
var parentMatrix = this.parentContainer.getBoundsTransformMatrix();
this.getTopLeft(output);
parentMatrix.transformPoint(output.x, output.y, output);
TLx = output.x;
TLy = output.y;
this.getTopRight(output);
parentMatrix.transformPoint(output.x, output.y, output);
TRx = output.x;
TRy = output.y;
this.getBottomLeft(output);
parentMatrix.transformPoint(output.x, output.y, output);
BLx = output.x;
BLy = output.y;
this.getBottomRight(output);
parentMatrix.transformPoint(output.x, output.y, output);
BRx = output.x;
BRy = output.y;
}
else
{
this.getTopLeft(output);
TLx = output.x;
TLy = output.y;
this.getTopRight(output);
TRx = output.x;
TRy = output.y;
this.getBottomLeft(output);
BLx = output.x;
BLy = output.y;
this.getBottomRight(output);
BRx = output.x;
BRy = output.y;
}
output.x = Math.min(TLx, TRx, BLx, BRx);
output.y = Math.min(TLy, TRy, BLy, BRy);
output.width = Math.max(TLx, TRx, BLx, BRx) - output.x;
output.height = Math.max(TLy, TRy, BLy, BRy) - output.y;
return output;
}
2018-02-01 01:36:52 +00:00
2016-12-07 01:40:56 +00:00
};
module.exports = GetBounds;