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.
|
2018-02-12 16:01:20 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-10-11 16:05:59 +00:00
|
|
|
var AlignIn = require('../display/align/in/QuickSet');
|
|
|
|
var CONST = require('../display/align/const');
|
2018-03-17 17:16:26 +00:00
|
|
|
var GetFastValue = require('../utils/object/GetFastValue');
|
2018-01-11 13:59:06 +00:00
|
|
|
var NOOP = require('../utils/NOOP');
|
2017-10-11 16:05:59 +00:00
|
|
|
var Zone = require('../gameobjects/zone/Zone');
|
2017-03-27 22:38:45 +00:00
|
|
|
|
2018-04-13 16:05:07 +00:00
|
|
|
var tempZone = new Zone({ sys: { queueDepthSort: NOOP, events: { once: NOOP } } }, 0, 0, 1, 1);
|
2017-03-27 16:34:49 +00:00
|
|
|
|
2017-10-04 16:05:26 +00:00
|
|
|
/**
|
2018-03-17 17:16:26 +00:00
|
|
|
* @typedef {object} GridAlignConfig
|
|
|
|
*
|
|
|
|
* @property {integer} [width=-1] - The width of the grid in items (not pixels). -1 means lay all items out horizontally, regardless of quantity.
|
|
|
|
* If both this value and height are set to -1 then this value overrides it and the `height` value is ignored.
|
|
|
|
* @property {integer} [height=-1] - The height of the grid in items (not pixels). -1 means lay all items out vertically, regardless of quantity.
|
|
|
|
* If both this value and `width` are set to -1 then `width` overrides it and this value is ignored.
|
2018-04-29 08:25:44 +00:00
|
|
|
* @property {integer} [cellWidth=1] - The width of the cell, in pixels, in which the item is positioned.
|
2018-03-17 17:16:26 +00:00
|
|
|
* @property {integer} [cellHeight=1] - The height of the cell, in pixels, in which the item is positioned.
|
|
|
|
* @property {integer} [position=0] - The alignment position. One of the Phaser.Display.Align consts such as `TOP_LEFT` or `RIGHT_CENTER`.
|
|
|
|
* @property {number} [x=0] - Optionally place the top-left of the final grid at this coordinate.
|
|
|
|
* @property {number} [y=0] - Optionally place the top-left of the final grid at this coordinate.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes an array of Game Objects, or any objects that have public `x` and `y` properties,
|
|
|
|
* and then aligns them based on the grid configuration given to this action.
|
2017-10-04 16:05:26 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Actions.GridAlign
|
|
|
|
* @since 3.0.0
|
2018-03-20 14:57:12 +00:00
|
|
|
*
|
2018-03-27 11:14:08 +00:00
|
|
|
* @generic {Phaser.GameObjects.GameObject[]} G - [items,$return]
|
|
|
|
*
|
2018-03-20 14:57:12 +00:00
|
|
|
* @param {(array|Phaser.GameObjects.GameObject[])} items - The array of items to be updated by this action.
|
2018-03-17 17:16:26 +00:00
|
|
|
* @param {GridAlignConfig} options - The GridAlign Configuration object.
|
2017-10-06 02:05:01 +00:00
|
|
|
*
|
2018-03-27 11:14:08 +00:00
|
|
|
* @return {(array|Phaser.GameObjects.GameObject[])} The array of objects that were passed to this Action.
|
2017-10-04 16:05:26 +00:00
|
|
|
*/
|
2017-03-29 00:34:46 +00:00
|
|
|
var GridAlign = function (items, options)
|
2017-03-27 16:34:49 +00:00
|
|
|
{
|
2018-03-17 17:16:26 +00:00
|
|
|
if (options === undefined) { options = {}; }
|
2018-02-16 18:17:51 +00:00
|
|
|
|
2018-03-17 17:16:26 +00:00
|
|
|
var width = GetFastValue(options, 'width', -1);
|
|
|
|
var height = GetFastValue(options, 'height', -1);
|
|
|
|
var cellWidth = GetFastValue(options, 'cellWidth', 1);
|
|
|
|
var cellHeight = GetFastValue(options, 'cellHeight', cellWidth);
|
|
|
|
var position = GetFastValue(options, 'position', CONST.TOP_LEFT);
|
|
|
|
var x = GetFastValue(options, 'x', 0);
|
|
|
|
var y = GetFastValue(options, 'y', 0);
|
2017-03-27 22:38:45 +00:00
|
|
|
|
2017-03-29 00:34:46 +00:00
|
|
|
var cx = 0;
|
|
|
|
var cy = 0;
|
2017-03-27 16:34:49 +00:00
|
|
|
var w = (width * cellWidth);
|
|
|
|
var h = (height * cellHeight);
|
|
|
|
|
2017-03-29 00:47:17 +00:00
|
|
|
tempZone.setPosition(x, y);
|
|
|
|
tempZone.setSize(cellWidth, cellHeight);
|
|
|
|
|
2017-03-28 12:20:39 +00:00
|
|
|
for (var i = 0; i < items.length; i++)
|
2017-03-27 16:34:49 +00:00
|
|
|
{
|
2017-03-28 12:20:39 +00:00
|
|
|
AlignIn(items[i], tempZone, position);
|
2017-03-27 16:34:49 +00:00
|
|
|
|
|
|
|
if (width === -1)
|
|
|
|
{
|
|
|
|
// We keep laying them out horizontally until we've done them all
|
2017-03-29 00:34:46 +00:00
|
|
|
cy += cellHeight;
|
2017-03-27 22:38:45 +00:00
|
|
|
tempZone.y += cellHeight;
|
2017-03-27 16:34:49 +00:00
|
|
|
|
2017-03-29 00:34:46 +00:00
|
|
|
if (cy === h)
|
2017-03-27 16:34:49 +00:00
|
|
|
{
|
2017-03-29 00:34:46 +00:00
|
|
|
cy = 0;
|
2017-03-27 22:38:45 +00:00
|
|
|
tempZone.x += cellWidth;
|
2017-03-29 00:34:46 +00:00
|
|
|
tempZone.y = y;
|
2017-03-27 16:34:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (height === -1)
|
|
|
|
{
|
|
|
|
// We keep laying them out vertically until we've done them all
|
2017-03-29 00:34:46 +00:00
|
|
|
cx += cellWidth;
|
2017-03-27 22:38:45 +00:00
|
|
|
tempZone.x += cellWidth;
|
2017-03-27 16:34:49 +00:00
|
|
|
|
2017-03-29 00:34:46 +00:00
|
|
|
if (cx === w)
|
2017-03-27 16:34:49 +00:00
|
|
|
{
|
2017-03-29 00:34:46 +00:00
|
|
|
cx = 0;
|
|
|
|
tempZone.x = x;
|
2017-03-27 22:38:45 +00:00
|
|
|
tempZone.y += cellHeight;
|
2017-03-27 16:34:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We keep laying them out until we hit the column limit
|
2017-03-29 00:34:46 +00:00
|
|
|
cx += cellWidth;
|
2017-03-27 22:38:45 +00:00
|
|
|
tempZone.x += cellWidth;
|
2017-03-27 16:34:49 +00:00
|
|
|
|
2017-03-29 00:34:46 +00:00
|
|
|
if (cx === w)
|
2017-03-27 16:34:49 +00:00
|
|
|
{
|
2017-03-29 00:34:46 +00:00
|
|
|
cx = 0;
|
|
|
|
cy += cellHeight;
|
|
|
|
tempZone.x = x;
|
2017-03-27 22:38:45 +00:00
|
|
|
tempZone.y += cellHeight;
|
2017-03-27 16:34:49 +00:00
|
|
|
|
2017-03-29 00:34:46 +00:00
|
|
|
if (cy === h)
|
2017-03-27 16:34:49 +00:00
|
|
|
{
|
2017-03-28 12:20:39 +00:00
|
|
|
// We've hit the column limit, so return, even if there are items left
|
|
|
|
break;
|
2017-03-27 16:34:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 12:20:39 +00:00
|
|
|
return items;
|
2017-03-27 16:34:49 +00:00
|
|
|
};
|
2017-03-27 22:10:11 +00:00
|
|
|
|
2017-03-28 12:20:39 +00:00
|
|
|
module.exports = GridAlign;
|