aboutsummaryrefslogtreecommitdiff
path: root/engines/sherlock/tattoo/widget_inventory.cpp
blob: 8ec3bd7dd31cd63c2c61cd09e98132173da06169 (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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/* 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 "sherlock/tattoo/widget_inventory.h"
#include "sherlock/tattoo/tattoo_fixed_text.h"
#include "sherlock/tattoo/tattoo_people.h"
#include "sherlock/tattoo/tattoo_scene.h"
#include "sherlock/tattoo/tattoo_user_interface.h"
#include "sherlock/tattoo/tattoo.h"

namespace Sherlock {

namespace Tattoo {

#define INVENTORY_XSIZE 70			// Width of the box that surrounds inventory items
#define INVENTORY_YSIZE 70			// Height of the box that surrounds inventory items
#define MAX_INV_COMMANDS 10			// Maximum elements in dialog
#define NUM_INV_PER_LINE 4			// Number of inentory items per line in the dialog

WidgetInventoryTooltip::WidgetInventoryTooltip(SherlockEngine *vm, WidgetInventory *owner) : 
		WidgetTooltipBase(vm), _owner(owner) {
}

void WidgetInventoryTooltip::setText(const Common::String &str) {
	// If no text specified, erase any previously displayed tooltip and free it's surface
	if (str.empty()) {
		erase();
		_surface.free();
		return;
	}

	int width = _surface.stringWidth(str) + 2;
	int height = 0;
	Common::String line1 = str, line2;

	// See if we need to split it into two lines
	if (width > 150) {
		// Yes, we do
		const char *s = str.c_str();
		const char *space = nullptr;
		int dif = 10000;

		while (*s) {
			s = strchr(s, ' ');

			if (!s) {
				if (!space) {
					height = _surface.stringHeight(str) + 2;
				} else {
					line1 = Common::String(str.c_str(), space);
					line2 = Common::String(space + 1);
					height = _surface.stringHeight(line1) + _surface.stringHeight(line2) + 4;
				}
				break;
			} else {
				line1 = Common::String(str.c_str(), s);
				line2 = Common::String(s + 1);
				int width1 = _surface.stringWidth(line1);
				int width2 = _surface.stringWidth(line2);

				if (ABS(width1 - width2) < dif) {
					// Found a split point that results in less overall width
					space = s;
					dif = ABS(width1 - width2);
					width = MAX(width1, width2);
				}

				s++;
			}
		}
	} else {
		height = _surface.stringHeight(str) + 2;
	}

	// Allocate a fresh surface for the new string
	_bounds = Common::Rect(width, height);
	_surface.create(width, height);
	_surface.fill(TRANSPARENCY);

	if (line2.empty()) {
		_surface.writeFancyString(str, Common::Point(0, 0), BLACK, INFO_TOP);
	} else {
		int xp, yp;
		
		xp = (_bounds.width() - _surface.stringWidth(line1) - 2) / 2;
		_surface.writeFancyString(line1, Common::Point(xp, 0), BLACK, INFO_TOP);

		xp = (_bounds.width() - _surface.stringWidth(line2) - 2) / 2;
		yp = _surface.stringHeight(line2) + 2;
		_surface.writeFancyString(line2, Common::Point(xp, yp), BLACK, INFO_TOP);
	}
}

void WidgetInventoryTooltip::handleEvents() {	
	Events &events = *_vm->_events;
	FixedText &fixedText = *_vm->_fixedText;
	Inventory &inv = *_vm->_inventory;
	TattooPeople &people = *(TattooPeople *)_vm->_people;
	Scene &scene = *_vm->_scene;
	TattooUserInterface &ui = *(TattooUserInterface *)_vm->_ui;
	Common::Point mousePos = events.mousePos();
	Common::String str;
	int select = -1, oldSelect = 999;
	Common::String strWith = fixedText.getText(kFixedText_With);
	Common::String strUse = fixedText.getText(kFixedText_Use);

	// Register the tooltip for requiring post-rendering drawing, since we draw directly to the screen if a scene
	// mask is active, since the initial draw to the screen will be covered by the mask rendering
	if (ui._mask) {
		ui._postRenderWidgets.push_back(this);
	}

	// If we are using an inventory item on an object in the room, display the appropriate text above the mouse cursor
	if (_owner->_invVerbMode == 3) {
		select = ui._bgFound;
		oldSelect = ui._oldBgFound;
	
		if (select != -1 && (select != oldSelect || (select != -1 && _surface.empty()))) {
			// See if we're pointing at a shape or a sprite
			if (select < 1000) {
				Object &obj = scene._bgShapes[select];

				if (!obj._description.empty() && !obj._description.hasPrefix(" ")) {
					if (_vm->getLanguage() == Common::GR_GRE) {

						if (!_owner->_swapItems)
							str = Common::String::format("%s %s %s %s", _owner->_action.c_str(), obj._description.c_str(), 
								inv[_owner->_invSelect]._name.c_str(), _owner->_verb.c_str());
						else
							str = Common::String::format("%s %s %s %s", _owner->_action.c_str(), inv[_owner->_invSelect]._name.c_str(), 
								obj._description.c_str(), _owner->_verb.c_str());
					} else {
						if (_owner->_swapItems)
							str = Common::String::format("%s %s %s %s", _owner->_verb.c_str(), obj._description.c_str(), _owner->_action.c_str(), 
								inv[_owner->_invSelect]._name.c_str());
						else
							str = Common::String::format("%s %s %s %s", _owner->_verb.c_str(), inv[_owner->_invSelect]._name.c_str(), 
								_owner->_action.c_str(), obj._description.c_str());
					}
				}
			} else {
				Person &person = people[ui._bgFound - 1000];

				if (!person._description.empty() && !person._description.hasPrefix(" ")) {
					if (_vm->getLanguage() == Common::GR_GRE) {
						if (!_owner->_swapItems)
							str = Common::String::format("%s %s %s %s", _owner->_action.c_str(), person._description.c_str(),
								inv[_owner->_invSelect]._name.c_str(), _owner->_verb.c_str());
						else
							str = Common::String::format("%s %s %s %s", _owner->_action.c_str(), inv[_owner->_invSelect]._name.c_str(),
								person._description.c_str(), _owner->_verb.c_str());
					} else {

						if (_owner->_swapItems)
							str = Common::String::format("%s %s %s %s", _owner->_verb.c_str(), person._description.c_str(),
								_owner->_action.c_str(), inv[_owner->_invSelect]._name.c_str());
						else
							str = Common::String::format("%s %s %s %s", _owner->_verb.c_str(),
								inv[_owner->_invSelect]._name.c_str(), _owner->_action.c_str(), person._description.c_str());
					}
				}
			}
		}
	} else {
		const Common::Rect &b = _owner->_bounds;
		Common::Rect r(b.left + 3, b.top + 3, b.right - 3 - BUTTON_SIZE, b.bottom - 3);

		if (r.contains(mousePos)) {
			select = (mousePos.x - r.left) / (INVENTORY_XSIZE + 3) + NUM_INVENTORY_SHOWN / 2 *
				((mousePos.y - r.top) / (INVENTORY_YSIZE + 3)) + inv._invIndex;

			if (select >= inv._holdings) {
				select = -1;
			} else {
				oldSelect = _owner->_invSelect;
				
				if (select != _owner->_invSelect || _surface.empty()) {

					if (_owner->_invMode == 1) {
						// See if we were pointing at a shapre or sprite
						if (ui._activeObj < 1000) {
							Object &obj = scene._bgShapes[ui._activeObj];

							if (!obj._description.empty() && !obj._description.hasPrefix(" "))
								str = Common::String::format("%s %s %s %s", strUse.c_str(), inv[select]._name.c_str(),
									strWith.c_str(), obj._description.c_str());
						} else {
							Person &person = people[ui._activeObj - 1000];

							if (!person._description.empty() && !person._description.hasPrefix(" "))
								str = Common::String::format("%s %s %s %s", strUse.c_str(), inv[select]._name.c_str(),
									strWith.c_str(), person._description.c_str());
						}
					} else {
						if (_owner->_invVerbMode == 2)
							str = Common::String::format("%s %s %s %s", strUse.c_str(), inv[_owner->_invSelect]._name.c_str(),
								strWith.c_str(), inv[select]._name.c_str());
						else
							str = inv[select]._description.c_str();
					}
				}
			}
		}
	}

	// See if they are pointing at a different inventory object and we need to
	// change the graphics of the Text Tag
	if (select != oldSelect || (select != -1 && _surface.empty())) {
		// Set the text
		setText(str);

		if (_owner->_invVerbMode != 3)
			_owner->_invSelect = select;
		else
			ui._oldBgFound = select;
	} else if (select == -1 && oldSelect != -1) {
		setText(Common::String());
		return;
	}

	if (_owner->_invVerbMode == 3)
		// Adjust tooltip to be above the inventory item being shown above the standard cursor
		mousePos.y -= events._hotspotPos.y;

	// Update the position of the tooltip
	int xs = CLIP(mousePos.x - _bounds.width() / 2, 0, SHERLOCK_SCENE_WIDTH - _bounds.width());
	int ys = CLIP(mousePos.y - _bounds.height(), 0, SHERLOCK_SCREEN_HEIGHT - _bounds.height());
	_bounds.moveTo(xs, ys);
}

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

WidgetInventoryVerbs::WidgetInventoryVerbs(SherlockEngine *vm, WidgetInventory *owner) : 
		WidgetBase(vm), _owner(owner) {
	_invVerbSelect = _oldInvVerbSelect = -1;
}

void WidgetInventoryVerbs::load() {
	Events &events = *_vm->_events;
	Inventory &inv = *_vm->_inventory;
	People &people = *_vm->_people;
	Scene &scene = *_vm->_scene;
	TattooUserInterface &ui = *(TattooUserInterface *)_vm->_ui;
	Common::Point mousePos = events.mousePos();

	// Make the Verb List for this Inventory Item
	_inventCommands.clear();
	_inventCommands.push_back(FIXED(Look));

	// Default the Action word to "with"
	_owner->_action = _vm->getLanguage() == Common::GR_GRE ? "" : FIXED(With);

	// Search all the bgshapes for any matching Target Fields
	for (uint idx = 0; idx < scene._bgShapes.size(); ++idx) {
		Object &obj = scene._bgShapes[idx];

		if (obj._type != INVALID && obj._type != HIDDEN) {
			for (int useNum = 0; useNum < 6; ++useNum) {
				if (!obj._use[useNum]._verb.hasPrefix("*") &&
					!obj._use[useNum]._target.compareToIgnoreCase(inv[_owner->_invSelect]._name)) {
					// Make sure the Verb is not already in the list
					bool found1 = false;
					for (uint cmdNum = 0; cmdNum < _inventCommands.size() && !found1; ++cmdNum) {
						if (!_inventCommands[cmdNum].compareToIgnoreCase(obj._use[useNum]._verb))
							found1 = true;
					}

					if (!found1) {
						_inventCommands.push_back(obj._use[useNum]._verb);

						// Check for any Special Action commands
						for (int nameNum = 0; nameNum < 4; ++nameNum) {
							if (!scumm_strnicmp(obj._use[useNum]._names[nameNum].c_str(), "*V", 2)) {
								if (!scumm_strnicmp(obj._use[useNum]._names[nameNum].c_str(), "*VSWAP", 6))
									_owner->_swapItems = true;
								else
									_owner->_action = Common::String(obj._use[useNum]._names[nameNum].c_str() + 2);
							}
						}
					}
				}
			}
		}
	}

	// Search the NPCs for matches as well
	for (int idx = 1; idx < MAX_CHARACTERS; ++idx) {
		for (int useNum = 0; useNum < 2; ++useNum) {
			if (!people[idx]._use[useNum]._target.compareToIgnoreCase(inv[_owner->_invSelect]._name) &&
				!people[idx]._use[useNum]._verb.empty() && !people[idx]._use[useNum]._verb.hasPrefix(" ")) {
				bool found1 = false;
				for (uint cmdNum = 0; cmdNum < _inventCommands.size() && !found1; ++cmdNum) {
					if (!_inventCommands[cmdNum].compareToIgnoreCase(people[idx]._use[cmdNum]._verb))
						found1 = true;
				}

				if (!found1)
					_inventCommands.push_back(people[idx]._use[useNum]._verb);
			}
		}
	}

	// Finally see if the item itself has a verb
	if (!inv[_owner->_invSelect]._verb._verb.empty()) {
		// Don't add "Solve" to the Foolscap if it's already been "Solved"
		if (inv[_owner->_invSelect]._verb._verb.compareToIgnoreCase(FIXED(Solve)) || !_vm->readFlags(299))
			_inventCommands.push_back(inv[_owner->_invSelect]._verb._verb);
	}

	// Now find the widest command in the _inventCommands array
	int width = 0;
	for (uint idx = 0; idx < _inventCommands.size(); ++idx)
		width = MAX(width, _surface.stringWidth(_inventCommands[idx]));

	// Set up bounds for the menu
	_bounds = Common::Rect(width + _surface.widestChar() * 2 + 6,
		(_surface.fontHeight() + 7) * _inventCommands.size() + 3);
	_bounds.moveTo(mousePos.x - _bounds.width() / 2, mousePos.y - _bounds.height() / 2);

	// Create the surface
	_surface.create(_bounds.width(), _bounds.height());
	_surface.fill(TRANSPARENCY);
	makeInfoArea();

	// Draw the Verb commands and the lines separating them
	ImageFile &images = *ui._interfaceImages;
	for (int idx = 0; idx < (int)_inventCommands.size(); ++idx) {
		_surface.writeString(_inventCommands[idx], Common::Point((_bounds.width() -
			_surface.stringWidth(_inventCommands[idx])) / 2, (_surface.fontHeight() + 7) * idx + 5), INFO_TOP);

		if (idx < (int)_inventCommands.size() - 1) {
			_surface.vLine(3, (_surface.fontHeight() + 7) * (idx + 1), _bounds.right - 4, INFO_TOP);
			_surface.vLine(3, (_surface.fontHeight() + 7) * (idx + 1) + 1, _bounds.right - 4, INFO_MIDDLE);
			_surface.vLine(3, (_surface.fontHeight() + 7) * (idx + 1) + 2, _bounds.right - 4, INFO_BOTTOM);

			_surface.transBlitFrom(images[4], Common::Point(0, (_surface.fontHeight() + 7) * (idx + 1)));
			_surface.transBlitFrom(images[5], Common::Point(_bounds.width() - images[5]._width,
				(_surface.fontHeight() + 7) * (idx + 1) - 1));
		}
	}

	summonWindow();
}

void WidgetInventoryVerbs::handleEvents() {
	Events &events = *_vm->_events;
	Inventory &inv = *_vm->_inventory;
	TattooScene &scene = *(TattooScene *)_vm->_scene;
	Common::Point mousePos = events.mousePos();
	TattooUserInterface &ui = *(TattooUserInterface *)_vm->_ui;
	TattooEngine &vm = *(TattooEngine *)_vm;

	// Handle changing highlighted verb entry
	highlightControls();

	// See if they want to close the menu (by clicking outside the menu)
	Common::Rect innerBounds = _bounds;
	innerBounds.grow(-3);

	// Flag is they started pressing outside of the menu
	if (events._firstPress && !_bounds.contains(mousePos))
		_outsideMenu = true;

	if (events._released || events._rightReleased || ui._keyState.keycode == Common::KEYCODE_ESCAPE) {
		ui._scrollHighlight = SH_NONE;
		banishWindow();

		if ((_outsideMenu && !innerBounds.contains(mousePos)) || ui._keyState.keycode == Common::KEYCODE_ESCAPE) {
			_owner->_invVerbMode = 0;
		} else if (innerBounds.contains(mousePos)) {
			_outsideMenu = false;

			// Check if they are trying to solve the Foolscap puzzle, or looking at the completed puzzle
			bool doHangman = !inv[_owner->_invSelect]._name.compareToIgnoreCase(FIXED(Inv6)) &&
				!_inventCommands[_invVerbSelect].compareToIgnoreCase(FIXED(Solve));
			doHangman |= (!inv[_owner->_invSelect]._name.compareToIgnoreCase(FIXED(Inv6)) || !inv[_owner->_invSelect]._name.compareToIgnoreCase(FIXED(Inv7)))
				&& _inventCommands[_invVerbSelect].compareToIgnoreCase(FIXED(Look)) && vm.readFlags(299);

			if (doHangman) {
				// Close the entire Inventory and return to Standard Mode
				_owner->_invVerbMode = 0;

				_owner->_tooltipWidget.banishWindow();
				inv.freeInv();

				events.clearEvents();
				events.setCursor(ARROW);
				ui._menuMode = scene._labTableScene ? LAB_MODE : STD_MODE;

				scene.doBgAnim();
				vm.doHangManPuzzle();
			} else if (_invVerbSelect == 0) {
				// They have released the mouse on the Look Verb command, so Look at the inventory item
				ui._invLookFlag = true;
				inv.freeInv();
				ui._windowOpen = false;
				ui._lookPos = mousePos;
				ui.printObjectDesc(inv[_owner->_invSelect]._examine, true);
			} else {
				_owner->_invVerbMode = 3;
				ui._oldBgFound = -1;

				// See if the selected Verb with the selected Iventory Item, is to be used by itself
				if (!_inventCommands[_invVerbSelect].compareToIgnoreCase(inv[_owner->_invSelect]._verb._verb) ||
					!inv[_owner->_invSelect]._verb._target.compareToIgnoreCase("*SELF")) {
					inv.freeInv();

					ui._menuMode = scene._labTableScene ? LAB_MODE : STD_MODE;
					events.clearEvents();
					ui.checkAction(inv[_owner->_invSelect]._verb, 2000);
				} else {
					_owner->_verb = _inventCommands[_invVerbSelect];
				}

				// If we are still in Inventory Mode, setup the graphic to float in front of the mouse cursor
				if (ui._menuMode == INV_MODE) {
					// Add the inventory item to the cursor
					ImageFrame &imgFrame = (*inv._invShapes[_owner->_invSelect - inv._invIndex])[0];
					events.setCursor(ARROW, Common::Point(-100, imgFrame._height), imgFrame._frame);

					// Close the inventory dialog without banishing it, so it can keep getting events
					// to handle tooltips and actually making the selection of what object to use them item on
					inv.freeInv();
					_owner->_surface.free();
				}
			}
		}
	}
}

void WidgetInventoryVerbs::highlightControls() {
	Events &events = *_vm->_events;
	Common::Point mousePos = events.mousePos();

	Common::Rect innerBounds = _bounds;
	innerBounds.grow(-3);

	// Set the highlighted verb
	_invVerbSelect = -1;
	if (innerBounds.contains(mousePos))
		_invVerbSelect = (mousePos.y - _bounds.top - 3) / (_surface.fontHeight() + 7);

	// See if the highlighted verb has changed
	if (_invVerbSelect != _oldInvVerbSelect) {
		// Draw the list again, with the new highlighting
		for (int idx = 0; idx < (int)_inventCommands.size(); ++idx) {
			byte color = (idx == _invVerbSelect) ? COMMAND_HIGHLIGHTED : INFO_TOP;
			_surface.writeString(_inventCommands[idx], Common::Point(
				(_bounds.width() - _surface.stringWidth(_inventCommands[idx])) / 2,
				(_surface.fontHeight() + 7) * idx + 5), color);
		}

		_oldInvVerbSelect = _invVerbSelect;
	}
}

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

WidgetInventory::WidgetInventory(SherlockEngine *vm) : WidgetBase(vm), 
		_tooltipWidget(vm, this), _verbList(vm, this) {
	_invMode = 0;
	_invVerbMode = 0;
	_invSelect = _oldInvSelect = -1;
	_selector = _oldSelector = -1;
	_swapItems = false;
}

void WidgetInventory::load(int mode) {
	Events &events = *_vm->_events;
	Inventory &inv = *_vm->_inventory;
	Screen &screen = *_vm->_screen;
	Common::Point mousePos = events.mousePos();

	if (mode == 3) {
		mode = 2;
		mousePos = Common::Point(screen._currentScroll.x + SHERLOCK_SCREEN_WIDTH / 2, SHERLOCK_SCREEN_HEIGHT / 2);
	}

	if (mode != 0)
		_invMode = mode;
	_invVerbMode = 0;
	_invSelect = _oldInvSelect = -1;
	_selector = _oldSelector = -1;
	_scroll = true;

	if (mode == 0) {
		banishWindow();
	} else {
		_bounds = Common::Rect((INVENTORY_XSIZE + 3) * NUM_INVENTORY_SHOWN / 2 + BUTTON_SIZE + 6,
			(INVENTORY_YSIZE + 3) * 2 + 3);
		_bounds.moveTo(mousePos.x - _bounds.width() / 2, mousePos.y - _bounds.height() / 2);
	}

	// Ensure menu will be on-screen
	restrictToScreen();

	// Load the inventory data
	inv.loadInv();

	// Redraw the inventory menu on the widget surface
	_surface.create(_bounds.width(), _bounds.height());
	_surface.fill(TRANSPARENCY);

	// Draw the window background and then the inventory on top of it
	makeInfoArea(_surface);
	drawBars();
	drawInventory();
}

void WidgetInventory::drawBars() {
	TattooUserInterface &ui = *(TattooUserInterface *)_vm->_ui;
	ImageFile &images = *ui._interfaceImages;
	int x;

	_surface.hLine(3, INVENTORY_YSIZE + 3, _bounds.width() - 4, INFO_TOP);
	_surface.hLine(3, INVENTORY_YSIZE + 4, _bounds.width() - 4, INFO_MIDDLE);
	_surface.hLine(3, INVENTORY_YSIZE + 5, _bounds.width() - 4, INFO_BOTTOM);
	_surface.transBlitFrom(images[4], Common::Point(0, INVENTORY_YSIZE + 2));

	for (int idx = 1; idx <= NUM_INVENTORY_SHOWN / 2; ++idx) {
		x = idx * (INVENTORY_XSIZE + 3);

		_surface.vLine(x, 3, _bounds.height() - 4, INFO_TOP);
		_surface.vLine(x + 1, 3, _bounds.height() - 4, INFO_MIDDLE);
		_surface.vLine(x + 2, 3, _bounds.height() - 4, INFO_BOTTOM);

		_surface.transBlitFrom(images[6], Common::Point(x - 1, 1));
		_surface.transBlitFrom(images[7], Common::Point(x - 1, _bounds.height() - 4));
		_surface.transBlitFrom(images[6], Common::Point(x - 1, INVENTORY_YSIZE + 5));
		_surface.transBlitFrom(images[7], Common::Point(x - 1, INVENTORY_YSIZE + 2));
	}

	_surface.hLine(x + 2, INVENTORY_YSIZE + 2, INVENTORY_YSIZE + 8, INFO_BOTTOM);
}

void WidgetInventory::drawInventory() {
	Inventory &inv = *_vm->_inventory;

	// TODO: Refactor _invIndex into this widget class
	for (int idx = 0, itemId = inv._invIndex; idx < NUM_INVENTORY_SHOWN; ++idx, ++itemId) {
		// Figure out the drawing position
		Common::Point pt(3 + (INVENTORY_XSIZE + 3) * (idx % (NUM_INVENTORY_SHOWN / 2)),
			3 + (INVENTORY_YSIZE + 3) * (idx / (NUM_INVENTORY_SHOWN / 2)));

		// Draw the box to serve as the background for the item
		_surface.hLine(pt.x + 1, pt.y, pt.x + INVENTORY_XSIZE - 2, TRANSPARENCY);
		_surface.fillRect(Common::Rect(pt.x, pt.y + 1, pt.x + INVENTORY_XSIZE, pt.y + INVENTORY_YSIZE - 1), TRANSPARENCY);
		_surface.hLine(pt.x + 1, pt.y + INVENTORY_YSIZE - 1, pt.x + INVENTORY_XSIZE - 2, TRANSPARENCY);

		// Draw the item
		if (itemId < inv._holdings) {
			ImageFrame &img = (*inv._invShapes[idx])[0];
			_surface.transBlitFrom(img, Common::Point(pt.x + (INVENTORY_XSIZE - img._width) / 2,
				pt.y + (INVENTORY_YSIZE - img._height) / 2));
		}
	}

	drawScrollBar(inv._invIndex / NUM_INV_PER_LINE, NUM_INVENTORY_SHOWN / NUM_INV_PER_LINE, 
		(inv._holdings + NUM_INV_PER_LINE - 1) / NUM_INV_PER_LINE);
}

void WidgetInventory::handleEvents() {
	TattooEngine &vm = *(TattooEngine *)_vm;
	Events &events = *_vm->_events;
	Inventory &inv = *_vm->_inventory;
	People &people = *_vm->_people;
	TattooScene &scene = *(TattooScene *)_vm->_scene;
	TattooUserInterface &ui = *(TattooUserInterface *)_vm->_ui;
	Common::Point mousePos = events.mousePos();

	if (_invVerbMode == 1) {
		checkTabbingKeys(MAX_INV_COMMANDS);
	} else if (_invVerbMode == 0) {
		checkInvTabbingKeys();

		// Handle scrollbar events
		int oldScrollIndex = inv._invIndex / NUM_INV_PER_LINE;
		int invIndex = inv._invIndex / NUM_INV_PER_LINE;

		ScrollHighlight oldHighlight = ui._scrollHighlight;
		handleScrollbarEvents(invIndex, NUM_INVENTORY_SHOWN / NUM_INV_PER_LINE, 
			(inv._holdings + NUM_INV_PER_LINE - 1) / NUM_INV_PER_LINE);

		handleScrolling(invIndex, NUM_INVENTORY_SHOWN / NUM_INV_PER_LINE, 
			(inv._holdings + NUM_INV_PER_LINE - 1) / NUM_INV_PER_LINE);

		if (oldScrollIndex != invIndex) {
			// Starting visible item index has changed, so set the index and reload inventory graphics
			inv._invIndex = invIndex * NUM_INV_PER_LINE;
			inv.freeGraphics();
			inv.loadGraphics();
		}

		if (ui._scrollHighlight != oldHighlight || oldScrollIndex != invIndex) {
			drawInventory();
			return;
		}
	}

	if (_invVerbMode != 1)
		_tooltipWidget.handleEvents();

	// Flag is they started pressing outside of the menu
	if (events._firstPress && !_bounds.contains(mousePos))
		_outsideMenu = true;

	if (_invVerbMode != 3)
		highlightControls();

	// See if they released a mouse button button
	if (events._released || events._rightReleased || ui._keyState.keycode == Common::KEYCODE_ESCAPE) {
		ui._scrollHighlight = SH_NONE;

		// See if they have a Verb List open for an Inventry Item
		if (_invVerbMode == 1)
			return;

		if (_invVerbMode == 3) {
			// Selecting object after inventory verb has been selected
			_tooltipWidget.banishWindow();
			close();

			if (ui._keyState.keycode != Common::KEYCODE_ESCAPE) {
				// If user pointed at an item, use the selected inventory item with this item
				bool found = false;
				if (ui._bgFound != -1) {
					if (ui._personFound) {
						for (int idx = 0; idx < 2; ++idx) {
							if (!people[ui._bgFound - 1000]._use[idx]._verb.compareToIgnoreCase(_verb) &&
								!people[ui._bgFound - 1000]._use[idx]._target.compareToIgnoreCase(_invTarget)) {
								ui.checkAction(people[ui._bgFound - 1000]._use[idx], ui._bgFound);
								found = true;
							}
						}
					} else {
						for (int idx = 0; idx < 6; ++idx) {
							if (!ui._bgShape->_use[idx]._verb.compareToIgnoreCase(_verb) &&
									!ui._bgShape->_use[idx]._target.compareToIgnoreCase(_invTarget)) {
								ui.checkAction(ui._bgShape->_use[idx], ui._bgFound);
								found = true;
							}
						}
					}
				}

				if (!found)
					ui.putMessage("%s", FIXED(NoEffect));
			}
		} else if ((_outsideMenu && !_bounds.contains(mousePos)) || ui._keyState.keycode == Common::KEYCODE_ESCAPE) {
			// Want to close the window (clicked outside of it). So close the window and return to Standard 
			close();

		} else if (_bounds.contains(mousePos)) {
			// Mouse button was released inside the inventory window
			_outsideMenu = false;
		
			//  See if they are pointing at one of the inventory items
			if (_invSelect != -1) {
				// See if they are in Use Obj with Inv. Mode (they right clicked on an item 
				// in the room and selected "Use with Inv.")
				if (_invMode == 1) {
					_tooltipWidget.banishWindow();
					banishWindow();

					// See if the item in the room that they started with was a person
					bool found = false;
					if (ui._activeObj >= 1000) {
						// Object was a person, activate anything in his two verb fields
						for (int idx = 0; idx < 2; ++idx) {
							if (!people[ui._activeObj - 1000]._use[idx]._target.compareToIgnoreCase(inv[_invSelect]._name)) {
								ui.checkAction(people[ui._activeObj - 1000]._use[idx], ui._activeObj);
								found = true;
							}
						}
					} else {
						// Object was a regular object, activate anything in its verb fields
						for (int idx = 0; idx < 6; ++idx) {
							if (!scene._bgShapes[ui._activeObj]._use[idx]._target.compareToIgnoreCase(inv[_invSelect]._name)) {
								ui.checkAction(scene._bgShapes[ui._activeObj]._use[idx], ui._activeObj);
								found = true;
							}
						}
					}
					if (!found)
						ui.putMessage("%s", FIXED(NoEffect));

				} else {
					// See if they right clicked on an item
					if (events._rightReleased) {
						_invVerbMode = 1;
						_verbList._oldInvVerbSelect = -1;
						_tooltipWidget.banishWindow();

						// Keep track of the name of the inventory object so we can check it against the target fields 
						// of verbs when we activate it
						_invTarget = inv[_invSelect]._name;
						_swapItems = false;

						_verbList.load();
					} else {		
						// They left clicked on an inventory item, so Look at it

						// Check if they are looking at the solved Foolscap
						if ((!inv[_invSelect]._name.compareToIgnoreCase(FIXED(Inv6)) || !inv[_invSelect]._name.compareToIgnoreCase(FIXED(Inv7)))
								&& vm.readFlags(299)) {
							banishWindow();
							_tooltipWidget.erase();

							_invVerbMode = 0;
							inv.freeInv();
							
							events.clearEvents();
							events.setCursor(ARROW);
							ui._menuMode = scene._labTableScene ? LAB_MODE : STD_MODE;

							scene.doBgAnim();
							vm.doHangManPuzzle();
						} else {
							ui._invLookFlag = true;
							inv.freeInv();

							_tooltipWidget.banishWindow();
							ui._windowOpen = false;
							ui._lookPos = mousePos;
							ui.printObjectDesc(inv[_invSelect]._examine, true);
						}
					}
				}
			}
		}
	}
}

void WidgetInventory::checkInvTabbingKeys() {
}

void WidgetInventory::highlightControls() {
	// TODO
}

void WidgetInventory::banishWindow() {
	WidgetBase::banishWindow();

	_verbList.banishWindow();
}

void WidgetInventory::draw() {
	WidgetBase::draw();
	_tooltipWidget.draw();
}

void WidgetInventory::erase() {
	WidgetBase::erase();
	_tooltipWidget.erase();
}

void WidgetInventory::close() {
	Events &events = *_vm->_events;
	Inventory &inv = *_vm->_inventory;
	TattooScene &scene = *(TattooScene *)_vm->_scene;
	TattooUserInterface &ui = *(TattooUserInterface *)_vm->_ui;

	banishWindow();
	inv.freeInv();
	events.clearEvents();

	events.setCursor(ARROW);
	ui._menuMode = scene._labTableScene ? LAB_MODE : STD_MODE;
}

} // End of namespace Tattoo

} // End of namespace Sherlock