-
Notifications
You must be signed in to change notification settings - Fork 0
Core.Data.SizeF
Dennis Steffen edited this page Jan 4, 2026
·
1 revision
The SizeF structure is a part of the Meatcorps.Engine.Core.Data namespace. It represents a 2D size defined by a floating-point Width and Height. It is designed for use in scenarios like UI layout, game object dimensions, and rendering where non-negative dimensions are required.
-
Non-negative Enforcement: The
WidthandHeightproperties automatically clamp values to a minimum of0usingMath.Max(0, value). This prevents "negative sizes" which can cause issues in rendering and layout logic. -
Vector2 Integration: It provides seamless interoperability with
System.Numerics.Vector2, including implicit conversions and mathematical operators. -
Serialization Ready: Decorated with
[DataContract]and[DataMember]attributes, making it compatible with various .NET serializers. -
Comprehensive Operators: Supports addition, subtraction, multiplication, and division with other
SizeFinstances,floatscalars, andVector2offsets.
- UI Layout: Defining the dimensions of buttons, panels, or text containers where dimensions cannot be less than zero.
- Sprite/Texture Dimensions: Storing the logical size of a game entity independent of its screen resolution.
- Scaling Logic: Using scalars to uniformly resize elements (e.g., doubling the size of a window).
- Collision Bounds: Representing the extent of an Axis-Aligned Bounding Box (AABB) when paired with a position vector.
The structure ensures that dimensions never drop below zero.
using Meatcorps.Engine.Core.Data;
// Normal initialization
var size = new SizeF(100.5f, 50.0f);
// Attempting to set negative values results in 0
size.Width = -10.0f;
Console.WriteLine(size.Width); // Output: 0You can easily scale or adjust sizes using standard operators.
var baseSize = new SizeF(10.0f, 10.0f);
// Multiplication by scalar (Scaling)
var doubleSize = baseSize * 2.0f; // {Width:20 Height:20}
// Addition with a Vector2 (Adjusting width/height by specific amounts)
var offset = new Vector2(5.0f, -2.0f);
var adjustedSize = baseSize + offset; // {Width:15 Height:8}Since SizeF behaves similarly to a vector, it can be implicitly converted.
SizeF size = new SizeF(1920, 1080);
// Implicit conversion to Vector2
Vector2 vec = size;
// Explicit conversion back
SizeF sizeFromVec = (SizeF)new Vector2(800, 600);Standard equality checks and predefined constants are available.
if (mySize == SizeF.Zero)
{
// Handle empty size
}
bool isSame = new SizeF(5, 5).Equals(SizeF.One * 5f); // True| Member | Description |
|---|---|
| Width / Height | Properties that clamp input values to [0, float.MaxValue]. |
| ToVector2() | Converts the size to a standard Vector2. |
| Implicit Operators | Allows seamless transition between SizeF and Vector2. |
| SizeF.Zero | Static shortcut for new SizeF(0, 0). |
| SizeF.One | Static shortcut for new SizeF(1, 1). |