2002-12-14 14:31:44 +00:00
|
|
|
/* 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"
|
2002-12-14 20:04:46 +00:00
|
|
|
#include "ScrollBarWidget.h"
|
2002-12-14 14:31:44 +00:00
|
|
|
|
|
|
|
#include "common/engine.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
_ _ _ _ _ _
|
|
|
|
| | | |_ __ __| | ___ _ __ ___ ___ _ __ ___| |_ _ __ _ _ ___| |_(_) ___ _ __
|
|
|
|
| | | | '_ \ / _` |/ _ \ '__| / __/ _ \| '_ \/ __| __| '__| | | |/ __| __| |/ _ \| '_ \
|
|
|
|
| |_| | | | | (_| | __/ | | (_| (_) | | | \__ \ |_| | | |_| | (__| |_| | (_) | | | |
|
|
|
|
\___/|_| |_|\__,_|\___|_| \___\___/|_| |_|___/\__|_| \__,_|\___|\__|_|\___/|_| |_|
|
|
|
|
|
|
|
|
This code is not finished, so please don't complain :-)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2002-12-14 18:57:15 +00:00
|
|
|
|
|
|
|
#define PROMPT "$ "
|
|
|
|
|
2002-12-14 14:31:44 +00:00
|
|
|
/* 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)
|
2002-12-14 16:08:37 +00:00
|
|
|
: Dialog(gui, 0, 0, 320, 6*kLineHeight+2)
|
2002-12-14 14:31:44 +00:00
|
|
|
{
|
2002-12-14 20:04:46 +00:00
|
|
|
_lineWidth = (_w - kScrollBarWidth - 2) / kCharWidth;
|
2002-12-14 14:31:44 +00:00
|
|
|
_linesPerPage = (_h - 2) / kLineHeight;
|
|
|
|
|
|
|
|
memset(_buffer, ' ', kBufferSize);
|
|
|
|
_linesInBuffer = kBufferSize / _lineWidth;
|
|
|
|
|
2002-12-14 21:37:40 +00:00
|
|
|
_currentPos = 0;
|
2002-12-14 18:57:15 +00:00
|
|
|
_scrollLine = _linesPerPage - 1;
|
2002-12-14 15:45:45 +00:00
|
|
|
|
2002-12-14 20:04:46 +00:00
|
|
|
_caretVisible = false;
|
|
|
|
_caretTime = 0;
|
|
|
|
|
|
|
|
// Add scrollbar
|
|
|
|
_scrollBar = new ScrollBarWidget(this, _w - kScrollBarWidth - 1, 0, kScrollBarWidth, _h);
|
|
|
|
_scrollBar->setTarget(this);
|
|
|
|
|
|
|
|
// Display greetings & prompt
|
2002-12-14 15:45:45 +00:00
|
|
|
print("ScummVM "SCUMMVM_VERSION" (" SCUMMVM_CVS ")\n");
|
|
|
|
print("Console is ready\n");
|
2002-12-14 18:57:15 +00:00
|
|
|
|
|
|
|
print(PROMPT);
|
2002-12-14 21:37:40 +00:00
|
|
|
_promptStartPos = _promptEndPos = _currentPos;
|
2002-12-14 14:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDialog::drawDialog()
|
|
|
|
{
|
2002-12-14 16:08:37 +00:00
|
|
|
// Blend over the background
|
|
|
|
_gui->blendRect(_x, _y, _w, _h, _gui->_bgcolor, 2);
|
2002-12-14 14:31:44 +00:00
|
|
|
|
2002-12-14 16:08:37 +00:00
|
|
|
// Draw a border
|
|
|
|
_gui->hline(_x, _y+_h-1, _x+_w-1, _gui->_color);
|
2002-12-14 14:31:44 +00:00
|
|
|
|
|
|
|
// Draw text
|
|
|
|
int start = _scrollLine - _linesPerPage + 1;
|
2002-12-14 18:57:15 +00:00
|
|
|
int y = _y + 2;
|
2002-12-14 14:31:44 +00:00
|
|
|
for (int line = 0; line < _linesPerPage; line++) {
|
|
|
|
int x = _x + 1;
|
|
|
|
for (int column = 0; column < _lineWidth; column++) {
|
2002-12-14 15:45:45 +00:00
|
|
|
int l = (start+line) % _linesInBuffer;
|
2002-12-14 14:31:44 +00:00
|
|
|
byte c = _buffer[l * _lineWidth + column];
|
|
|
|
_gui->drawChar(c, x, y, _gui->_textcolor);
|
|
|
|
x += kCharWidth;
|
|
|
|
}
|
|
|
|
y += kLineHeight;
|
|
|
|
}
|
|
|
|
|
2002-12-14 20:04:46 +00:00
|
|
|
// Draw the scrollbar
|
|
|
|
_scrollBar->draw();
|
|
|
|
|
2002-12-14 16:08:37 +00:00
|
|
|
// Finally blit it all to the screen
|
2002-12-14 14:31:44 +00:00
|
|
|
_gui->addDirtyRect(_x, _y, _w, _h);
|
|
|
|
}
|
|
|
|
|
2002-12-14 18:57:15 +00:00
|
|
|
void ConsoleDialog::handleTickle()
|
|
|
|
{
|
|
|
|
uint32 time = _gui->get_time();
|
|
|
|
if (_caretTime < time) {
|
|
|
|
_caretTime = time + kCaretBlinkTime;
|
|
|
|
if (_caretVisible) {
|
|
|
|
drawCaret(true);
|
|
|
|
} else {
|
|
|
|
drawCaret(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-14 20:04:46 +00:00
|
|
|
void ConsoleDialog::handleMouseWheel(int x, int y, int direction)
|
|
|
|
{
|
|
|
|
_scrollBar->handleMouseWheel(x, y, direction);
|
|
|
|
}
|
|
|
|
|
2002-12-14 21:37:40 +00:00
|
|
|
void ConsoleDialog::handleKeyDown(uint16 ascii, int keycode, int modifiers)
|
|
|
|
{
|
2002-12-14 22:18:00 +00:00
|
|
|
int i;
|
|
|
|
|
2002-12-14 18:57:15 +00:00
|
|
|
switch (keycode) {
|
|
|
|
case '\n': // enter/return
|
2002-12-14 22:18:00 +00:00
|
|
|
case '\r': {
|
2002-12-14 21:57:30 +00:00
|
|
|
if (_caretVisible)
|
|
|
|
drawCaret(true);
|
2002-12-14 22:18:00 +00:00
|
|
|
|
2002-12-14 18:57:15 +00:00
|
|
|
nextLine();
|
2002-12-14 22:18:00 +00:00
|
|
|
|
|
|
|
int len = _promptEndPos - _promptStartPos;
|
|
|
|
char str[len + 1];
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
str[i] = _buffer[(_promptStartPos + i) % kBufferSize];
|
|
|
|
str[len] = 0;
|
|
|
|
printf("You entered '%s'\n", str);
|
|
|
|
|
2002-12-14 18:57:15 +00:00
|
|
|
print(PROMPT);
|
2002-12-14 21:37:40 +00:00
|
|
|
_promptStartPos = _promptEndPos = _currentPos;
|
2002-12-14 18:57:15 +00:00
|
|
|
|
|
|
|
draw();
|
|
|
|
break;
|
2002-12-14 22:18:00 +00:00
|
|
|
}
|
2002-12-14 18:57:15 +00:00
|
|
|
case 27: // escape
|
|
|
|
close();
|
|
|
|
break;
|
|
|
|
case 8: // backspace
|
2002-12-14 21:57:30 +00:00
|
|
|
if (_caretVisible)
|
|
|
|
drawCaret(true);
|
|
|
|
|
2002-12-14 21:37:40 +00:00
|
|
|
if (_currentPos > _promptStartPos) {
|
|
|
|
_currentPos--;
|
2002-12-14 22:18:00 +00:00
|
|
|
for (i = _currentPos; i < _promptEndPos; i++)
|
2002-12-14 21:37:40 +00:00
|
|
|
_buffer[i % kBufferSize] = _buffer[(i+1) % kBufferSize];
|
|
|
|
_buffer[_promptEndPos % kBufferSize] = ' ';
|
|
|
|
_promptEndPos--;
|
2002-12-14 18:57:15 +00:00
|
|
|
}
|
2002-12-14 22:10:37 +00:00
|
|
|
scrollToCurrent();
|
2002-12-14 18:57:15 +00:00
|
|
|
draw(); // FIXME - not nice to redraw the full console just for one char!
|
|
|
|
break;
|
2002-12-14 20:04:46 +00:00
|
|
|
/*
|
|
|
|
case 256+24: // pageup
|
|
|
|
_selectedItem -= _entriesPerPage - 1;
|
|
|
|
if (_selectedItem < 0)
|
|
|
|
_selectedItem = 0;
|
|
|
|
break;
|
|
|
|
case 256+25: // pagedown
|
|
|
|
_selectedItem += _entriesPerPage - 1;
|
|
|
|
if (_selectedItem >= _list.size() )
|
|
|
|
_selectedItem = _list.size() - 1;
|
|
|
|
break;
|
|
|
|
*/
|
|
|
|
case 256+22: // home
|
|
|
|
_scrollLine = _linesPerPage - 1; // FIXME - this is not correct after a wrap around
|
|
|
|
updateScrollBar();
|
|
|
|
draw();
|
|
|
|
break;
|
|
|
|
case 256+23: // end
|
2002-12-14 21:37:40 +00:00
|
|
|
_scrollLine = _currentPos / _lineWidth;
|
2002-12-14 20:04:46 +00:00
|
|
|
updateScrollBar();
|
|
|
|
draw();
|
|
|
|
break;
|
2002-12-14 21:37:40 +00:00
|
|
|
case 275: // cursor right
|
|
|
|
if (_currentPos < _promptEndPos)
|
|
|
|
_currentPos++;
|
|
|
|
draw();
|
|
|
|
break;
|
|
|
|
case 276: // cursor left
|
|
|
|
if (_currentPos > _promptStartPos)
|
|
|
|
_currentPos--;
|
|
|
|
draw();
|
|
|
|
break;
|
|
|
|
|
2002-12-14 18:57:15 +00:00
|
|
|
default:
|
|
|
|
if (ascii == '~' || ascii == '#') {
|
|
|
|
close();
|
2002-12-14 21:57:30 +00:00
|
|
|
} else if (modifiers == OSystem::KBD_CTRL) {
|
2002-12-14 21:37:40 +00:00
|
|
|
specialKeys(keycode);
|
2002-12-14 18:57:15 +00:00
|
|
|
} else if (isprint((char)ascii)) {
|
2002-12-14 22:18:00 +00:00
|
|
|
for (i = _promptEndPos-1; i >= _currentPos; i--)
|
2002-12-14 21:37:40 +00:00
|
|
|
_buffer[(i+1) % kBufferSize] = _buffer[i % kBufferSize];
|
|
|
|
_promptEndPos++;
|
2002-12-14 21:57:30 +00:00
|
|
|
putchar((char)ascii);
|
2002-12-14 22:10:37 +00:00
|
|
|
scrollToCurrent();
|
2002-12-14 18:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-14 20:04:46 +00:00
|
|
|
void ConsoleDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data)
|
|
|
|
{
|
|
|
|
switch (cmd) {
|
|
|
|
case kSetPositionCmd:
|
|
|
|
int newPos = (int)data + _linesPerPage - 1;
|
|
|
|
if (newPos != _scrollLine) {
|
|
|
|
_scrollLine = newPos;
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-14 21:37:40 +00:00
|
|
|
void ConsoleDialog::specialKeys(int keycode)
|
|
|
|
{
|
|
|
|
switch (keycode) {
|
|
|
|
case 'a':
|
|
|
|
_currentPos = _promptStartPos;
|
|
|
|
draw();
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
_currentPos = _promptEndPos;
|
|
|
|
draw();
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
killLine();
|
|
|
|
draw();
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
killLastWord();
|
|
|
|
draw();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDialog::killLine()
|
|
|
|
{
|
|
|
|
for (int i = _currentPos; i < _promptEndPos; i++)
|
|
|
|
_buffer[i % kBufferSize] = ' ';
|
|
|
|
_promptEndPos = _currentPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDialog::killLastWord()
|
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
while (_currentPos > _promptStartPos) {
|
|
|
|
_currentPos--;
|
|
|
|
pos = getBufferPos();
|
|
|
|
if (_buffer[pos] != ' ')
|
|
|
|
_buffer[pos] = ' ';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-14 14:31:44 +00:00
|
|
|
void ConsoleDialog::nextLine()
|
|
|
|
{
|
2002-12-14 21:37:40 +00:00
|
|
|
int line = _currentPos / _lineWidth;
|
|
|
|
if (line == _scrollLine)
|
2002-12-14 15:45:45 +00:00
|
|
|
_scrollLine++;
|
2002-12-14 21:37:40 +00:00
|
|
|
_currentPos = (line + 1) * _lineWidth;
|
2002-12-14 20:04:46 +00:00
|
|
|
|
|
|
|
updateScrollBar();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDialog::updateScrollBar()
|
|
|
|
{
|
2002-12-14 21:37:40 +00:00
|
|
|
int line = _currentPos / _lineWidth;
|
|
|
|
_scrollBar->_numEntries = (line < _linesInBuffer) ? line + 1 : _linesInBuffer;
|
|
|
|
_scrollBar->_currentPos = _scrollBar->_numEntries - (line - _scrollLine + _linesPerPage);
|
2002-12-14 20:04:46 +00:00
|
|
|
_scrollBar->_entriesPerPage = _linesPerPage;
|
|
|
|
_scrollBar->recalc();
|
2002-12-14 15:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2002-12-14 21:57:30 +00:00
|
|
|
{
|
|
|
|
if (_caretVisible)
|
|
|
|
drawCaret(true);
|
|
|
|
|
|
|
|
putcharIntern(c);
|
|
|
|
|
|
|
|
draw(); // FIXME - not nice to redraw the full console just for one char!
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDialog::putcharIntern(int c)
|
2002-12-14 15:45:45 +00:00
|
|
|
{
|
|
|
|
if (c == '\n')
|
|
|
|
nextLine();
|
|
|
|
else {
|
2002-12-14 21:37:40 +00:00
|
|
|
_buffer[getBufferPos()] = (char)c;
|
|
|
|
_currentPos++;
|
|
|
|
if ((_scrollLine + 1) * _lineWidth == _currentPos) {
|
|
|
|
_scrollLine++;
|
|
|
|
updateScrollBar();
|
|
|
|
}
|
2002-12-14 15:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDialog::print(const char *str)
|
|
|
|
{
|
2002-12-14 21:57:30 +00:00
|
|
|
if (_caretVisible)
|
|
|
|
drawCaret(true);
|
|
|
|
|
2002-12-14 21:37:40 +00:00
|
|
|
while (*str)
|
2002-12-14 21:57:30 +00:00
|
|
|
putcharIntern(*str++);
|
|
|
|
|
|
|
|
draw();
|
2002-12-14 14:31:44 +00:00
|
|
|
}
|
|
|
|
|
2002-12-14 18:57:15 +00:00
|
|
|
void ConsoleDialog::drawCaret(bool erase)
|
|
|
|
{
|
2002-12-14 21:37:40 +00:00
|
|
|
int line = _currentPos / _lineWidth;
|
|
|
|
int displayLine = line - _scrollLine + _linesPerPage - 1;
|
2002-12-14 18:57:15 +00:00
|
|
|
|
2002-12-14 21:57:30 +00:00
|
|
|
// Only draw caret if visible
|
|
|
|
if (!isVisible() || displayLine < 0 || displayLine >= _linesPerPage) {
|
|
|
|
_caretVisible = false;
|
2002-12-14 18:57:15 +00:00
|
|
|
return;
|
2002-12-14 21:57:30 +00:00
|
|
|
}
|
2002-12-14 18:57:15 +00:00
|
|
|
|
2002-12-14 21:37:40 +00:00
|
|
|
int x = _x + 1 + (_currentPos % _lineWidth) * kCharWidth;
|
2002-12-14 18:57:15 +00:00
|
|
|
int y = _y + displayLine * kLineHeight;
|
|
|
|
|
2002-12-14 21:57:30 +00:00
|
|
|
char c = _buffer[getBufferPos()];
|
|
|
|
if (erase) {
|
|
|
|
_gui->fillRect(x, y, kCharWidth, kLineHeight, _gui->_bgcolor);
|
|
|
|
_gui->drawChar(c, x, y+2, _gui->_textcolor);
|
|
|
|
} else {
|
|
|
|
_gui->fillRect(x, y, kCharWidth, kLineHeight, _gui->_textcolor);
|
|
|
|
_gui->drawChar(c, x, y+2, _gui->_bgcolor);
|
|
|
|
}
|
2002-12-14 18:57:15 +00:00
|
|
|
_gui->addDirtyRect(x, y, kCharWidth, kLineHeight);
|
|
|
|
|
|
|
|
_caretVisible = !erase;
|
2002-12-14 14:31:44 +00:00
|
|
|
}
|
2002-12-14 22:10:37 +00:00
|
|
|
|
|
|
|
void ConsoleDialog::scrollToCurrent()
|
|
|
|
{
|
|
|
|
int line = _currentPos / _lineWidth;
|
|
|
|
int displayLine = line - _scrollLine + _linesPerPage - 1;
|
|
|
|
|
|
|
|
if (displayLine < 0) {
|
|
|
|
// TODO - this should only occur for loong edit lines, though
|
|
|
|
} else if (displayLine >= _linesPerPage) {
|
|
|
|
_scrollLine = _currentPos / _lineWidth;
|
|
|
|
updateScrollBar();
|
|
|
|
}
|
|
|
|
}
|