View Javadoc
1   package org.opentrafficsim.swing.gui;
2   
3   import java.awt.Color;
4   import java.util.LinkedHashMap;
5   import java.util.List;
6   import java.util.Map;
7   
8   import org.djutils.immutablecollections.Immutable;
9   import org.djutils.immutablecollections.ImmutableLinkedHashMap;
10  import org.djutils.immutablecollections.ImmutableMap;
11  import org.opentrafficsim.animation.Colors;
12  import org.opentrafficsim.animation.colorer.Colorer;
13  import org.opentrafficsim.animation.colorer.FixedColorer;
14  import org.opentrafficsim.animation.data.gtu.AccelerationGtuColorer;
15  import org.opentrafficsim.animation.data.gtu.IdGtuColorer;
16  import org.opentrafficsim.animation.data.gtu.SpeedGtuColorer;
17  import org.opentrafficsim.animation.data.util.DefaultAnimationFactory;
18  import org.opentrafficsim.animation.gtu.DefaultCarAnimation.GtuData.GtuMarker;
19  import org.opentrafficsim.core.definitions.DefaultsNl;
20  import org.opentrafficsim.core.gtu.Gtu;
21  import org.opentrafficsim.core.gtu.GtuType;
22  import org.opentrafficsim.core.network.Network;
23  
24  /**
25   * Decorator for the {@link OtsSimulationPanel}. Default method implementations are provided which sub-classes may implement
26   * with different decoration, e.g. additional tabs.
27   * <p>
28   * Copyright (c) 2026-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
29   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
30   * </p>
31   * @author Alexander Verbraeck
32   * @author Peter Knoppers
33   * @author Wouter Schakel
34   */
35  public interface OtsSimulationPanelDecorator
36  {
37  
38      /** Default GTU colorers. */
39      // List.of(...) results in an unmodifiable list
40      List<Colorer<? super Gtu>> DEFAULT_GTU_COLORERS = List.of(new FixedColorer<>(Colors.OTS_BLUE, "Blue"), new IdGtuColorer(),
41              new SpeedGtuColorer(), new AccelerationGtuColorer());
42  
43      /** Standard drawing colors for GTU types. */
44      ImmutableMap<GtuType, Color> GTU_TYPE_COLORS = new ImmutableLinkedHashMap<>(new LinkedHashMap<>()
45      {
46          {
47              put(DefaultsNl.CAR, Color.BLUE);
48              put(DefaultsNl.TRUCK, Color.RED);
49              put(DefaultsNl.VEHICLE, Color.GRAY);
50              put(DefaultsNl.PEDESTRIAN, Color.YELLOW);
51              put(DefaultsNl.MOTORCYCLE, Color.PINK);
52              put(DefaultsNl.BICYCLE, Color.GREEN);
53          }
54      }, Immutable.WRAP);
55  
56      /** Standard markers for GTU types. */
57      ImmutableMap<GtuType, GtuMarker> GTU_TYPE_MARKERS = new ImmutableLinkedHashMap<>(new LinkedHashMap<>()
58      {
59          {
60              put(DefaultsNl.TRUCK, GtuMarker.SQUARE);
61          }
62      }, Immutable.WRAP);
63  
64      /**
65       * Invokes all decoration methods.
66       * @param simulationPanel simulation panel
67       * @param network network
68       */
69      default void decorate(final OtsSimulationPanel simulationPanel, final Network network)
70      {
71          setAnimationToggles(simulationPanel);
72          animateSimulation(simulationPanel, network);
73          setupDemo(simulationPanel, network);
74          addTabs(simulationPanel, network);
75      }
76  
77      /**
78       * Set simulation toggles. The default implementation sets standard icon toggles.
79       * @param simulationPanel simulation panel
80       */
81      default void setAnimationToggles(final OtsSimulationPanel simulationPanel)
82      {
83          AnimationToggles.setIconAnimationTogglesStandard(simulationPanel);
84      }
85  
86      /**
87       * Creates the animation objects. This method is overridable. The default uses {@link DefaultAnimationFactory}.
88       * @param simulationPanel simulation panel
89       * @param network network
90       */
91      default void animateSimulation(final OtsSimulationPanel simulationPanel, final Network network)
92      {
93          DefaultAnimationFactory.animateNetwork(network, network.getSimulator(), simulationPanel.getGtuColorerManager(),
94                  getGtuMarkers());
95      }
96  
97      /**
98       * Adds tabs. The default implementation does nothing.
99       * @param simulationPanel simulation panel
100      * @param network network
101      */
102     default void addTabs(final OtsSimulationPanel simulationPanel, final Network network)
103     {
104         //
105     }
106 
107     /**
108      * Setup a demo panel within the simulation panel. The default implementation does nothing.
109      * @param simulationPanel simulation panel
110      * @param network network
111      */
112     default void setupDemo(final OtsSimulationPanel simulationPanel, final Network network)
113     {
114         //
115     }
116 
117     /**
118      * Return GTU colorers. The default implementation returns the default colorers.
119      * @return GTU colorers
120      */
121     default List<Colorer<? super Gtu>> getGtuColorers()
122     {
123         return DEFAULT_GTU_COLORERS;
124     }
125 
126     /**
127      * Returns a map of markers to use on GTU types. The default implementation returns default markers (NL.TRUCK as square).
128      * @return map of markers to use on GTU types
129      */
130     default Map<GtuType, GtuMarker> getGtuMarkers()
131     {
132         return GTU_TYPE_MARKERS.toMap();
133     }
134 
135 }