Retro68/App2/Console.cc

151 lines
2.9 KiB
C++
Raw Normal View History

/*
Copyright 2012 Wolfgang Thaller.
This file is part of Retro68.
Retro68 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 3 of the License, or
(at your option) any later version.
Retro68 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 Retro68. If not, see <http://www.gnu.org/licenses/>.
*/
2012-03-29 08:31:05 +00:00
#include "Console.h"
2012-03-30 08:01:55 +00:00
#include "MacUtils.h"
#include "Events.h"
2014-09-14 21:59:35 +00:00
#include "Fonts.h"
2012-03-29 08:31:05 +00:00
Console *Console::currentInstance = NULL;
2012-04-05 23:43:44 +00:00
Console::Console(GrafPtr port, Rect r)
: consolePort(port), bounds(r)
2012-03-29 08:31:05 +00:00
{
2012-04-05 23:46:12 +00:00
PortSetter setport(consolePort);
InsetRect(&bounds, 2,2);
2014-09-14 21:59:35 +00:00
TextFont(kFontIDMonaco);
2012-04-05 23:46:12 +00:00
TextSize(9);
2014-09-14 21:59:35 +00:00
cellSizeY = 12;
2012-04-05 23:46:12 +00:00
cellSizeX = CharWidth('M');
rows = (bounds.bottom - bounds.top) / cellSizeY;
cols = (bounds.right - bounds.left) / cellSizeX;
chars = std::vector<char>(rows*cols, ' ');
cursorX = cursorY = 0;
currentInstance = this;
2012-03-29 08:31:05 +00:00
}
2012-04-08 12:45:54 +00:00
Console::~Console()
{
}
2012-03-29 08:31:05 +00:00
Rect Console::CellRect(short x, short y)
{
2012-04-05 23:46:12 +00:00
return { (short) (bounds.top + y * cellSizeY), (short) (bounds.left + x * cellSizeX),
(short) (bounds.top + (y+1) * cellSizeY), (short) (bounds.left + (x+1) * cellSizeX) };
2012-03-29 08:31:05 +00:00
}
void Console::DrawCell(short x, short y)
{
2012-04-05 23:46:12 +00:00
Rect r = CellRect(x,y);
EraseRect(&r);
MoveTo(r.left, r.bottom - 2);
DrawChar(chars[y * cols + x]);
2012-03-29 08:31:05 +00:00
}
void Console::Draw()
{
2012-04-05 23:46:12 +00:00
PortSetter setport(consolePort);
for(short row = 0; row < rows; ++row)
{
for(short col = 0; col < cols; ++col)
{
DrawCell(col, row);
}
}
2012-03-29 08:31:05 +00:00
}
void Console::ScrollUp(short n)
{
2012-04-05 23:46:12 +00:00
cursorY--;
std::copy(chars.begin() + cols, chars.end(), chars.begin());
std::fill(chars.end() - cols, chars.end(), ' ');
RgnHandle rgn = NewRgn();
ScrollRect(&bounds, 0, -cellSizeY, rgn);
DisposeRgn(rgn);
2012-03-29 08:31:05 +00:00
}
void Console::putch(char c)
{
2012-04-05 23:46:12 +00:00
PortSetter setport(consolePort);
switch(c)
{
case '\r':
cursorX = 0;
break;
case '\n':
cursorY++;
cursorX = 0;
if(cursorY >= rows)
ScrollUp();
break;
default:
chars[cursorY * cols + cursorX] = c;
DrawCell(cursorX, cursorY);
cursorX++;
if(cursorX >= cols)
putch('\n');
}
2012-03-29 08:31:05 +00:00
}
2012-04-05 23:43:44 +00:00
2012-03-30 08:01:55 +00:00
std::string Console::ReadLine()
{
2012-04-05 23:46:12 +00:00
std::string buffer;
EventRecord event;
char c;
do
{
do
{
while(!GetOSEvent(everyEvent, &event))
;
} while(event.what != keyDown && event.what != autoKey);
c = event.message & charCodeMask;
if(c == '\r')
c = '\n';
if(c == '\b')
{
2014-09-14 22:23:20 +00:00
if(buffer.size())
{
cursorX--;
putch(' ');
cursorX--;
buffer.resize(buffer.size()-1);
}
2012-04-05 23:46:12 +00:00
continue;
}
putch(c);
2014-09-14 22:23:20 +00:00
buffer.append(1,c);
2012-04-05 23:46:12 +00:00
} while(c != '\n');
return buffer;
2012-03-30 08:01:55 +00:00
}