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;
18  
19  import java.awt.BorderLayout;
20  import java.awt.FlowLayout;
21  import java.awt.GridLayout;
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  import java.awt.event.KeyEvent;
25  
26  import javax.swing.JFrame;
27  import javax.swing.JMenu;
28  import javax.swing.JMenuBar;
29  import javax.swing.JMenuItem;
30  import javax.swing.JOptionPane;
31  import javax.swing.JPanel;
32  import javax.swing.SwingUtilities;
33  
34  import nl.tudelft.goal.unreal.util.vecmathcheck.VecmathCheck;
35  import nl.tudelft.goal.ut2004.visualizer.controller.ServerController;
36  import nl.tudelft.goal.ut2004.visualizer.gui.action.PauseResumeAction;
37  import nl.tudelft.goal.ut2004.visualizer.gui.action.ShowDialogueAction;
38  import nl.tudelft.goal.ut2004.visualizer.gui.action.ShowServerDependentDialogueAction;
39  import nl.tudelft.goal.ut2004.visualizer.gui.action.ShowServerEnvironmentDependentDialogueAction;
40  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.AddNativeBotDialog;
41  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.AddUnrealGoalBotDialog;
42  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ChangeGameSpeedDialog;
43  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ChangeMapDialog;
44  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ListEnvironmentsDialog;
45  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ListPlayerDialog;
46  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ServerConnectionDialog;
47  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.SettingsDialog;
48  import nl.tudelft.goal.ut2004.visualizer.gui.panels.MapPanel;
49  import nl.tudelft.goal.ut2004.visualizer.util.WindowPersistenceHelper;
50  import nl.tudelft.pogamut.base.server.ReconnectingServerDefinition;
51  import nl.tudelft.pogamut.base.server.ServerDefinition;
52  import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
53  
54  /**
55   * 
56   * This is the main class to start the UnrealVisualizer application. The
57   * UnrealVisualizer is started by calling the constructor for this class with an
58   * {@link UTServer} instance. All messaging from and to the server will be
59   * handled by the application itself.
60   * 
61   * Agents in Pogamut will be known as Bots inside the GUI.
62   * 
63   * @author Lennard de Rijk
64   * @author M.P. Korstanje
65   * 
66   */
67  public class UnrealVisualizerGUI extends JFrame {
68  
69  	/**
70  	 * The width of the application window.
71  	 */
72  	private static final int WINDOW_WIDTH = 800;
73  	/**
74  	 * The height of the application window.
75  	 */
76  	private static final int WINDOW_HEIGHT = 600;
77  
78  	/**
79  	 * The {@link MapPanel} in use by the {@link UnrealVisualizerGUI}.
80  	 */
81  	private final MapPanel mapPanel;
82  	
83  	/**
84  	 * Helper class to persist this window.
85  	 */
86  	private WindowPersistenceHelper persistenceHelper;
87  
88  	public UnrealVisualizerGUI() {
89  		super();
90  		
91  
92  
93  		// Create the controllers
94  		ServerController.createNewController();
95  		ServerController controller = ServerController.getInstance();
96  		ServerDefinition<IUT2004Server> serverDefinition = controller
97  				.getServerDefinition();
98  		
99  		// Set up the main window properties
100 		setTitle("Unreal Tournament 2004 Visualizer for GOAL");
101 		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
102 		setResizable(true);
103 		setLayout(new BorderLayout());
104 		setDefaultCloseOperation(EXIT_ON_CLOSE);
105 
106 		// Setup persistence
107 		persistenceHelper = new WindowPersistenceHelper(this);
108 		persistenceHelper.load();
109 		
110 		// Set up the initial menu bar at the top
111 		setupMenuBar();
112 
113 		// Instantiate the MapPanel and add it to the tabbed pane
114 		mapPanel = new MapPanel();
115 
116 		// Instantiate the mainPanel that contains our tabbed pane
117 		JPanel mainPanel = new JPanel(new GridLayout(1, 1));
118 		mainPanel.add(mapPanel);
119 		add(mainPanel);
120 	}
121 
122 	
123 
124 	/**
125 	 * Setups the {@link JMenuBar} that is shown at the top of the screen.
126 	 */
127 	private void setupMenuBar() {
128 		// This is the main bar that goes on top of the screen
129 		JMenuBar menuBar = new JMenuBar();
130 		menuBar.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
131 		setJMenuBar(menuBar);
132 
133 		// Level 0 menu for the application itself. Contains for instance Exit
134 		// entry.
135 		JMenu applicationMenu = new JMenu("Visualizer");
136 		{
137 			// Add settings menu to Visualizer entry
138 			SettingsDialog settingsDialogue = new SettingsDialog(this);
139 			String name = "Settings";
140 			String description = "Change the settings of the visualizer.";
141 			ShowDialogueAction showSettings = new ShowDialogueAction(settingsDialogue, name, description);
142 			JMenuItem settings = new JMenuItem(showSettings);
143 			applicationMenu.add(settings);
144 			
145 			// Add Exit menu item to Visualizer entry
146 			JMenuItem exitVis = new JMenuItem("Exit", KeyEvent.VK_E);
147 			exitVis.setToolTipText("Exit the Visualizer");
148 			exitVis.addActionListener(new ActionListener() {
149 				@Override
150 				public void actionPerformed(ActionEvent e) {
151 					// Close the application
152 					dispose();
153 					System.exit(0);
154 				}
155 			});
156 			applicationMenu.add(exitVis);
157 			
158 		
159 		}
160 		menuBar.add(applicationMenu);
161 
162 		// Pause resume item
163 		JMenu server = new JMenu("Server");
164 		{
165 			{
166 				ServerController controller = ServerController.getInstance();
167 				ServerDefinition<IUT2004Server> serverDefinition = controller
168 						.getServerDefinition();
169 				ServerConnectionDialog connectionDialog = new ServerConnectionDialog(
170 						this, (ReconnectingServerDefinition) serverDefinition);
171 				String name = "Connection";
172 				String description = "Connect to an Unreal Tournament Server.";
173 				ShowDialogueAction action = new ShowDialogueAction(
174 						connectionDialog, name, description);
175 				JMenuItem connect = new JMenuItem(action);
176 				server.add(connect);
177 			}
178 
179 			JMenuItem pauseResume = new JMenuItem(new PauseResumeAction());
180 			server.add(pauseResume);
181 			{
182 				ChangeGameSpeedDialog gameSpeedDialog = new ChangeGameSpeedDialog(
183 						this);
184 				String name = "Game Speed";
185 				String description = "Change the speed of the game.";
186 				ShowServerDependentDialogueAction action = new ShowServerDependentDialogueAction(
187 						gameSpeedDialog, name, description);
188 				JMenuItem speed = new JMenuItem(action);
189 				server.add(speed);
190 			}
191 
192 			{
193 				ChangeMapDialog changeMapDialog = new ChangeMapDialog(this);
194 				String name = "Change Map";
195 				String description = "Change the current map.";
196 				ShowServerDependentDialogueAction action = new ShowServerDependentDialogueAction(
197 						changeMapDialog, name, description);
198 				JMenuItem change = new JMenuItem(action);
199 				server.add(change);
200 			}
201 			{
202 				AddNativeBotDialog addNativeBotDialog = new AddNativeBotDialog(
203 						this, null);
204 				String name = "Add Native Bot";
205 				String description = "Adds a native unreal bot to the game.";
206 				ShowServerDependentDialogueAction action = new ShowServerDependentDialogueAction(
207 						addNativeBotDialog, name, description);
208 				JMenuItem add = new JMenuItem(action);
209 				server.add(add);
210 			}
211 			{
212 				AddUnrealGoalBotDialog addUnrealGoalBotDialog = new AddUnrealGoalBotDialog(
213 						this, null);
214 				String name = "Add UnrealGoal Bot";
215 				String description = "Adds an UnrealGoal bot to the game.";
216 				ShowServerEnvironmentDependentDialogueAction action = new ShowServerEnvironmentDependentDialogueAction(
217 						addUnrealGoalBotDialog, name, description);
218 				JMenuItem add = new JMenuItem(action);
219 				server.add(add);
220 			}
221 			{
222 				ListPlayerDialog listPlayerDialog = new ListPlayerDialog(this);
223 				String name = "List Players";
224 				String description = "Lists all player in the game.";
225 				ShowServerDependentDialogueAction action = new ShowServerDependentDialogueAction(
226 						listPlayerDialog, name, description);
227 				JMenuItem add = new JMenuItem(action);
228 				server.add(add);
229 			}
230 		}
231 
232 		menuBar.add(server);
233 
234 		JMenu environments = new JMenu("Environments");
235 		{
236 			String name = "List Environments";
237 			String description = "List Goal Environments connected to the visualizier.";
238 			ListEnvironmentsDialog listDialog = new ListEnvironmentsDialog(this);
239 			ShowDialogueAction action = new ShowDialogueAction(listDialog,
240 					name, description);
241 			JMenuItem list = new JMenuItem(action);
242 
243 			environments.add(list);
244 		}
245 		menuBar.add(environments);
246 
247 	}
248 
249 	/**
250 	 * The interface is being closed. We will "try to" dispose all resources in
251 	 * use.
252 	 */
253 	@Override
254 	public void dispose() {
255 		super.dispose();
256 
257 		// The user interface has been disposed, dispose of all other
258 		// resources.
259 		ServerController.disposeController();
260 	}
261 
262 	/**
263 	 * Main method for testing purposes only since we need to launch with an
264 	 * active {@link UTServer} instance
265 	 * 
266 	 * @param args
267 	 */
268 	public static void main(String[] args) {
269 		
270 		if (!VecmathCheck.check()) {
271 			JOptionPane.showMessageDialog(null, VecmathCheck.getErrorMessage(),"Version Conflict",JOptionPane.ERROR_MESSAGE);
272 			
273 			return;
274 		}
275 		
276 		
277 		SwingUtilities.invokeLater(new Runnable() {
278 
279 			@Override
280 			public void run() {
281 				UnrealVisualizerGUI vizualiser = new UnrealVisualizerGUI();
282 				vizualiser.setVisible(true);
283 			}
284 		});
285 	}
286 
287 }