aboutsummaryrefslogtreecommitdiff
path: root/engines/mohawk/myst_stacks/menu.cpp
blob: e83a05e0cd7138a5c34725643802fcd115f65d5d (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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "mohawk/myst.h"
#include "mohawk/myst_areas.h"
#include "mohawk/myst_card.h"
#include "mohawk/myst_graphics.h"
#include "mohawk/myst_state.h"
#include "mohawk/cursors.h"
#include "mohawk/sound.h"
#include "mohawk/video.h"
#include "mohawk/myst_stacks/menu.h"

#include "common/translation.h"
#include "graphics/cursorman.h"
#include "gui/message.h"

namespace Mohawk {
namespace MystStacks {

Menu::Menu(MohawkEngine_Myst *vm) :
		MystScriptParser(vm, kMenuStack),
		_inGame(false),
		_canSave(false),
		_wasCursorVisible(true),
		_introMoviesRunning(false) {

	for (uint i = 0; i < ARRAYSIZE(_menuItemHovered); i++) {
		_menuItemHovered[i] = false;
	}

	setupOpcodes();
}

Menu::~Menu() {
}

void Menu::setupOpcodes() {
	// "Stack-Specific" Opcodes
	REGISTER_OPCODE(150, Menu, o_menuItemEnter);
	REGISTER_OPCODE(151, Menu, o_menuItemLeave);
	REGISTER_OPCODE(152, Menu, o_menuResume);
	REGISTER_OPCODE(153, Menu, o_menuLoad);
	REGISTER_OPCODE(154, Menu, o_menuSave);
	REGISTER_OPCODE(155, Menu, o_menuNew);
	REGISTER_OPCODE(156, Menu, o_menuOptions);
	REGISTER_OPCODE(157, Menu, o_menuQuit);

	// "Init" Opcodes
	REGISTER_OPCODE(200, Menu, o_playIntroMovies);
	REGISTER_OPCODE(201, Menu, o_menuInit);

	// "Exit" Opcodes
	REGISTER_OPCODE(300, Menu, NOP);
	REGISTER_OPCODE(301, Menu, o_menuExit);
}

void Menu::disablePersistentScripts() {
	_introMoviesRunning = false;
}

void Menu::runPersistentScripts() {
	if (_introMoviesRunning)
		introMovies_run();
}

uint16 Menu::getVar(uint16 var) {
	switch (var) {
	case 1000: // New game
	case 1001: // Load
	case 1004: // Quit
	case 1005: // Options
		return _menuItemHovered[var - 1000] ? 1 : 0;
	case 1002: // Save
		if (_canSave) {
			return _menuItemHovered[var - 1000] ? 1 : 0;
		} else {
			return 2;
		}
	case 1003: // Resume
		if (_inGame) {
			return _menuItemHovered[var - 1000] ? 1 : 0;
		} else {
			return 2;
		}
	default:
		return MystScriptParser::getVar(var);
	}
}

void Menu::o_menuInit(uint16 var, const ArgumentsArray &args) {
	_vm->pauseEngine(true);

	if (_inGame) {
		_wasCursorVisible = CursorMan.isVisible();
	}

	if (!_wasCursorVisible) {
		CursorMan.showMouse(true);
	}

	struct MenuButton {
		uint16 highlightedIndex;
		uint16 disabledIndex;
		Graphics::TextAlign align;
	};

	static const MenuButton buttons[] = {
		{ 1, 0, Graphics::kTextAlignRight },
		{ 1, 0, Graphics::kTextAlignRight },
		{ 1, 2, Graphics::kTextAlignRight },
		{ 1, 2, Graphics::kTextAlignRight },
		{ 1, 0, Graphics::kTextAlignRight },
		{ 1, 0, Graphics::kTextAlignLeft  }
	};

	const char **buttonCaptions = getButtonCaptions();

	for (uint i = 0; i < ARRAYSIZE(buttons); i++) {
		MystAreaImageSwitch *image  = _vm->getCard()->getResource<MystAreaImageSwitch>(2 * i + 0);
		MystAreaHover       *hover  = _vm->getCard()->getResource<MystAreaHover>      (2 * i + 1);

		Common::U32String str = Common::convertUtf8ToUtf32(buttonCaptions[i]);
		drawButtonImages(str, image, buttons[i].align, buttons[i].highlightedIndex, buttons[i].disabledIndex);
		hover->setRect(image->getRect());
	}
}

const char **Menu::getButtonCaptions() const {
	static const char *buttonCaptionsEnglish[] = {
		"NEW GAME",
		"LOAD GAME",
		"SAVE GAME",
		"RESUME",
		"QUIT",
		"OPTIONS"
	};

	static const char *buttonCaptionsFrench[] = {
		"NOUVEAU",
		"CHARGER",
		"SAUVER",
		"REPRENDRE",
		"QUITTER",
		"OPTIONS"
	};

	static const char *buttonCaptionsGerman[] = {
		"NEUES SPIEL",
		"SPIEL LADEN",
		"SPIEL SPEICHERN",
		"FORTSETZEN",
		"BEENDEN",
		"OPTIONEN"
	};

	static const char *buttonCaptionsSpanish[] = {
		"JUEGO NUEVO",
		"CARGAR JUEGO",
		"GUARDAR JUEGO",
		"CONTINUAR",
		"SALIR",
		"OPCIONES"
	};

	static const char *buttonCaptionsPolish[] = {
		"NOWA GRA",
		"ZAŁADUJ GRĘ",
		"ZAPISZ GRĘ",
		"POWRÓT",
		"WYJŚCIE",
		"OPCJE"
	};

	switch (_vm->getLanguage()) {
		case Common::FR_FRA:
			return buttonCaptionsFrench;
		case Common::DE_DEU:
			return buttonCaptionsGerman;
		case Common::ES_ESP:
			return buttonCaptionsSpanish;
		case Common::PL_POL:
			return buttonCaptionsPolish;
		case Common::EN_ANY:
		default:
			return buttonCaptionsEnglish;
	}
}

void Menu::drawButtonImages(const Common::U32String &text, MystAreaImageSwitch *area, Graphics::TextAlign align, uint16 highlightedIndex, uint16 disabledIndex) const {
	Common::Rect backgroundRect = area->getRect();
	Common::Rect textBoundingBox = _vm->_gfx->getTextBoundingBox(text, backgroundRect, align);

	// Restrict the rectangle to the portion were the text will be drawn
	if (align == Graphics::kTextAlignLeft) {
		backgroundRect.right = textBoundingBox.right;
	} else if (align == Graphics::kTextAlignRight) {
		backgroundRect.left = textBoundingBox.left;
	} else {
		error("Unexpected align: %d", align);
	}

	// Update the area with the new background rect
	area->setRect(backgroundRect);

	MystAreaImageSwitch::SubImage idle = area->getSubImage(0);
	area->setSubImageRect(0, Common::Rect(backgroundRect.left, idle.rect.top, backgroundRect.right, idle.rect.bottom));

	// Align the text to the top of the destination rectangles
	int16 deltaY;
	if (_vm->getLanguage() == Common::PL_POL) {
		deltaY = -2;
	} else {
		deltaY = backgroundRect.top - textBoundingBox.top;
	}

	if (highlightedIndex) {
		replaceButtonSubImageWithText(text, align, area, highlightedIndex, backgroundRect, deltaY, 215, 216, 219);
	}

	if (disabledIndex) {
		replaceButtonSubImageWithText(text, align, area, disabledIndex, backgroundRect, deltaY, 136, 140, 145);
	}

	uint16 cardBackground = _vm->getCard()->getBackgroundImageId();
	_vm->_gfx->drawText(cardBackground, text, backgroundRect, 181, 184, 189, align, deltaY);
}

void Menu::replaceButtonSubImageWithText(const Common::U32String &text, const Graphics::TextAlign &align, MystAreaImageSwitch *area,
                                         uint16 subimageIndex, const Common::Rect &backgroundRect, int16 deltaY,
                                         uint8 r, uint8 g, uint8 b) const {
	uint16 cardBackground = _vm->getCard()->getBackgroundImageId();

	MystAreaImageSwitch::SubImage highlighted = area->getSubImage(subimageIndex);
	Common::Rect subImageRect(0, 0, backgroundRect.width(), backgroundRect.height());

	// Create an image exactly the size of the rendered text with the backdrop as a background
	_vm->_gfx->replaceImageWithRect(highlighted.wdib, cardBackground, backgroundRect);
	area->setSubImageRect(subimageIndex, subImageRect);

	// Draw the text in the subimage
	_vm->_gfx->drawText(highlighted.wdib, text, subImageRect, r, g, b, align, deltaY);
}

void Menu::o_menuItemEnter(uint16 var, const ArgumentsArray &args) {
	_menuItemHovered[var - 1000] = true;
	_vm->getCard()->redrawArea(var);
}

void Menu::o_menuItemLeave(uint16 var, const ArgumentsArray &args) {
	_menuItemHovered[var - 1000] = false;
	_vm->getCard()->redrawArea(var);
}

void Menu::o_menuResume(uint16 var, const ArgumentsArray &args) {
	if (!_inGame) {
		return;
	}

	_vm->resumeFromMainMenu();
}

void Menu::o_menuLoad(uint16 var, const ArgumentsArray &args) {
	if (!showConfirmationDialog(_("Are you sure you want to load a saved game? All unsaved progress will be lost."),
	                            _("Load game"), _("Cancel"))) {
		return;
	}

	_vm->runLoadDialog();
}

void Menu::o_menuSave(uint16 var, const ArgumentsArray &args) {
	if (!_canSave) {
		return;
	}

	_vm->runSaveDialog();
}

void Menu::o_menuNew(uint16 var, const ArgumentsArray &args) {
	if (!showConfirmationDialog(_("Are you sure you want to start a new game? All unsaved progress will be lost."),
	                            _("New game"), _("Cancel"))) {
		return;
	}

	_vm->_gameState->reset();
	_vm->setTotalPlayTime(0);
	_vm->setMainCursor(kDefaultMystCursor);
	_vm->changeToStack(kIntroStack, 1, 0, 0);
}

void Menu::o_menuOptions(uint16 var, const ArgumentsArray &args) {
	resetButtons();

	_vm->runOptionsDialog();
}

void Menu::o_menuQuit(uint16 var, const ArgumentsArray &args) {
	if (!showConfirmationDialog(_("Are you sure you want to quit? All unsaved progress will be lost."), _("Quit"),
	                            _("Cancel"))) {
		return;
	}

	_vm->changeToStack(kCreditsStack, 10000, 0, 0);
}

void Menu::o_menuExit(uint16 var, const ArgumentsArray &args) {
	if (_inGame) {
		_vm->_gfx->restoreStateForMainMenu();
	}

	CursorMan.showMouse(_wasCursorVisible);

	_vm->pauseEngine(false);
}

void Menu::o_playIntroMovies(uint16 var, const ArgumentsArray &args) {
	_introMoviesRunning = true;
	_introStep = 0;
}

void Menu::introMovies_run() {
	// Play Intro Movies
	// This is all quite messy...

	VideoEntryPtr video;

	switch (_introStep) {
		case 0:
			_introStep = 1;
			video = _vm->playMovieFullscreen("broder", kIntroStack);
			break;
		case 1:
			if (!_vm->_video->isVideoPlaying())
				_introStep = 2;
			break;
		case 2:
			_introStep = 3;
			video = _vm->playMovieFullscreen("cyanlogo", kIntroStack);
			break;
		case 3:
			if (!_vm->_video->isVideoPlaying())
				_introStep = 4;
			break;
		default:
			_vm->changeToCard(1000, kTransitionCopy);
	}
}

bool Menu::showConfirmationDialog(const char *message, const char *confirmButton, const char *cancelButton) {
	if (!_inGame) {
		return true;
	}

	resetButtons();

	GUI::MessageDialog dialog(message, confirmButton, cancelButton);

	return dialog.runModal() !=0;
}

void Menu::resetButtons() {
	for (uint i = 0; i < ARRAYSIZE(_menuItemHovered); i++) {
		_menuItemHovered[i] = false;
		_vm->getCard()->redrawArea(1000 + i);
	}

	_vm->doFrame();
}


} // End of namespace MystStacks
} // End of namespace Mohawk