-
Notifications
You must be signed in to change notification settings - Fork 35
/
Rectangle.ts
202 lines (182 loc) · 5.38 KB
/
Rectangle.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
export interface IRectangle {
width: number
height: number
x: number
y: number
[propName: string]: any
}
export class Rectangle implements IRectangle {
/**
* Oversized tag on rectangle which is bigger than packer itself.
*
* @type {boolean}
* @memberof Rectangle
*/
public oversized: boolean = false;
/**
* Creates an instance of Rectangle.
*
* @param {number} [width=0]
* @param {number} [height=0]
* @param {number} [x=0]
* @param {number} [y=0]
* @param {boolean} [rot=false]
* @param {boolean} [allowRotation=false]
* @memberof Rectangle
*/
constructor (
width: number = 0,
height: number = 0,
x: number = 0,
y: number = 0,
rot: boolean = false,
allowRotation: boolean | undefined = undefined
) {
this._width = width;
this._height = height;
this._x = x;
this._y = y;
this._data = {};
this._rot = rot;
this._allowRotation = allowRotation;
}
/**
* Test if two given rectangle collide each other
*
* @static
* @param {IRectangle} first
* @param {IRectangle} second
* @returns
* @memberof Rectangle
*/
public static Collide (first: IRectangle, second: IRectangle) { return first.collide(second); }
/**
* Test if the first rectangle contains the second one
*
* @static
* @param {IRectangle} first
* @param {IRectangle} second
* @returns
* @memberof Rectangle
*/
public static Contain (first: IRectangle, second: IRectangle) { return first.contain(second); }
/**
* Get the area (w * h) of the rectangle
*
* @returns {number}
* @memberof Rectangle
*/
public area (): number { return this.width * this.height; }
/**
* Test if the given rectangle collide with this rectangle.
*
* @param {IRectangle} rect
* @returns {boolean}
* @memberof Rectangle
*/
public collide (rect: IRectangle): boolean {
return (
rect.x < this.x + this.width &&
rect.x + rect.width > this.x &&
rect.y < this.y + this.height &&
rect.y + rect.height > this.y
);
}
/**
* Test if this rectangle contains the given rectangle.
*
* @param {IRectangle} rect
* @returns {boolean}
* @memberof Rectangle
*/
public contain (rect: IRectangle): boolean {
return (rect.x >= this.x && rect.y >= this.y &&
rect.x + rect.width <= this.x + this.width && rect.y + rect.height <= this.y + this.height);
}
protected _width: number;
get width (): number { return this._width; }
set width (value: number) {
if (value === this._width) return;
this._width = value;
this._dirty ++;
}
protected _height: number;
get height (): number { return this._height; }
set height (value: number) {
if (value === this._height) return;
this._height = value;
this._dirty ++;
}
protected _x: number;
get x (): number { return this._x; }
set x (value: number) {
if (value === this._x) return;
this._x = value;
this._dirty ++;
}
protected _y: number;
get y (): number { return this._y; }
set y (value: number) {
if (value === this._y) return;
this._y = value;
this._dirty ++;
}
protected _rot: boolean = false;
/**
* If the rectangle is rotated
*
* @type {boolean}
* @memberof Rectangle
*/
get rot (): boolean { return this._rot; }
/**
* Set the rotate tag of the rectangle.
*
* note: after `rot` is set, `width/height` of this rectangle is swaped.
*
* @memberof Rectangle
*/
set rot (value: boolean) {
if (this._allowRotation === false) return;
if (this._rot !== value) {
const tmp = this.width;
this.width = this.height;
this.height = tmp;
this._rot = value;
this._dirty ++;
}
}
protected _allowRotation: boolean | undefined = undefined;
/**
* If the rectangle allow rotation
*
* @type {boolean}
* @memberof Rectangle
*/
get allowRotation (): boolean | undefined { return this._allowRotation; }
/**
* Set the allowRotation tag of the rectangle.
*
* @memberof Rectangle
*/
set allowRotation (value: boolean | undefined) {
if (this._allowRotation !== value) {
this._allowRotation = value;
this._dirty ++;
}
}
protected _data: any;
get data (): any { return this._data; }
set data (value: any) {
if (value === null || value === this._data) return;
this._data = value;
// extract allowRotation settings
if (typeof value === "object" && value.hasOwnProperty("allowRotation")) {
this._allowRotation = value.allowRotation;
}
this._dirty ++;
}
protected _dirty: number = 0;
get dirty (): boolean { return this._dirty > 0; }
public setDirty (value: boolean = true): void { this._dirty = value ? this._dirty + 1 : 0; }
}