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
22
23
24
25
26
27
28
29
30
31
32
33
34
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
49
50 protected int lastGLName = 1;
51
52 private IUnrealMap map;
53
54
55
56
57
58
59
60
61 protected MapGLPanel(GLCapabilities caps, IUnrealMap map) {
62 super(caps);
63
64
65
66
67
68 this.map = map;
69
70
71 mapViewpoint = new MapViewpoint();
72 mapController = new MapController(this, mapViewpoint, map.getBox());
73 mapController.registerListeners();
74
75
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
83 this.addGLEventListener(environmentRenderer);
84
85
86 mapViewpoint.addViewpointListener(this);
87
88
89 mapViewpoint.setFromViewedBox(map.getBox());
90
91 }
92
93
94
95
96
97
98
99 protected MapGLPanel(IUnrealMap map, Logger log) {
100 this(getCapabilities(), map);
101 }
102
103
104
105
106
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
117
118
119
120 @Override
121 public synchronized void onChangedViewpoint(MapViewpoint viewpoint) {
122 display();
123 }
124
125
126
127
128
129
130
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
140 selection.addAll(agentRenderes.getObjectsByGLName(list));
141
142
143 selection.addAll(objectRenderes.getObjectsByGLName(list));
144
145 return selection;
146 }
147
148
149
150
151
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 }