From d9f3fe186d03716ac3fc7a3d73036a27ae3851dc Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Mon, 2 Oct 2023 00:13:29 -0400 Subject: [PATCH] Implement function to properly get the size of the value editor for an InputBox. --- Source/Editor/Surface/Elements/InputBox.cs | 30 ++++++ Source/Editor/Surface/SurfaceNode.cs | 113 +++++++-------------- 2 files changed, 64 insertions(+), 79 deletions(-) diff --git a/Source/Editor/Surface/Elements/InputBox.cs b/Source/Editor/Surface/Elements/InputBox.cs index 4eac03105..5b0a43ca9 100644 --- a/Source/Editor/Surface/Elements/InputBox.cs +++ b/Source/Editor/Surface/Elements/InputBox.cs @@ -1652,5 +1652,35 @@ namespace FlaxEditor.Surface.Elements } } } + + /// + /// Get the size of the value editor for this box. + /// + /// The size of the value editor for this box. + public Float2 GetValueEditorSize() + { + if (!HasValue) + return Float2.Zero; + + if (_defaultValueEditor != null) + return _defaultValueEditor.Bounds.Size; + + for (int i = 0; i < DefaultValueEditors.Count; i++) + { + if (DefaultValueEditors[i].CanUse(this, ref _currentType)) + { + var bounds = new Rectangle(X + Width + 8 + Style.Current.FontSmall.MeasureText(Text).X, Y, 90, Height); + _editor = DefaultValueEditors[i]; + + // TODO: Find a better way to evaluate the size than using existing create code to resolve the size for each editor type. + var tempEditor = _editor.Create(this, ref bounds); + Float2 editorSize = tempEditor.Size; + tempEditor.Dispose(); + return editorSize; + } + } + + return Float2.Zero; + } } } diff --git a/Source/Editor/Surface/SurfaceNode.cs b/Source/Editor/Surface/SurfaceNode.cs index d6275e439..c18698890 100644 --- a/Source/Editor/Surface/SurfaceNode.cs +++ b/Source/Editor/Surface/SurfaceNode.cs @@ -180,40 +180,6 @@ namespace FlaxEditor.Surface } } - private Float2 GetBoxControlWidthHeight(Control control, Font boxLabelFont) - { - float boxWidth = 0; - float boxHeight = 0; - - if (control is InputBox inputBox) - { - boxWidth = boxLabelFont.MeasureText(inputBox.Text).X + 20; - if (inputBox.DefaultValueEditor != null) - boxWidth += inputBox.DefaultValueEditor.Width + 4; - boxHeight = inputBox.Archetype.Position.Y - Constants.NodeMarginY - Constants.NodeHeaderSize + 20.0f; - } - else if (control is OutputBox outputBox) - { - boxWidth = boxLabelFont.MeasureText(outputBox.Text).X + 20; - boxHeight = outputBox.Archetype.Position.Y - Constants.NodeMarginY - Constants.NodeHeaderSize + 20.0f; - } - else if (control is Control defaultControl) - { - if (defaultControl.AnchorPreset == AnchorPresets.TopLeft) - { - boxWidth = defaultControl.Right + 4 - Constants.NodeMarginX; - boxHeight = defaultControl.Bottom + 4 - Constants.NodeMarginY - Constants.NodeHeaderSize; - } - else - { - boxWidth = defaultControl.Width + 4; - boxHeight = defaultControl.Height + 4; - } - } - - return new Float2(boxWidth, boxHeight); - } - public ContainerControl HACK = null; private Float2 CompareAndGetNewCollisionSize(Rectangle rect1, Rectangle rect2, float collisionWidth, float collisionHeight) @@ -370,6 +336,39 @@ namespace FlaxEditor.Surface return color; } + private Float2 GetBoxControlWidthHeight(Control control, Font boxLabelFont) + { + float boxWidth = 0; + float boxHeight = 0; + + if (control is InputBox inputBox) + { + boxWidth = boxLabelFont.MeasureText(inputBox.Text).X + 24; + boxWidth += inputBox.GetValueEditorSize().X + 8; + boxHeight = inputBox.Archetype.Position.Y - Constants.NodeMarginY - Constants.NodeHeaderSize + 20.0f; + } + else if (control is OutputBox outputBox) + { + boxWidth = boxLabelFont.MeasureText(outputBox.Text).X + 24; + boxHeight = outputBox.Archetype.Position.Y - Constants.NodeMarginY - Constants.NodeHeaderSize + 20.0f; + } + else if (control is Control defaultControl) + { + if (defaultControl.AnchorPreset == AnchorPresets.TopLeft) + { + boxWidth = defaultControl.Right + 4 - Constants.NodeMarginX; + boxHeight = defaultControl.Bottom + 4 - Constants.NodeMarginY - Constants.NodeHeaderSize; + } + else + { + boxWidth = defaultControl.Width + 4; + boxHeight = defaultControl.Height + 4; + } + } + + return new Float2(boxWidth, boxHeight); + } + /// /// Automatically resizes the node to match the title size and all the elements for best fit of the node dimensions. /// @@ -414,11 +413,6 @@ namespace FlaxEditor.Surface height = Mathf.Max(height, boxSize.Y); } } - Debug.Log(Title); - Float2 collisionSize = CalculateCollisionSize(Children, boxLabelFont); - Debug.Log(collisionSize.ToString()); - //width += collisionSize.X; - //height += collisionSize.Y; width = Mathf.Max(width, leftWidth + rightWidth + 10); width = Mathf.Max(width, titleLabelFont.MeasureText(Title).X + 30); @@ -442,49 +436,10 @@ namespace FlaxEditor.Surface Parent = HACK }; - /*Rectangle testRect = Rectangle.Default; - testRect.Width = width; - testRect.Height = height; - testRect.Y += 10; // ra - new Panel - { - BackgroundColor = colliderColor, - Location = testRect.Location, - Size = testRect.Size, - Parent = HACK - };*/ - Float2 roundedSize = VisjectSurface.RoundToGrid(new Float2(width, height), ceil: true); - //Resize(roundedSize.X, roundedSize.Y); + Resize(roundedSize.X, roundedSize.Y); } - /* if (child is InputBox inputBox) -{ - var boxWidth = boxLabelFont.MeasureText(inputBox.Text).X + 20; - if (inputBox.DefaultValueEditor != null) - boxWidth += inputBox.DefaultValueEditor.Width + 4; - leftWidth = Mathf.Max(leftWidth, boxWidth); - leftHeight = Mathf.Max(leftHeight, inputBox.Archetype.Position.Y - Constants.NodeMarginY - Constants.NodeHeaderSize + 20.0f); -} -else if (child is OutputBox outputBox) -{ - rightWidth = Mathf.Max(rightWidth, boxLabelFont.MeasureText(outputBox.Text).X + 20); - rightHeight = Mathf.Max(rightHeight, outputBox.Archetype.Position.Y - Constants.NodeMarginY - Constants.NodeHeaderSize + 20.0f); -} -else if (child is Control control) -{ - if (control.AnchorPreset == AnchorPresets.TopLeft) - { - width = Mathf.Max(width, control.Right + 4 - Constants.NodeMarginX); - height = Mathf.Max(height, control.Bottom + 4 - Constants.NodeMarginY - Constants.NodeHeaderSize); - } - else - { - width = Mathf.Max(width, control.Width + 4); - height = Mathf.Max(height, control.Height + 4); - } -}*/ - /// /// Creates an element from the archetype and adds the element to the node. ///