1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "sherlock/scalpel/scalpel_fixed_text.h"
24 #include "sherlock/scalpel/scalpel_saveload.h"
25 #include "sherlock/scalpel/scalpel_screen.h"
26 #include "sherlock/scalpel/scalpel.h"
27 
28 namespace Sherlock {
29 
30 namespace Scalpel {
31 
32 const int ENV_POINTS[6][3] = {
33 	{ 41, 80, 61 },		// Exit
34 	{ 81, 120, 101 },	// Load
35 	{ 121, 160, 141 },	// Save
36 	{ 161, 200, 181 },	// Up
37 	{ 201, 240, 221 },	// Down
38 	{ 241, 280, 261 }	// Quit
39 };
40 
41 /*----------------------------------------------------------------*/
42 
ScalpelSaveManager(SherlockEngine * vm,const Common::String & target)43 ScalpelSaveManager::ScalpelSaveManager(SherlockEngine *vm, const Common::String &target) :
44 	SaveManager(vm, target), _envMode(SAVEMODE_NONE) {
45 
46 	_fixedTextExit = FIXED(LoadSave_Exit);
47 	_fixedTextLoad = FIXED(LoadSave_Load);
48 	_fixedTextSave  = FIXED(LoadSave_Save);
49 	_fixedTextUp = FIXED(LoadSave_Up);
50 	_fixedTextDown = FIXED(LoadSave_Down);
51 	_fixedTextQuit = FIXED(LoadSave_Quit);
52 
53 	_hotkeyExit = toupper(_fixedTextExit[0]);
54 	_hotkeyLoad = toupper(_fixedTextLoad[0]);
55 	_hotkeySave = toupper(_fixedTextSave[0]);
56 	_hotkeyUp   = toupper(_fixedTextUp[0]);
57 	_hotkeyDown = toupper(_fixedTextDown[0]);
58 	_hotkeyQuit = toupper(_fixedTextQuit[0]);
59 
60 	_hotkeysIndexed[0] = _hotkeyExit;
61 	_hotkeysIndexed[1] = _hotkeyLoad;
62 	_hotkeysIndexed[2] = _hotkeySave;
63 	_hotkeysIndexed[3] = _hotkeyUp;
64 	_hotkeysIndexed[4] = _hotkeyDown;
65 	_hotkeysIndexed[5] = _hotkeyQuit;
66 
67 	_fixedTextQuitGameQuestion = FIXED(QuitGame_Question);
68 	_fixedTextQuitGameYes = FIXED(QuitGame_Yes);
69 	_fixedTextQuitGameNo = FIXED(QuitGame_No);
70 
71 	_hotkeyQuitGameYes = toupper(_fixedTextQuitGameYes[0]);
72 	_hotkeyQuitGameNo  = toupper(_fixedTextQuitGameNo[0]);
73 }
74 
identifyUserButton(int key)75 int ScalpelSaveManager::identifyUserButton(int key) {
76 	for (uint16 hotkeyNr = 0; hotkeyNr < sizeof(_hotkeysIndexed); hotkeyNr++) {
77 		if (key == _hotkeysIndexed[hotkeyNr])
78 			return hotkeyNr;
79 	}
80 	return -1;
81 }
82 
drawInterface()83 void ScalpelSaveManager::drawInterface() {
84 	ScalpelScreen &screen = *(ScalpelScreen *)_vm->_screen;
85 	UserInterface &ui = *_vm->_ui;
86 
87 	// Create a list of savegame slots
88 	createSavegameList();
89 
90 	screen._backBuffer1.fillRect(Common::Rect(0, CONTROLS_Y, SHERLOCK_SCREEN_WIDTH, CONTROLS_Y + 10), BORDER_COLOR);
91 	screen._backBuffer1.fillRect(Common::Rect(0, CONTROLS_Y + 10, 2, SHERLOCK_SCREEN_HEIGHT), BORDER_COLOR);
92 	screen._backBuffer1.fillRect(Common::Rect(318, CONTROLS_Y + 10, SHERLOCK_SCREEN_WIDTH, SHERLOCK_SCREEN_HEIGHT), BORDER_COLOR);
93 	screen._backBuffer1.fillRect(Common::Rect(0, 199, SHERLOCK_SCREEN_WIDTH, SHERLOCK_SCREEN_HEIGHT), BORDER_COLOR);
94 	screen._backBuffer1.fillRect(Common::Rect(2, CONTROLS_Y + 10, SHERLOCK_SCREEN_WIDTH - 2, SHERLOCK_SCREEN_HEIGHT - 2), INV_BACKGROUND);
95 
96 	screen.makeButton(Common::Rect(ENV_POINTS[0][0], CONTROLS_Y, ENV_POINTS[0][1], CONTROLS_Y + 10),
97 		ENV_POINTS[0][2], _fixedTextExit);
98 	screen.makeButton(Common::Rect(ENV_POINTS[1][0], CONTROLS_Y, ENV_POINTS[1][1], CONTROLS_Y + 10),
99 		ENV_POINTS[1][2], _fixedTextLoad);
100 	screen.makeButton(Common::Rect(ENV_POINTS[2][0], CONTROLS_Y, ENV_POINTS[2][1], CONTROLS_Y + 10),
101 		ENV_POINTS[2][2], _fixedTextSave);
102 	screen.makeButton(Common::Rect(ENV_POINTS[3][0], CONTROLS_Y, ENV_POINTS[3][1], CONTROLS_Y + 10),
103 		ENV_POINTS[3][2], _fixedTextUp);
104 	screen.makeButton(Common::Rect(ENV_POINTS[4][0], CONTROLS_Y, ENV_POINTS[4][1], CONTROLS_Y + 10),
105 		ENV_POINTS[4][2], _fixedTextDown);
106 	screen.makeButton(Common::Rect(ENV_POINTS[5][0], CONTROLS_Y, ENV_POINTS[5][1], CONTROLS_Y + 10),
107 		ENV_POINTS[5][2], _fixedTextQuit);
108 
109 	if (!_savegameIndex)
110 		screen.buttonPrint(Common::Point(ENV_POINTS[3][2], CONTROLS_Y), COMMAND_NULL, 0, _fixedTextUp);
111 
112 	if (_savegameIndex == MAX_SAVEGAME_SLOTS - ONSCREEN_FILES_COUNT)
113 		screen.buttonPrint(Common::Point(ENV_POINTS[4][2], CONTROLS_Y), COMMAND_NULL, 0, _fixedTextDown);
114 
115 	for (int idx = _savegameIndex; idx < _savegameIndex + ONSCREEN_FILES_COUNT; ++idx) {
116 		screen.gPrint(Common::Point(6, CONTROLS_Y + 11 + (idx - _savegameIndex) * 10),
117 			INV_FOREGROUND, "%d.", idx + 1);
118 		screen.gPrint(Common::Point(24, CONTROLS_Y + 11 + (idx - _savegameIndex) * 10),
119 			INV_FOREGROUND, "%s", _savegames[idx].c_str());
120 	}
121 
122 	if (!ui._slideWindows) {
123 		screen.slamRect(Common::Rect(0, CONTROLS_Y, SHERLOCK_SCREEN_WIDTH, SHERLOCK_SCREEN_HEIGHT));
124 	} else {
125 		ui.summonWindow();
126 	}
127 
128 	_envMode = SAVEMODE_NONE;
129 }
130 
getHighlightedButton() const131 int ScalpelSaveManager::getHighlightedButton() const {
132 	Common::Point pt = _vm->_events->mousePos();
133 
134 	for (int idx = 0; idx < 6; ++idx) {
135 		if (pt.x > ENV_POINTS[idx][0] && pt.x < ENV_POINTS[idx][1] && pt.y > CONTROLS_Y
136 				&& pt.y < (CONTROLS_Y + 10))
137 			return idx;
138 	}
139 
140 	return -1;
141 }
142 
highlightButtons(int btnIndex)143 void ScalpelSaveManager::highlightButtons(int btnIndex) {
144 	ScalpelScreen &screen = *(ScalpelScreen *)_vm->_screen;
145 	byte color = (btnIndex == 0) ? COMMAND_HIGHLIGHTED : COMMAND_FOREGROUND;
146 
147 	screen.buttonPrint(Common::Point(ENV_POINTS[0][2], CONTROLS_Y), color, 1, _fixedTextExit);
148 
149 	if ((btnIndex == 1) || ((_envMode == SAVEMODE_LOAD) && (btnIndex != 2)))
150 		screen.buttonPrint(Common::Point(ENV_POINTS[1][2], CONTROLS_Y), COMMAND_HIGHLIGHTED, true, _fixedTextLoad);
151 	else
152 		screen.buttonPrint(Common::Point(ENV_POINTS[1][2], CONTROLS_Y), COMMAND_FOREGROUND, true, _fixedTextLoad);
153 
154 	if ((btnIndex == 2) || ((_envMode == SAVEMODE_SAVE) && (btnIndex != 1)))
155 		screen.buttonPrint(Common::Point(ENV_POINTS[2][2], CONTROLS_Y), COMMAND_HIGHLIGHTED, true, _fixedTextSave);
156 	else
157 		screen.buttonPrint(Common::Point(ENV_POINTS[2][2], CONTROLS_Y), COMMAND_FOREGROUND, true, _fixedTextSave);
158 
159 	if (btnIndex == 3 && _savegameIndex)
160 		screen.buttonPrint(Common::Point(ENV_POINTS[3][2], CONTROLS_Y), COMMAND_HIGHLIGHTED, true, _fixedTextUp);
161 	else
162 		if (_savegameIndex)
163 			screen.buttonPrint(Common::Point(ENV_POINTS[3][2], CONTROLS_Y), COMMAND_FOREGROUND, true, _fixedTextUp);
164 
165 	if ((btnIndex == 4) && (_savegameIndex < MAX_SAVEGAME_SLOTS - 5))
166 		screen.buttonPrint(Common::Point(ENV_POINTS[4][2], CONTROLS_Y), COMMAND_HIGHLIGHTED, true, _fixedTextDown);
167 	else if (_savegameIndex < (MAX_SAVEGAME_SLOTS - 5))
168 		screen.buttonPrint(Common::Point(ENV_POINTS[4][2], CONTROLS_Y), COMMAND_FOREGROUND, true, _fixedTextDown);
169 
170 	color = (btnIndex == 5) ? COMMAND_HIGHLIGHTED : COMMAND_FOREGROUND;
171 	screen.buttonPrint(Common::Point(ENV_POINTS[5][2], CONTROLS_Y), color, 1, _fixedTextQuit);
172 }
173 
checkGameOnScreen(int slot)174 bool ScalpelSaveManager::checkGameOnScreen(int slot) {
175 	ScalpelScreen &screen = *(ScalpelScreen *)_vm->_screen;
176 
177 	// Check if it's already on-screen
178 	if (slot != -1 && (slot < _savegameIndex || slot >= (_savegameIndex + ONSCREEN_FILES_COUNT))) {
179 		_savegameIndex = slot;
180 
181 		screen._backBuffer1.fillRect(Common::Rect(3, CONTROLS_Y + 11, SHERLOCK_SCREEN_WIDTH - 2,
182 			SHERLOCK_SCREEN_HEIGHT - 1), INV_BACKGROUND);
183 
184 		for (int idx = _savegameIndex; idx < (_savegameIndex + 5); ++idx) {
185 			screen.gPrint(Common::Point(6, CONTROLS_Y + 11 + (idx - _savegameIndex) * 10),
186 				INV_FOREGROUND, "%d.", idx + 1);
187 			screen.gPrint(Common::Point(24, CONTROLS_Y + 11 + (idx - _savegameIndex) * 10),
188 				INV_FOREGROUND, "%s", _savegames[idx].c_str());
189 		}
190 
191 		screen.slamRect(Common::Rect(3, CONTROLS_Y + 11, 318, SHERLOCK_SCREEN_HEIGHT));
192 
193 		byte color = !_savegameIndex ? COMMAND_NULL : COMMAND_FOREGROUND;
194 		screen.buttonPrint(Common::Point(ENV_POINTS[3][2], CONTROLS_Y), color, 1, _fixedTextUp);
195 
196 		color = (_savegameIndex == (MAX_SAVEGAME_SLOTS - 5)) ? COMMAND_NULL : COMMAND_FOREGROUND;
197 		screen.buttonPrint(Common::Point(ENV_POINTS[4][2], CONTROLS_Y), color, 1, _fixedTextDown);
198 
199 		return true;
200 	}
201 
202 	return false;
203 }
204 
promptForDescription(int slot)205 bool ScalpelSaveManager::promptForDescription(int slot) {
206 	Events &events = *_vm->_events;
207 	Scene &scene = *_vm->_scene;
208 	ScalpelScreen &screen = *(ScalpelScreen *)_vm->_screen;
209 	Talk &talk = *_vm->_talk;
210 	int xp, yp;
211 	bool flag = false;
212 
213 	screen.buttonPrint(Common::Point(ENV_POINTS[0][2], CONTROLS_Y), COMMAND_NULL, true, _fixedTextExit);
214 	screen.buttonPrint(Common::Point(ENV_POINTS[1][2], CONTROLS_Y), COMMAND_NULL, true, _fixedTextLoad);
215 	screen.buttonPrint(Common::Point(ENV_POINTS[2][2], CONTROLS_Y), COMMAND_NULL, true, _fixedTextSave);
216 	screen.buttonPrint(Common::Point(ENV_POINTS[3][2], CONTROLS_Y), COMMAND_NULL, true, _fixedTextUp);
217 	screen.buttonPrint(Common::Point(ENV_POINTS[4][2], CONTROLS_Y), COMMAND_NULL, true, _fixedTextDown);
218 	screen.buttonPrint(Common::Point(ENV_POINTS[5][2], CONTROLS_Y), COMMAND_NULL, true, _fixedTextQuit);
219 
220 	Common::String saveName = _savegames[slot];
221 	if (isSlotEmpty(slot)) {
222 		// It's an empty slot, so start off with an empty save name
223 		saveName = "";
224 
225 		yp = CONTROLS_Y + 12 + (slot - _savegameIndex) * 10;
226 		screen.vgaBar(Common::Rect(24, yp, 85, yp + 9), INV_BACKGROUND);
227 	}
228 
229 	screen.print(Common::Point(6, CONTROLS_Y + 12 + (slot - _savegameIndex) * 10), TALK_FOREGROUND, "%d.", slot + 1);
230 	screen.print(Common::Point(24, CONTROLS_Y + 12 + (slot - _savegameIndex) * 10), TALK_FOREGROUND, "%s", saveName.c_str());
231 	xp = 24 + screen.stringWidth(saveName);
232 	yp = CONTROLS_Y + 12 + (slot - _savegameIndex) * 10;
233 
234 	int done = 0;
235 	do {
236 		while (!_vm->shouldQuit() && !events.kbHit()) {
237 			scene.doBgAnim();
238 
239 			if (talk._talkToAbort)
240 				return false;
241 
242 			// Allow event processing
243 			events.pollEventsAndWait();
244 			events.setButtonState();
245 
246 			flag = !flag;
247 			if (flag)
248 				screen.vgaBar(Common::Rect(xp, yp - 1, xp + 8, yp + 9), INV_FOREGROUND);
249 			else
250 				screen.vgaBar(Common::Rect(xp, yp - 1, xp + 8, yp + 9), INV_BACKGROUND);
251 		}
252 		if (_vm->shouldQuit())
253 			return false;
254 
255 		// Get the next keypress
256 		Common::KeyState keyState = events.getKey();
257 
258 		if (keyState.keycode == Common::KEYCODE_BACKSPACE && saveName.size() > 0) {
259 			// Delete character of save name
260 			screen.vgaBar(Common::Rect(xp - screen.charWidth(saveName.lastChar()), yp - 1,
261 				xp + 8, yp + 9), INV_BACKGROUND);
262 			xp -= screen.charWidth(saveName.lastChar());
263 			screen.vgaBar(Common::Rect(xp, yp - 1, xp + 8, yp + 9), INV_FOREGROUND);
264 			saveName.deleteLastChar();
265 
266 		} else if (keyState.keycode == Common::KEYCODE_RETURN && saveName.compareToIgnoreCase(EMPTY_SAVEGAME_SLOT)) {
267 			done = 1;
268 
269 		} else if (keyState.keycode == Common::KEYCODE_ESCAPE) {
270 			screen.vgaBar(Common::Rect(xp, yp - 1, xp + 8, yp + 9), INV_BACKGROUND);
271 			done = -1;
272 
273 		} else if (keyState.ascii >= ' ' && keyState.ascii <= 'z' && saveName.size() < 50
274 				&& (xp + screen.charWidth(keyState.ascii)) < 308) {
275 			char c = (char)keyState.ascii;
276 
277 			screen.vgaBar(Common::Rect(xp, yp - 1, xp + 8, yp + 9), INV_BACKGROUND);
278 			screen.print(Common::Point(xp, yp), TALK_FOREGROUND, "%c", c);
279 			xp += screen.charWidth(c);
280 			screen.vgaBar(Common::Rect(xp, yp - 1, xp + 8, yp + 9), INV_FOREGROUND);
281 			saveName += c;
282 		}
283 	} while (!done);
284 
285 	if (done == 1) {
286 		// Enter key perssed
287 		_savegames[slot] = saveName;
288 	} else {
289 		done = 0;
290 		_envMode = SAVEMODE_NONE;
291 		highlightButtons(-1);
292 	}
293 
294 	return done == 1;
295 }
296 
297 } // End of namespace Scalpel
298 
299 } // End of namespace Sherlock
300