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
|
/* 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.
*
* MIT License:
*
* Copyright (c) 2009 Alexei Svitkine, Eugene Sandulenko
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "common/system.h"
#include "common/unzip.h"
#include "graphics/cursorman.h"
#include "graphics/fontman.h"
#include "graphics/font.h"
#include "graphics/fonts/bdf.h"
#include "graphics/palette.h"
#include "wage/wage.h"
#include "wage/design.h"
#include "wage/entities.h"
#include "wage/gui.h"
namespace Wage {
enum {
kMenuHeight = 19,
kMenuPadding = 6,
kMenuItemHeight = 19,
kBorderWidth = 17,
kDesktopArc = 7
};
static const byte palette[] = {
0, 0, 0, // Black
0x80, 0x80, 0x80, // Gray
0xff, 0xff, 0xff, // White
0x00, 0xff, 0x00 // Green
};
static byte checkersPattern[8] = { 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa };
static byte fillPattern[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
static const byte macCursorArrow[] = {
2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2, 0, 2, 3, 3, 3, 3, 3, 3, 3, 3,
2, 0, 0, 2, 3, 3, 3, 3, 3, 3, 3,
2, 0, 0, 0, 2, 3, 3, 3, 3, 3, 3,
2, 0, 0, 0, 0, 2, 3, 3, 3, 3, 3,
2, 0, 0, 0, 0, 0, 2, 3, 3, 3, 3,
2, 0, 0, 0, 0, 0, 0, 2, 3, 3, 3,
2, 0, 0, 0, 0, 0, 0, 0, 2, 3, 3,
2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3,
2, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2,
2, 0, 0, 2, 0, 0, 2, 3, 3, 3, 3,
2, 0, 2, 3, 2, 0, 0, 2, 3, 3, 3,
2, 2, 3, 3, 2, 0, 0, 2, 3, 3, 3,
2, 3, 3, 3, 3, 2, 0, 0, 2, 3, 3,
3, 3, 3, 3, 3, 2, 0, 0, 2, 3, 3,
3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 3
};
static const byte macCursorBeam[] = {
0, 0, 3, 3, 3, 0, 0, 3, 3, 3, 3,
3, 3, 0, 3, 0, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 0, 3, 0, 3, 3, 3, 3, 3, 3,
0, 0, 3, 3, 3, 0, 0, 3, 3, 3, 3,
};
Gui::Gui() {
_scene = NULL;
_sceneDirty = true;
_screen.create(g_system->getWidth(), g_system->getHeight(), Graphics::PixelFormat::createFormatCLUT8());
Patterns p;
p.push_back(checkersPattern);
_scrollPos = 0;
_builtInFonts = false;
g_system->getPaletteManager()->setPalette(palette, 0, 4);
CursorMan.replaceCursorPalette(palette, 0, 4);
CursorMan.replaceCursor(macCursorArrow, 11, 16, 1, 1, 3);
_cursorIsArrow = true;
CursorMan.showMouse(true);
// Draw desktop
Common::Rect r(0, 0, _screen.w - 1, _screen.h - 1);
Design::drawFilledRoundRect(&_screen, r, kDesktopArc, kColorBlack, p, 1);
loadFonts();
}
Gui::~Gui() {
}
void Gui::setScene(Scene *scene) {
if (_scene != scene)
_sceneDirty = true;
_scene = scene;
}
void Gui::appendText(String &str) {
if (!str.contains('\n')) {
_out.push_back(str);
return;
}
// Okay, we got new lines, need to split it
// and push substrings individually
Common::String tmp = "";
for (int i = 0; i < str.size(); i++) {
if (str[i] == '\n') {
_out.push_back(tmp);
tmp = "";
continue;
}
tmp += str[i];
}
_out.push_back(tmp);
}
void Gui::draw() {
if (_scene != NULL && _sceneDirty) {
_scene->paint(&_screen, 0, kMenuHeight);
paintBorder(&_screen, 0, kMenuHeight, _scene->_design->getBounds()->width(), _scene->_design->getBounds()->height(),
kWindowScene);
_sceneDirty = false;
}
// Render console
int sceneW = _scene->_design->getBounds()->width();
int consoleW = _screen.w - sceneW - 2 * kBorderWidth;
int consoleH = _scene->_design->getBounds()->height() - 2 * kBorderWidth;
renderConsole(&_screen, sceneW + kBorderWidth, kMenuHeight + kBorderWidth, consoleW, consoleH);
paintBorder(&_screen, sceneW, kMenuHeight, _screen.w - sceneW, _scene->_design->getBounds()->height(),
kWindowConsole);
renderMenu();
// Blit to screen
g_system->copyRectToScreen(_screen.getPixels(), _screen.pitch, 0, 0, _screen.w, _screen.h);
}
void Gui::drawBox(Graphics::Surface *g, int x, int y, int w, int h) {
Common::Rect r(x, y, x + w + 1, y + h + 1);
g->fillRect(r, kColorWhite);
g->frameRect(r, kColorBlack);
}
void Gui::fillRect(Graphics::Surface *g, int x, int y, int w, int h) {
Common::Rect r(x, y, x + w, y + h);
g->fillRect(r, kColorBlack);
}
#define ARROW_W 12
#define ARROW_H 6
const int arrowPixels[ARROW_H][ARROW_W] = {
{0,0,0,0,0,1,1,0,0,0,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0},
{0,0,0,1,1,1,1,1,1,0,0,0},
{0,0,1,1,1,1,1,1,1,1,0,0},
{0,1,1,1,1,1,1,1,1,1,1,0},
{1,1,1,1,1,1,1,1,1,1,1,1}};
void Gui::paintBorder(Graphics::Surface *g, int x, int y, int width, int height, WindowType windowType) {
bool active, scrollable, closeable, closeBoxPressed, drawTitle;
switch (windowType) {
case kWindowScene:
active = false;
scrollable = false;
closeable = false;
closeBoxPressed = false;
drawTitle = true;
break;
case kWindowConsole:
active = true;
scrollable = true;
closeable = true;
closeBoxPressed = false;
drawTitle = false;
break;
}
const int size = kBorderWidth;
drawBox(g, x, y, size, size);
drawBox(g, x+width-size-1, y, size, size);
drawBox(g, x+width-size-1, y+height-size-1, size, size);
drawBox(g, x, y+height-size-1, size, size);
drawBox(g, x + size, y + 2, width - 2*size - 1, size - 4);
drawBox(g, x + size, y + height - size + 1, width - 2*size - 1, size - 4);
drawBox(g, x + 2, y + size, size - 4, height - 2*size - 1);
drawBox(g, x + width - size + 1, y + size, size - 4, height - 2*size-1);
if (active) {
fillRect(g, x + size, y + 5, width - 2*size - 1, 8);
fillRect(g, x + size, y + height - 13, width - 2*size - 1, 8);
fillRect(g, x + 5, y + size, 8, height - 2*size - 1);
if (!scrollable) {
fillRect(g, x + width - 13, y + size, 8, height - 2*size - 1);
} else {
int x1 = x + width - 15;
int y1 = y + size + 1;
for (int yy = 0; yy < ARROW_H; yy++) {
for (int xx = 0; xx < ARROW_W; xx++) {
if (arrowPixels[yy][xx] != 0) {
g->hLine(x1+xx, y1+yy, x1+xx, kColorBlack);
} else {
g->hLine(x1+xx, y1+yy, x1+xx, kColorWhite);
}
}
}
fillRect(g, x + width - 13, y + size + ARROW_H, 8, height - 2*size - 1 - ARROW_H*2);
y1 += height - 2*size - ARROW_H - 2;
for (int yy = 0; yy < ARROW_H; yy++) {
for (int xx = 0; xx < ARROW_W; xx++) {
if (arrowPixels[ARROW_H-yy-1][xx] != 0) {
g->hLine(x1+xx, y1+yy, x1+xx, kColorBlack);
} else {
g->hLine(x1+xx, y1+yy, x1+xx, kColorWhite);
}
}
}
}
if (closeable) {
if (closeBoxPressed) {
fillRect(g, x + 6, y + 6, 6, 6);
} else {
drawBox(g, x + 5, y + 5, 7, 7);
}
}
}
if (drawTitle) {
const Graphics::Font *font;
int yOff = 1;
if (!_builtInFonts) {
font = FontMan.getFontByName("Chicago-12");
if (!font)
warning("Cannot load font Chicago-12");
}
if (_builtInFonts || !font) {
font = FontMan.getFontByUsage(Graphics::FontManager::kBigGUIFont);
yOff = 3;
}
int w = font->getStringWidth(_scene->_name) + 6;
int maxWidth = width - size*2 - 7;
if (w > maxWidth)
w = maxWidth;
drawBox(g, x + (width - w) / 2, y, w, size);
font->drawString(g, _scene->_name, x + (width - w) / 2 + 3, y + yOff, w, kColorBlack);
}
}
enum {
kConWOverlap = 20,
kConHOverlap = 20,
kConWPadding = 2,
kConHPadding = 10,
kConOverscan = 3,
kLineSpacing = 0
};
void Gui::renderConsole(Graphics::Surface *g, int x, int y, int width, int height) {
bool fullRedraw = false;
bool textReflow = false;
int surfW = width + kConWOverlap * 2;
int surfH = height + kConHOverlap * 2;
Common::Rect boundsR(kConWOverlap - kConOverscan, kConHOverlap - kConOverscan,
width + kConWOverlap + kConOverscan, height + kConHOverlap + kConOverscan);
Common::Rect fullR(0, 0, surfW, surfH);
if (_console.w != surfW || _console.h != surfH) {
if (_console.w != surfW)
textReflow = true;
_console.free();
_console.create(surfW, surfH, Graphics::PixelFormat::createFormatCLUT8());
fullRedraw = true;
}
if (fullRedraw)
_console.fillRect(fullR, kColorWhite);
const Graphics::Font *font;
if (!_builtInFonts) {
char fontName[128];
snprintf(fontName, 128, "%s-%d", _scene->getFontName(), _scene->_fontSize);
font = FontMan.getFontByName(fontName);
if (!font)
warning("Cannot load font %s", fontName);
}
if (_builtInFonts || !font)
font = FontMan.getFontByUsage(Graphics::FontManager::kConsoleFont);
int lineHeight = font->getFontHeight() + kLineSpacing;
int textW = width - kConWPadding * 2;
int textH = height - kConHPadding * 2;
if (textReflow) {
_lines.clear();
for (int i = 0; i < _out.size(); i++) {
Common::StringArray wrappedLines;
font->wordWrapText(_out[i], textW, wrappedLines);
for (Common::StringArray::const_iterator j = wrappedLines.begin(); j != wrappedLines.end(); ++j)
_lines.push_back(*j);
}
}
const int firstLine = _scrollPos / lineHeight;
const int lastLine = MIN((_scrollPos + textH) / lineHeight + 1, _lines.size());
const int xOff = kConWOverlap;
const int yOff = kConHOverlap;
int x1 = xOff + kConWPadding;
int y1 = yOff - (_scrollPos % lineHeight) + kConHPadding;
for (int line = firstLine; line < lastLine; line++) {
const char *str = _lines[line].c_str();
if (*str)
font->drawString(&_console, _lines[line], x1, y1, textW, kColorBlack);
y1 += lineHeight;
}
_consoleTextArea.left = x;
_consoleTextArea.top = y;
_consoleTextArea.right = x + width;
_consoleTextArea.bottom = y + height;
g->copyRectToSurface(_console, x - kConOverscan, y - kConOverscan, boundsR);
}
void Gui::loadFonts() {
Common::Archive *dat;
dat = Common::makeZipArchive("wage.dat");
if (!dat) {
warning("Could not find wage.dat. Falling back to built-in fonts");
_builtInFonts = true;
}
Common::ArchiveMemberList list;
dat->listMembers(list);
for (Common::ArchiveMemberList::iterator it = list.begin(); it != list.end(); ++it) {
Common::SeekableReadStream *stream = dat->createReadStreamForMember((*it)->getName());
Graphics::BdfFont *font = Graphics::BdfFont::loadFont(*stream);
delete stream;
Common::String fontName = (*it)->getName();
// Trim the .bdf extension
for (int i = fontName.size() - 1; i >= 0; --i) {
if (fontName[i] == '.') {
while ((uint)i < fontName.size()) {
fontName.deleteLastChar();
}
break;
}
}
FontMan.assignFontToName(fontName, font);
debug(2, " %s", fontName.c_str());
}
_builtInFonts = false;
delete dat;
}
void Gui::mouseMove(int x, int y) {
if (_consoleTextArea.contains(x, y)) {
if (_cursorIsArrow) {
CursorMan.replaceCursor(macCursorBeam, 11, 16, 3, 8, 3);
_cursorIsArrow = false;
}
} else if (_cursorIsArrow == false) {
CursorMan.replaceCursor(macCursorArrow, 11, 16, 1, 1, 3);
_cursorIsArrow = true;
}
}
void Gui::renderMenu() {
Common::Rect r(0, 0, _screen.w - 1, kMenuHeight - 1);
Patterns p;
p.push_back(fillPattern);
Design::drawFilledRoundRect(&_screen, r, kDesktopArc, kColorWhite, p, 1);
r.top = 7;
Design::drawFilledRect(&_screen, r, kColorWhite, p, 1);
r.top = kMenuHeight - 1;
Design::drawFilledRect(&_screen, r, kColorBlack, p, 1);
}
} // End of namespace Wage
|