aboutsummaryrefslogtreecommitdiff
path: root/engines/mads/nebular/menu_nebular.cpp
blob: 0ed7a939969056bda8b86a99b625c1880f9b058c (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
/* 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 "common/scummsys.h"
#include "mads/game.h"
#include "mads/mads.h"
#include "mads/resources.h"
#include "mads/screen.h"
#include "mads/nebular/menu_nebular.h"

namespace MADS {

namespace Nebular {

#define NEBULAR_MENUSCREEN 990
#define MADS_MENU_Y ((MADS_SCREEN_HEIGHT - MADS_SCENE_HEIGHT) / 2)
#define MADS_MENU_ANIM_DELAY 70

MenuView::MenuView(MADSEngine *vm) : FullScreenDialog(vm) {
	_breakFlag = false;
}

void MenuView::execute() {
	Common::Event event;

	// Main event loop to show the view
	while (!_breakFlag) {
		// Handle events
		while (g_system->getEventManager()->pollEvent(event)) {
			onEvent(event);
		}

		if (_vm->_events->checkForNextFrameCounter()) {
			// Next frame drawn, so allow view to prepare for following one
			doFrame();
		}

		// Slight delay
		g_system->delayMillis(10);
		_breakFlag = _vm->shouldQuit();
	}
}

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

MainMenu::MainMenu(MADSEngine *vm): MenuView(vm) {
	_itemPosList[0] = Common::Point(12, 68);
	_itemPosList[1] = Common::Point(12, 87);
	_itemPosList[2] = Common::Point(12, 107);
	_itemPosList[3] = Common::Point(184, 75);
	_itemPosList[4] = Common::Point(245, 75);
	_itemPosList[5] = Common::Point(184, 99);

	_delayTimeout = 0;
	_menuItem = NULL;
	_menuItemIndex = 0;
	_frameIndex = 0;
	_highlightedIndex = -1;
	_skipFlag = false;
}

MainMenu::~MainMenu() {
	if (_menuItem)
		delete _menuItem;
}

bool MainMenu::onEvent(Common::Event &event) {
	// Handle keypresses - these can be done at any time, even when the menu items are being drawn
	if (event.type == Common::EVENT_KEYDOWN) {
		switch (event.kbd.keycode) {
		case Common::KEYCODE_ESCAPE:
		case Common::KEYCODE_F6:
			handleAction(EXIT);
			break;

		case Common::KEYCODE_F1:
			handleAction(START_GAME);
			break;

		case Common::KEYCODE_F2:
			handleAction(RESUME_GAME);
			break;

		case Common::KEYCODE_F3:
			handleAction(SHOW_INTRO);
			break;

		case Common::KEYCODE_F4:
			handleAction(CREDITS);
			break;

		case Common::KEYCODE_F5:
			handleAction(QUOTES);
			break;

		case Common::KEYCODE_s: {
			// Goodness knows why, but Rex has a key to restart the menuitem animations

			// Delete the current menu items
			if (_menuItem)
				delete _menuItem;
			/*
			_vm->_palette->deleteRange(_bgPalData);
			delete _bgPalData;
			for (uint i = 0; i < _itemPalData.size(); ++i) {
				_vm->_palette->deleteRange(_itemPalData[i]);
				delete _itemPalData[i];
			}
			_itemPalData.clear();
			*/
			// Restart the animation
			_menuItemIndex = 0;
			_skipFlag = false;
			_menuItem = NULL;
			_vm->_events->hideCursor();
			break;
		}

		default:
			// Any other key skips the menu animation
			_skipFlag = true;
			return false;
		}

		return true;
	}

	int menuIndex;

	switch (event.type) {
	case Common::EVENT_LBUTTONDOWN:
		if (_vm->_events->isCursorVisible()) {
			menuIndex = getHighlightedItem(event.mouse.x, event.mouse.y);

			if (menuIndex != _highlightedIndex) {
//				_bgSurface->copyTo(this, Common::Point(0, MADS_MENU_Y));

				_highlightedIndex = menuIndex;
				if (_highlightedIndex != -1) {
					MSprite *spr = _menuItem->getFrame(_highlightedIndex);
					const Common::Point &pt = _itemPosList[_highlightedIndex];
					spr->copyTo(&_vm->_screen, Common::Point(pt.x, MADS_MENU_Y + pt.y));
				}
			}
		} else {
			// Skip the menu animation
			_skipFlag = true;
		}
		return true;

	case Common::EVENT_LBUTTONUP:
		if (_highlightedIndex != -1)
			handleAction((MADSGameAction)_highlightedIndex);
		return true;

	default:
		break;
	}

	return false;
}

