aboutsummaryrefslogtreecommitdiff
path: root/src/esieequest/view/app/UserInterface.java
blob: 66a0af120f691e0d118c316854155e54072782ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package esieequest.view.app;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;

import com.wordpress.tipsforjava.swing.StretchIcon;

import esieequest.controller.GameEngine;
import esieequest.controller.commands.Command;
import esieequest.model.events.Quest;
import esieequest.model.items.SimpleItem;
import esieequest.model.map.Direction;
import esieequest.model.map.Orientation;
import esieequest.model.map.Room;
import esieequest.model.map.Side;
import esieequest.view.Viewable;

/**
 * The Swing based graphical user interface.
 * 
 * @author Pacien TRAN-GIRARD
 */
abstract class UserInterface implements Viewable, ActionListener {

	private GameEngine gameEngine;

	private JPanel layout;

	private JTextPane questTextPane;
	private JTextPane infoTextPane;

	private JLabel imageLabel;

	private HashMap<String, JButton> gameButtons;
	private HashMap<String, JButton> controlButtons;
	private JTextField inputField;

	/**
	 * The default constructor.
	 */
	public UserInterface() {
		this.buildUI();
		this.inputField.addActionListener(this);
		this.setActionListener(this);
		this.setControlsState(false);
		this.inputField.setEnabled(true);
	}

	/**
	 * Creates the interface widgets.
	 */
	private void buildUI() {
		this.layout = new JPanel();
		this.layout.setLayout(new BorderLayout(0, 0));

		final JPanel menuPanel = new JPanel();
		this.layout.add(menuPanel, BorderLayout.NORTH);
		menuPanel.setLayout(new BorderLayout(0, 0));

		final JPanel questPanel = new JPanel();
		menuPanel.add(questPanel, BorderLayout.CENTER);

		this.questTextPane = new JTextPane();
		this.questTextPane.setEditable(false);
		this.questTextPane.setText("ESIEEquest");
		questPanel.add(this.questTextPane);

		final JPanel gamePanel = new JPanel();
		menuPanel.add(gamePanel, BorderLayout.WEST);

		final JButton newButton = new JButton("New");
		newButton.setToolTipText("New game");
		gamePanel.add(newButton);

		final JButton soundButton = new JButton("Sound");
		soundButton.setToolTipText("Toggle sound");
		gamePanel.add(soundButton);

		final JPanel filePanel = new JPanel();
		menuPanel.add(filePanel, BorderLayout.EAST);

		final JButton loadButton = new JButton("Load");
		loadButton.setToolTipText("Load a game");
		filePanel.add(loadButton);

		final JButton saveButton = new JButton("Save");
		saveButton.setToolTipText("Save the current game");
		filePanel.add(saveButton);

		final JPanel imagePanel = new JPanel();
		this.layout.add(imagePanel, BorderLayout.CENTER);
		imagePanel.setLayout(new BorderLayout(0, 0));

		this.imageLabel = new JLabel();
		imagePanel.add(this.imageLabel);

		final JPanel userPanel = new JPanel();
		this.layout.add(userPanel, BorderLayout.SOUTH);
		userPanel.setLayout(new BorderLayout(0, 0));

		final JPanel dispPanel = new JPanel();
		dispPanel.setBorder(new EmptyBorder(5, 5, 5, 0));
		userPanel.add(dispPanel, BorderLayout.CENTER);
		dispPanel.setLayout(new BorderLayout(0, 0));

		this.infoTextPane = new JTextPane();
		this.infoTextPane.setEditable(false);
		this.infoTextPane.setText("Welcome to ESIEEquest!");
		dispPanel.add(this.infoTextPane);

		this.inputField = new JTextField();
		dispPanel.add(this.inputField, BorderLayout.SOUTH);
		this.inputField.setColumns(10);

		final JPanel controlPanel = new JPanel();
		controlPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		userPanel.add(controlPanel, BorderLayout.EAST);
		controlPanel.setLayout(new BorderLayout(0, 0));

		final JPanel topControlPanel = new JPanel();
		controlPanel.add(topControlPanel, BorderLayout.NORTH);
		topControlPanel.setLayout(new BorderLayout(0, 0));

		final Font font = new Font("Dialog", Font.BOLD, 19);

		final JButton forwardButton = new JButton("↥");
		forwardButton.setFont(font);
		forwardButton.setToolTipText("Go forward");
		topControlPanel.add(forwardButton, BorderLayout.CENTER);

		final JButton inventoryButton = new JButton("⇱");
		inventoryButton.setFont(font);
		inventoryButton.setToolTipText("Use item");
		topControlPanel.add(inventoryButton, BorderLayout.WEST);

		final JButton actionButton = new JButton("⇲");
		actionButton.setFont(font);
		actionButton.setToolTipText("Talk/Take item");
		topControlPanel.add(actionButton, BorderLayout.EAST);

		final JPanel bottomControlPanel = new JPanel();
		controlPanel.add(bottomControlPanel, BorderLayout.SOUTH);
		bottomControlPanel.setLayout(new BorderLayout(0, 0));

		final JButton backButton = new JButton("↧");
		backButton.setFont(font);
		backButton.setToolTipText("Go back");
		bottomControlPanel.add(backButton, BorderLayout.CENTER);

		final JButton leftButton = new JButton("↰");
		leftButton.setFont(font);
		leftButton.setToolTipText("Turn left");
		bottomControlPanel.add(leftButton, BorderLayout.WEST);

		final JButton rightButton = new JButton("↱");
		rightButton.setFont(font);
		rightButton.setToolTipText("Turn right");
		bottomControlPanel.add(rightButton, BorderLayout.EAST);

		final Dimension squareButton = new Dimension(50, 50);
		forwardButton.setPreferredSize(squareButton);
		inventoryButton.setPreferredSize(squareButton);
		actionButton.setPreferredSize(squareButton);
		backButton.setPreferredSize(squareButton);
		leftButton.setPreferredSize(squareButton);
		rightButton.setPreferredSize(squareButton);

		newButton.setActionCommand(Command.NEW.name());
		soundButton.setActionCommand(Command.SOUND.name());
		loadButton.setActionCommand(Command.LOAD.name());
		saveButton.setActionCommand(Command.SAVE.name());

		forwardButton.setActionCommand(Command.FORWARD.name());
		inventoryButton.setActionCommand(Command.INVENTORY.name());
		actionButton.setActionCommand(Command.DO.name());
		backButton.setActionCommand(Command.BACK.name());
		leftButton.setActionCommand(Command.TURN.name() + " " + Orientation.LEFT);
		rightButton.setActionCommand(Command.TURN.name() + " " + Orientation.RIGHT);

		this.gameButtons = new HashMap<String, JButton>();

		this.gameButtons.put("newButton", newButton);
		this.gameButtons.put("soundButton", soundButton);
		this.gameButtons.put("loadButton", loadButton);
		this.gameButtons.put("saveButton", saveButton);

		this.controlButtons = new HashMap<String, JButton>();

		this.controlButtons.put("forwardButton", forwardButton);
		this.controlButtons.put("inventoryButton", inventoryButton);
		this.controlButtons.put("actionButton", actionButton);
		this.controlButtons.put("backButton", backButton);
		this.controlButtons.put("leftButton", leftButton);
		this.controlButtons.put("rightButton", rightButton);
	}

