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
|
/* 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/timer.h"
#include "common/unzip.h"
#include "graphics/cursorman.h"
#include "graphics/fonts/bdf.h"
#include "graphics/palette.h"
#include "wage/wage.h"
#include "wage/design.h"
#include "wage/entities.h"
#include "wage/menu.h"
#include "wage/gui.h"
#include "wage/world.h"
namespace Wage {
const Graphics::Font *Gui::getConsoleFont() {
char fontName[128];
Scene *scene = _engine->_world->_player->_currentScene;
snprintf(fontName, 128, "%s-%d", scene->getFontName(), scene->_fontSize);
return getFont(fontName, Graphics::FontManager::kConsoleFont);
}
void Gui::clearOutput() {
_out.clear();
_lines.clear();
_consoleFullRedraw = true;
}
void Gui::appendText(const char *s) {
Common::String str(s);
_consoleDirty = true;
if (!str.contains('\n')) {
_out.push_back(str);
flowText(str);
return;
}
// Okay, we got new lines, need to split it
// and push substrings individually
Common::String tmp;
for (uint i = 0; i < str.size(); i++) {
if (str[i] == '\n') {
_out.push_back(tmp);
flowText(tmp);
tmp.clear();
continue;
}
tmp += str[i];
}
_out.push_back(tmp);
flowText(tmp);
}
enum {
kConWOverlap = 20,
kConHOverlap = 20,
kConWPadding = 3,
kConHPadding = 4,
kConOverscan = 3
};
void Gui::flowText(Common::String &str) {
Common::StringArray wrappedLines;
int textW = _consoleTextArea.width() - kConWPadding * 2;
const Graphics::Font *font = getConsoleFont();
font->wordWrapText(str, textW, wrappedLines);
if (wrappedLines.empty()) // Sometimes we have empty lines
_lines.push_back("");
for (Common::StringArray::const_iterator j = wrappedLines.begin(); j != wrappedLines.end(); ++j)
_lines.push_back(*j);
uint pos = _scrollPos;
_scrollPos = MAX<int>(0, (_lines.size() - 1 - _consoleNumLines) * _consoleLineHeight);
_cursorX = kConWPadding;
if (_scrollPos)
_cursorY = (_consoleNumLines) * _consoleLineHeight + kConHPadding;
else
_cursorY = (_lines.size() - 1) * _consoleLineHeight + kConHPadding;
if (pos != _scrollPos)
_consoleFullRedraw = true;
if (!_engine->_temporarilyHidden)
draw();
}
void Gui::renderConsole(Graphics::Surface *g, Common::Rect &r) {
bool fullRedraw = _consoleFullRedraw;
bool textReflow = false;
int surfW = r.width() + kConWOverlap * 2;
int surfH = r.height() + kConHOverlap * 2;
Common::Rect boundsR(kConWOverlap - kConOverscan, kConHOverlap - kConOverscan,
r.width() + kConWOverlap + kConOverscan, r.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 = getConsoleFont();
_consoleLineHeight = font->getFontHeight();
int textW = r.width() - kConWPadding * 2;
int textH = r.height() - kConHPadding * 2;
if (textReflow) {
_lines.clear();
for (uint i = 0; i < _out.size(); i++)
flowText(_out[i]);
}
const int firstLine = _scrollPos / _consoleLineHeight;
const int lastLine = MIN((_scrollPos + textH) / _consoleLineHeight + 1, _lines.size());
const int xOff = kConWOverlap;
const int yOff = kConHOverlap;
int x1 = xOff + kConWPadding;
int y1 = yOff - (_scrollPos % _consoleLineHeight) + kConHPadding;
if (fullRedraw)
_consoleNumLines = (r.height() - 2 * kConWPadding) / _consoleLineHeight - 2;
for (int line = firstLine; line < lastLine; line++) {
const char *str = _lines[line].c_str();
int color = kColorBlack;
if ((line > _selectionStartY && line < _selectionEndY) ||
(line > _selectionEndY && line < _selectionStartY)) {
color = kColorWhite;
Common::Rect trect(0, y1, _console.w, y1 + _consoleLineHeight);
Design::drawFilledRect(&_console, trect, kColorBlack, _patterns, kPatternSolid);
}
if (line == _selectionStartY || line == _selectionEndY) {
if (_selectionStartY != _selectionEndY) {
int color1 = kColorBlack;
int color2 = kColorWhite;
int midpoint = _selectionStartX;
if (_selectionStartY > _selectionEndY)
SWAP(color1, color2);
if (line == _selectionEndY) {
SWAP(color1, color2);
midpoint = _selectionEndX;
}
Common::String beg(_lines[line].c_str(), &_lines[line].c_str()[midpoint]);
Common::String end(&_lines[line].c_str()[midpoint]);
int rectW = font->getStringWidth(beg) + kConWPadding + kConWOverlap;
Common::Rect trect(0, y1, _console.w, y1 + _consoleLineHeight);
if (color1 == kColorWhite)
trect.right = rectW;
else
trect.left = rectW;
Design::drawFilledRect(&_console, trect, kColorBlack, _patterns, kPatternSolid);
font->drawString(&_console, beg, x1, y1, textW, color1);
font->drawString(&_console, end, x1 + rectW - kConWPadding - kConWOverlap, y1, textW, color2);
} else {
int startPos = _selectionStartX;
int endPos = _selectionEndX;
if (startPos > endPos)
SWAP(startPos, endPos);
Common::String beg(_lines[line].c_str(), &_lines[line].c_str()[startPos]);
Common::String mid(&_lines[line].c_str()[startPos], &_lines[line].c_str()[endPos]);
Common::String end(&_lines[line].c_str()[endPos]);
int rectW1 = font->getStringWidth(beg) + kConWPadding + kConWOverlap;
int rectW2 = rectW1 + font->getStringWidth(mid);
Common::Rect trect(rectW1, y1, rectW2, y1 + _consoleLineHeight);
Design::drawFilledRect(&_console, trect, kColorBlack, _patterns, kPatternSolid);
font->drawString(&_console, beg, x1, y1, textW, kColorBlack);
font->drawString(&_console, mid, x1 + rectW1 - kConWPadding - kConWOverlap, y1, textW, kColorWhite);
font->drawString(&_console, end, x1 + rectW2 - kConWPadding - kConWOverlap, y1, textW, kColorBlack);
}
} else {
if (*str)
font->drawString(&_console, _lines[line], x1, y1, textW, color);
}
y1 += _consoleLineHeight;
}
// Now we need to clip it to the screen
int xcon = r.left - kConOverscan;
int ycon = r.top - kConOverscan;
if (xcon < 0) {
boundsR.left -= xcon;
xcon = 0;
}
if (ycon < 0) {
boundsR.top -= ycon;
ycon = 0;
}
if (xcon + boundsR.width() >= g->w)
boundsR.right -= xcon + boundsR.width() - g->w;
if (ycon + boundsR.height() >= g->h)
boundsR.bottom -= ycon + boundsR.height() - g->h;
Common::Rect rr(r);
if (rr.right > _screen.w - 1)
rr.right = _screen.w - 1;
if (rr.bottom > _screen.h - 1)
rr.bottom = _screen.h - 1;
g->copyRectToSurface(_console, xcon, ycon, boundsR);
g_system->copyRectToScreen(g->getBasePtr(rr.left, rr.top), g->pitch, rr.left, rr.top, rr.width(), rr.height());
}
void Gui::drawInput() {
if (!_screen.getPixels())
return;
if (_sceneIsActive) {
_sceneIsActive = false;
_bordersDirty = true;
}
_out.pop_back();
_lines.pop_back();
appendText(_engine->_inputText.c_str());
_inputTextLineNum = _out.size() - 1;
const Graphics::Font *font = getConsoleFont();
if (_engine->_inputText.contains('\n')) {
_consoleDirty = true;
} else {
int x = kConWPadding + _consoleTextArea.left;
int y = _cursorY + _consoleTextArea.top;
Common::Rect r(x, y, x + _consoleTextArea.width() - kConWPadding, y + font->getFontHeight());
_screen.fillRect(r, kColorWhite);
undrawCursor();
font->drawString(&_screen, _out[_inputTextLineNum], x, y, _screen.w, kColorBlack);
g_system->copyRectToScreen(_screen.getBasePtr(x, y), _screen.pitch, x, y, _consoleTextArea.width(), font->getFontHeight());
}
_cursorX = font->getStringWidth(_out[_inputTextLineNum]) + kConHPadding;
}
void Gui::actionCopy() {
if (_selectionStartX == -1)
return;
int startX = _selectionStartX;
int startY = _selectionStartY;
int endX = _selectionEndX;
int endY = _selectionEndY;
if (startY > endY) {
SWAP(startX, endX);
SWAP(endX, endY);
}
_clipboard.clear();
for (int i = startY; i <= endY; i++) {
if (startY == endY) {
_clipboard = Common::String(&_lines[i].c_str()[startX], &_lines[i].c_str()[endX]);
break;
}
if (i == startY) {
_clipboard += &_lines[i].c_str()[startX];
_clipboard += '\n';
} else if (i == endY) {
_clipboard += Common::String(_lines[i].c_str(), &_lines[i].c_str()[endX]);
} else {
_clipboard += _lines[i];
_clipboard += '\n';
}
}
_menu->enableCommand(kMenuEdit, kMenuActionPaste, true);
}
void Gui::actionPaste() {
_undobuffer = _engine->_inputText;
_engine->_inputText += _clipboard;
drawInput();
_engine->_inputText = _out.back(); // Set last part of the multiline text
_menu->enableCommand(kMenuEdit, kMenuActionUndo, true);
}
void Gui::actionUndo() {
_engine->_inputText = _undobuffer;
drawInput();
_menu->enableCommand(kMenuEdit, kMenuActionUndo, false);
}
void Gui::actionClear() {
int startPos = _selectionStartX;
int endPos = _selectionEndX;
if (startPos > endPos)
SWAP(startPos, endPos);
Common::String beg(_lines[_selectionStartY].c_str(), &_lines[_selectionStartY].c_str()[startPos]);
Common::String end(&_lines[_selectionStartY].c_str()[endPos]);
_undobuffer = _engine->_inputText;
_engine->_inputText = beg + end;
drawInput();
_menu->enableCommand(kMenuEdit, kMenuActionUndo, true);
_selectionStartY = -1;
_selectionEndY = -1;
}
void Gui::actionCut() {
int startPos = _selectionStartX;
int endPos = _selectionEndX;
if (startPos > endPos)
SWAP(startPos, endPos);
Common::String beg(_lines[_selectionStartY].c_str(), &_lines[_selectionStartY].c_str()[startPos]);
Common::String mid(&_lines[_selectionStartY].c_str()[startPos], &_lines[_selectionStartY].c_str()[endPos]);
Common::String end(&_lines[_selectionStartY].c_str()[endPos]);
_undobuffer = _engine->_inputText;
_engine->_inputText = beg + end;
_clipboard = mid;
drawInput();
_menu->enableCommand(kMenuEdit, kMenuActionUndo, true);
_menu->enableCommand(kMenuEdit, kMenuActionPaste, true);
_selectionStartY = -1;
_selectionEndY = -1;
}
void Gui::disableUndo() {
_menu->enableCommand(kMenuEdit, kMenuActionUndo, false);
}
void Gui::disableAllMenus() {
_menu->disableAllMenus();
}
void Gui::enableNewGameMenus() {
_menu->enableCommand(kMenuFile, kMenuActionNew, true);
_menu->enableCommand(kMenuFile, kMenuActionOpen, true);
_menu->enableCommand(kMenuFile, kMenuActionQuit, true);
}
} // End of namespace Wage
|