aboutsummaryrefslogtreecommitdiff
path: root/sword1/menu.cpp
blob: bbe993201df3b7792fe0472f3ac5b4305fd249cd (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2003 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 "stdafx.h"
#include "menu.h"
#include "resman.h"
#include "scummsys.h"
#include "common/util.h"
#include "system.h"
#include "mouse.h"
#include "screen.h"
#include "logic.h"

SwordMenuIcon::SwordMenuIcon(uint8 menuType, uint8 menuPos, uint32 resId, uint32 frame, SwordScreen *screen) {
	_menuType = menuType;
	_menuPos = menuPos;
	_resId = resId;
	_frame = frame;
	_screen = screen;
	_selected = false;
}

bool SwordMenuIcon::wasClicked(uint16 mouseX, uint16 mouseY) {
	if (((_menuType == MENU_TOP) && (mouseY >= 40)) || ((_menuType == MENU_BOT) && (mouseY < 440)))
		return false;
	if ((mouseX >= _menuPos * 40) && (mouseX < (_menuPos + 1) * 40))
		return true;
	else
		return false;
}

void SwordMenuIcon::setSelect(bool pSel) {
	_selected = pSel;
}

void SwordMenuIcon::draw(void) {
	uint16 x = _menuPos * 40;
	uint16 y = (_menuType == MENU_TOP)?(0):(440);
	_screen->showFrame(x, y, _resId, _frame + (_selected ? 1 : 0));
}

SwordMenu::SwordMenu(SwordScreen *pScreen, SwordMouse *pMouse) {
	_screen = pScreen;
	_mouse = pMouse;
	_subjectBarShown = false;
	_objectBarShown = false;
	for (uint8 cnt = 0; cnt < TOTAL_subjects; cnt++)
		_subjects[cnt] = NULL;
	for (uint8 cnt = 0; cnt < TOTAL_pockets; cnt++)
		_objects[cnt] = NULL;
	_inMenu = 0;
}

uint8 SwordMenu::checkMenuClick(uint8 menuType) {
	uint16 mouseEvent = _mouse->testEvent();
	if (!mouseEvent)
		return 0;
	uint16 x, y;
	_mouse->giveCoords(&x, &y);
	if (menuType == MENU_BOT) {
		for (uint8 cnt = 0; cnt < SwordLogic::_scriptVars[IN_SUBJECT]; cnt++)
			if (_subjects[cnt]->wasClicked(x, y))
				if (mouseEvent & BS1L_BUTTON_DOWN) {
					SwordLogic::_scriptVars[OBJECT_HELD] = _subjectBar[cnt];
					buildSubjects();
				} else if (mouseEvent & BS1L_BUTTON_UP) {
					if (SwordLogic::_scriptVars[OBJECT_HELD] == _subjectBar[cnt])
						return cnt + 1;
					else {
						SwordLogic::_scriptVars[OBJECT_HELD] = 0;
						buildSubjects();
					}
				}
	} else {
		for (uint8 cnt = 0; cnt < _inMenu; cnt++) {
			if (_objects[cnt]->wasClicked(x, y))
				if (mouseEvent & BS1L_BUTTON_DOWN) {
					SwordLogic::_scriptVars[OBJECT_HELD] = _menuList[cnt];
					buildMenu();
				} else if (mouseEvent & BS1L_BUTTON_UP) {
					if (SwordLogic::_scriptVars[OBJECT_HELD] == _menuList[cnt]) {
						return cnt + 1;
					} else {
						SwordLogic::_scriptVars[OBJECT_HELD] = 0;
						buildMenu();
					}
				}
		}
	}
	return 0;
}

void SwordMenu::buildSubjects(void) {
	_screen->clearMenu(MENU_BOT);
	for (uint8 cnt = 0; cnt < 16; cnt++)
		if (_subjects[cnt]) {
			delete _subjects[cnt];
			_subjects[cnt] = NULL;
		}
	for (uint8 cnt = 0; cnt < SwordLogic::_scriptVars[IN_SUBJECT]; cnt++) {
		uint32 res = _subjectList[(_subjectBar[cnt] & 65535) - BASE_SUBJECT].subjectRes;
		uint32 frame = _subjectList[(_subjectBar[cnt] & 65535) - BASE_SUBJECT].frameNo;
		_subjects[cnt] = new SwordMenuIcon(MENU_BOT, cnt, res, frame, _screen);
		_subjects[cnt]->setSelect(SwordLogic::_scriptVars[OBJECT_HELD] == _subjectBar[cnt]);
		_subjects[cnt]->draw();
	}
}

void SwordMenu::refresh(uint8 menuType) {
	//warning("stub: SwordMenu::refresh())");
}

void SwordMenu::buildMenu(void) {
	uint32 *pockets = SwordLogic::_scriptVars + POCKET_1;
	_inMenu = 0;
	for (uint32 pocketNo = 0; pocketNo < TOTAL_pockets; pocketNo++)
		if (pockets[pocketNo]) {
			_menuList[_inMenu] = pocketNo + 1;
			_inMenu++;
		}
	for (uint32 menuSlot = 0; menuSlot < _inMenu; menuSlot++) {
		_objects[menuSlot] = new SwordMenuIcon(MENU_TOP, menuSlot, _objectDefs[_menuList[menuSlot]].bigIconRes, _objectDefs[_menuList[menuSlot]].bigIconFrame, _screen);
		uint32 objHeld = SwordLogic::_scriptVars[OBJECT_HELD];

		// check highlighting
		if (SwordLogic::_scriptVars[MENU_LOOKING] || _subjectBarShown) { // either we're in the chooser or we're doing a 'LOOK AT'
			if ((!objHeld) || (objHeld == _menuList[menuSlot]))
				_objects[menuSlot]->setSelect(true);
		} else if (_secondItem) { // clicked luggage onto 2nd icon - we need to colour-highlight the 2 relevant icons & grey out the rest
			if ((_menuList[menuSlot] == objHeld) || (_menuList[menuSlot] == _secondItem))
				_objects[menuSlot]->setSelect(true);
		} else { // this object is selected - ie. GREYED OUT
			if (objHeld != _menuList[menuSlot])
				_objects[menuSlot]->setSelect(true);
		}
	}
}

void SwordMenu::showMenu(uint8 menuType) {
	if (menuType == MENU_TOP) {
		for (uint8 cnt = 0; cnt < _inMenu; cnt++)
			_objects[cnt]->draw();
	}
}

void SwordMenu::fnStartMenu(void) {
	SwordLogic::_scriptVars[OBJECT_HELD] = 0;  // icon no longer selected
	SwordLogic::_scriptVars[MENU_LOOKING] = 0; // second icon no longer selected (after using one on another)
	_secondItem = 0;                           // no longer 'looking at' an icon
	buildMenu();
	if (_inMenu > 0) {	// if there's something in the object menu
		_objectBarShown = true;
		showMenu(MENU_TOP);
	} else {
		_objectBarShown = false;
		_inMenu = 0;
	}
}

void SwordMenu::fnEndMenu(void) {
	if (_objectBarShown) {
		for (uint32 cnt = 0; cnt < _inMenu; cnt++)
			delete _objects[cnt];
		_screen->clearMenu(MENU_TOP);
		_screen->clearMenu(MENU_BOT);
		_objectBarShown = false;
		_mouse->setMenuStatus(0);
	}
}

void SwordMenu::fnChooser(BsObject *compact) {
	SwordLogic::_scriptVars[OBJECT_HELD] = 0;
	buildSubjects();
	compact->o_logic = LOGIC_choose;
	_mouse->setMenuStatus(1); // so the mouse cursor will be shown.
	_subjectBarShown = true;
}

void SwordMenu::fnEndChooser(void) {
	SwordLogic::_scriptVars[OBJECT_HELD] = 0;
	_screen->clearMenu(MENU_BOT);
	_screen->clearMenu(MENU_TOP);
	for (uint8 cnt = 0; cnt < 16; cnt++)
		if (_subjects[cnt]) {
			delete _subjects[cnt];
			_subjects[cnt] = NULL;
		}
	_mouse->setMenuStatus(0);
	_subjectBarShown = false;
}

void SwordMenu::checkTopMenu(void) {
	if (_objectBarShown)
		checkMenuClick(MENU_TOP);
}

int SwordMenu::logicChooser(BsObject *compact) {
	if (_objectBarShown)
		uint8 objSelected = checkMenuClick(MENU_TOP);
	if (checkMenuClick(MENU_BOT)) {
		compact->o_logic = LOGIC_script;
		return 1;
	} else 
		return 0;
	return 0;
}

void SwordMenu::fnAddSubject(int32 sub) {
	_subjectBar[SwordLogic::_scriptVars[IN_SUBJECT]] = sub;
	SwordLogic::_scriptVars[IN_SUBJECT]++;
}

void SwordMenu::cfnReleaseMenu(void) {
	_screen->clearMenu(MENU_TOP);
}