cc-haxe/src/kernel/ui/VirtualTermWriter.hx
2021-12-20 01:55:30 +01:00

164 lines
3.6 KiB
Haxe

package kernel.ui;
import util.Signal.SignalListner;
import util.Vec.Vec2;
import util.Color;
import lib.TermWriteable;
class VirtualTermWriter implements TermWriteable extends TermBuffer {
private static final defaultSize:Vec2<Int> = {x: 50,y: 50};
private var target: TermWriteable;
private var enabled:Bool = false;
private var onResizeListner: SignalListner<Vec2<Int>>;
public function new(?target: TermWriteable) {
setTarget(target);
if (enabled){
enable();
}
super();
}
public function enable() {
if (target != null){
enabled = true;
super.copyBufferToTarget(target);
}
}
public function disable() {
enabled = false;
}
public inline function isEnabled(): Bool {
return enabled;
}
public function setTarget(newTarget: TermWriteable) {
if (newTarget != null){
super.setSize(newTarget.getSize());
// Remove old target event listner
if (onResizeListner != null && target != null){
target.onResize.remove(onResizeListner);
}
// Add new target event listner
onResizeListner = newTarget.onResize.on(newSize -> {
setSuperSize(newSize);
});
target = newTarget;
}
}
private function setSuperSize(size: Vec2<Int>) {
super.setSize(target.getSize());
}
//
// TermWriteable functions.
//
public override function write(text:String) {
if (isEnabled()){
target.write(text);
}
super.write(text);
}
public override function scroll(y:Int) {
if (isEnabled()){
target.scroll(y);
}
super.scroll(y);
}
public override function getCursorPos():Vec2<Int> {
if (isEnabled()){
return target.getCursorPos();
}else{
return super.getCursorPos();
}
}
public override function setCursorPos(x:Int, y:Int) {
if (isEnabled()){
target.setCursorPos(x,y);
}
super.setCursorPos(x,y);
}
public override function getCursorBlink():Bool {
throw new haxe.exceptions.NotImplementedException();
}
public override function setCursorBlink(blink:Bool) {
// TODO
}
public override function getSize():Vec2<Int> {
// TODO: make sense ?
if (target != null){
return target.getSize();
}
return defaultSize;
}
public override function clear() {
if (isEnabled()){
target.clear();
}
super.clear();
}
public override function clearLine() {
if (isEnabled()){
target.clearLine();
}
super.clearLine();
}
public override function getTextColor():Color {
if (isEnabled()){
return target.getTextColor();
}
return super.getTextColor();
}
public override function setTextColor(colour:Color) {
if (isEnabled()){
target.setTextColor(colour);
}
super.setTextColor(colour);
}
public override function getBackgroundColor():Color {
if (isEnabled()){
return target.getBackgroundColor();
}
return super.getBackgroundColor();
}
public override function setBackgroundColor(color:Color) {
if (isEnabled()){
target.setBackgroundColor(color);
}
super.setBackgroundColor(color);
}
public override function isColor():Bool {
throw new haxe.exceptions.NotImplementedException();
}
}