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.util.function.Supplier;
8
9 import org.djutils.base.Identifiable;
10 import org.djutils.draw.line.PolyLine2d;
11 import org.djutils.draw.line.Polygon2d;
12 import org.djutils.draw.point.OrientedPoint2d;
13 import org.opentrafficsim.base.geometry.OtsLocatable;
14 import org.opentrafficsim.base.geometry.OtsShape;
15 import org.opentrafficsim.draw.ClickableLineLocatable;
16 import org.opentrafficsim.draw.DrawLevel;
17 import org.opentrafficsim.draw.OtsRenderable;
18 import org.opentrafficsim.draw.PaintLine;
19 import org.opentrafficsim.draw.TextAlignment;
20 import org.opentrafficsim.draw.TextAnimation;
21 import org.opentrafficsim.draw.road.CrossSectionElementAnimation.CrossSectionElementData;
22 import org.opentrafficsim.draw.road.LaneAnimation.LaneData;
23
24 import nl.tudelft.simulation.naming.context.Contextualized;
25
26
27
28
29
30
31
32
33
34
35 public class LaneAnimation extends CrossSectionElementAnimation<LaneData>
36 {
37
38 private static final long serialVersionUID = 20141017L;
39
40
41 private final Color color;
42
43
44 private final Text text;
45
46
47 private final CenterLineAnimation centerLineAnimation;
48
49
50
51
52
53
54
55 public LaneAnimation(final LaneData lane, final Contextualized contextualized, final Color color)
56 {
57 super(lane, contextualized, color);
58 this.color = color;
59 this.text = new Text(lane, lane::getId, 0.0f, 0.0f, TextAlignment.CENTER, Color.BLACK, contextualized);
60 this.centerLineAnimation = new CenterLineAnimation(
61 new CenterLine(lane.getCenterLine(), lane.getLinkId() + "." + lane.getId()), contextualized);
62 }
63
64
65
66
67 public final Text getText()
68 {
69 return this.text;
70 }
71
72 @Override
73 public void destroy(final Contextualized contextProvider)
74 {
75 super.destroy(contextProvider);
76 this.text.destroy(contextProvider);
77 this.centerLineAnimation.destroy(contextProvider);
78 }
79
80 @Override
81 public final String toString()
82 {
83 return "LaneAnimation [lane = " + getSource().toString() + ", color=" + this.color + "]";
84 }
85
86
87
88
89 public static class CenterLine implements ClickableLineLocatable
90 {
91
92 private final PolyLine2d centerLine;
93
94
95 private final OrientedPoint2d location;
96
97
98 private OtsShape shape;
99
100
101 private final String fullId;
102
103
104
105
106
107
108 CenterLine(final PolyLine2d centerLine, final String fullId)
109 {
110 this.centerLine = centerLine;
111 this.location = new OrientedPoint2d(this.centerLine.getBounds().midPoint(), 0.0);
112 this.fullId = fullId;
113 }
114
115 @Override
116 public final OrientedPoint2d getLocation()
117 {
118 return this.location;
119 }
120
121 @Override
122 public OtsShape getShape()
123 {
124 if (this.shape == null)
125 {
126 this.shape = ClickableLineLocatable.super.getShape();
127 }
128 return this.shape;
129 }
130
131 @Override
132 public Polygon2d getContour()
133 {
134 return new Polygon2d(this.centerLine.getPoints());
135 }
136
137
138
139
140
141 public PolyLine2d getCenterLine()
142 {
143 return this.centerLine;
144 }
145
146 @Override
147 public PolyLine2d getLine()
148 {
149 return OtsLocatable.transformLine(this.centerLine, getLocation());
150 }
151
152 @Override
153 public double getZ()
154 {
155 return DrawLevel.CENTER_LINE.getZ();
156 }
157
158 @Override
159 public String toString()
160 {
161 return "Center line " + this.fullId;
162 }
163
164 }
165
166
167
168
169 public static class CenterLineAnimation extends OtsRenderable<CenterLine>
170 {
171
172 private static final Color COLOR = Color.MAGENTA.darker().darker();
173
174
175 private static final long serialVersionUID = 20180426L;
176
177
178 private final Path2D.Float path;
179
180
181
182
183
184
185 public CenterLineAnimation(final CenterLine centerLine, final Contextualized contextualized)
186 {
187 super(centerLine, contextualized);
188 this.path = PaintLine.getPath(getSource().getLocation(), getSource().getCenterLine());
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 public Text(final LaneData source, final Supplier<String> text, final float dx, final float dy,
227 final TextAlignment textPlacement, final Color color, final Contextualized contextualized)
228 {
229 super(source, text, dx, dy, textPlacement, color, contextualized, TextAnimation.RENDERWHEN10);
230 }
231
232 @Override
233 public final String toString()
234 {
235 return "Text []";
236 }
237
238 }
239
240
241
242
243
244
245
246
247
248
249 public interface LaneData extends CrossSectionElementData, Identifiable
250 {
251 @Override
252 default double getZ()
253 {
254 return DrawLevel.LANE.getZ();
255 }
256 }
257
258 }