aboutsummaryrefslogtreecommitdiff
path: root/engines/cge2/vmenu.cpp
blob: fa4c29c1d752ec2be8d5d83e762a23aeefef8b4e (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
/* 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.
 *
 */

/*
 * This code is based on original Sfinx source code
 * Copyright (c) 1994-1997 Janus B. Wisniewski and L.K. Avalon
 */

#include "cge2/cge2.h"
#include "cge2/vmenu.h"
#include "cge2/events.h"

namespace CGE2 {

void StartCountDownChoice::proc() {
	_vm->switchScene(-1);
}

void ResetQSwitchChoice::proc() {
	_vm->_commandHandlerTurbo->addCommand(kCmdSeq, kPowerRef, 1, nullptr);
	_vm->keyClick();
}

MenuBar::MenuBar(CGE2Engine *vm, uint16 w, byte *c) : Talk(vm) {
	_color = c;
	int h = kFontHigh + 2 * kMenuBarVerticalMargin, i = (w += 2 * kMenuBarHorizontalMargin) * h;
	uint8 *p = new uint8[i];
	uint8 *p1;
	uint8 *p2;
	uint8 lt = _color[kLt];
	uint8 rb = _color[kRb];
	BitmapPtr b;

	memset(p + w, kPixelTransp, i - 2 * w);
	memset(p, lt, w);
	memset(p + i - w, rb, w);
	p1 = p;
	p2 = p + i - 1;
	for (i = 0; i < h; i++) {
		*p1 = lt;
		*p2 = rb;
		p1 += w;
		p2 -= w;
	}
	b = new Bitmap[1];
	b[0] = Bitmap(vm, w, h, p);
	delete[] p;
	setShapeList(b, 1);
	_flags._slav = true;
	_flags._tran = true;
	_flags._kill = true;
}

VMenu *VMenu::_addr = nullptr;

VMenu::VMenu(CGE2Engine *vm, Common::Array<Choice *> list, V2D pos, ColorBank col)
	: Talk(vm, vmGather(list), kTBRect, col), _menu(list), _bar(nullptr), _items(list.size()), _vm(vm) {
	delete[] _vmgt; // Lefotver of vmGather.

	_addr = this;
	_recent = -1;
	_flags._kill = true;

	if (pos.x < 0 || pos.y < 0)
		center();
	else
		gotoxyz(V2D(_vm, pos.x - _siz.x / 2, pos.y - (kTextVMargin + kFontHigh / 2)));

	_vm->_vga->_showQ->append(this);
	_bar = new MenuBar(_vm, _siz.x - 2 * kTextHMargin, _color);
	_bar->gotoxyz(V2D(_vm, _pos2D.x, _pos2D.y + kTextVMargin - kMenuBarVerticalMargin));
	_vm->_vga->_showQ->append(_bar);
}

char *VMenu::vmGather(Common::Array<Choice *> list) {
	int len = 0;
	int h = 0;

	for (uint i = 0; i < list.size(); i++) {
		len += strlen(list[i]->_text);
		++h;
	}
	_vmgt = new char[len + h];
	if (_vmgt) {
		*_vmgt = '\0';
		for (uint i = 0; i < list.size(); i++) {
			if (*_vmgt)
				strcat(_vmgt, "|");
			strcat(_vmgt, list[i]->_text);
			++h;
		}
	}
	return _vmgt;
}


VMenu::~VMenu() {
	_addr = nullptr;

	for (uint i = 0; i < _menu.size(); i++) {
		delete _menu[i];
	}
}

void VMenu::touch(uint16 mask, V2D pos, Common::KeyCode keyCode) {
	if (_items) {
		Sprite::touch(mask, pos, keyCode);

		int n = 0;
		bool ok = false;
		int h = kFontHigh + kTextLineSpace;
		pos.y -= kTextVMargin - 1;
		if (pos.y >= 0) {
			if (pos.x < 0)
				pos.x = -pos.x;
			n = pos.y / h;
			if (n < _items)
				ok = (pos.x <= (_siz.x >> 1) - kTextHMargin);
			else
				n = _items - 1;
		}

		_bar->gotoxyz(V2D(_vm, _pos2D.x, _pos2D.y + kTextVMargin + n * h - kMenuBarVerticalMargin));
		n = _items - 1 - n;

		if (ok && (mask & kMouseLeftUp)) {
			_items = 0;
			_vm->_commandHandlerTurbo->addCommand(kCmdKill, -1, 0, this);
			_menu[_recent = n]->proc();
		}
	}
}

} // End of namespace CGE2