termiraly 0.1.0 termiraly: ^0.1.0 copied to clipboard
ncurses like package for a terminal-style UI in a web canvas.
import "package:termiraly/termiraly.dart";
import "package:utilary/utilary.dart" show rng;
const rows = 25;
const cols = 40;
final Font font = Font("Consolas", size: 14, w: 16, h: 16, x: 1, y: 13);
//Create a new 40 x 25 characters terminal using a Consolas font.
//Optional argument [canvas] isn't provided (will create a new canvas)
final Terminal terminal = Terminal(cols, rows, font);
final UserInterface<String> ui = UserInterface(terminal);
void main() {
ui.keyPress.bind("up", KeyInput(KeyCodes.up));
ui.keyPress.bind("right", KeyInput(KeyCodes.right));
ui.keyPress.bind("down", KeyInput(KeyCodes.down));
ui.keyPress.bind("left", KeyInput(KeyCodes.left));
ui.push(ExampleScreen("HELLO, Move me with the arrow keys"));
//Start the UI
ui.handlingInput = true;
ui.running = true;
}
class ExampleScreen extends Screen<String> {
int _x;
int _y;
int _ticks;
Color _textColor;
final String _text;
ExampleScreen(this._text, [this._textColor]) {
_x = _y = 0;
_ticks = 10;
_textColor ??= Color.white;
}
@override
bool handleInput(String input) {
int xDir = 0, yDir = 0;
switch (input) {
case "up":
yDir = -1;
break;
case "right":
xDir = 1;
break;
case "down":
yDir = 1;
break;
case "left":
xDir = -1;
break;
default:
return false;
}
_move(xDir, yDir);
dirty();
return true;
}
void _move(int x, int y) {
_x += x;
_y += y;
}
@override
void update() {
_ticks--;
if (_ticks <= 0) {
_ticks = 10;
_textColor = Color(rng.range(256), rng.range(256), rng.range(256));
dirty();
}
}
@override
void render(Terminal t) {
t.drawString(_x, _y, _text, _textColor);
}
}