void MainMenu::doFrame() {
	int itemSize;

	uint32 currTime = g_system->getMillis();
	if (currTime < _delayTimeout)
		return;
	_delayTimeout = currTime + MADS_MENU_ANIM_DELAY;

	// Rex Nebular handling to cycle through the animated display of the menu items
	if (_menuItemIndex == 7)
		return;

	// If the user has chosen to skip the menu animation, show the menu immediately
	if (_skipFlag && !_vm->_events->isCursorVisible()) {
		// Clear any pending animation
		_savedSurface.copyTo(&_vm->_screen, Common::Point(0, MADS_MENU_Y));
		
		// Quickly loop through all the menuitems to display each's final frame
		while (_menuItemIndex < 7) {

			if (_menuItem) {
				// Draw the final frame of the menuitem
				MSprite *spr = _menuItem->getFrame(0);
				itemSize = _menuItem->getFrame(0)->h;
				spr->copyTo(&_vm->_screen, Common::Point(_itemPosList[_menuItemIndex - 1].x,
					_itemPosList[_menuItemIndex - 1].y + MADS_MENU_Y + (itemSize / 2) - (spr->h / 2)));

				delete _menuItem;
				//copyTo(_bgSurface, Common::Rect(0, row, width(), row + MADS_SCENE_HEIGHT), 0, 0);
			}

			// Get the next sprite set
			Common::String spritesName = Resources::formatName(NEBULAR_MENUSCREEN, 
				'A', ++_menuItemIndex, EXT_SS, "");
			_menuItem = new SpriteAsset(_vm, spritesName, 0);

			// Slot it into available palette space
/*
			RGBList *palData = _menuItem->getRgbList();
			_vm->_palette->addRange(palData);
			_menuItem->translate(palData, true);
			_itemPalData.push_back(palData);
*/
		}

		_vm->_events->showCursor();
		return;
	}

	if ((_menuItemIndex == 0) || (_frameIndex == 0)) {
		// Get the next menu item
		if (_menuItem) {
			delete _menuItem;

			// Copy over the current display surface area to the background, so the final frame 
			// of the previous menuitem should be kept on the screen
//			copyTo(_bgSurface, Common::Rect(0, row, width(), row + MADS_SCENE_HEIGHT), 0, 0);
		}

		// Get the next menuitem resource
		Common::String spritesName = Resources::formatName(NEBULAR_MENUSCREEN,
			'A', ++_menuItemIndex, EXT_SS, "");

		/*
		//sprintf(resName, "RM%dA%d.SS", REX_MENUSCREEN, ++_menuItemIndex);
		data = _vm->res()->get(resName);
		_menuItem = new SpriteAsset(_vm, data, data->size(), resName);
		_vm->res()->toss(resName);
		*/
		// Slot it into available palette space
		/*
		RGBList *palData = _menuItem->getRgbList();
		_vm->_palette->addRange(palData);
		_menuItem->translate(palData, true);
		_itemPalData.push_back(palData);
		*/
		_frameIndex = _menuItem->getCount() - 1;

		// If the final resource is now loaded, which contains the highlighted versions of 
		// each menuitem, then the startup animation is complete
		if (_menuItemIndex == 7) {
			_vm->_events->showCursor();
			return;
		}
	} else {
		--_frameIndex;
	}

	// Move to the next menuitem frame

	itemSize = _menuItem->getFrame(0)->h;

	//_bgSurface->copyTo(this, 0, row);
	MSprite *spr = _menuItem->getFrame(_frameIndex);
	
	spr->copyTo(&_vm->_screen, 
		Common::Point(_itemPosList[_menuItemIndex - 1].x, 
			_itemPosList[_menuItemIndex - 1].y + MADS_MENU_Y + 
			(itemSize / 2) - (spr->h / 2)));
}

int MainMenu::getHighlightedItem(int x, int y) {
	y -= MADS_MENU_Y;

	for (int index = 0; index < 6; ++index) {
		const Common::Point &pt = _itemPosList[index];
		MSprite *spr = _menuItem->getFrame(index);

		if ((x >= pt.x) && (y >= pt.y) && (x < (pt.x + spr->w)) && (y < (pt.y + spr->h)))
			return index;
	}

	return -1;
}

void MainMenu::handleAction(MADSGameAction action) {
	_vm->_events->hideCursor();
	/*
	switch (action) {
	case START_GAME:
	case RESUME_GAME:
		// Load a sample starting scene - note that, currently, calling loadScene automatically
		// removes this menu screen from being displayed
		_vm->_mouse->cursorOn();
		_vm->_viewManager->addView(_vm->_scene);
		_vm->_scene->loadScene(101);
		return;

	case SHOW_INTRO:
		_vm->_viewManager->showAnimView("@rexopen");
		break;

	case CREDITS:
		_vm->_viewManager->showTextView("credits");
		return;

	case QUOTES:
		_vm->_viewManager->showTextView("quotes");
		return;

	case EXIT:
	{
					// When the Exit action is done from the menu, show one of two possible advertisements

					// Activate the scene display with the specified scene
					bool altAdvert = _vm->_random->getRandomNumber(1000) >= 500;
					_vm->_scene->loadScene(altAdvert ? 995 : 996);
					_vm->_viewManager->addView(_vm->_scene);

					_vm->_viewManager->refreshAll();
					_vm->delay(10000);

					_vm->_events->quitFlag = true;
					return;
	}
		break;
	default:
		break;
	}
	*/
}

} // End of namespace Nebular

} // End of namespace MADS