Skip to content

Commit 1802f8b

Browse files
committed
GetBounds getTopLeft, getTopRight, getBottomLeft and getBottomRight all have a new optional argument includeParent which will factor in all ancestor transforms to the returned point.
1 parent 2e722b0 commit 1802f8b

2 files changed

Lines changed: 45 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* TransformMatrix.destroy is a new method that will clear out the array and object used by a Matrix internally.
4848
* BaseSound, and by extension WebAudioSound and HTMLAudioSound, will now emit a `destroy` event when they are destroyed (thanks @rexrainbow)
4949
* A new property was added to the Scene config: `mapAdd` which is used to extend the default injection map of a scene instead of overwriting it (thanks @sebashwa)
50+
* GetBounds `getTopLeft`, `getTopRight`, `getBottomLeft` and `getBottomRight` all have a new optional argument `includeParent` which will factor in all ancestor transforms to the returned point.
5051

5152
### Bug Fixes
5253

src/gameobjects/components/GetBounds.js

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ var GetBounds = {
5151
* @generic {Phaser.Math.Vector2} O - [output,$return]
5252
*
5353
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
54+
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
5455
*
5556
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
5657
*/
57-
getTopLeft: function (output)
58+
getTopLeft: function (output, includeParent)
5859
{
59-
if (output === undefined) { output = new Vector2(); }
60+
if (!output) { output = new Vector2(); }
61+
if (includeParent === undefined) { includeParent = false; }
6062

6163
output.x = this.x - (this.displayWidth * this.originX);
6264
output.y = this.y - (this.displayHeight * this.originY);
@@ -66,6 +68,13 @@ var GetBounds = {
6668
RotateAround(output, this.x, this.y, this.rotation);
6769
}
6870

71+
if (includeParent && this.parentContainer)
72+
{
73+
var parentMatrix = this.parentContainer.getBoundsTransformMatrix();
74+
75+
parentMatrix.transformPoint(output.x, output.y, output);
76+
}
77+
6978
return output;
7079
},
7180

@@ -79,12 +88,14 @@ var GetBounds = {
7988
* @generic {Phaser.Math.Vector2} O - [output,$return]
8089
*
8190
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
91+
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
8292
*
8393
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
8494
*/
85-
getTopRight: function (output)
95+
getTopRight: function (output, includeParent)
8696
{
87-
if (output === undefined) { output = new Vector2(); }
97+
if (!output) { output = new Vector2(); }
98+
if (includeParent === undefined) { includeParent = false; }
8899

89100
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
90101
output.y = this.y - (this.displayHeight * this.originY);
@@ -94,6 +105,13 @@ var GetBounds = {
94105
RotateAround(output, this.x, this.y, this.rotation);
95106
}
96107

108+
if (includeParent && this.parentContainer)
109+
{
110+
var parentMatrix = this.parentContainer.getBoundsTransformMatrix();
111+
112+
parentMatrix.transformPoint(output.x, output.y, output);
113+
}
114+
97115
return output;
98116
},
99117

@@ -107,12 +125,14 @@ var GetBounds = {
107125
* @generic {Phaser.Math.Vector2} O - [output,$return]
108126
*
109127
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
128+
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
110129
*
111130
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
112131
*/
113-
getBottomLeft: function (output)
132+
getBottomLeft: function (output, includeParent)
114133
{
115-
if (output === undefined) { output = new Vector2(); }
134+
if (!output) { output = new Vector2(); }
135+
if (includeParent === undefined) { includeParent = false; }
116136

117137
output.x = this.x - (this.displayWidth * this.originX);
118138
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
@@ -122,6 +142,13 @@ var GetBounds = {
122142
RotateAround(output, this.x, this.y, this.rotation);
123143
}
124144

145+
if (includeParent && this.parentContainer)
146+
{
147+
var parentMatrix = this.parentContainer.getBoundsTransformMatrix();
148+
149+
parentMatrix.transformPoint(output.x, output.y, output);
150+
}
151+
125152
return output;
126153
},
127154

@@ -135,12 +162,14 @@ var GetBounds = {
135162
* @generic {Phaser.Math.Vector2} O - [output,$return]
136163
*
137164
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
165+
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
138166
*
139167
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
140168
*/
141-
getBottomRight: function (output)
169+
getBottomRight: function (output, includeParent)
142170
{
143-
if (output === undefined) { output = new Vector2(); }
171+
if (!output) { output = new Vector2(); }
172+
if (includeParent === undefined) { includeParent = false; }
144173

145174
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
146175
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
@@ -150,6 +179,13 @@ var GetBounds = {
150179
RotateAround(output, this.x, this.y, this.rotation);
151180
}
152181

182+
if (includeParent && this.parentContainer)
183+
{
184+
var parentMatrix = this.parentContainer.getBoundsTransformMatrix();
185+
186+
parentMatrix.transformPoint(output.x, output.y, output);
187+
}
188+
153189
return output;
154190
},
155191

0 commit comments

Comments
 (0)