aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic/pet_control/pet_inventory_glyphs.cpp
blob: 9d0dd518b9c3f03746a507957aa10d6dd2fc54b1 (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
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
/* 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_glyphs.h"
#include "titanic/pet_control/pet_control.h"
#include "titanic/pet_control/pet_inventory.h"
#include "titanic/messages/pet_messages.h"
#include "titanic/titanic.h"

namespace Titanic {

const uint ITEM_MODES[40] = {
	0, 2, 11, 10, 12, 13, 9, 40, 7, 6,
	4, 5, 8, 15, 19, 24, 25, 26, 30, 20,
	21, 22, 23, 36, 39, 39, 31, 31, 32, 32,
	33, 34, 35, 38, 41, 42, 43, 44, 45, 37
};

void CPetInventoryGlyph::enter() {
	startRepeatedMovie();
}

void CPetInventoryGlyph::leave() {
	stopMovie();
}

void CPetInventoryGlyph::drawAt(CScreenManager *screenManager, const Point &pt, bool isHighlighted_) {
	if (!_active)
		return;

	if (_singular) {
		if (_singular->hasActiveMovie()) {
			if (isHighlighted_)
				_singular->draw(screenManager);
			else
				_singular->draw(screenManager, pt);
			return;
		}

		_singular = nullptr;
		if (_repeated && isHighlighted_) {
			_repeated->setPosition(pt);
			startRepeatedMovie();
		}
	}

	if (_repeated) {
		if (isHighlighted_)
			_repeated->draw(screenManager);
		else
			_repeated->draw(screenManager, pt);
	} else if (_singular) {
		_singular->draw(screenManager, pt, Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT));
	}
}


void CPetInventoryGlyph::unhighlightCurrent() {
	if (_singular) {
		_singular->setPosition(Point(0, 0));
		stopMovie();
	} else if (_repeated) {
		_repeated->setPosition(Point(0, 0));
		_repeated->loadFrame(0);
		stopMovie();
	}
}

void CPetInventoryGlyph::highlightCurrent(const Point &pt) {
	reposition(pt);
	if (_item) {
		CPETObjectSelectedMsg selectedMsg;
		selectedMsg.execute(_item);
	}
}

void CPetInventoryGlyph::glyphFocused(const Point &topLeft, bool flag) {
	if (_repeated && flag)
		_repeated->setPosition(topLeft);
}

bool CPetInventoryGlyph::dragGlyph(const Point &topLeft, CMouseDragStartMsg *msg) {
	if (!_item)
		return false;

	if (_repeated) {
		_active = false;
		stopMovie();
	}

	CPetControl *petControl = getPetControl();
	if (!petControl)
		return false;

	CGameObject *carryParcel = petControl->getHiddenObject("CarryParcel");
	CGameObject *item = _item;

	if (petControl->isSuccUBusActive() && carryParcel) {
		petControl->removeFromInventory(_item, carryParcel, false, true);
		petControl->removeFromInventory(_item, false, false);

		carryParcel->setPosition(Point(msg->_mousePos.x - carryParcel->_bounds.width() / 2,
			msg->_mousePos.y - carryParcel->_bounds.height() / 2));
		carryParcel->setPosition(Point(SCREEN_WIDTH, SCREEN_HEIGHT));
		item = carryParcel;
	} else {
		petControl->removeFromInventory(_item, false, true);

		_item->setPosition(Point(msg->_mousePos.x - _item->_bounds.width() / 2,
			msg->_mousePos.y - _item->_bounds.height() / 2));
		_item->setVisible(true);
	}

	msg->_handled = true;
	if (msg->execute(item)) {
		_item = nullptr;
		_repeated = nullptr;
		_active = false;
		petControl->setAreaChangeType(1);
		return true;
	} else {
		petControl->addToInventory(item);
		return false;
	}
}

void CPetInventoryGlyph::getTooltip(CTextControl *text) {
	if (text) {
		text->setText("");

		if (_active && _item) {
			int itemIndex = populateItem(_item, 0);
			if (itemIndex >= 14 && itemIndex <= 18) {
				// Variations of the chicken
				CPETObjectStateMsg stateMsg(0);
				stateMsg.execute(_item);

				CString temperature = g_vm->_strings[stateMsg._value ? A_HOT : A_COLD];
				text->setText(CString::format("%s %s", temperature.c_str(),
					g_vm->_itemDescriptions[itemIndex].c_str()
				));

			} else {
				text->setText(g_vm->_itemDescriptions[itemIndex]);
			}
		}
	}
}

bool CPetInventoryGlyph::doAction(CGlyphAction *action) {
	CInventoryGlyphAction *invAction = static_cast<CInventoryGlyphAction *>(action);
	CPetInventoryGlyphs *owner = dynamic_cast<CPetInventoryGlyphs *>(_owner);
	if (!invAction)
		return false;

	switch (invAction->getMode()) {
	case ACTION_REMOVED:
		if (invAction->_item == _item) {
			_item = nullptr;
			_repeated = nullptr;
			_active = false;
		}
		break;

	case ACTION_CHANGE:
		if (_item == invAction->_item && _owner) {
			int v = populateItem(_item, 0);
			_repeated = owner->getBackground(v);

			if (isHighlighted()) {
				Point glyphPos = _owner->getHighlightedGlyphPos();
				reposition(glyphPos);
				updateTooltip();
			}
		}
		break;

	default:
		break;
	}

	return true;
}

void CPetInventoryGlyph::setItem(CGameObject *item, bool isLoading) {
	_item = item;

	if (_owner && item) {
		int idx = populateItem(item, isLoading);
		_repeated = static_cast<CPetInventoryGlyphs *>(_owner)->getBackground(idx);
		_singular = static_cast<CPetInventory *>(getPetSection())->getTransformAnimation(idx);
	}
}

int CPetInventoryGlyph::populateItem(CGameObject *item, bool isLoading) {
	// Scan the master item names list
	CString itemName = item->getName();
	int itemIndex = -1;
	for (int idx = 0; idx < 40 && itemIndex == -1; ++idx) {
		if (itemName == g_vm->_itemIds[idx])
			itemIndex = idx;
	}
	if (itemIndex == -1)
		return -1;

	// Some objects can be in multiple different states. These are handled 
	// below to give each the correct inventory glyph and description
	switch (ITEM_MODES[itemIndex]) {
	case 0:
		// Maitre d'Bot's left arm
		switch (getItemIndex(item, isLoading)) {
		case 0:
		case 1:
			return 0;
		case 2:
		case 3:
			return 1;
		default:
			return 0;
		}

	case 2:
		// Maitre d'Bot's right arm
		switch (getItemIndex(item, isLoading)) {
		case 0:
			return 2;
		default:
			return 3;
		}
		break;

	case 15:
		// Chicken
		switch (getItemIndex(item, isLoading)) {
		case 0:
		case 1:
			return 14;
		case 2:
			return 16;
		case 3:
			return 15;
		case 4:
			return 17;
		case 5:
			return 18;
		default:
			return 15;
		}
		break;

	case 26:
		// Beer glass
		switch (getItemIndex(item, isLoading)) {
		case 0:
			return 26;
		case 1:
			return 29;
		case 2:
			return 28;
		case 3:
			return 27;
		default:
			return 26;
		}
		break;

	default:
		break;
	}

	return ITEM_MODES[itemIndex];
}

int CPetInventoryGlyph::getItemIndex(CGameObject *item, bool isLoading) {
	int frameNum = item->getFrameNumber();
	int movieFrame = item->getMovieFrame();

	if (isLoading && frameNum != -1 && frameNum != movieFrame) {
		item->loadFrame(frameNum);
		movieFrame = frameNum;
	}

	return movieFrame;
}

void CPetInventoryGlyph::startRepeatedMovie() {
	if (_owner) {
		CPetInventory *section = dynamic_cast<CPetInventory *>(_owner->getOwner());
		if (section)
			section->playMovie(_repeated, true);
	}
}

void CPetInventoryGlyph::startSingularMovie() {
	if (_owner) {
		CPetInventory *section = dynamic_cast<CPetInventory *>(_owner->getOwner());
		if (section)
			section->playMovie(_singular, false);
	}
}

void CPetInventoryGlyph::stopMovie() {
	if (_owner) {
		CPetInventory *section = dynamic_cast<CPetInventory *>(_owner->getOwner());
		if (section)
			section->playMovie(nullptr);
	}
}

void CPetInventoryGlyph::reposition(const Point &pt) {
	if (_singular) {
		// Special transformation of item to piece of Titania
		_singular->setPosition(pt);
		startSingularMovie();
	} else if (_repeated) {
		// Standard repeating animation
		_repeated->setPosition(pt);
		startRepeatedMovie();
	}
}

/*------------------------------------------------------------------------*/

bool CPetInventoryGlyphs::doAction(CInventoryGlyphAction *action) {
	for (iterator i = begin(); i != end(); ++i) {
		(*i)->doAction(action);
	}

	return true;
}

CGameObject *CPetInventoryGlyphs::getBackground(int index) {
	return _owner ? _owner->getBackground(index) : nullptr;
}

} // End of namespace Titanic