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