View Javadoc

1   package nl.tudelft.goal.ut2004.visualizer.map;
2   
3   import java.awt.Point;
4   import java.util.HashSet;
5   import java.util.Set;
6   import java.util.logging.Logger;
7   
8   import javax.media.opengl.GLCapabilities;
9   import javax.media.opengl.GLProfile;
10  import javax.media.opengl.awt.GLJPanel;
11  
12  import nl.tudelft.goal.ut2004.visualizer.timeline.map.EnvironmentRenderer;
13  import nl.tudelft.goal.ut2004.visualizer.timeline.map.CollectionRenderer;
14  import nl.tudelft.goal.ut2004.visualizer.timeline.map.MapController;
15  import nl.tudelft.goal.ut2004.visualizer.timeline.map.MapRenderer;
16  import nl.tudelft.goal.ut2004.visualizer.timeline.map.MapViewpoint;
17  import cz.cuni.amis.pogamut.unreal.communication.messages.gbinfomessages.IPlayer;
18  import cz.cuni.amis.pogamut.unreal.communication.worldview.map.IUnrealMap;
19  
20  /**
21   * This is a GLJPanel that displays UT2004Map. it is a base class, so it takes
22   * care of some things, while others are left for derived class to do.
23   * <p>
24   * What it does: * Can render passed map * Can render all stuff in
25   * agentRenderers * Takes care of user interaction for map viewpoint * Selection
26   * of object
27   * <p>
28   * What it doesn't do: * automatically display map, someone else has to take
29   * care of that (adding/removing) from agentRenderers * fill in agentRenderers,
30   * derived class has to do that.
31   * 
32   * In lookup are selected objects
33   * 
34   * @author Honza
35   */
36  public abstract class MapGLPanel extends GLJPanel implements
37  		MapViewpoint.ViewpointListener {
38  
39  	protected MapViewpoint mapViewpoint;
40  	protected MapController mapController;
41  
42  	protected MapRenderer mapRenderer;
43  	protected CollectionRenderer<IPlayer> agentRenderes;
44  	protected CollectionRenderer<Object> objectRenderes;
45  
46  	protected EnvironmentRenderer environmentRenderer;
47  
48  	// iterator used to assign unique names for gl rendering, this enabling
49  	// selection of objects
50  	protected int lastGLName = 1;
51  
52  	private IUnrealMap map;
53  
54  	/**
55  	 * Create a panel for
56  	 * 
57  	 * @param caps
58  	 * @param map
59  	 * @param log
60  	 */
61  	protected MapGLPanel(GLCapabilities caps, IUnrealMap map) {
62  		super(caps);
63  
64  		// if (Beans.isDesignTime()) {
65  		// Beans.setDesignTime(false);
66  		// }
67  
68  		this.map = map;
69  
70  		// Stuff for controlling viewpoint in map
71  		mapViewpoint = new MapViewpoint();
72  		mapController = new MapController(this, mapViewpoint, map.getBox());
73  		mapController.registerListeners();
74  
75  		// Create renderers
76  		mapRenderer = new MapRenderer(map, lastGLName++);
77  		agentRenderes = new CollectionRenderer<IPlayer>();
78  		objectRenderes = new CollectionRenderer<Object>();
79  		environmentRenderer = new EnvironmentRenderer(mapViewpoint,
80  				agentRenderes, objectRenderes, mapRenderer);
81  
82  		// Add listener so this level is rendered
83  		this.addGLEventListener(environmentRenderer);
84  
85  		// Listen for changes in viewpoint
86  		mapViewpoint.addViewpointListener(this);
87  
88  		// Set initial position of view + thanks to listener display
89  		mapViewpoint.setFromViewedBox(map.getBox());
90  
91  	}
92  
93  	/**
94  	 * Create a pane showing passed map
95  	 * 
96  	 * @param map
97  	 *            Map this pane is supposed to show
98  	 */
99  	protected MapGLPanel(IUnrealMap map, Logger log) {
100 		this(getCapabilities(), map);
101 	}
102 
103 	/**
104 	 * I require HW acceleration and double buffering.
105 	 * 
106 	 * @return Set of required capabilities
107 	 */
108 	private static GLCapabilities getCapabilities() {
109 		GLCapabilities caps = new GLCapabilities(GLProfile.getDefault());
110 		caps.setHardwareAccelerated(true);
111 		caps.setDoubleBuffered(true);
112 		return caps;
113 	}
114 
115 	/**
116 	 * When viewpoint is changed, render the map (call display()).
117 	 * 
118 	 * @param viewpoint
119 	 */
120 	@Override
121 	public synchronized void onChangedViewpoint(MapViewpoint viewpoint) {
122 		display();
123 	}
124 
125 	/**
126 	 * Get agents at point p in the scene.
127 	 * 
128 	 * @param p
129 	 *            in window coordiates system, [0,0] is left top
130 	 * @return List of renderable agents that are at the passed point
131 	 */
132 	public synchronized Set<Object> getObjectsAt(Point p) {
133 		environmentRenderer.setSelectPoint(p);
134 		display();
135 		int[] list = environmentRenderer.getSelectedObjects();
136 
137 		Set<Object> selection = new HashSet<Object>();
138 
139 		// find that miserable renderer in agents
140 		selection.addAll(agentRenderes.getObjectsByGLName(list));
141 		
142 		// find that miserable renderer in world objects
143 		selection.addAll(objectRenderes.getObjectsByGLName(list));
144 
145 		return selection;
146 	}
147 
148 	/**
149 	 * Remove listeners and basically clean up this map. Any call to this object
150 	 * after this method should invoke exception (it doesn't but I can always
151 	 * hope in NullPointerException).
152 	 */
153 	public synchronized void destroy() {
154 		this.removeGLEventListener(environmentRenderer);
155 		this.mapViewpoint.removeViewpointListener(this);
156 
157 		mapRenderer.destroy();
158 		agentRenderes.destroy();
159 		objectRenderes.destroy();
160 		
161 		mapRenderer = null;
162 		environmentRenderer = null;
163 		agentRenderes = null;
164 		objectRenderes = null;
165 
166 		mapController = null;
167 		mapViewpoint = null;
168 	}
169 
170 	protected IUnrealMap getMap() {
171 		return map;
172 	}
173 }