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  import javax.swing.SwingUtilities;
25  
26  /**
27   * Simple map that overviews what is happening in the level right now. It
28   * listens for changes in server.getAgents() and server.getNativeBots() for
29   * adding and removing bots from map.
30   *
31   * This is intended for one map only, it is not intended to handle change from
32   * one map to another, simply discard this, call destroy() and create a new one.
33   *
34   * This is panel that shows one particular map. After you are finished with
35   * using the class, please call destroy() to remove listeners, stops redrawing
36   * and do some other clean up.
37   *
38   * @author Honza
39   */
40  public class PureMapGLPanel extends SelectableMapGLPanel implements
41          CollectionEventListener<IPlayer> {
42  
43      /**
44       * Server on which we listen for changes in agents and bots
45       */
46      protected IUnrealServer server;
47      // Timer used to redraw this panel.
48      private Timer timer;
49      // How often should map be redrawn, be careful, because of traffic overhead,
50      // we get the data from somewhere in the net.
51      private final int REDRAW_DELAY = 250;
52  
53      protected PureMapGLPanel(IUnrealMap map, IUnrealServer server) {
54          super(map, Logger.getLogger("PureMapGLPanel"));
55  
56          this.server = server;
57  
58          // add all found agents in the map
59          for (Object agent : server.getPlayers()) {
60              addAgentRenderer((IPlayer) agent);
61          }
62  
63          // add all objects in the map
64          Map<WorldObjectId, FlagInfo> flags = server.getWorldView().getAll(
65                  FlagInfo.class);
66          for (FlagInfo flag : flags.values()) {
67              addFlagRenderer(flag);
68          }
69  
70          Collection<IUnrealWaypoint> navs = map.vertexSet();
71          for (IUnrealWaypoint waypoint : navs) {
72              addWaypointRenderer(waypoint);
73          }
74  
75          // add listeners so I can update agents
76          server.getPlayers().addCollectionListener(this);
77  
78      }
79      static int called = 0;
80  
81      /**
82       * Start display loop
83       */
84      public synchronized void startDisplayLoop() {
85          SwingUtilities.invokeLater(new Runnable() {
86              @Override
87              public void run() {
88                  if (timer == null) {
89                      timer = new java.util.Timer("Overview map redrawer");
90                      timer.schedule(new TimerTask() {
91                          @Override
92                          public void run() {
93                              display();
94                          }
95                      }, REDRAW_DELAY, REDRAW_DELAY);
96                  }
97              }
98          });
99      }
100 
101     /**
102      * Stop display loop
103      */
104     public synchronized void stopDisplayLoop() {
105         if (timer != null) {
106             timer.cancel();
107             timer = null;
108         }
109     }
110 
111     /**
112      * Create a renderable representation of agent and add it to renderers.
113      *
114      * @param agent Agent that will be added to drawn agents
115      */
116     private void addAgentRenderer(IPlayer agent) {
117         agentRenderes.addRenderer(new PlayerRenderer(agent, lastGLName++));
118     }
119 
120     private void removeAgentRenderer(IPlayer agent) {
121         agentRenderes.removeRenderersOf(agent);
122     }
123 
124     private void addFlagRenderer(FlagInfo flag) {
125         ISubGLRenderer<FlagInfo> subRenderer = new FlagRenderer(flag, lastGLName++);
126         objectRenderes.addRenderer(subRenderer);
127     }
128 
129     private void addWaypointRenderer(IUnrealWaypoint waypoint) {
130         ISubGLRenderer<IUnrealWaypoint> subRenderer = new WaypointRenderer(waypoint, lastGLName++);
131         objectRenderes.addRenderer(subRenderer);
132     }
133 
134     /**
135      * Do nothing.
136      *
137      * @param toBeAdded
138      * @param whereToAdd
139      */
140     @Override
141     public void preAddEvent(Collection<IPlayer> toBeAdded,
142             Collection<IPlayer> whereToAdd) {
143     }
144 
145     /**
146      * Add renderers representing the agents to the map.
147      *
148      * @param alreadyAdded
149      * @param whereWereAdded
150      */
151     @Override
152     public synchronized void postAddEvent(Collection<IPlayer> alreadyAdded,
153             Collection<IPlayer> whereWereAdded) {
154         for (IPlayer agent : alreadyAdded) {
155             addAgentRenderer(agent);
156         }
157     }
158 
159     /**
160      * Remove renderers that represented the removed agents from the map
161      *
162      * @param toBeRemoved
163      * @param whereToRemove
164      */
165     @Override
166     public synchronized void preRemoveEvent(Collection<IPlayer> toBeRemoved,
167             Collection<IPlayer> whereToRemove) {
168         for (IPlayer removedAgent : toBeRemoved) {
169             removeAgentRenderer(removedAgent);
170         }
171     }
172 
173     /**
174      * Do nothing
175      *
176      * @param alreadyAdded
177      * @param whereWereRemoved
178      */
179     @Override
180     public void postRemoveEvent(Collection<IPlayer> alreadyAdded,
181             Collection<IPlayer> whereWereRemoved) {
182     }
183 
184     @Override
185     public synchronized void destroy() {
186         if (timer != null) {
187             timer.cancel();
188         }
189 
190         server.getAgents().removeCollectionListener(this);
191         server.getNativeAgents().removeCollectionListener(this);
192 
193         server = null;
194 
195         super.destroy();
196     }
197 }