	/**
	 * Returns the layout.
	 * 
	 * @return the layout
	 */
	public JPanel getLayout() {
		return this.layout;
	}

	/**
	 * Sets the action listener for the given JButtons.
	 * 
	 * @param hashMap
	 *            the map of the JButtons
	 * @param actionListener
	 *            the action listener
	 */
	private void setActionListener(final HashMap<String, JButton> hashMap, final ActionListener actionListener) {
		for (final JButton vButton : hashMap.values()) {
			vButton.addActionListener(actionListener);
		}
	}

	/**
	 * Sets the action listener for all UI elements.
	 * 
	 * @param actionListener
	 *            the action listener
	 */
	public void setActionListener(final ActionListener actionListener) {
		this.inputField.addActionListener(actionListener);
		this.setActionListener(this.gameButtons, actionListener);
		this.setActionListener(this.controlButtons, actionListener);
	}

	/**
	 * Clears the textual input field.
	 */
	private void clearInputField() {
		this.inputField.setText(null);
	}

	/**
	 * Sets the controls state.
	 * 
	 * @param state
	 *            the controls state
	 */
	private void setControlsState(final boolean state) {
		this.inputField.setEnabled(state);
		for (final JButton vButton : this.controlButtons.values()) {
			vButton.setEnabled(state);
		}
	}

	/**
	 * Sets the quest title.
	 * 
	 * @param quest
	 *            the quest title
	 */
	private void setQuestLabel(final String questTitle) {
		this.questTextPane.setText(questTitle);
	}

	/**
	 * Updates the room's illustration.
	 * 
	 * Placeholder generation from http://stackoverflow.com/a/17802477/1348634
	 */
	private void updateIllustration(final String imageName) {
		final URL imageURL = this.getClass().getClassLoader().getResource(imageName + ".jpg");
		final StretchIcon imageIcon;
		if (imageURL == null) {
			final BufferedImage bufferedImage = new BufferedImage(240, 135, BufferedImage.TYPE_BYTE_BINARY);
			final Graphics graphics = bufferedImage.getGraphics();
			graphics.drawString(imageName, 5, 10 + 5);
			imageIcon = new StretchIcon(bufferedImage, true);
		} else {
			imageIcon = new StretchIcon(imageURL, true);
		}
		this.imageLabel.setIcon(imageIcon);
	}

	/**
	 * Translates the received ActionEvent into the corresponding game command
	 * and forwards it to the game engine.
	 */
	@Override
	public void actionPerformed(final ActionEvent actionEvent) {
		this.gameEngine.interpret(actionEvent.getActionCommand());
	}

	@Override
	public void setController(final GameEngine gameEngine) {
		this.gameEngine = gameEngine;
	}

	@Override
	public void enable() {
		this.setControlsState(true);
	}

	@Override
	public void disable() {
		this.setControlsState(false);
		this.setQuestLabel("ESIEEquest");
	}

	@Override
	public void echo(final String message) {
		this.infoTextPane.setText(message);
		this.clearInputField();
	}

	@Override
	public void updateLocation(final Room room, final Direction direction, final Side side) {
		this.echo(room.getInformations());
		this.updateIllustration(room.name() + "_" + direction.name());
	}

	@Override
	public void updateQuest(final Quest quest) {
		this.setQuestLabel(quest.getTitle());
	}

	@Override
	public void updateInventory(final HashMap<String, SimpleItem> items) {
		// TODO Auto-generated method stub
	}

}