aboutsummaryrefslogtreecommitdiff
path: root/gui/console.cpp
blob: 0a7c249e081792f6bde6959bed01e3226fbbf651 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2002 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 "console.h"
#include "newgui.h"

#include "common/engine.h"

/*

 _   _           _                                 _                   _   _             
| | | |_ __   __| | ___ _ __    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_(_) ___  _ __  
| | | | '_ \ / _` |/ _ \ '__|  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __| |/ _ \| '_ \ 
| |_| | | | | (_| |  __/ |    | (_| (_) | | | \__ \ |_| |  | |_| | (__| |_| | (_) | | | |
 \___/|_| |_|\__,_|\___|_|     \___\___/|_| |_|___/\__|_|   \__,_|\___|\__|_|\___/|_| |_|
                                                                                         
This code is not finished, so please don't complain :-)

*/


#define PROMPT	"$ "

/* TODO:
 * - it is very inefficient to redraw the full thingy when just one char is added/removed.
 *   Instead, we could just copy the GFX of the blank console (i.e. after the transparent
 *   background is drawn, before any text is drawn). Then using that, it becomes trivial
 *   to erase a single character, do scrolling etc.
 * - add a scrollbar widget to allow scrolling in the history
 * - a *lot* of others things, this code is in no way complete and heavily under progress
 */
ConsoleDialog::ConsoleDialog(NewGui *gui)
	: Dialog(gui, 0, 0, 320, 6*kLineHeight+2)
{
	_lineWidth = (_w - 2) / kCharWidth;
	_linesPerPage = (_h - 2) / kLineHeight;

	memset(_buffer, ' ', kBufferSize);
	_linesInBuffer = kBufferSize / _lineWidth;
	
	_currentColumn = 0;
	_currentLine = 0;
	_scrollLine = _linesPerPage - 1;
	
	print("ScummVM "SCUMMVM_VERSION" (" SCUMMVM_CVS ")\n");
	print("Console is ready\n");
	
	print(PROMPT);
	_promptLine = _currentLine;


	_caretVisible = false;
	_caretTime = 0;
}

void ConsoleDialog::drawDialog()
{
	// Blend over the background
	_gui->blendRect(_x, _y, _w, _h, _gui->_bgcolor, 2);
	
	// Draw a border
	_gui->hline(_x, _y+_h-1, _x+_w-1, _gui->_color);

	// Draw text
	int start = _scrollLine - _linesPerPage + 1;
	int y = _y + 2;
	for (int line = 0; line < _linesPerPage; line++) {
		int x = _x + 1;
		for (int column = 0; column < _lineWidth; column++) {
			int l = (start+line) % _linesInBuffer;
			byte c = _buffer[l * _lineWidth + column];
			_gui->drawChar(c, x, y, _gui->_textcolor);
			x += kCharWidth;
		}
		y += kLineHeight;
	}

	// Finally blit it all to the screen
	_gui->addDirtyRect(_x, _y, _w, _h);
}

void ConsoleDialog::handleTickle()
{
	uint32 time = _gui->get_time();
	if (_caretTime < time) {
		_caretTime = time + kCaretBlinkTime;
		if (_caretVisible) {
			drawCaret(true);
		} else {
			drawCaret(false);
		}
	}
}

void ConsoleDialog::handleKeyDown(uint16 ascii, int keycode, int modifiers) {

	switch (keycode) {
		case '\n':	// enter/return
		case '\r':
			nextLine();
			print(PROMPT);
			_promptLine = _currentLine;
			
			if (_caretVisible)
				drawCaret(true);
			draw();
			break;
		case 27:	// escape
			close();
			break;
		case 8:		// backspace
			if (_currentColumn == 0 && _currentLine > _promptLine) {
				_currentColumn = _lineWidth - 1;
				_currentLine--;
			} else if (_currentColumn > 0) {
				if (_currentLine > _promptLine || _currentColumn >= (int)sizeof(PROMPT))
					_currentColumn--;
			}
			_buffer[(_currentLine % _linesInBuffer) * _lineWidth + _currentColumn] = ' ';

			if (_caretVisible)
				drawCaret(true);
			draw();	// FIXME - not nice to redraw the full console just for one char!
			break;
		default:
			if (ascii == '~' || ascii == '#') {
				close();
			} else if (isprint((char)ascii)) {
				putchar(ascii);
			}
	}
}

void ConsoleDialog::nextLine()
{
	_currentColumn = 0;
	if (_currentLine == _scrollLine)
		_scrollLine++;
	_currentLine++;
}

int ConsoleDialog::printf(const char *format, ...)
{
	va_list	argptr;

	va_start(argptr, format);
	int count = this->vprintf(format, argptr);
	va_end (argptr);
	return count;
}

int ConsoleDialog::vprintf(const char *format, va_list argptr)
{
	char	buf[2048];

	int count = vsnprintf(buf, sizeof(buf), format, argptr);
	print(buf);
	return count;
}

void ConsoleDialog::putchar(int c)
{
	if (c == '\n')
		nextLine();
	else {
		int pos = (_currentLine % _linesInBuffer) * _lineWidth + _currentColumn;
		_buffer[pos] = (char)c;
		_currentColumn++;
		if (_currentColumn >= _lineWidth)
			nextLine();
	}
	draw();	// FIXME - not nice to redraw the full console just for one char!
}

void ConsoleDialog::print(const char *str)
{
	int pos = (_currentLine % _linesInBuffer) * _lineWidth + _currentColumn;
	while (*str) {
		if (*str == '\n') {
			nextLine();
			pos += _lineWidth - _currentColumn;
		} else {
			_buffer[pos++] = *str;
			_currentColumn++;
			if (_currentColumn >= _lineWidth)
				nextLine();
		}
		pos %= kBufferSize;
		str++;
	}
	draw();
}

void ConsoleDialog::drawCaret(bool erase)
{
	// Only draw if item is visible
	if (!isVisible())
		return;
	
	int displayLine = _currentLine - _scrollLine + _linesPerPage - 1;

	if (displayLine < 0 || displayLine >= _linesPerPage)
		return;

	int x = _x + 1 + _currentColumn * kCharWidth;
	int y = _y + displayLine * kLineHeight;

	_gui->fillRect(x, y, kCharWidth, kLineHeight, erase ? _gui->_bgcolor : _gui->_textcolor);
	_gui->addDirtyRect(x, y, kCharWidth, kLineHeight);
	
	_caretVisible = !erase;
}