View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   package nl.tudelft.goal.ut2004.visualizer.map;
6   
7   import java.util.Collection;
8   import java.util.Map;
9   import java.util.Set;
10  import java.util.Timer;
11  import java.util.TimerTask;
12  import java.util.logging.Logger;
13  
14  import nl.tudelft.goal.ut2004.visualizer.timeline.map.IRenderableUTAgent;
15  import nl.tudelft.goal.ut2004.visualizer.timeline.map.IRenderableWorldObject;
16  import nl.tudelft.goal.ut2004.visualizer.timeline.map.ISubGLRenderer;
17  import nl.tudelft.goal.ut2004.visualizer.timeline.map.UTAgentSubGLRenderer;
18  import nl.tudelft.goal.ut2004.visualizer.timeline.map.UTFlagSubGLRenderer;
19  import nl.tudelft.goal.ut2004.visualizer.timeline.map.UTWaypointSubGLRenderer;
20  import cz.cuni.amis.pogamut.base.communication.worldview.object.WorldObjectId;
21  import cz.cuni.amis.pogamut.unreal.bot.IUnrealBot;
22  import cz.cuni.amis.pogamut.unreal.communication.messages.gbinfomessages.IPlayer;
23  import cz.cuni.amis.pogamut.unreal.communication.worldview.map.IUnrealMap;
24  import cz.cuni.amis.pogamut.unreal.communication.worldview.map.IUnrealWaypoint;
25  import cz.cuni.amis.pogamut.unreal.server.IUnrealServer;
26  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.FlagInfo;
27  import cz.cuni.amis.utils.collections.CollectionEventListener;
28  
29  /**
30   * Simple map that overviews what is happening in the level right now. It
31   * listens for changes in server.getAgents() and server.getNativeBots() for
32   * adding and removing bots from map.
33   * 
34   * This is intended for one map only, it is not intended to handle change from
35   * one map to another, simply discard this, call destroy() and create a new one.
36   * 
37   * This is panel that shows one particular map. After you are finished with
38   * using the class, please call destroy() to remove listeners, stops redrawing
39   * and do some other clean up.
40   * 
41   * @author Honza
42   */
43  public class PureMapGLPanel extends SelectableMapGLPanel implements
44  		CollectionEventListener<IPlayer> {
45  
46  	/** Server on which we listen for changes in agents and bots */
47  	protected IUnrealServer server;
48  	// Generator used to create colors for new agents.
49  	protected MapColorGenerator colorGenerator;
50  	// Timer used to redraw this panel.
51  	private Timer timer;
52  	// How often should map be redrawn, be careful, because of traffic overhead,
53  	// we get the data from somewhere in the net.
54  	private final int REDRAW_DELAY = 250;
55  
56  	protected PureMapGLPanel(IUnrealMap map, IUnrealServer server) {
57  		super(map, Logger.getLogger("PureMapGLPanel"));
58  
59  		this.server = server;
60  		this.colorGenerator = new MapColorGenerator();
61  
62  		// add all found agents in the map
63  		for (Object agent : server.getPlayers()) {
64  			addAgentRenderer((IPlayer) agent);
65  		}
66  
67  		// add all objects in the map
68  		Map<WorldObjectId, FlagInfo> flags = server.getWorldView().getAll(
69  				FlagInfo.class);
70  		for (FlagInfo flag : flags.values()) {
71  			addFlagRenderer(flag);
72  		}
73  
74  		Collection<IUnrealWaypoint> navs = map.vertexSet();
75  		for (IUnrealWaypoint waypoint : navs) {
76  			addWaypointRenderer(waypoint);
77  		}
78  
79  		// add listeners so I can update agents
80  		server.getPlayers().addCollectionListener(this);
81  
82  	}
83  
84  	/**
85  	 * Start display loop
86  	 */
87  	public synchronized void startDisplayLoop() {
88  		if (timer == null) {
89  			timer = new java.util.Timer("Overview map redrawer");
90  			timer.schedule(new TimerTask() {
91  
92  				@Override
93  				public void run() {
94  					display();
95  				}
96  			}, REDRAW_DELAY, REDRAW_DELAY);
97  		}
98  	}
99  
100 	/**
101 	 * Stop display loop
102 	 */
103 	public synchronized void stopDisplayLoop() {
104 		if (timer != null) {
105 			timer.cancel();
106 			timer = null;
107 		}
108 	}
109 
110 	/**
111 	 * Create a renderable representation of agent and add it to renderers.
112 	 * 
113 	 * @param agent
114 	 *            Agent that will be added to drawn agents
115 	 */
116 	private void addAgentRenderer(IPlayer agent) {
117 		IRenderableUTAgent renderableUTAgent = new ProxyRenderablePlayer(agent,
118 				colorGenerator.getUniqueTeamColor(), lastGLName++);
119 		agentRenderes.addSubRenderer(new UTAgentSubGLRenderer(
120 				renderableUTAgent, getMap()));
121 	}
122 
123 	private void removeAgentRenderer(IPlayer agent) {
124 		Set<IRenderableUTAgent> drawnAgent = agentRenderes.getDrawnObjects();
125 
126 		for (IRenderableUTAgent renderableAgent : drawnAgent) {
127 			if (renderableAgent.getDataSource() == agent) {
128 				agentRenderes.removeRenderersOf(renderableAgent);
129 			}
130 		}
131 	}
132 
133 	private void addFlagRenderer(FlagInfo flag) {
134 		IRenderableWorldObject renderableFlag = new RenderableFlag(flag,
135 				lastGLName++);
136 		ISubGLRenderer<IRenderableWorldObject> subRenderer = new UTFlagSubGLRenderer(
137 				renderableFlag);
138 		objectRenderes.addSubRenderer(subRenderer);
139 	}
140 
141 	private void addWaypointRenderer(IUnrealWaypoint waypoint) {
142 		IRenderableWorldObject renderableFlag = new RenderableWaypoint(
143 				waypoint, lastGLName++);
144 		ISubGLRenderer<IRenderableWorldObject> subRenderer = new UTWaypointSubGLRenderer(
145 				renderableFlag);
146 		objectRenderes.addSubRenderer(subRenderer);
147 	}
148 
149 	/**
150 	 * Do nothing.
151 	 * 
152 	 * @param toBeAdded
153 	 * @param whereToAdd
154 	 */
155 	@Override
156 	public void preAddEvent(Collection<IPlayer> toBeAdded,
157 			Collection<IPlayer> whereToAdd) {
158 	}
159 
160 	/**
161 	 * Add renderers representing the agents to the map.
162 	 * 
163 	 * @param alreadyAdded
164 	 * @param whereWereAdded
165 	 */
166 	@Override
167 	public synchronized void postAddEvent(Collection<IPlayer> alreadyAdded,
168 			Collection<IPlayer> whereWereAdded) {
169 		for (IPlayer agent : alreadyAdded) {
170 			addAgentRenderer(agent);
171 		}
172 	}
173 
174 	/**
175 	 * Remove renderers that represented the removed agents from the map
176 	 * 
177 	 * @param toBeRemoved
178 	 * @param whereToRemove
179 	 */
180 	@Override
181 	public synchronized void preRemoveEvent(Collection<IPlayer> toBeRemoved,
182 			Collection<IPlayer> whereToRemove) {
183 		for (IPlayer removedAgent : toBeRemoved) {
184 			removeAgentRenderer(removedAgent);
185 		}
186 	}
187 
188 	/**
189 	 * Do nothing
190 	 * 
191 	 * @param alreadyAdded
192 	 * @param whereWereRemoved
193 	 */
194 	@Override
195 	public void postRemoveEvent(Collection<IPlayer> alreadyAdded,
196 			Collection<IPlayer> whereWereRemoved) {
197 	}
198 
199 	@Override
200 	public synchronized void destroy() {
201 		if (timer != null) {
202 			timer.cancel();
203 		}
204 
205 		server.getAgents().removeCollectionListener(this);
206 		server.getNativeAgents().removeCollectionListener(this);
207 
208 		server = null;
209 
210 		super.destroy();
211 	}
212 }