Skip to content

Commit e752b5d

Browse files
committed
Add Phaser.Actions.AlignTo() and Phaser.Display.Align.To.QuickSet()
1 parent 819a6b2 commit e752b5d

4 files changed

Lines changed: 90 additions & 0 deletions

File tree

src/actions/AlignTo.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @author samme
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var QuickSet = require('../display/align/to/QuickSet');
8+
9+
/**
10+
* Takes an array of Game Objects, or any objects that have public `x` and `y` properties, and aligns them next to each other.
11+
*
12+
* The first item isn't moved. The second item is aligned next to the first, then the third next to the second, and so on.
13+
*
14+
* @function Phaser.Actions.AlignTo
15+
* @since 3.22.0
16+
*
17+
* @generic {Phaser.GameObjects.GameObject[]} G - [items,$return]
18+
*
19+
* @param {(array|Phaser.GameObjects.GameObject[])} items - The array of items to be updated by this action.
20+
* @param {integer} position - The position to align the items with. This is an align constant, such as `Phaser.Display.Align.LEFT_CENTER`.
21+
* @param {number} [offsetX=0] - Optional horizontal offset from the position.
22+
* @param {number} [offsetY=0] - Optional vertical offset from the position.
23+
*
24+
* @return {(array|Phaser.GameObjects.GameObject[])} The array of objects that were passed to this Action.
25+
*/
26+
var AlignTo = function (items, position, offsetX, offsetY)
27+
{
28+
var target = items[0];
29+
30+
for (var i = 1; i < items.length; i++)
31+
{
32+
var item = items[i];
33+
34+
QuickSet(item, target, position, offsetX, offsetY);
35+
36+
target = item;
37+
}
38+
39+
return items;
40+
};
41+
42+
module.exports = AlignTo;

src/actions/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
module.exports = {
1212

13+
AlignTo: require('./AlignTo'),
1314
Angle: require('./Angle'),
1415
Call: require('./Call'),
1516
GetFirst: require('./GetFirst'),

src/display/align/to/QuickSet.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @author samme
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var ALIGN_CONST = require('../const');
8+
9+
var AlignToMap = [];
10+
11+
AlignToMap[ALIGN_CONST.BOTTOM_CENTER] = require('./BottomCenter');
12+
AlignToMap[ALIGN_CONST.BOTTOM_LEFT] = require('./BottomLeft');
13+
AlignToMap[ALIGN_CONST.BOTTOM_RIGHT] = require('./BottomRight');
14+
AlignToMap[ALIGN_CONST.LEFT_BOTTOM] = require('./LeftBottom');
15+
AlignToMap[ALIGN_CONST.LEFT_CENTER] = require('./LeftCenter');
16+
AlignToMap[ALIGN_CONST.LEFT_TOP] = require('./LeftTop');
17+
AlignToMap[ALIGN_CONST.RIGHT_BOTTOM] = require('./RightBottom');
18+
AlignToMap[ALIGN_CONST.RIGHT_CENTER] = require('./RightCenter');
19+
AlignToMap[ALIGN_CONST.RIGHT_TOP] = require('./RightTop');
20+
AlignToMap[ALIGN_CONST.TOP_CENTER] = require('./TopCenter');
21+
AlignToMap[ALIGN_CONST.TOP_LEFT] = require('./TopLeft');
22+
AlignToMap[ALIGN_CONST.TOP_RIGHT] = require('./TopRight');
23+
24+
/**
25+
* Takes a Game Object and aligns it next to another, at the given position.
26+
* The alignment used is based on the `position` argument, which is a `Phaser.Display.Align` property such as `LEFT_CENTER` or `TOP_RIGHT`.
27+
*
28+
* @function Phaser.Display.Align.To.QuickSet
29+
* @since 3.22.0
30+
*
31+
* @generic {Phaser.GameObjects.GameObject} G - [child,$return]
32+
*
33+
* @param {Phaser.GameObjects.GameObject} child - The Game Object that will be positioned.
34+
* @param {Phaser.GameObjects.GameObject} alignTo - The Game Object to base the alignment position on.
35+
* @param {integer} position - The position to align the Game Object with. This is an align constant, such as `Phaser.Display.Align.LEFT_CENTER`.
36+
* @param {number} [offsetX=0] - Optional horizontal offset from the position.
37+
* @param {number} [offsetY=0] - Optional vertical offset from the position.
38+
*
39+
* @return {Phaser.GameObjects.GameObject} The Game Object that was aligned.
40+
*/
41+
var QuickSet = function (child, alignTo, position, offsetX, offsetY)
42+
{
43+
return AlignToMap[position](child, alignTo, offsetX, offsetY);
44+
};
45+
46+
module.exports = QuickSet;

src/display/align/to/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
LeftBottom: require('./LeftBottom'),
1717
LeftCenter: require('./LeftCenter'),
1818
LeftTop: require('./LeftTop'),
19+
QuickSet: require('./QuickSet'),
1920
RightBottom: require('./RightBottom'),
2021
RightCenter: require('./RightCenter'),
2122
RightTop: require('./RightTop'),

0 commit comments

Comments
 (0)