forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBounds.js
More file actions
executable file
·399 lines (307 loc) · 13 KB
/
Copy pathBounds.js
File metadata and controls
executable file
·399 lines (307 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Bounds component contains properties related to the bounds of the Game Object.
*
* @class
*/
Phaser.Component.Bounds = function () {};
Phaser.Component.Bounds.prototype = {
/**
* The amount the Game Object is visually offset from its x coordinate.
* This is the same as `width * anchor.x`.
* It will only be > 0 if anchor.x is not equal to zero.
*
* @property {number} offsetX
* @readOnly
*/
offsetX: {
get: function () {
return this.anchor.x * this.width;
}
},
/**
* The amount the Game Object is visually offset from its y coordinate.
* This is the same as `height * anchor.y`.
* It will only be > 0 if anchor.y is not equal to zero.
*
* @property {number} offsetY
* @readOnly
*/
offsetY: {
get: function () {
return this.anchor.y * this.height;
}
},
/**
* The center x coordinate of the Game Object.
* This is the same as `(x - offsetX) + (width / 2)`.
*
* @property {number} centerX
*/
centerX: {
get: function () {
return (this.x - this.offsetX) + (this.width * 0.5);
},
set: function (value) {
this.x = (value + this.offsetX) - (this.width * 0.5);
}
},
/**
* The center y coordinate of the Game Object.
* This is the same as `(y - offsetY) + (height / 2)`.
*
* @property {number} centerY
*/
centerY: {
get: function () {
return (this.y - this.offsetY) + (this.height * 0.5);
},
set: function (value) {
this.y = (value + this.offsetY) - (this.height * 0.5);
}
},
/**
* The left coordinate of the Game Object.
* This is the same as `x - offsetX`.
*
* @property {number} left
*/
left: {
get: function () {
return this.x - this.offsetX;
},
set: function (value) {
this.x = value + this.offsetX;
}
},
/**
* The right coordinate of the Game Object.
* This is the same as `x + width - offsetX`.
*
* @property {number} right
*/
right: {
get: function () {
return (this.x + this.width) - this.offsetX;
},
set: function (value) {
this.x = value - (this.width) + this.offsetX;
}
},
/**
* The y coordinate of the Game Object.
* This is the same as `y - offsetY`.
*
* @property {number} top
*/
top: {
get: function () {
return this.y - this.offsetY;
},
set: function (value) {
this.y = value + this.offsetY;
}
},
/**
* The sum of the y and height properties.
* This is the same as `y + height - offsetY`.
*
* @property {number} bottom
*/
bottom: {
get: function () {
return (this.y + this.height) - this.offsetY;
},
set: function (value) {
this.y = value - (this.height) + this.offsetY;
}
},
/**
* Aligns this Game Object within another Game Object, or Rectangle, known as the
* 'container', to one of 9 possible positions.
*
* The container must be a Game Object, or Phaser.Rectangle object. This can include properties
* such as `World.bounds` or `Camera.view`, for aligning Game Objects within the world
* and camera bounds. Or it can include other Sprites, Images, Text objects, BitmapText,
* TileSprites or Buttons.
*
* Please note that aligning a Sprite to another Game Object does **not** make it a child of
* the container. It simply modifies its position coordinates so it aligns with it.
*
* The position constants you can use are:
*
* `Phaser.TOP_LEFT`, `Phaser.TOP_CENTER`, `Phaser.TOP_RIGHT`, `Phaser.LEFT_CENTER`,
* `Phaser.CENTER`, `Phaser.RIGHT_CENTER`, `Phaser.BOTTOM_LEFT`,
* `Phaser.BOTTOM_CENTER` and `Phaser.BOTTOM_RIGHT`.
*
* The Game Objects are placed in such a way that their _bounds_ align with the
* container, taking into consideration rotation, scale and the anchor property.
* This allows you to neatly align Game Objects, irrespective of their position value.
*
* The optional `offsetX` and `offsetY` arguments allow you to apply extra spacing to the final
* aligned position of the Game Object. For example:
*
* `sprite.alignIn(background, Phaser.BOTTOM_RIGHT, -20, -20)`
*
* Would align the `sprite` to the bottom-right, but moved 20 pixels in from the corner.
* Think of the offsets as applying an adjustment to the containers bounds before the alignment takes place.
* So providing a negative offset will 'shrink' the container bounds by that amount, and providing a positive
* one expands it.
*
* @method
* @param {Phaser.Rectangle|Phaser.Sprite|Phaser.Image|Phaser.Text|Phaser.BitmapText|Phaser.Button|Phaser.Graphics|Phaser.TileSprite} container - The Game Object or Rectangle with which to align this Game Object to. Can also include properties such as `World.bounds` or `Camera.view`.
* @param {integer} [position] - The position constant. One of `Phaser.TOP_LEFT` (default), `Phaser.TOP_CENTER`, `Phaser.TOP_RIGHT`, `Phaser.LEFT_CENTER`, `Phaser.CENTER`, `Phaser.RIGHT_CENTER`, `Phaser.BOTTOM_LEFT`, `Phaser.BOTTOM_CENTER` or `Phaser.BOTTOM_RIGHT`.
* @param {integer} [offsetX=0] - A horizontal adjustment of the Containers bounds, applied to the aligned position of the Game Object. Use a negative value to shrink the bounds, positive to increase it.
* @param {integer} [offsetY=0] - A vertical adjustment of the Containers bounds, applied to the aligned position of the Game Object. Use a negative value to shrink the bounds, positive to increase it.
* @return {Object} This Game Object.
*/
alignIn: function (container, position, offsetX, offsetY) {
if (offsetX === undefined) { offsetX = 0; }
if (offsetY === undefined) { offsetY = 0; }
switch (position)
{
default:
case Phaser.TOP_LEFT:
this.left = container.left - offsetX;
this.top = container.top - offsetY;
break;
case Phaser.TOP_CENTER:
this.centerX = container.centerX + offsetX;
this.top = container.top - offsetY;
break;
case Phaser.TOP_RIGHT:
this.right = container.right + offsetX;
this.top = container.top - offsetY;
break;
case Phaser.LEFT_CENTER:
this.left = container.left - offsetX;
this.centerY = container.centerY + offsetY;
break;
case Phaser.CENTER:
this.centerX = container.centerX + offsetX;
this.centerY = container.centerY + offsetY;
break;
case Phaser.RIGHT_CENTER:
this.right = container.right + offsetX;
this.centerY = container.centerY + offsetY;
break;
case Phaser.BOTTOM_LEFT:
this.left = container.left - offsetX;
this.bottom = container.bottom + offsetY;
break;
case Phaser.BOTTOM_CENTER:
this.centerX = container.centerX + offsetX;
this.bottom = container.bottom + offsetY;
break;
case Phaser.BOTTOM_RIGHT:
this.right = container.right + offsetX;
this.bottom = container.bottom + offsetY;
break;
}
return this;
},
/**
* Aligns this Game Object to the side of another Game Object, or Rectangle, known as the
* 'parent', in one of 11 possible positions.
*
* The parent must be a Game Object, or Phaser.Rectangle object. This can include properties
* such as `World.bounds` or `Camera.view`, for aligning Game Objects within the world
* and camera bounds. Or it can include other Sprites, Images, Text objects, BitmapText,
* TileSprites or Buttons.
*
* Please note that aligning a Sprite to another Game Object does **not** make it a child of
* the parent. It simply modifies its position coordinates so it aligns with it.
*
* The position constants you can use are:
*
* `Phaser.TOP_LEFT` (default), `Phaser.TOP_CENTER`, `Phaser.TOP_RIGHT`, `Phaser.LEFT_TOP`,
* `Phaser.LEFT_CENTER`, `Phaser.LEFT_BOTTOM`, `Phaser.RIGHT_TOP`, `Phaser.RIGHT_CENTER`,
* `Phaser.RIGHT_BOTTOM`, `Phaser.BOTTOM_LEFT`, `Phaser.BOTTOM_CENTER`
* and `Phaser.BOTTOM_RIGHT`.
*
* The Game Objects are placed in such a way that their _bounds_ align with the
* parent, taking into consideration rotation, scale and the anchor property.
* This allows you to neatly align Game Objects, irrespective of their position value.
*
* The optional `offsetX` and `offsetY` arguments allow you to apply extra spacing to the final
* aligned position of the Game Object. For example:
*
* `sprite.alignTo(background, Phaser.BOTTOM_RIGHT, -20, -20)`
*
* Would align the `sprite` to the bottom-right, but moved 20 pixels in from the corner.
* Think of the offsets as applying an adjustment to the parents bounds before the alignment takes place.
* So providing a negative offset will 'shrink' the parent bounds by that amount, and providing a positive
* one expands it.
*
* @method
* @param {Phaser.Rectangle|Phaser.Sprite|Phaser.Image|Phaser.Text|Phaser.BitmapText|Phaser.Button|Phaser.Graphics|Phaser.TileSprite} parent - The Game Object or Rectangle with which to align this Game Object to. Can also include properties such as `World.bounds` or `Camera.view`.
* @param {integer} [position] - The position constant. One of `Phaser.TOP_LEFT`, `Phaser.TOP_CENTER`, `Phaser.TOP_RIGHT`, `Phaser.LEFT_TOP`, `Phaser.LEFT_CENTER`, `Phaser.LEFT_BOTTOM`, `Phaser.RIGHT_TOP`, `Phaser.RIGHT_CENTER`, `Phaser.RIGHT_BOTTOM`, `Phaser.BOTTOM_LEFT`, `Phaser.BOTTOM_CENTER` or `Phaser.BOTTOM_RIGHT`.
* @param {integer} [offsetX=0] - A horizontal adjustment of the Containers bounds, applied to the aligned position of the Game Object. Use a negative value to shrink the bounds, positive to increase it.
* @param {integer} [offsetY=0] - A vertical adjustment of the Containers bounds, applied to the aligned position of the Game Object. Use a negative value to shrink the bounds, positive to increase it.
* @return {Object} This Game Object.
*/
alignTo: function (parent, position, offsetX, offsetY) {
if (offsetX === undefined) { offsetX = 0; }
if (offsetY === undefined) { offsetY = 0; }
switch (position)
{
default:
case Phaser.TOP_LEFT:
this.left = parent.left - offsetX;
this.bottom = parent.top - offsetY;
break;
case Phaser.TOP_CENTER:
this.centerX = parent.centerX + offsetX;
this.bottom = parent.top - offsetY;
break;
case Phaser.TOP_RIGHT:
this.right = parent.right + offsetX;
this.bottom = parent.top - offsetY;
break;
case Phaser.LEFT_TOP:
this.right = parent.left - offsetX;
this.top = parent.top - offsetY;
break;
case Phaser.LEFT_CENTER:
this.right = parent.left - offsetX;
this.centerY = parent.centerY + offsetY;
break;
case Phaser.LEFT_BOTTOM:
this.right = parent.left - offsetX;
this.bottom = parent.bottom + offsetY;
break;
case Phaser.RIGHT_TOP:
this.left = parent.right + offsetX;
this.top = parent.top - offsetY;
break;
case Phaser.RIGHT_CENTER:
this.left = parent.right + offsetX;
this.centerY = parent.centerY + offsetY;
break;
case Phaser.RIGHT_BOTTOM:
this.left = parent.right + offsetX;
this.bottom = parent.bottom + offsetY;
break;
case Phaser.BOTTOM_LEFT:
this.left = parent.left - offsetX;
this.top = parent.bottom + offsetY;
break;
case Phaser.BOTTOM_CENTER:
this.centerX = parent.centerX + offsetX;
this.top = parent.bottom + offsetY;
break;
case Phaser.BOTTOM_RIGHT:
this.right = parent.right + offsetX;
this.top = parent.bottom + offsetY;
break;
}
return this;
}
};
// Phaser.Group extensions
Phaser.Group.prototype.alignIn = Phaser.Component.Bounds.prototype.alignIn;
Phaser.Group.prototype.alignTo = Phaser.Component.Bounds.prototype.alignTo;