Repository navigation
Expand file tree
/
Copy pathGrid.ts
More file actions
73 lines (59 loc) · 2.11 KB
/
Copy pathGrid.ts
File metadata and controls
73 lines (59 loc) · 2.11 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
module gameui {
//this class alow user to arrange objects in a grid forrmat
//the anchor point is the center of object
export class Grid extends UIItem {
//provided variables
private flowHorizontal = false;
private cols: number;
private rows: number;
private padding: number;
//defined variabples
private hSpacing: number;
private wSpacing: number;
//control variables;
private currentCol: number = 0;
private currentRow: number = 0;
constructor(cols: number, rows: number, width: number, height: number, padding: number=0, flowHorizontal?: boolean) {
super();
//define the variables
this.flowHorizontal = flowHorizontal;
this.cols = cols;
this.rows = rows;
this.padding = padding;
this.width = width;
this.height = height;
//define other parameters
this.wSpacing = (width - padding * 2) / cols;
this.hSpacing = (height - padding * 2) / rows;
}
//place objecrs into a grid format
public addObject(object: PIXI.DisplayObject) {
this.addChild(object);
object.x = this.getXPos();
object.y = this.getYPos();
this.updatePosition();
}
private getXPos(): number {
return this.padding + this.currentCol * this.wSpacing + this.wSpacing / 2;
}
private getYPos(): number {
return this.padding + this.currentRow * this.hSpacing + this.hSpacing / 2;
}
//define next Item position
private updatePosition() {
if (!this.flowHorizontal) {
this.currentCol++;
if (this.currentCol >= this.cols) {
this.currentCol = 0;
this.currentRow++;
}
} else {
this.currentRow++;
if (this.currentRow >= this.rows) {
this.currentRow = 0;
this.currentCol++;
}
}
}
}
}