forked from OpenXcom/OpenXcom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText.cpp
More file actions
378 lines (341 loc) · 7.28 KB
/
Copy pathText.cpp
File metadata and controls
378 lines (341 loc) · 7.28 KB
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
/*
* Copyright 2010 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom 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.
*
* OpenXcom 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 OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Text.h"
#include <sstream>
#include "../Engine/Font.h"
namespace OpenXcom
{
/**
* Sets up a blank text with the specified size and position.
* @param width Width in pixels.
* @param height Height in pixels.
* @param x X position in pixels.
* @param y Y position in pixels.
*/
Text::Text(int width, int height, int x, int y) : Surface(width, height, x, y), _big(0), _small(0), _font(0), _text(L""), _wrap(false), _invert(false), _align(ALIGN_LEFT), _valign(ALIGN_TOP), _color(0)
{
}
/**
*
*/
Text::~Text()
{
}
/**
* Takes an integer value and formats it as currency,
* spacing the thousands and adding a $ sign to the front.
* @param funds The funding value.
* @return The formatted string.
*/
std::wstring Text::formatFunding(int funds)
{
std::wstringstream ss;
ss << funds;
std::wstring s = ss.str();
size_t spacer = s.size() - 3;
while (spacer > 0 && spacer < s.size())
{
s.insert(spacer, L" ");
spacer -= 3;
}
s.insert(0, L"$");
return s;
}
/**
* Changes the text to use the big-size font.
*/
void Text::setBig()
{
_font = _big;
}
/**
* Changes the text to use the small-size font.
*/
void Text::setSmall()
{
_font = _small;
}
/**
* Returns the font currently used by the text.
* @return Pointer to font.
*/
Font *const Text::getFont() const
{
return _font;
}
/**
* Changes the various fonts for the text to use.
* The different fonts need to be passed in advance since the
* text size can change mid-text.
* @param big Pointer to large-size font.
* @param small Pointer to small-size font.
*/
void Text::setFonts(Font *big, Font *small)
{
_big = big;
_small = small;
_font = _small;
}
/**
* Changes the string displayed on screen.
* @param text Text string.
*/
void Text::setText(const std::wstring &text)
{
_text = text;
processText();
}
/**
* Returns the string displayed on screen.
* @return Text string.
*/
std::wstring Text::getText() const
{
return _text;
}
/**
* Enables/disables text wordwrapping. When enabled, lines of
* text are automatically split to ensure they stay within the
* drawing area, otherwise they simply go off the edge.
* @param wrap Wordwrapping setting.
*/
void Text::setWordWrap(bool wrap)
{
if (wrap != _wrap)
{
_wrap = wrap;
processText();
}
}
/**
* Enables/disables color inverting. Mostly used to make
* button text look pressed along with the button.
* @param invert Invert setting.
*/
void Text::setInvert(bool invert)
{
_invert = invert;
draw();
}
/**
* Changes the way the text is aligned horizontally
* relative to the drawing area.
* @param align Horizontal alignment.
*/
void Text::setAlign(TextHAlign align)
{
_align = align;
draw();
}
/**
* Changes the way the text is aligned vertically
* relative to the drawing area.
* @param valign Vertical alignment.
*/
void Text::setVerticalAlign(TextVAlign valign)
{
_valign = valign;
draw();
}
/**
* Changes the color used to render the text. Unlike regular graphics,
* fonts are greyscale so they need to be assigned a specific position
* in the palette to be displayed.
* @param color Color value.
*/
void Text::setColor(Uint8 color)
{
_color = color;
draw();
}
/**
* Returns the color used to render the text.
* @return Color value.
*/
Uint8 Text::getColor() const
{
return _color;
}
/**
* Returns the rendered text's height. Useful to check if wordwrap applies.
* @return height value.
*/
int Text::getTextHeight()
{
int height = 0;
for (std::vector<int>::iterator i = _lineHeight.begin(); i != _lineHeight.end(); i++)
{
height += *i;
}
return height;
}
/**
* Takes care of any text post-processing like calculating
* line metrics for alignment and wordwrapping if necessary.
*/
void Text::processText()
{
if (_font == 0)
{
return;
}
std::wstring *s = &_text;
// Use a separate string for wordwrapping text
if (_wrap)
{
_wrappedText = _text;
s = &_wrappedText;
}
_lineWidth.clear();
_lineHeight.clear();
int width = 0, word = 0;
std::wstring::iterator space = s->begin();
Font *font = _font;
// Go through the text character by character
for (std::wstring::iterator c = s->begin(); c <= s->end(); c++)
{
// End of the line
if (c == s->end() || *c == '\n' || *c == 2)
{
// Add line measurements for alignment later
_lineWidth.push_back(width);
_lineHeight.push_back(font->getHeight() + font->getSpacing());
width = 0;
word = 0;
if (c == s->end())
break;
else if (*c == 2)
font = _small;
}
// Keep track of spaces for wordwrapping
else if (*c == ' ')
{
space = c;
width += font->getWidth() / 2;
word = 0;
}
// Keep track of the width of the last line and word
else
{
width += font->getChar(*c)->getCrop()->w + font->getSpacing();
word += font->getChar(*c)->getCrop()->w + font->getSpacing();
}
// Wordwrap if the last word doesn't fit the line
if (_wrap && width > getWidth())
{
// Go back to the last space and put a linebreak there
*space = '\n';
c = space - 1;
width -= word + font->getWidth() / 2;
}
}
draw();
}
/**
* Draws all the characters in the text with a really
* nasty complex gritty text rendering algorithm logic stuff.
*/
void Text::draw()
{
clear();
if (_text.empty() || _font == 0)
{
return;
}
int x = 0, y = 0, line = 0, height = 0, pos = 0;
Font *font = _font;
std::wstring *s = &_text;
for (std::vector<int>::iterator i = _lineHeight.begin(); i != _lineHeight.end(); i++)
{
height += *i;
}
switch (_valign)
{
case ALIGN_TOP:
y = 0;
break;
case ALIGN_MIDDLE:
y = (getHeight() - height) / 2;
break;
case ALIGN_BOTTOM:
y = getHeight() - height;
break;
}
switch (_align)
{
case ALIGN_LEFT:
x = 0;
break;
case ALIGN_CENTER:
x = (getWidth() - _lineWidth[line]) / 2;
break;
case ALIGN_RIGHT:
x = getWidth() - _lineWidth[line];
break;
}
if (_wrap)
s = &_wrappedText;
// Draw each letter one by one
for (std::wstring::iterator c = s->begin(); c != s->end(); c++)
{
if (*c == ' ')
{
// panther: trim spaces on line begins
if (pos>0)
{
x += font->getWidth() / 2;
}
}
else if (*c == '\n' || *c == 2)
{
line++;
pos = 0;
y += font->getHeight() + font->getSpacing();
switch (_align)
{
case ALIGN_LEFT:
x = 0;
break;
case ALIGN_CENTER:
x = (getWidth() - _lineWidth[line]) / 2;
break;
case ALIGN_RIGHT:
x = getWidth() - _lineWidth[line];
break;
}
if (*c == 2)
font = _small;
}
else
{
Surface* chr = font->getChar(*c);
chr->setX(x);
chr->setY(y);
chr->blit(this);
x += chr->getCrop()->w + font->getSpacing();
pos += 1;
}
}
this->offset(_color);
if (_invert)
{
this->invert(_color + 3);
}
}
}