UIElement
Namespace:
namespace Lenga\Engine\UI;
abstract class UIElement
Base class for all UI elements — Button, Text, and Image all extend UIElement. It provides the shared API for visibility, hierarchy, layout, and child-element creation.
Obtain an element reference via Canvas::getElement(). You do not instantiate UIElement directly; the canvas creates and manages element instances.
Properties
| Property | Type | Access | Description |
|---|---|---|---|
$name |
string |
read | The name of the element as set in the scene. |
$enabled |
bool |
read/write | Whether the element processes input and updates. |
$visible |
bool |
read/write | Whether the element is drawn on screen. |
$activeInHierarchy |
bool |
read | true when the element and all of its ancestors are active. |
$sortOrder |
int |
read/write | Draw order relative to sibling elements. Higher values render on top. |
$rectTransform |
RectTransform |
read | Layout data (anchors, position, size, pivot). |
Methods
getId
public function getId(): int
Returns the native element identifier. Used internally by engine bindings.
getCanvas
public function getCanvas(): ?Canvas
Returns the Canvas this element belongs to.
getParent / setParent
public function getParent(): ?UIElement
public function setParent(?UIElement $parent): bool
Gets or re-parents the element in the UI hierarchy.
getChildren / findDescendantByName
public function getChildren(): list<UIElement>
public function findDescendantByName(string $name): ?UIElement
Traverses child elements. findDescendantByName searches the entire subtree recursively.
createText / createImage / createButton
public function createText(string $name): Text
public function createImage(string $name): Image
public function createButton(string $name): Button
Creates a new child element of the given type, parented to this element. Throws RuntimeException if the element is not attached to a canvas.
Example
use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\UI\Canvas;
use Lenga\Engine\UI\Button;
class InventoryPanel extends Behaviour
{
private ?Canvas $canvas = null;
public function start(): void
{
$this->canvas = $this->getComponent(Canvas::class);
$panel = $this->canvas->getElement('InventoryPanel');
// Toggle panel visibility
$panel->visible = true;
// Dynamically create a close button as a child of the panel
$closeBtn = $panel->createButton('CloseButton');
$closeBtn->text = 'Close';
$closeBtn->visible = true;
}
public function update(): void
{
$closeBtn = $this->canvas?->getElement('CloseButton');
if ($closeBtn instanceof Button && $closeBtn->clicked) {
$this->canvas->getElement('InventoryPanel')->visible = false;
}
}
}