Mercurial > projects > dwt-addons
comparison dwtx/draw2d/geometry/Dimension.d @ 98:95307ad235d9
Added Draw2d code, still work in progress
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 03 Aug 2008 00:52:14 +0200 |
parents | |
children | 1082a0fc2bb8 |
comparison
equal
deleted
inserted
replaced
96:b492ba44e44d | 98:95307ad235d9 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2008 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.draw2d.geometry.Dimension; | |
14 | |
15 import dwtx.draw2d.geometry.Point; | |
16 | |
17 import dwt.dwthelper.utils; | |
18 | |
19 import dwtx.draw2d.geometry.Translatable; | |
20 static import dwt.graphics.Point; | |
21 static import dwt.graphics.Image; | |
22 static import dwt.graphics.Rectangle; | |
23 import tango.text.convert.Format; | |
24 | |
25 /** | |
26 * Stores an integer width and height. This class provides various methods for | |
27 * manipulating this Dimension or creating new derived Objects. | |
28 */ | |
29 public class Dimension | |
30 : Cloneable/+, java.io.Serializable+/, Translatable | |
31 { | |
32 | |
33 /**A singleton for use in short calculations. Use to avoid newing unnecessary objects.*/ | |
34 public static const Dimension SINGLETON; | |
35 | |
36 static this(){ | |
37 SINGLETON = new Dimension(); | |
38 } | |
39 | |
40 /**The width.*/ | |
41 public int width; | |
42 /**The height. */ | |
43 public int height; | |
44 | |
45 static final long serialVersionUID = 1; | |
46 | |
47 /** | |
48 * Constructs a Dimension of zero width and height. | |
49 * | |
50 * @since 2.0 | |
51 */ | |
52 public this() { } | |
53 | |
54 /** | |
55 * Constructs a Dimension with the width and height of the passed Dimension. | |
56 * | |
57 * @param d the Dimension supplying the initial values | |
58 * @since 2.0 | |
59 */ | |
60 public this(Dimension d) { | |
61 width = d.width; | |
62 height = d.height; | |
63 } | |
64 | |
65 /** | |
66 * Constructs a Dimension where the width and height are the x and y distances of the | |
67 * input point from the origin. | |
68 * | |
69 * @param pt the Point supplying the initial values | |
70 * @since 2.0 | |
71 */ | |
72 public this(dwt.graphics.Point.Point pt) { | |
73 width = pt.x; | |
74 height = pt.y; | |
75 } | |
76 | |
77 /** | |
78 * Constructs a Dimension with the supplied width and height values. | |
79 * | |
80 * @param w the width | |
81 * @param h the height | |
82 * @since 2.0 | |
83 */ | |
84 public this(int w, int h) { | |
85 width = w; | |
86 height = h; | |
87 } | |
88 | |
89 /** | |
90 * Constructs a Dimension with the width and height of the Image supplied as input. | |
91 * | |
92 * @param image the image supplying the dimensions | |
93 * @since 2.0 | |
94 */ | |
95 public this(dwt.graphics.Image.Image image) { | |
96 dwt.graphics.Rectangle.Rectangle r = image.getBounds(); | |
97 width = r.width; | |
98 height = r.height; | |
99 } | |
100 | |
101 /** | |
102 * Returns <code>true</code> if the input Dimension fits into this Dimension. A Dimension | |
103 * of the same size is considered to "fit". | |
104 * | |
105 * @param d the dimension being tested | |
106 * @return <code>true</code> if this Dimension contains <i>d</i> | |
107 * @since 2.0 | |
108 */ | |
109 public bool contains(Dimension d) { | |
110 return width >= d.width && height >= d.height; | |
111 } | |
112 | |
113 /** | |
114 * Returns <code>true</code> if this Dimension properly contains the one specified. | |
115 * Proper containment is defined as containment using "<", instead of "<=". | |
116 * | |
117 * @param d the dimension being tested | |
118 * @return <code>true</code> if this Dimension properly contains the one specified | |
119 * @since 2.0 | |
120 */ | |
121 public bool containsProper(Dimension d) { | |
122 return width > d.width && height > d.height; | |
123 } | |
124 | |
125 /** | |
126 * Copies the width and height values of the input Dimension to this Dimension. | |
127 * | |
128 * @param d the dimension supplying the values | |
129 * @since 2.0 | |
130 */ | |
131 public void setSize(Dimension d) { | |
132 width = d.width; | |
133 height = d.height; | |
134 } | |
135 | |
136 /** | |
137 * Returns the area of this Dimension. | |
138 * | |
139 * @return the area | |
140 * @since 2.0 | |
141 */ | |
142 public int getArea() { | |
143 return width * height; | |
144 } | |
145 | |
146 /** | |
147 * Creates and returns a copy of this Dimension. | |
148 * @return a copy of this Dimension | |
149 * @since 2.0 | |
150 */ | |
151 public Dimension getCopy() { | |
152 return new Dimension(this); | |
153 } | |
154 | |
155 /** | |
156 * Creates and returns a new Dimension representing the difference between this Dimension | |
157 * and the one specified. | |
158 * | |
159 * @param d the dimension being compared | |
160 * @return a new dimension representing the difference | |
161 * @since 2.0 | |
162 */ | |
163 public Dimension getDifference(Dimension d) { | |
164 return new Dimension(width - d.width, height - d.height); | |
165 } | |
166 | |
167 /** | |
168 * Creates and returns a Dimension representing the sum of this Dimension and the one | |
169 * specified. | |
170 * | |
171 * @param d the dimension providing the expansion width and height | |
172 * @return a new dimension expanded by <i>d</i> | |
173 * @since 2.0 | |
174 */ | |
175 public Dimension getExpanded(Dimension d) { | |
176 return new Dimension(width + d.width, height + d.height); | |
177 } | |
178 | |
179 /** | |
180 * Creates and returns a new Dimension representing the sum of this Dimension and the one | |
181 * specified. | |
182 * | |
183 * @param w value by which the width of this is to be expanded | |
184 * @param h value by which the height of this is to be expanded | |
185 * @return a new Dimension expanded by the given values | |
186 * @since 2.0 | |
187 */ | |
188 public Dimension getExpanded(int w, int h) { | |
189 return new Dimension(width + w, height + h); | |
190 } | |
191 | |
192 /** | |
193 * Creates and returns a new Dimension representing the intersection of this Dimension and | |
194 * the one specified. | |
195 * | |
196 * @param d the Dimension to intersect with | |
197 * @return A new Dimension representing the intersection | |
198 * @since 2.0 | |
199 */ | |
200 public Dimension getIntersected(Dimension d) { | |
201 return (new Dimension(this)).intersect(d); | |
202 } | |
203 | |
204 /** | |
205 * Creates and returns a new Dimension with negated values. | |
206 * | |
207 * @return a new Dimension with negated values | |
208 * @since 2.0 | |
209 */ | |
210 public Dimension getNegated() { | |
211 return new Dimension(0 - width, 0 - height); | |
212 } | |
213 | |
214 /** | |
215 * Returns whether the input Object is equivalent to this Dimension. <code>true</code> if | |
216 * the Object is a Dimension and its width and height are equal to this Dimension's width | |
217 * and height, <code>false</code> otherwise. | |
218 * | |
219 * @param o the Object being tested for equality | |
220 * @return <code>true</code> if the given object is equal to this dimension | |
221 * @since 2.0 | |
222 */ | |
223 public override int opEquals(Object o) { | |
224 if (auto d = cast(Dimension)o ) { | |
225 return (d.width is width && d.height is height); | |
226 } | |
227 return false; | |
228 } | |
229 | |
230 /** | |
231 * Returns <code>true</code> if this Dimension's width and height are equal to the given | |
232 * width and height. | |
233 * | |
234 * @param w the width | |
235 * @param h the height | |
236 * @return <code>true</code> if this dimension's width and height are equal to those given. | |
237 * @since 2.0 | |
238 */ | |
239 public bool equals(int w, int h) { | |
240 return width is w && height is h; | |
241 } | |
242 | |
243 /** | |
244 * Expands the size of this Dimension by the specified amount. | |
245 * | |
246 * @param d the Dimension providing the expansion width and height | |
247 * @return <code>this</code> for convenience | |
248 * @since 2.0 | |
249 */ | |
250 public Dimension expand(Dimension d) { | |
251 width += d.width; | |
252 height += d.height; | |
253 return this; | |
254 } | |
255 | |
256 /** | |
257 * Expands the size of this Dimension by the specified amound. | |
258 * | |
259 * @param pt the Point supplying the dimensional values | |
260 * @return <code>this</code> for convenience | |
261 * @since 2.0 | |
262 */ | |
263 public Dimension expand(Point pt) { | |
264 width += pt.x; | |
265 height += pt.y; | |
266 return this; | |
267 } | |
268 | |
269 /** | |
270 * Expands the size of this Dimension by the specified width and height. | |
271 * | |
272 * @param w Value by which the width should be increased | |
273 * @param h Value by which the height should be increased | |
274 * @return <code>this</code> for convenience | |
275 * @since 2.0 | |
276 */ | |
277 public Dimension expand(int w, int h) { | |
278 width += w; | |
279 height += h; | |
280 return this; | |
281 } | |
282 | |
283 /** | |
284 * Creates a new Dimension with its width and height scaled by the specified value. | |
285 * | |
286 * @param amount Value by which the width and height are scaled | |
287 * @return a new dimension with the scale applied | |
288 * @since 2.0 | |
289 */ | |
290 public Dimension getScaled(double amount) { | |
291 return (new Dimension(this)) | |
292 .scale(amount); | |
293 } | |
294 | |
295 /** | |
296 * Creates a new Dimension with its height and width swapped. Useful in orientation change | |
297 * calculations. | |
298 * | |
299 * @return a new Dimension with its height and width swapped | |
300 * @since 2.0 | |
301 */ | |
302 public Dimension getTransposed() { | |
303 return (new Dimension(this)) | |
304 .transpose(); | |
305 } | |
306 | |
307 /** | |
308 * Creates a new Dimension representing the union of this Dimension with the one | |
309 * specified. Union is defined as the max() of the values from each Dimension. | |
310 * | |
311 * @param d the Dimension to be unioned | |
312 * @return a new Dimension | |
313 * @since 2.0 | |
314 */ | |
315 public Dimension getUnioned(Dimension d) { | |
316 return (new Dimension(this)).union_(d); | |
317 } | |
318 | |
319 /** | |
320 * @see java.lang.Object#toHash() | |
321 */ | |
322 public override hash_t toHash() { | |
323 return (width * height) ^ (width + height); | |
324 } | |
325 | |
326 | |
327 /** | |
328 * This Dimension is intersected with the one specified. Intersection is performed by | |
329 * taking the min() of the values from each dimension. | |
330 * | |
331 * @param d the Dimension used to perform the min() | |
332 * @return <code>this</code> for convenience | |
333 * @since 2.0 | |
334 */ | |
335 public Dimension intersect(Dimension d) { | |
336 width = Math.min(d.width, width); | |
337 height = Math.min(d.height, height); | |
338 return this; | |
339 } | |
340 | |
341 /** | |
342 * Returns <code>true</code> if the Dimension has width or height greater than 0. | |
343 * | |
344 * @return <code>true</code> if this Dimension is empty | |
345 * @since 2.0 | |
346 */ | |
347 public bool isEmpty() { | |
348 return (width <= 0) || (height <= 0); | |
349 } | |
350 | |
351 /** | |
352 * Negates the width and height of this Dimension. | |
353 * | |
354 * @return <code>this</code> for convenience | |
355 * @since 2.0 | |
356 */ | |
357 public Dimension negate() { | |
358 width = 0 - width; | |
359 height = 0 - height; | |
360 return this; | |
361 } | |
362 | |
363 /** | |
364 * @see dwtx.draw2d.geometry.Translatable#performScale(double) | |
365 */ | |
366 public void performScale(double factor) { | |
367 scale(factor); | |
368 } | |
369 | |
370 /** | |
371 * @see dwtx.draw2d.geometry.Translatable#performTranslate(int, int) | |
372 */ | |
373 public void performTranslate(int dx, int dy) { } | |
374 | |
375 /** | |
376 * Scales the width and height of this Dimension by the amount supplied, and returns this | |
377 * for convenience. | |
378 * | |
379 * @param amount value by which this Dimension's width and height are to be scaled | |
380 * @return <code>this</code> for convenience | |
381 * @since 2.0 | |
382 */ | |
383 public Dimension scale(double amount) { | |
384 return scale(amount, amount); | |
385 } | |
386 | |
387 /** | |
388 * Scales the width of this Dimension by <i>w</i> and scales the height of this Dimension | |
389 * by <i>h</i>. Returns this for convenience. | |
390 * | |
391 * @param w the value by which the width is to be scaled | |
392 * @param h the value by which the height is to be scaled | |
393 * @return <code>this</code> for convenience | |
394 * @since 2.0 | |
395 */ | |
396 public Dimension scale(double w, double h) { | |
397 width = cast(int)(Math.floor(width * w)); | |
398 height = cast(int)(Math.floor(height * h)); | |
399 return this; | |
400 } | |
401 | |
402 /** | |
403 * Reduces the width of this Dimension by <i>w</i>, and reduces the height of this | |
404 * Dimension by <i>h</i>. Returns this for convenience. | |
405 * | |
406 * @param w the value by which the width is to be reduced | |
407 * @param h the value by which the height is to be reduced | |
408 * @return <code>this</code> for convenience | |
409 * @since 2.0 | |
410 */ | |
411 public Dimension shrink(int w, int h) { | |
412 return expand(-w, -h); | |
413 } | |
414 | |
415 /** | |
416 * @see Object#toString() | |
417 */ | |
418 | |
419 public override String toString() { | |
420 return Format("Dimension({}, {})", width, height); | |
421 } | |
422 | |
423 /** | |
424 * Swaps the width and height of this Dimension, and returns this for convenience. Can be | |
425 * useful in orientation changes. | |
426 * | |
427 * @return <code>this</code> for convenience | |
428 * @since 2.0 | |
429 */ | |
430 public Dimension transpose() { | |
431 int temp = width; | |
432 width = height; | |
433 height = temp; | |
434 return this; | |
435 } | |
436 | |
437 /** | |
438 * Sets the width of this Dimension to the greater of this Dimension's width and | |
439 * <i>d</i>.width. Likewise for this Dimension's height. | |
440 * | |
441 * @param d the Dimension to union with this Dimension | |
442 * @return <code>this</code> for convenience | |
443 * @since 2.0 | |
444 */ | |
445 public Dimension union_ (Dimension d) { | |
446 width = Math.max(width, d.width); | |
447 height = Math.max(height, d.height); | |
448 return this; | |
449 } | |
450 | |
451 /** | |
452 * Returns <code>double</code> width | |
453 * | |
454 * @return <code>double</code> width | |
455 * @since 3.4 | |
456 */ | |
457 public double preciseWidth() { | |
458 return width; | |
459 } | |
460 | |
461 /** | |
462 * Returns <code>double</code> height | |
463 * | |
464 * @return <code>double</code> height | |
465 * @since 3.4 | |
466 */ | |
467 public double preciseHeight() { | |
468 return height; | |
469 } | |
470 | |
471 } |