File: grequest.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (442 lines) | stat: -rw-r--r-- 10,798 bytes parent folder | download | duplicates (3)
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
/* 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 3 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
 * aint32 with this program; if not, write to the Free Software
 *
 *
 * Based on the original sources
 *   Faery Tale II -- The Halls of the Dead
 *   (c) 1993-1996 The Wyrmkeep Entertainment Co.
 */

#include "saga2/saga2.h"
#include "saga2/grequest.h"
#include "saga2/modal.h"
#include "saga2/mouseimg.h"

namespace Saga2 {

extern gFont        *mainFont;
extern vDisplayPage *drawPage;

const int           maxLines = 16,
                    maxButtons = 16,
                    maxText = 512,
                    maxButtonText = 128,
                    buttonWidth = 60;

//  This function takes a string which is composed of several
//  substrings, each separated by a delimiter character, and
//  breaks it up into the individual sub-strings.

int16 SplitString(
    char            *text,
    char            *textStart[],
    int16           maxStrings,
    char            delimiter) {
	int16           count;

	for (count = 0; count < maxStrings;) {
		textStart[count++] = text;
		if ((text = strchr(text, delimiter)) == nullptr) break;
		*text++ = '\0';
	}
	return count;
}


static void handleRequestEvent(gEvent &ev) {
	gWindow         *win;
	requestInfo     *ri;

	if (ev.panel && ev.eventType == kEventNewValue && ev.value) {
		win = ev.panel->getWindow();        // get the window pointer
		ri = win ? (requestInfo *)win->_userData : nullptr;

		if (ri) {
			ri->running = 0;
			ri->result = ev.panel->_id;
		}
	}
}

/* ===================================================================== *
   ModalDialogWindow class: The base class for modal dialog windows
 * ===================================================================== */

class ModalDialogWindow : public ModalWindow {

	int16   _titleCount;
	Point16 _titlePos[maxLines];
	char    *_titleStrings[maxLines];
	char    _titleBuf[maxText];

	void positionText(
	    char *windowText,
	    va_list args,
	    const Rect16 &textArea);

public:
	ModalDialogWindow(const Rect16 &r,
	                  uint16 ident,
	                  AppFunc *cmd,
	                  char *windowText,
	                  va_list args);
	ModalDialogWindow(const Rect16 &r,
	                  uint16 ident,
	                  AppFunc *cmd,
	                  char *windowText,
	                  va_list args,
	                  const Rect16 &textArea);

	void drawClipped(
	    gPort         &port,
	    const Point16 &offset,
	    const Rect16  &r) override;
};

void ModalDialogWindow::positionText(
    char *windowText,
    va_list args,
    const Rect16 &textArea) {
	if (windowText) {
		int16   i,
		        yPos,
		        maxY;

		int16   fontHeight = mainFont->height;

		// make a copy of the window text string
		Common::vsprintf_s(_titleBuf, windowText, args);

		//  break up the title text string
		_titleCount = SplitString(_titleBuf, _titleStrings, maxLines, '\n');

		yPos = textArea.y +
		       ((textArea.height - _titleCount * fontHeight) >> 1);
		yPos = MAX(yPos, textArea.y);

		maxY = textArea.y + textArea.height - fontHeight;

		for (i = 0; i < _titleCount; i++, yPos += fontHeight) {
			if (yPos < maxY) {
				_titlePos[i].y = yPos;
				_titlePos[i].x =
				    textArea.x +
				    ((textArea.width -
				      TextWidth(mainFont, _titleStrings[i], -1, 0))
				     >> 1);
			} else _titleCount = i;
		}
	} else _titleCount = 0;
}


ModalDialogWindow::ModalDialogWindow(
    const Rect16 &r,
    uint16 ident,
    AppFunc *cmd,
    char *windowText,
    va_list args) :
	ModalWindow(r, ident, cmd) {
	positionText(windowText, args, Rect16(0, 0, r.width, r.height));
}

ModalDialogWindow::ModalDialogWindow(
    const Rect16 &r,
    uint16 ident,
    AppFunc *cmd,
    char *windowText,
    va_list args,
    const Rect16 &textArea) :
	ModalWindow(r, ident, cmd) {
	positionText(windowText, args, textArea);
}

void ModalDialogWindow::drawClipped(
    gPort         &port,
    const Point16 &offset,
    const Rect16  &r) {
	if (!_extent.overlap(r)) return;

	int16   i;
	Point16 origin;
	gFont   *textFont = mainFont;
	Rect16  rect;

	SAVE_GPORT_STATE(port);

	origin.x = _extent.x - offset.x;
	origin.y = _extent.y - offset.y;

	rect.x = origin.x;
	rect.y = origin.y;
	rect.width  = _extent.width;
	rect.height = _extent.height;

	port.setColor(4);
	port.frameRect(rect, 2);
	rect.expand(-2, -2);
	port.setColor(12);
	port.fillRect(rect);

	port.setFont(textFont);
	for (i = 0; i < _titleCount; i++) {
		Point16 textPos = origin + _titlePos[i];

		port.moveTo(textPos + Point16(-1, -1));
		port.setColor(2);
		port.drawText(_titleStrings[i], -1);
		port.moveTo(textPos + Point16(1, 1));
		port.setColor(14);
		port.drawText(_titleStrings[i], -1);
		port.moveTo(textPos);
		port.setColor(8);
		port.drawText(_titleStrings[i], -1);
	}

	ModalWindow::drawClipped(port, offset, r);
}

/* ===================================================================== *
   ModalRequestWindow class: A modal request dialog box
 * ===================================================================== */

class ModalRequestWindow : public ModalDialogWindow {
	char    _buttonBuf[maxButtonText];

