aboutsummaryrefslogtreecommitdiff
path: root/engines/agi/menu.cpp
blob: 950b054cb3fb06918c360d29b4561979e830dddf (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2006 The ScummVM project
 *
 * Copyright (C) 1999-2002 Sarien Team
 *
 * 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "common/stdafx.h"

#include "agi/agi.h"
#include "agi/sprite.h"
#include "agi/graphics.h"
#include "agi/keyboard.h"
#include "agi/menu.h"
#include "agi/text.h"
#include "agi/list.h"

namespace Agi {

struct agi_menu {
	struct list_head list;		/**< list head for menubar list */
	struct list_head down;		/**< list head for menu options */
	int index;			/**< number of menu in menubar */
	int width;			/**< width of menu in characters */
	int height;			/**< height of menu in characters */
	int col;			/**< column of menubar entry */
	int wincol;			/**< column of menu window */
	char *text;			/**< menu name */
};

struct agi_menu_option {
	struct list_head list;		/**< list head for menu options */
	int enabled;			/**< option is enabled or disabled */
	int event;			/**< menu event */
	int index;			/**< number of option in this menu */
	char *text;			/**< text of menu option */
};

static LIST_HEAD(menubar);

static int h_cur_menu;
static int v_cur_menu;

static struct agi_menu *get_menu(int i) {
	struct list_head *h;
	struct agi_menu *m;

	list_for_each(h, &menubar, next) {
		m = list_entry(h, struct agi_menu, list);
		if (m->index == i)
			return m;
	}
	return NULL;
}

static struct agi_menu_option *get_menu_option(int i, int j) {
	struct list_head *h;
	struct agi_menu *m;
	struct agi_menu_option *d;

	m = get_menu(i);

	list_for_each(h, &m->down, next) {
		d = list_entry(h, struct agi_menu_option, list);
		if (d->index == j)
			return d;
	}

	return NULL;
}

static void draw_menu_bar() {
	struct list_head *h;
	struct agi_menu *m;

	clear_lines(0, 0, MENU_BG);
	flush_lines(0, 0);

	list_for_each(h, &menubar, next) {
		m = list_entry(h, struct agi_menu, list);
		print_text(m->text, 0, m->col, 0, 40, MENU_FG, MENU_BG);
	}

}

static void draw_menu_hilite(int cur_menu) {
	struct agi_menu *m;

	m = get_menu(cur_menu);
	debugC(6, kDebugLevelMenu, "[%s]", m->text);
	print_text(m->text, 0, m->col, 0, 40, MENU_BG, MENU_FG);
	flush_lines(0, 0);
}

/* draw box and pulldowns. */
static void draw_menu_option(int h_menu) {
	struct list_head *h;
	struct agi_menu *m = NULL;
	struct agi_menu_option *d = NULL;

	/* find which vertical menu it is */
	m = get_menu(h_menu);

	draw_box(m->wincol * CHAR_COLS, 1 * CHAR_LINES, (m->wincol + m->width + 2) * CHAR_COLS,
			(1 + m->height + 2) * CHAR_LINES, MENU_BG, MENU_LINE, 0);

	list_for_each(h, &m->down, next) {
		d = list_entry(h, struct agi_menu_option, list);
		print_text(d->text, 0, m->wincol + 1, d->index + 2, m->width + 2,
				d->enabled ? MENU_FG : MENU_DISABLED, MENU_BG);
	}
}

static void draw_menu_option_hilite(int h_menu, int v_menu) {
	struct agi_menu *m;
	struct agi_menu_option *d;

	m = get_menu(h_menu);
	d = get_menu_option(h_menu, v_menu);

	print_text(d->text, 0, m->wincol + 1, v_menu + 2, m->width + 2,
			MENU_BG, d->enabled ? MENU_FG : MENU_DISABLED);
}

static void new_menu_selected(int i) {
	show_pic();
	draw_menu_bar();
	draw_menu_hilite(i);
	draw_menu_option(i);
}

#ifdef USE_MOUSE
static int mouse_over_text(unsigned int line, unsigned int col, char *s) {
	if (mouse.x < col * CHAR_COLS)
		return false;

	if (mouse.x > (col + strlen(s)) * CHAR_COLS)
		return false;

	if (mouse.y < line * CHAR_LINES)
		return false;

	if (mouse.y >= (line + 1) * CHAR_LINES)
		return false;

	return true;
}
#endif

static int h_index;
static int v_index;
static int h_col;
static int h_max_menu;
static int v_max_menu[10];

#if 0
static void add_about_option() {
	struct agi_menu *m;
	struct agi_menu_option *d;
	char text[] = "About AGI engine";

	d = malloc(sizeof(struct agi_menu_option));
	d->text = strdup(text);
	d->enabled = true;
	d->event = 255;
	d->index = (v_max_menu[0] += 1);

	m = list_entry(menubar.next, struct agi_menu, list);
	list_add_tail(&d->list, &m->down);
	m->height++;
	if (m->width < strlen(text))
		m->width = strlen(text);
}
#endif

/*
 * Public functions
 */

void menu_init() {
	h_index = 0;
	h_col = 1;
	h_cur_menu = 0;
	v_cur_menu = 0;
}

void menu_deinit() {
	struct list_head *h, *h2, *v, *v2;
	struct agi_menu *m = NULL;
	struct agi_menu_option *d = NULL;

	for (h = (&menubar)->prev; h != (&menubar); h = h2) {
		m = list_entry(h, struct agi_menu, list);
		h2 = h->prev;
		debugC(3, kDebugLevelMenu, "deiniting hmenu %s", m->text);
		for (v = (&m->down)->prev; v != (&m->down); v = v2) {
			d = list_entry(v, struct agi_menu_option, list);
			v2 = v->prev;
			debugC(3, kDebugLevelMenu, "  deiniting vmenu %s", d->text);
			list_del(v);
			free(d->text);
			free(d);
		}
		list_del(h);
		free(m->text);
		free(m);
	}
}

void menu_add(char *s) {
	struct agi_menu *m;

	m = (agi_menu *) malloc(sizeof(struct agi_menu));
	m->text = strdup(s);
	while (m->text[strlen(m->text) - 1] == ' ')
		m->text[strlen(m->text) - 1] = 0;
	m->down.next = &m->down;
	m->down.prev = &m->down;
	m->width = 0;
	m->height = 0;
	m->index = h_index++;
	m->col = h_col;
	m->wincol = h_col - 1;
	v_index = 0;
	v_max_menu[m->index] = 0;
	h_col += strlen(m->text) + 1;
	h_max_menu = m->index;

	debugC(3, kDebugLevelMenu, "add menu: '%s' %02x", s, m->text[strlen(m->text)]);
	list_add_tail(&m->list, &menubar);
}

void menu_add_item(char *s, int code) {
	struct agi_menu *m;
	struct agi_menu_option *d;
	int l;

	d = (agi_menu_option *) malloc(sizeof(struct agi_menu_option));
	d->text = strdup(s);
	d->enabled = true;
	d->event = code;
	d->index = v_index++;

	m = list_entry(menubar.prev, struct agi_menu, list);
	m->height++;

	v_max_menu[m->index] = d->index;

	l = strlen(d->text);
	if (l > 40)
		l = 38;
	if (m->wincol + l > 38)
		m->wincol = 38 - l;
	if (l > m->width)
		m->width = l;

	debugC(3, kDebugLevelMenu, "Adding menu item: %s (size = %d)", s, m->height);
	list_add_tail(&d->list, &m->down);
}

void menu_submit() {
	struct list_head *h, *h2;
	struct agi_menu *m = NULL;

	debugC(3, kDebugLevelMenu, "Submitting menu");

	/* add_about_option (); */

	/* If a menu has no options, delete it */
	for (h = (&menubar)->prev; h != (&menubar); h = h2) {
		m = list_entry(h, struct agi_menu, list);
		h2 = h->prev;
		if ((&m->down)->prev == (&m->down)) {
			list_del(h);
			free(m->text);
			free(m);
			h_max_menu--;
		}
	}
}

int menu_keyhandler(int key) {
	static int clock_val;
	static int menu_active = false;
	struct agi_menu_option *d;
	struct list_head *h;
	struct agi_menu *m;
	static int button_used = 0;

	if (!getflag(F_menus_work))
		return false;

	if (!menu_active) {
		clock_val = game.clock_enabled;
		game.clock_enabled = false;
		draw_menu_bar();
	}
#ifdef USE_MOUSE
	/*
	 * Mouse handling
	 */
	if (mouse.button) {
		int hmenu, vmenu;

		button_used = 1;	/* Button has been used at least once */
		if (mouse.y <= CHAR_LINES) {
			/* on the menubar */
			hmenu = 0;

			list_for_each(h, &menubar, next) {
				m = list_entry(h, struct agi_menu, list);
				if (mouse_over_text(0, m->col, m->text)) {
					break;
				} else {
					hmenu++;
				}
			}

			if (hmenu <= h_max_menu) {
				if (h_cur_menu != hmenu) {
					v_cur_menu = -1;
					new_menu_selected(hmenu);
				}
				h_cur_menu = hmenu;
			}
		} else {
			/* not in menubar */
			struct agi_menu_option *do1;

			vmenu = 0;

			m = get_menu(h_cur_menu);
			list_for_each(h, &m->down, next) {
				do1 = list_entry(h, struct agi_menu_option, list);
				if (mouse_over_text(2 + do1->index, m->wincol + 1, do1->text)) {
					break;
				} else {
					vmenu++;
				}
			}

			if (vmenu <= v_max_menu[h_cur_menu]) {
				if (v_cur_menu != vmenu) {
					draw_menu_option(h_cur_menu);
					draw_menu_option_hilite(h_cur_menu, vmenu);
				}
				v_cur_menu = vmenu;
			}
		}
	} else if (button_used) {
		/* Button released */
		button_used = 0;

		debugC(6, kDebugLevelMenu | kDebugLevelInput, "button released!");

		if (v_cur_menu < 0)
			v_cur_menu = 0;

		draw_menu_option_hilite(h_cur_menu, v_cur_menu);

		if (mouse.y <= CHAR_LINES) {
			/* on the menubar */
		} else {
			/* see which option we selected */
			m = get_menu(h_cur_menu);
			list_for_each(h, &m->down, next) {
				d = list_entry(h, struct agi_menu_option, list);
				if (mouse_over_text(2 + d->index,
					m->wincol + 1, d->text)) {
					/* activate that option */
					if (d->enabled) {
						debugC(6, kDebugLevelMenu | kDebugLevelInput, "event %d registered", d->event);
						game.ev_keyp[d->event].occured = true;
						game.ev_keyp[d->event].data = d->event;
						goto exit_menu;
					}
				}
			}
			goto exit_menu;
		}
	}
#endif				/* USE_MOUSE */

	if (!menu_active) {
		if (h_cur_menu >= 0) {
			draw_menu_hilite(h_cur_menu);
			draw_menu_option(h_cur_menu);
			if (!button_used && v_cur_menu >= 0)
				draw_menu_option_hilite(h_cur_menu, v_cur_menu);
		}
		menu_active = true;
	}

	switch (key) {
	case KEY_ESCAPE:
		debugC(6, kDebugLevelMenu | kDebugLevelInput, "KEY_ESCAPE");
		goto exit_menu;
	case KEY_ENTER:
		debugC(6, kDebugLevelMenu | kDebugLevelInput, "KEY_ENTER");
		d = get_menu_option(h_cur_menu, v_cur_menu);
		if (d->enabled) {
			debugC(6, kDebugLevelMenu | kDebugLevelInput, "event %d registered", d->event);
			game.ev_keyp[d->event].occured = true;
			goto exit_menu;
		}
		break;
	case KEY_DOWN:
	case KEY_UP:
		v_cur_menu += key == KEY_DOWN ? 1 : -1;

		if (v_cur_menu < 0)
			v_cur_menu = 0;
		if (v_cur_menu > v_max_menu[h_cur_menu])
			v_cur_menu = v_max_menu[h_cur_menu];

		draw_menu_option(h_cur_menu);
		draw_menu_option_hilite(h_cur_menu, v_cur_menu);
		break;
	case KEY_RIGHT:
	case KEY_LEFT:
		h_cur_menu += key == KEY_RIGHT ? 1 : -1;

		if (h_cur_menu < 0)
			h_cur_menu = h_max_menu;
		if (h_cur_menu > h_max_menu)
			h_cur_menu = 0;

		v_cur_menu = 0;
		new_menu_selected(h_cur_menu);
		draw_menu_option_hilite(h_cur_menu, v_cur_menu);
		break;
	}

	return true;

exit_menu:
	button_used = 0;
	show_pic();
	write_status();

	setvar(V_key, 0);
	game.keypress = 0;
	game.clock_enabled = clock_val;
	old_input_mode();
	debugC(3, kDebugLevelMenu, "exit_menu: input mode reset to %d", game.input_mode);
	menu_active = false;

	return true;
}

void menu_set_item(int event, int state) {
	struct list_head *h, *v;
	struct agi_menu *m = NULL;
	struct agi_menu_option *d = NULL;

	/* scan all menus for event number # */

	debugC(6, kDebugLevelMenu, "event = %d, state = %d", event, state);
	list_for_each(h, &menubar, next) {
		m = list_entry(h, struct agi_menu, list);
		list_for_each(v, &m->down, next) {
			d = list_entry(v, struct agi_menu_option, list);
			if (d->event == event) {
				d->enabled = state;
				return;
			}
		}
	}
}

void menu_enable_all() {
	struct list_head *h, *v;
	struct agi_menu *m = NULL;
	struct agi_menu_option *d = NULL;

	list_for_each(h, &menubar, next) {
		m = list_entry(h, struct agi_menu, list);
		list_for_each(v, &m->down, next) {
			d = list_entry(v, struct agi_menu_option, list);
			d->enabled = true;
		}
	}

}

}                             // End of namespace Agi