From 85c219369d3194b10c4aa52524dcbb86990daeb2 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 3 Feb 2021 21:59:21 +0100 Subject: [PATCH] Fix updating UI layout when adding control Fixes #190 --- Source/Engine/UI/UIControl.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Source/Engine/UI/UIControl.cs b/Source/Engine/UI/UIControl.cs index e0f15a106..e4a6b4e33 100644 --- a/Source/Engine/UI/UIControl.cs +++ b/Source/Engine/UI/UIControl.cs @@ -30,10 +30,11 @@ namespace FlaxEngine return; // Cleanup previous - if (_control != null) + var prevControl = _control; + if (prevControl != null) { - _control.LocationChanged -= OnControlLocationChanged; - _control.Dispose(); + prevControl.LocationChanged -= OnControlLocationChanged; + prevControl.Dispose(); } // Set value @@ -42,16 +43,17 @@ namespace FlaxEngine // Link the new one (events and parent) if (_control != null) { + // Setup control var containerControl = _control as ContainerControl; if (containerControl != null) containerControl.UnlockChildrenRecursive(); - _control.Parent = GetParent(); _control.IndexInParent = OrderInParent; _control.Location = new Vector2(LocalPosition); // TODO: sync control order in parent with actor order in parent (think about special cases like Panel with scroll bars used as internal controls) _control.LocationChanged += OnControlLocationChanged; + // Link children UI controls if (containerControl != null && IsActiveInHierarchy) { var children = ChildrenCount; @@ -64,6 +66,12 @@ namespace FlaxEngine } } } + + // Refresh + if (prevControl == null && _control.Parent != null) + _control.Parent.PerformLayout(); + else + _control.PerformLayout(); } } }