Skip to content

Core.Data.MarginF

Dennis Steffen edited this page Jan 4, 2026 · 1 revision

MarginF Documentation

MarginF is a lightweight struct located in the Meatcorps.Engine.Core.Data namespace. it is designed to represent 4-sided margins or padding (Left, Top, Right, Bottom) using floating-point values. It is commonly used for UI layouts, rendering offsets, or defining spatial boundaries.

Key Features

  • Non-Negative Enforcement: The properties Left, Top, Right, and Bottom are clamped to a minimum of 0 using Math.Max(0, value).
  • Multiple Constructors: Supports initialization for uniform margins, horizontal/vertical pairs, or individual side values.
  • Arithmetic Operator Overloading: Supports addition, subtraction, multiplication, and division between MarginF objects and scalars.
  • Rectangle Interaction: Includes a specific operator to apply margins to a RectF object.
  • Value Equality: Implements IEquatable<MarginF> and overrides equality operators for efficient value-based comparison.

Use Cases

  1. UI Padding: Defining the space between a container's border and its internal content.
  2. Layout Spacing: Setting the distance between adjacent UI elements (External margins).
  3. Safe Areas: Defining "dead zones" or offsets for rendering on screens with notches or specific aspect ratios.
  4. Geometry Expansion: Growing or shrinking boundaries of shapes (specifically rectangles).

Examples

1. Initialization

You can create a MarginF in several ways depending on your needs:

using Meatcorps.Engine.Core.Data;

// Uniform margin of 10 on all sides
var uniform = MarginF.All(10f);

// 20px on Left/Right, 5px on Top/Bottom
var hv = MarginF.HorizontalVertical(20f, 5f);

// Specific values: Left=5, Top=10, Right=15, Bottom=20
var specific = new MarginF(5, 10, 15, 20);

// Default (Zero)
var empty = MarginF.Zero;

2. Basic Arithmetic

Margins can be combined or scaled easily:

var marginA = MarginF.All(10f);
var marginB = MarginF.Horizontal(5f);

// Addition: Results in L:15, T:10, R:15, B:10
var combined = marginA + marginB;

// Scaling: Results in L:20, T:20, R:20, B:20
var doubled = marginA * 2;

3. Modifying a Rectangle

The + operator can be used to adjust a RectF by the margin values. Note that in this implementation, adding a margin shifts the origin and adjusts the dimensions:

RectF box = new RectF(100, 100, 200, 200);
MarginF padding = new MarginF(10, 10, 10, 10);

// Applying the margin to the rectangle
RectF adjustedBox = box + padding;

Implementation Details

Properties

Property Type Description
Left float The left margin. Minimum value is 0.
Top float The top margin. Minimum value is 0.
Right float The right margin. Minimum value is 0.
Bottom float The bottom margin. Minimum value is 0.

Static Methods

  • MarginF.All(float all): Creates a margin with the same value for all sides.
  • MarginF.Horizontal(float horizontal): Creates a margin with Left/Right values set, Top/Bottom as 0.
  • MarginF.Vertical(float vertical): Creates a margin with Top/Bottom values set, Left/Right as 0.
  • MarginF.Zero: Returns a margin with all sides set to 0.

Equality and Comparison

The struct uses EqualsSafe (likely an extension method for float epsilon comparison) to ensure that floating-point inaccuracies do not cause false inequality results.

Clone this wiki locally