File: saveloadmenu.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (360 lines) | stat: -rw-r--r-- 10,528 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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "engines/stark/ui/menu/saveloadmenu.h"
#include "engines/stark/services/services.h"
#include "engines/stark/services/userinterface.h"
#include "engines/stark/services/stateprovider.h"
#include "engines/stark/services/global.h"
#include "engines/stark/services/settings.h"
#include "engines/stark/services/gamechapter.h"
#include "engines/stark/services/gamemessage.h"
#include "engines/stark/gfx/driver.h"
#include "engines/stark/gfx/bitmap.h"
#include "engines/stark/gfx/surfacerenderer.h"
#include "engines/stark/stark.h"
#include "engines/stark/savemetadata.h"

#include "engines/engine.h"

#include "common/config-manager.h"
#include "common/savefile.h"

#include "gui/message.h"

namespace Stark {

SaveLoadMenuScreen::SaveLoadMenuScreen(Gfx::Driver *gfx, Cursor *cursor, Screen::Name screenName) :
		StaticLocationScreen(gfx, cursor, "LoadSaveLocation", screenName),
		_page(0),
		_maxPage(10) {
}

SaveLoadMenuScreen::~SaveLoadMenuScreen() {
}

void SaveLoadMenuScreen::open() {
	StaticLocationScreen::open();

	_maxPage = computeMaxPage();

	_page = StarkSettings->getIntSetting(Settings::kSaveLoadPage);
	if (_page > _maxPage) {
		_page = _maxPage;
	}

	_widgets.push_back(new StaticLocationWidget(
			"loadsavebg",
			nullptr,
			nullptr));

	_widgets.push_back(new StaticLocationWidget(
			"back to index",
			CLICK_HANDLER(SaveLoadMenuScreen, backHandler),
			nullptr));
	_widgets.back()->setupSounds(0, 1);

	_widgets.push_back(new StaticLocationWidget(
			"Cancel",
			CLICK_HANDLER(SaveLoadMenuScreen, backHandler),
			nullptr));
	_widgets.back()->setupSounds(0, 1);

	_widgets.push_back(new StaticLocationWidget(
			"SaveText",
			nullptr,
			nullptr));

	_widgets.push_back(new StaticLocationWidget(
			"LoadText",
			nullptr,
			nullptr));

	_widgets.push_back(new StaticLocationWidget(
			"Back",
			CLICK_HANDLER(SaveLoadMenuScreen, prevPageHandler),
			nullptr));
	_widgets.back()->setupSounds(0, 1);
	_widgets.back()->setTextColor(Gfx::Color(0, 0, 0));
	_widgets.back()->setVisible(_page > 0);

	_widgets.push_back(new StaticLocationWidget(
			"Next",
			CLICK_HANDLER(SaveLoadMenuScreen, nextPageHandler),
			nullptr));
	_widgets.back()->setupSounds(0, 1);
	_widgets.back()->setTextColor(Gfx::Color(0, 0, 0));
	_widgets.back()->setVisible(_page < _maxPage);

	loadSaveData(_page);
}

void SaveLoadMenuScreen::close() {
	ConfMan.flushToDisk();
	StaticLocationScreen::close();
}

int SaveLoadMenuScreen::computeMaxPage() {
	const char *target = ConfMan.getActiveDomainName().c_str();

	int maxSlot = 0;
	Common::StringArray saves = StarkEngine::listSaveNames(target);
	for (Common::StringArray::const_iterator filename = saves.begin(); filename != saves.end(); filename++) {
		int slot = StarkEngine::getSaveNameSlot(target, *filename);

		if (slot > maxSlot) {
			maxSlot = slot;
		}
	}

	// Allow using one more page than the last page with saves
	int maxPage = CLIP((maxSlot / _slotPerPage) + 1, 0, 110);
	if (maxPage < 10) {
		maxPage = 10;
	}

	return maxPage;
}

void SaveLoadMenuScreen::backHandler() {
	StarkUserInterface->backPrevScreen();
}

void SaveLoadMenuScreen::checkError(Common::Error error) {
	if (error.getCode() != Common::kNoError) {
		GUI::MessageDialog dialog(error.getDesc());
		dialog.runModal();
	}
}

void SaveLoadMenuScreen::removeSaveDataWidgets() {
	assert(_widgets.size() == 7 + _slotPerPage);

	for (int i = 0; i < _slotPerPage; ++i) {
		delete _widgets.back();
		_widgets.pop_back();
	}
}

void SaveLoadMenuScreen::loadSaveData(int page) {
	for (int i = 0; i < _slotPerPage; ++i) {
		_widgets.push_back(new SaveDataWidget(i + page * _slotPerPage, _gfx, this));
	}
}

void SaveLoadMenuScreen::changePage(int page) {
	assert(page >= 0 && page <= _maxPage);

	removeSaveDataWidgets();
	loadSaveData(page);

	_widgets[kWidgetBack]->setVisible(page > 0);
	_widgets[kWidgetNext]->setVisible(page < _maxPage);

	StarkSettings->setIntSetting(Settings::kSaveLoadPage, page);
	_page = page;
}

void SaveMenuScreen::open() {
	SaveLoadMenuScreen::open();
	_widgets[kWidgetLoadText]->setVisible(false);
}

void SaveMenuScreen::onWidgetSelected(SaveDataWidget *widget) {
	if (widget->hasSave()) {
		_slotToSaveAfterConfirm = widget;

		Common::String format = StarkGameMessage->getTextByKey(GameMessage::kOverwriteSave);
		Common::String prompt = Common::String::format(format.c_str(), widget->getName().c_str());

		StarkUserInterface->confirm(prompt, this, &SaveMenuScreen::saveConfirmSlot);
	} else {
		saveGameToSlot(widget);
	}
}

void SaveMenuScreen::saveConfirmSlot() {
	assert(_slotToSaveAfterConfirm);

	saveGameToSlot(_slotToSaveAfterConfirm);
	_slotToSaveAfterConfirm = nullptr;
}

void SaveMenuScreen::saveGameToSlot(SaveDataWidget *widget) {
	checkError(g_engine->saveGameState(widget->getSlot(), StarkGameChapter->getCurrentChapterTitle()));

	// Freeze the screen for a while to let the user notice the change
	widget->loadSaveDataElements();
	render();
	StarkGfx->flipBuffer();
	g_system->delayMillis(100);
	render();
	StarkGfx->flipBuffer();

	StarkUserInterface->backPrevScreen();
}

void LoadMenuScreen::open() {
	SaveLoadMenuScreen::open();
	_widgets[kWidgetSaveText]->setVisible(false);
}

void LoadMenuScreen::onWidgetSelected(SaveDataWidget *widget) {
	if (!StarkGlobal->getCurrent()) {
		checkError(g_engine->loadGameState(widget->getSlot()));
	} else {
		_slotToLoadAfterConfirm = widget->getSlot();
		StarkUserInterface->confirm(GameMessage::kEndAndLoad, this, &LoadMenuScreen::loadConfirmSlot);
	}
}

void LoadMenuScreen::loadConfirmSlot() {
	assert(_slotToLoadAfterConfirm >= 0);

	checkError(g_engine->loadGameState(_slotToLoadAfterConfirm));
	_slotToLoadAfterConfirm = -1;
}

SaveDataWidget::SaveDataWidget(int slot, Gfx::Driver *gfx, SaveLoadMenuScreen *screen) :
		StaticLocationWidget(nullptr, nullptr, nullptr),
		_slot(slot),
		_screen(screen),
		_thumbWidth(kThumbnailWidth),
		_thumbHeight(kThumbnailHeight),
		_bitmap(gfx->createBitmap()),
		_outline(gfx->createBitmap()),
		_surfaceRenderer(gfx->createSurfaceRenderer()),
		_textDesc(gfx),
		_textTime(gfx),
		_isMouseHovered(false),
		_hasSave(false),
		_name() {
	// Load from the save data
	loadSaveDataElements();

	_textDesc.setColor(_textColor);
	_textDesc.setFont(FontProvider::kCustomFont, 3);

	_textTime.setColor(_textColor);
	_textTime.setFont(FontProvider::kCustomFont, 3);

	Graphics::PixelFormat pixelFormat = Gfx::Driver::getRGBAPixelFormat();
	uint32 outlineColor = pixelFormat.ARGBToColor(
			_outlineColor.a, _outlineColor.r, _outlineColor.g, _outlineColor.b
	);

	// Create the outline bitmap
	Graphics::Surface lineSurface;
	lineSurface.create(_thumbWidth, _thumbHeight, pixelFormat);
	lineSurface.drawThickLine(0, 0, _thumbWidth - 1, 0, 2, 2, outlineColor);
	lineSurface.drawThickLine(0, 0, 0, _thumbHeight - 1, 2, 2, outlineColor);
	lineSurface.drawThickLine(_thumbWidth - 2, 0, _thumbWidth - 2, _thumbHeight - 2, 2, 2, outlineColor);
	lineSurface.drawThickLine(0, _thumbHeight - 2, _thumbWidth - 2, _thumbHeight - 2, 2, 2, outlineColor);

	_outline->update(&lineSurface);
	lineSurface.free();

	// Set the position
	_thumbPos.x = 41 + (_slot % SaveLoadMenuScreen::_slotPerRow) * (_thumbWidth + 39);
	_thumbPos.y = 61 + (_slot % SaveLoadMenuScreen::_slotPerPage / SaveLoadMenuScreen::_slotPerColumn) * (_thumbHeight + 38);

	_textDescPos.x = _thumbPos.x;
	_textDescPos.y = _thumbPos.y + _thumbHeight + 2;

	_textTimePos.x = _thumbPos.x;
	_textTimePos.y = _textDescPos.y + 12;
}

SaveDataWidget::~SaveDataWidget() {
	delete _bitmap;
	delete _outline;
	delete _surfaceRenderer;
}

void SaveDataWidget::render() {
	_surfaceRenderer->render(_bitmap, _thumbPos);
	_textDesc.render(_textDescPos);
	_textTime.render(_textTimePos);
	if (_isMouseHovered) {
		_surfaceRenderer->render(_outline, _thumbPos);
	}
}

bool SaveDataWidget::isMouseInside(const Common::Point &mousePos) const {
	return mousePos.x >= _thumbPos.x && mousePos.x <= _thumbPos.x + _thumbWidth &&
		   mousePos.y >= _thumbPos.y && mousePos.y <= _thumbPos.y + _thumbHeight;
}

void SaveDataWidget::onClick() {
	StaticLocationWidget::onClick();
	_screen->onWidgetSelected(this);
}

void SaveDataWidget::onMouseMove(const Common::Point &mousePos) {
	StaticLocationWidget::onMouseMove(mousePos);
	_isMouseHovered = isMouseInside(mousePos);
}

void SaveDataWidget::onScreenChanged() {
	StaticLocationWidget::onScreenChanged();
	_textDesc.reset();
	_textTime.reset();
}

void SaveDataWidget::loadSaveDataElements() {
	Common::String filename = StarkEngine::formatSaveName(ConfMan.getActiveDomainName().c_str(), _slot);
	Common::InSaveFile *save = g_system->getSavefileManager()->openForLoading(filename);
	if (save) {
		_hasSave = true;

		SaveMetadata metadata;
		StateReadStream stream(save);
		Common::ErrorCode metadataErrorCode = metadata.read(&stream, filename);
		if (metadataErrorCode != Common::kNoError) {
			error("Unable to read save metadata with error code %d.", metadataErrorCode);
		}

		// Obtain the thumbnail
		if (metadata.version >= 9) {
			Graphics::Surface *thumb = metadata.readGameScreenThumbnail(&stream);
			_bitmap->update(thumb);
			_bitmap->setSamplingFilter(StarkSettings->getImageSamplingFilter());

			thumb->free();
			delete thumb;
		}

		// Obtain the text
		Common::String desc = metadata.description;
		Common::String time = Common::String::format("%02d:%02d:%02d %02d/%02d/%02d",
				metadata.saveHour, metadata.saveMinute, metadata.saveSecond,
				metadata.saveMonth, metadata.saveDay, metadata.saveYear % 100);

		_textDesc.setText(desc);
		_textTime.setText(time);
		_name = desc + " " + time;
	} else {
		_hasSave = false;
		setVisible(_screen->isSaveMenu());
	}
}

} // End of namespace Stark