File: menu.cpp

package info (click to toggle)
residualvm 0.3.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 31,292 kB
  • sloc: cpp: 227,029; sh: 7,256; xml: 1,731; perl: 1,067; java: 861; asm: 738; python: 691; ansic: 272; makefile: 139; objc: 81; sed: 22; php: 1
file content (977 lines) | stat: -rw-r--r-- 25,822 bytes parent folder | download | duplicates (2)
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
/* ResidualVM - A 3D game interpreter
 *
 * ResidualVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the AUTHORS
 * 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 "engines/myst3/cursor.h"
#include "engines/myst3/database.h"
#include "engines/myst3/inventory.h"
#include "engines/myst3/menu.h"
#include "engines/myst3/myst3.h"
#include "engines/myst3/node.h"
#include "engines/myst3/scene.h"
#include "engines/myst3/sound.h"
#include "engines/myst3/state.h"

#include "common/events.h"

#include "graphics/colormasks.h"

namespace Myst3 {

Dialog::Dialog(Myst3Engine *vm, uint id):
	_vm(vm),
	_texture(0) {
	// Draw on the whole screen
	_isConstrainedToWindow = false;
	_scaled = !_vm->isWideScreenModEnabled();

	const DirectorySubEntry *countDesc = _vm->getFileDescription("DLGI", id, 0, DirectorySubEntry::kNumMetadata);
	const DirectorySubEntry *movieDesc = _vm->getFileDescription("DLOG", id, 0, DirectorySubEntry::kDialogMovie);
	if (!movieDesc) {
		movieDesc = _vm->getFileDescription("DLOG", id, 0, DirectorySubEntry::kStillMovie);
	}

	if (!movieDesc || !countDesc)
		error("Unable to load dialog %d", id);

	// Retrieve button count
	_buttonCount = countDesc->getMiscData(0);
	assert(_buttonCount <= 3);

	// Load the movie
	Common::MemoryReadStream *movieStream = movieDesc->getData();
	_bink.setDefaultHighColorFormat(Texture::getRGBAPixelFormat());
	_bink.loadStream(movieStream);
	_bink.start();

	const Graphics::Surface *frame = _bink.decodeNextFrame();
	_texture = _vm->_gfx->createTexture(frame);

	_vm->_sound->playEffect(699, 10);
}

Dialog::~Dialog() {
	_vm->_gfx->freeTexture(_texture);
}

void Dialog::draw() {
	Common::Rect textureRect = Common::Rect(_texture->width, _texture->height);
	_vm->_gfx->drawTexturedRect2D(getPosition(), textureRect, _texture);
}

Common::Rect Dialog::getPosition() const {
	Common::Rect viewport;
	if (_scaled) {
		viewport = Common::Rect(Renderer::kOriginalWidth, Renderer::kOriginalHeight);
	} else {
		viewport = _vm->_gfx->viewport();
	}

	Common::Rect screenRect = Common::Rect(_texture->width, _texture->height);
	screenRect.translate((viewport.width() - _texture->width) / 2,
			(viewport.height() - _texture->height) / 2);
	return screenRect;
}

ButtonsDialog::ButtonsDialog(Myst3Engine *vm, uint id):
	Dialog(vm, id),
	_frameToDisplay(0),
	_previousframe(0) {

	loadButtons();
}

ButtonsDialog::~ButtonsDialog() {
}

void ButtonsDialog::loadButtons() {
	const DirectorySubEntry *buttonsDesc = _vm->getFileDescription("DLGB", 1000, 0, DirectorySubEntry::kNumMetadata);

	if (!buttonsDesc)
		error("Unable to load dialog buttons description");

	for (uint i = 0; i < 3; i++) {
		uint32 left = buttonsDesc->getMiscData(i * 4);
		uint32 top = buttonsDesc->getMiscData(i * 4 + 1);
		uint32 width = buttonsDesc->getMiscData(i * 4 + 2);
		uint32 height = buttonsDesc->getMiscData(i * 4 + 3);
		_buttons[i] = Common::Rect(width, height);
		_buttons[i].translate(left, top);
	}
}

void ButtonsDialog::draw() {
	if (_frameToDisplay != _previousframe) {
		_bink.seekToFrame(_frameToDisplay);

		const Graphics::Surface *frame = _bink.decodeNextFrame();
		_texture->update(frame);

		_previousframe = _frameToDisplay;
	}

	Dialog::draw();
}

int16 ButtonsDialog::update() {
	// Process events
	Common::Event event;
	while (_vm->getEventManager()->pollEvent(event)) {
		if (event.type == Common::EVENT_MOUSEMOVE) {
			// Compute local mouse coordinates
			_vm->_cursor->updatePosition(event.mouse);
			Common::Point localMouse = getRelativeMousePosition();

			// No hovered button
			_frameToDisplay = 0;

			// Display the frame corresponding to the hovered button
			for (uint i = 0; i < _buttonCount; i++) {
				if (_buttons[i].contains(localMouse)) {
					_frameToDisplay = i + 1;
					break;
				}
			}
		} else if (event.type == Common::EVENT_LBUTTONDOWN) {
			return _frameToDisplay;
		} else if (event.type == Common::EVENT_KEYDOWN) {
			switch (event.kbd.keycode) {
			case Common::KEYCODE_ESCAPE:
				return -1;
			default:
				break;
			}
		}
	}

	return -2;
}

Common::Point ButtonsDialog::getRelativeMousePosition() const {
	Common::Rect position = getPosition();
	Common::Point localMouse =_vm->_cursor->getPosition(_scaled);
	localMouse.x -= position.left;
	localMouse.y -= position.top;
	return localMouse;
}

GamepadDialog::GamepadDialog(Myst3Engine *vm, uint id):
	Dialog(vm, id) {
}

GamepadDialog::~GamepadDialog() {
}

int16 GamepadDialog::update() {
	// Process events
	Common::Event event;
	while (_vm->getEventManager()->pollEvent(event)) {
		if (event.type == Common::EVENT_MOUSEMOVE) {
			// Compute local mouse coordinates
			_vm->_cursor->updatePosition(event.mouse);
		} else if (event.type == Common::EVENT_KEYDOWN) {
			switch (event.kbd.keycode) {
			case Common::KEYCODE_RETURN:
			case Common::KEYCODE_KP_ENTER:
				return 1;
			case Common::KEYCODE_ESCAPE:
				if (_buttonCount == 2) {
					return 2;
				} else {
					return 1;
				}
			default:
				break;
			}
		}
	}

	return -2;
}

Menu::Menu(Myst3Engine *vm) :
		_vm(vm),
		_saveLoadSpotItem(0) {
}

Menu::~Menu() {
}

void Menu::updateMainMenu(uint16 action) {
	switch (action) {
	case 1: {
			// New game
			int16 choice = dialogConfirmValue();

			// If a game is playing, ask if wants to save
			if (_vm->_state->getMenuSavedAge() != 0) {
				choice = _vm->openDialog(dialogIdFromType(kConfirmNewGame));
			}

			if (choice == dialogSaveValue()) {
				// Go to save screen
				_vm->_state->setMenuSaveBack(1);
				_vm->_state->setMenuSaveAction(6);
				goToNode(300);
			} else if (choice == dialogConfirmValue()) {
				// New game
				goToNode(98);
			}
		}
		break;
	case 2: {
			// Load game
			int16 choice = dialogConfirmValue();

			// If a game is playing, ask if wants to save
			if (_vm->_state->getMenuSavedAge() != 0) {
				choice = _vm->openDialog(dialogIdFromType(kConfirmLoadGame));
			}

			if (choice == dialogSaveValue()) {
				// Go to save screen
				_vm->_state->setMenuSaveBack(1);
				_vm->_state->setMenuSaveAction(3);
				goToNode(300);
			} else if (choice == dialogConfirmValue()) {
				// Load game screen
				_vm->_state->setMenuLoadBack(1);
				goToNode(200);
			}
		}
		break;
	case 3:
		// Go to save screen
		_vm->_state->setMenuSaveBack(1);
		_vm->_state->setMenuSaveAction(1);
		goToNode(300);
		break;
	case 4:
		// Settings
		_vm->_state->setMenuOptionsBack(1);
		_vm->runScriptsFromNode(599, 0, 0);
		break;
	case 5: {
			// Asked to quit
			int16 choice = dialogConfirmValue();

			// If a game is playing, ask if wants to save
			if (_vm->_state->getMenuSavedAge() != 0) {
				choice = _vm->openDialog(dialogIdFromType(kConfirmQuit));
			}

			if (choice == dialogSaveValue()) {
				// Go to save screen
				_vm->_state->setMenuSaveBack(1);
				_vm->_state->setMenuSaveAction(5);
				goToNode(300);
			} else if (choice == dialogConfirmValue()) {
				// Quit
				_vm->quitGame();
			}
		}
		break;
	default:
		warning("Menu action %d is not implemented", action);
		break;
	}
}

void Menu::goToNode(uint16 node) {
	if (_vm->_state->getMenuSavedAge() == 0 && _vm->_state->getLocationRoom() != 901) {
		// Entering menu, save current location ...
		_vm->_state->setMenuSavedAge(_vm->_state->getLocationAge());
		_vm->_state->setMenuSavedRoom(_vm->_state->getLocationRoom());
		_vm->_state->setMenuSavedNode(_vm->_state->getLocationNode());

		// ... and capture the screen
		Graphics::Surface *big = _vm->_gfx->getScreenshot();
		Graphics::Surface *thumb = createThumbnail(big);
		_vm->_state->setSaveThumbnail(thumb);
		big->free();
		delete big;

		// Reset some sound variables
		if (_vm->_state->getLocationAge() == 6 && _vm->_state->getSoundEdannaUnk587() == 1 && _vm->_state->getSoundEdannaUnk1031()) {
			_vm->_state->setSoundEdannaUnk587(0);
		}
		if (_vm->_state->getLocationAge() == 10 && _vm->_state->getSoundAmateriaUnk627() == 1 && _vm->_state->getSoundAmateriaUnk930()){
			_vm->_state->setSoundAmateriaUnk627(0);
		}
		if (_vm->_state->getLocationAge() == 7 && _vm->_state->getSoundVoltaicUnk540() == 1 && _vm->_state->getSoundVoltaicUnk1146()) {
			_vm->_state->setSoundVoltaicUnk540(0);
		}

		_vm->_sound->stopMusic(60);
		_vm->_state->setSoundScriptsSuspended(1);
	}

	if (_vm->_state->hasVarMenuEscapePressed()) {
		// This variable does not exist in the Xbox version
		_vm->_state->setMenuEscapePressed(0);
	}

	_vm->_state->setLocationNextAge(9);
	_vm->_state->setLocationNextRoom(901);
	_vm->goToNode(node, kTransitionNone);
}

uint Menu::dialogIdFromType(DialogType type) {
	struct {
		DialogType type;
		uint id;
		uint idXbox;
	} mapping[] = {
			{ kConfirmNewGame,        1080, 1010 },
			{ kConfirmLoadGame,       1060, 1003 },
			{ kConfirmOverwrite,      1040, 1004 },
			{ kConfirmEraseSavedGame, 1020, 0 },
			{ kErrorEraseSavedGame,   1050, 0 },
			{ kConfirmQuit,           1070, 0 }
	};

	uint id = 0;

	for (uint i = 0; i < ARRAYSIZE(mapping); i++) {
		if (mapping[i].type == type) {
			if (_vm->getPlatform() == Common::kPlatformXbox) {
				id = mapping[i].idXbox;
			} else {
				id = mapping[i].id;
			}
		}
	}

	if (id == 0) {
		error("No id for dialog %d", type);
	}

	return id;
}

uint16 Menu::dialogConfirmValue() {
	if (_vm->getPlatform() == Common::kPlatformXbox) {
		return 1;
	}

	return 2;
}

uint16 Menu::dialogSaveValue() {
	if (_vm->getPlatform() == Common::kPlatformXbox) {
		return 999; // No save value
	}

	return 1;
}

Common::String Menu::getAgeLabel(GameState *gameState) {
	uint32 age = 0;
	uint32 room = gameState->getLocationRoom();
	if (room == 901)
		age = gameState->getMenuSavedAge();
	else
		age = gameState->getLocationAge();

	// Look for the age name
	const DirectorySubEntry *desc = _vm->getFileDescription("AGES", 1000, 0, DirectorySubEntry::kTextMetadata);

	if (!desc)
		error("Unable to load age descriptions.");

	Common::String label = desc->getTextData(_vm->_db->getAgeLabelId(age));
	label.toUppercase();

	return label;
}

Graphics::Surface *Menu::createThumbnail(Graphics::Surface *big) {
	assert(big->format == Texture::getRGBAPixelFormat());

	Graphics::Surface *small = new Graphics::Surface();
	small->create(GameState::kThumbnailWidth, GameState::kThumbnailHeight, Texture::getRGBAPixelFormat());

	// The portion of the screenshot to keep
	Common::Rect frame = _vm->_scene->getPosition();
	Graphics::Surface frameSurface = big->getSubArea(frame);

	uint32 *dst = (uint32 *)small->getPixels();
	for (uint i = 0; i < small->h; i++) {
		for (uint j = 0; j < small->w; j++) {
			uint32 srcX = frameSurface.w * j / small->w;
			uint32 srcY = frameSurface.h * i / small->h;
			uint32 *src = (uint32 *)frameSurface.getBasePtr(srcX, srcY);

			// Copy RGBA pixel
			*dst++ = *src;
		}
	}

	return small;
}

void Menu::setSaveLoadSpotItem(uint16 id, SpotItemFace *spotItem) {
	if (id == 1) {
		_saveLoadSpotItem = spotItem;
	}
}

PagingMenu::PagingMenu(Myst3Engine *vm) :
		Menu(vm),
		_saveDrawCaret(false),
		_saveCaretCounter(0) {
}

PagingMenu::~PagingMenu() {
}

void PagingMenu::saveLoadAction(uint16 action, uint16 item) {
	switch (action) {
	case 0:
		loadMenuOpen();
		break;
	case 1:
		loadMenuSelect(item);
		break;
	case 2:
		loadMenuLoad();
		break;
	case 3:
		saveMenuOpen();
		break;
	case 4:
		saveMenuSelect(item);
		break;
	case 5:
		saveMenuSave();
		break;
	case 6:
		loadMenuChangePage();
		break;
	case 7:
		saveMenuChangePage();
		break;
	case 8:
		saveLoadErase();
		break;
	default:
		warning("Save load menu action %d for item %d is not implemented", action, item);
	}
}

void PagingMenu::loadMenuOpen() {
	_saveLoadFiles = Saves::list(_vm->getSaveFileManager(), _vm->getPlatform());

	_vm->_state->setMenuSaveLoadCurrentPage(0);
	saveLoadUpdateVars();
}

void PagingMenu::saveLoadUpdateVars() {
	int16 page = _vm->_state->getMenuSaveLoadCurrentPage();

	// Go back one page if the last element of the last page was removed
	if (page && (7 * page > (int)_saveLoadFiles.size() - 1))
		page--;
	_vm->_state->setMenuSaveLoadCurrentPage(page);

	// Set up pagination
	bool canGoLeft = (_saveLoadFiles.size() > 7) && page;
	bool canGoRight = (_saveLoadFiles.size() > 7) && (7 * (page + 1) < (int)_saveLoadFiles.size());

	_vm->_state->setMenuSaveLoadPageLeft(canGoLeft);
	_vm->_state->setMenuSaveLoadPageRight(canGoRight);
	_vm->_state->setMenuSaveLoadSelectedItem(-1);

	// Enable items
	uint16 itemsOnPage = _saveLoadFiles.size() % 7;

	if (itemsOnPage == 0 && _saveLoadFiles.size() != 0)
		itemsOnPage = 7;
	if (canGoRight)
		itemsOnPage = 7;

	for (uint i = 0; i < 7; i++)
		_vm->_state->setVar(1354 + i, i < itemsOnPage);
}

void PagingMenu::loadMenuSelect(uint16 item) {
	// Selecting twice the same item loads it
	if (item == _vm->_state->getMenuSaveLoadSelectedItem()) {
		loadMenuLoad();
		return;
	}

	_vm->_state->setMenuSaveLoadSelectedItem(item);
	int16 page = _vm->_state->getMenuSaveLoadCurrentPage();

	uint16 index = page * 7 + item;

	assert(index < _saveLoadFiles.size());
	Common::String filename = _saveLoadFiles[index];
	Common::InSaveFile *saveFile = _vm->getSaveFileManager()->openForLoading(filename);
	if (!saveFile) {
		warning("Unable to open save '%s'", filename.c_str());
		return;
	}

	// Extract the age to load from the savegame
	GameState *gameState = new GameState(_vm->getPlatform(), _vm->_db);
	gameState->load(saveFile);

	// Update the age name
	_saveLoadAgeName = getAgeLabel(gameState);

	// Update the save thumbnail
	if (_saveLoadSpotItem)
		_saveLoadSpotItem->updateData(gameState->getSaveThumbnail());

	delete gameState;
	delete saveFile;
}

void PagingMenu::loadMenuLoad() {
	uint16 item = _vm->_state->getMenuSaveLoadSelectedItem();
	int16 page = _vm->_state->getMenuSaveLoadCurrentPage();

	uint16 index = page * 7 + item;
	assert(index < _saveLoadFiles.size());

	_vm->loadGameState(_saveLoadFiles[index], kTransitionFade);
}

void PagingMenu::saveMenuOpen() {
	_saveLoadFiles = Saves::list(_vm->getSaveFileManager(), _vm->getPlatform());

	_saveLoadAgeName = getAgeLabel(_vm->_state);
	_saveCaretCounter = kCaretSpeed;

	_vm->_state->setMenuSaveLoadCurrentPage(0);
	saveLoadUpdateVars();

	// Update the thumbnail to display
	Graphics::Surface *saveThumb = _vm->_state->getSaveThumbnail();
	if (_saveLoadSpotItem && saveThumb)
		_saveLoadSpotItem->updateData(saveThumb);
}

void PagingMenu::saveMenuSelect(uint16 item) {
	_vm->_state->setMenuSaveLoadSelectedItem(item);

	if (item != 7) {
		int16 page = _vm->_state->getMenuSaveLoadCurrentPage();

		uint16 index = page * 7 + item;

		assert(index < _saveLoadFiles.size());
		_saveName = _saveLoadFiles[index];
	}
}

void PagingMenu::saveMenuChangePage() {
	saveLoadUpdateVars();
	_vm->_state->setMenuSaveLoadSelectedItem(7);
}

void PagingMenu::saveMenuSave() {
	if (_saveName.empty())
		return;

	Common::String fileName = _saveName;
	if (!fileName.hasSuffix(".M3S") && !fileName.hasSuffix(".m3s"))
		fileName += ".M3S";

	// Check if file exists
	bool fileExists = false;
	for (uint i = 0; i < _saveLoadFiles.size(); i++) {
		if (_saveLoadFiles[i].equalsIgnoreCase(fileName)) {
			fileExists = true;
			break;
		}
	}

	// Ask the user if he wants to overwrite the existing save
	if (fileExists && _vm->openDialog(dialogIdFromType(kConfirmOverwrite)) != 1)
		return;

	// Save the state and the thumbnail
	Common::OutSaveFile *save = _vm->getSaveFileManager()->openForSaving(fileName);
	_vm->_state->setSaveDescription(_saveName);
	_vm->_state->save(save);
	delete save;

	// Do next action
	_vm->_state->setMenuNextAction(_vm->_state->getMenuSaveAction());
	_vm->runScriptsFromNode(88);
}

void PagingMenu::saveLoadErase() {
	uint16 node = _vm->_state->getLocationNode();
	uint16 item = _vm->_state->getMenuSaveLoadSelectedItem();
	int16 page = _vm->_state->getMenuSaveLoadCurrentPage();

	uint16 index = page * 7 + item;
	assert(index < _saveLoadFiles.size());

	// Confirm dialog
	if (_vm->openDialog(dialogIdFromType(kConfirmEraseSavedGame)) != 1)
		return;

	// Delete the file
	if (!_vm->getSaveFileManager()->removeSavefile(_saveLoadFiles[index]))
		_vm->openDialog(dialogIdFromType(kErrorEraseSavedGame)); // Error dialog

	_saveLoadFiles = Saves::list(_vm->getSaveFileManager(), _vm->getPlatform());

	saveLoadUpdateVars();

	// Load menu specific
	if (node == 200 && _saveLoadSpotItem) {
		_saveLoadSpotItem->clear();
		_saveLoadAgeName.clear();
	}

	// Save menu specific
	if (node == 300)
		_vm->_state->setMenuSaveLoadSelectedItem(7);
}

void PagingMenu::draw() {
	uint16 node = _vm->_state->getLocationNode();
	uint16 room = _vm->_state->getLocationRoom();
	uint16 age = _vm->_state->getLocationAge();

	if (room != 901 || !(node == 200 || node == 300))
		return;

	int16 page = _vm->_state->getMenuSaveLoadCurrentPage();
	NodePtr nodeData = _vm->_db->getNodeData(node, room, age);

	for (uint i = 0; i < 7; i++) {
		uint itemToDisplay = page * 7 + i;

		if (itemToDisplay >= _saveLoadFiles.size())
			break;

		PolarRect rect = nodeData->hotspots[i + 1].rects[0];

		Common::String display = prepareSaveNameForDisplay(_saveLoadFiles[itemToDisplay]);
		_vm->_gfx->draw2DText(display, Common::Point(rect.centerPitch, rect.centerHeading));
	}

	if (!_saveLoadAgeName.empty()) {
		PolarRect rect = nodeData->hotspots[8].rects[0];
		_vm->_gfx->draw2DText(_saveLoadAgeName, Common::Point(rect.centerPitch, rect.centerHeading));
	}

	// Save screen specific
	if (node == 300) {
		uint16 item = _vm->_state->getMenuSaveLoadSelectedItem();
		Common::String display = prepareSaveNameForDisplay(_saveName);

		if (item == 7) {
			_saveCaretCounter--;
			if (_saveCaretCounter < 0) {
				_saveCaretCounter = kCaretSpeed;
				_saveDrawCaret = !_saveDrawCaret;
			}

			if (_saveDrawCaret) {
				display += '|';
			}
		}

		PolarRect rect = nodeData->hotspots[9].rects[0];
		_vm->_gfx->draw2DText(display, Common::Point(rect.centerPitch, rect.centerHeading));
	}
}

bool PagingMenu::handleInput(const Common::KeyState &e) {
	uint16 node = _vm->_state->getLocationNode();
	uint16 room = _vm->_state->getLocationRoom();
	uint16 item = _vm->_state->getMenuSaveLoadSelectedItem();

	if (room != 901 || node != 300 || item != 7)
		return false;

	Common::String display = prepareSaveNameForDisplay(_saveName);

	if (e.keycode == Common::KEYCODE_BACKSPACE
			|| e.keycode == Common::KEYCODE_DELETE) {
		display.deleteLastChar();
		_saveName = display;
		return true;
	} else if (e.keycode == Common::KEYCODE_RETURN
			|| e.keycode == Common::KEYCODE_KP_ENTER) {
		saveMenuSave();
		return true;
	}

	if (((e.ascii >= 'a' && e.ascii <= 'z')
			|| (e.ascii >= 'A' && e.ascii <= 'Z')
			|| (e.ascii >= '0' && e.ascii <= '9')
			|| e.ascii == ' ')
			&& (display.size() < 17)) {
		display += e.ascii;
		display.toUppercase();
		_saveName = display;

		return true;
	}

	return false;
}

void PagingMenu::loadMenuChangePage() {
	saveLoadUpdateVars();
}

Common::String PagingMenu::prepareSaveNameForDisplay(const Common::String &name) {
	Common::String display = name;
	display.toUppercase();
	if (display.hasSuffix(".M3S")) {
		display.deleteLastChar();
		display.deleteLastChar();
		display.deleteLastChar();
		display.deleteLastChar();
	}

	while (display.size() > 17)
		display.deleteLastChar();

	return display;
}

AlbumMenu::AlbumMenu(Myst3Engine *vm) :
		Menu(vm) {
}

AlbumMenu::~AlbumMenu() {
}

void AlbumMenu::draw() {
	uint16 node = _vm->_state->getLocationNode();
	uint16 room = _vm->_state->getLocationRoom();

	// Load and save menus only
	if (room != 901 || !(node == 200 || node == 300))
		return;

	if (!_saveLoadAgeName.empty()) {
		Common::Point p(184 - (13 * _saveLoadAgeName.size()) / 2, 305);
		_vm->_gfx->draw2DText(_saveLoadAgeName, p);
	}

	if (!_saveLoadTime.empty()) {
		Common::Point p(184 - (13 * _saveLoadTime.size()) / 2, 323);
		_vm->_gfx->draw2DText(_saveLoadTime, p);
	}
}

bool AlbumMenu::handleInput(const Common::KeyState &e) {
	return false;
}

void AlbumMenu::saveLoadAction(uint16 action, uint16 item) {
	switch (action) {
	case 0:
		loadMenuOpen();
		break;
	case 1:
		loadMenuSelect();
		break;
	case 2:
		loadMenuLoad();
		break;
	case 3:
		saveMenuOpen();
		break;
	case 4:
		saveMenuSave();
		break;
	case 5:
		setSavesAvailable();
		break;
	default:
		warning("Save load menu action %d for item %d is not implemented", action, item);
	}
}

Common::String AlbumMenu::getSaveNameTemplate() {
	const DirectorySubEntry *saveNameDesc = _vm->getFileDescription("SAVE", 1000, 0, DirectorySubEntry::kTextMetadata);
	return saveNameDesc->getTextData(0); // "EXILE Saved Game %d"
}

Common::HashMap<int, Common::String> AlbumMenu::listSaveFiles() {
	Common::StringArray saveFiles = _vm->getSaveFileManager()->listSavefiles("*.m3x");
	Common::String fileNameTemplate = Common::String::format("%s.m3x", getSaveNameTemplate().c_str());

	Common::HashMap<int, Common::String> filteredSaveFiles;
	for (uint i = 0; i < 10; i++) {
		Common::String fileName = Common::String::format(fileNameTemplate.c_str(), i);

		for (uint j = 0; j < saveFiles.size(); j++) {
			if (saveFiles[j].equalsIgnoreCase(fileName)) {
				filteredSaveFiles.setVal(i, saveFiles[j]);
				break;
			}
		}
	}

	return filteredSaveFiles;
}

void AlbumMenu::loadSaves() {
	Common::HashMap<int, Common::String> saveFiles = listSaveFiles();
	for (uint i = 0; i < 10; i++) {
		// Skip empty slots
		if (!saveFiles.contains(i)) continue;

		// Open save
		Common::InSaveFile *saveFile = _vm->getSaveFileManager()->openForLoading(saveFiles[i]);

		// Read state data
		Common::Serializer s = Common::Serializer(saveFile, 0);
		GameState::StateData data;
		data.syncWithSaveGame(s);

		if (_albumSpotItems.contains(i)) {
			// Read and resize the thumbnail
			Graphics::Surface *miniThumb = new Graphics::Surface();
			miniThumb->create(kAlbumThumbnailWidth, kAlbumThumbnailHeight, Texture::getRGBAPixelFormat());
			data.resizeThumbnail(miniThumb);

			SpotItemFace *spotItem = _albumSpotItems.getVal(i);
			spotItem->updateData(miniThumb);

			miniThumb->free();
			delete miniThumb;
		}
	}
}

void AlbumMenu::loadMenuOpen() {
	_saveLoadAgeName = "";
	_saveLoadTime = "";

	loadSaves();
}

void AlbumMenu::loadMenuSelect() {
	uint16 node = _vm->_state->getLocationNode();
	uint16 room = _vm->_state->getLocationRoom();

	// Details are only updated on the load menu
	if (room != 901 || node != 200)
		return;

	int32 selectedSave = _vm->_state->getMenuSelectedSave();
	Common::HashMap<int, Common::String> saveFiles = listSaveFiles();

	if (!saveFiles.contains(selectedSave)) {
		// No save in the selected slot
		_saveLoadAgeName = "";
		_saveLoadTime = "";
		_saveLoadSpotItem->initBlack(GameState::kThumbnailWidth, GameState::kThumbnailHeight);
		return;
	}

	Common::String filename = saveFiles[selectedSave];
	Common::InSaveFile *saveFile = _vm->getSaveFileManager()->openForLoading(filename);
	if (!saveFile) {
		warning("Unable to open save '%s'", filename.c_str());
		return;
	}

	// Extract the age to load from the savegame
	GameState *gameState = new GameState(_vm->getPlatform(), _vm->_db);
	gameState->load(saveFile);

	// Update the age name and save date
	_saveLoadAgeName = getAgeLabel(gameState);
	_saveLoadTime = gameState->formatSaveTime();

	// Update the save thumbnail
	if (_saveLoadSpotItem)
		_saveLoadSpotItem->updateData(gameState->getSaveThumbnail());

	delete gameState;
}

void AlbumMenu::loadMenuLoad() {
	int32 selectedSave = _vm->_state->getMenuSelectedSave();
	Common::HashMap<int, Common::String> saveFiles = listSaveFiles();

	if (!saveFiles.contains(selectedSave)) {
		return; // No save to load, do nothing
	}

	_vm->loadGameState(saveFiles[selectedSave], kTransitionFade);
}

void AlbumMenu::saveMenuOpen() {
	loadSaves();

	_saveLoadAgeName = getAgeLabel(_vm->_state);
	_saveLoadTime = "";

	// Update the thumbnail to display
	Graphics::Surface *saveThumb = _vm->_state->getSaveThumbnail();
	if (_saveLoadSpotItem && saveThumb)
		_saveLoadSpotItem->updateData(saveThumb);
}

void AlbumMenu::saveMenuSave() {
	int32 selectedSave = _vm->_state->getMenuSelectedSave();

	Common::String saveNameTemplate = getSaveNameTemplate();
	Common::String saveName = Common::String::format(saveNameTemplate.c_str(), selectedSave);
	Common::String fileName = saveName + ".m3x";

	// Ask the user if he wants to overwrite the existing save
	Common::HashMap<int, Common::String> saveFiles = listSaveFiles();
	if (saveFiles.contains(selectedSave) && _vm->openDialog(dialogIdFromType(kConfirmOverwrite)) != 1)
		return;

	// Save the state and the thumbnail
	Common::OutSaveFile *save = _vm->getSaveFileManager()->openForSaving(fileName);
	_vm->_state->setSaveDescription(saveName);
	_vm->_state->save(save);
	delete save;

	// Do next action
	_vm->_state->setMenuNextAction(_vm->_state->getMenuSaveAction());
	_vm->runScriptsFromNode(88);
}

void AlbumMenu::setSavesAvailable() {
	Common::HashMap<int, Common::String> saveFiles = listSaveFiles();
	_vm->_state->setMenuSavesAvailable(!saveFiles.empty());
}

void AlbumMenu::setSaveLoadSpotItem(uint16 id, SpotItemFace *spotItem) {
	if (id % 100 == 2) {
		_albumSpotItems.setVal(id / 100, spotItem);
	} else {
		Menu::setSaveLoadSpotItem(id, spotItem);
	}
}

} // End of namespace Myst3