phaser/src/actions/GridAlign.js

102 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-10-11 16:05:59 +00:00
var AlignIn = require('../display/align/in/QuickSet');
var CONST = require('../display/align/const');
var GetValue = require('../utils/object/GetValue');
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
var tempZone = new Zone({ sys: { queueDepthSort: NOOP }}, 0, 0, 1, 1);
2017-03-27 16:34:49 +00:00
2017-10-04 16:05:26 +00:00
/**
* [description]
*
* @function Phaser.Actions.GridAlign
* @since 3.0.0
*
* @param {array} items - An array of Game Objects. The contents of this array are updated by this Action.
* @param {object} options - [description]
2017-10-06 02:05:01 +00:00
*
2017-10-04 16:05:26 +00:00
* @return {array} The array of Game Objects that was passed to this Action.
*/
var GridAlign = function (items, options)
2017-03-27 16:34:49 +00:00
{
var width = GetValue(options, 'width', -1);
var height = GetValue(options, 'height', -1);
var cellWidth = GetValue(options, 'cellWidth', 1);
var cellHeight = GetValue(options, 'cellHeight', cellWidth);
var position = GetValue(options, 'position', CONST.TOP_LEFT);
var x = GetValue(options, 'x', 0);
var y = GetValue(options, 'y', 0);
// var centerX = GetValue(options, 'centerX', null);
// var centerY = GetValue(options, 'centerY', null);
2017-03-27 22:38:45 +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
// If the Grid is centered on a position then we need to calculate it now
// if (centerX !== null && centerY !== null)
// {
//
// }
tempZone.setPosition(x, y);
tempZone.setSize(cellWidth, cellHeight);
for (var i = 0; i < items.length; i++)
2017-03-27 16:34:49 +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
cy += cellHeight;
2017-03-27 22:38:45 +00:00
tempZone.y += cellHeight;
2017-03-27 16:34:49 +00:00
if (cy === h)
2017-03-27 16:34:49 +00:00
{
cy = 0;
2017-03-27 22:38:45 +00:00
tempZone.x += cellWidth;
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
cx += cellWidth;
2017-03-27 22:38:45 +00:00
tempZone.x += cellWidth;
2017-03-27 16:34:49 +00:00
if (cx === w)
2017-03-27 16:34:49 +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
cx += cellWidth;
2017-03-27 22:38:45 +00:00
tempZone.x += cellWidth;
2017-03-27 16:34:49 +00:00
if (cx === w)
2017-03-27 16:34:49 +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
if (cy === h)
2017-03-27 16:34:49 +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
}
}
}
}
return items;
2017-03-27 16:34:49 +00:00
};
2017-03-27 22:10:11 +00:00
module.exports = GridAlign;