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
|
/* ResidualVM - A 3D game interpreter
*
* ResidualVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* 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.
*
*/
#include "engines/grim/debug.h"
#include "engines/grim/grim.h"
#include "engines/grim/textobject.h"
#include "engines/grim/savegame.h"
#include "engines/grim/lua.h"
#include "engines/grim/font.h"
#include "engines/grim/gfx_base.h"
#include "engines/grim/color.h"
namespace Grim {
TextObjectCommon::TextObjectCommon() :
_x(0), _y(0), _fgColor(0), _justify(0), _width(0), _height(0),
_font(nullptr), _duration(0), _layer(0) {
if (g_grim)
g_grim->invalidateTextObjectsSortOrder();
}
void TextObjectCommon::setLayer(int layer) {
_layer = layer;
if (g_grim)
g_grim->invalidateTextObjectsSortOrder();
}
TextObject::TextObject() :
TextObjectCommon(), _numberLines(1), _textID(""), _elapsedTime(0),
_maxLineWidth(0), _lines(nullptr), _userData(nullptr), _created(false),
_blastDraw(false), _isSpeech(false), _stackLevel(0) {
}
TextObject::~TextObject() {
delete[] _lines;
if (_created) {
g_driver->destroyTextObject(this);
}
if (g_grim)
g_grim->invalidateTextObjectsSortOrder();
}
void TextObject::setText(const Common::String &text, bool delaySetup) {
destroy();
_textID = text;
if (!delaySetup)
setupText();
}
void TextObject::reset() {
destroy();
setupText();
}
void TextObject::saveState(SaveGame *state) const {
state->writeColor(_fgColor);
state->writeLESint32(_x);
state->writeLESint32(_y);
state->writeLESint32(_width);
state->writeLESint32(_height);
state->writeLESint32(_justify);
state->writeLESint32(_numberLines);
state->writeLESint32(_duration);
state->writeBool(_blastDraw);
state->writeBool(_isSpeech);
state->writeLESint32(_elapsedTime);
if (_font) {
state->writeLESint32(_font->getId());
} else {
state->writeLESint32(-1);
}
state->writeString(_textID);
if (g_grim->getGameType() == GType_MONKEY4) {
state->writeLESint32(_layer);
state->writeLESint32(_stackLevel);
}
}
bool TextObject::restoreState(SaveGame *state) {
_fgColor = state->readColor();
_x = state->readLESint32();
_y = state->readLESint32();
_width = state->readLESint32();
_height = state->readLESint32();
_justify = state->readLESint32();
_numberLines = state->readLESint32();
_duration = state->readLESint32();
_blastDraw = state->readBool();
_isSpeech = state->readBool();
_elapsedTime = state->readLESint32();
int32 fontId = state->readLESint32();
if (fontId == -1) {
_font = nullptr;
} else {
_font = Font::getPool().getObject(fontId);
}
_textID = state->readString();
if (g_grim->getGameType() == GType_MONKEY4) {
_layer = state->readLESint32();
_stackLevel = state->readLESint32();
g_grim->invalidateTextObjectsSortOrder();
}
setupText();
_created = false;
_userData = nullptr;
return true;
}
void TextObject::setDefaults(const TextObjectDefaults *defaults) {
_x = defaults->getX();
_y = defaults->getY();
_font = defaults->getFont();
_fgColor = defaults->getFGColor();
_justify = defaults->getJustify();
}
int TextObject::getBitmapWidth() const {
return _maxLineWidth;
}
int TextObject::getBitmapHeight() const {
return _numberLines * _font->getKernedHeight();
}
int TextObject::getTextCharPosition(int pos) {
int width = 0;
Common::String msg = LuaBase::instance()->parseMsgText(_textID.c_str(), nullptr);
for (int i = 0; (msg[i] != '\0') && (i < pos); ++i) {
width += _font->getCharKernedWidth(msg[i]);
}
return width;
}
void TextObject::destroy() {
if (_created) {
g_driver->destroyTextObject(this);
_created = false;
}
}
void TextObject::setupText() {
Common::String msg = LuaBase::instance()->parseMsgText(_textID.c_str(), nullptr);
Common::String message;
// remove spaces (NULL_TEXT) from the end of the string,
// while this helps make the string unique it screws up
// text justification
// remove char of id 13 from the end of the string,
int pos = msg.size() - 1;
while (pos >= 0 && (msg[pos] == ' ' || msg[pos] == 13)) {
msg.deleteLastChar();
pos = msg.size() - 1;
}
delete[] _lines;
if (msg.size() == 0) {
_lines = nullptr;
return;
}
// format the output message to incorporate line wrapping
// (if necessary) for the text object
const int SCREEN_WIDTH = _width ? _width : 640;
const int SCREEN_MARGIN = SCREEN_WIDTH / 10;
// If the speaker is too close to the edge of the screen we have to make
// some room for the subtitles.
if (_isSpeech) {
if (_x < SCREEN_MARGIN) {
_x = SCREEN_MARGIN;
} else if (SCREEN_WIDTH - _x < SCREEN_MARGIN) {
_x = SCREEN_WIDTH - SCREEN_MARGIN;
}
}
// The maximum width for any line of text is determined by the justification
// mode. Note that there are no left/right margins -- this is consistent
// with GrimE.
int maxWidth = 0;
if (_justify == CENTER) {
maxWidth = 2 * MIN(_x, SCREEN_WIDTH - _x);
} else if (_justify == LJUSTIFY) {
maxWidth = SCREEN_WIDTH - _x;
} else if (_justify == RJUSTIFY) {
maxWidth = _x;
}
// We break the message to lines not longer than maxWidth
Common::String currLine;
_numberLines = 1;
int lineWidth = 0;
for (uint i = 0; i < msg.size(); i++) {
message += msg[i];
currLine += msg[i];
lineWidth += _font->getCharKernedWidth(msg[i]);
if (currLine.size() > 1 && lineWidth > maxWidth) {
if (currLine.contains(' ')) {
while (currLine.lastChar() != ' ' && currLine.size() > 1) {
lineWidth -= _font->getCharKernedWidth(currLine.lastChar());
message.deleteLastChar();
currLine.deleteLastChar();
--i;
}
} else { // if it is a unique word
int dashWidth = _font->getCharKernedWidth('-');
while (lineWidth + dashWidth > maxWidth && currLine.size() > 1) {
lineWidth -= _font->getCharKernedWidth(currLine.lastChar());
message.deleteLastChar();
currLine.deleteLastChar();
--i;
}
message += '-';
}
message += '\n';
currLine.clear();
_numberLines++;
lineWidth = 0;
}
}
// If the text object is a speech subtitle, the y parameter is the
// coordinate of the bottom of the text block (instead of the top). It means
// that every extra line pushes the previous lines up, instead of being
// printed further down the screen.
const int SCREEN_TOP_MARGIN = _font->getKernedHeight();
if (_isSpeech) {
_y -= _numberLines * _font->getKernedHeight();
if (_y < SCREEN_TOP_MARGIN) {
_y = SCREEN_TOP_MARGIN;
}
}
_lines = new Common::String[_numberLines];
for (int j = 0; j < _numberLines; j++) {
int nextLinePos, cutLen;
const char *breakPos = strchr(message.c_str(), '\n');
if (breakPos) {
nextLinePos = breakPos - message.c_str();
cutLen = nextLinePos + 1;
} else {
nextLinePos = message.size();
cutLen = nextLinePos;
}
Common::String currentLine(message.c_str(), message.c_str() + nextLinePos);
_lines[j] = currentLine;
int width = _font->getKernedStringLength(currentLine);
if (width > _maxLineWidth)
_maxLineWidth = width;
for (int count = 0; count < cutLen; count++)
message.deleteChar(0);
}
_elapsedTime = 0;
}
int TextObject::getLineX(int line) const {
int x = _x;
if (_justify == CENTER)
x = _x - (_font->getKernedStringLength(_lines[line]) / 2);
else if (_justify == RJUSTIFY)
x = _x - getBitmapWidth();
if (x < 0)
x = 0;
return x;
}
int TextObject::getLineY(int line) const {
int y = _y;
if (g_grim->getGameType() == GType_GRIM) {
if (_blastDraw) { // special case for Grim for menu text draw, issue #1083
y = _y + 5;
} else {
/* if (_font->getKernedHeight() == 21) // talk_font,verb_font
y = _y - 6;
else if (_font->getKernedHeight() == 26) // special_font
y = _y - 12;
else */if (_font->getKernedHeight() == 13) // computer_font
y = _y - 6;/*
else if (_font->getKernedHeight() == 19) // pt_font
y = _y - 9;*/
else
y = _y;
}
}
if (y < 0)
y = 0;
y += _font->getKernedHeight() * line;
return y;
}
void TextObject::draw() {
if (!_lines)
return;
if (!_created) {
g_driver->createTextObject(this);
_created = true;
}
if (_justify > 3 || _justify < 0)
warning("TextObject::draw: Unknown justification code (%d)", _justify);
g_driver->drawTextObject(this);
}
void TextObject::update() {
if (!_duration || !_created) {
return;
}
_elapsedTime += g_grim->getFrameTime();
if (_elapsedTime > _duration) {
delete this;
}
}
} // end of namespace Grim
|