1 package org.opentrafficsim.road.gtu.colorer;
2
3 import java.awt.Color;
4 import java.io.Serializable;
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.LinkedHashMap;
8 import java.util.List;
9 import java.util.Map;
10
11 import org.opentrafficsim.core.animation.gtu.colorer.GTUColorer;
12 import org.opentrafficsim.core.animation.gtu.colorer.IDGTUColorer;
13 import org.opentrafficsim.core.gtu.GTU;
14 import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
15 import org.opentrafficsim.road.gtu.lane.perception.mental.Fuller;
16 import org.opentrafficsim.road.gtu.lane.perception.mental.Fuller.IdentifiableTask;
17 import org.opentrafficsim.road.gtu.lane.perception.mental.Fuller.Task;
18 import org.opentrafficsim.road.gtu.lane.perception.mental.Mental;
19 import org.opentrafficsim.road.gtu.lane.perception.mental.sdm.DefaultDistraction;
20
21
22
23
24
25
26
27
28
29
30
31
32 public class DistractionColorer implements GTUColorer, Serializable
33 {
34
35 private static final long serialVersionUID = 20181106L;
36
37
38 private static final Color NONE = Color.MAGENTA.darker().darker();
39
40
41 private final ArrayList<LegendEntry> legend = new ArrayList<>();
42
43
44 private final Map<String, Color> colors = new LinkedHashMap<>();
45
46
47
48
49
50 public DistractionColorer(final DefaultDistraction... distractions)
51 {
52 this.legend.add(new LegendEntry(NONE, "None", "No distraction"));
53 for (int i = 0; i < distractions.length; i++)
54 {
55 Color c = IDGTUColorer.LEGEND.get(i % 10).getColor();
56 this.colors.put(distractions[i].getId(), c);
57 this.legend.add(new LegendEntry(c, distractions[i].getDescription(), distractions[i].getDescription()));
58 }
59 }
60
61
62 @Override
63 public Color getColor(final GTU gtu)
64 {
65 if (gtu.getTacticalPlanner().getPerception() instanceof LanePerception)
66 {
67 Mental mental = ((LanePerception) gtu.getTacticalPlanner().getPerception()).getMental();
68 if (mental != null && mental instanceof Fuller)
69 {
70 for (Task task : ((Fuller) mental).getTasks())
71 {
72 if (task instanceof IdentifiableTask)
73 {
74 String id = ((IdentifiableTask) task).getId();
75 if (this.colors.containsKey(id))
76 {
77 return this.colors.get(id);
78 }
79 }
80 }
81 }
82 }
83 return NONE;
84 }
85
86
87 @Override
88 public final List<LegendEntry> getLegend()
89 {
90 return Collections.unmodifiableList(this.legend);
91 }
92
93
94 @Override
95 public final String toString()
96 {
97 return "Distraction";
98 }
99
100 }