-
Notifications
You must be signed in to change notification settings - Fork 0
Core.Data.MarginF
Dennis Steffen edited this page Jan 4, 2026
·
1 revision
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.
-
Non-Negative Enforcement: The properties
Left,Top,Right, andBottomare clamped to a minimum of0usingMath.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
MarginFobjects and scalars. -
Rectangle Interaction: Includes a specific operator to apply margins to a
RectFobject. -
Value Equality: Implements
IEquatable<MarginF>and overrides equality operators for efficient value-based comparison.
- UI Padding: Defining the space between a container's border and its internal content.
- Layout Spacing: Setting the distance between adjacent UI elements (External margins).
- Safe Areas: Defining "dead zones" or offsets for rendering on screens with notches or specific aspect ratios.
- Geometry Expansion: Growing or shrinking boundaries of shapes (specifically rectangles).
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;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;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;| 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. |
-
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.
The struct uses EqualsSafe (likely an extension method for float epsilon comparison) to ensure that floating-point inaccuracies do not cause false inequality results.