dartterm/lib/terminal/cursor.dart
2023-09-10 19:00:06 -07:00

106 lines
2.0 KiB
Dart

part of '../terminal.dart';
var _nextHighlightGroup = 0;
class Cursor {
final Terminal t;
int x, y;
Color? _bg, _fg;
Font font = Font.normal;
int? highlightGroup;
final List<Act> _acts = [];
// TODO: Clip
Cursor({required this.t, required this.x, required this.y});
void clear() {
t._tiles = [for (var i = 0; i < nTiles; i += 1) Tile()];
}
void putc(int c) {
for (var dx = 0; dx < font.cellWidth; dx += 1) {
for (var dy = 0; dy < font.cellHeight; dy += 1) {
var i = t._fromXY(x + dx, y + dy);
if (i != null) {
final (sourceCx, sourceCy) = (
(c % font.nCellsW) * font.cellWidth + dx,
(c ~/ font.nCellsW) * font.cellHeight + dy
);
t._tiles[i].content = Content(font.imageName, sourceCx, sourceCy);
final (bg, fg) = (_bg, _fg);
if (bg != null) {
t._tiles[i].bg = bg;
}
if (fg != null) {
t._tiles[i].fg = fg;
}
if (highlightGroup != null) {
t._tiles[i].highlightGroup = highlightGroup;
}
t._tiles[i].actions.addAll(_acts);
}
}
}
}
void puts(String s) {
var startX = x;
for (final c in toCp437String(s)) {
if (c == 10) {
// newline
x = startX;
y += font.cellHeight;
continue;
}
if (c == 13) {
// carriage return
x = startX;
continue;
}
putc(c);
x += font.cellWidth;
}
}
Cursor fg(Color c) {
_fg = c;
return this;
}
Cursor bg(Color c) {
_bg = c;
return this;
}
Cursor small() {
font = Font.small;
return this;
}
Cursor normal() {
font = Font.normal;
return this;
}
Cursor big() {
font = Font.big;
return this;
}
Cursor highlight() {
highlightGroup = _nextHighlightGroup;
_nextHighlightGroup += 1;
return this;
}
Cursor act(Act a) {
_acts.add(a);
return this;
}
}