aboutsummaryrefslogtreecommitdiff
path: root/sword2/icons.cpp
blob: 1182f0e22001727d5769ac3e9085c846522542c3 (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
/* Copyright (C) 1994-1998 Revolution Software Ltd.
 * Copyright (C) 2003-2005 The ScummVM project
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 */

#include "common/stdafx.h"
#include "sword2/sword2.h"
#include "sword2/defs.h"
#include "sword2/interpreter.h"
#include "sword2/logic.h"
#include "sword2/memory.h"
#include "sword2/mouse.h"
#include "sword2/resman.h"

namespace Sword2 {

void Mouse::addMenuObject(MenuObject *obj) {
	assert(_totalTemp < TOTAL_engine_pockets);
	memcpy(&_tempList[_totalTemp], obj, sizeof(MenuObject));
	_totalTemp++;
}

/**
 * Create and start the inventory (bottom) menu
 */

void Mouse::buildMenu() {
	uint32 i, j;

	// Clear the temporary inventory list, since we are going to build a
	// new one from scratch.

	for (i = 0; i < TOTAL_engine_pockets; i++)
		_tempList[i].icon_resource = 0;

	_totalTemp = 0;

	// Run the 'build_menu' script in the 'menu_master' object. This will
	// register all carried menu objects.

	uint32 null_pc = 0;
	char *menuScript = (char *) _vm->_resman->openResource(MENU_MASTER_OBJECT);
	_vm->_logic->runScript(menuScript, menuScript, &null_pc);
	_vm->_resman->closeResource(MENU_MASTER_OBJECT);

	// Create a new master list based on the old master inventory list and
	// the new temporary inventory list. The purpose of all this is, as
	// far as I can tell, that the new list is ordered in the same way as
	// the old list, with new objects added to the end of it.

	// Compare new with old. Anything in master thats not in new gets
	// removed from master - if found in new too, remove from temp

	for (i = 0; i < _totalMasters; i++) {
		bool found_in_temp = false;

		for (j = 0; j < TOTAL_engine_pockets; j++) {
			if (_masterMenuList[i].icon_resource == _tempList[j].icon_resource) {
				// We alread know about this object, so kill it
				// in the temporary list.
				_tempList[j].icon_resource = 0;
				found_in_temp = true;
				break;
			}
		}

		if (!found_in_temp) {
			// The object is in the master list, but not in the
			// temporary list. The player must have lost the object
			// since the last time we checked, so kill it in the
			// master list.
			_masterMenuList[i].icon_resource = 0;
		}
	}

	// Eliminate blank entries from the master list.

	_totalMasters = 0;

	for (i = 0; i < TOTAL_engine_pockets; i++) {
		if (_masterMenuList[i].icon_resource) {
			if (i != _totalMasters) {
				memcpy(&_masterMenuList[_totalMasters], &_masterMenuList[i], sizeof(MenuObject));
				_masterMenuList[i].icon_resource = 0;
			}
			_totalMasters++;
		}
	}

	// Add the new objects - i.e. the ones still in the temporary list but
	// not yet in the master list - to the end of the master.

	for (i = 0; i < TOTAL_engine_pockets; i++) {
		if (_tempList[i].icon_resource) {
			memcpy(&_masterMenuList[_totalMasters++], &_tempList[i], sizeof(MenuObject));
		}
	}

	// Initialise the menu from the master list.

	for (i = 0; i < 15; i++) {
		uint32 res = _masterMenuList[i].icon_resource;
		byte *icon = NULL;

		if (res) {
			bool icon_coloured;

			if (_examiningMenuIcon) {
				// When examining an object, that object is
				// coloured. The rest are greyed out.
				icon_coloured = (res == Logic::_scriptVars[OBJECT_HELD]);
			} else if (Logic::_scriptVars[COMBINE_BASE]) {
				// When combining two menu object (i.e. using
				// one on another), both are coloured. The rest
				// are greyed out.
				icon_coloured = (res == Logic::_scriptVars[OBJECT_HELD] || res == Logic::_scriptVars[COMBINE_BASE]);
			} else {
				// If an object is selected but we are not yet
				// doing anything with it, the selected object
				// is greyed out. The rest are coloured.
				icon_coloured = (res != Logic::_scriptVars[OBJECT_HELD]);
			}

			icon = _vm->_resman->openResource(res) + sizeof(StandardHeader);

			// The coloured icon is stored directly after the
			// greyed out one.

			if (icon_coloured)
				icon += (RDMENU_ICONWIDE * RDMENU_ICONDEEP);
		}

		setMenuIcon(RDMENU_BOTTOM, i, icon);

		if (res)
			_vm->_resman->closeResource(res);
	}

	showMenu(RDMENU_BOTTOM);
}

/**
 * Build a fresh system (top) menu.
 */

void Mouse::buildSystemMenu() {
	uint32 icon_list[5] = {
		OPTIONS_ICON,
		QUIT_ICON,
		SAVE_ICON,
		RESTORE_ICON,
		RESTART_ICON
	};

	// Build them all high in full colour - when one is clicked on all the
	// rest will grey out.

	for (int i = 0; i < ARRAYSIZE(icon_list); i++) {
		byte *icon = _vm->_resman->openResource(icon_list[i]) + sizeof(StandardHeader);
		
		// The only case when an icon is grayed is when the player
		// is dead. Then SAVE is not available.

		if (!Logic::_scriptVars[DEAD] || icon_list[i] != SAVE_ICON)
			icon += (RDMENU_ICONWIDE * RDMENU_ICONDEEP);

		setMenuIcon(RDMENU_TOP, i, icon);
		_vm->_resman->closeResource(icon_list[i]);
	}

	showMenu(RDMENU_TOP);
}

} // End of namespace Sword2