Mercurial > projects > dwt-addons
annotate dwtx/jface/util/Geometry.d @ 192:c3583c6ec027
Added missing default cases for switch statements
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 03 Nov 2008 22:52:26 +0100 |
parents | 46a6e0e6ccd4 |
children |
rev | line source |
---|---|
4 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
2 * Copyright (c) 2004, 2007 IBM Corporation and others. |
4 | 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.jface.util.Geometry; | |
14 | |
15 import dwt.DWT; | |
16 import dwt.graphics.Point; | |
17 import dwt.graphics.Rectangle; | |
18 import dwt.widgets.Control; | |
19 | |
20 import dwt.dwthelper.utils; | |
21 | |
22 /** | |
23 * Contains static methods for performing simple geometric operations | |
24 * on the DWT geometry classes. | |
25 * | |
26 * @since 3.0 | |
27 */ | |
28 public class Geometry { | |
29 | |
30 /** | |
31 * Prevent this class from being instantiated. | |
32 * | |
33 * @since 3.0 | |
34 */ | |
35 private this() { | |
36 //This is not instantiated | |
37 } | |
38 | |
39 /** | |
40 * Returns the square of the distance between two points. | |
41 * <p>This is preferred over the real distance when searching | |
42 * for the closest point, since it avoids square roots.</p> | |
43 * | |
44 * @param p1 first endpoint | |
45 * @param p2 second endpoint | |
46 * @return the square of the distance between the two points | |
47 * | |
48 * @since 3.0 | |
49 */ | |
50 public static int distanceSquared(Point p1, Point p2) { | |
51 int term1 = p1.x - p2.x; | |
52 int term2 = p1.y - p2.y; | |
53 return term1 * term1 + term2 * term2; | |
54 } | |
55 | |
56 /** | |
57 * Returns the magnitude of the given 2d vector (represented as a Point) | |
58 * | |
59 * @param p point representing the 2d vector whose magnitude is being computed | |
60 * @return the magnitude of the given 2d vector | |
61 * @since 3.0 | |
62 */ | |
63 public static double magnitude(Point p) { | |
64 return Math.sqrt( cast(real) magnitudeSquared(p)); | |
65 } | |
66 | |
67 /** | |
68 * Returns the square of the magnitude of the given 2-space vector (represented | |
69 * using a point) | |
70 * | |
71 * @param p the point whose magnitude is being computed | |
72 * @return the square of the magnitude of the given vector | |
73 * @since 3.0 | |
74 */ | |
75 public static int magnitudeSquared(Point p) { | |
76 return p.x * p.x + p.y * p.y; | |
77 } | |
78 | |
79 /** | |
80 * Returns the dot product of the given vectors (expressed as Points) | |
81 * | |
82 * @param p1 the first vector | |
83 * @param p2 the second vector | |
84 * @return the dot product of the two vectors | |
85 * @since 3.0 | |
86 */ | |
87 public static int dotProduct(Point p1, Point p2) { | |
88 return p1.x * p2.x + p1.y * p2.y; | |
89 } | |
90 | |
91 /** | |
92 * Returns a new point whose coordinates are the minimum of the coordinates of the | |
93 * given points | |
94 * | |
95 * @param p1 a Point | |
96 * @param p2 a Point | |
97 * @return a new point whose coordinates are the minimum of the coordinates of the | |
98 * given points | |
99 * @since 3.0 | |
100 */ | |
101 public static Point min(Point p1, Point p2) { | |
102 return new Point(Math.min(p1.x, p2.x), Math.min(p1.y, p2.y)); | |
103 } | |
104 | |
105 /** | |
106 * Returns a new point whose coordinates are the maximum of the coordinates | |
107 * of the given points | |
108 * @param p1 a Point | |
109 * @param p2 a Point | |
110 * @return point a new point whose coordinates are the maximum of the coordinates | |
111 * @since 3.0 | |
112 */ | |
113 public static Point max(Point p1, Point p2) { | |
114 return new Point(Math.max(p1.x, p2.x), Math.max(p1.y, p2.y)); | |
115 } | |
116 | |
117 /** | |
118 * Returns a vector in the given direction with the given | |
119 * magnitude. Directions are given using DWT direction constants, and | |
120 * the resulting vector is in the screen's coordinate system. That is, | |
121 * the vector (0, 1) is down and the vector (1, 0) is right. | |
122 * | |
123 * @param distance magnitude of the vector | |
124 * @param direction one of DWT.TOP, DWT.BOTTOM, DWT.LEFT, or DWT.RIGHT | |
125 * @return a point representing a vector in the given direction with the given magnitude | |
126 * @since 3.0 | |
127 */ | |
128 public static Point getDirectionVector(int distance, int direction) { | |
129 switch (direction) { | |
130 case DWT.TOP: | |
131 return new Point(0, -distance); | |
132 case DWT.BOTTOM: | |
133 return new Point(0, distance); | |
134 case DWT.LEFT: | |
135 return new Point(-distance, 0); | |
136 case DWT.RIGHT: | |
137 return new Point(distance, 0); | |
192
c3583c6ec027
Added missing default cases for switch statements
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
138 default: |
4 | 139 } |
140 | |
141 return new Point(0, 0); | |
142 } | |
143 | |
144 /** | |
145 * Returns the point in the center of the given rectangle. | |
146 * | |
147 * @param rect rectangle being computed | |
148 * @return a Point at the center of the given rectangle. | |
149 * @since 3.0 | |
150 */ | |
151 public static Point centerPoint(Rectangle rect) { | |
152 return new Point(rect.x + rect.width / 2, rect.y + rect.height / 2); | |
153 } | |
154 | |
155 /** | |
156 * Returns a copy of the given point | |
157 * | |
158 * @param toCopy point to copy | |
159 * @return a copy of the given point | |
160 */ | |
161 public static Point copy(Point toCopy) { | |
162 return new Point(toCopy.x, toCopy.y); | |
163 } | |
164 | |
165 /** | |
166 * Sets result equal to toCopy | |
167 * | |
168 * @param result object that will be modified | |
169 * @param toCopy object that will be copied | |
170 * @since 3.1 | |
171 */ | |
172 public static void set(Point result, Point toCopy) { | |
173 result.x = toCopy.x; | |
174 result.y = toCopy.y; | |
175 } | |
176 | |
177 /** | |
178 * Sets result equal to toCopy | |
179 * | |
180 * @param result object that will be modified | |
181 * @param toCopy object that will be copied | |
182 * @since 3.1 | |
183 */ | |
184 public static void set(Rectangle result, Rectangle toCopy) { | |
185 result.x = toCopy.x; | |
186 result.y = toCopy.y; | |
187 result.width = toCopy.width; | |
188 result.height = toCopy.height; | |
189 } | |
190 | |
191 /** | |
192 * <p>Returns a new difference Rectangle whose x, y, width, and height are equal to the difference of the corresponding | |
193 * attributes from the given rectangles</p> | |
194 * | |
195 * <p></p> | |
196 * <b>Example: Compute the margins for a given Composite, and apply those same margins to a new GridLayout</b> | |
197 * | |
198 * <code><pre> | |
199 * // Compute the client area, in the coordinate system of the input composite's parent | |
200 * Rectangle clientArea = Display.getCurrent().map(inputComposite, | |
201 * inputComposite.getParent(), inputComposite.getClientArea()); | |
202 * | |
203 * // Compute the margins for a given Composite by subtracting the client area from the composite's bounds | |
204 * Rectangle margins = Geometry.subtract(inputComposite.getBounds(), clientArea); | |
205 * | |
206 * // Now apply these margins to a new GridLayout | |
207 * GridLayout layout = GridLayoutFactory.fillDefaults().margins(margins).create(); | |
208 * </pre></code> | |
209 * | |
210 * @param rect1 first rectangle | |
211 * @param rect2 rectangle to subtract | |
212 * @return the difference between the two rectangles (computed as rect1 - rect2) | |
213 * @since 3.3 | |
214 */ | |
215 public static Rectangle subtract(Rectangle rect1, Rectangle rect2) { | |
216 return new Rectangle(rect1.x - rect2.x, rect1.y - rect2.y, rect1.width - rect2.width, rect1.height - rect2.height); | |
217 } | |
218 | |
219 /** | |
220 * <p>Returns a new Rectangle whose x, y, width, and height is the sum of the x, y, width, and height values of | |
221 * both rectangles respectively.</p> | |
222 * | |
223 * @param rect1 first rectangle to add | |
224 * @param rect2 second rectangle to add | |
225 * @return a new rectangle whose x, y, height, and width attributes are the sum of the corresponding attributes from | |
226 * the arguments. | |
227 * @since 3.3 | |
228 */ | |
229 public static Rectangle add(Rectangle rect1, Rectangle rect2) { | |
230 return new Rectangle(rect1.x + rect2.x, rect1.y + rect2.y, | |
231 rect1.width + rect2.width, rect1.height + rect2.height); | |
232 } | |
233 | |
234 /** | |
235 * Adds two points as 2d vectors. Returns a new point whose coordinates are | |
236 * the sum of the original two points. | |
237 * | |
238 * @param point1 the first point (not null) | |
239 * @param point2 the second point (not null) | |
240 * @return a new point whose coordinates are the sum of the given points | |
241 * @since 3.0 | |
242 */ | |
243 public static Point add(Point point1, Point point2) { | |
244 return new Point(point1.x + point2.x, point1.y + point2.y); | |
245 } | |
246 | |
247 /** | |
248 * Divides both coordinates of the given point by the given scalar. | |
249 * | |
250 * @since 3.1 | |
251 * | |
252 * @param toDivide point to divide | |
253 * @param scalar denominator | |
254 * @return a new Point whose coordinates are equal to the original point divided by the scalar | |
255 */ | |
256 public static Point divide(Point toDivide, int scalar) { | |
257 return new Point(toDivide.x / scalar, toDivide.y / scalar); | |
258 } | |
259 | |
260 | |
261 /** | |
262 * Performs vector subtraction on two points. Returns a new point equal to | |
263 * (point1 - point2). | |
264 * | |
265 * @param point1 initial point | |
266 * @param point2 vector to subtract | |
267 * @return the difference (point1 - point2) | |
268 * @since 3.0 | |
269 */ | |
270 public static Point subtract(Point point1, Point point2) { | |
271 return new Point(point1.x - point2.x, point1.y - point2.y); | |
272 } | |
273 | |
274 /** | |
275 * Swaps the X and Y coordinates of the given point. | |
276 * | |
277 * @param toFlip modifies this point | |
278 * @since 3.1 | |
279 */ | |
280 public static void flipXY(Point toFlip) { | |
281 int temp = toFlip.x; | |
282 toFlip.x = toFlip.y; | |
283 toFlip.y = temp; | |
284 } | |
285 | |
286 /** | |
287 * Swaps the X and Y coordinates of the given rectangle, along with the height and width. | |
288 * | |
289 * @param toFlip modifies this rectangle | |
290 * @since 3.1 | |
291 */ | |
292 public static void flipXY(Rectangle toFlip) { | |
293 int temp = toFlip.x; | |
294 toFlip.x = toFlip.y; | |
295 toFlip.y = temp; | |
296 | |
297 temp = toFlip.width; | |
298 toFlip.width = toFlip.height; | |
299 toFlip.height = temp; | |
300 } | |
301 | |
302 /** | |
303 * Returns the height or width of the given rectangle. | |
304 * | |
305 * @param toMeasure rectangle to measure | |
306 * @param width returns the width if true, and the height if false | |
307 * @return the width or height of the given rectangle | |
308 * @since 3.0 | |
309 */ | |
310 public static int getDimension(Rectangle toMeasure, bool width) { | |
311 if (width) { | |
312 return toMeasure.width; | |
313 } | |
314 return toMeasure.height; | |
315 } | |
316 | |
317 /** | |
318 * Returns the x or y coordinates of the given point. | |
319 * | |
320 * @param toMeasure point being measured | |
321 * @param width if true, returns x. Otherwise, returns y. | |
322 * @return the x or y coordinate | |
323 * @since 3.1 | |
324 */ | |
325 public static int getCoordinate(Point toMeasure, bool width) { | |
326 return width ? toMeasure.x : toMeasure.y; | |
327 } | |
328 | |
329 /** | |
330 * Returns the x or y coordinates of the given rectangle. | |
331 * | |
332 * @param toMeasure rectangle being measured | |
333 * @param width if true, returns x. Otherwise, returns y. | |
334 * @return the x or y coordinate | |
335 * @since 3.1 | |
336 */ | |
337 public static int getCoordinate(Rectangle toMeasure, bool width) { | |
338 return width ? toMeasure.x : toMeasure.y; | |
339 } | |
340 | |
341 /** | |
342 * Sets one dimension of the given rectangle. Modifies the given rectangle. | |
343 * | |
344 * @param toSet rectangle to modify | |
345 * @param width if true, the width is modified. If false, the height is modified. | |
346 * @param newCoordinate new value of the width or height | |
347 * @since 3.1 | |
348 */ | |
349 public static void setDimension(Rectangle toSet, bool width, int newCoordinate) { | |
350 if (width) { | |
351 toSet.width = newCoordinate; | |
352 } else { | |
353 toSet.height = newCoordinate; | |
354 } | |
355 } | |
356 | |
357 /** | |
358 * Sets one coordinate of the given rectangle. Modifies the given rectangle. | |
359 * | |
360 * @param toSet rectangle to modify | |
361 * @param width if true, the x coordinate is modified. If false, the y coordinate is modified. | |
362 * @param newCoordinate new value of the x or y coordinates | |
363 * @since 3.1 | |
364 */ | |
365 public static void setCoordinate(Rectangle toSet, bool width, int newCoordinate) { | |
366 if (width) { | |
367 toSet.x = newCoordinate; | |
368 } else { | |
369 toSet.y = newCoordinate; | |
370 } | |
371 } | |
372 | |
373 /** | |
374 * Sets one coordinate of the given point. Modifies the given point. | |
375 * | |
376 * @param toSet point to modify | |
377 * @param width if true, the x coordinate is modified. If false, the y coordinate is modified. | |
378 * @param newCoordinate new value of the x or y coordinates | |
379 * @since 3.1 | |
380 */ | |
381 public static void setCoordinate(Point toSet, bool width, int newCoordinate) { | |
382 if (width) { | |
383 toSet.x = newCoordinate; | |
384 } else { | |
385 toSet.y = newCoordinate; | |
386 } | |
387 } | |
388 | |
389 /** | |
390 * Returns the distance of the given point from a particular side of the given rectangle. | |
391 * Returns negative values for points outside the rectangle. | |
392 * | |
393 * @param rectangle a bounding rectangle | |
394 * @param testPoint a point to test | |
395 * @param edgeOfInterest side of the rectangle to test against | |
396 * @return the distance of the given point from the given edge of the rectangle | |
397 * @since 3.0 | |
398 */ | |
399 public static int getDistanceFromEdge(Rectangle rectangle, Point testPoint, | |
400 int edgeOfInterest) { | |
401 switch (edgeOfInterest) { | |
402 case DWT.TOP: | |
403 return testPoint.y - rectangle.y; | |
404 case DWT.BOTTOM: | |
405 return rectangle.y + rectangle.height - testPoint.y; | |
406 case DWT.LEFT: | |
407 return testPoint.x - rectangle.x; | |
408 case DWT.RIGHT: | |
409 return rectangle.x + rectangle.width - testPoint.x; | |
192
c3583c6ec027
Added missing default cases for switch statements
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
410 default: |
4 | 411 } |
412 | |
413 return 0; | |
414 } | |
415 | |
416 /** | |
417 * Extrudes the given edge inward by the given distance. That is, if one side of the rectangle | |
418 * was sliced off with a given thickness, this returns the rectangle that forms the slice. Note | |
419 * that the returned rectangle will be inside the given rectangle if size > 0. | |
420 * | |
421 * @param toExtrude the rectangle to extrude. The resulting rectangle will share three sides | |
422 * with this rectangle. | |
423 * @param size distance to extrude. A negative size will extrude outwards (that is, the resulting | |
424 * rectangle will overlap the original iff this is positive). | |
425 * @param orientation the side to extrude. One of DWT.LEFT, DWT.RIGHT, DWT.TOP, or DWT.BOTTOM. The | |
426 * resulting rectangle will always share this side with the original rectangle. | |
427 * @return a rectangle formed by extruding the given side of the rectangle by the given distance. | |
428 * @since 3.0 | |
429 */ | |
430 public static Rectangle getExtrudedEdge(Rectangle toExtrude, int size, | |
431 int orientation) { | |
432 Rectangle bounds = new Rectangle(toExtrude.x, toExtrude.y, | |
433 toExtrude.width, toExtrude.height); | |
434 | |
435 if (!isHorizontal(orientation)) { | |
436 bounds.width = size; | |
437 } else { | |
438 bounds.height = size; | |
439 } | |
440 | |
441 switch (orientation) { | |
442 case DWT.RIGHT: | |
443 bounds.x = toExtrude.x + toExtrude.width - bounds.width; | |
444 break; | |
445 case DWT.BOTTOM: | |
446 bounds.y = toExtrude.y + toExtrude.height - bounds.height; | |
447 break; | |
192
c3583c6ec027
Added missing default cases for switch statements
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
448 default: |
4 | 449 } |
450 | |
451 normalize(bounds); | |
452 | |
453 return bounds; | |
454 } | |
455 | |
456 /** | |
457 * Returns the opposite of the given direction. That is, returns DWT.LEFT if | |
458 * given DWT.RIGHT and visa-versa. | |
459 * | |
460 * @param swtDirectionConstant one of DWT.LEFT, DWT.RIGHT, DWT.TOP, or DWT.BOTTOM | |
461 * @return one of DWT.LEFT, DWT.RIGHT, DWT.TOP, or DWT.BOTTOM | |
462 * @since 3.0 | |
463 */ | |
464 public static int getOppositeSide(int swtDirectionConstant) { | |
465 switch (swtDirectionConstant) { | |
466 case DWT.TOP: | |
467 return DWT.BOTTOM; | |
468 case DWT.BOTTOM: | |
469 return DWT.TOP; | |
470 case DWT.LEFT: | |
471 return DWT.RIGHT; | |
472 case DWT.RIGHT: | |
473 return DWT.LEFT; | |
192
c3583c6ec027
Added missing default cases for switch statements
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
474 default: |
4 | 475 } |
476 | |
477 return swtDirectionConstant; | |
478 } | |
479 | |
480 /** | |
481 * Converts the given bool into an DWT orientation constant. | |
482 * | |
483 * @param horizontal if true, returns DWT.HORIZONTAL. If false, returns DWT.VERTICAL | |
484 * @return DWT.HORIZONTAL or DWT.VERTICAL. | |
485 * @since 3.0 | |
486 */ | |
487 public static int getSwtHorizontalOrVerticalConstant(bool horizontal) { | |
488 if (horizontal) { | |
489 return DWT.HORIZONTAL; | |
490 } | |
491 return DWT.VERTICAL; | |
492 } | |
493 | |
494 /** | |
495 * Returns true iff the given DWT side constant corresponds to a horizontal side | |
496 * of a rectangle. That is, returns true for the top and bottom but false for the | |
497 * left and right. | |
498 * | |
499 * @param swtSideConstant one of DWT.TOP, DWT.BOTTOM, DWT.LEFT, or DWT.RIGHT | |
500 * @return true iff the given side is horizontal. | |
501 * @since 3.0 | |
502 */ | |
503 public static bool isHorizontal(int swtSideConstant) { | |
504 return !(swtSideConstant is DWT.LEFT || swtSideConstant is DWT.RIGHT); | |
505 } | |
506 | |
507 /** | |
508 * Moves the given rectangle by the given delta. | |
509 * | |
510 * @param rect rectangle to move (will be modified) | |
511 * @param delta direction vector to move the rectangle by | |
512 * @since 3.0 | |
513 */ | |
514 public static void moveRectangle(Rectangle rect, Point delta) { | |
515 rect.x += delta.x; | |
516 rect.y += delta.y; | |
517 } | |
518 | |
519 /** | |
520 * Moves each edge of the given rectangle outward by the given amount. Negative values | |
521 * cause the rectangle to contract. Does not allow the rectangle's width or height to be | |
522 * reduced below zero. | |
523 * | |
524 * @param rect normalized rectangle to modify | |
525 * @param differenceRect difference rectangle to be added to rect | |
526 * @since 3.3 | |
527 */ | |
528 public static void expand(Rectangle rect, Rectangle differenceRect) { | |
529 rect.x += differenceRect.x; | |
530 rect.y += differenceRect.y; | |
531 rect.height = Math.max(0, rect.height + differenceRect.height); | |
532 rect.width = Math.max(0, rect.width + differenceRect.width); | |
533 } | |
534 | |
535 /** | |
536 * <p>Returns a rectangle which, when added to another rectangle, will expand each side | |
537 * by the given number of units.</p> | |
538 * | |
539 * <p>This is commonly used to store margin sizes. For example:</p> | |
540 * | |
541 * <code><pre> | |
542 * // Expands the left, right, top, and bottom | |
543 * // of the given control by 10, 5, 1, and 15 units respectively | |
544 * | |
545 * Rectangle margins = Geometry.createDifferenceRect(10,5,1,15); | |
546 * Rectangle bounds = someControl.getBounds(); | |
547 * someControl.setBounds(Geometry.add(bounds, margins)); | |
548 * </pre></code> | |
549 * | |
550 * @param left distance to expand the left side (negative values move the edge inward) | |
551 * @param right distance to expand the right side (negative values move the edge inward) | |
552 * @param top distance to expand the top (negative values move the edge inward) | |
553 * @param bottom distance to expand the bottom (negative values move the edge inward) | |
554 * | |
555 * @return a difference rectangle that, when added to another rectangle, will cause each | |
556 * side to expand by the given number of units | |
557 * @since 3.3 | |
558 */ | |
559 public static Rectangle createDiffRectangle(int left, int right, int top, int bottom) { | |
560 return new Rectangle(-left, -top, left + right, top + bottom); | |
561 } | |
562 | |
563 /** | |
564 * Moves each edge of the given rectangle outward by the given amount. Negative values | |
565 * cause the rectangle to contract. Does not allow the rectangle's width or height to be | |
566 * reduced below zero. | |
567 * | |
568 * @param rect normalized rectangle to modify | |
569 * @param left distance to move the left edge outward (negative values move the edge inward) | |
570 * @param right distance to move the right edge outward (negative values move the edge inward) | |
571 * @param top distance to move the top edge outward (negative values move the edge inward) | |
572 * @param bottom distance to move the bottom edge outward (negative values move the edge inward) | |
573 * @since 3.1 | |
574 */ | |
575 public static void expand(Rectangle rect, int left, int right, int top, int bottom) { | |
576 rect.x -= left; | |
577 rect.width = Math.max(0, rect.width + left + right); | |
578 rect.y -= top; | |
579 rect.height = Math.max(0, rect.height + top + bottom); | |
580 } | |
581 | |
582 /** | |
583 * Normalizes the given rectangle. That is, any rectangle with | |
584 * negative width or height becomes a rectangle with positive | |
585 * width or height that extends to the upper-left of the original | |
586 * rectangle. | |
587 * | |
588 * @param rect rectangle to modify | |
589 * @since 3.0 | |
590 */ | |
591 public static void normalize(Rectangle rect) { | |
592 if (rect.width < 0) { | |
593 rect.width = -rect.width; | |
594 rect.x -= rect.width; | |
595 } | |
596 | |
597 if (rect.height < 0) { | |
598 rect.height = -rect.height; | |
599 rect.y -= rect.height; | |
600 } | |
601 } | |
602 | |
603 /** | |
604 * Converts the given rectangle from display coordinates to the local coordinate system | |
605 * of the given object into display coordinates. | |
606 * | |
607 * @param coordinateSystem local coordinate system being converted to | |
608 * @param toConvert rectangle to convert | |
609 * @return a rectangle in control coordinates | |
610 * @since 3.0 | |
611 */ | |
612 public static Rectangle toControl(Control coordinateSystem, | |
613 Rectangle toConvert) { | |
614 return(coordinateSystem.getDisplay().map | |
615 (null,coordinateSystem,toConvert)); | |
616 } | |
617 | |
618 /** | |
619 * Converts the given rectangle from the local coordinate system of the given object | |
620 * into display coordinates. | |
621 * | |
622 * @param coordinateSystem local coordinate system being converted from | |
623 * @param toConvert rectangle to convert | |
624 * @return a rectangle in display coordinates | |
625 * @since 3.0 | |
626 */ | |
627 public static Rectangle toDisplay(Control coordinateSystem, | |
628 Rectangle toConvert) { | |
629 return(coordinateSystem.getDisplay().map | |
630 (coordinateSystem,null,toConvert)); | |
631 | |
632 } | |
633 | |
634 /** | |
635 * Determines where the given point lies with respect to the given rectangle. | |
636 * Returns a combination of DWT.LEFT, DWT.RIGHT, DWT.TOP, and DWT.BOTTOM, combined | |
637 * with bitwise or (for example, returns DWT.TOP | DWT.LEFT if the point is to the | |
638 * upper-left of the rectangle). Returns 0 if the point lies within the rectangle. | |
639 * Positions are in screen coordinates (ie: a point is to the upper-left of the | |
640 * rectangle if its x and y coordinates are smaller than any point in the rectangle) | |
641 * | |
642 * @param boundary normalized boundary rectangle | |
643 * @param toTest point whose relative position to the rectangle is being computed | |
644 * @return one of DWT.LEFT | DWT.TOP, DWT.TOP, DWT.RIGHT | DWT.TOP, DWT.LEFT, 0, | |
645 * DWT.RIGHT, DWT.LEFT | DWT.BOTTOM, DWT.BOTTOM, DWT.RIGHT | DWT.BOTTOM | |
646 * @since 3.0 | |
647 */ | |
648 public static int getRelativePosition(Rectangle boundary, Point toTest) { | |
649 int result = 0; | |
650 | |
651 if (toTest.x < boundary.x) { | |
652 result |= DWT.LEFT; | |
653 } else if (toTest.x >= boundary.x + boundary.width) { | |
654 result |= DWT.RIGHT; | |
655 } | |
656 | |
657 if (toTest.y < boundary.y) { | |
658 result |= DWT.TOP; | |
659 } else if (toTest.y >= boundary.y + boundary.height) { | |
660 result |= DWT.BOTTOM; | |
661 } | |
662 | |
663 return result; | |
664 } | |
665 | |
666 /** | |
667 * Returns the distance from the point to the nearest edge of the given | |
668 * rectangle. Returns negative values if the point lies outside the rectangle. | |
669 * | |
670 * @param boundary rectangle to test | |
671 * @param toTest point to test | |
672 * @return the distance between the given point and the nearest edge of the rectangle. | |
673 * Returns positive values for points inside the rectangle and negative values for points | |
674 * outside the rectangle. | |
675 * @since 3.1 | |
676 */ | |
677 public static int getDistanceFrom(Rectangle boundary, Point toTest) { | |
678 int side = getClosestSide(boundary, toTest); | |
679 return getDistanceFromEdge(boundary, toTest, side); | |
680 } | |
681 | |
682 /** | |
683 * Returns the edge of the given rectangle is closest to the given | |
684 * point. | |
685 * | |
686 * @param boundary rectangle to test | |
687 * @param toTest point to compare | |
688 * @return one of DWT.LEFT, DWT.RIGHT, DWT.TOP, or DWT.BOTTOM | |
689 * | |
690 * @since 3.0 | |
691 */ | |
692 public static int getClosestSide(Rectangle boundary, Point toTest) { | |
693 int[] sides = [ DWT.LEFT, DWT.RIGHT, DWT.TOP, DWT.BOTTOM ]; | |
694 | |
695 int closestSide = DWT.LEFT; | |
696 int closestDistance = Integer.MAX_VALUE; | |
697 | |
698 for (int idx = 0; idx < sides.length; idx++) { | |
699 int side = sides[idx]; | |
700 | |
701 int distance = getDistanceFromEdge(boundary, toTest, side); | |
702 | |
703 if (distance < closestDistance) { | |
704 closestDistance = distance; | |
705 closestSide = side; | |
706 } | |
707 } | |
708 | |
709 return closestSide; | |
710 } | |
711 | |
712 /** | |
713 * Returns a copy of the given rectangle | |
714 * | |
715 * @param toCopy rectangle to copy | |
716 * @return a copy of the given rectangle | |
717 * @since 3.0 | |
718 */ | |
719 public static Rectangle copy(Rectangle toCopy) { | |
720 return new Rectangle(toCopy.x, toCopy.y, toCopy.width, toCopy.height); | |
721 } | |
722 | |
723 /** | |
724 * Returns the size of the rectangle, as a Point | |
725 * | |
726 * @param rectangle rectangle whose size is being computed | |
727 * @return the size of the given rectangle | |
728 * @since 3.0 | |
729 */ | |
730 public static Point getSize(Rectangle rectangle) { | |
731 return new Point(rectangle.width, rectangle.height); | |
732 } | |
733 | |
734 /** | |
735 * Sets the size of the given rectangle to the given size | |
736 * | |
737 * @param rectangle rectangle to modify | |
738 * @param newSize new size of the rectangle | |
739 * @since 3.0 | |
740 */ | |
741 public static void setSize(Rectangle rectangle, Point newSize) { | |
742 rectangle.width = newSize.x; | |
743 rectangle.height = newSize.y; | |
744 } | |
745 | |
746 /** | |
747 * Sets the x,y position of the given rectangle. For a normalized | |
748 * rectangle (a rectangle with positive width and height), this will | |
749 * be the upper-left corner of the rectangle. | |
750 * | |
751 * @param rectangle rectangle to modify | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
752 * @param newLocation new location of the rectangle |
4 | 753 * |
754 * @since 3.0 | |
755 */ | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
756 public static void setLocation(Rectangle rectangle, Point newLocation) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
757 rectangle.x = newLocation.x; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
758 rectangle.y = newLocation.y; |
4 | 759 } |
760 | |
761 /** | |
762 * Returns the x,y position of the given rectangle. For normalized rectangles | |
763 * (rectangles with positive width and height), this is the upper-left | |
764 * corner of the rectangle. | |
765 * | |
766 * @param toQuery rectangle to query | |
767 * @return a Point containing the x,y position of the rectangle | |
768 * | |
769 * @since 3.0 | |
770 */ | |
771 public static Point getLocation(Rectangle toQuery) { | |
772 return new Point(toQuery.x, toQuery.y); | |
773 } | |
774 | |
775 /** | |
776 * Returns a new rectangle with the given position and dimensions, expressed | |
777 * as points. | |
778 * | |
779 * @param position the (x,y) position of the rectangle | |
780 * @param size the size of the new rectangle, where (x,y) -> (width, height) | |
781 * @return a new Rectangle with the given position and size | |
782 * | |
783 * @since 3.0 | |
784 */ | |
785 public static Rectangle createRectangle(Point position, Point size) { | |
786 return new Rectangle(position.x, position.y, size.x, size.y); | |
787 } | |
788 | |
789 /** | |
790 * Repositions the 'inner' rectangle to lie completely within the bounds of the 'outer' | |
791 * rectangle if possible. One use for this is to ensure that, when setting a control's bounds, | |
792 * that they will always lie within its parent's client area (to avoid clipping). | |
793 * | |
794 * @param inner The 'inner' rectangle to be repositioned (should be smaller than the 'outer' rectangle) | |
795 * @param outer The 'outer' rectangle | |
796 */ | |
797 public static void moveInside(Rectangle inner, Rectangle outer) { | |
798 // adjust X | |
799 if (inner.x < outer.x) { | |
800 inner.x = outer.x; | |
801 } | |
802 if ((inner.x + inner.width) > (outer.x + outer.width)) { | |
803 inner.x -= (inner.x + inner.width) - (outer.x + outer.width); | |
804 } | |
805 | |
806 // Adjust Y | |
807 if (inner.y < outer.y) { | |
808 inner.y = outer.y; | |
809 } | |
810 if ((inner.y + inner.height) > (outer.y + outer.height)) { | |
811 inner.y -= (inner.y + inner.height) - (outer.y + outer.height); | |
812 } | |
813 } | |
814 | |
815 } |