Layouts and Alignment
Layouts allow multiple components to be arranged together. They can be nested to create more complex interfaces.
SSGL provides vertical and horizontal layouts.
Vertical layout
A vertical layout places its children from top to bottom.
GuiLayout.vertical()
.gap(8)
.add(GuiElement.label("General"))
.add(GuiElement.toggle("Enabled", enabled))
.add(GuiElement.button("Apply", this::apply));
Horizontal layout
A horizontal layout places its children from left to right.
GuiLayout.horizontal()
.gap(8)
.add(GuiElement.button("Apply", this::apply))
.add(GuiElement.button("Cancel", this::cancel));
Layout spacing
Use .gap(int) to control the space between children.
GuiLayout.vertical()
.gap(12)
.add(GuiElement.label("Settings"))
.add(GuiElement.toggle("Enabled", enabled));
The gap is measured in GUI pixels.
Nested layouts
Layouts can contain other layouts.
This is useful when you want to group related settings.
gui.add(
GuiLayout.vertical()
.gap(10)
.add(GuiElement.label("General"))
.add(
GuiLayout.horizontal()
.gap(8)
.add(GuiElement.toggle("Enabled", enabled))
.add(GuiElement.toggle("Particles", particles))
)
.add(GuiElement.button(
"Apply",
this::applySettings
))
);
There is no fixed nesting limit imposed by the API.
Width
You can set a component's preferred width:
GuiElement.button("Apply", this::apply)
.width(120);
This is useful when you want buttons or image previews to have consistent dimensions.
Without an explicitly assigned width, the layout system uses the space allocated to the component.
Alignment
Use .align() to position a component inside its available horizontal space.
SSGL provides three alignment options:
| Alignment | Description |
|---|---|
Alignment.LEFT |
Aligns to the left |
Alignment.CENTER |
Centers the component |
Alignment.RIGHT |
Aligns to the right |
Example:
gui.add(
GuiElement.button("Centered Button", this::apply)
.width(160)
.align(Alignment.CENTER)
);
The same alignment API works for labels and image buttons as well.
Alignment inside horizontal layouts
Horizontal layouts normally allocate space according to their regular width-allocation behavior.
When a child explicitly requests CENTER or RIGHT, the horizontal layout uses equal-width slots for its children. Each child is then aligned inside its own slot.
For predictable results, assign a fixed width to children that need individual alignment:
GuiLayout.horizontal()
.gap(8)
.add(
GuiElement.button("Left", this::left)
.width(80)
.align(Alignment.LEFT)
)
.add(
GuiElement.button("Center", this::center)
.width(80)
.align(Alignment.CENTER)
)
.add(
GuiElement.button("Right", this::right)
.width(80)
.align(Alignment.RIGHT)
);