1
2
3
4
5 package nl.tudelft.goal.ut2004.visualizer.map;
6
7 import java.util.Collection;
8 import java.util.Map;
9 import java.util.Timer;
10 import java.util.TimerTask;
11 import java.util.logging.Logger;
12
13 import nl.tudelft.goal.ut2004.visualizer.timeline.map.ISubGLRenderer;
14 import nl.tudelft.goal.ut2004.visualizer.timeline.map.PlayerRenderer;
15 import nl.tudelft.goal.ut2004.visualizer.timeline.map.FlagRenderer;
16 import nl.tudelft.goal.ut2004.visualizer.timeline.map.WaypointRenderer;
17 import cz.cuni.amis.pogamut.base.communication.worldview.object.WorldObjectId;
18 import cz.cuni.amis.pogamut.unreal.communication.messages.gbinfomessages.IPlayer;
19 import cz.cuni.amis.pogamut.unreal.communication.worldview.map.IUnrealMap;
20 import cz.cuni.amis.pogamut.unreal.communication.worldview.map.IUnrealWaypoint;
21 import cz.cuni.amis.pogamut.unreal.server.IUnrealServer;
22 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.FlagInfo;
23 import cz.cuni.amis.utils.collections.CollectionEventListener;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public class PureMapGLPanel extends SelectableMapGLPanel implements
40 CollectionEventListener<IPlayer> {
41
42
43 protected IUnrealServer server;
44
45 private Timer timer;
46
47
48 private final int REDRAW_DELAY = 30;
49
50 protected PureMapGLPanel(IUnrealMap map, IUnrealServer server) {
51 super(map, Logger.getLogger("PureMapGLPanel"));
52
53 this.server = server;
54
55
56 for (Object agent : server.getPlayers()) {
57 addAgentRenderer((IPlayer) agent);
58 }
59
60
61 Map<WorldObjectId, FlagInfo> flags = server.getWorldView().getAll(
62 FlagInfo.class);
63 for (FlagInfo flag : flags.values()) {
64 addFlagRenderer(flag);
65 }
66
67 Collection<IUnrealWaypoint> navs = map.vertexSet();
68 for (IUnrealWaypoint waypoint : navs) {
69 addWaypointRenderer(waypoint);
70 }
71
72
73 server.getPlayers().addCollectionListener(this);
74
75 }
76
77
78
79
80 public synchronized void startDisplayLoop() {
81 if (timer == null) {
82 timer = new java.util.Timer("Overview map redrawer");
83 timer.schedule(new TimerTask() {
84
85 @Override
86 public void run() {
87 display();
88 }
89 }, REDRAW_DELAY, REDRAW_DELAY);
90 }
91 }
92
93
94
95
96 public synchronized void stopDisplayLoop() {
97 if (timer != null) {
98 timer.cancel();
99 timer = null;
100 }
101 }
102
103
104
105
106
107
108
109 private void addAgentRenderer(IPlayer agent) {
110 agentRenderes.addRenderer(new PlayerRenderer(agent, lastGLName++));
111 }
112
113 private void removeAgentRenderer(IPlayer agent) {
114 agentRenderes.removeRenderersOf(agent);
115 }
116
117 private void addFlagRenderer(FlagInfo flag) {
118 ISubGLRenderer<FlagInfo> subRenderer = new FlagRenderer(flag,lastGLName++);
119 objectRenderes.addRenderer(subRenderer);
120 }
121
122 private void addWaypointRenderer(IUnrealWaypoint waypoint) {
123 ISubGLRenderer<IUnrealWaypoint> subRenderer = new WaypointRenderer(waypoint, lastGLName++);
124 objectRenderes.addRenderer(subRenderer);
125 }
126
127
128
129
130
131
132
133 @Override
134 public void preAddEvent(Collection<IPlayer> toBeAdded,
135 Collection<IPlayer> whereToAdd) {
136 }
137
138
139
140
141
142
143
144 @Override
145 public synchronized void postAddEvent(Collection<IPlayer> alreadyAdded,
146 Collection<IPlayer> whereWereAdded) {
147 for (IPlayer agent : alreadyAdded) {
148 addAgentRenderer(agent);
149 }
150 }
151
152
153
154
155
156
157
158 @Override
159 public synchronized void preRemoveEvent(Collection<IPlayer> toBeRemoved,
160 Collection<IPlayer> whereToRemove) {
161 for (IPlayer removedAgent : toBeRemoved) {
162 removeAgentRenderer(removedAgent);
163 }
164 }
165
166
167
168
169
170
171
172 @Override
173 public void postRemoveEvent(Collection<IPlayer> alreadyAdded,
174 Collection<IPlayer> whereWereRemoved) {
175 }
176
177 @Override
178 public synchronized void destroy() {
179 if (timer != null) {
180 timer.cancel();
181 }
182
183 server.getAgents().removeCollectionListener(this);
184 server.getNativeAgents().removeCollectionListener(this);
185
186 server = null;
187
188 super.destroy();
189 }
190 }