From 1cc26c871d1cf3ceba5428730b44120e09c03ae8 Mon Sep 17 00:00:00 2001 From: Wojciech Figat Date: Tue, 7 Dec 2021 14:58:54 +0100 Subject: [PATCH] Fixes for regression in tree UI layout after recent refactor --- Source/Editor/GUI/Tree/Tree.cs | 72 ++++++++++++++------------ Source/Editor/GUI/Tree/TreeNode.cs | 65 ++++++++++++----------- Source/Editor/Modules/SceneModule.cs | 3 -- Source/Editor/Modules/WindowsModule.cs | 6 ++- Source/Editor/Windows/ContentWindow.cs | 2 +- 5 files changed, 76 insertions(+), 72 deletions(-) diff --git a/Source/Editor/GUI/Tree/Tree.cs b/Source/Editor/GUI/Tree/Tree.cs index 4eb6607bf..788535a57 100644 --- a/Source/Editor/GUI/Tree/Tree.cs +++ b/Source/Editor/GUI/Tree/Tree.cs @@ -333,30 +333,6 @@ namespace FlaxEditor.GUI.Tree } } - /// - /// Updates the tree size. - /// - public void UpdateSize() - { - if (!_autoSize) - return; - - // Use max of parent clint area width and root node width - float width = 0; - if (HasParent) - width = Parent.GetClientArea().Width; - var rightBottom = Vector2.Zero; - for (int i = 0; i < _children.Count; i++) - { - if (_children[i] is TreeNode node && node.Visible) - { - width = Mathf.Max(width, node.MinimumWidth); - rightBottom = Vector2.Max(rightBottom, node.BottomRight); - } - } - Size = new Vector2(width + _margin.Width, rightBottom.Y + _margin.Bottom); - } - /// public override void Update(float deltaTime) { @@ -521,25 +497,43 @@ namespace FlaxEditor.GUI.Tree base.OnGotFocus(); } - /// - public override void OnChildResized(Control control) - { - UpdateSize(); - - base.OnChildResized(control); - } - /// public override void OnParentResized() { - UpdateSize(); + PerformLayout(); base.OnParentResized(); } + /// + protected override void PerformLayoutBeforeChildren() + { + if (_autoSize) + { + // Use max of parent clint area width and root node width + var parent = Parent; + var width = parent != null ? Mathf.Max(parent.GetClientArea().Width, 0) : 0.0f; + for (int i = 0; i < _children.Count; i++) + { + if (_children[i] is TreeNode node && node.Visible) + width = Mathf.Max(width, node.MinimumWidth); + } + for (int i = 0; i < _children.Count; i++) + { + if (_children[i] is TreeNode node && node.Visible) + node.Width = width; + } + Width = width + _margin.Width; + } + + base.PerformLayoutBeforeChildren(); + } + /// protected override void PerformLayoutAfterChildren() { + base.PerformLayoutAfterChildren(); + // Arrange children float y = _margin.Top; for (int i = 0; i < _children.Count; i++) @@ -551,7 +545,17 @@ namespace FlaxEditor.GUI.Tree } } - UpdateSize(); + if (_autoSize) + { + // Update height based on the nodes + var bottom = 0.0f; + for (int i = 0; i < _children.Count; i++) + { + if (_children[i] is TreeNode node && node.Visible) + bottom = Mathf.Max(bottom, node.Bottom); + } + Height = bottom + _margin.Bottom; + } } } } diff --git a/Source/Editor/GUI/Tree/TreeNode.cs b/Source/Editor/GUI/Tree/TreeNode.cs index d65848589..95c2acaa2 100644 --- a/Source/Editor/GUI/Tree/TreeNode.cs +++ b/Source/Editor/GUI/Tree/TreeNode.cs @@ -325,23 +325,18 @@ namespace FlaxEditor.GUI.Tree ExpandAllParents(noAnimation); // Change state + if (_opened && _animationProgress >= 1.0f) + return; bool prevState = _opened; _opened = true; - if (prevState != _opened) - _animationProgress = 1.0f - _animationProgress; - if (noAnimation) - { - // Speed up an animation _animationProgress = 1.0f; - } + else if (prevState != _opened) + _animationProgress = 1.0f - _animationProgress; // Update OnExpandedChanged(); - if (HasParent) - Parent.PerformLayout(); - else - PerformLayout(); + OnExpandAnimationChanged(); } /// @@ -351,23 +346,18 @@ namespace FlaxEditor.GUI.Tree public void Collapse(bool noAnimation = false) { // Change state + if (!_opened && _animationProgress >= 1.0f) + return; bool prevState = _opened; _opened = false; - if (prevState != _opened) - _animationProgress = 1.0f - _animationProgress; - if (noAnimation) - { - // Speed up an animation _animationProgress = 1.0f; - } + else if (prevState != _opened) + _animationProgress = 1.0f - _animationProgress; // Update OnExpandedChanged(); - if (HasParent) - Parent.PerformLayout(); - else - PerformLayout(); + OnExpandAnimationChanged(); } /// @@ -433,7 +423,7 @@ namespace FlaxEditor.GUI.Tree if (_animationProgress < 1.0f) { _animationProgress = 1.0f; - PerformLayout(); + OnExpandAnimationChanged(); } } @@ -521,6 +511,19 @@ namespace FlaxEditor.GUI.Tree { } + /// + /// Called when expand/collapse animation progress changes. + /// + protected virtual void OnExpandAnimationChanged() + { + if (ParentTree != null) + ParentTree.PerformLayout(); + else if (Parent != null) + Parent.PerformLayout(); + else + PerformLayout(); + } + /// /// Tests the header hit. /// @@ -595,7 +598,7 @@ namespace FlaxEditor.GUI.Tree } // Arrange controls - PerformLayout(); + OnExpandAnimationChanged(); } // Check for long press @@ -894,7 +897,6 @@ namespace FlaxEditor.GUI.Tree return; PerformLayout(); - ParentTree.UpdateSize(); base.OnChildResized(control); } @@ -1022,9 +1024,14 @@ namespace FlaxEditor.GUI.Tree if (!wasLocked) LockChildrenRecursive(); + // Auto-size tree nodes to match the parent size + var parent = Parent; + var width = parent is TreeNode ? parent.Width : Width; + // Optimize layout logic if node is collapsed if (_opened || _animationProgress < 1.0f) { + Width = width; PerformLayoutBeforeChildren(); for (int i = 0; i < _children.Count; i++) _children[i].PerformLayout(true); @@ -1035,7 +1042,7 @@ namespace FlaxEditor.GUI.Tree // TODO: perform layout for any non-TreeNode controls _cachedHeight = _headerHeight; _cachedTextColor = CacheTextColor(); - Size = new Vector2(Parent?.Width ?? Width, _headerHeight); + Size = new Vector2(width, _headerHeight); } if (!wasLocked) @@ -1070,8 +1077,6 @@ namespace FlaxEditor.GUI.Tree if (_opened || _animationProgress < 1.0f) { y -= _cachedHeight * (_opened ? 1.0f - _animationProgress : _animationProgress); - - // Arrange children for (int i = 0; i < _children.Count; i++) { if (_children[i] is TreeNode node && node.Visible) @@ -1085,21 +1090,15 @@ namespace FlaxEditor.GUI.Tree } } - // Cache data _cachedHeight = height; _cachedTextColor = CacheTextColor(); - - // Set bounds - Size = new Vector2(Parent?.Width ?? Width, Mathf.Max(_headerHeight, y)); + Height = Mathf.Max(_headerHeight, y); } /// protected override void OnParentChangedInternal() { - // Clear cached tree _tree = null; - if (Parent != null) - Width = Parent.Width; base.OnParentChangedInternal(); } diff --git a/Source/Editor/Modules/SceneModule.cs b/Source/Editor/Modules/SceneModule.cs index 4a583a0d8..9106088b5 100644 --- a/Source/Editor/Modules/SceneModule.cs +++ b/Source/Editor/Modules/SceneModule.cs @@ -412,11 +412,8 @@ namespace FlaxEditor.Modules // Add to the tree var rootNode = Root.TreeNode; rootNode.IsLayoutLocked = true; - // sceneNode.ParentNode = Root; rootNode.SortChildren(); - // - treeNode.UnlockChildrenRecursive(); rootNode.IsLayoutLocked = false; rootNode.Parent.PerformLayout(); diff --git a/Source/Editor/Modules/WindowsModule.cs b/Source/Editor/Modules/WindowsModule.cs index 80714924d..5ecd798c2 100644 --- a/Source/Editor/Modules/WindowsModule.cs +++ b/Source/Editor/Modules/WindowsModule.cs @@ -350,7 +350,7 @@ namespace FlaxEditor.Modules { // Perform layout var windowGUI = window.GUI; - windowGUI.UnlockChildrenRecursive(); + windowGUI.IsLayoutLocked = false; windowGUI.PerformLayout(); // Show @@ -379,6 +379,10 @@ namespace FlaxEditor.Modules Editor.LogWarning(ex); return false; } + finally + { + masterPanel.PerformLayout(); + } return true; } diff --git a/Source/Editor/Windows/ContentWindow.cs b/Source/Editor/Windows/ContentWindow.cs index 011df0fe9..6415db889 100644 --- a/Source/Editor/Windows/ContentWindow.cs +++ b/Source/Editor/Windows/ContentWindow.cs @@ -803,7 +803,7 @@ namespace FlaxEditor.Windows NavigationClearHistory(); // Update UI layout - UnlockChildrenRecursive(); + _isLayoutLocked = false; PerformLayout(); // Load last viewed folder