aboutsummaryrefslogtreecommitdiff
path: root/engines/zvision/scripting/effects/ttytext_effect.cpp
blob: 8d340dae395eff510739e5492a8babbe04506457 (plain)
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
/* 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.
 *
 */

#include "common/scummsys.h"

#include "zvision/scripting/effects/ttytext_effect.h"

#include "zvision/zvision.h"
#include "zvision/scripting/script_manager.h"
#include "zvision/graphics/render_manager.h"
#include "zvision/text/text.h"

#include "common/stream.h"
#include "common/file.h"

namespace ZVision {

ttyTextNode::ttyTextNode(ZVision *engine, uint32 key, const Common::String &file, const Common::Rect &r, int32 delay) :
	ScriptingEffect(engine, key, SCRIPTING_EFFECT_TTYTXT),
	_fnt(engine) {
	_delay = delay;
	_r = r;
	_txtpos = 0;
	_nexttime = 0;
	_dx = 0;
	_dy = 0;

	Common::File *infile = _engine->getSearchManager()->openFile(file);
	if (infile) {
		while (!infile->eos()) {
			Common::String asciiLine = readWideLine(*infile);
			if (asciiLine.empty()) {
				continue;
			}
			_txtbuf += asciiLine;
		}

		delete infile;
	}
	_img.create(_r.width(), _r.height(), _engine->_resourcePixelFormat);
	_state._sharp = true;
	_state.readAllStyles(_txtbuf);
	_state.updateFontWithTextState(_fnt);
	_engine->getScriptManager()->setStateValue(_key, 1);
}

ttyTextNode::~ttyTextNode() {
	_engine->getScriptManager()->setStateValue(_key, 2);
	_img.free();
}

bool ttyTextNode::process(uint32 deltaTimeInMillis) {
	_nexttime -= deltaTimeInMillis;

	if (_nexttime < 0) {
		if (_txtpos < _txtbuf.size()) {
			if (_txtbuf[_txtpos] == '<') {
				int32 start = _txtpos;
				int32 end = 0;
				int16 ret = 0;
				while (_txtbuf[_txtpos] != '>' && _txtpos < _txtbuf.size())
					_txtpos++;
				end = _txtpos;
				if (start != -1) {
					if ((end - start - 1) > 0) {
						ret = _state.parseStyle(_txtbuf.c_str() + start + 1, end - start - 1);
					}
				}

				if (ret & (TEXT_CHANGE_FONT_TYPE | TEXT_CHANGE_FONT_STYLE)) {
					_state.updateFontWithTextState(_fnt);
				} else if (ret & TEXT_CHANGE_NEWLINE) {
					newline();
				}

				if (ret & TEXT_CHANGE_HAS_STATE_BOX) {
					Common::String buf;
					buf = Common::String::format("%d", _engine->getScriptManager()->getStateValue(_state._statebox));

					for (uint8 j = 0; j < buf.size(); j++)
						outchar(buf[j]);
				}

				_txtpos++;
			} else {
				int8 charsz = getUtf8CharSize(_txtbuf[_txtpos]);

				uint16 chr = readUtf8Char(_txtbuf.c_str() + _txtpos);

				if (chr == ' ') {
					uint32 i = _txtpos + charsz;
					uint16 width = _fnt.getCharWidth(chr);

					while (i < _txtbuf.size() && _txtbuf[i] != ' ' && _txtbuf[i] != '<') {

						int8 chsz   = getUtf8CharSize(_txtbuf[i]);
						uint16 uchr = readUtf8Char(_txtbuf.c_str() + _txtpos);

						width += _fnt.getCharWidth(uchr);

						i += chsz;
					}

					if (_dx + width > _r.width())
						newline();
					else
						outchar(chr);
				} else
					outchar(chr);

				_txtpos += charsz;
			}
			_nexttime = _delay;
			_engine->getRenderManager()->blitSurfaceToBkg(_img, _r.left, _r.top);
		} else
			return stop();
	}

	return false;
}

void ttyTextNode::scroll() {
	int32 scrl = 0;
	while (_dy - scrl > _r.height() - _fnt.getFontHeight())
		scrl += _fnt.getFontHeight();
	int8 *pixels = (int8 *)_img.getPixels();
	for (uint16 h = scrl; h < _img.h; h++)
		memcpy(pixels + _img.pitch * (h - scrl), pixels + _img.pitch * h, _img.pitch);

	_img.fillRect(Common::Rect(0, _img.h - scrl, _img.w, _img.h), 0);
	_dy -= scrl;
}

void ttyTextNode::newline() {
	_dy += _fnt.getFontHeight();
	_dx = 0;
}

void ttyTextNode::outchar(uint16 chr) {
	uint32 clr = _engine->_resourcePixelFormat.RGBToColor(_state._red, _state._green, _state._blue);

	if (_dx + _fnt.getCharWidth(chr) > _r.width())
		newline();

	if (_dy + _fnt.getFontHeight() >= _r.height())
		scroll();

	_fnt.drawChar(&_img, chr, _dx, _dy, clr);

	_dx += _fnt.getCharWidth(chr);
}

} // End of namespace ZVision