aboutsummaryrefslogtreecommitdiff
path: root/src/esieequest/ui/rich/UserInterface.java
blob: c743d686154c56407d1157a47761e3ac68d05f9d (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
package esieequest.ui.rich;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import lombok.Getter;

import org.newdawn.easyogg.OggClip;

import com.wordpress.tipsforjava.swing.StretchIcon;

import esieequest.engine.GameEngine;
import esieequest.engine.commands.Command;
import esieequest.game.Text;
import esieequest.game.items.Inventory;
import esieequest.game.items.Item;
import esieequest.game.map.Direction;
import esieequest.game.map.Orientation;
import esieequest.game.map.Room;
import esieequest.game.map.Side;
import esieequest.game.states.Callback;
import esieequest.game.states.Quest;
import esieequest.game.states.Scene;
import esieequest.ui.Viewable;

/**
 * The Swing based graphical user interface.
 * 
 * @author Pacien TRAN-GIRARD
 * @author Benoît LUBRANO DI SBARAGLIONE
 */
abstract class UserInterface implements Viewable, ActionListener {

	private static final String ILLUSTRATION_DIR = "resources/images/";
	private static final String ILLUSTRATION_EXT = ".png";

	private static final String SOUND_DIR = "resources/audio/";
	private static final String SOUND_EXT = ".ogg";

	private static final String SAVE_LABEL = "ESIEEquest Game";
	private static final String SAVE_EXT = "eqg";

	private GameEngine gameEngine;

	@Getter
	private JPanel layout;

	private JTextPane questTextPane;

	private JLabel imageLabel;

	private JPanel menuPanel;
	private JPanel questPanel;
	private JPanel gamePanel;

	private JButton newButton;
	private JButton soundButton;
	private JPanel filePanel;
	private JButton loadButton;
	private JButton saveButton;

	private JPanel imagePanel;

	private JPanel userPanel;
	private JPanel dispPanel;

	private JPanel consolePanel;
	private JPanel inventoryPanel;

	private JPanel controlPanel;
	private JPanel topControlPanel;
	private JPanel bottomControlPanel;

	private JTextPane infoTextPane;
	private JTextField inputField;

	private JButton forwardButton;
	private JButton inventoryButton;
	private JButton actionButton;
	private JButton backButton;
	private JButton leftButton;
	private JButton rightButton;

	private Timer timer;
	private TimerTask timerTask;
	private boolean scenePlaying;

	private OggClip audio;
	private boolean muted;

	/**
	 * The default constructor.
	 */
	public UserInterface() {
		this.buildUI();
		this.setActionListener(this);
		this.bindKeys();
		this.bindFocus();
		this.setControlsState(false);
		this.muted = false;
	}

	/**
	 * Creates the interface widgets.
	 */
	private void buildUI() {

		// main window
		this.layout = new JPanel();
		this.layout.setLayout(new BorderLayout(0, 0));

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

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

		this.questTextPane = new JTextPane();
		this.questTextPane.setEditable(false);
		this.questTextPane.setText(Text.DEFAULT_QUEST_TITLE.toString());
		this.questPanel.add(this.questTextPane);

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

		this.newButton = new JButton(Text.NEW_GAME_BUTTON.toString());
		this.newButton.setToolTipText(Text.NEW_GAME_TOOLTIP.toString());
		this.newButton.setActionCommand(Command.NEW.name());
		this.gamePanel.add(this.newButton);

		this.soundButton = new JButton(Text.TOGGLE_SOUND_BUTTON.toString());
		this.soundButton.setToolTipText(Text.TOGGLE_SOUND_TOOLTIP.toString());
		this.soundButton.setActionCommand(Command.SOUND.name());
		this.gamePanel.add(this.soundButton);

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

		this.loadButton = new JButton(Text.LOAD_GAME_BUTTON.toString());
		this.loadButton.setToolTipText(Text.LOAD_GAME_TOOLTIP.toString());
		this.loadButton.setActionCommand(Command.LOAD.name());
		this.filePanel.add(this.loadButton);

		this.saveButton = new JButton(Text.SAVE_GAME_BUTTON.toString());
		this.saveButton.setToolTipText(Text.SAVE_GAME_TOOLTIP.toString());
		this.saveButton.setActionCommand(Command.SAVE.name());
		this.filePanel.add(this.saveButton);

		// central illustration panel
		this.imagePanel = new JPanel();
		this.layout.add(this.imagePanel, BorderLayout.CENTER);
		this.imagePanel.setLayout(new BorderLayout(0, 0));

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

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

		// console and inventory
		this.dispPanel = new JPanel();
		this.dispPanel.setBorder(new EmptyBorder(5, 5, 5, 0));
		this.userPanel.add(this.dispPanel, BorderLayout.CENTER);
		this.dispPanel.setLayout(new BorderLayout(0, 0));

		this.consolePanel = new JPanel();
		this.dispPanel.add(this.consolePanel, BorderLayout.CENTER);
		this.consolePanel.setLayout(new BorderLayout(0, 0));

		this.infoTextPane = new JTextPane();
		this.consolePanel.add(this.infoTextPane, BorderLayout.CENTER);
		this.infoTextPane.setEditable(false);
		this.infoTextPane.setText(Text.DEFAULT_CONSOLE.toString());

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

		this.inventoryPanel = new JPanel();
		this.dispPanel.add(this.inventoryPanel, BorderLayout.NORTH);
		this.inventoryPanel.setLayout(new GridLayout(4, 3, 0, 0));

		final JTextPane emptyTextPane = new JTextPane();
		emptyTextPane.setText(Text.INVENTORY_EMPTY.toString());
		this.inventoryPanel.add(emptyTextPane);
		this.inventoryPanel.setVisible(false);

		// control buttons
		this.controlPanel = new JPanel();
		this.controlPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		this.userPanel.add(this.controlPanel, BorderLayout.EAST);
		this.controlPanel.setLayout(new BorderLayout(0, 0));

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

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

		this.forwardButton = new JButton(Text.GO_FORWARD_BUTTON.toString());
		this.forwardButton.setToolTipText(Text.GO_FORWARD_TOOLTIP.toString());
		this.forwardButton.setActionCommand(Command.GO.name());
		this.forwardButton.setFont(font);
		this.forwardButton.setPreferredSize(squareButton);
		this.topControlPanel.add(this.forwardButton, BorderLayout.CENTER);

		this.inventoryButton = new JButton(Text.INVENTORY_BUTTON.toString());
		this.inventoryButton.setToolTipText(Text.INVENTORY_TOOLTIP.toString());
		this.inventoryButton.setActionCommand(Command.INVENTORY.name());
		this.inventoryButton.setFont(font);
		this.inventoryButton.setPreferredSize(squareButton);
		this.topControlPanel.add(this.inventoryButton, BorderLayout.WEST);

		this.actionButton = new JButton(Text.ACTION_BUTTON.toString());
		this.actionButton.setToolTipText(Text.ACTION_TOOLTIP.toString());
		this.actionButton.setActionCommand(Command.DO.name());
		this.actionButton.setFont(font);
		this.actionButton.setPreferredSize(squareButton);
		this.topControlPanel.add(this.actionButton, BorderLayout.EAST);

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

		this.backButton = new JButton(Text.GO_BACK_BUTTON.toString());
		this.backButton.setToolTipText(Text.GO_BACK_TOOLTIP.toString());
		this.backButton.setActionCommand(Command.BACK.name());
		this.backButton.setFont(font);
		this.backButton.setPreferredSize(squareButton);
		this.bottomControlPanel.add(this.backButton, BorderLayout.CENTER);

		this.leftButton = new JButton(Text.TURN_LEFT_BUTTON.toString());
		this.leftButton.setToolTipText(Text.TURN_LEFT_TOOLTIP.toString());
		this.leftButton.setActionCommand(Command.TURN.name() + Text.COMMAND_SEPARATOR.toString()
				+ Orientation.LEFT.name());
		this.leftButton.setFont(font);
		this.leftButton.setPreferredSize(squareButton);
		this.bottomControlPanel.add(this.leftButton, BorderLayout.WEST);

		this.rightButton = new JButton(Text.TURN_RIGHT_BUTTON.toString());
		this.rightButton.setToolTipText(Text.TURN_RIGHT_TOOLTIP.toString());
		this.rightButton.setActionCommand(Command.TURN.name() + Text.COMMAND_SEPARATOR.toString()
				+ Orientation.RIGHT.name());
		this.rightButton.setFont(font);
		this.rightButton.setPreferredSize(squareButton);
		this.bottomControlPanel.add(this.rightButton, BorderLayout.EAST);

	}

	/**
	 * Sets the action listener for all UI elements.
	 * 
	 * @param actionListener
	 *            the action listener
	 */
	private void setActionListener(final ActionListener actionListener) {
		this.inputField.addActionListener(actionListener);
		this.newButton.addActionListener(actionListener);
		this.soundButton.addActionListener(actionListener);
		this.loadButton.addActionListener(actionListener);
		this.saveButton.addActionListener(actionListener);
		this.forwardButton.addActionListener(actionListener);
		this.inventoryButton.addActionListener(actionListener);
		this.actionButton.addActionListener(actionListener);
		this.backButton.addActionListener(actionListener);
		this.leftButton.addActionListener(actionListener);
		this.rightButton.addActionListener(actionListener);
	}

	/**
	 * Binds keys to corresponding buttons.
	 */
	private void bindKeys() {

		final HashMap<Integer, JButton> keys = new HashMap<>();
		keys.put(KeyEvent.VK_LEFT, this.leftButton);
		keys.put(KeyEvent.VK_RIGHT, this.rightButton);
		keys.put(KeyEvent.VK_UP, this.forwardButton);
		keys.put(KeyEvent.VK_DOWN, this.backButton);
		keys.put(KeyEvent.VK_HOME, this.inventoryButton);
		keys.put(KeyEvent.VK_PAGE_UP, this.actionButton);

		for (final Entry<Integer, JButton> entry : keys.entrySet()) {
			final int key = entry.getKey();
			final JButton button = entry.getValue();

			this.layout.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
					KeyStroke.getKeyStroke(key, 0), key);

			this.layout.getActionMap().put(key, new AbstractAction() {

				private static final long serialVersionUID = 1L;

				@Override
				public void actionPerformed(final ActionEvent actionEvent) {
					button.doClick();
				}

			});
		}

		this.layout.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
				KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), KeyEvent.VK_ESCAPE);

		this.layout.getActionMap().put(KeyEvent.VK_ESCAPE, new AbstractAction() {

			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(final ActionEvent actionEvent) {
				UserInterface.this.skipScene();
			}

		});

	}

	/**
	 * Binds focus catching components to delegate key strokes to the main
	 * window.
	 */
	public void bindFocus() {
		final FocusListener focusLossDelegator = new FocusListener() {
			@Override
			public void focusGained(final FocusEvent focusEvent) {
			}

			@Override
			public void focusLost(final FocusEvent focusEvent) {
				UserInterface.this.layout.requestFocus();
			}
		};

		final FocusListener focusGainDelegator = new FocusListener() {
			@Override
			public void focusGained(final FocusEvent focusEvent) {
				UserInterface.this.layout.requestFocus();
			}

			@Override
			public void focusLost(final FocusEvent focusEvent) {
			}
		};

		this.inputField.addFocusListener(focusLossDelegator);
		this.infoTextPane.addFocusListener(focusGainDelegator);
		this.questTextPane.addFocusListener(focusGainDelegator);
	}

	/**
	 * 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);
		this.saveButton.setEnabled(state);
		this.inventoryButton.setEnabled(state);
		this.leftButton.setEnabled(state);
		this.rightButton.setEnabled(state);
		if (!state) {
			this.forwardButton.setEnabled(state);
			this.backButton.setEnabled(state);
			this.actionButton.setEnabled(state);
		}
	}

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

	/**
	 * Sets the Room's illustration.
	 * 
	 * @param imageName
	 *            the name of the image
	 */
	private void setIllustration(final String imageName) {
		final URL imageURL = this.getClass().getClassLoader().getResource(
				UserInterface.ILLUSTRATION_DIR + imageName + UserInterface.ILLUSTRATION_EXT);
		final StretchIcon imageIcon;
		if (imageURL == null) {
			imageIcon = new StretchIcon(this.generatePlaceholderImage(imageName), true);
		} else {
			imageIcon = new StretchIcon(imageURL, true);
		}
		this.imageLabel.setIcon(imageIcon);
	}

	/**
	 * Placeholder image generation from
	 * http://stackoverflow.com/a/17802477/1348634
	 * 
	 * @param text
	 *            the image text
	 * @return the Image
	 */
	private Image generatePlaceholderImage(final String text) {
		final BufferedImage bufferedImage = new BufferedImage(240, 135,
				BufferedImage.TYPE_BYTE_BINARY);
		final Graphics graphics = bufferedImage.getGraphics();
		graphics.drawString(text, 5, 10 + 5);
		return bufferedImage;
	}

	/**
	 * Skips the currently player Scene.
	 */
	private void skipScene() {
		if (this.scenePlaying) {
			this.timerTask.run();
			this.timerTask.cancel();
		}
	}

	/**
	 * Opens the inventory (switches to the inventory tab).
	 */
	private void openInventory() {
		this.consolePanel.setVisible(false);
		this.inventoryPanel.setVisible(true);
	}

	/**
	 * Closes the inventory (switches to the console tab).
	 */
	private void closeInventory() {
		this.inventoryPanel.setVisible(false);
		this.consolePanel.setVisible(true);
	}

	/**
	 * Toggles the inventory tab.
	 */
	private void toggleInventory() {
		if (this.inventoryPanel.isVisible()) {
			this.closeInventory();
		} else {
			this.openInventory();
		}
	}

	/**
	 * Sets the current music playing.
	 * 
	 * @param fileName
	 *            the URL of the audio file
	 */
	private void playAudio(final String fileName) {
		if (this.audio != null) {
			// stop() and close() are not working properly...
			this.audio.pause();
		}

		try {
			this.audio = new OggClip(UserInterface.SOUND_DIR + fileName + UserInterface.SOUND_EXT);
			this.audio.play();
			this.setAudioGain();
		} catch (final IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Sets the gain.
	 * 
	 * PulseAudio does not comply with the JSAPI => pause/play instead of muting
	 */
	private void setAudioGain() {
		if (this.audio == null) {
			return;
		}
		if (this.audio.stopped()) {
			return;
		}

		if (this.muted) {
			// this.audio.setGain(0.0f);
			this.audio.pause();
		} else if (this.audio.isPaused()) {
			// this.audio.setGain(-1.0f);
			this.audio.resume();
		}
	}

	/**
	 * Translates the received ActionEvent into the corresponding game command
	 * and forwards it to the game engine.
	 */
	@Override
	public void actionPerformed(final ActionEvent actionEvent) {
		if (actionEvent.getActionCommand() == Command.INVENTORY.name()) {
			this.toggleInventory();
		} else if (actionEvent.getActionCommand() == Command.LOAD.name()) {
			final JFileChooser fileChooser = new JFileChooser();
			fileChooser.setFileFilter(new FileNameExtensionFilter(UserInterface.SAVE_LABEL,
					UserInterface.SAVE_EXT));
			final int option = fileChooser.showOpenDialog(this.layout);
			if (option == JFileChooser.APPROVE_OPTION) {
				this.gameEngine.interpret(actionEvent.getActionCommand() + Text.COMMAND_SEPARATOR
						+ fileChooser.getSelectedFile().getPath());
			}
		} else if (actionEvent.getActionCommand() == Command.SAVE.name()) {
			final JFileChooser fileChooser = new JFileChooser();
			fileChooser.setFileFilter(new FileNameExtensionFilter(UserInterface.SAVE_LABEL,
					UserInterface.SAVE_EXT));
			final int option = fileChooser.showSaveDialog(this.layout);
			if (option == JFileChooser.APPROVE_OPTION) {
				final String path = fileChooser.getSelectedFile().getPath();
				final String filePath = path.endsWith(UserInterface.SAVE_EXT) ? path : path + "."
						+ UserInterface.SAVE_EXT;
				this.gameEngine.interpret(actionEvent.getActionCommand() + Text.COMMAND_SEPARATOR
						+ filePath);
			}
		} else {
			this.gameEngine.interpret(actionEvent.getActionCommand());
			this.clearInputField();
		}
	}

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

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

	@Override
	public void disableInput() {
		this.setControlsState(false);
		this.setQuestLabel(Text.DEFAULT_QUEST_TITLE.toString());
	}

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

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

	@Override
	public void updateLocation(final Room room, final Direction direction, final Side side,
			final boolean canGoBack) {
		String locationImageName = room.name() + Text.FILENAME_SEPARATOR.toString()
				+ direction.name();
		if (side.getInventory().getSize() > 0) {
			locationImageName += Text.FILENAME_SEPARATOR.toString()
					+ side.getInventory().getItem().name();
		}
		this.setIllustration(locationImageName);
		this.backButton.setEnabled(canGoBack);
		this.forwardButton.setEnabled(side.hasDoor());
		this.actionButton.setEnabled(side.hasCharacter() || (side.getInventory().getSize() > 0));

		this.echo(room.getInformations());
	}

	@Override
	public void updateInventory(final Inventory inventory) {
		this.inventoryPanel.removeAll();

		if (inventory.getSize() < 1) {
			final JTextPane emptyTextPane = new JTextPane();
			emptyTextPane.setText(Text.INVENTORY_EMPTY.toString());
			this.inventoryPanel.add(emptyTextPane);
			return;
		}

		for (final Item item : inventory.getItems().values()) {
			final JButton button = new JButton(item.getName());
			button.setActionCommand(Command.USE.name() + Text.COMMAND_SEPARATOR.toString()
					+ item.getName());
			button.addActionListener(this);
			this.inventoryPanel.add(button);
		}

		this.openInventory();
	}

	@Override
	public void playScene(final Scene scene, final Callback callback) {
		if (this.scenePlaying) {
			this.timerTask.cancel();
		}

		this.disableInput();

		this.setQuestLabel(scene.getTitle());
		this.echo(scene.getText());
		this.setIllustration(scene.name());
		this.playAudio(scene.name());

		this.scenePlaying = true;

		this.timerTask = new TimerTask() {
			@Override
			public void run() {
				callback.call();
				UserInterface.this.scenePlaying = false;
			}
		};

		if (this.timer != null) {
			this.timer.cancel();
		}
		this.timer = new Timer();
		this.timer.schedule(this.timerTask, scene.getDuration());
	}

	@Override
	public void stopMusic() {
		if (this.audio != null) {
			// stop() and close() are not working properly...
			this.audio.pause();
		}
	}

	@Override
	public void toggleSound() {
		this.muted = !this.muted;
		this.setAudioGain();
	}

	@Override
	public String load(final String path) {
		final Path filePath = Paths.get(path);
		String serialisedGame = null;
		try {
			serialisedGame = new String(Files.readAllBytes(filePath));
		} catch (final IOException e) {
			e.printStackTrace();
		}
		this.skipScene();
		return serialisedGame;
	}

	@Override
	public void save(final String path, final String serialisedGame) {
		try {
			final FileWriter fileWriter = new FileWriter(path);
			fileWriter.write(serialisedGame);
			fileWriter.close();
		} catch (final IOException e) {
			e.printStackTrace();
		}
	}

}