aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic/pet_control/pet_load_save.cpp
blob: 77b61262490ca0ebf1ac4b68ecc293b5c7ea4346 (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
/* 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 "titanic/pet_control/pet_load_save.h"
#include "titanic/pet_control/pet_control.h"
#include "titanic/core/project_item.h"
#include "titanic/titanic.h"

namespace Titanic {

int CPetLoadSave::_savegameSlotNum;

bool CPetLoadSave::setup(CPetControl *petControl, CPetGlyphs *owner) {
	CPetGlyph::setup(petControl, owner);
	_savegameSlotNum = -1;

	for (int idx = 0; idx < SAVEGAME_SLOTS_COUNT; ++idx) {
		Rect slotRect = getSlotBounds(idx);
		_slotNames[idx].setBounds(slotRect);
		_slotNames[idx].resize(3);
		_slotNames[idx].setMaxCharsPerLine(22);
		_slotNames[idx].setHasBorder(false);
		_slotNames[idx].setup();
	}

	Rect r1(0, 0, 68, 52);
	r1.moveTo(496, 388);
	_btnLoadSave.setBounds(r1);

	Rect r2(0, 0, 168, 78);
	r2.moveTo(309, 377);
	_gutter.setBounds(r2);
	return true;
}

bool CPetLoadSave::reset() {
	highlightChange();
	resetSlots();

	CPetControl *pet = getPetControl();
	if (pet) {
		_gutter.reset("PetSaveGutter", pet, MODE_UNSELECTED);
	}

	return true;
}

void CPetLoadSave::draw2(CScreenManager *screenManager) {
	_gutter.draw(screenManager);

	for (int idx = 0; idx < SAVEGAME_SLOTS_COUNT; ++idx)
		_slotNames[idx].draw(screenManager);

	_btnLoadSave.draw(screenManager);
}

bool CPetLoadSave::MouseButtonDownMsg(const Point &pt) {
	if (_btnLoadSave.MouseButtonDownMsg(pt))
		return true;
	
	checkSlotsHighlight(pt);
	return false;
}

bool CPetLoadSave::KeyCharMsg(Common::KeyCode key) {
	switch (key) {
	case Common::KEYCODE_TAB:
	case Common::KEYCODE_DOWN:
	case Common::KEYCODE_KP2:
		if (_savegameSlotNum != -1) {
			highlightSlot((_savegameSlotNum + 1) % 5);
			getPetControl()->makeDirty();
		}
		return true;

	case Common::KEYCODE_UP:
	case Common::KEYCODE_KP8:
		if (_savegameSlotNum != -1) {
			int slotNum = --_savegameSlotNum;			
			highlightSlot((slotNum == -1) ? SAVEGAME_SLOTS_COUNT - 1 : slotNum);
			getPetControl()->makeDirty();
		}
		return true;

	case Common::KEYCODE_RETURN:
	case Common::KEYCODE_KP_ENTER:
		execute();
		return true;

	default:
		return false;
	}
}

Rect CPetLoadSave::getSlotBounds(int index) {
	return Rect(323, 376 + index * 16, 473, 392 + index * 16);
}

void CPetLoadSave::resetSlots() {
	for (int idx = 0; idx < SAVEGAME_SLOTS_COUNT; ++idx) {
		_slotNames[idx].setText("Empty");
		_slotInUse[idx] = false;

		// Try and open up the savegame for access
		Common::InSaveFile *in = g_system->getSavefileManager()->openForLoading(
			g_vm->generateSaveName(idx));

		if (in) {
			// Read in the savegame header data
			CompressedFile file;
			file.open(in);

			TitanicSavegameHeader header;
			if (CProjectItem::readSavegameHeader(&file, header)) {
				_slotInUse[idx] = true;
				_slotNames[idx].setText(header._saveName);
			}

			if (header._thumbnail) {
				header._thumbnail->free();
				delete header._thumbnail;
			}

			file.close();
		}
	}

	highlightSlot(_savegameSlotNum);
}

void CPetLoadSave::highlightSlot(int index) {
	unhighlightSave(_savegameSlotNum);
	_savegameSlotNum = index;
	highlightChange();
	highlightSave(_savegameSlotNum);
}

void CPetLoadSave::highlightChange() {
	CPetSection *section = getPetSection();

	uint col = section ? section->getColor(3) : 0;
	for (int idx = 0; idx < SAVEGAME_SLOTS_COUNT; ++idx)
		_slotNames[idx].setColor(col);

	if (_savegameSlotNum != -1) {
		col = section ? section->getColor(4) : 0;
		_slotNames[_savegameSlotNum].setLineColor(0, col);
	}
}

bool CPetLoadSave::checkSlotsHighlight(const Point &pt) {
	for (int idx = 0; idx < SAVEGAME_SLOTS_COUNT; ++idx) {
		if (isSlotHighlighted(idx, pt)) {
			highlightSlot(idx);
			return true;
		}
	}

	return false;
}

bool CPetLoadSave::isSlotHighlighted(int index, const Point &pt) {
	Rect r = getSlotBounds(index);
	if (r.contains(pt)) {
		highlightSlot(index);
		return true;
	} else {
		return false;
	}
}

} // End of namespace Titanic