forked from as3/as3-utils
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgridAlignSpaceNumber.as
More file actions
69 lines (63 loc) · 1.42 KB
/
gridAlignSpaceNumber.as
File metadata and controls
69 lines (63 loc) · 1.42 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
package utils.align
{
/**
* Aligns the items in the array in a grid based on the number of rows and columns.
* Uses the same spacing for all.
*
* @example <listing version="3.0">Alignment.hAlignSpaceNum( [ clip0, clip1, clip2], 10 );</listing>
*
* @param items An array of items
* @param rows The number of rows
* @param cols The number of columns
* @param spacing The spacing between items in pixels
*/
public function gridAlignSpaceNumber(items:Array, rows:Number, cols:Number, spacing:Number = 0):void
{
var col:Number = 0;
var row:Number = 1;
var yPos:Number = 0;
var xPos:Number = 0;
var maxHeightCurrentRow:Number = 0;
var maxHeightPreviousRow:Number = 0;
var n:int = items.length;
for (var i:Number = 0; i < n; i++)
{
if (row <= rows)
{
items[i].x = xPos;
if (col == (cols - 1))
{
xPos = 0;
row++;
}
else
{
xPos += items[(i)].width + spacing;
}
if (col == cols)
{
col = 0;
/*
* Get the max item height from previous row
*/
maxHeightPreviousRow = maxHeightCurrentRow;
maxHeightCurrentRow = items[(i)].height;
yPos += maxHeightPreviousRow + spacing;
}
else
{
/*
* Calculate the max item height from previous row
*/
maxHeightCurrentRow = Math.max(maxHeightCurrentRow, items[(i)].height);
}
items[i].y = yPos;
col++;
}
else
{
break;
}
}
}
}