aboutsummaryrefslogtreecommitdiff
path: root/gui/walkthrough.cpp
blob: b3a7de5eac2bf4048464129d5c131471d21713f6 (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
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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2002-2004 The ScummVM project
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 */

#include "stdafx.h"

#include "common/file.h"

#include "graphics/font.h"

#include "gui/walkthrough.h"
#include "gui/ScrollBarWidget.h"

namespace GUI {

extern const Graphics::NewFont g_consolefont;

#define kConsoleCharWidth	(g_consolefont.getMaxCharWidth())
#define kConsoleLineHeight	(g_consolefont.getFontHeight() + 2)



WalkthroughDialog::WalkthroughDialog(float widthPercent, float heightPercent)
	: Dialog(0, 0, 1, 1),
		_initialized(false), _widthPercent(widthPercent), _heightPercent(heightPercent) {
	_gameName[0] = 0;
}

WalkthroughDialog::~WalkthroughDialog() {
	destroy();
}

static int getNextWordLength(byte *src, int maxLength) {
	int l;

	for (l = 0; l < maxLength; l++) {
		byte tmp = src[l];
		if ((tmp >= 0) && (tmp <= ' '))
			break;
	}

	return l;
}

bool WalkthroughDialog::loadWalkthroughText() {
	char filename[260];
	File file;

	sprintf(filename, "%s.wkt", _gameName);
	file.open(filename);
	if (!file.isOpen())
		return false;
	int bufferSize = file.size();
	byte *buffer = (byte *)malloc(bufferSize);
	file.read(buffer, bufferSize);
	file.close();

	_linesArray.clear();

	int currentLinePos = 0;
	byte *currentBuffer = buffer;
	byte *lineBuffer = (byte *)malloc(_lineWidth + 1);
	lineBuffer[0] = 0;

	for (;;) {
		if ((currentBuffer - buffer) >= bufferSize)
			break;
		int wordLength = getNextWordLength(currentBuffer, _lineWidth);
		if (((currentLinePos + wordLength) < _lineWidth) && 
				((*currentBuffer != 0x0a) && (*currentBuffer != 0x0d))) {
			if ((*currentBuffer >= 0) && (*currentBuffer <= ' ')) {
				lineBuffer[currentLinePos++] = ' ';
				currentBuffer++;
			} else {
				memcpy(lineBuffer + currentLinePos, currentBuffer, wordLength);
				currentLinePos += wordLength;
				currentBuffer += wordLength;
				if ((currentLinePos + 1) < _lineWidth) {
					lineBuffer[currentLinePos] = ' ';
					currentLinePos++;
				}
			}
		} else {
			if (*currentBuffer == 0x0d) {
				currentBuffer++;
				if ((*currentBuffer == 0x0a) && ((currentBuffer - buffer) < bufferSize)) {
					currentBuffer++;
				}
			} else if (*currentBuffer == 0x0a) {
				currentBuffer++;
			}
			lineBuffer[currentLinePos] = 0;
			Entry line;
			line.text = String((char *)lineBuffer);
			_linesArray.push_back(line);
			lineBuffer[0] = 0;
			currentLinePos = 0;
		}
	}

	free(buffer);
	free(lineBuffer);

	return true;
}

void WalkthroughDialog::setGameName(const char *gameName) {
	strcpy(_gameName, gameName);
}

void WalkthroughDialog::create() {
	if (_initialized)
		destroy();

	// Setup basic layout/dialog size
	reflowLayout();

	// Add scrollbar
	_scrollBar = new ScrollBarWidget(this, _w - kScrollBarWidth - 1, 0, kScrollBarWidth, _h);
	_scrollBar->setTarget(this);

	_currentPos = 0;
	_scrollLine = _linesPerPage - 1;

	_linesArray.clear();

	loadWalkthroughText();

	_initialized = true;
}

void WalkthroughDialog::destroy() {
	if (!_initialized)
		return;

	_linesArray.clear();

	_initialized = false;
}

void WalkthroughDialog::reflowLayout() {
	// Calculate the real width/height (rounded to char/line multiples)
	_w = (uint16)(_widthPercent * g_system->getOverlayWidth());
	_h = (uint16)((_heightPercent * g_system->getOverlayHeight() - 2) / kConsoleLineHeight);
	_h = _h * kConsoleLineHeight + 2;

	// Calculate depending values
	_lineWidth = (_w - kScrollBarWidth - 2) / kConsoleCharWidth;
	_linesPerPage = (_h - 2) / kConsoleLineHeight;
}

void WalkthroughDialog::open() {
	Dialog::open();
}

void WalkthroughDialog::drawDialog() {
	// Blend over the background
	g_gui.fillRect(_x, _y, _w, _h, g_gui._bgcolor);

	// Draw a border
	g_gui.hLine(_x, _y + _h - 1, _x + _w - 1, g_gui._color);

	// Draw text
	int y = _y + 2;

	for (int line = 0; (line < _linesPerPage) && ((_currentPos + line) < (int)_linesArray.size()); line++) {
		const char *text = _linesArray[line + _currentPos].text.c_str();
		int textLen = strlen(text);
		int x = _x + 1;
		for (int column = 0; (column < _lineWidth) && (column < textLen); column++) {
			byte c = text[column];
			g_gui.drawChar(c, x, y, g_gui._textcolor, &g_consolefont);
			x += kConsoleCharWidth;
		}
		y += kConsoleLineHeight;
	}

	// Draw the scrollbar
	_scrollBar->_numEntries = _linesArray.size();
	_scrollBar->_currentPos = _currentPos;
	_scrollBar->_entriesPerPage = _linesPerPage;
	_scrollBar->recalc();
	_scrollBar->draw();

	// Finally blit it all to the screen
	g_gui.addDirtyRect(_x, _y, _w, _h);
}


void WalkthroughDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
	switch (cmd) {
	case kSetPositionCmd:
		_currentPos = _scrollBar->_currentPos;
		drawDialog();
		break;
	default:
		return;
	}
}

void WalkthroughDialog::handleMouseWheel(int x, int y, int direction) {
	_scrollBar->handleMouseWheel(x, y, direction);
}

void WalkthroughDialog::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
	if ((modifiers == OSystem::KBD_CTRL) && (keycode == 'w')) {
		close();
		return;
	}
	switch (keycode) {
	case 256 + 17:	// up arrow
		_currentPos--;
		break;
	case 256 + 18:	// down arrow
		_currentPos++;
		break;
	case 256 + 22:	// home
		_currentPos = 0;
		break;
	case 256 + 23:	// end
		_currentPos = _linesArray.size() - _linesPerPage;
		break;
	case 256 + 24:	// page up
		_currentPos -= _linesPerPage;
		break;
	case 256 + 25:	// page down
		_currentPos += _linesPerPage;
		break;
	default:
		return;
	}
	if (_currentPos < 0) {
		_currentPos = 0;
	}
	if ((_currentPos + _linesPerPage) >= (int)_linesArray.size()) {
		_currentPos = _linesArray.size() - _linesPerPage;
	}

	drawDialog();
}


} // End of namespace GUI