View Javadoc
1   package org.opentrafficsim.editor.extensions.map.edit;
2   
3   import java.awt.BasicStroke;
4   import java.awt.Color;
5   import java.awt.Graphics2D;
6   import java.awt.image.ImageObserver;
7   
8   import org.djutils.draw.line.Polygon2d;
9   import org.djutils.draw.point.DirectedPoint2d;
10  import org.opentrafficsim.animation.DrawLevel;
11  import org.opentrafficsim.animation.PaintLine;
12  import org.opentrafficsim.base.geometry.OtsShape;
13  import org.opentrafficsim.editor.OtsEditor;
14  import org.opentrafficsim.editor.extensions.map.EditorMap;
15  import org.opentrafficsim.editor.extensions.map.MapData;
16  import org.opentrafficsim.editor.extensions.map.MapNodeData;
17  import org.opentrafficsim.editor.extensions.map.edit.SelectionAnnotation.ZAdjustedMapData;
18  
19  /**
20   * Renderable that indicates the selection of an object. It paints a line along the contour of the object.
21   * <p>
22   * Copyright (c) 2026-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.<br>
23   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
24   * @author Wouter Schakel
25   */
26  public class SelectionAnnotation extends MapAnnotation<ZAdjustedMapData>
27  {
28  
29      /** Selection color. */
30      static final Color SELECTION_COLOR = OtsEditor.PROPERTIES_STORE.getColorOrDefault("map.selectionColor", Color.ORANGE);
31  
32      /** Invalid color. */
33      static final Color INVALID_COLOR = OtsEditor.PROPERTIES_STORE.getColorOrDefault("map.invalidColor", Color.RED);
34  
35      /**
36       * Constructor.
37       * @param source source
38       * @param map map
39       */
40      public SelectionAnnotation(final MapData source, final EditorMap map)
41      {
42          this(source, map, DrawLevel.SELECTION.getZ());
43      }
44  
45      /**
46       * Constructor.
47       * @param source source
48       * @param map map
49       * @param z z-level
50       */
51      protected SelectionAnnotation(final MapData source, final EditorMap map, final double z)
52      {
53          super(new ZAdjustedMapData(source, z), map);
54          setScaleY(true);
55          setRotate(!(source instanceof MapNodeData));
56      }
57  
58      @Override
59      public void paint(final Graphics2D graphics, final ImageObserver observer)
60      {
61          setRendering(graphics);
62          graphics.setStroke(new BasicStroke(px(1.75f), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10.0f));
63          graphics.setColor(getColor());
64          graphics.draw(PaintLine.getPath(getSource().getLocation(), getSource().getAbsoluteContour()));
65          resetRendering(graphics);
66      }
67  
68      /**
69       * Returns the color.
70       * @return color
71       */
72      protected Color getColor()
73      {
74          return getSource().mapData().isValid() ? SELECTION_COLOR : INVALID_COLOR;
75      }
76  
77      /**
78       * Helper class that wraps a {@link MapData} and acts like it, except with an adjusted z-value as given.
79       * @param mapData map data
80       * @param adjustedZ adjusted z-value, see {@link DrawLevel} for values
81       */
82      public record ZAdjustedMapData(MapData mapData, double adjustedZ) implements OtsShape
83      {
84  
85          @Override
86          public double getZ()
87          {
88              return adjustedZ();
89          }
90  
91          @Override
92          public DirectedPoint2d getLocation()
93          {
94              return mapData().getLocation();
95          }
96  
97          @Override
98          public Polygon2d getRelativeContour()
99          {
100             return mapData().getRelativeContour();
101         }
102 
103     }
104 
105 }