idk routing stuff i guess
This commit is contained in:
48
src/lib/ui/reactive/TextElement.hx
Normal file
48
src/lib/ui/reactive/TextElement.hx
Normal file
@@ -0,0 +1,48 @@
|
||||
package lib.ui.reactive;
|
||||
|
||||
import util.Observable;
|
||||
using tink.CoreApi;
|
||||
|
||||
import util.Color;
|
||||
import util.Vec.Vec2;
|
||||
import util.MathI;
|
||||
|
||||
class TextElement implements IElement {
|
||||
public var changed(default, null):Signal<Noise>;
|
||||
|
||||
private var changedTrigger:SignalTrigger<Noise>;
|
||||
|
||||
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) {
|
||||
setupTrigger();
|
||||
|
||||
this.text = text;
|
||||
this.bg = background;
|
||||
this.fg = textColor;
|
||||
|
||||
this.text.subscribe(value -> {
|
||||
this.changedTrigger.trigger(null);
|
||||
});
|
||||
}
|
||||
|
||||
private function setupTrigger() {
|
||||
changedTrigger = Signal.trigger();
|
||||
changed = changedTrigger.asSignal();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user