This commit is contained in:
Wolfgang Thaller 2012-04-06 01:46:12 +02:00
parent 26b18b2f4c
commit 1430d6d7e2

View File

@ -7,125 +7,125 @@ Console *Console::currentInstance = NULL;
Console::Console(GrafPtr port, Rect r) Console::Console(GrafPtr port, Rect r)
: consolePort(port), bounds(r) : consolePort(port), bounds(r)
{ {
PortSetter setport(consolePort); PortSetter setport(consolePort);
InsetRect(&bounds, 2,2); InsetRect(&bounds, 2,2);
TextFont(9); TextFont(9);
TextSize(9); TextSize(9);
cellSizeY = 10; cellSizeY = 10;
cellSizeX = CharWidth('M'); cellSizeX = CharWidth('M');
rows = (bounds.bottom - bounds.top) / cellSizeY; rows = (bounds.bottom - bounds.top) / cellSizeY;
cols = (bounds.right - bounds.left) / cellSizeX; cols = (bounds.right - bounds.left) / cellSizeX;
chars = std::vector<char>(rows*cols, ' '); chars = std::vector<char>(rows*cols, ' ');
cursorX = cursorY = 0; cursorX = cursorY = 0;
currentInstance = this; currentInstance = this;
} }
Rect Console::CellRect(short x, short y) Rect Console::CellRect(short x, short y)
{ {
return { (short) (bounds.top + y * cellSizeY), (short) (bounds.left + x * cellSizeX), return { (short) (bounds.top + y * cellSizeY), (short) (bounds.left + x * cellSizeX),
(short) (bounds.top + (y+1) * cellSizeY), (short) (bounds.left + (x+1) * cellSizeX) }; (short) (bounds.top + (y+1) * cellSizeY), (short) (bounds.left + (x+1) * cellSizeX) };
} }
void Console::DrawCell(short x, short y) void Console::DrawCell(short x, short y)
{ {
Rect r = CellRect(x,y); Rect r = CellRect(x,y);
EraseRect(&r); EraseRect(&r);
MoveTo(r.left, r.bottom - 2); MoveTo(r.left, r.bottom - 2);
DrawChar(chars[y * cols + x]); DrawChar(chars[y * cols + x]);
} }
void Console::Draw() void Console::Draw()
{ {
PortSetter setport(consolePort); PortSetter setport(consolePort);
for(short row = 0; row < rows; ++row) for(short row = 0; row < rows; ++row)
{ {
for(short col = 0; col < cols; ++col) for(short col = 0; col < cols; ++col)
{ {
DrawCell(col, row); DrawCell(col, row);
} }
} }
} }
void Console::ScrollUp(short n) void Console::ScrollUp(short n)
{ {
cursorY--; cursorY--;
std::copy(chars.begin() + cols, chars.end(), chars.begin()); std::copy(chars.begin() + cols, chars.end(), chars.begin());
std::fill(chars.end() - cols, chars.end(), ' '); std::fill(chars.end() - cols, chars.end(), ' ');
//Console::Draw(); //Console::Draw();
RgnHandle rgn = NewRgn(); RgnHandle rgn = NewRgn();
ScrollRect(&bounds, 0, -cellSizeY, rgn); ScrollRect(&bounds, 0, -cellSizeY, rgn);
DisposeRgn(rgn); DisposeRgn(rgn);
} }
void Console::putch(char c) void Console::putch(char c)
{ {
PortSetter setport(consolePort); PortSetter setport(consolePort);
//Debugger(); //Debugger();
switch(c) switch(c)
{ {
case '\r': case '\r':
cursorX = 0; cursorX = 0;
break; break;
case '\n': case '\n':
cursorY++; cursorY++;
cursorX = 0; cursorX = 0;
if(cursorY >= rows) if(cursorY >= rows)
ScrollUp(); ScrollUp();
break; break;
default: default:
chars[cursorY * cols + cursorX] = c; chars[cursorY * cols + cursorX] = c;
DrawCell(cursorX, cursorY); DrawCell(cursorX, cursorY);
cursorX++; cursorX++;
if(cursorX >= cols) if(cursorX >= cols)
putch('\n'); putch('\n');
} }
} }
std::string Console::ReadLine() std::string Console::ReadLine()
{ {
std::string buffer; std::string buffer;
EventRecord event; EventRecord event;
char c; char c;
do do
{ {
do do
{ {
while(!GetOSEvent(everyEvent, &event)) while(!GetOSEvent(everyEvent, &event))
; ;
} while(event.what != keyDown && event.what != autoKey); } while(event.what != keyDown && event.what != autoKey);
c = event.message & charCodeMask; c = event.message & charCodeMask;
if(c == '\r') if(c == '\r')
c = '\n'; c = '\n';
if(c == '\b') if(c == '\b')
{ {
cursorX--; cursorX--;
putch(' '); putch(' ');
cursorX--; cursorX--;
buffer.substr(0,buffer.size()-1); buffer.substr(0,buffer.size()-1);
continue; continue;
//c = 'X'; //c = 'X';
} }
if(c == 127) if(c == 127)
{ {
c = 'Y'; c = 'Y';
} }
putch(c); putch(c);
buffer += std::string(1,c); buffer += std::string(1,c);
} while(c != '\n'); } while(c != '\n');
return buffer; return buffer;
} }