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
|
/* 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(0), you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation(0), 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(0), 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(0), if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "titanic/pet_control/pet_text.h"
namespace Titanic {
CPetText::CPetText(uint count) :
_stringsMerged(false), _maxCharsPerLine(-1), _lineCount(0),
_fontNumber1(-1), _field3C(0), _field40(0), _field44(0),
_backR(0xff), _backG(0xff), _backB(0xff),
_textR(0), _textG(0), _textB(200),
_fontNumber2(0), _field64(0), _field68(0), _field6C(0),
_hasBorder(true), _scrollTop(0), _textCursor(nullptr), _field7C(0) {
setupArrays(count);
}
void CPetText::setupArrays(int count) {
freeArrays();
if (count < 10 || count > 60)
count = 10;
_array.resize(count);
}
void CPetText::freeArrays() {
_array.clear();
}
void CPetText::setup() {
for (int idx = 0; idx < (int)_array.size(); ++idx) {
_array[idx]._line.clear();
setLineColor(idx, _textR, _textG, _textB);
_array[idx]._string3.clear();
}
_lineCount = 0;
_stringsMerged = false;
}
void CPetText::setLineColor(uint lineNum, uint col) {
setLineColor(lineNum, col & 0xff, (col >> 16) & 0xff, (col >> 8) & 0xff);
}
void CPetText::setLineColor(uint lineNum, byte r, byte g, byte b) {
char buffer[6];
if (!r)
r = 1;
if (!g)
g = 1;
if (!b)
b = 1;
buffer[0] = TEXTCMD_SET_COLOR;
buffer[1] = r;
buffer[2] = g;
buffer[3] = b;
buffer[4] = TEXTCMD_SET_COLOR;
buffer[5] = '\0';
_array[lineNum]._rgb = buffer;
}
void CPetText::load(SimpleFile *file, int param) {
if (!param) {
int var1 = file->readNumber();
int var2 = file->readNumber();
uint count = file->readNumber();
_bounds.left = file->readNumber();
_bounds.top = file->readNumber();
_bounds.right = file->readNumber();
_bounds.bottom = file->readNumber();
_field3C = file->readNumber();
_field40 = file->readNumber();
_field44 = file->readNumber();
_backR = file->readNumber();
_backG = file->readNumber();
_backB = file->readNumber();
_textR = file->readNumber();
_textG = file->readNumber();
_textB = file->readNumber();
_hasBorder = file->readNumber() != 0;
_scrollTop = file->readNumber();
warning("TODO: CPetText::load %d,%d", var1, var2);
assert(_array.size() >= count);
for (uint idx = 0; idx < count; ++idx) {
_array[idx]._line = file->readString();
_array[idx]._rgb = file->readString();
_array[idx]._string3 = file->readString();
}
}
}
void CPetText::draw(CScreenManager *screenManager) {
Rect tempRect = _bounds;
if (_hasBorder) {
// Create border effect
// Top edge
tempRect.bottom = tempRect.top + 1;
screenManager->fillRect(SURFACE_BACKBUFFER, &tempRect, _backR, _backG, _backB);
// Bottom edge
tempRect.top = _bounds.bottom - 1;
tempRect.bottom = _bounds.bottom;
screenManager->fillRect(SURFACE_BACKBUFFER, &tempRect, _backR, _backG, _backB);
// Left edge
tempRect = _bounds;
tempRect.right = tempRect.left + 1;
screenManager->fillRect(SURFACE_BACKBUFFER, &tempRect, _backR, _backG, _backB);
// Right edge
tempRect = _bounds;
tempRect.left = tempRect.right - 1;
screenManager->fillRect(SURFACE_BACKBUFFER, &tempRect, _backR, _backG, _backB);
}
getTextHeight(screenManager);
tempRect = _bounds;
tempRect.grow(-2);
screenManager->setFontNumber(_fontNumber2);
screenManager->writeString(SURFACE_BACKBUFFER, tempRect, _scrollTop, _lines, _textCursor);
screenManager->setFontNumber(_fontNumber1);
}
void CPetText::mergeStrings() {
if (!_stringsMerged) {
_lines.clear();
for (int idx = 0; idx <= _lineCount; ++idx) {
CString line = _array[idx]._rgb + _array[idx]._string3 +
_array[idx]._line + "\n";
_lines += line;
}
_stringsMerged = true;
}
}
void CPetText::resize(uint count) {
if (!count || _array.size() == count)
return;
_array.clear();
_array.resize(count);
}
CString CPetText::getText() const {
CString result = "";
for (int idx = 0; idx < _lineCount; ++idx)
result += _array[idx]._line;
return result;
}
void CPetText::setText(const CString &str) {
setup();
appendText(str);
}
void CPetText::appendText(const CString &str) {
int lineSize = _array[_lineCount]._line.size();
int strSize = str.size();
if (_maxCharsPerLine == -1) {
// No limit on horizontal characters, so append string to current line
_array[_lineCount]._line += str;
} else if ((lineSize + strSize) <= _maxCharsPerLine) {
// New string fits into line, so add it on
_array[_lineCount]._line += str;
} else {
// Only add part of the str up to the maximum allowed limit for line
_array[_lineCount]._line += str.left(_maxCharsPerLine - lineSize);
}
updateStr3(_lineCount);
_stringsMerged = false;
}
void CPetText::setColor(uint col) {
_textR = col & 0xff;
_textG = (col >> 8) & 0xff;
_textB = (col >> 16) & 0xff;
}
void CPetText::setColor(byte r, byte g, byte b) {
_textR = r;
_textG = g;
_textB = b;
}
void CPetText::setMaxCharsPerLine(int maxChars) {
if (maxChars >= -1 && maxChars < 257)
_maxCharsPerLine = maxChars;
}
void CPetText::updateStr3(int lineNum) {
if (_field64 > 0 && _field68 > 0) {
char line[5];
line[0] = line[3] = TEXTCMD_26;
line[1] = _field64;
line[2] = _field68;
line[4] = '\0';
_array[lineNum]._string3 = CString(line);
_stringsMerged = false;
_field64 = _field68 = 0;
}
}
int CPetText::getTextHeight(CScreenManager *screenManager) {
mergeStrings();
int oldFontNumber = screenManager->setFontNumber(_fontNumber2);
int textHeight = screenManager->getTextBounds(_lines, _bounds.width());
screenManager->setFontNumber(oldFontNumber);
return textHeight;
}
void CPetText::deleteLastChar() {
if (!_array[_lineCount]._line.empty()) {
_array[_lineCount]._line.deleteLastChar();
_stringsMerged = false;
}
}
void CPetText::setNPC(int val1, int npcId) {
_field64 = val1;
_field68 = npcId;
}
void CPetText::scrollUp(CScreenManager *screenManager) {
int oldFontNumber = screenManager->setFontNumber(_fontNumber2);
_scrollTop -= screenManager->getFontHeight();
constrainScrollUp(screenManager);
screenManager->setFontNumber(oldFontNumber);
}
void CPetText::scrollDown(CScreenManager *screenManager) {
int oldFontNumber = screenManager->setFontNumber(_fontNumber2);
_scrollTop += screenManager->getFontHeight();
constrainScrollDown(screenManager);
screenManager->setFontNumber(oldFontNumber);
}
void CPetText::scrollToBottom(CScreenManager *screenManager) {
int oldFontNumber = screenManager->setFontNumber(_fontNumber2);
_scrollTop = _bounds.height();
constrainScrollDown(screenManager);
screenManager->setFontNumber(oldFontNumber);
}
void CPetText::constrainScrollUp(CScreenManager *screenManager) {
if (_scrollTop < 0)
_scrollTop = 0;
}
void CPetText::constrainScrollDown(CScreenManager *screenManager) {
// Figure out the maximum scroll amount allowed
int maxScroll = _bounds.height() - getTextHeight(screenManager) - 4;
if (maxScroll < 0)
maxScroll = 0;
if (_scrollTop > maxScroll)
_scrollTop = maxScroll;
}
void CPetText::addLine(const CString &str, uint color) {
addLine(str, color & 0xff, (color >> 8) & 0xff,
(color >> 16) & 0xff);
}
void CPetText::addLine(const CString &str, byte r, byte g, byte b) {
if (_lineCount == ((int)_array.size() - 1)) {
// Lines array is full
if (_array.size() > 1) {
// Delete the oldest line, and add a new entry at the end
_array.remove_at(0);
_array.resize(_array.size() + 1);
}
--_lineCount;
}
setLineColor(_lineCount, r, g, b);
appendText(str);
++_lineCount;
}
bool CPetText::handleKey(const Common::KeyState &keyState) {
switch (keyState.keycode) {
case Common::KEYCODE_BACKSPACE:
deleteLastChar();
break;
case Common::KEYCODE_RETURN:
case Common::KEYCODE_KP_ENTER:
return true;
default:
if (keyState.ascii >= 32 && keyState.ascii <= 127)
appendText(CString(keyState.ascii, 1));
break;
}
return false;
}
} // End of namespace Titanic
|