Skip to content

Core.Data.PaddingF

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

PaddingF Documentation

The PaddingF struct is a utility data structure designed to represent 2D padding or internal spacing values (Left, Top, Right, Bottom) using floating-point precision. It is commonly used in UI layouts, rendering systems, and coordinate calculations to define the offset between a container's boundary and its content.

Key Features

  • Non-Negative Constraints: All properties (Left, Top, Right, Bottom) are automatically clamped to a minimum value of 0 when set, ensuring valid physical spacing.
  • Arithmetic Support: Includes operator overloading for addition, subtraction, multiplication, and division between PaddingF instances or with scalar values.
  • Rectangle Integration: Provides a specialized operator to apply padding directly to a RectF, effectively shrinking the rectangle's usable area.
  • Precision Comparison: Uses safe floating-point equality checks (via .EqualsSafe) to prevent common rounding issues.

Properties

Property Type Description
Left float The spacing on the left side (minimum 0).
Top float The spacing on the top side (minimum 0).
Right float The spacing on the right side (minimum 0).
Bottom float The spacing on the bottom side (minimum 0).

Static Helpers

Instead of calling constructors manually, you can use these expressive static methods:

  • PaddingF.Zero: Returns padding with all sides set to 0.
  • PaddingF.All(float value): Sets the same padding for all four sides.
  • PaddingF.Horizontal(float horizontal): Sets Left/Right only.
  • PaddingF.Vertical(float vertical): Sets Top/Bottom only.
  • PaddingF.HorizontalVertical(float h, float v): Sets horizontal and vertical pairs.

Use Cases

  1. UI Layout Engines: Defining the space between a button's border and its internal text.
  2. Rendering: Calculating the "safe area" inside a window or panel where content can be drawn without overlapping borders.
  3. Dynamic Scaling: Multiplying padding by a scalar value to adjust UI density for different screen resolutions.
  4. Nested Containers: Combining multiple layers of padding using the + operator.

Examples

1. Creating Padding

// Uniform padding of 10 units
var uniform = PaddingF.All(10f);

// 20 units on sides, 5 units on top/bottom
var symmetric = new PaddingF(20f, 5f);

// Specific sides: Left: 5, Top: 10, Right: 15, Bottom: 20
var specific = new PaddingF(5f, 10f, 15f, 20f);

2. Applying Padding to a Rectangle

When you add a PaddingF to a RectF, it returns a new rectangle that is "pushed inward" by the padding amounts.

var outerBounds = new RectF(0, 0, 100, 100);
var padding = PaddingF.All(10f);

// The resulting inner rect will be at (10, 10) with a size of 80x80
RectF innerContentArea = outerBounds + padding;

3. Arithmetic Operations

var basePadding = PaddingF.All(5f);
var extraPadding = PaddingF.Horizontal(10f);

// Combine paddings: Result is L:15, T:5, R:15, B:5
var combined = basePadding + extraPadding;

// Scale padding for "Big Mode": Result is L:30, T:10, R:30, B:10
var scaled = combined * 2;

4. Equality Check

var p1 = PaddingF.All(10f);
var p2 = new PaddingF(10f, 10f, 10f, 10f);

if (p1 == p2) 
{
    // This will be true
}

Clone this wiki locally