comparison dwtx/draw2d/PolygonDecoration.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 2d6540440fe6
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.PolygonDecoration;
14
15 import dwt.dwthelper.utils;
16
17
18
19 import dwt.graphics.Color;
20 import dwtx.draw2d.geometry.Point;
21 import dwtx.draw2d.geometry.PointList;
22 import dwtx.draw2d.geometry.Transform;
23 import dwtx.draw2d.RotatableDecoration;
24 import dwtx.draw2d.Polygon;
25
26 /**
27 * A rotatable, polygon shaped decoration most commonly used for decorating the ends of
28 * {@link dwtx.draw2d.Polyline polylines}.
29 */
30 public class PolygonDecoration
31 : Polygon
32 , RotatableDecoration
33 {
34
35 /** Template for a triangle that points to the right when the rotation angle is 0 */
36 public static const PointList TRIANGLE_TIP;
37 /** Template for a triangle that points to the left when the rotation angle is 0 */
38 public static const PointList INVERTED_TRIANGLE_TIP;
39
40 static this() {
41 TRIANGLE_TIP = new PointList();
42 INVERTED_TRIANGLE_TIP = new PointList();
43 TRIANGLE_TIP.addPoint(0, 0);
44 TRIANGLE_TIP.addPoint(-1, 1);
45 TRIANGLE_TIP.addPoint(-1, -1);
46
47 INVERTED_TRIANGLE_TIP.addPoint(0, 1);
48 INVERTED_TRIANGLE_TIP.addPoint(0, -1);
49 INVERTED_TRIANGLE_TIP.addPoint(-1, 0);
50 }
51
52 private Point location;
53 private PointList template_;
54 private Transform transform;
55
56 /**
57 * Constructs a PolygonDecoration. Defaults the PolygonDecoration to fill its region
58 * with black.
59 *
60 * @since 2.0
61 */
62 public this() {
63 location = new Point();
64 template_ = TRIANGLE_TIP;
65 transform = new Transform();
66 setFill(true);
67 setScale(7, 3);
68 }
69
70 /**
71 * @see dwtx.draw2d.IFigure#getBackgroundColor()
72 */
73 public Color getLocalBackgroundColor() {
74 if (super.getLocalBackgroundColor() is null)
75 return getForegroundColor();
76 return super.getLocalBackgroundColor();
77 }
78
79 /**
80 * Returns the points in the PolygonDecoration as a PointList.
81 *
82 * @return the points in this PolygonDecoration
83 * @since 2.0
84 */
85 public PointList getPoints() {
86 if (points is null) {
87 points = new PointList();
88 for (int i = 0; i < template_.size(); i++)
89 points.addPoint(transform.getTransformed(template_.getPoint(i)));
90 }
91 return points;
92 }
93
94 /**
95 * Sets the location of this PolygonDecoration.
96 * @param p the new location
97 */
98 public void setLocation(Point p) {
99 points = null;
100 bounds = null;
101 location.setLocation(p);
102 transform.setTranslation(p.x, p.y);
103 }
104
105 /**
106 * Sets the PolygonDecorations point template_ to the passed PointList. This template_ is an
107 * outline of the PolygonDecoration's region. (The default value is TRIANGLE_TIP which is
108 * a triangle whose tip is pointing to the right).
109 *
110 * @param pl the PointList outline to use as the PolygonDecoration's region
111 * @since 2.0
112 */
113 public void setTemplate(PointList pl) {
114 erase();
115 template_ = pl;
116 points = null;
117 bounds = null;
118 repaint();
119 }
120
121 /**
122 * Sets the amount of scaling to be done along X and Y axes on the PolygonDecoration's
123 * template_.
124 *
125 * @param x X scaling
126 * @param y Y scaling
127 * @since 2.0
128 */
129 public void setScale(double x, double y) {
130 points = null;
131 bounds = null;
132 transform.setScale(x, y);
133 }
134
135 /**
136 * Sets the rotation of this decoration so that the decoration points toward the
137 * given reference point.
138 * @param ref the reference point
139 */
140 public void setReferencePoint(Point ref_) {
141 Point pt = Point.SINGLETON;
142 pt.setLocation(ref_);
143 pt.negate().translate(location);
144 setRotation(Math.atan2(pt.y, pt.x));
145 }
146
147 /**
148 * Sets the angle by which rotation is to be done on the PolygonDecoration.
149 *
150 * @param angle Angle of rotation
151 * @since 2.0
152 */
153 public void setRotation(double angle) {
154 points = null;
155 bounds = null;
156 transform.setRotation(angle);
157 }
158
159 }