Prototype AspectRatioPanel UI control

Wrote this as a quicky, needed it for something. Don't feel like stabilizing it yet so it's internal.
This commit is contained in:
PJB3005
2025-12-05 17:33:51 +01:00
parent d579c68082
commit dff5b5fe95

View File

@@ -0,0 +1,57 @@
using System.Numerics;
using Robust.Shared.Maths;
namespace Robust.Client.UserInterface.Controls;
// TODO BEFORE MAKING PUBLIC:
// Do we need to constrain the aspect ratio in MeasureOverride() too?
// Doc comments
/// <summary>
/// A simple UI control that ensures its children are laid out with a fixed aspect ratio.
/// </summary>
internal sealed class AspectRatioPanel : Control
{
public AspectRatio AspectRatio
{
get;
set
{
field = value;
InvalidateArrange();
}
} = AspectRatio.One;
protected override Vector2 ArrangeOverride(Vector2 finalSize)
{
var givenRatio = finalSize.X / finalSize.Y;
if (!MathHelper.CloseTo(givenRatio, AspectRatio.Ratio))
{
if (givenRatio < AspectRatio.Ratio)
{
// Too narrow, derive height from width.
finalSize = finalSize with { Y = finalSize.X / AspectRatio.Ratio };
}
else
{
// Too wide, derive width from height.
finalSize = finalSize with { X = finalSize.Y * AspectRatio.Ratio };
}
}
return base.ArrangeOverride(finalSize);
}
}
internal struct AspectRatio(float ratio)
{
public static readonly AspectRatio One = new(1);
public float Ratio = ratio;
public static AspectRatio FromWidthHeight(float width, float height)
{
return new AspectRatio(width / height);
}
}