From 23cd9868b4dc577e476487e6d163548b8ce7077e Mon Sep 17 00:00:00 2001 From: Felipe Alfonso Date: Wed, 11 Apr 2018 12:55:32 -0300 Subject: [PATCH] Added getWorldTransformMatrix to Container and added support to parent container transform to GetBounds component. --- src/gameobjects/components/GetBounds.js | 69 +++++++++++++++++++------ src/gameobjects/container/Container.js | 24 +++++++++ 2 files changed, 76 insertions(+), 17 deletions(-) diff --git a/src/gameobjects/components/GetBounds.js b/src/gameobjects/components/GetBounds.js index be4a90701..f22cf0866 100644 --- a/src/gameobjects/components/GetBounds.js +++ b/src/gameobjects/components/GetBounds.js @@ -20,6 +20,7 @@ var GetBounds = { /** * 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 * * @method Phaser.GameObjects.Components.GetBounds#getCenter * @since 3.0.0 @@ -42,6 +43,7 @@ var GetBounds = { /** * 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 * * @method Phaser.GameObjects.Components.GetBounds#getTopLeft * @since 3.0.0 @@ -69,6 +71,7 @@ var GetBounds = { /** * 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 * * @method Phaser.GameObjects.Components.GetBounds#getTopRight * @since 3.0.0 @@ -96,6 +99,7 @@ var GetBounds = { /** * 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 * * @method Phaser.GameObjects.Components.GetBounds#getBottomLeft * @since 3.0.0 @@ -123,6 +127,7 @@ var GetBounds = { /** * 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 * * @method Phaser.GameObjects.Components.GetBounds#getBottomRight * @since 3.0.0 @@ -167,36 +172,66 @@ var GetBounds = { // We can use the output object to temporarily store the x/y coords in: - this.getTopLeft(output); + var TLx, TLy, TRx, TRy, BLx, BLy, BRx, BRy; - var TLx = output.x; - var TLy = output.y; + // Instead of doing a check if parent container is + // defined per corner we only do it once. + if (this.parentContainer) + { + var parentMatrix = this.parentContainer.getWorldTransformMatrix(); - this.getTopRight(output); + this.getTopLeft(output); + parentMatrix.transformPoint(output.x, output.y, output); - var TRx = output.x; - var TRy = output.y; + TLx = output.x; + TLy = output.y; - this.getBottomLeft(output); + this.getTopRight(output); + parentMatrix.transformPoint(output.x, output.y, output); - var BLx = output.x; - var BLy = output.y; + TRx = output.x; + TRy = output.y; - this.getBottomRight(output); + this.getBottomLeft(output); + parentMatrix.transformPoint(output.x, output.y, output); - var BRx = output.x; - var BRy = output.y; + 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; - if (this.parentContainer) - { - // Clearly something needs to happen here :) - } - return output; } diff --git a/src/gameobjects/container/Container.js b/src/gameobjects/container/Container.js index 7e06c5c35..ac30a8ae9 100644 --- a/src/gameobjects/container/Container.js +++ b/src/gameobjects/container/Container.js @@ -329,6 +329,30 @@ var Container = new Class({ return output; }, + /** + * Returns the world transform matrix. + * The returned matrix is a temporal and shouldn't be stored. + * + * @method Phaser.GameObjects.Container#getWorldTransformMatrix + * @since 3.4.0 + * + * @return {Phaser.GameObjects.Components.TransformMatrix} The world transform matrix. + */ + getWorldTransformMatrix: function () + { + var tempMatrix = this.tempTransformMatrix; + + tempMatrix.applyITRS(this.x, this.y, this.rotation, this.scaleX, this.scaleY); + + if (this.parentContainer) + { + var parentMatrix = this.parentContainer.getTransformMatrix(); + tempMatrix.multiply(parentMatrix); + } + + return tempMatrix; + }, + /** * Adds the given Game Object, or array of Game Objects, to this Container. *