File: inventorybox.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 (352 lines) | stat: -rw-r--r-- 9,837 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
/* 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/nancy/nancy.h"
#include "engines/nancy/graphics.h"
#include "engines/nancy/resource.h"
#include "engines/nancy/cursor.h"
#include "engines/nancy/sound.h"
#include "engines/nancy/input.h"
#include "engines/nancy/util.h"

#include "engines/nancy/ui/inventorybox.h"

#include "engines/nancy/state/scene.h"

#include "engines/nancy/ui/scrollbar.h"

namespace Nancy {
namespace UI {

InventoryBox::InventoryBox() :
		RenderObject(6),
		_scrollbar(nullptr),
		_scrollbarPos(0),
		_highlightedHotspot(-1),
		_inventoryData(nullptr) {}

InventoryBox::~InventoryBox() {
	_fullInventorySurface.free();
	_iconsSurface.free(); delete _scrollbar;
}

void InventoryBox::init() {
	auto *bootSummary = GetEngineData(BSUM);
	assert(bootSummary);

	_inventoryData = GetEngineData(INV);
	assert(_inventoryData);

	_order.clear();

	moveTo(bootSummary->inventoryBoxScreenPosition);
	g_nancy->_resource->loadImage(_inventoryData->inventoryBoxIconsImageName, _iconsSurface);

	_fullInventorySurface.create(_screenPosition.width(), _screenPosition.height() * ((g_nancy->getStaticData().numItems / 4) + 1), g_nancy->_graphics->getScreenPixelFormat());
	Common::Rect sourceRect = _screenPosition;
	sourceRect.moveTo(0, 0);
	_drawSurface.create(_fullInventorySurface, sourceRect);

	for (uint i = 0; i < 4; ++i) {
		Common::Rect &r = _itemHotspots[i].hotspot;
		r = _screenPosition;
		r.setWidth(r.width() / 2);
		r.setHeight(r.height() / 2);
		r.translate((i % 2) * r.width(), (i / 2) * r.height());
	}

	RenderObject::init();

	_scrollbar = new Scrollbar(	9,
								_inventoryData->scrollbarSrcBounds,
								_inventoryData->scrollbarDefaultPos,
								_inventoryData->scrollbarMaxScroll - _inventoryData->scrollbarDefaultPos.y);
	_scrollbar->init();
	_curtains.init();
}

void InventoryBox::updateGraphics() {
	if (_scrollbarPos != _scrollbar->getPos()) {
		_scrollbarPos = _scrollbar->getPos();

		onScrollbarMove();
	}
}

void InventoryBox::registerGraphics() {
	RenderObject::registerGraphics();
	_scrollbar->registerGraphics();
	_curtains.registerGraphics();
}

void InventoryBox::handleInput(NancyInput &input) {
	// Disable input when primary video is playing
	if (NancySceneState.getActiveConversation()) {
		return;
	}

	if (_order.size()) {
		_scrollbar->handleInput(input);
	}

	int hoveredHotspot = -1;

	for (uint i = 0; i < 4; ++i) {
		if (_itemHotspots[i].hotspot.contains(input.mousePos)) {
			if (NancySceneState.getHeldItem() != -1) {
				g_nancy->_cursor->setCursorType(CursorManager::kHotspotArrow);
				if (input.input & NancyInput::kLeftMouseButtonUp) {
					NancySceneState.addItemToInventory(NancySceneState.getHeldItem());
					g_nancy->_sound->playSound("BULS");
				}
			} else if (_itemHotspots[i].itemID != -1) {
				g_nancy->_cursor->setCursorType(CursorManager::kHotspotArrow);

				hoveredHotspot = i;

				if (input.input & NancyInput::kLeftMouseButtonUp) {
					uint16 itemID = _itemHotspots[i].itemID;
					INV::ItemDescription item = _inventoryData->itemDescriptions[itemID];
					byte disabled = NancySceneState.getItemDisabledState(itemID);

					if (!disabled) {
						// Item is not disabled
						NancySceneState.removeItemFromInventory(itemID, item.keepItem != kInvItemNewSceneView);
						_highlightedHotspot = -1;
						hoveredHotspot = -1;

						if (item.keepItem == kInvItemNewSceneView) {
							// Transport the player to a close-up scene, temporarily remove the item from the inventory
							NancySceneState.pushScene(itemID);
							SceneChangeDescription sceneChange;
							sceneChange.sceneID = item.sceneID;
							sceneChange.continueSceneSound = item.sceneSoundFlag;
							NancySceneState.changeScene(sceneChange);
						}
					} else {
						// Item is disabled
						if (disabled == 2) {
							// ...and set so it plays the "can't" sound when you click it
							NancySceneState.playItemCantSound(itemID);
						}
					}
				}
			}
			break;
		}
	}

	if (_highlightedHotspot != hoveredHotspot) {
		if (_highlightedHotspot != -1) {
			// Un-highlight last hovered item
			drawItemInSlot(_itemHotspots[_highlightedHotspot].itemID, _itemHotspots[_highlightedHotspot].itemOrder, false);
			_highlightedHotspot = -1;
		}

		if (hoveredHotspot != -1) {
			// Highlight hovered item
			drawItemInSlot(_itemHotspots[hoveredHotspot].itemID, _itemHotspots[hoveredHotspot].itemOrder, true);
			_highlightedHotspot = hoveredHotspot;
		}
	}
}

void InventoryBox::addItem(const int16 itemID) {
	if (_order.size() == 0) {
		// Adds first item, start curtains animation
		_curtains.setOpen(true);
	}
	Common::Array<int16> back = _order;
	_order.clear();
	_order.push_back(itemID);
	_order.push_back(back);

	onReorder();
}

void InventoryBox::removeItem(const int16 itemID) {
	for (auto &i : _order) {
		if (i == itemID) {
			_order.erase(&i);
			onReorder();
			break;
		}
	}
}

void InventoryBox::onReorder() {
	onScrollbarMove();

	_fullInventorySurface.clear();
	for (uint i = 0; i < _order.size(); ++i) {
		drawItemInSlot(_order[i], i);
	}

	if (_order.size() > 0) {
		_curtains.setOpen(true);
	} else {
		_curtains.setOpen(false);
	}

	_needsRedraw = true;
}

void InventoryBox::setHotspots(const uint pageNr) {
	for (uint i = 0; i < 4; ++i) {
		if (i + pageNr * 4 < _order.size()) {
			_itemHotspots[i].itemID = _order[i + pageNr * 4];
			_itemHotspots[i].itemOrder = i + pageNr * 4;
		} else {
			_itemHotspots[i].itemID = -1;
			_itemHotspots[i].itemOrder = -1;
		}
	}
}

void InventoryBox::drawItemInSlot(const uint itemID, const uint slotID, const bool highlighted) {
	auto &item = _inventoryData->itemDescriptions[itemID];
	Common::Rect dest;

	dest.setWidth(_screenPosition.width() / 2);
	dest.setHeight(_screenPosition.height() / 2);
	dest.moveTo((slotID % 2) * dest.width(), (slotID / 2) * dest.height());
	Common::Point destPoint = Common::Point (dest.left, dest.top);

	_fullInventorySurface.blitFrom(_iconsSurface, highlighted ? item.highlightedSourceRect : item.sourceRect, destPoint);
	_needsRedraw = true;
}

void InventoryBox::onScrollbarMove() {
	float scrollPos = _scrollbar->getPos();

	float numPages = (_order.size() - 1) / 4 + 1;
	float pageFrac = 1 / numPages;
	uint curPage = MIN<uint>(scrollPos / pageFrac, numPages - 1);

	Common::Rect sourceRect = _screenPosition;
	sourceRect.moveTo(0, curPage * (sourceRect.height() - 1));
	_drawSurface.create(_fullInventorySurface, sourceRect);

	setHotspots(curPage);

	_needsRedraw = true;
}

InventoryBox::Curtains::Curtains() :
	RenderObject(10),
	_soundTriggered(false),
	_areOpen(false),
	_curFrame(0),
	_numFrames(g_nancy->getGameType() == kGameTypeVampire ? 10 : 7) {}

void InventoryBox::Curtains::init() {
	auto *inventoryData = GetEngineData(INV);
	assert(inventoryData);

	moveTo(inventoryData->curtainsScreenPosition);
	Common::Rect bounds = _screenPosition;
	bounds.moveTo(0, 0);
	_drawSurface.create(bounds.width(), bounds.height(), g_nancy->_graphics->getInputPixelFormat());

	if (g_nancy->getGameType() == kGameTypeVampire) {
		uint8 palette[256 * 3];
		g_nancy->_graphics->_object0.grabPalette(palette, 0, 256);
		_drawSurface.setPalette(palette, 0, 256);
	}

	_nextFrameTime = 0;
	setAnimationFrame(_curFrame);

	setTransparent(true);

	RenderObject::init();
}

void InventoryBox::Curtains::updateGraphics() {
	Time time = g_nancy->getTotalPlayTime();
	if (_areOpen) {
		if (_curFrame < _numFrames && time > _nextFrameTime) {
			auto *inventoryData = GetEngineData(INV);
			assert(inventoryData);

			setAnimationFrame(++_curFrame);
			_nextFrameTime = time + inventoryData->curtainsFrameTime;

			if (!_soundTriggered) {
				_soundTriggered = true;
				g_nancy->_sound->playSound("CURT");
			}
		}
	} else {
		if (_curFrame > 0 && time > _nextFrameTime) {
			auto *inventoryData = GetEngineData(INV);
			assert(inventoryData);

			setAnimationFrame(--_curFrame);
			_nextFrameTime = time + inventoryData->curtainsFrameTime;

			if (!_soundTriggered) {
				_soundTriggered = true;
				g_nancy->_sound->playSound("CURT");
			}
		}
	}

	if (_curFrame == 0 || _curFrame == _numFrames) {
		_soundTriggered = false;
	}
}

void InventoryBox::Curtains::setAnimationFrame(uint frame) {
	Graphics::ManagedSurface &_object0 = g_nancy->_graphics->_object0;
	Common::Rect srcRect;
	Common::Point destPoint;

	if (frame > (uint)(_numFrames - 1)) {
		// TVD keeps the last frame visible
		if (g_nancy->getGameType() > kGameTypeVampire) {
			setVisible(false);
		}

		return;
	} else {
		setVisible(true);
	}

	auto *inventoryData = GetEngineData(INV);
	assert(inventoryData);

	_drawSurface.clear(g_nancy->_graphics->getTransColor());

	// Draw left curtain
	srcRect = inventoryData->curtainAnimationSrcs[frame * 2];
	_drawSurface.blitFrom(_object0, srcRect, destPoint);

	// Draw right curtain
	srcRect = inventoryData->curtainAnimationSrcs[frame * 2 + 1];
	destPoint.x = getBounds().width() - srcRect.width();
	_drawSurface.blitFrom(_object0, srcRect, destPoint);

	_needsRedraw = true;
}

} // End of namespace UI
} // End of namespace Nancy