mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
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:
57
Robust.Client/UserInterface/Controls/AspectRatioPanel.cs
Normal file
57
Robust.Client/UserInterface/Controls/AspectRatioPanel.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user