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

59 lines
1.4 KiB
Haxe

package lib.ui.elements;
class RootElement implements IUIElement {
private var children:Array<IUIElement> = [];
private final eventManager:UIEventManager = new UIEventManager();
private var title:String = "";
public function new(?children:Array<IUIElement>) {
if (children == null) {
children = [];
} else {
this.children = children;
}
}
public function getEventHandlers():UIEvents {
return eventManager.getEventHandlers();
}
public function setChildren(children:Array<IUIElement>) {
this.children = children;
}
public function render(bounds:Pos):Canvas {
var canvas = new Canvas();
var offset = new Pos({x: 0, y: 0});
if (hasTitle()) {
var title = new TextElement(this.title);
var halfWidth = Math.floor(bounds.x / 2) - Math.floor(this.title.length / 2);
canvas.combine(title.render(bounds), {x: halfWidth, y: offset.y});
offset = new Pos({x: 0, y: 1});
}
this.eventManager.clearMap();
for (child in children) {
var childCanvas = child.render(bounds);
var bounds = childCanvas.getBounds();
bounds.offset(offset);
this.eventManager.addMapElement(child, bounds);
canvas.combine(childCanvas, offset);
offset = new Pos({x: 0, y: offset.y + bounds.getHight() + 1});
}
return canvas;
}
public function setTitle(title:String) {
this.title = title;
}
private inline function hasTitle():Bool {
return title != "";
}
}