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.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   * Simple map that overviews what is happening in the level right now. It
27   * listens for changes in server.getAgents() and server.getNativeBots() for
28   * adding and removing bots from map.
29   * 
30   * This is intended for one map only, it is not intended to handle change from
31   * one map to another, simply discard this, call destroy() and create a new one.
32   * 
33   * This is panel that shows one particular map. After you are finished with
34   * using the class, please call destroy() to remove listeners, stops redrawing
35   * and do some other clean up.
36   * 
37   * @author Honza
38   */
39  public class PureMapGLPanel extends SelectableMapGLPanel implements
40  		CollectionEventListener<IPlayer> {
41  
42  	/** Server on which we listen for changes in agents and bots */
43  	protected IUnrealServer server;
44  	// Timer used to redraw this panel.
45  	private Timer timer;
46  	// How often should map be redrawn, be careful, because of traffic overhead,
47  	// we get the data from somewhere in the net.
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  		// add all found agents in the map
56  		for (Object agent : server.getPlayers()) {
57  			addAgentRenderer((IPlayer) agent);
58  		}
59  
60  		// add all objects in the map
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  		// add listeners so I can update agents
73  		server.getPlayers().addCollectionListener(this);
74  
75  	}
76  
77  	/**
78  	 * Start display loop
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  	 * Stop display loop
95  	 */
96  	public synchronized void stopDisplayLoop() {
97  		if (timer != null) {
98  			timer.cancel();
99  			timer = null;
100 		}
101 	}
102 
103 	/**
104 	 * Create a renderable representation of agent and add it to renderers.
105 	 * 
106 	 * @param agent
107 	 *            Agent that will be added to drawn agents
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 	 * Do nothing.
129 	 * 
130 	 * @param toBeAdded
131 	 * @param whereToAdd
132 	 */
133 	@Override
134 	public void preAddEvent(Collection<IPlayer> toBeAdded,
135 			Collection<IPlayer> whereToAdd) {
136 	}
137 
138 	/**
139 	 * Add renderers representing the agents to the map.
140 	 * 
141 	 * @param alreadyAdded
142 	 * @param whereWereAdded
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 	 * Remove renderers that represented the removed agents from the map
154 	 * 
155 	 * @param toBeRemoved
156 	 * @param whereToRemove
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 	 * Do nothing
168 	 * 
169 	 * @param alreadyAdded
170 	 * @param whereWereRemoved
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 }