cc-haxe/src/lib/ui/elements/TextElement.hx

49 lines
947 B
Haxe

package lib.ui.elements;
import lib.ui.elements.UIElement;
class TextElement implements UIElement {
public var text:String;
private final uiEvents:UIEvents;
private final style:Style;
public function new(text:String, ?props:{?style:Style, ?uiEvents:UIEvents}) {
this.text = text;
this.uiEvents = props?.uiEvents;
this.style = props?.style ?? {fgColor: White, bgColor: Black};
}
public function set(text:String) {
this.text = text;
}
public function get():String {
return this.text;
}
public function getEventHandlers():UIEvents {
return uiEvents;
}
public function render(bounds:Pos):Canvas {
var canvas = new Canvas();
var x = 0;
var y = 0;
for (i in 0...this.text.length) {
var c = this.text.charAt(i);
if (c == "\n") {
x = 0;
y++;
} else {
canvas.set({x: x, y: y}, {bg: style.bgColor ?? Black, textColor: style.fgColor ?? White, char: c});
x++;
}
}
return canvas;
}
}