File: inventory.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 (371 lines) | stat: -rw-r--r-- 9,312 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* 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 "m4/riddle/gui/inventory.h"
#include "m4/riddle/gui/inventory.h"
#include "m4/riddle/vars.h"
#include "m4/core/errors.h"
#include "m4/graphics/gr_line.h"
#include "m4/graphics/gr_series.h"
#include "m4/gui/gui_vmng_core.h"
#include "m4/gui/gui_vmng_screen.h"

namespace M4 {
namespace Riddle {
namespace GUI {

Inventory::Inventory(const RectClass &r, int32 sprite, int16 cells_h, int16 cells_v, int16 cell_w, int16 cell_h, int16 tag)
	: RectClass(r) {
	_sprite = sprite;

	for (int16 iter = 0; iter < INVENTORY_CELLS_COUNT; iter++) {
		_items[iter]._cell = -1;
		_items[iter]._cursor = -1;
	}

	_num_cells = 0;
	_tag = tag;
	_cells_h = cells_h;
	_cells_v = cells_v;
	_cell_w = cell_w;
	_cell_h = cell_h;

	// If requested cell configuration doesn't fit, blow up.
	if ((cells_h * cell_w > (_x2 - _x1)) || (cells_v * cell_h > (_y2 - _y1))) {
		error_show(FL, 'CGIC');
	}

	_highlight = -1;
	_must_redraw_all = true;
	_must_redraw1 = -1;
	_must_redraw2 = -1;
	_scroll = 0;
	_right_arrow_visible = false;

	_btnScrollLeft = new ButtonClass(RectClass(178, -8, 198, 101), "scroll left", 9, 129, 130, 131, INTERFACE_SPRITES);
	_btnScrollRight = new ButtonClass(RectClass(551, -8, 571, 101), "scroll right", 9, 133, 134, 135, INTERFACE_SPRITES);
	refresh_left_arrow();
	refresh_right_arrow();
}

Inventory::~Inventory() {
	delete _btnScrollLeft;
	delete _btnScrollRight;
}

void Inventory::addToInterfaceBox(InterfaceBox *box) {
	box->add(_btnScrollLeft);
	box->add(_btnScrollRight);
}

bool Inventory::add(const Common::String &name, const Common::String &verb, int32 invSprite, int32 cursor) {
	// Don't add something twice
	int iter;
	for (iter = 0; iter < _num_cells; iter++) {
		if (name.equals(_items[iter]._name))
			return true;
	}

	if (_num_cells >= INVENTORY_CELLS_COUNT) {
		error_show(FL, 'CGIA');
		return false;
	}

	auto &item = _items[_num_cells++];
	item._name = name;
	item._verb = verb;
	item._cell = invSprite;
	item._cursor = cursor;

	_must_redraw_all = true;

	if (INTERFACE_VISIBLE)
		_G(interface).show();

	return true;
}

bool Inventory::need_left() const {
	return (_scroll != 0);
}

bool Inventory::need_right() const {
	if ((_num_cells - _scroll - MAX_INVENTORY) > 0)
		return true;

	return false;
}

void Inventory::set_scroll(int32 new_scroll) {
	_scroll = new_scroll;
	_must_redraw_all = true;
}

void Inventory::toggleHidden() {
	_hidden = !_hidden;
	_must_redraw_all = true;
}

bool Inventory::remove(const Common::String &name) {
	int iter;
	for (iter = 0; iter < _num_cells; iter++) {
		// Found the thing?
		if (name.equals(_items[iter]._name)) {
			// Eat up its slot by moving everything down
			for (; iter < _num_cells; ++iter)
				_items[iter] = _items[iter + 1];

			--_num_cells;
			_must_redraw_all = true;
			_scroll = 0;

			if (INTERFACE_VISIBLE)
				_G(interface).show();

			return true;
		}
	}

	// Didn't find that thing.
	return false;
}

int16 Inventory::inside(int16 x, int16 y) const {
	if ((x < _x1) || (x >= (_x2 - 1)) || (y < _y1 + 2) ||
		(y > _y1 + _cells_v * _cell_h - 2))
		return -1;

	x -= _x1;
	y -= _y1;
	return (int16)((x / _cell_w) * _cells_v + (y / _cell_h));
}

int16 Inventory::cell_pos_x(int16 index) {
	if (_cells_h > _cells_v) {				// Horizontal orientation, fill left to right
		return (int16)((index / _cells_v) * _cell_w);
	} else {								// Vertical orientation, fill top to bottom
		return (int16)((index / _cells_h) * _cell_w);
	}
}

int16 Inventory::cell_pos_y(int16 index) {
	if (_cells_h > _cells_v) {
		// Horizontal orientation, fill left to right
		return (int16)((index % _cells_v) * _cell_h);
	} else {
		// Vertical orientation, fill top to bottom
		return (int16)((index % _cells_h) * _cell_h);
	}
}

void Inventory::highlight_part(int16 index) {
	if (_highlight == index)
		return;

	_must_redraw1 = _highlight;
	_highlight = index;
	_must_redraw2 = _highlight;
}

void Inventory::draw(GrBuff *myBuffer) {
	if (!_must_redraw1 && !_must_redraw2 && !_must_redraw_all)
		return;

	int cell_iter;

	Buffer *myBuff = myBuffer->get_buffer();

	if (_must_redraw_all || _hidden) {
		gr_color_set(__BLACK);
		gr_buffer_rect_fill(myBuff, _x1, _y1, _x2 - _x1, _y2 - _y1);
	}

	if (!_hidden) {
		_right_arrow_visible = false;
		const int X_BORDER = 2, Y_BORDER = 2;

		for (cell_iter = 0; (cell_iter + _scroll < _num_cells) && (cell_iter < MAX_INVENTORY); cell_iter++) {
			int16 left =_x1 + X_BORDER + cell_pos_x(cell_iter);
			int16 top = _y1 + Y_BORDER + cell_pos_y(cell_iter);
			int16 leftOffset = left + _cell_w;
			int16 topOffset = top + _cell_h;

			if (_must_redraw1 == cell_iter || _must_redraw2 == cell_iter || _must_redraw_all) {
				// Update the scroll buttons
				refresh_right_arrow();
				refresh_left_arrow();

				// Draw icon here
				gr_color_set(__BLACK);
				gr_buffer_rect_fill(myBuff, left, top, leftOffset - left, topOffset - top);
				series_show_frame(_sprite, _items[cell_iter + _scroll]._cell,
					myBuff, left - 3, top - 3);

				// Draw box around icon
				if (_highlight == cell_iter) {
					gr_line(left, top, left + _cell_w - 2, top, __LTGRAY, myBuff);
					gr_line(left, top + _cell_h - 2, left + _cell_w - 2, top + _cell_h - 2, __LTGRAY, myBuff);
					gr_line(left, top, left, top + _cell_w - 2, __LTGRAY, myBuff);
					gr_line(left + _cell_w - 2, top, left + _cell_w - 2, top + _cell_h - 2, __LTGRAY, myBuff);
				}
			}
		}
	}

	ScreenContext *iC = vmng_screen_find(_G(gameInterfaceBuff), nullptr);
	RestoreScreensInContext(_x1, _y1, _x2, _y2, iC);
	_must_redraw1 = _must_redraw2 = -1;
	_must_redraw_all = false;

	myBuffer->release();
}

ControlStatus Inventory::track(int32 eventType, int16 x, int16 y) {
	if (!INTERFACE_VISIBLE)
		return NOTHING;

	ControlStatus result = NOTHING;

	int16 over = inside(x, y);
	bool button_clicked = eventType == _ME_L_click || eventType == _ME_L_hold || eventType == _ME_L_drag;

	// If Button is pressed
	if (button_clicked) {
		// If we are not tracking, start tracking
		if (interface_tracking == -1) {
			highlight_part(over);
			interface_tracking = over;
			result = IN_CONTROL;
		} else {
			// Else if we are over something we are tracking
			if (interface_tracking == over) {
				highlight_part(over);
				result = IN_CONTROL;
			} else {
				// Else highlight nothing
				highlight_part(-1);
				result = NOTHING;
			}
		}
	} else {
		// If Button isn't pressed

		// If we unpressed on something we were tracking
		if (interface_tracking == over) {
			if (interface_tracking == -1)
				result = NOTHING;
			else
				result = SELECTED;
		} else {
			if (over + _scroll < _num_cells)
				result = OVER_CONTROL;
			else
				result = NOTHING;
		}

		// Stop tracking anything
		highlight_part(over);
		interface_tracking = -1;
	}

	if (result == NOTHING && button_clicked)
		return TRACKING;

	return result;
}


void Inventory::refresh_right_arrow() {
	if (need_right() || need_left()) {
		_btnScrollRight->unhide();

		if (need_right()) {
			_btnScrollRight->set_sprite_relaxed(133);
			_btnScrollRight->set_sprite_picked(135);
			_btnScrollRight->set_sprite_over(134);
		} else {
			_btnScrollRight->set_sprite_relaxed(136);
			_btnScrollRight->set_sprite_picked(136);
			_btnScrollRight->set_sprite_over(136);
		}
	} else {
		_btnScrollRight->hide();
		_btnScrollLeft->hide();
	}
}

void Inventory::refresh_left_arrow() {
	if (need_right() || need_left()) {
		_btnScrollLeft->unhide();

		if (need_left()) {
			_btnScrollLeft->set_sprite_relaxed(129);
			_btnScrollLeft->set_sprite_picked(131);
			_btnScrollLeft->set_sprite_over(130);
		} else {
			_btnScrollLeft->set_sprite_relaxed(132);
			_btnScrollLeft->set_sprite_picked(132);
			_btnScrollLeft->set_sprite_over(132);
		}
	} else {
		_btnScrollRight->hide();
		_btnScrollLeft->hide();
	}
}

void Inventory::refresh_scrollbars() {
	if (_btnScrollRight->is_hidden())
		refresh_right_arrow();
	else
		_btnScrollRight->hide();

	if (_btnScrollLeft->is_hidden())
		refresh_left_arrow();
	else
		_btnScrollLeft->hide();
}

void Inventory::check_left() {
	if (!_btnScrollLeft->is_hidden()) {
		if (need_left()) {
			_scroll = (_scroll <= 0) ? 0 : _scroll - _cells_v;
		}

		refresh_right_arrow();
		refresh_left_arrow();
		_must_redraw_all = true;
	}
}

void Inventory::check_right() {
	if (!_btnScrollRight->is_hidden()) {
		if (need_right())
			_scroll += _cells_v;

		refresh_right_arrow();
		refresh_left_arrow();
		_must_redraw_all = true;
	}
}

} // namespace GUI
} // namespace Riddle
} // namespace M4