-
Notifications
You must be signed in to change notification settings - Fork 1
AlignHelpers
AlignHelpers provides a comprehensive set of alignment and distribution utilities for UI layout. It supports horizontal and vertical alignment, stretching, padding, offset, and even distribution of multiple elements within containers or the viewport.
| Feature | Description |
|---|---|
| Horizontal Alignment | Left, Center, Right, Stretch |
| Vertical Alignment | Top, Center, Bottom, Stretch |
| Container Positioning | Align within any container |
| Viewport Positioning | Align within the game viewport |
| Padding Support | Add padding around elements |
| Offset Support | Add offsets to positions |
| Distribution | Evenly distribute multiple items |
Defines horizontal alignment options.
| Value | Description |
|---|---|
Left |
Aligns the element to the left edge |
Center |
Aligns the element to the center |
Right |
Aligns the element to the right edge |
Stretch |
Stretches the element to fill the available width |
Defines vertical alignment options.
| Value | Description |
|---|---|
Top |
Aligns the element to the top edge |
Center |
Aligns the element to the center |
Bottom |
Aligns the element to the bottom edge |
Stretch |
Stretches the element to fill the available height |
Calculates the horizontal position of a child element within a parent.
float x = AlignHelpers.AlignWidth(containerWidth, childWidth, HAlign.Center);
float xWithOffset = AlignHelpers.AlignWidth(containerWidth, childWidth, HAlign.Center, 10f);Calculates the vertical position of a child element within a parent.
float y = AlignHelpers.AlignHeight(containerHeight, childHeight, VAlign.Center);
float yWithOffset = AlignHelpers.AlignHeight(containerHeight, childHeight, VAlign.Center, 10f);Aligns a child element to the center of a parent.
Vect2 position = AlignHelpers.AlignCenter(parentSize, childSize);
Vect2 positionWithOffset = AlignHelpers.AlignCenter(parentSize, childSize, new Vect2(10f, -10f));Calculates the width for a stretched element within a parent.
float width = AlignHelpers.StretchWidth(containerWidth);
float widthWithPadding = AlignHelpers.StretchWidth(containerWidth, 10f);Calculates the height for a stretched element within a parent.
float height = AlignHelpers.StretchHeight(containerHeight);
float heightWithPadding = AlignHelpers.StretchHeight(containerHeight, 10f);Aligns a child element within the game viewport.
// Center an element in the viewport
Vect2 position = AlignHelpers.AlignToViewport(
childSize,
HAlign.Center,
VAlign.Center
);
// With offset
Vect2 position = AlignHelpers.AlignToViewport(
childSize,
HAlign.Center,
VAlign.Center,
new Vect2(10f, -10f)
);Aligns a child element within a container.
// Basic alignment
Vect2 position = AlignHelpers.AlignToContainer(
containerSize,
childSize,
HAlign.Center,
VAlign.Center
);
// With offset
Vect2 position = AlignHelpers.AlignToContainer(
containerSize,
childSize,
HAlign.Center,
VAlign.Center,
new Vect2(10f, -10f)
);Aligns a child element within a container with padding.
Vect2 position = AlignHelpers.AlignToContainer(
containerSize,
new Vect2(10f, 10f), // Padding
childSize,
HAlign.Right,
VAlign.Bottom
);
// With offset
Vect2 position = AlignHelpers.AlignToContainer(
containerSize,
new Vect2(10f, 10f),
childSize,
HAlign.Right,
VAlign.Bottom,
new Vect2(5f, -5f)
);Aligns a child element within a container and returns a complete rectangle.
Rect2 rect = AlignHelpers.AlignRect(
container,
childSize,
HAlign.Center,
VAlign.Center
);
// With offset
Rect2 rect = AlignHelpers.AlignRect(
container,
childSize,
HAlign.Center,
VAlign.Center,
new Vect2(10f, -10f)
);Aligns a child element within a container with padding and returns a complete rectangle.
Rect2 rect = AlignHelpers.AlignRect(
container,
new Vect2(10f, 10f), // Padding
childSize,
HAlign.Right,
VAlign.Bottom
);
// With offset
Rect2 rect = AlignHelpers.AlignRect(
container,
new Vect2(10f, 10f),
childSize,
HAlign.Right,
VAlign.Bottom,
new Vect2(5f, -5f)
);Calculates a position based on a percentage of the available space.
// Single dimension
float x = AlignHelpers.AlignPercent(containerWidth, childWidth, 0.5f);
// Both dimensions
Vect2 position = AlignHelpers.AlignPercent(
parentSize,
childSize,
new Vect2(0.5f, 0.5f)
);Determines whether content overflows the container.
// Single dimension
bool overflows = AlignHelpers.HasOverflow(containerWidth, contentWidth);
// Both dimensions
bool overflows = AlignHelpers.HasOverflow(containerSize, contentSize);Calculates the remaining space in a container after placing an element.
float remaining = AlignHelpers.Remaining(containerWidth, elementWidth, spacing);Distributes multiple items evenly across a container with specified spacing.
// Distribute items with size and spacing
float[] positions = AlignHelpers.DistributeEvenly(
containerWidth,
itemCount: 5,
itemSize: 50f,
spacing: 10f
);
// Distribute items with zero size (spacing only)
float[] positions = AlignHelpers.DistributeEvenly(
containerWidth,
itemCount: 5,
spacing: 10f
);public void OnDraw(FrameTime frameTime)
{
var viewport = GameSettings.Instance.Viewport;
var buttonSize = new Vect2(200f, 50f);
// Center a button in the viewport
var buttonPos = AlignHelpers.AlignToViewport(
buttonSize,
HAlign.Center,
VAlign.Center
);
// Create a panel with padding
var panelSize = new Vect2(800f, 600f);
var panelPos = AlignHelpers.AlignToViewport(
panelSize,
HAlign.Center,
VAlign.Center
);
var panel = new Rect2(panelPos, panelSize);
// Align a child element inside the panel with padding
var childSize = new Vect2(100f, 50f);
var childRect = AlignHelpers.AlignRect(
panel,
new Vect2(20f, 20f), // Padding
childSize,
HAlign.Center,
VAlign.Center
);
// Distribute menu items horizontally
var menuItems = 5;
var itemSize = 80f;
var positions = AlignHelpers.DistributeEvenly(
panel.Width - 40f,
menuItems,
itemSize,
10f
);
}| Category | Methods |
|---|---|
| Alignment | AlignWidth, AlignHeight, AlignCenter |
| Stretching | StretchWidth, StretchHeight |
| Viewport | AlignToViewport |
| Container | AlignToContainer |
| Rectangle | AlignRect |
| Percentage | AlignPercent |
| Overflow | HasOverflow |
| Remaining | Remaining |
| Distribution | DistributeEvenly |
- MathHelper
- FileHelper
- HashHelper
- JsonHelper
- MapHelper
- TextHelper
- SoundHelper
- AlignHelpers
- Instance Helper
- Enum Extensions
- String Extensions
- Int Extensions
- Float Extensions
- IEnumerable Extensions
- Random Extensions
- Sound Extensions
- Font Extensions