View Javadoc

1   /*
2    * Copyright (C) 2010 Unreal Visualizer Authors
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package nl.tudelft.goal.ut2004.visualizer.gui.panels;
18  
19  import java.awt.BorderLayout;
20  import java.awt.Component;
21  import java.awt.Menu;
22  import java.awt.MenuItem;
23  import java.awt.Point;
24  import java.awt.PopupMenu;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  import java.awt.event.MouseAdapter;
28  import java.awt.event.MouseEvent;
29  import java.awt.event.MouseListener;
30  import java.util.Collection;
31  import java.util.Set;
32  
33  import javax.swing.JPanel;
34  import javax.swing.JTextArea;
35  
36  
37  import nl.tudelft.goal.ut2004.visualizer.controller.ServerController;
38  import nl.tudelft.goal.ut2004.visualizer.data.EnvironmentData;
39  import nl.tudelft.goal.ut2004.visualizer.gui.action.AddInventoryAction;
40  import nl.tudelft.goal.ut2004.visualizer.gui.action.ChangeTeamAction;
41  import nl.tudelft.goal.ut2004.visualizer.gui.action.KickAction;
42  import nl.tudelft.goal.ut2004.visualizer.gui.action.RespawnHereAction;
43  import nl.tudelft.goal.ut2004.visualizer.gui.action.RespawnRandomAction;
44  import nl.tudelft.goal.ut2004.visualizer.gui.action.SpawnItemAction;
45  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.AddNativeBotDialog;
46  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.AddUnrealGoalBotDialog;
47  import nl.tudelft.goal.ut2004.visualizer.map.PureMapTopPanel;
48  import nl.tudelft.goal.ut2004.visualizer.map.RenderableWaypoint;
49  import nl.tudelft.goal.ut2004.visualizer.services.ISelectionHandler;
50  import nl.tudelft.goal.ut2004.visualizer.timeline.map.IRenderableUTAgent;
51  import nl.tudelft.goal.ut2004.visualizer.timeline.map.IRenderableWorldObject;
52  import nl.tudelft.goal.ut2004.visualizer.util.UnrealActors;
53  import nl.tudelft.pogamut.base.server.ServerDefinition;
54  
55  import cz.cuni.amis.pogamut.unreal.bot.IUnrealBot;
56  import cz.cuni.amis.pogamut.unreal.communication.messages.gbinfomessages.IPlayer;
57  import cz.cuni.amis.pogamut.unreal.communication.worldview.map.IUnrealMap;
58  import cz.cuni.amis.pogamut.unreal.communication.worldview.map.IUnrealWaypoint;
59  import cz.cuni.amis.pogamut.unreal.server.IUnrealServer;
60  import cz.cuni.amis.pogamut.ut2004.bot.impl.NativeUT2004BotAdapter;
61  import cz.cuni.amis.pogamut.ut2004.communication.messages.INavPoint;
62  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.AddInventory;
63  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
64  
65  /**
66   * This is the main panel in the Visualizer. It contains the map and scoreboard
67   * overview for the game.
68   * 
69   * @author Lennard de Rijk
70   * 
71   */
72  public class MapPanel extends JPanel {
73  
74  	/**
75  	 * Message displayed if no information is available.
76  	 */
77  	private static final String NO_INFO_MSG = "No information Available, please check connection";
78  
79  	private static final String[] teamNames = new String[] { "Red", "Blue" };
80  
81  	private final PopupMenuSelectionHandler popupMenuSelectionHandler;
82  
83  	private final PureMapTopPanel mapVisualization;
84  
85  	/**
86  	 * The {@link JTextArea} which is used as drawing board for the scores.
87  	 */
88  	// private final JTextArea scoreBoard;
89  
90  	public MapPanel() {
91  		super();
92  
93  		this.setLayout(new BorderLayout());
94  
95  		popupMenuSelectionHandler = new PopupMenuSelectionHandler();
96  
97  		// Setup map visualisation panel.
98  		ServerController controller = ServerController.getInstance();
99  		ServerDefinition<? extends IUnrealServer> serverDefition = controller
100 				.getServerDefinition();
101 		mapVisualization = new PureMapTopPanel(
102 				(ServerDefinition<IUnrealServer>) serverDefition);
103 		mapVisualization.setSelectionHandler(popupMenuSelectionHandler);
104 
105 		this.add(popupMenuSelectionHandler);
106 		this.add(mapVisualization);
107 
108 	}
109 
110 	private class PopupMenuSelectionHandler extends PopupMenu implements
111 			ISelectionHandler {
112 
113 		@Override
114 		public void selected(Set<IRenderableWorldObject> selected, Point at,
115 				Component origin) {
116 
117 			this.removeAll();
118 
119 			if (selected.isEmpty()) {
120 				return;
121 			}
122 
123 			for (IRenderableWorldObject object : selected) {
124 				if (object instanceof IRenderableUTAgent) {
125 					addAgentMenu((IPlayer) object.getDataSource());
126 				} else if (object instanceof RenderableWaypoint) {
127 					addNavPointMenu((IUnrealWaypoint) object.getDataSource(),
128 							at);
129 				}
130 			}
131 
132 			show(mapVisualization, at.x, at.y);
133 		}
134 
135 		private void addNavPointMenu(final IUnrealWaypoint navPoint,
136 				final Point at) {
137 			Menu m = new Menu(navPoint.getID());
138 			{
139 
140 				Menu respawn = getRespawnHereMenu(navPoint);
141 				{
142 
143 					if (respawn != null)
144 						m.add(respawn);
145 				}
146 				Menu addBot = new Menu("Add Bot");
147 				{
148 					MenuItem nativeBot = getAddNativeMenu(navPoint, at);
149 					addBot.add(nativeBot);
150 					MenuItem unrealGoal = getAddUnrealGoalMenu(navPoint, at);
151 					if (unrealGoal != null)
152 						addBot.add(unrealGoal);
153 					m.add(addBot);
154 				}
155 
156 				Menu spawnItem = getSpawnItemMenu(navPoint);
157 				m.add(spawnItem);
158 			}
159 			this.add(m);
160 		}
161 
162 		private Menu getSpawnItemMenu(final IUnrealWaypoint navPoint) {
163 			Menu spawnItem = new Menu("Spawn Item");
164 
165 			for (String id : UnrealActors.INVENTORY_TYPES) {
166 				MenuItem item = new MenuItem(id);
167 				item.addActionListener(new SpawnItemAction(navPoint, id));
168 				spawnItem.add(item);
169 			}
170 			return spawnItem;
171 		}
172 
173 		private MenuItem getAddUnrealGoalMenu(final IUnrealWaypoint navPoint,
174 				final Point at) {
175 
176 			ServerController controller = ServerController.getInstance();
177 			EnvironmentData data = controller.getEnvironmentData();
178 
179 			if (data.getEnvironments().isEmpty()) {
180 				return null;
181 			}
182 
183 			MenuItem unrealGoal = new MenuItem("UnrealGoal");
184 
185 			unrealGoal.addActionListener(new ActionListener() {
186 
187 				@Override
188 				public void actionPerformed(ActionEvent e) {
189 					AddUnrealGoalBotDialog d = new AddUnrealGoalBotDialog(null,
190 							navPoint);
191 					d.setLocationRelativeTo(MapPanel.this);
192 					d.setLocation(at);
193 					d.setVisible(true);
194 
195 				}
196 			});
197 			return unrealGoal;
198 		}
199 
200 		private MenuItem getAddNativeMenu(final IUnrealWaypoint navPoint,
201 				final Point at) {
202 			MenuItem nativeBot = new MenuItem("Native");
203 			nativeBot.addActionListener(new ActionListener() {
204 
205 				@Override
206 				public void actionPerformed(ActionEvent e) {
207 					AddNativeBotDialog d = new AddNativeBotDialog(null,
208 							navPoint);
209 					d.setLocationRelativeTo(MapPanel.this);
210 					d.setLocation(at);
211 					d.setVisible(true);
212 				}
213 			});
214 			return nativeBot;
215 		}
216 
217 		private Menu getRespawnHereMenu(final IUnrealWaypoint navPoint) {
218 			ServerController controller = ServerController.getInstance();
219 			Collection<Player> players = controller.getGameData().getPlayers();
220 
221 			if (players.isEmpty()) {
222 				return null;
223 			}
224 
225 			Menu respawn = new Menu("Respawn (here)");
226 
227 			for (Player p : players) {
228 				MenuItem player = new MenuItem(p.getName());
229 				player.addActionListener(new RespawnHereAction(p, navPoint));
230 				respawn.add(player);
231 			}
232 
233 			return respawn;
234 
235 		}
236 
237 		private void addAgentMenu(IPlayer bot) {
238 
239 			Menu m = new Menu(bot.getName());
240 			{
241 				Menu inventory = getAddInventoryMenu(bot);
242 					
243 				MenuItem kick = new MenuItem("Kick");
244 				kick.addActionListener(new KickAction(bot));
245 
246 				MenuItem change = new MenuItem("Change Team");
247 				change.addActionListener(new ChangeTeamAction(bot));
248 
249 				MenuItem respawn = new MenuItem("Respawn (random)");
250 				respawn.addActionListener(new RespawnRandomAction(bot));
251 
252 
253 				m.add(inventory);
254 				m.add(kick);
255 				m.add(change);
256 				m.add(respawn);
257 			}
258 			this.add(m);
259 		}
260 
261 		private Menu getAddInventoryMenu(IPlayer bot) {
262 			Menu spawnItem = new Menu("Add Inventory");
263 
264 			for (String id : UnrealActors.INVENTORY_TYPES) {
265 				MenuItem item = new MenuItem(id);
266 				item.addActionListener(new AddInventoryAction(bot, id));
267 				spawnItem.add(item);
268 			}
269 			return spawnItem;
270 		}
271 	}
272 
273 }