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.util.Collection;
28  import java.util.Set;
29  
30  import javax.swing.JPanel;
31  import javax.swing.JTextArea;
32  
33  import nl.tudelft.goal.ut2004.visualizer.controller.ServerController;
34  import nl.tudelft.goal.ut2004.visualizer.data.EnvironmentData;
35  import nl.tudelft.goal.ut2004.visualizer.gui.action.AddInventoryAction;
36  import nl.tudelft.goal.ut2004.visualizer.gui.action.ChangeTeamAction;
37  import nl.tudelft.goal.ut2004.visualizer.gui.action.KickAction;
38  import nl.tudelft.goal.ut2004.visualizer.gui.action.RespawnHereAction;
39  import nl.tudelft.goal.ut2004.visualizer.gui.action.RespawnRandomAction;
40  import nl.tudelft.goal.ut2004.visualizer.gui.action.SpawnItemAction;
41  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.AddNativeBotDialog;
42  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.AddUnrealGoalBotDialog;
43  import nl.tudelft.goal.ut2004.visualizer.map.PureMapTopPanel;
44  import nl.tudelft.goal.ut2004.visualizer.services.ISelectionHandler;
45  import nl.tudelft.goal.ut2004.visualizer.util.UnrealActors;
46  import nl.tudelft.pogamut.base.server.ServerDefinition;
47  import cz.cuni.amis.pogamut.unreal.communication.messages.gbinfomessages.IPlayer;
48  import cz.cuni.amis.pogamut.unreal.communication.worldview.map.IUnrealWaypoint;
49  import cz.cuni.amis.pogamut.unreal.server.IUnrealServer;
50  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
51  
52  /**
53   * This is the main panel in the Visualizer. It contains the map and scoreboard
54   * overview for the game.
55   * 
56   * @author Lennard de Rijk
57   * 
58   */
59  public class MapPanel extends JPanel {
60  
61  	/**
62  	 * Message displayed if no information is available.
63  	 */
64  	private static final String NO_INFO_MSG = "No information Available, please check connection";
65  
66  	private static final String[] teamNames = new String[] { "Red", "Blue" };
67  
68  	private final PopupMenuSelectionHandler popupMenuSelectionHandler;
69  
70  	private final PureMapTopPanel mapVisualization;
71  
72  	/**
73  	 * The {@link JTextArea} which is used as drawing board for the scores.
74  	 */
75  	// private final JTextArea scoreBoard;
76  
77  	public MapPanel() {
78  		super();
79  
80  		this.setLayout(new BorderLayout());
81  
82  		popupMenuSelectionHandler = new PopupMenuSelectionHandler();
83  
84  		// Setup map visualisation panel.
85  		ServerController controller = ServerController.getInstance();
86  		ServerDefinition<? extends IUnrealServer> serverDefition = controller.getServerDefinition();
87  		mapVisualization = new PureMapTopPanel((ServerDefinition<IUnrealServer>) serverDefition);
88  		mapVisualization.setSelectionHandler(popupMenuSelectionHandler);
89  
90  		this.add(popupMenuSelectionHandler);
91  		this.add(mapVisualization);
92  
93  	}
94  
95  	private class PopupMenuSelectionHandler extends PopupMenu implements ISelectionHandler {
96  
97  		@Override
98  		public void selected(Set<Object> selected, Point at, Component origin) {
99  
100 			this.removeAll();
101 
102 			if (selected.isEmpty()) {
103 				return;
104 			}
105 
106 			for (Object object : selected) {
107 				if (object instanceof IPlayer) {
108 					addAgentMenu((IPlayer) object);
109 				} else if (object instanceof IUnrealWaypoint) {
110 					addNavPointMenu((IUnrealWaypoint) object, at);
111 				}
112 			}
113 
114 			show(mapVisualization, at.x, at.y);
115 		}
116 
117 		private void addNavPointMenu(final IUnrealWaypoint navPoint, final Point at) {
118 			Menu m = new Menu(navPoint.getID());
119 			{
120 
121 				Menu respawn = getRespawnHereMenu(navPoint);
122 				{
123 
124 					if (respawn != null)
125 						m.add(respawn);
126 				}
127 				Menu addBot = new Menu("Add Bot");
128 				{
129 					MenuItem nativeBot = getAddNativeMenu(navPoint, at);
130 					addBot.add(nativeBot);
131 					MenuItem unrealGoal = getAddUnrealGoalMenu(navPoint, at);
132 					if (unrealGoal != null)
133 						addBot.add(unrealGoal);
134 					m.add(addBot);
135 				}
136 
137 				Menu spawnItem = getSpawnItemMenu(navPoint);
138 				m.add(spawnItem);
139 			}
140 			this.add(m);
141 		}
142 
143 		private Menu getSpawnItemMenu(final IUnrealWaypoint navPoint) {
144 			Menu spawnItem = new Menu("Spawn Item");
145 
146 			for (String id : UnrealActors.INVENTORY_TYPES) {
147 				MenuItem item = new MenuItem(id);
148 				item.addActionListener(new SpawnItemAction(navPoint, id));
149 				spawnItem.add(item);
150 			}
151 			return spawnItem;
152 		}
153 
154 		private MenuItem getAddUnrealGoalMenu(final IUnrealWaypoint navPoint, final Point at) {
155 
156 			ServerController controller = ServerController.getInstance();
157 			EnvironmentData data = controller.getEnvironmentData();
158 
159 			if (data.getEnvironments().isEmpty()) {
160 				return null;
161 			}
162 
163 			MenuItem unrealGoal = new MenuItem("UnrealGoal");
164 
165 			unrealGoal.addActionListener(new ActionListener() {
166 
167 				@Override
168 				public void actionPerformed(ActionEvent e) {
169 					AddUnrealGoalBotDialog d = new AddUnrealGoalBotDialog(null, navPoint);
170 					d.setLocationRelativeTo(MapPanel.this);
171 					d.setLocation(at);
172 					d.setVisible(true);
173 
174 				}
175 			});
176 			return unrealGoal;
177 		}
178 
179 		private MenuItem getAddNativeMenu(final IUnrealWaypoint navPoint, final Point at) {
180 			MenuItem nativeBot = new MenuItem("Native");
181 			nativeBot.addActionListener(new ActionListener() {
182 
183 				@Override
184 				public void actionPerformed(ActionEvent e) {
185 					AddNativeBotDialog d = new AddNativeBotDialog(null, navPoint);
186 					d.setLocationRelativeTo(MapPanel.this);
187 					d.setLocation(at);
188 					d.setVisible(true);
189 				}
190 			});
191 			return nativeBot;
192 		}
193 
194 		private Menu getRespawnHereMenu(final IUnrealWaypoint navPoint) {
195 			ServerController controller = ServerController.getInstance();
196 			Collection<Player> players = controller.getGameData().getPlayers();
197 
198 			if (players.isEmpty()) {
199 				return null;
200 			}
201 
202 			Menu respawn = new Menu("Respawn (here)");
203 
204 			for (Player p : players) {
205 				MenuItem player = new MenuItem(p.getName());
206 				player.addActionListener(new RespawnHereAction(p, navPoint));
207 				respawn.add(player);
208 			}
209 
210 			return respawn;
211 
212 		}
213 
214 		private void addAgentMenu(IPlayer bot) {
215 
216 			Menu m = new Menu(bot.getName());
217 			{
218 				Menu inventory = getAddInventoryMenu(bot);
219 
220 				MenuItem kick = new MenuItem("Kick");
221 				kick.addActionListener(new KickAction(bot));
222 
223 				MenuItem change = new MenuItem("Change Team");
224 				change.addActionListener(new ChangeTeamAction(bot));
225 
226 				MenuItem respawn = new MenuItem("Respawn (random)");
227 				respawn.addActionListener(new RespawnRandomAction(bot));
228 
229 				m.add(inventory);
230 				m.add(kick);
231 				m.add(change);
232 				m.add(respawn);
233 			}
234 			this.add(m);
235 		}
236 
237 		private Menu getAddInventoryMenu(IPlayer bot) {
238 			Menu spawnItem = new Menu("Add Inventory");
239 
240 			for (String id : UnrealActors.INVENTORY_TYPES) {
241 				MenuItem item = new MenuItem(id);
242 				item.addActionListener(new AddInventoryAction(bot, id));
243 				spawnItem.add(item);
244 			}
245 			return spawnItem;
246 		}
247 	}
248 
249 }