Skip to content

AlignHelpers

Shmellyorc edited this page Aug 31, 2026 · 3 revisions

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.


Overview

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

Enums

HAlign

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

VAlign

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

Alignment Methods

AlignWidth

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);

AlignHeight

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);

AlignCenter

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));

Stretching Methods

StretchWidth

Calculates the width for a stretched element within a parent.

float width = AlignHelpers.StretchWidth(containerWidth);
float widthWithPadding = AlignHelpers.StretchWidth(containerWidth, 10f);

StretchHeight

Calculates the height for a stretched element within a parent.

float height = AlignHelpers.StretchHeight(containerHeight);
float heightWithPadding = AlignHelpers.StretchHeight(containerHeight, 10f);

Viewport Alignment

AlignToViewport

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)
);

Container Alignment

AlignToContainer

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)
);

With Padding

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)
);

Rectangle Alignment

AlignRect

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)
);

With Padding

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)
);

Percentage Alignment

AlignPercent

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)
);

Overflow Detection

HasOverflow

Determines whether content overflows the container.

// Single dimension
bool overflows = AlignHelpers.HasOverflow(containerWidth, contentWidth);

// Both dimensions
bool overflows = AlignHelpers.HasOverflow(containerSize, contentSize);

Remaining Space

Remaining

Calculates the remaining space in a container after placing an element.

float remaining = AlignHelpers.Remaining(containerWidth, elementWidth, spacing);

Distribution

DistributeEvenly

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
);

Example: UI Layout

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
    );
}

Summary

Category Methods
Alignment AlignWidth, AlignHeight, AlignCenter
Stretching StretchWidth, StretchHeight
Viewport AlignToViewport
Container AlignToContainer
Rectangle AlignRect
Percentage AlignPercent
Overflow HasOverflow
Remaining Remaining
Distribution DistributeEvenly

Back to Home

Clone this wiki locally