aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic/pet_control/pet_inventory.cpp
blob: 0f9f253cb0ffc9913e202f425414641aaa320344 (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
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
/* 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_inventory.h"
#include "titanic/pet_control/pet_control.h"
#include "titanic/carry/carry.h"
#include "titanic/titanic.h"

namespace Titanic {

CPetInventory::CPetInventory() : CPetSection(),
		_movie(nullptr), _isLoading(false), _titaniaBitFlags(0) {
	for (int idx = 0; idx < TOTAL_ITEMS; ++idx) {
		_itemBackgrounds[idx] = nullptr;
	}
}

bool CPetInventory::setup(CPetControl *petControl) {
	return setPetControl(petControl) && reset();
}

bool CPetInventory::reset() {
	_items.reset();
	_text.setup();
	_text.setColor(getColor(0));
	_text.setLineColor(0, getColor(0));

	return true;
}

void CPetInventory::draw(CScreenManager *screenManager) {
	_petControl->drawSquares(screenManager, 7);
	_items.draw(screenManager);
	_text.draw(screenManager);
}

Rect CPetInventory::getBounds() const {
	return _movie ? _movie->getBounds() : Rect();
}

void CPetInventory::changed(int changeType) {
	switch (changeType) {
	case 0:
	case 2:
		itemsChanged();
		break;
	case 1:
		removeInvalid();
		break;
	default:
		break;
	}
}

void CPetInventory::enterRoom(CRoomItem *room) {
	int index = _items.getHighlightIndex();
	if (index != -1) {
		_items.resetHighlight();
		_items.highlight(index);
	}
}

bool CPetInventory::MouseButtonDownMsg(CMouseButtonDownMsg *msg) {
	return _items.MouseButtonDownMsg(msg->_mousePos);
}

bool CPetInventory::MouseDragStartMsg(CMouseDragStartMsg *msg) {
	bool result = _items.MouseDragStartMsg(msg);
	if (result)
		_petControl->makeDirty();
	return result;
}

bool CPetInventory::MouseButtonUpMsg(CMouseButtonUpMsg *msg) {
	return _items.MouseButtonUpMsg(msg->_mousePos);
}

bool CPetInventory::MouseDoubleClickMsg(CMouseDoubleClickMsg *msg) {
	return _items.MouseDoubleClickMsg(msg->_mousePos);
}

bool CPetInventory::VirtualKeyCharMsg(CVirtualKeyCharMsg *msg) {
	return _items.VirtualKeyCharMsg(msg);
}

bool CPetInventory::MouseWheelMsg(CMouseWheelMsg *msg) {
	if (msg->_wheelUp)
		_items.scrollLeft();
	else
		_items.scrollRight();

	return true;
}

CGameObject *CPetInventory::dragEnd(const Point &pt) const {
	return _items.getObjectAt(pt);
}

bool CPetInventory::isValid(CPetControl *petControl) {
	setPetControl(petControl);
	return true;
}

void CPetInventory::load(SimpleFile *file, int param) {
	_titaniaBitFlags = file->readNumber();
}

void CPetInventory::postLoad() {
	reset();
	_isLoading = true;
	itemsChanged();
	_isLoading = false;
}

void CPetInventory::save(SimpleFile *file, int indent) {
	file->writeNumberLine(_titaniaBitFlags, indent);
}

void CPetInventory::enter(PetArea oldArea) {
	_items.enter();
}

void CPetInventory::leave() {
	_items.leave();
}

CGameObject *CPetInventory::getBackground(int index) const {
	return (index >= 0 && index < 46) ? _itemBackgrounds[index] : nullptr;
}

bool CPetInventory::setPetControl(CPetControl *petControl) {
	if (!petControl)
		return false;

	_petControl = petControl;
	_items.setup(7,  this);
	_items.setFlags(28);

	Rect tempRect(0, 0, 52, 52);
	for (uint idx = 0; idx < TOTAL_ITEMS; ++idx) {
		if (!g_vm->_itemNames[idx].empty()) {
			CString name = "3Pet" + g_vm->_itemNames[idx];
			_itemBackgrounds[idx] = petControl->getHiddenObject(name);
		}
	}

	tempRect = Rect(0, 0, 580, 15);
	tempRect.translate(32, 445);
	_text.setBounds(tempRect);
	_text.setHasBorder(false);

	return true;
}

void CPetInventory::change(CGameObject *item) {
	if (item) {
		CInventoryGlyphAction action(item, ACTION_CHANGE);
		_items.doAction(&action);
	}
}

void CPetInventory::itemRemoved(CGameObject *item) {
	if (item) {
		CInventoryGlyphAction action(item, ACTION_REMOVED);
		_items.doAction(&action);
	}
}

void CPetInventory::itemsChanged() {
	_items.clear();

	CGameObject *item = _petControl->getFirstObject();
	while (item) {
		CPetInventoryGlyph *glyph = new CPetInventoryGlyph();
		glyph->setup(_petControl, &_items);
		glyph->setItem(item, _isLoading);

		_items.push_back(glyph);
		item = _petControl->getNextObject(item);
	}
}

void CPetInventory::highlightItem(CGameObject *item) {
	int itemIndex = getItemIndex(item);
	_items.highlight(itemIndex);
}

int CPetInventory::getItemIndex(CGameObject *item) const {
	int index = 0;
	for (CGameObject *obj = _petControl->getFirstObject(); obj && obj != item;
			++index, obj = _petControl->getNextObject(obj)) {
	}

	return index;
}

CGameObject *CPetInventory::getTransformAnimation(int index) {
	if (index >= 0 && index < 46) {
		// Certain items are pieces of Titania, and they only have the
		// one-time initial transformation into Titania pieces
		CString name;
		int bits = 0;

		switch (index) {
		case 20:
			name = "PetEarMorph";
			bits = 4;
			break;
		case 21:
			name = "PetEarMorph1";
			bits = 8;
			break;
		case 22:
			name = "PetEyeMorph";
			bits = 1;
			break;
		case 23:
			name = "PetEyeMorph";
			bits = 2;
			break;
		case 36:
			name = "PetMouthMorph";
			bits = 32;
			break;
		case 39:
			name = "PetNoseMorph";
			bits = 16;
			break;
		default:
			break;
		}

		if (!(bits & _titaniaBitFlags) && !name.empty()) {
			CGameObject *obj = getPetControl()->getHiddenObject(name);
			assert(obj);

			_titaniaBitFlags = bits | _titaniaBitFlags;
			return obj;
		}
	}

	return nullptr;
}

void CPetInventory::playMovie(CGameObject *movie, bool repeat) {
	if (_movie)
		_movie->stopMovie();
	_movie = movie;

	if (_movie) {
		if (repeat)
			_movie->playMovie(0, 14, MOVIE_REPEAT);
		else
			_movie->playMovie(0);
	}
}

void CPetInventory::removeInvalid() {
	_items.removeInvalid();
}

} // End of namespace Titanic