aboutsummaryrefslogtreecommitdiff
path: root/engines/mads/dialogs.cpp
blob: 5e38f34fc6f333f30ffc76e299d27360fa13a256 (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
/* 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 "common/config-manager.h"
#include "mads/mads.h"
#include "mads/screen.h"
#include "mads/msurface.h"
#include "mads/nebular/dialogs_nebular.h"

namespace MADS {

Dialog::Dialog(MADSEngine *vm)
	: _vm(vm), _savedSurface(nullptr), _position(Common::Point(-1, -1)),
	  _width(0), _height(0) {
	TEXTDIALOG_CONTENT1 = 0XF8;
	TEXTDIALOG_CONTENT2 = 0XF9;
	TEXTDIALOG_EDGE = 0XFA;
	TEXTDIALOG_BACKGROUND = 0XFB;
	TEXTDIALOG_FC = 0XFC;
	TEXTDIALOG_FD = 0XFD;
	TEXTDIALOG_FE = 0XFE;
	TEXTDIALOG_BLACK = 0;
}

Dialog::~Dialog() {
}

void Dialog::save() {
	_savedSurface = new MSurface(_width, _height);
	_vm->_screen.copyTo(_savedSurface,
		Common::Rect(_position.x, _position.y, _position.x + _width, _position.y + _height),
		Common::Point());

	_vm->_screen.copyRectToScreen(getBounds());
}

void Dialog::restore() {
	if (_savedSurface) {
		_savedSurface->copyTo(&_vm->_screen, _position);
		delete _savedSurface;
		_savedSurface = nullptr;

		_vm->_screen.copyRectToScreen(getBounds());

		Common::copy(&_dialogPalette[0], &_dialogPalette[8 * 3],
			&_vm->_palette->_mainPalette[248 * 3]);
		_vm->_palette->setPalette(&_vm->_palette->_mainPalette[248 * 3], 248, 8);
	}
}

void Dialog::draw() {
	// Calculate the dialog positioning
	calculateBounds();

	// Save the screen portion the dialog will overlap
	save();

	setDialogPalette();

	// Draw the dialog
	// Fill entire content of dialog
	Common::Rect bounds = getBounds();
	_vm->_screen.fillRect(bounds, TEXTDIALOG_BACKGROUND);

	// Draw the outer edge lines
	_vm->_screen.hLine(_position.x + 1, _position.y + _height - 2,
		_position.x + _width - 2, TEXTDIALOG_EDGE);
	_vm->_screen.hLine(_position.x, _position.y + _height - 1,
		_position.x + _width - 1, TEXTDIALOG_EDGE);
	_vm->_screen.vLine(_position.x + _width - 2, _position.y + 2,
		_position.y + _height - 2, TEXTDIALOG_EDGE);
	_vm->_screen.vLine(_position.x + _width - 1, _position.y + 1,
		_position.y + _height - 1, TEXTDIALOG_EDGE);

	// Draw the gravelly dialog content
	drawContent(Common::Rect(_position.x + 2, _position.y + 2,
		_position.x + _width - 2, _position.y + _height - 2), 0,
		TEXTDIALOG_CONTENT1, TEXTDIALOG_CONTENT2);
}

void Dialog::setDialogPalette() {
	// Save the high end of the palette, and set up the entries for dialog display
	Common::copy(&_vm->_palette->_mainPalette[TEXTDIALOG_CONTENT1 * 3],
		&_vm->_palette->_mainPalette[TEXTDIALOG_CONTENT1 * 3 + 8 * 3],
		&_dialogPalette[0]);
	Palette::setGradient(_vm->_palette->_mainPalette, TEXTDIALOG_CONTENT1, 2, 0x90, 0x80);
	Palette::setGradient(_vm->_palette->_mainPalette, TEXTDIALOG_EDGE, 2, 0x9C, 0x70);
	Palette::setGradient(_vm->_palette->_mainPalette, TEXTDIALOG_FC, 2, 0x90, 0x80);
	Palette::setGradient(_vm->_palette->_mainPalette, TEXTDIALOG_FE, 1, 0xDC, 0xDC);

	_vm->_palette->setPalette(_vm->_palette->_mainPalette + (TEXTDIALOG_CONTENT1 * 3),
		TEXTDIALOG_CONTENT1, 8);
}

void Dialog::calculateBounds() {
}

void Dialog::drawContent(const Common::Rect &r, int seed, byte color1, byte color2) {
	uint16 currSeed = seed ? seed : 0xB78E;

	for (int yp = 0; yp < r.height(); ++yp) {
		byte *destP = _vm->_screen.getBasePtr(r.left, r.top + yp);

		for (int xp = 0; xp < r.width(); ++xp) {
			uint16 seedAdjust = currSeed;
			currSeed += 0x181D;
			seedAdjust = (seedAdjust >> 9) | ((seedAdjust & 0x1ff) << 7);
			currSeed ^= seedAdjust;
			seedAdjust = (seedAdjust >> 3) | ((seedAdjust & 7) << 13);
			currSeed += seedAdjust;

			*destP++ = (currSeed & 0x10) ? color2 : color1;
		}
	}
}

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

TextDialog::TextDialog(MADSEngine *vm, const Common::String &fontName,
		const Common::Point &pos, int maxChars)
	: Dialog(vm) {
	_vm = vm;
	_font = _vm->_font->getFont(fontName);
	_position = pos;

	_vm->_font->setColors(TEXTDIALOG_BLACK, TEXTDIALOG_BLACK, TEXTDIALOG_BLACK, TEXTDIALOG_BLACK);

	_innerWidth = (_font->maxWidth() + 1) * maxChars;
	_width = _innerWidth + 10;
	_lineSize = maxChars * 2;
	_lineWidth = 0;
	_currentX = 0;
	_numLines = 0;
	Common::fill(&_lineXp[0], &_lineXp[TEXT_DIALOG_MAX_LINES], 0);
	_askLineNum = -1;
	_askXp = 0;
}

TextDialog::~TextDialog() {
}

void TextDialog::addLine(const Common::String &line, bool underline) {
	if (_lineWidth > 0 || _currentX > 0)
		incNumLines();

	int stringWidth = _font->getWidth(line, 1);
	if (stringWidth >= _innerWidth || (int)line.size() >= _lineSize) {
		wordWrap(line);
	} else {
		_lineXp[_numLines] = (_innerWidth / 2) - (stringWidth / 2);
		_lines[_numLines] = line;

		if (underline)
			underlineLine();
	}

	incNumLines();
}

void TextDialog::underlineLine() {
	_lineXp[_numLines] |= 0x80;
}

void TextDialog::downPixelLine() {
	_lineXp[_numLines] |= 0x40;
}

void TextDialog::incNumLines() {
	_lineWidth = 0;
	_currentX = 0;
	if (++_numLines == TEXT_DIALOG_MAX_LINES)
		error("Exceeded text dialog line max");
}

void TextDialog::wordWrap(const Common::String &line) {
	Common::String tempLine;

	if (!line.empty()) {
		const char *srcP = line.c_str();

		do {
			tempLine = "";
			bool endWord = false;
			bool newLine = false;
			bool continueFlag = true;

			do {
				if (!*srcP) {
					continueFlag = false;
				} else {
					tempLine += *srcP;

					if (*srcP == 10) {
						continueFlag = false;
						newLine = true;
						++srcP;
						tempLine.deleteLastChar();
					} else if (*srcP == ' ') {
						++srcP;
						endWord = true;
					} else if (!endWord) {
						++srcP;
					} else {
						tempLine.deleteLastChar();
						continueFlag = false;
					}
				}
			} while (continueFlag);

			if (tempLine.hasSuffix(" "))
				tempLine.deleteLastChar();

			Common::String tempLine2;
			if (_currentX > 0)
				tempLine2 += ' ';
			tempLine2 += tempLine;

			int lineWidth = _font->getWidth(tempLine2, 1);
			if (((_currentX + (int)tempLine2.size()) > _lineSize) ||
					((_lineWidth + lineWidth) > _innerWidth)) {
				incNumLines();
				appendLine(tempLine);
			} else {
				appendLine(tempLine2);
			}

			if (newLine)
				incNumLines();
		} while (*srcP);
	}
}

void TextDialog::appendLine(const Common::String &line) {
	_currentX += line.size();
	_lineWidth += _font->getWidth(line, 1) + 1;
	_lines[_numLines] += line;
}

void TextDialog::addInput() {
	_askXp = _currentX + 1;
	_askLineNum = _numLines;
	incNumLines();
}

void TextDialog::addBarLine() {
	if (_lineWidth > 0 || _currentX > 0)
		incNumLines();

	_lineXp[_numLines] = 0xFF;
	incNumLines();
}

void TextDialog::setLineXp(int xp) {
	_lineXp[_numLines] = xp;
}

void TextDialog::draw() {
	if (!_lineWidth)
		--_numLines;

	// Figure out the size and position for the dialog
	calculateBounds();

	// Draw the underlying dialog
	Dialog::draw();

	// Draw the text lines
	int lineYp = _position.y + 5;
	for (int lineNum = 0; lineNum <= _numLines; ++lineNum) {
		if (_lineXp[lineNum] == -1) {
			// Draw a line across the entire dialog
			_vm->_screen.hLine(_position.x + 2,
				lineYp + (_font->getHeight() + 1)  / 2,
				_position.x + _width - 4, TEXTDIALOG_BLACK);
		} else {
			// Draw a text line
			int xp = (_lineXp[lineNum] & 0x7F) + _position.x + 5;
			int yp = lineYp;
			if (_lineXp[lineNum] & 0x40)
				++yp;

			_font->writeString(&_vm->_screen, _lines[lineNum],
				Common::Point(xp, yp), 1);

			if (_lineXp[lineNum] & 0x80) {
				// Draw an underline under the text
				int lineWidth = _font->getWidth(_lines[lineNum], 1);
				_vm->_screen.hLine(xp, yp + _font->getHeight(), xp + lineWidth,
					TEXTDIALOG_BLACK);
			}
		}

		lineYp += _font->getHeight() + 1;
	}

	_vm->_screen.copyRectToScreen(getBounds());
}

void TextDialog::calculateBounds() {
	_height = (_font->getHeight() + 1) * (_numLines + 1) + 10;
	if (_position.x == -1)
		_position.x = 160 - (_width / 2);
	if (_position.y == -1)
		_position.y = 100 - (_height / 2);

	if ((_position.x + _width) > _vm->_screen.getWidth())
		_position.x = _vm->_screen.getWidth() - (_position.x + _width);
	if ((_position.y + _height) > _vm->_screen.getHeight())
		_position.y = _vm->_screen.getHeight() - (_position.y + _height);
}

void TextDialog::drawWithInput() {
	//int innerWidth = _innerWidth;
	//int lineHeight = _font->getHeight() + 1;
	//int xp = _position.x + 5;

	// Draw the content of the dialog
	drawContent(Common::Rect(_position.x + 2, _position.y + 2,
		_position.x + _width - 2, _position.y + _height - 2), 0,
		TEXTDIALOG_CONTENT1, TEXTDIALOG_CONTENT2);

	error("TODO: drawWithInput");
}

void TextDialog::show() {
	// Draw the dialog
	draw();
	_vm->_events->showCursor();

	// Wait for mouse click
	do {
		_vm->_events->waitForNextFrame();
	} while (!_vm->shouldQuit() && !_vm->_events->isKeyPressed() && !_vm->_events->_mouseReleased);

	// Allow the mouse release or keypress to be gobbled up
	if (!_vm->shouldQuit()) {
		_vm->_events->waitForNextFrame();
		_vm->_events->_pendingKeys.clear();
	}

	// Restore the background
	restore();
}

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

MessageDialog::MessageDialog(MADSEngine *vm, int maxChars, ...)
	: TextDialog(vm, FONT_INTERFACE, Common::Point(-1, -1), maxChars) {
	// Add in passed line list
	va_list va;
	va_start(va, maxChars);

	const char *line = va_arg(va, const char *);
	while (line) {
		addLine(line);
		line = va_arg(va, const char *);
	}
	va_end(va);
}

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

Dialogs *Dialogs::init(MADSEngine *vm) {
	if (vm->getGameID() == GType_RexNebular)
		return new Nebular::DialogsNebular(vm);

	// Throw a warning for now, since the associated Dialogs class isn't implemented yet
	warning("Dialogs: Unknown game");
	// HACK: Reuse the implemented Nebular dialogs for now, to avoid crashing later on
	return new Nebular::DialogsNebular(vm);
}

Dialogs::Dialogs(MADSEngine *vm)
	: _vm(vm) {
	_pendingDialog = DIALOG_NONE;
}

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

FullScreenDialog::FullScreenDialog(MADSEngine *vm) : _vm(vm) {
	switch (_vm->getGameID()) {
	case GType_RexNebular:
		_screenId = 990;
		break;
	case GType_Phantom:
		_screenId = 920;
		break;
	case GType_Dragonsphere:
		_screenId = 922;
		break;
	default:
		error("FullScreenDialog:Unknown game");
	}
	_palFlag = true;
}

FullScreenDialog::~FullScreenDialog() {
	_vm->_screen.resetClipBounds();
	_vm->_game->_scene.restrictScene();
}

void FullScreenDialog::display() {
	Game &game = *_vm->_game;
	Scene &scene = game._scene;

	int nextSceneId = scene._nextSceneId;
	int currentSceneId = scene._currentSceneId;
	int priorSceneId = scene._priorSceneId;

	if (_screenId > 0) {
		SceneInfo *sceneInfo = SceneInfo::init(_vm);
		sceneInfo->load(_screenId, 0, "", 0, scene._depthSurface, scene._backgroundSurface);
	}

	scene._priorSceneId = priorSceneId;
	scene._currentSceneId = currentSceneId;
	scene._nextSceneId = nextSceneId;

	_vm->_events->initVars();
	game._kernelMode = KERNEL_ROOM_INIT;

	byte pal[768];
	if (_vm->_screenFade) {
		Common::fill(&pal[0], &pal[PALETTE_SIZE], 0);
		_vm->_palette->setFullPalette(pal);
	} else {
		_vm->_palette->getFullPalette(pal);
		_vm->_palette->fadeOut(pal, nullptr, 0, PALETTE_COUNT, 0, 1, 1, 16);
	}

	// Set Fx state and palette entries
	game._fx = _vm->_screenFade == SCREEN_FADE_SMOOTH ? kTransitionFadeIn : kCenterVertTransition;
	game._trigger = 0;

	// Clear the screen and draw the upper and lower horizontal lines
	_vm->_screen.empty();
	_vm->_palette->setLowRange();
	_vm->_screen.hLine(0, 20, MADS_SCREEN_WIDTH, 2);
	_vm->_screen.hLine(0, 179, MADS_SCREEN_WIDTH, 2);
	_vm->_screen.resetClipBounds();
	_vm->_screen.copyRectToScreen(Common::Rect(0, 0, MADS_SCREEN_WIDTH, MADS_SCREEN_HEIGHT));

	// Restrict the screen to the area between the two lines
	_vm->_screen.setClipBounds(Common::Rect(0, DIALOG_TOP, MADS_SCREEN_WIDTH,
		DIALOG_TOP + MADS_SCENE_HEIGHT));
	_vm->_game->_scene.restrictScene();

	if (_screenId > 0)
		scene._spriteSlots.fullRefresh();
}

} // End of namespace MADS