aboutsummaryrefslogtreecommitdiff
path: root/src/esieequest/view/web/WebInterface.java
blob: e749c1f1934ddb2946f76969750ce0d6a909be99 (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
package esieequest.view.web;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.media.client.Audio;
import com.google.gwt.storage.client.Storage;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import esieequest.controller.GameEngine;
import esieequest.controller.commands.Command;
import esieequest.model.Text;
import esieequest.model.events.Quest;
import esieequest.model.events.Scene;
import esieequest.model.items.Inventory;
import esieequest.model.items.Item;
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 web view.
 * 
 * @author Pacien TRAN-GIRARD
 */
class WebInterface extends Composite implements Viewable {

	private static final String ILLUSTRATION_DIR = "res/frame/";
	private static final String ILLUSTRATION_EXT = ".html";

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

	private static final String SAVE_KEY = "eqg";

	private GameEngine gameEngine;

	private static WebInterfaceUiBinder uiBinder = GWT.create(WebInterfaceUiBinder.class);

	@UiField
	TabLayoutPanel bottomPanel;
	@UiField
	FlowPanel inventoryPanel;
	@UiField
	Label displayLabel;
	@UiField
	Label questLabel;
	@UiField
	Frame illustrationFrame;
	@UiField
	TextBox inputField;
	@UiField
	Button newButton;
	@UiField
	Button soundButton;
	@UiField
	Button saveButton;
	@UiField
	Button loadButton;
	@UiField
	Button inventoryButton;
	@UiField
	Button forwardButton;
	@UiField
	Button actionButton;
	@UiField
	Button rightButton;
	@UiField
	Button backButton;
	@UiField
	Button leftButton;

	private final Audio audio;
	private Timer timer;

	private final Storage storage;

	/**
	 * The web user interface binder interface.
	 */
	interface WebInterfaceUiBinder extends UiBinder<Widget, WebInterface> {
	}

	/**
	 * The default constructor that initialises the user interface and binds
	 * event handlers.
	 */
	public WebInterface() {
		this.initWidget(WebInterface.uiBinder.createAndBindUi(this));

		this.displayLabel.setText(Text.DEFAULT_CONSOLE.toString());
		this.questLabel.setText(Text.DEFAULT_QUEST_TITLE.toString());

		final Label emptyLabel = new Label(Text.INVENTORY_EMPTY.toString());
		this.inventoryPanel.add(emptyLabel);

		this.bindInputField();
		this.bindKeys();
		this.bindButtons();

		this.storage = Storage.getLocalStorageIfSupported();

		this.audio = Audio.createIfSupported();
		if (this.audio != null) {
			this.bottomPanel.add(this.audio);
		}
	}

	/**
	 * Forwards a command string to the game engine for interpretation.
	 * 
	 * @param commandString
	 *            the command string
	 */
	private void interpret(final String commandString) {
		this.gameEngine.interpret(commandString);
	}

	/**
	 * Creates a custom click handler that sends a command to the controller on
	 * click.
	 * 
	 * @param command
	 *            the command String
	 * @return the click handler
	 */
	private ClickHandler makeClickHandler(final String command) {
		return new ClickHandler() {
			@Override
			public void onClick(final ClickEvent event) {
				WebInterface.this.closeInventory();
				WebInterface.this.interpret(command);
			}
		};
	}

	/**
	 * Binds the textual input field to send the entered command to the
	 * controller.
	 */
	private void bindInputField() {
		this.inputField.addKeyDownHandler(new KeyDownHandler() {
			@Override
			public void onKeyDown(final KeyDownEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					WebInterface.this.interpret(WebInterface.this.inputField.getText());
					WebInterface.this.inputField.setText(null);
				}
			}
		});
	}

	/**
	 * Binds keys
	 */
	private void bindKeys() {
		Event.addNativePreviewHandler(new NativePreviewHandler() {
			@Override
			public void onPreviewNativeEvent(final NativePreviewEvent event) {
				if (WebInterface.this.inputField.getText().length() > 0) {
					return;
				}

				if (event.getTypeInt() != Event.ONKEYDOWN) {
					return;
				}

				switch (event.getNativeEvent().getKeyCode()) {
				case KeyCodes.KEY_LEFT:
					WebInterface.this.leftButton.click();
					break;
				case KeyCodes.KEY_RIGHT:
					WebInterface.this.rightButton.click();
					break;
				case KeyCodes.KEY_UP:
					WebInterface.this.forwardButton.click();
					break;
				case KeyCodes.KEY_DOWN:
					WebInterface.this.backButton.click();
					break;
				case KeyCodes.KEY_HOME:
					WebInterface.this.inventoryButton.click();
					break;
				case KeyCodes.KEY_PAGEUP:
					WebInterface.this.actionButton.click();
					break;
				case KeyCodes.KEY_ESCAPE:
					WebInterface.this.skipScene();
					break;
				default:
					WebInterface.this.inputField.setFocus(true);
					break;
				}
			}
		});
	}

	/**
	 * Binds the buttons to send a defined command to the controller.
	 */
	private void bindButtons() {
		// menu
		this.newButton.setText(Text.NEW_GAME_BUTTON.toString());
		this.newButton.setTitle(Text.NEW_GAME_TOOLTIP.toString());
		this.newButton.addClickHandler(this.makeClickHandler(Command.NEW.name()));

		this.soundButton.setText(Text.TOGGLE_SOUND_BUTTON.toString());
		this.soundButton.setTitle(Text.TOGGLE_SOUND_TOOLTIP.toString());
		this.soundButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(final ClickEvent event) {
				WebInterface.this.toggleAudio();
			}
		});

		this.loadButton.setText(Text.LOAD_GAME_BUTTON.toString());
		this.loadButton.setTitle(Text.LOAD_GAME_TOOLTIP.toString());
		this.loadButton.addClickHandler(this.makeClickHandler(Command.LOAD.name()
				+ Text.COMMAND_SEPARATOR + WebInterface.SAVE_KEY));

		this.saveButton.setText(Text.SAVE_GAME_BUTTON.toString());
		this.saveButton.setTitle(Text.SAVE_GAME_TOOLTIP.toString());
		this.saveButton.addClickHandler(this.makeClickHandler(Command.SAVE.name()
				+ Text.COMMAND_SEPARATOR + WebInterface.SAVE_KEY));

		// controls
		this.actionButton.setTitle(Text.ACTION_TOOLTIP.toString());
		this.actionButton.addClickHandler(this.makeClickHandler(Command.DO.name()));

		this.inventoryButton.setTitle(Text.INVENTORY_TOOLTIP.toString());
		this.inventoryButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(final ClickEvent event) {
				WebInterface.this.toggleInventory();
			}
		});
		this.forwardButton.setTitle(Text.GO_FORWARD_TOOLTIP.toString());
		this.forwardButton.addClickHandler(this.makeClickHandler(Command.FORWARD.name()));

		this.backButton.setTitle(Text.GO_BACK_TOOLTIP.toString());
		this.backButton.addClickHandler(this.makeClickHandler(Command.BACK.name()));

		this.leftButton.setTitle(Text.TURN_LEFT_TOOLTIP.toString());
		this.leftButton.addClickHandler(this.makeClickHandler(Command.TURN.name()
				+ Text.COMMAND_SEPARATOR.toString() + Orientation.LEFT.name()));

		this.rightButton.setTitle(Text.TURN_RIGHT_TOOLTIP.toString());
		this.rightButton.addClickHandler(this.makeClickHandler(Command.TURN.name()
				+ Text.COMMAND_SEPARATOR.toString() + Orientation.RIGHT.name()));

	}

	/**
	 * Sets the controls (the text input field and the control buttons) state.
	 * 
	 * @param state
	 *            the controls state
	 */
	private void setControlsEnabled(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 label.
	 * 
	 * @param quest
	 *            the quest title
	 */
	private void setQuestLabel(final String quest) {
		this.questLabel.setText(quest);
	}

	/**
	 * Sets the current music playing.
	 * 
	 * @param fileName
	 *            the URL of the audio file
	 */
	private void playAudio(final String fileName) {
		if (this.audio == null) {
			return;
		}
		this.audio.setSrc(WebInterface.SOUND_DIR + fileName + WebInterface.SOUND_EXT);
		this.audio.play();
	}

	/**
	 * Toggles the sound (music).
	 */
	private void toggleAudio() {
		if (this.audio == null) {
			return;
		}
		this.audio.setMuted(!this.audio.isMuted());
	}

	/**
	 * Sets the illustration of the side of the room.
	 * 
	 * @param illustration
	 *            the illustration page URL
	 */
	private void setIllustration(final String illustration) {
		this.illustrationFrame.setUrl(WebInterface.ILLUSTRATION_DIR + illustration
				+ WebInterface.ILLUSTRATION_EXT);
	}

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

	/**
	 * Opens the inventory (switches to the inventory tab).
	 */
	private void openInventory() {
		this.bottomPanel.selectTab(1);
		this.inventoryButton.addStyleName("button-activated");
	}

	/**
	 * Closes the inventory (switches to the console tab).
	 */
	private void closeInventory() {
		this.bottomPanel.selectTab(0);
		this.inventoryButton.removeStyleName("button-activated");
	}

	/**
	 * Toggles the inventory tab.
	 */
	private void toggleInventory() {
		if (this.bottomPanel.getSelectedIndex() == 0) {
			this.openInventory();
		} else {
			this.closeInventory();
		}
	}

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

	@Override
	public void show() {
		RootLayoutPanel.get().add(this);
	}

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

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

	@Override
	public void echo(final String message) {
		this.closeInventory();
		this.displayLabel.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) {
		this.setIllustration(room.name() + Text.FILENAME_SEPARATOR.toString() + direction.name());
		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.clear();

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

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

		this.openInventory();
	}

	@Override
	public void playScene(final Scene scene) {
		this.disableInput();

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

		this.timer = new Timer() {
			@Override
			public void run() {
				scene.getCallback().call();
			}
		};
		this.timer.schedule(scene.getDuration());
	}

	@Override
	public void stopMusic() {
		if (this.audio == null) {
			return;
		}
		this.audio.setMuted(true);
	}

	@Override
	public void toggleSound() {
		return;
	}

	@Override
	public String load(final String path) {
		if (this.storage == null) {
			return null;
		}
		return this.storage.getItem(path);
	}

	@Override
	public void save(final String path, final String serialisedGame) {
		if (this.storage == null) {
			return;
		}
		this.storage.setItem(path, serialisedGame);
	}

}