Files
cc-haxe/src/lib/ui/reactive/TextElement.hx
2022-03-04 00:59:02 +01:00

46 lines
1.0 KiB
Haxe

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<String>;
private final bg:Color;
private final fg:Color;
public function new(text:Observable<String>, ?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<Int>):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<Int>,button: ButtonType):Void {
Log.debug('Click: ${this.text.get()}');
}
}