File: inventorywindow.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 (302 lines) | stat: -rw-r--r-- 9,591 bytes parent folder | download | duplicates (3)
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
/* 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/stark/ui/world/inventorywindow.h"
#include "engines/stark/ui/cursor.h"
#include "engines/stark/ui/world/actionmenu.h"
#include "engines/stark/gfx/driver.h"
#include "engines/stark/resources/knowledgeset.h"
#include "engines/stark/resources/item.h"
#include "engines/stark/resources/pattable.h"
#include "engines/stark/services/global.h"
#include "engines/stark/services/services.h"
#include "engines/stark/services/staticprovider.h"
#include "engines/stark/services/gameinterface.h"
#include "engines/stark/visual/image.h"

namespace Stark {

static const int kAutoCloseSuspended = -1;
static const int kAutoCloseDisabled  = -2;
static const int kAutoCloseDelay     = 200;

InventoryWindow::InventoryWindow(Gfx::Driver *gfx, Cursor *cursor, ActionMenu *actionMenu) :
		Window(gfx, cursor),
	_actionMenu(actionMenu),
	_firstVisibleSlot(0),
	_selectedInventoryItem(-1),
	_autoCloseTimeRemaining(kAutoCloseDisabled) {
	// The window has the same size as the game window
	_position = Common::Rect(Gfx::Driver::kGameViewportWidth, Gfx::Driver::kGameViewportHeight);
	_position.translate(0, Gfx::Driver::kTopBorderHeight);

	_backgroundImage = StarkStaticProvider->getUIImage(StaticProvider::kInventoryBg);

	// Center the background in the window
	_backgroundRect = Common::Rect(_backgroundImage->getWidth(), _backgroundImage->getHeight());
	_backgroundRect.translate((_position.width() - _backgroundRect.width()) / 2,
			(_position.height() - _backgroundRect.height()) / 2);

	_scrollUpArrowImage = StarkStaticProvider->getUIElement(StaticProvider::kInventoryScrollUpArrow);
	_scrollDownArrowImage = StarkStaticProvider->getUIElement(StaticProvider::kInventoryScrollDownArrow);

	_scrollUpArrowRect = Common::Rect(_scrollUpArrowImage->getWidth(), _scrollUpArrowImage->getHeight());
	_scrollUpArrowRect.translate(_backgroundRect.right - _scrollUpArrowRect.width(),
	                             _backgroundRect.top + 2);
	_scrollDownArrowRect = Common::Rect(_scrollDownArrowImage->getWidth(), _scrollDownArrowImage->getHeight());
	_scrollDownArrowRect.translate(_backgroundRect.right - _scrollDownArrowRect.width(),
	                               _backgroundRect.bottom - _scrollDownArrowRect.height() - 2);
}

void InventoryWindow::open() {
	if (!_visible) {
		_actionMenu->close();
	}

	_visible = true;

	// The user needs to move the mouse over the background at least once
	// before autoclose is enabled.
	_autoCloseTimeRemaining = kAutoCloseDisabled;
}

void InventoryWindow::close() {
	if (_visible) {
		_actionMenu->close();
	}

	_visible = false;
}

void InventoryWindow::setSelectedInventoryItem(int16 selectedInventoryItem) {
	// The first 4 elements are UI elements (Eye, Mouth, Hand, ...)
	// Scripts pass 0 when they want to clear the selected inventory item
	if (selectedInventoryItem < 4) {
		_selectedInventoryItem = -1;
	} else {
		_selectedInventoryItem = selectedInventoryItem;
	}
}

int16 InventoryWindow::getSelectedInventoryItem() const {
	return _selectedInventoryItem;
}

Common::Rect InventoryWindow::getSlotRect(uint32 slot) const {
	Common::Rect rect = Common::Rect(64, 64);
	rect.translate(
			96 * (slot % 5) + _backgroundRect.left + 24,
			96 * (slot / 5) + _backgroundRect.left + 8); // The original uses left here as well
	return rect;
}

Common::Rect InventoryWindow::getItemRect(uint32 slot, VisualImageXMG *image) const {
	Common::Rect rect = getSlotRect(slot % _visibleSlotsCount);

	// Center the image in the inventory slot
	rect.translate((rect.width() - image->getWidth()) / 2,
	              (rect.height() - image->getHeight()) / 2);

	return rect;
}

void InventoryWindow::onRender() {
	_renderEntries = StarkGlobal->getInventory()->getInventoryRenderEntries();

	_backgroundImage->render(Common::Point(_backgroundRect.left, _backgroundRect.top), false);
	drawScrollArrows();

	for (uint i = _firstVisibleSlot; i < _renderEntries.size() && isSlotVisible(i); i++) {
		VisualImageXMG *image = _renderEntries[i]->getImage();

		// Get the item rect
		Common::Rect pos = getItemRect(i, image);

		image->render(Common::Point(pos.left, pos.top), false);
	}
}

void InventoryWindow::drawScrollArrows() const {
	if (canScrollUp()) {
		_scrollUpArrowImage->render(Common::Point(_scrollUpArrowRect.left, _scrollUpArrowRect.top), false);
	}
	if (canScrollDown()) {
		_scrollDownArrowImage->render(Common::Point(_scrollDownArrowRect.left, _scrollDownArrowRect.top), false);
	}
}

void InventoryWindow::checkObjectAtPos(Common::Point pos, Resources::ItemVisual **item, int16 selectedInventoryItem, int16 &singlePossibleAction) {
	*item = nullptr;
	singlePossibleAction = -1;

	// Check for inventory mouse overs
	for (uint i = _firstVisibleSlot; i < _renderEntries.size() && isSlotVisible(i); i++) {
		VisualImageXMG *image = _renderEntries[i]->getImage();
		Common::Rect itemRect = getItemRect(i, image);

		if (itemRect.contains(pos)) {
			*item = _renderEntries[i]->getOwner();
			break;
		}
	}

	if (!*item) {
		// No item at specified position
		return;
	}

	if (selectedInventoryItem == -1) {
		Resources::ActionArray actionsPossible;
		actionsPossible = StarkGameInterface->listStockActionsPossibleForObject(*item);

		if (actionsPossible.empty()) {
			// The item can still be taken
			singlePossibleAction = Resources::PATTable::kActionUse;
		}
	} else {
		if (StarkGameInterface->itemHasAction(*item, selectedInventoryItem)) {
			singlePossibleAction = selectedInventoryItem;
		}
	}
}

void InventoryWindow::onMouseMove(const Common::Point &pos) {
	Resources::ItemVisual *hoveredItem = nullptr;
	int16 hoveredItemAction = -1;

	checkObjectAtPos(pos, &hoveredItem, _selectedInventoryItem, hoveredItemAction);

	if (_selectedInventoryItem == -1) {
		if (hoveredItem) {
			_cursor->setCursorType(Cursor::kActive);
		} else if ((canScrollDown() && _scrollDownArrowRect.contains(pos))
		           || (canScrollUp() && _scrollUpArrowRect.contains(pos))) {
			_cursor->setCursorType(Cursor::kActive);
			_cursor->setItemActive(false);
		} else {
			_cursor->setCursorType(Cursor::kDefault);
		}
		_cursor->setItemActive(false);
	} else {
		VisualImageXMG *cursorImage = StarkGameInterface->getCursorImage(_selectedInventoryItem);
		_cursor->setCursorImage(cursorImage);
		_cursor->setItemActive(hoveredItemAction == _selectedInventoryItem);
	}

	if (hoveredItem) {
		Common::String hint = StarkGameInterface->getItemTitle(hoveredItem);
		_cursor->setMouseHint(hint);
	} else {
		_cursor->setMouseHint("");
	}

	if (!_backgroundRect.contains(pos)) {
		if (_autoCloseTimeRemaining == kAutoCloseSuspended) {
			_autoCloseTimeRemaining = kAutoCloseDelay;
		}
	} else {
		_autoCloseTimeRemaining = kAutoCloseSuspended;
	}
}

void InventoryWindow::onClick(const Common::Point &pos) {
	_actionMenu->close();

	Resources::ItemVisual *clickedItem = nullptr;
	int16 clickedItemAction = -1;
	checkObjectAtPos(pos, &clickedItem, _selectedInventoryItem, clickedItemAction);

	if (clickedItem) {
		// An item was clicked
		if (clickedItemAction != -1) {
			// A single action is possible
			if (clickedItemAction == Resources::PATTable::kActionUse) {
				setSelectedInventoryItem(clickedItem->getIndex());
			} else {
				StarkGameInterface->itemDoAction(clickedItem, clickedItemAction);
			}
		} else {
			// Multiple actions are possible
			if (_selectedInventoryItem == -1) {
				_actionMenu->open(clickedItem, Common::Point());
			}
		}
	} else if (_scrollDownArrowRect.contains(pos)) {
		if (canScrollDown()) {
			scrollDown();
		}
	} else if (_scrollUpArrowRect.contains(pos)) {
		if (canScrollUp()) {
			scrollUp();
		}
	} else {
		// Nothing was under the mouse cursor, close the inventory
		close();
	}
}

void InventoryWindow::onRightClick(const Common::Point &pos) {
	if (_selectedInventoryItem == -1) {
		close();
	} else {
		setSelectedInventoryItem(-1);
	}
}

void InventoryWindow::reset() {
	_renderEntries.clear();
}

bool InventoryWindow::isSlotVisible(uint32 slot) const {
	return slot < _firstVisibleSlot + _visibleSlotsCount;
}

bool InventoryWindow::canScrollDown() const {
	return _renderEntries.size() - _firstVisibleSlot > _visibleSlotsCount;
}

bool InventoryWindow::canScrollUp() const {
	return _firstVisibleSlot > 0;
}

void InventoryWindow::scrollDown() {
	if (canScrollDown()) {
		_firstVisibleSlot += _visibleSlotsCount;
	}
}

void InventoryWindow::scrollUp() {
	if (canScrollUp()) {
		_firstVisibleSlot -= _visibleSlotsCount;
	}
}

void InventoryWindow::onGameLoop() {
	if (_autoCloseTimeRemaining >= 0 && !_actionMenu->isVisible()) {
		_autoCloseTimeRemaining -= StarkGlobal->getMillisecondsPerGameloop();

		if (_autoCloseTimeRemaining <= 0) {
			_autoCloseTimeRemaining = kAutoCloseSuspended;
			close();
		}
	}
}
} // End of namespace Stark