aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/graphics/controls32.h
blob: de292da3b7d3c5480cb09b5463a88d1d09d684b7 (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
/* 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.
 *
 */

#ifndef SCI_GRAPHICS_CONTROLS32_H
#define SCI_GRAPHICS_CONTROLS32_H

#include "sci/graphics/text32.h"

namespace Sci {

class GfxCache;
class GfxScreen;
class GfxText32;

enum MessageBoxStyle {
	kMessageBoxOK    = 0x0,
	kMessageBoxYesNo = 0x4
};

struct TextEditor {
	/**
	 * The bitmap where the editor is rendered.
	 */
	reg_t bitmap;

	/**
	 * The width of the editor, in bitmap pixels.
	 */
	int16 width;

	/**
	 * The text in the editor.
	 */
	Common::String text;

	/**
	 * The rect where text should be drawn into the editor,
	 * in bitmap pixels.
	 */
	Common::Rect textRect;

	/**
	 * The color of the border. -1 indicates no border.
	 */
	int16 borderColor;

	/**
	 * The text color.
	 */
	uint8 foreColor;

	/**
	 * The background color.
	 */
	uint8 backColor;

	/**
	 * The transparent color.
	 */
	uint8 skipColor;

	/**
	 * The font used to render the text in the editor.
	 */
	GuiResourceId fontId;

	/**
	 * The current position of the cursor within the editor.
	 */
	uint16 cursorCharPosition;

	/**
	 * Whether or not the cursor is currently drawn to the screen.
	 */
	bool cursorIsDrawn;

	/**
	 * The rectangle for drawing the input cursor, in bitmap pixels.
	 */
	Common::Rect cursorRect;

	/**
	 * The maximum allowed text length, in characters.
	 */
	uint16 maxLength;
};

/**
 * A single block of text written to a ScrollWindow.
 */
struct ScrollWindowEntry {
	/**
	 * ID of the line. In SSCI this was actually a memory handle for the string
	 * of this line. We use a simple numeric ID instead.
	 */
	reg_t id;

	/**
	 * The alignment to use when rendering this line of text. If -1, the default
	 * alignment from the corresponding ScrollWindow will be used.
	 */
	TextAlign alignment;

	/**
	 * The color to use to render this line of text. If -1, the default
	 * foreground color from the corresponding ScrollWindow will be used.
	 */
	int16 foreColor;

	/**
	 * The font to use to render this line of text. If -1, the default font from
	 * the corresponding ScrollWindow will be used.
	 */
	GuiResourceId fontId;

	/**
	 * The text.
	 */
	Common::String text;
};

class ScreenItem;

/**
 * A scrollable text window.
 */
class ScrollWindow {
public:
	ScrollWindow(SegManager *segMan, const Common::Rect &gameRect, const Common::Point &position, const reg_t planeObj, const uint8 defaultForeColor, const uint8 defaultBackColor, const GuiResourceId defaultFontId, const TextAlign defaultAlignment, const int16 defaultBorderColor, const uint16 maxNumEntries);
	~ScrollWindow();

	/**
	 * Adds a new text entry to the window. If `fontId`, `foreColor`, or
	 * `alignment` are `-1`, the ScrollWindow's default values will be used.
	 */
	reg_t add(const Common::String &text, const GuiResourceId fontId, const int16 foreColor, const TextAlign alignment, const bool scrollTo);

	/**
	 * Modifies an existing text entry with the given ID. If `fontId`,
	 * `foreColor`, or `alignment` are `-1`, the ScrollWindow's default values
	 * will be used.
	 */
	reg_t modify(const reg_t id, const Common::String &text, const GuiResourceId fontId, const int16 foreColor, const TextAlign alignment, const bool scrollTo);

	/**
	 * Shows the ScrollWindow if it is not already visible.
	 */
	void show();

	/**
	 * Hides the ScrollWindow if it is currently visible.
	 */
	void hide();

	/**
	 * Gets the number of lines that the content of a ScrollWindow is scrolled
	 * upward, as a ratio of the total number of lines of content.
	 */
	Ratio where() const;

	/**
	 * Scrolls the window to a specific location.
	 */
	void go(const Ratio location);

	/**
	 * Scrolls the window to the top.
	 */
	void home();

	/**
	 * Scrolls the window to the bottom.
	 */
	void end();

	/**
	 * Scrolls the window up one line.
	 */
	void upArrow();

	/**
	 * Scrolls the window down one line.
	 */
	void downArrow();

	/**
	 * Scrolls the window up by one page.
	 */
	void pageUp();

	/**
	 * Scrolls the window down by one page.
	 */
	void pageDown();

	/**
	 * Gets a reference to the in-memory bitmap that is used to render the text
	 * in the ScrollWindow.
	 */
	const reg_t getBitmap() const { return _bitmap; }

private:
	SegManager *_segMan;

	typedef Common::Array<ScrollWindowEntry> EntriesList;

	/**
	 * A convenience function that fills a ScrollWindowEntry's properties.
	 */
	void fillEntry(ScrollWindowEntry &entry, const Common::String &text, const GuiResourceId fontId, const int16 foreColor, const TextAlign alignment);

	/**
	 * Rescans the entire text of the ScrollWindow when an entry is added or
	 * modified, calculating the character offsets of all line endings, the
	 * total number of lines of text, the height of the viewport (in lines of
	 * text), the last character visible in the viewport (assuming the viewport
	 * is scrolled to the top), and the line index of the bottommost visible
	 * line (assuming the viewport is scrolled to the top).
	 */
	void computeLineIndices();

	/**
	 * Calculates which text is visible within the ScrollWindow's viewport and
	 * renders the text to the internal bitmap.
	 *
	 * If `doFrameOut` is true, the screen will be refreshed immediately instead
	 * of waiting for the next call to `kFrameOut`.
	 */
	void update(const bool doFrameOut);

	/**
	 * The text renderer.
	 */
	GfxText32 _gfxText32;

	/**
	 * The individual text entries added to the ScrollWindow.
	 */
	EntriesList _entries;

	/**
	 * The maximum number of entries allowed. Once this limit is reached, the
	 * oldest entry will be removed when a new entry is added.
	 */
	uint _maxNumEntries;

	/**
	 * A mapping from a line index to the line's character offset in `_text`.
	 */
	Common::Array<int> _startsOfLines;

	/**
	 * All text added to the window.
	 */
	Common::String _text;

	/**
	 * Text that is within the viewport of the ScrollWindow.
	 */
	Common::String _visibleText;

	/**
	 * The offset of the first visible character in `_text`.
	 */
	int _firstVisibleChar;

	/**
	 * The index of the line that is at the top of the viewport.
	 */
	int _topVisibleLine;

	/**
	 * The index of the last visible character in `_text`, or -1 if there is no
	 * text.
	 */
	int _lastVisibleChar;

	/**
	 * The index of the line that is at the bottom of the viewport, or -1 if
	 * there is no text.
	 */
	int _bottomVisibleLine;

	/**
	 * The total number of lines in the backbuffer. This number may be higher
	 * than the total number of entries if an entry contains newlines.
	 */
	int _numLines;

	/**
	 * The number of lines that are currently visible in the text area of the
	 * window.
	 */
	int _numVisibleLines;

	/**
	 * The plane in which the ScrollWindow should be rendered.
	 */
	reg_t _plane;

	/**
	 * The default text color.
	 */
	uint8 _foreColor;

	/**
	 * The default background color of the text bitmap.
	 */
	uint8 _backColor;

	/**
	 * The default border color of the text bitmap. If -1, the viewport will
	 * have no border.
	 */
	int16 _borderColor;

	/**
	 * The default font used for rendering text into the ScrollWindow.
	 */
	GuiResourceId _fontId;

	/**
	 * The default text alignment used for rendering text into the ScrollWindow.
	 */
	TextAlign _alignment;

	/**
	 * The visibility of the ScrollWindow.
	 */
	bool _visible;

	/**
	 * The dimensions of the text box inside the font bitmap, in text-system
	 * coordinates.
	 */
	Common::Rect _textRect;

	/**
	 * The top-left corner of the ScrollWindow's screen item, in game script
	 * coordinates, relative to the parent plane.
	 */
	Common::Point _position;

	/**
	 * The height of the default font in screen pixels. All fonts rendered into
	 * the ScrollWindow must have this same height.
	 */
	uint8 _pointSize;

	/**
	 * The bitmap used to render text.
	 */
	reg_t _bitmap;

	/**
	 * A monotonically increasing ID used to identify text entries added to the
	 * ScrollWindow.
	 */
	uint16 _nextEntryId;

	/**
	 * The ScrollWindow's screen item.
	 */
	ScreenItem *_screenItem;
};

/**
 * Controls class, handles drawing of UI controls in SCI32 games that use kernel
 * controls instead of custom script controls.
 */
class GfxControls32 {
public:
	GfxControls32(SegManager *segMan, GfxCache *cache, GfxText32 *text);
	~GfxControls32();

private:
	SegManager *_segMan;
	GfxCache *_gfxCache;
	GfxText32 *_gfxText32;

#pragma mark -
#pragma mark Text input control
public:
	reg_t kernelEditText(const reg_t controlObject);

private:
	/**
	 * If true, typing will overwrite text that already exists at the text
	 * cursor's current position.
	 */
	bool _overwriteMode;

	/**
	 * The tick at which the text cursor should be toggled by `flashCursor`.
	 */
	uint32 _nextCursorFlashTick;

	/**
	 * Draws the text cursor for the given editor.
	 */
	void drawCursor(TextEditor &editor);

	/**
	 * Erases the text cursor for the given editor.
	 */
	void eraseCursor(TextEditor &editor);

	/**
	 * Toggles the text cursor for the given editor to be either drawn or
	 * erased.
	 */
	void flashCursor(TextEditor &editor);

#pragma mark -
#pragma mark Scrollable window control
public:
	/**
	 * Creates a new scrollable window and returns the ID for the new window,
	 * which is used by game scripts to interact with scrollable windows.
	 */
	reg_t makeScrollWindow(const Common::Rect &gameRect, const Common::Point &position, const reg_t plane, const uint8 defaultForeColor, const uint8 defaultBackColor, const GuiResourceId defaultFontId, const TextAlign defaultAlignment, const int16 defaultBorderColor, const uint16 maxNumEntries);

	/**
	 * Gets a registered ScrollWindow instance by ID.
	 */
	ScrollWindow *getScrollWindow(const reg_t id);

	/**
	 * Destroys the scroll window with the given ID.
	 */
	void destroyScrollWindow(const reg_t id);

private:
	typedef Common::HashMap<uint16, ScrollWindow *> ScrollWindowMap;

	/**
	 * Monotonically increasing ID used to identify ScrollWindow instances.
	 */
	uint16 _nextScrollWindowId;

	/**
	 * A lookup table for registered ScrollWindow instances.
	 */
	ScrollWindowMap _scrollWindows;

#pragma mark -
#pragma mark Message box
public:
	/**
	 * Displays an OS-level message dialog.
	 */
	reg_t kernelMessageBox(const Common::String &message, const Common::String &title, const uint16 style);

private:
	/**
	 * Convenience function for creating and showing a message box.
	 */
	int16 showMessageBox(const Common::String &message, const char *const okLabel, const char *const altLabel, const int16 okValue, const int16 altValue);
};

} // End of namespace Sci

#endif