	static Rect16 getTextArea(const Rect16 &r) {
		return Rect16(2, 2, r.width - 4, r.height - mainFont->height - 12);
	}

public:
	ModalRequestWindow(
	    const Rect16 &r,
	    uint16 ident,
	    AppFunc *cmd,
	    char *windowText,
	    char *buttonText,
	    va_list args);
};

ModalRequestWindow::ModalRequestWindow(
    const Rect16 &r,
    uint16 ident,
    AppFunc *cmd,
    char *windowText,
    char *buttonText,
    va_list args) :
	ModalDialogWindow(r, ident, cmd, windowText, args, getTextArea(r)) {
	int16   i;

	int16   xPos;

	int16   extraSpace,
	        intervals;

	int16   buttonCount;
	char    *buttonStrings[maxButtons];

	int16   fontHeight = mainFont->height;

	Common::strlcpy(_buttonBuf, (buttonText ? buttonText : "_OK"), sizeof(_buttonBuf));

	//  break up the button text string
	buttonCount = SplitString(_buttonBuf, buttonStrings, maxButtons, '|');

	extraSpace = r.width - buttonWidth * buttonCount;

	intervals = buttonCount * 2 + 1;
	xPos = 0;

	for (i = 0; i < buttonCount; i++) {
		int16       width,
		            extra;

		width = buttonWidth;

		if (intervals > 0) {
			extra = extraSpace / intervals;

			extraSpace -= extra;
			intervals--;
			xPos += extra;
		}

		if (intervals > 0) {
			extra = extraSpace / intervals;

			extraSpace -= extra;
			intervals--;
		} else extra = 0;

		width += extra;

		new LabeledButton(*this,
		                     Rect16(xPos,
		                            r.height - fontHeight - 8,
		                            width,
		                            fontHeight + 6),
		                     *mouseCursors[kMouseCloseBx2Image],
		                     *mouseCursors[kMouseCloseBx1Image],
		                     buttonStrings[i],
		                     i,
		                     handleRequestEvent);

		xPos += width;
	}
}

/* ===================================================================== *
   ModalDisplayWindow class: A modal text display window
 * ===================================================================== */

class ModalDisplayWindow : public ModalDialogWindow {
public:
	ModalDisplayWindow(
	    const Rect16 &r,
	    uint16 ident,
	    AppFunc *cmd,
	    char *windowText,
	    va_list args) :
		ModalDialogWindow(r, ident, cmd, windowText, args) {
	}

	void pointerRelease(gPanelMessage &) override;
	bool keyStroke(gPanelMessage &) override;
};

void ModalDisplayWindow::pointerRelease(gPanelMessage &) {
	requestInfo     *ri = (requestInfo *)_userData;
	if (ri) ri->running = false;
}

bool ModalDisplayWindow::keyStroke(gPanelMessage &) {
	requestInfo     *ri = (requestInfo *)_userData;
	if (ri) ri->running = false;
	return true;
}

/* ===================================================================== *
   Modal game dialog functions
 * ===================================================================== */

int16 GameDialogA(
    char            *windowText,
    char            *buttonText,
    int             /*resnum*/,
    va_list         args) {
	requestInfo     rInfo;

	ModalRequestWindow *win;

	rInfo.result = -1;
	rInfo.running = true;

	win = new ModalRequestWindow(
	          Rect16((drawPage->_size.x - 200) / 2,
	                 (drawPage->_size.y - 100) / 3,
	                 200,
	                 100),
	          0,
	          nullptr,
	          windowText,
	          buttonText,
	          args);

	if (win == nullptr) {
		//  REM: perhaps a memory alert of some sort???
		error("Unable to open requester window.");
	}

	win->_userData = &rInfo;
	win->open();

	EventLoop(rInfo.running, false);

	delete win;
	return rInfo.result;
}

int16 GameDialog(
    char            *windowText,
    char            *buttonText,
    int             resNum,
    ...) {
	int16   result;
	va_list argptr;

	va_start(argptr, resNum);
	result = GameDialogA(windowText,
	                     buttonText,
	                     resNum,
	                     argptr);
	va_end(argptr);
	return result;
}

int16 GameDisplayA(
    char            *windowText,
    int             /*resnum*/,
    va_list         args) {
	requestInfo     rInfo;

	ModalDisplayWindow *win;

	rInfo.result = -1;
	rInfo.running = true;

	win = new ModalDisplayWindow(
	          Rect16((drawPage->_size.x - 200) / 2,
	                 (drawPage->_size.y - 100) / 3,
	                 200,
	                 100),
	          0,
	          nullptr,
	          windowText,
	          args);

	if (win == nullptr) {
		//  REM: perhaps a memory alert of some sort???
		error("Unable to open requester window.");
	}

	win->_userData = &rInfo;
	win->open();

	EventLoop(rInfo.running, false);

	delete win;
	return rInfo.result;
}

int16 GameDisplay(
    char            *windowText,
    int             resNum,
    ...) {
	int16   result;
	va_list argptr;

	va_start(argptr, resNum);
	result = GameDisplayA(windowText,
	                      resNum,
	                      argptr);
	va_end(argptr);
	return result;
}

} // end of namespace Saga2