Implement function to properly get the size of the value editor for an InputBox.

This commit is contained in:
Menotdan
2023-10-02 00:13:29 -04:00
parent 032793d202
commit d9f3fe186d
2 changed files with 64 additions and 79 deletions

View File

@@ -1652,5 +1652,35 @@ namespace FlaxEditor.Surface.Elements
}
}
}
/// <summary>
/// Get the size of the value editor for this box.
/// </summary>
/// <returns>The size of the value editor for this box.</returns>
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;
}
}
}

View File

@@ -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);
}
/// <summary>
/// Automatically resizes the node to match the title size and all the elements for best fit of the node dimensions.
/// </summary>
@@ -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);
}
}*/
/// <summary>
/// Creates an element from the archetype and adds the element to the node.
/// </summary>