From fa68a5f5722edbfece6fd8ddbe145fad88a8ffb8 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sun, 16 Mar 2025 22:57:18 +0100 Subject: [PATCH] Fix UI Controls layout auto-resizing to always use pivot-relative logic #3031 --- Source/Engine/UI/GUI/Common/Label.cs | 4 +--- Source/Engine/UI/GUI/Control.Bounds.cs | 13 +++++++++++++ Source/Engine/UI/GUI/Panels/DropPanel.cs | 3 ++- Source/Engine/UI/GUI/Panels/HorizontalPanel.cs | 2 +- Source/Engine/UI/GUI/Panels/VerticalPanel.cs | 2 +- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Source/Engine/UI/GUI/Common/Label.cs b/Source/Engine/UI/GUI/Common/Label.cs index dca68948c..7cb3e4d42 100644 --- a/Source/Engine/UI/GUI/Common/Label.cs +++ b/Source/Engine/UI/GUI/Common/Label.cs @@ -337,9 +337,7 @@ namespace FlaxEngine.GUI size.X = _textSize.X + Margin.Width; if (_autoHeight) size.Y = _textSize.Y + Margin.Height; - var pivotRelative = PivotRelative; - Size = size; - PivotRelative = pivotRelative; + Resize(ref size); } } } diff --git a/Source/Engine/UI/GUI/Control.Bounds.cs b/Source/Engine/UI/GUI/Control.Bounds.cs index 6054250f8..de2711b25 100644 --- a/Source/Engine/UI/GUI/Control.Bounds.cs +++ b/Source/Engine/UI/GUI/Control.Bounds.cs @@ -419,6 +419,19 @@ namespace FlaxEngine.GUI } } + /// + /// Resizes the control based on where the pivot is rather than just the top-left. + /// + [NoAnimate] + public void Resize(ref Float2 value) + { + if (_bounds.Size.Equals(ref value)) + return; + var bounds = new Rectangle(_bounds.Location, value); + bounds.Location += (_bounds.Size - value) * Pivot; // Pivot-relative resizing + SetBounds(ref bounds); + } + /// /// Updates the control cached bounds (based on anchors and offsets). /// diff --git a/Source/Engine/UI/GUI/Panels/DropPanel.cs b/Source/Engine/UI/GUI/Panels/DropPanel.cs index e053edd9c..28a1fb8c9 100644 --- a/Source/Engine/UI/GUI/Panels/DropPanel.cs +++ b/Source/Engine/UI/GUI/Panels/DropPanel.cs @@ -585,7 +585,8 @@ namespace FlaxEngine.GUI _cachedHeight = height; if (_animationProgress >= 1.0f && _isClosed) y = minHeight; - Height = Mathf.Max(minHeight, y); + var size = new Float2(Width, Mathf.Max(minHeight, y)); + Resize(ref size); } /// diff --git a/Source/Engine/UI/GUI/Panels/HorizontalPanel.cs b/Source/Engine/UI/GUI/Panels/HorizontalPanel.cs index 1c8e646e3..06d185f2d 100644 --- a/Source/Engine/UI/GUI/Panels/HorizontalPanel.cs +++ b/Source/Engine/UI/GUI/Panels/HorizontalPanel.cs @@ -78,7 +78,7 @@ namespace FlaxEngine.GUI size.X = left + right; if (!ControlChildSize) size.Y = maxHeight; - Size = size; + Resize(ref size); } else if (_alignment != TextAlignment.Near && hasAnyLeft) { diff --git a/Source/Engine/UI/GUI/Panels/VerticalPanel.cs b/Source/Engine/UI/GUI/Panels/VerticalPanel.cs index aae95bd43..d5c6dfb9f 100644 --- a/Source/Engine/UI/GUI/Panels/VerticalPanel.cs +++ b/Source/Engine/UI/GUI/Panels/VerticalPanel.cs @@ -78,7 +78,7 @@ namespace FlaxEngine.GUI size.Y = top + bottom; if (!ControlChildSize) size.X = maxWidth; - Size = size; + Resize(ref size); } else if (_alignment != TextAlignment.Near && hasAnyTop) {