This commit is contained in:
2022-02-21 15:35:37 +01:00
parent f21e49c520
commit 0ad907f74a
33 changed files with 788 additions and 815 deletions

View File

@@ -13,27 +13,28 @@ import lib.TermWriteable;
}
class TermBuffer implements TermWriteable {
/**
format [y][x]. First index is the line. Second index the char in the line.
**/
private var screenBuffer: Array<Array<Pixel>>;
private var cursorPos: Vec2<Int> = {x: 0, y: 0};
private var currentTextColor: Color = White;
private var currentBgColor: Color = Black;
private var size: Vec2<Int> = {x: 51,y:19}; // Default size set to default size of the main terminal
private var screenBuffer:Array<Array<Pixel>>;
private var cursorPos:Vec2<Int> = {x: 0, y: 0};
private var currentTextColor:Color = White;
private var currentBgColor:Color = Black;
private var size:Vec2<Int> = {x: 51, y: 19}; // Default size set to default size of the main terminal
public var onResize(default, null):Signal<Vec2<Int>>;
public var onResize(default,null):Signal<Vec2<Int>>;
private final onResizeTrigger:SignalTrigger<Vec2<Int>>;
public function new() {
this.onResizeTrigger = Signal.trigger();
this.onResize = onResizeTrigger.asSignal();
initScreenBuffer(size);
}
private function setSize(size: Vec2<Int>) {
if (this.size != size){
private function setSize(size:Vec2<Int>) {
if (this.size != size) {
this.onResizeTrigger.trigger(size);
}
@@ -41,80 +42,82 @@ class TermBuffer implements TermWriteable {
updateScreenBufferSize(size);
}
private function updateScreenBufferSize(size: Vec2<Int>) {
private function updateScreenBufferSize(size:Vec2<Int>) {
// TODO
}
private function initScreenBuffer(size: Vec2<Int>) {
private function initScreenBuffer(size:Vec2<Int>) {
screenBuffer = new Array();
for (y in 0...size.y){
for (y in 0...size.y) {
screenBuffer[y] = new Array();
for (x in 0...size.x){
for (x in 0...size.x) {
screenBuffer[y][x] = {
char: " ",
textColor: White,
bg: Black,
}
}
}
}
}
private function copyBufferToTarget(target: TermWriteable) {
target.setCursorPos(0,0);
private function copyBufferToTarget(target:TermWriteable) {
target.setCursorPos(0, 0);
target.setBackgroundColor(Black);
target.setTextColor(White);
var tmpFgColor: Color = White;
var tmpBgColor: Color = Black;
var tmpFgColor:Color = White;
var tmpBgColor:Color = Black;
for (y => line in screenBuffer){
for(x => pixel in line){
if (tmpFgColor != pixel.textColor){
for (y => line in screenBuffer) {
for (x => pixel in line) {
if (tmpFgColor != pixel.textColor) {
tmpFgColor = pixel.textColor;
target.setTextColor(pixel.textColor);
}
if (tmpBgColor != pixel.bg){
if (tmpBgColor != pixel.bg) {
tmpBgColor = pixel.bg;
target.setBackgroundColor(pixel.bg);
}
target.setCursorPos(x,y);
target.setCursorPos(x, y);
target.write(pixel.char);
}
}
target.setCursorPos(cursorPos.x,cursorPos.y);
target.setCursorPos(cursorPos.x, cursorPos.y);
target.setTextColor(currentTextColor);
target.setBackgroundColor(currentBgColor);
}
private function safeWriteScreenBuffer(pos: Vec2<Int>,char: String) {
if (screenBuffer.length > pos.y && screenBuffer[pos.y].length > pos.x){
private function safeWriteScreenBuffer(pos:Vec2<Int>, char:String) {
if (screenBuffer.length > pos.y && screenBuffer[pos.y].length > pos.x) {
screenBuffer[pos.y][pos.x].char = char;
screenBuffer[pos.y][pos.x].bg = currentBgColor;
screenBuffer[pos.y][pos.x].textColor = currentTextColor;
}
}
//
// TermWriteable functions
//
//
public function write(text:String) {
for (i in 0...text.length){
safeWriteScreenBuffer({x: cursorPos.x,y: cursorPos.y},text.charAt(i));
cursorPos = {y: cursorPos.y,x: cursorPos.x + 1};
}
for (i in 0...text.length) {
safeWriteScreenBuffer({x: cursorPos.x, y: cursorPos.y}, text.charAt(i));
cursorPos = {y: cursorPos.y, x: cursorPos.x + 1};
}
}
public function scroll(y:Int) {
screenBuffer.unshift([for (i in 0...size.x) {
char: " ",
textColor: White, // TODO: maybe replace with current bg/text color. Check nativ implementation
bg: Black
}]);
screenBuffer.unshift([
for (i in 0...size.x)
{
char: " ",
textColor: White, // TODO: maybe replace with current bg/text color. Check nativ implementation
bg: Black
}
]);
}
public function getCursorPos():Vec2<Int> {
@@ -145,8 +148,8 @@ class TermBuffer implements TermWriteable {
}
public function clearLine() {
if (screenBuffer.length > cursorPos.y){
screenBuffer[cursorPos.y] = [for(x in 0...size.x){textColor: White,char: " ",bg: Black}];
if (screenBuffer.length > cursorPos.y) {
screenBuffer[cursorPos.y] = [for (x in 0...size.x) {textColor: White, char: " ", bg: Black}];
}
}