comparison org.eclipse.draw2d/src/org/eclipse/draw2d/RoutingAnimator.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 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
14 module org.eclipse.draw2d.RoutingAnimator;
15
16 import java.lang.all;
17
18 import org.eclipse.draw2d.geometry.Point;
19 import org.eclipse.draw2d.geometry.PointList;
20 import org.eclipse.draw2d.Animator;
21 import org.eclipse.draw2d.RoutingListener;
22 import org.eclipse.draw2d.IFigure;
23 import org.eclipse.draw2d.Connection;
24 import org.eclipse.draw2d.Animation;
25
26 /**
27 * Animates the routing of a connection. The animator will capture the effects of the
28 * connection's router, and the play back the placement of the routing, interpolating the
29 * intermediate routes.
30 * <P>
31 * To use a routing animator, hook it as a routing listener for the connection whose
32 * points are to be animated, by calling {@link
33 * PolylineConnection#addRoutingListener(RoutingListener)}. An animator is active
34 * only when the Animation utility is activated.
35 *
36 * @since 3.2
37 */
38 public class RoutingAnimator : Animator , RoutingListener {
39
40 private static RoutingAnimator INSTANCE_;
41 static RoutingAnimator INSTANCE(){
42 if( INSTANCE_ is null ){
43 synchronized( RoutingAnimator.classinfo ){
44 if( INSTANCE_ is null ){
45 INSTANCE_ = new RoutingAnimator();
46 }
47 }
48 }
49 return INSTANCE_;
50 }
51
52 /**
53 * Constructs a routing animator for use with one or more connections. The default
54 * instance ({@link #getDefault()} can be used on any number of connections.
55 *
56 * @since 3.2
57 */
58 protected this() { }
59
60 /**
61 * Overridden to sync initial and final states.
62 * @see Animator#playbackStarting(IFigure)
63 */
64 public void playbackStarting(IFigure connection) {
65 reconcileStates(cast(Connection)connection);
66 }
67
68 /**
69 * Returns the current state of the connection. Currently, this is a copy of the list of
70 * points. However this Object could change in future releases and should not be
71 * considered API.
72 * @see Animator#getCurrentState(IFigure)
73 */
74 protected Object getCurrentState(IFigure connection) {
75 return (cast(Connection)connection).getPoints().getCopy();
76 }
77
78 /**
79 * Returns the default instance.
80 * @return the default instance
81 * @since 3.2
82 */
83 public static RoutingAnimator getDefault() {
84 return INSTANCE;
85 }
86
87 /**
88 * Hooks invalidate for animation purposes.
89 * @see RoutingListener#invalidate(Connection)
90 */
91 public final void invalidate(Connection conn) {
92 if (Animation.isInitialRecording())
93 Animation.hookAnimator(conn, this);
94 }
95
96 /**
97 * Plays back the interpolated state.
98 * @see Animator#playback(IFigure)
99 */
100 protected bool playback(IFigure figure) {
101 Connection conn = cast(Connection) figure;
102
103 PointList list1 = cast(PointList)Animation.getInitialState(this, conn);
104 PointList list2 = cast(PointList)Animation.getFinalState(this, conn);
105 if (list1 is null) {
106 conn.setVisible(false);
107 return true;
108 }
109
110 float progress = Animation.getProgress();
111 if (list1.size() is list2.size()) {
112 Point pt1 = new Point(), pt2 = new Point();
113 PointList points = conn.getPoints();
114 points.removeAllPoints();
115 for (int i = 0; i < list1.size(); i++) {
116 list1.getPoint(pt2, i);
117 list2.getPoint(pt1, i);
118 pt1.x = cast(int)Math.round(pt1.x * progress + (1 - progress) * pt2.x);
119 pt1.y = cast(int)Math.round(pt1.y * progress + (1 - progress) * pt2.y);
120 points.addPoint(pt1);
121 }
122 conn.setPoints(points);
123 }
124 return true;
125 }
126
127 /**
128 * Hooks post routing for animation purposes.
129 * @see RoutingListener#postRoute(Connection)
130 */
131 public final void postRoute(Connection connection) {
132 if (Animation.isFinalRecording())
133 Animation.hookNeedsCapture(connection, this);
134 }
135
136 private void reconcileStates(Connection conn) {
137 PointList points1 = cast(PointList)Animation.getInitialState(this, conn);
138 PointList points2 = cast(PointList)Animation.getFinalState(this, conn);
139
140 if (points1 !is null && points1.size() !is points2.size()) {
141 Point p = new Point(), q = new Point();
142
143 int size1 = points1.size() - 1;
144 int size2 = points2.size() - 1;
145
146 int i1 = size1;
147 int i2 = size2;
148
149 double current1 = 1.0;
150 double current2 = 1.0;
151
152 double prev1 = 1.0;
153 double prev2 = 1.0;
154
155 while (i1 > 0 || i2 > 0) {
156 if (Math.abs(current1 - current2) < 0.1
157 && i1 > 0 && i2 > 0) {
158 //Both points are the same, use them and go on;
159 prev1 = current1;
160 prev2 = current2;
161 i1--;
162 i2--;
163 current1 = cast(double)i1 / size1;
164 current2 = cast(double)i2 / size2;
165 } else if (current1 < current2) {
166 //2 needs to catch up
167 // current1 < current2 < prev1
168 points1.getPoint(p, i1);
169 points1.getPoint(q, i1 + 1);
170
171 p.x = cast(int)(((q.x * (current2 - current1) + p.x * (prev1 - current2))
172 / (prev1 - current1)));
173 p.y = cast(int)(((q.y * (current2 - current1) + p.y * (prev1 - current2))
174 / (prev1 - current1)));
175
176 points1.insertPoint(p, i1 + 1);
177
178 prev1 = prev2 = current2;
179 i2--;
180 current2 = cast(double)i2 / size2;
181
182 } else {
183 //1 needs to catch up
184 // current2< current1 < prev2
185
186 points2.getPoint(p, i2);
187 points2.getPoint(q, i2 + 1);
188
189 p.x = cast(int)(((q.x * (current1 - current2) + p.x * (prev2 - current1))
190 / (prev2 - current2)));
191 p.y = cast(int)(((q.y * (current1 - current2) + p.y * (prev2 - current1))
192 / (prev2 - current2)));
193
194 points2.insertPoint(p, i2 + 1);
195
196 prev2 = prev1 = current1;
197 i1--;
198 current1 = cast(double)i1 / size1;
199 }
200 }
201 }
202 }
203
204 /**
205 * This callback is unused. Reserved for possible future use.
206 * @see RoutingListener#remove(Connection)
207 */
208 public final void remove(Connection connection) { }
209
210 /**
211 * Hooks route to intercept routing during animation playback.
212 * @see RoutingListener#route(Connection)
213 */
214 public final bool route(Connection conn) {
215 return Animation.isAnimating() && Animation.hookPlayback(conn, this);
216 }
217
218 /**
219 * This callback is unused. Reserved for possible future use.
220 * @see RoutingListener#setConstraint(Connection, Object)
221 */
222 public final void setConstraint(Connection connection, Object constraint) { }
223
224 }