Files
cc-haxe/src/kernel/ui/BufferedVirtualTermWriter.hx
2024-10-14 21:40:26 +02:00

181 lines
3.3 KiB
Haxe

package kernel.ui;
import lib.ScreenPos;
import lib.Vec.Vec2;
import lib.Color;
import kernel.ui.ITermWriteable;
using tink.CoreApi;
/**
A term writer that can switch beetween its internal buffer and another termwriter.
The other target is most of the time a real screen
**/
class BufferedVirtualTermWriter implements IVirtualTermWriter extends TermBuffer {
private static final defaultSize:Vec2<Int> = {x: 50, y: 50};
private var target:ITermWriteable;
private var enabled:Bool = false;
private var onResizeLink:CallbackLink;
@:allow(kernel.ui)
private function new(?target:ITermWriteable) {
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:ITermWriteable) {
if (newTarget != null) {
super.setSize(newTarget.getSize());
// Remove old target event listner
if (onResizeLink != null) {
onResizeLink.cancel();
}
// Add new target event listner
onResizeLink = newTarget.onResize.handle(newSize -> {
setSuperSize(newSize);
});
newTarget.reset();
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():ScreenPos {
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 {
if (isEnabled()) {
return target.getCursorBlink();
} else {
return super.getCursorBlink();
}
}
public override function setCursorBlink(blink:Bool) {
if (isEnabled()) {
target.setCursorBlink(blink);
}
super.setCursorBlink(blink);
}
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(color:Color) {
if (isEnabled()) {
target.setTextColor(color);
}
super.setTextColor(color);
}
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();
}
}