initial commit
This commit is contained in:
174
src/kernel/ui/TermBuffer.hx
Normal file
174
src/kernel/ui/TermBuffer.hx
Normal file
@@ -0,0 +1,174 @@
|
||||
package kernel.ui;
|
||||
|
||||
import util.Signal;
|
||||
import util.Vec.Vec2;
|
||||
import util.Color;
|
||||
import lib.TermWriteable;
|
||||
|
||||
@:structInit class Pixel {
|
||||
public var char:String;
|
||||
public var bg:Color;
|
||||
public var textColor:Color;
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
public function new() {
|
||||
initScreenBuffer(size);
|
||||
}
|
||||
|
||||
private function setSize(size: Vec2<Int>) {
|
||||
if (this.size != size){
|
||||
this._onResize.emit(size);
|
||||
}
|
||||
|
||||
this.size = size;
|
||||
updateScreenBufferSize(size);
|
||||
}
|
||||
|
||||
private function updateScreenBufferSize(size: Vec2<Int>) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
private function initScreenBuffer(size: Vec2<Int>) {
|
||||
screenBuffer = new Array();
|
||||
for (y in 0...size.y){
|
||||
screenBuffer[y] = new Array();
|
||||
for (x in 0...size.x){
|
||||
screenBuffer[y][x] = {
|
||||
char: " ",
|
||||
textColor: White,
|
||||
bg: Black,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function copyBufferToTarget(target: TermWriteable) {
|
||||
target.setCursorPos(0,0);
|
||||
target.setBackgroundColor(Black);
|
||||
target.setTextColor(White);
|
||||
|
||||
var tmpFgColor: Color = White;
|
||||
var tmpBgColor: Color = Black;
|
||||
|
||||
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){
|
||||
tmpBgColor = pixel.bg;
|
||||
target.setBackgroundColor(pixel.bg);
|
||||
}
|
||||
|
||||
target.setCursorPos(x,y);
|
||||
target.write(pixel.char);
|
||||
}
|
||||
}
|
||||
|
||||
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){
|
||||
screenBuffer[pos.y][pos.x].char = char;
|
||||
screenBuffer[pos.y][pos.x].bg = currentBgColor;
|
||||
screenBuffer[pos.y][pos.x].textColor = currentTextColor;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// TermWriteable functions
|
||||
//
|
||||
|
||||
public var onResize(get,null):SignalReadonly<Vec2<Int>>;
|
||||
private final _onResize:Signal<Vec2<Int>> = new Signal();
|
||||
|
||||
function get_onResize():Signal<Vec2<Int>> {
|
||||
return _onResize;
|
||||
}
|
||||
|
||||
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};
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}]);
|
||||
}
|
||||
|
||||
public function getCursorPos():Vec2<Int> {
|
||||
return cursorPos;
|
||||
}
|
||||
|
||||
public function setCursorPos(x:Int, y:Int) {
|
||||
cursorPos = {
|
||||
x: x,
|
||||
y: y,
|
||||
};
|
||||
}
|
||||
|
||||
public function getCursorBlink():Bool {
|
||||
throw new haxe.exceptions.NotImplementedException();
|
||||
}
|
||||
|
||||
public function setCursorBlink(blink:Bool) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
public function getSize():Vec2<Int> {
|
||||
return size;
|
||||
}
|
||||
|
||||
public function clear() {
|
||||
initScreenBuffer(size);
|
||||
}
|
||||
|
||||
public function clearLine() {
|
||||
if (screenBuffer.length > cursorPos.y){
|
||||
screenBuffer[cursorPos.y] = [for(x in 0...size.x){textColor: White,char: " ",bg: Black}];
|
||||
}
|
||||
}
|
||||
|
||||
public function getTextColor():Color {
|
||||
return currentTextColor;
|
||||
}
|
||||
|
||||
public function setTextColor(colour:Color) {
|
||||
currentTextColor = colour;
|
||||
}
|
||||
|
||||
public function getBackgroundColor():Color {
|
||||
return currentBgColor;
|
||||
}
|
||||
|
||||
public function setBackgroundColor(color:Color) {
|
||||
currentBgColor = color;
|
||||
}
|
||||
|
||||
public function isColor():Bool {
|
||||
throw new haxe.exceptions.NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user