~eda-qa/dhlib/main

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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* <license>
 * This file is part of the dis-Emi-A HaXe Library. Copyright (c) edA-qa mort-ora-y
 * For full copyright and license information please refer to doc/license.txt.
 * </license> 
 */
package mathx;

/**
 * A generic rectangle class, this parallels the flash Rectangle class but will fill
 * in missing items and be usable in more than just a flash environment.
 *
 * May also be used for padding in which case the four core members represent
 * padding variables and not a rectangle.
 */
class Rect2
{
	public var left : Float;
	public var top : Float;
	public var right : Float;
	public var bottom : Float;
	
	private function new( left : Float, top : Float, right : Float, bottom : Float )
	{
		this.left = left;
		this.top = top;
		this.right = right;
		this.bottom = bottom;
	}
	
#if flash
	static public function fromFlash( fr : flash.geom.Rectangle ) : Rect2
	{
		return new Rect2(fr.left, fr.top,fr.right,fr.bottom);
	}
	
	public function toFlash( ) : flash.geom.Rectangle
	{
		return new flash.geom.Rectangle( left, top, right-left, bottom-top );
	}
	
	public function toWindowGlobal( obj : flash.display.DisplayObject ) : Rect2
	{
		var ul = obj.localToGlobal( upperLeft.toFlash() );
		var br = obj.localToGlobal( bottomRight.toFlash() );
		return new Rect2( ul.x, ul.y, br.x, br.y );
	}
	
	public function toWindowLocal( obj : flash.display.DisplayObject ) : Rect2
	{
		var ul = obj.globalToLocal( upperLeft.toFlash() );
		var br = obj.globalToLocal( bottomRight.toFlash() );
		return new Rect2( ul.x, ul.y, br.x, br.y );
	}
	
	public function fromWindowToWindow( objS, objT ) : Rect2
	{
		return toWindowGlobal( objS ).toWindowLocal( objT );
	}
	
#end
	static public function fromULBR( left : Float, top : Float, right : Float, bottom : Float )
	{
		return new Rect2( left, top, right, bottom );
	}
	
	static public function fromULSizePt( ul : Point2, size : Point2 )
	{
		return new Rect2( ul.x, ul.y, ul.x + size.x, ul.y + size.y );
	}

	static public function fromULSize( ulx : Float, uly : Float, cx : Float, cy : Float )
	{
		return new Rect2( ulx, uly, ulx+cx, uly+cy );
	}
	
	/** 
	 * Using the values from Widget.getPos(), getSize() and getCenter()
	 */
	static public function fromPosSizeCtrPt( pos : Point2, size : Point2, ctr : Point2 )
	{
		var ulR = ctr.sub( Point2.at( 0.5, 0.5 ) );
		var ul = pos.add( size.scale( ulR ) );
		var br = ul.add( size );
		return new Rect2( ul.x, ul.y, br.x, br.y );
	}
	
#if flash
	static public function fromWidgetBounds( widget : ui.Widget )
	{
		return fromPosSizeCtrPt( widget.getPos(), widget.getSize(), widget.getCenter() );
	}
#end

	public function toString() : String
	{
		return "(" + left + "," + top + "," + (right-left) + "," + (bottom-top) + ")";
	}
	
	public function clone() : Rect2
	{
		return new Rect2( left, top, right, bottom );
	}
	
	public function intersects( o : Rect2 ) : Bool
	{
		//TODO: I'm being lazy for now, but perhaps this is faster than our own code...?
		return toFlash().intersects( o.toFlash() );
	}
	
	public function containsPt( pt : Point2 ) : Bool
	{
		return pt.x >= left
			&& pt.x <= right
			&& pt.y >= top
			&& pt.y <= bottom;
	}
	
	public function contains( x : Float, y : Float ) : Bool
	{
		return x >= left
			&& x <= right
			&& y >= top
			&& y <= bottom;
	}
	
	public function scalePt( pt : Point2 ) : Rect2
	{
		var cl = clone();
		cl.left *= pt.x;
		cl.right *= pt.x;
		cl.top *= pt.y;
		cl.bottom *= pt.y;
		return cl;
	}
	
	public function scale( cx : Float, cy : Float ) : Rect2
	{
		var cl = clone();
		cl.left *= cx;
		cl.right *= cx;
		cl.top *= cy;
		cl.bottom *= cy;
		return cl;
	}
	
	public function addOff( rect : Rect2 ) : Rect2
	{
		return new Rect2( left + rect.left, top + rect.top, right + rect.right, bottom + rect.bottom );
	}
	
	public function addOffPt( pt : Point2 ) : Rect2 
	{
		return new Rect2( left + pt.x, top + pt.y, right + pt.x, bottom + pt.y );
	}
	
	/**
	 * Obtains a relatively size rectangle.
	 */
	inline public function rel( x1 : Float, y1 : Float,  x2 : Float, y2 : Float ) : Rect2
	{
		return new Rect2( left + width * x1, top + height * y1,
			left + width * x2, top + height * y2 );
	}
	
	/**
	 * OBtains a relatively sized rectangle giving a size rather than position
	 * of bottom right corner.
	 */
	inline public function relSize( x1 : Float, y1 : Float, sizeX : Float, sizeY : Float ) : Rect2
	{
		return new Rect2( left + width * x1, top + height * y1,
			left + width * (x1 + sizeX), top + height * (y1 + sizeY) );
	}
	
	inline public function relSizeCtr( x1 : Float, y1 : Float, sizeX : Float, sizeY : Float ) : Rect2
	{
		return new Rect2( left + width * (x1 - sizeX/2), top + height * (y1 - sizeY/2),
			left + width * (x1 + sizeX/2), top + height * (y1 + sizeY/2) );
	}
	
define(`VIRTUAL',`
	public var $1(get$1,set$1) : $2;
	inline public function get$1() : $2 { $3 }
	inline public function set$1( v : $2 ) : $2 { $4 $3 }
')
	VIRTUAL(width,Float,
		return right-left;
		,
		right=left+v;
		)
	VIRTUAL(height,Float,
		return bottom-top;
		,
		bottom=top+v;
		)
	VIRTUAL(center,Point2,
		return Point2.at( ( left + right ) / 2, (top + bottom) / 2 );
		,
		)
		
	VIRTUAL(size,Point2,
		return Point2.at( right - left, bottom - top );
		,
		var ctr = getcenter();
		left = ctr.x - v.x/2;
		top = ctr.y - v.y/2;
		right = ctr.x + v.x/2;
		bottom = ctr.y + v.y/2;
		)
		
	VIRTUAL(upperLeft,Point2,
		return Point2.at( left, top );
		,
		left = v.x;
		top = v.y;
		)
		
	VIRTUAL(bottomRight,Point2,
		return Point2.at( right, bottom );
		,
		right = v.x;
		bottom = v.y;
		)
		
	/**
	 * Returns the coordinates of the corners starting at the upper Left and moving clockwise
	 */
	public function coords() : Array<Point2>
	{
		return [
			Point2.at( left, top ),
			Point2.at( right, top ),
			Point2.at( right, bottom ),
			Point2.at( left, bottom )
			];
	}
	
}