Skip to content

Stack Panel

Refaltor77 edited this page Mar 29, 2026 · 2 revisions

Stack Panel

Layout panel that automatically stacks children vertically or horizontally. Extends Panel.

Creation

use refaltor\ui\elements\StackPanel;
use refaltor\ui\helpers\OrientationHelper;

$stack = StackPanel::create("my_stack")
    ->setOrientation(OrientationHelper::VERTICAL);

Orientation

$stack->setOrientation(OrientationHelper::VERTICAL);   // Top to bottom
$stack->setOrientation(OrientationHelper::HORIZONTAL);  // Left to right

Complete Example

Vertical Menu

$menu = StackPanel::create("menu")
    ->setOrientation(OrientationHelper::VERTICAL)
    ->setSize(200, 200)
    ->setAnchorFrom(Element::ANCHOR_CENTER)
    ->setAnchorTo(Element::ANCHOR_CENTER);

$menu->addChild(
    Label::create("item1", "Play Game")->setSize(200, 30)->setShadow()
);
$menu->addChild(
    Label::create("item2", "Settings")->setSize(200, 30)->setShadow()
);
$menu->addChild(
    Label::create("item3", "Quit")->setSize(200, 30)->setShadow()
);

$root->addElement($menu);

Horizontal Toolbar

$toolbar = StackPanel::create("toolbar")
    ->setOrientation(OrientationHelper::HORIZONTAL)
    ->setCustomSize(["100%", 40])
    ->setAnchorFrom(Element::ANCHOR_BOTTOM_MIDDLE)
    ->setAnchorTo(Element::ANCHOR_BOTTOM_MIDDLE);

$toolbar->addChild(Label::create("t1", "Home")->setSize(80, 40));
$toolbar->addChild(Label::create("t2", "Inventory")->setSize(80, 40));
$toolbar->addChild(Label::create("t3", "Map")->setSize(80, 40));

Important

Children must have a defined size for stacking to work properly. Use setSize() or setCustomSize() on each child.

See Also

  • Panel — Parent class (all Panel properties apply)
  • OrientationHelper — Orientation constants
  • Button — Commonly used with stack panels

Clone this wiki locally