package lib.ui.reactive; import kernel.Log; import kernel.ButtonType; import util.Observable; using tink.CoreApi; import util.Color; import util.Vec.Vec2; import util.MathI; class TextElement extends UIElement { private final text:Observable; private final bg:Color; private final fg:Color; public function new(text:Observable, ?background:Color = Black, ?textColor:Color = White) { super(); this.text = text; this.bg = background; this.fg = textColor; this.text.subscribe(value -> { this.changedTrigger.trigger(null); }); } public function render(bounds:Vec2):Canvas { var rtn = new Canvas(); for (i in 0...MathI.min(Math.floor(text.get().length / bounds.x) + 1, bounds.y)) { var line = (text.get().substr(i * bounds.x, bounds.x)); for (char in 0...line.length) { rtn.set({x: char, y: i}, {textColor: fg, char: line.charAt(char), bg: bg}); } } return rtn; } public override function handleClickEvent(pos: Vec2,button: ButtonType):Void { Log.debug('Click: ${this.text.get()}'); } }