1 package org.opentrafficsim.draw.road;
2
3 import java.awt.Color;
4 import java.awt.Graphics2D;
5 import java.awt.geom.Path2D;
6 import java.awt.image.ImageObserver;
7 import java.rmi.RemoteException;
8 import java.util.function.Supplier;
9
10 import javax.naming.NamingException;
11
12 import org.djutils.base.Identifiable;
13 import org.djutils.draw.line.PolyLine2d;
14 import org.djutils.draw.point.OrientedPoint2d;
15 import org.opentrafficsim.base.geometry.BoundingPolygon;
16 import org.opentrafficsim.base.geometry.ClickableBounds;
17 import org.opentrafficsim.base.geometry.OtsBounds2d;
18 import org.opentrafficsim.base.geometry.OtsLocatable;
19 import org.opentrafficsim.base.geometry.OtsRenderable;
20 import org.opentrafficsim.draw.DrawLevel;
21 import org.opentrafficsim.draw.PaintLine;
22 import org.opentrafficsim.draw.TextAlignment;
23 import org.opentrafficsim.draw.TextAnimation;
24 import org.opentrafficsim.draw.road.CrossSectionElementAnimation.CrossSectionElementData;
25 import org.opentrafficsim.draw.road.LaneAnimation.LaneData;
26
27 import nl.tudelft.simulation.naming.context.Contextualized;
28
29
30
31
32
33
34
35
36
37
38 public class LaneAnimation extends CrossSectionElementAnimation<LaneData>
39 {
40
41 private static final long serialVersionUID = 20141017L;
42
43
44 private final Color color;
45
46
47 private final Text text;
48
49
50 private final CenterLineAnimation centerLineAnimation;
51
52
53
54
55
56
57
58
59
60 public LaneAnimation(final LaneData lane, final Contextualized contextualized, final Color color)
61 throws NamingException, RemoteException
62 {
63 super(lane, contextualized, color);
64 this.color = color;
65 this.text = new Text(lane, lane::getId, 0.0f, 0.0f, TextAlignment.CENTER, Color.BLACK, contextualized);
66 this.centerLineAnimation = new CenterLineAnimation(
67 new CenterLine(lane.getCenterLine(), lane.getLinkId() + "." + lane.getId()), contextualized);
68 }
69
70
71
72
73 public final Text getText()
74 {
75 return this.text;
76 }
77
78
79 @Override
80 public void destroy(final Contextualized contextProvider)
81 {
82 super.destroy(contextProvider);
83 this.text.destroy(contextProvider);
84 this.centerLineAnimation.destroy(contextProvider);
85 }
86
87
88 @Override
89 public final String toString()
90 {
91 return "LaneAnimation [lane = " + getSource().toString() + ", color=" + this.color + "]";
92 }
93
94
95
96
97 public static class CenterLine implements OtsLocatable
98 {
99
100 private final PolyLine2d centerLine;
101
102
103 private final OrientedPoint2d location;
104
105
106 private final OtsBounds2d bounds;
107
108
109 private final String fullId;
110
111
112
113
114
115
116 CenterLine(final PolyLine2d centerLine, final String fullId)
117 {
118 this.centerLine = centerLine;
119 this.location = new OrientedPoint2d(this.centerLine.getBounds().midPoint(), 0.0);
120 this.bounds = ClickableBounds.get(BoundingPolygon.geometryToBounds(this.location, centerLine).asPolygon());
121 this.fullId = fullId;
122 }
123
124
125 @Override
126 public final OrientedPoint2d getLocation()
127 {
128 return this.location;
129 }
130
131
132 @Override
133 public final OtsBounds2d getBounds()
134 {
135 return this.bounds;
136 }
137
138
139
140
141
142 public PolyLine2d getCenterLine()
143 {
144 return this.centerLine;
145 }
146
147
148 @Override
149 public double getZ()
150 {
151 return DrawLevel.CENTER_LINE.getZ();
152 }
153
154
155 @Override
156 public String toString()
157 {
158 return "Center line " + this.fullId;
159 }
160 }
161
162
163
164
165 public static class CenterLineAnimation extends OtsRenderable<CenterLine>
166 {
167
168 private static final Color COLOR = Color.MAGENTA.darker().darker();
169
170
171 private static final long serialVersionUID = 20180426L;
172
173
174 private final Path2D.Float path;
175
176
177
178
179
180
181
182
183 public CenterLineAnimation(final CenterLine centerLine, final Contextualized contextualized)
184 throws NamingException, RemoteException
185 {
186 super(centerLine, contextualized);
187 this.path = PaintLine.getPath(getSource().getLocation(), getSource().getCenterLine());
188 }
189
190
191 @Override
192 public final void paint(final Graphics2D graphics, final ImageObserver observer)
193 {
194 setRendering(graphics);
195 PaintLine.paintLine(graphics, COLOR, 0.1, this.path);
196 resetRendering(graphics);
197 }
198
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212 public class Text extends TextAnimation<LaneData, Text>
213 {
214
215 private static final long serialVersionUID = 20161211L;
216
217
218
219
220
221
222
223
224
225
226
227
228 public Text(final LaneData source, final Supplier<String> text, final float dx, final float dy,
229 final TextAlignment textPlacement, final Color color, final Contextualized contextualized)
230 throws RemoteException, NamingException
231 {
232 super(source, text, dx, dy, textPlacement, color, contextualized, TextAnimation.RENDERWHEN10);
233 }
234
235
236 @Override
237 public final String toString()
238 {
239 return "Text []";
240 }
241
242 }
243
244
245
246
247
248
249
250
251
252
253 public interface LaneData extends CrossSectionElementData, Identifiable
254 {
255
256 @Override
257 default double getZ()
258 {
259 return DrawLevel.LANE.getZ();
260 }
261 }
262
263 }