added statelessVirtualTermWriter
- changed how WindowManager handles new contexts
This commit is contained in:
180
src/kernel/ui/BufferedVirtualTermWriter.hx
Normal file
180
src/kernel/ui/BufferedVirtualTermWriter.hx
Normal file
@@ -0,0 +1,180 @@
|
||||
package kernel.ui;
|
||||
|
||||
import lib.Pos;
|
||||
import lib.Vec.Vec2;
|
||||
import lib.Color;
|
||||
import kernel.ui.TermWriteable;
|
||||
|
||||
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 VirtualTermWriter extends TermBuffer {
|
||||
private static final defaultSize:Vec2<Int> = {x: 50, y: 50};
|
||||
|
||||
private var target:TermWriteable;
|
||||
private var enabled:Bool = false;
|
||||
private var onResizeLink:CallbackLink;
|
||||
|
||||
@:allow(kernel.ui)
|
||||
private 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 (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():Pos {
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user