aboutsummaryrefslogtreecommitdiff
path: root/engines/bladerunner/ui/kia_section_load.cpp
blob: 62facbde397a4549dc561268890efb989ab97d0a (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
/* 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 "bladerunner/ui/kia_section_load.h"

#include "bladerunner/audio_player.h"
#include "bladerunner/bladerunner.h"
#include "bladerunner/game_info.h"
#include "bladerunner/savefile.h"
#include "bladerunner/text_resource.h"
#include "bladerunner/time.h"
#include "bladerunner/ui/kia.h"
#include "bladerunner/ui/kia_shapes.h"
#include "bladerunner/ui/ui_container.h"
#include "bladerunner/ui/ui_scroll_box.h"

#include "common/error.h"
#include "common/system.h"

namespace BladeRunner {

KIASectionLoad::KIASectionLoad(BladeRunnerEngine *vm) : KIASectionBase(vm) {
	_uiContainer = new UIContainer(_vm);
	_scrollBox   = new UIScrollBox(_vm, scrollBoxCallback, this, 1025, 0, true, Common::Rect(155, 158, 461, 346), Common::Rect(506, 160, 506, 350));
	_uiContainer->add(_scrollBox);

	_timeLast = 0;
	_timeLeft = 0;

	_hoveredLineId = -1;
	_displayingLineId = -1;
	_newGameEasyLineId = -1;
	_newGameMediumLineId = -1;
	_newGameHardLineId = -1;
}

KIASectionLoad::~KIASectionLoad() {
	_uiContainer->clear();
	delete _scrollBox;
	delete _uiContainer;
}

void KIASectionLoad::open() {
	_scheduledSwitch = false;
	_scrollBox->show();
	_scrollBox->clearLines();

	_saveList = SaveFileManager::list(_vm->getTargetName());

	if (!_saveList.empty()) {
		_scrollBox->addLine(_vm->_textOptions->getText(36), -1, 4); // Load game:
		for (uint i = 0; i < _saveList.size(); ++i) {
			_scrollBox->addLine(_saveList[i].getDescription(), i, 0);
		}
		_scrollBox->addLine("", -1, 4);
	}

	_newGameEasyLineId = _saveList.size();
	_newGameMediumLineId = _saveList.size() + 1;
	_newGameHardLineId = _saveList.size() + 2;

	_scrollBox->addLine(_vm->_textOptions->getText(37), -1, 4); // New game:
	_scrollBox->addLine(_vm->_textOptions->getText(20), _newGameEasyLineId, 0); // Easy
	_scrollBox->addLine(_vm->_textOptions->getText(28), _newGameMediumLineId, 0); // Medium
	_scrollBox->addLine(_vm->_textOptions->getText(29), _newGameHardLineId, 0); // Hard

	_hoveredLineId = -1;
	_timeLast = _vm->_time->currentSystem();
	_timeLeft = 800;
}

void KIASectionLoad::close() {
	_scrollBox->hide();
	_vm->_kia->playerReset();

	_saveList.clear();
}

void KIASectionLoad::draw(Graphics::Surface &surface){
	_vm->_kia->_shapes->get(69)->draw(surface, 501, 123);

	_uiContainer->draw(surface);

	int selectedLineId = _scrollBox->getSelectedLineData();

	if (_hoveredLineId != selectedLineId) {
		if (selectedLineId >= 0 && selectedLineId < (int)_saveList.size() && _displayingLineId != selectedLineId) {
			if (_timeLeft == 0) {
				SaveStateDescriptor desc = SaveFileManager::queryMetaInfos(_vm->getTargetName(), selectedLineId);
				const Graphics::Surface *thumbnail = desc.getThumbnail();
				if (thumbnail != nullptr) {
					_vm->_kia->playImage(*thumbnail);
					_displayingLineId = selectedLineId;
				}
			}
		} else {
			_vm->_kia->playerReset();
			_timeLeft = 800;
			_displayingLineId = -1;
		}
		_hoveredLineId = selectedLineId;
	}

	uint32 now = _vm->_time->currentSystem();
	if (selectedLineId >= 0 && selectedLineId < (int)_saveList.size() && _displayingLineId != selectedLineId) {
		if (_timeLeft) {
			uint32 timeDiff = now - _timeLast;
			if (timeDiff >= _timeLeft) {
				SaveStateDescriptor desc = SaveFileManager::queryMetaInfos(_vm->getTargetName(), _saveList[selectedLineId].getSaveSlot());
				const Graphics::Surface *thumbnail = desc.getThumbnail();
				if (thumbnail != nullptr) {
					_vm->_kia->playImage(*thumbnail);
					_displayingLineId = selectedLineId;
				}
			} else {
				_timeLeft -= timeDiff;
			}
		}
	}

	_timeLast = now;
}

void KIASectionLoad::handleMouseMove(int mouseX, int mouseY) {
	_uiContainer->handleMouseMove(mouseX, mouseY);
}

void KIASectionLoad::handleMouseDown(bool mainButton) {
	_uiContainer->handleMouseDown(!mainButton);
}

void KIASectionLoad::handleMouseUp(bool mainButton) {
	_uiContainer->handleMouseUp(!mainButton);
}

void KIASectionLoad::handleMouseScroll(int direction) {
	_uiContainer->handleMouseScroll(direction);
}

void KIASectionLoad::scrollBoxCallback(void *callbackData, void *source, int lineData, int mouseButton) {
	KIASectionLoad *self = (KIASectionLoad *)callbackData;

	if (mouseButton == 0 && source == self->_scrollBox && lineData >= 0) {
		if (lineData == self->_newGameEasyLineId) {
			self->_vm->newGame(0);
		} else if (lineData == self->_newGameMediumLineId) {
			self->_vm->newGame(1);
		} else if (lineData == self->_newGameHardLineId) {
			self->_vm->newGame(2);
		} else {
			self->_vm->loadGameState(self->_saveList[lineData].getSaveSlot());
		}

		self->_vm->_audioPlayer->playAud(self->_vm->_gameInfo->getSfxTrack(513), 90, 0, 0, 50, 0);
		self->_vm->_kia->resume();
		self->_scheduledSwitch = true;
	}
}

} // End of namespace BladeRunner