comparison dwtx/draw2d/ChopboxAnchor.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
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 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.ChopboxAnchor;
14
15 import dwt.dwthelper.utils;
16
17 import dwtx.draw2d.geometry.Point;
18 import dwtx.draw2d.geometry.Rectangle;
19 import dwtx.draw2d.IFigure;
20 import dwtx.draw2d.AbstractConnectionAnchor;
21
22 /**
23 * The ChopboxAnchor's location is found by calculating the intersection of a line drawn
24 * from the center point of its owner's box to a reference point on that box. Thus
25 * {@link Connection Connections} using the ChopBoxAnchor will be oriented such that they
26 * point to their owner's center.
27 */
28 public class ChopboxAnchor
29 : AbstractConnectionAnchor
30 {
31
32 /**
33 * Constructs a new ChopboxAnchor.
34 */
35 protected this() { }
36
37 /**
38 * Constructs a ChopboxAnchor with the given <i>owner</i> figure.
39 *
40 * @param owner The owner figure
41 * @since 2.0
42 */
43 public this(IFigure owner) {
44 super(owner);
45 }
46
47 /**
48 * Gets a Rectangle from {@link #getBox()} and returns the Point where a line from the
49 * center of the Rectangle to the Point <i>reference</i> intersects the Rectangle.
50 *
51 * @param reference The reference point
52 * @return The anchor location
53 */
54 public Point getLocation(Point reference) {
55 Rectangle r = Rectangle.SINGLETON;
56 r.setBounds(getBox());
57 r.translate(-1, -1);
58 r.resize(1, 1);
59
60 getOwner().translateToAbsolute(r);
61 float centerX = r.x + 0.5f * r.width;
62 float centerY = r.y + 0.5f * r.height;
63
64 if (r.isEmpty() || (reference.x is cast(int)centerX && reference.y is cast(int)centerY))
65 return new Point(cast(int)centerX, cast(int)centerY); //This avoids divide-by-zero
66
67 float dx = reference.x - centerX;
68 float dy = reference.y - centerY;
69
70 //r.width, r.height, dx, and dy are guaranteed to be non-zero.
71 float scale = 0.5f / Math.max(Math.abs(dx) / r.width, Math.abs(dy) / r.height);
72
73 dx *= scale;
74 dy *= scale;
75 centerX += dx;
76 centerY += dy;
77
78 return new Point(Math.round(centerX), Math.round(centerY));
79 }
80
81 /**
82 * Returns the bounds of this ChopboxAnchor's owner. Subclasses can override this method
83 * to adjust the box the anchor can be placed on. For instance, the owner figure may have
84 * a drop shadow that should not be included in the box.
85 *
86 * @return The bounds of this ChopboxAnchor's owner
87 * @since 2.0
88 */
89 protected Rectangle getBox() {
90 return getOwner().getBounds();
91 }
92
93 /**
94 * Returns the anchor's reference point. In the case of the ChopboxAnchor, this is the
95 * center of the anchor's owner.
96 *
97 * @return The reference point
98 */
99 public Point getReferencePoint() {
100 Point ref_ = getBox().getCenter();
101 getOwner().translateToAbsolute(ref_);
102 return ref_;
103 }
104
105
106 /**
107 * Returns <code>true</code> if the other anchor has the same owner and box.
108 * @param obj the other anchor
109 * @return <code>true</code> if equal
110 */
111 public override int opEquals(Object obj) {
112 if (auto other = cast(ChopboxAnchor)obj ) {
113 return other.getOwner() is getOwner() && other.getBox().opEquals(getBox());
114 }
115 return false;
116 }
117
118 /**
119 * The owning figure's hashcode is used since equality is approximately based on the
120 * owner.
121 * @return the hash code.
122 */
123 public override hash_t toHash() {
124 if (getOwner() !is null)
125 return (cast(Object)getOwner()).toHash();
126 else
127 return super.toHash();
128 }
129
130 }