Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ public void SetDefaultSize()
/// <param name="aspectRatio"></param>
public void SetAspectRatio( float aspectRatio )
{

MinimumSize = Vector2.Zero;
MaximumSize = QT_MAX_SIZE;
FixedSize = QT_MAX_SIZE;

// Don't update if the aspect ratio hasn't changed
if ( ForcedAspectRatio.HasValue && ForcedAspectRatio.Value.AlmostEqual( aspectRatio ) && !ForcedSize.HasValue )
{
return;
}

ForcedAspectRatio = aspectRatio;
ForcedSize = null;

Expand Down Expand Up @@ -59,6 +70,20 @@ private void UpdateSizeConstraints()
MaximumSize = size;
FixedSize = size;
}
else if ( ForcedAspectRatio.HasValue )
{
if ( Parent != null )
{
var contentRect = Parent.ContentRect;
var parentSize = new Vector2( contentRect.Width, contentRect.Height );
MaximumSize = parentSize;
}
else
{
MaximumSize = new Vector2( QT_MAX_SIZE, QT_MAX_SIZE );
}
FixedSize = QT_MAX_SIZE;
}
else
{
// For aspect ratio mode or free, don't lock the size - let it be dynamic
Expand All @@ -68,41 +93,58 @@ private void UpdateSizeConstraints()

Layout.SizeConstraint = SizeConstraint.SetDefaultConstraint;
SetSizeMode( SizeMode.Expand, SizeMode.Expand );

UpdateGeometry();
AdjustSize();
}

Vector2? defaultSize;

protected override Vector2 SizeHint()
{
// Exact size, easy
if ( ForcedSize.HasValue )
{
return ForcedSize.Value;
}

// Free
if ( !ForcedAspectRatio.HasValue )
{
return base.SizeHint();
}

// Dynamically calculate size based on current parent size
var parentSize = Parent?.Size ?? base.SizeHint();
var parentAspect = parentSize.x / parentSize.y;
if ( defaultSize == null )
{
defaultSize = Size;
}

var availableSize = defaultSize.Value;
if ( availableSize.x <= 0 || availableSize.y <= 0 )
{
return base.SizeHint();
}

var aspectRatio = ForcedAspectRatio.Value;

if ( aspectRatio > parentAspect )
if ( aspectRatio <= 0 || float.IsNaN( aspectRatio ) || float.IsInfinity( aspectRatio ) )
{
return availableSize;
}

var availableAspect = availableSize.x / availableSize.y;
Vector2 result;
if ( aspectRatio > availableAspect )
{
// Fit to width
return new Vector2( parentSize.x, parentSize.x / aspectRatio );
result = new Vector2( availableSize.x, availableSize.x / aspectRatio );
}
else
{
// Fit to height
return new Vector2( parentSize.y * aspectRatio, parentSize.y );
result = new Vector2( availableSize.y * aspectRatio, availableSize.y );
}

if ( MaximumSize.x > 0 && MaximumSize.y > 0 )
{
result = new Vector2( MathF.Min( result.x, MaximumSize.x ), MathF.Min( result.y, MaximumSize.y ) );
}

return result;
}
}