diff --git a/game/addons/tools/Code/Scene/SceneView/SceneViewportWidget.Sizing.cs b/game/addons/tools/Code/Scene/SceneView/SceneViewportWidget.Sizing.cs index 3e266012..f2a118f7 100644 --- a/game/addons/tools/Code/Scene/SceneView/SceneViewportWidget.Sizing.cs +++ b/game/addons/tools/Code/Scene/SceneView/SceneViewportWidget.Sizing.cs @@ -31,6 +31,17 @@ public void SetDefaultSize() /// 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; @@ -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 @@ -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; } }