improved caret

svn-id: r5966
This commit is contained in:
Max Horn 2002-12-14 21:57:30 +00:00
parent 3af3741562
commit 09e8c84ca3
2 changed files with 39 additions and 21 deletions

View file

@ -20,7 +20,6 @@
#include "stdafx.h" #include "stdafx.h"
#include "console.h" #include "console.h"
#include "newgui.h"
#include "ScrollBarWidget.h" #include "ScrollBarWidget.h"
#include "common/engine.h" #include "common/engine.h"
@ -127,18 +126,22 @@ void ConsoleDialog::handleKeyDown(uint16 ascii, int keycode, int modifiers)
switch (keycode) { switch (keycode) {
case '\n': // enter/return case '\n': // enter/return
case '\r': case '\r':
if (_caretVisible)
drawCaret(true);
nextLine(); nextLine();
print(PROMPT); print(PROMPT);
_promptStartPos = _promptEndPos = _currentPos; _promptStartPos = _promptEndPos = _currentPos;
if (_caretVisible)
drawCaret(true);
draw(); draw();
break; break;
case 27: // escape case 27: // escape
close(); close();
break; break;
case 8: // backspace case 8: // backspace
if (_caretVisible)
drawCaret(true);
if (_currentPos > _promptStartPos) { if (_currentPos > _promptStartPos) {
_currentPos--; _currentPos--;
for (int i = _currentPos; i < _promptEndPos; i++) for (int i = _currentPos; i < _promptEndPos; i++)
@ -146,8 +149,6 @@ void ConsoleDialog::handleKeyDown(uint16 ascii, int keycode, int modifiers)
_buffer[_promptEndPos % kBufferSize] = ' '; _buffer[_promptEndPos % kBufferSize] = ' ';
_promptEndPos--; _promptEndPos--;
} }
if (_caretVisible)
drawCaret(true);
draw(); // FIXME - not nice to redraw the full console just for one char! draw(); // FIXME - not nice to redraw the full console just for one char!
break; break;
/* /*
@ -186,19 +187,13 @@ void ConsoleDialog::handleKeyDown(uint16 ascii, int keycode, int modifiers)
default: default:
if (ascii == '~' || ascii == '#') { if (ascii == '~' || ascii == '#') {
close(); close();
} else if (modifiers == OSystem::KBD_CTRL) { // CTRL } else if (modifiers == OSystem::KBD_CTRL) {
specialKeys(keycode); specialKeys(keycode);
} else if (isprint((char)ascii)) { } else if (isprint((char)ascii)) {
for (int i = _promptEndPos-1; i >= _currentPos; i--) for (int i = _promptEndPos-1; i >= _currentPos; i--)
_buffer[(i+1) % kBufferSize] = _buffer[i % kBufferSize]; _buffer[(i+1) % kBufferSize] = _buffer[i % kBufferSize];
_buffer[_currentPos % kBufferSize] = (char)ascii;
_currentPos++;
_promptEndPos++; _promptEndPos++;
if ((_scrollLine + 1) * _lineWidth == _currentPos) { putchar((char)ascii);
_scrollLine++;
updateScrollBar();
}
draw(); // FIXME - not nice to redraw the full console just for one char!
} }
} }
} }
@ -297,6 +292,16 @@ int ConsoleDialog::vprintf(const char *format, va_list argptr)
} }
void ConsoleDialog::putchar(int c) void ConsoleDialog::putchar(int c)
{
if (_caretVisible)
drawCaret(true);
putcharIntern(c);
draw(); // FIXME - not nice to redraw the full console just for one char!
}
void ConsoleDialog::putcharIntern(int c)
{ {
if (c == '\n') if (c == '\n')
nextLine(); nextLine();
@ -308,31 +313,41 @@ void ConsoleDialog::putchar(int c)
updateScrollBar(); updateScrollBar();
} }
} }
draw(); // FIXME - not nice to redraw the full console just for one char!
} }
void ConsoleDialog::print(const char *str) void ConsoleDialog::print(const char *str)
{ {
if (_caretVisible)
drawCaret(true);
while (*str) while (*str)
putchar(*str++); putcharIntern(*str++);
draw();
} }
void ConsoleDialog::drawCaret(bool erase) void ConsoleDialog::drawCaret(bool erase)
{ {
// Only draw if item is visible
if (!isVisible())
return;
int line = _currentPos / _lineWidth; int line = _currentPos / _lineWidth;
int displayLine = line - _scrollLine + _linesPerPage - 1; int displayLine = line - _scrollLine + _linesPerPage - 1;
if (displayLine < 0 || displayLine >= _linesPerPage) // Only draw caret if visible
if (!isVisible() || displayLine < 0 || displayLine >= _linesPerPage) {
_caretVisible = false;
return; return;
}
int x = _x + 1 + (_currentPos % _lineWidth) * kCharWidth; int x = _x + 1 + (_currentPos % _lineWidth) * kCharWidth;
int y = _y + displayLine * kLineHeight; int y = _y + displayLine * kLineHeight;
_gui->fillRect(x, y, kCharWidth, kLineHeight, erase ? _gui->_bgcolor : _gui->_textcolor); 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);
}
_gui->addDirtyRect(x, y, kCharWidth, kLineHeight); _gui->addDirtyRect(x, y, kCharWidth, kLineHeight);
_caretVisible = !erase; _caretVisible = !erase;

View file

@ -22,6 +22,7 @@
#define CONSOLE_DIALOG_H #define CONSOLE_DIALOG_H
#include "dialog.h" #include "dialog.h"
#include "newgui.h"
#include "common/str.h" #include "common/str.h"
#include "common/list.h" #include "common/list.h"
@ -52,6 +53,7 @@ protected:
bool _caretVisible; bool _caretVisible;
uint32 _caretTime; uint32 _caretTime;
byte _caretBuf[kLineHeight * kCharWidth * 2];
ScrollBarWidget *_scrollBar; ScrollBarWidget *_scrollBar;
@ -72,6 +74,7 @@ public:
protected: protected:
void drawCaret(bool erase); void drawCaret(bool erase);
void putcharIntern(int c);
void print(const char *str); void print(const char *str);
void nextLine(); void nextLine();
void updateScrollBar(); void updateScrollBar();