Add debug view for collision boxes.
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
|
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using FlaxEditor.Scripting;
|
using FlaxEditor.Scripting;
|
||||||
using FlaxEngine;
|
using FlaxEngine;
|
||||||
|
using FlaxEngine.GUI;
|
||||||
|
|
||||||
namespace FlaxEditor.Surface
|
namespace FlaxEditor.Surface
|
||||||
{
|
{
|
||||||
@@ -178,10 +181,50 @@ namespace FlaxEditor.Surface
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public Func<Elements.Box, ScriptType, ScriptType> DependentBoxFilter;
|
public Func<Elements.Box, ScriptType, ScriptType> DependentBoxFilter;
|
||||||
|
|
||||||
|
private NodeElementArchetype[] _elements;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Array with default elements descriptions.
|
/// Array with default elements descriptions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public NodeElementArchetype[] Elements;
|
public NodeElementArchetype[] Elements
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _elements;
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_elements = value;
|
||||||
|
|
||||||
|
/*Float2 topLeft = Float2.Zero;
|
||||||
|
Float2 bottomRight = Float2.Zero;
|
||||||
|
List<Float2[]> textRectangles = new List<Float2[]>();
|
||||||
|
|
||||||
|
foreach (NodeElementArchetype nodeElementType in _elements)
|
||||||
|
{
|
||||||
|
bool isInputElement = nodeElementType.Type == NodeElementType.Input;
|
||||||
|
bool isOutputElement = nodeElementType.Type == NodeElementType.Output;
|
||||||
|
if (isInputElement)
|
||||||
|
{
|
||||||
|
// Text will be to the right
|
||||||
|
}
|
||||||
|
|
||||||
|
// In case of negatives.. most likely not needed.
|
||||||
|
topLeft.X = Math.Min(topLeft.X, nodeElementType.Position.X);
|
||||||
|
topLeft.Y = Math.Min(topLeft.Y, nodeElementType.Position.Y);
|
||||||
|
|
||||||
|
bottomRight.X = Math.Max(bottomRight.X, nodeElementType.Position.X + nodeElementType.Size.X);
|
||||||
|
bottomRight.Y = Math.Max(bottomRight.Y, nodeElementType.Position.Y + nodeElementType.Size.Y);
|
||||||
|
}
|
||||||
|
|
||||||
|
float paddingConst = 15;
|
||||||
|
|
||||||
|
float sizeXElements = bottomRight.X - topLeft.X + paddingConst;
|
||||||
|
float sizeYElements = bottomRight.Y - topLeft.Y + paddingConst;
|
||||||
|
float titleSize = Style.Current.FontLarge.MeasureText(Title).X + paddingConst;
|
||||||
|
|
||||||
|
Size = new Float2(Math.Max(sizeXElements, titleSize), sizeYElements);*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Tries to parse some text and extract the data from it.
|
/// Tries to parse some text and extract the data from it.
|
||||||
|
|||||||
@@ -132,8 +132,8 @@ namespace FlaxEditor.Surface
|
|||||||
{
|
{
|
||||||
Type = NodeElementType.Input,
|
Type = NodeElementType.Input,
|
||||||
Position = new Float2(
|
Position = new Float2(
|
||||||
Constants.NodeMarginX - Constants.BoxOffsetX,
|
Constants.NodeMarginX - Constants.BoxOffsetX,
|
||||||
Constants.NodeMarginY + Constants.NodeHeaderSize + yLevel * Constants.LayoutOffsetY),
|
Constants.NodeMarginY + Constants.NodeHeaderSize + yLevel * Constants.LayoutOffsetY),
|
||||||
Text = text,
|
Text = text,
|
||||||
Single = single,
|
Single = single,
|
||||||
ValueIndex = valueIndex,
|
ValueIndex = valueIndex,
|
||||||
|
|||||||
@@ -165,7 +165,7 @@ namespace FlaxEditor.Surface
|
|||||||
{
|
{
|
||||||
if (Surface == null)
|
if (Surface == null)
|
||||||
return;
|
return;
|
||||||
Size = CalculateNodeSize(width, height);
|
Size = CalculateNodeSize(width, height);
|
||||||
|
|
||||||
// Update boxes on width change
|
// Update boxes on width change
|
||||||
//if (!Mathf.NearEqual(prevSize.X, Size.X))
|
//if (!Mathf.NearEqual(prevSize.X, Size.X))
|
||||||
@@ -180,6 +180,109 @@ 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)
|
||||||
|
{
|
||||||
|
Rectangle sharedArea;
|
||||||
|
Rectangle.Shared(ref rect1, ref rect2, out sharedArea);
|
||||||
|
|
||||||
|
Color colliderColor = Color.Chocolate;
|
||||||
|
colliderColor.A = 0.1f;
|
||||||
|
Panel colliderPanel = new Panel
|
||||||
|
{
|
||||||
|
BackgroundColor = colliderColor,
|
||||||
|
Location = sharedArea.Location,
|
||||||
|
Size = sharedArea.Size,
|
||||||
|
Parent = HACK
|
||||||
|
};
|
||||||
|
|
||||||
|
return new Float2(Mathf.Max(collisionWidth, sharedArea.Width + 4), Mathf.Max(collisionHeight, sharedArea.Height + 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Float2 CalculateCollisionSize(List<Control> controls, Font boxLabelFont)
|
||||||
|
{
|
||||||
|
List<Rectangle> colliderRectangles = new List<Rectangle>();
|
||||||
|
int controlsCount = controls.Count;
|
||||||
|
for (int i = 0; i < controlsCount; i++)
|
||||||
|
{
|
||||||
|
var control = controls[i];
|
||||||
|
if (!control.Visible || control is Panel panel)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Float2 boxSize = GetBoxControlWidthHeight(control, boxLabelFont);
|
||||||
|
|
||||||
|
Rectangle controlRect = new Rectangle(control.X, control.Y, boxSize.X, boxSize.Y);
|
||||||
|
colliderRectangles.Add(controlRect);
|
||||||
|
|
||||||
|
Color colliderColor = Style.Current.BackgroundSelected;
|
||||||
|
colliderColor.A = 0.25f;
|
||||||
|
Panel colliderPanel = new Panel
|
||||||
|
{
|
||||||
|
BackgroundColor = colliderColor,
|
||||||
|
Location = controlRect.Location,
|
||||||
|
Size = controlRect.Size,
|
||||||
|
Parent = HACK
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
float collisionWidth = 0;
|
||||||
|
float collisionHeight = 0;
|
||||||
|
for (int i = 0; i < colliderRectangles.Count; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < colliderRectangles.Count; j++)
|
||||||
|
{
|
||||||
|
if (i == j)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Rectangle rect1 = colliderRectangles[i];
|
||||||
|
Rectangle rect2 = colliderRectangles[j];
|
||||||
|
Float2 newCollisionSize = CompareAndGetNewCollisionSize(rect1, rect2, collisionWidth, collisionHeight);
|
||||||
|
|
||||||
|
collisionWidth = newCollisionSize.X;
|
||||||
|
collisionHeight = newCollisionSize.Y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Float2(collisionWidth, collisionHeight);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Automatically resizes the node to match the title size and all the elements for best fit of the node dimensions.
|
/// Automatically resizes the node to match the title size and all the elements for best fit of the node dimensions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -187,6 +290,7 @@ namespace FlaxEditor.Surface
|
|||||||
{
|
{
|
||||||
if (Surface == null)
|
if (Surface == null)
|
||||||
return;
|
return;
|
||||||
|
HACK = this;
|
||||||
var width = 0.0f;
|
var width = 0.0f;
|
||||||
var height = 0.0f;
|
var height = 0.0f;
|
||||||
var leftHeight = 0.0f;
|
var leftHeight = 0.0f;
|
||||||
@@ -195,44 +299,75 @@ namespace FlaxEditor.Surface
|
|||||||
var rightWidth = 40.0f;
|
var rightWidth = 40.0f;
|
||||||
var boxLabelFont = Style.Current.FontSmall;
|
var boxLabelFont = Style.Current.FontSmall;
|
||||||
var titleLabelFont = Style.Current.FontLarge;
|
var titleLabelFont = Style.Current.FontLarge;
|
||||||
for (int i = 0; i < Children.Count; i++)
|
int childrenCount = Children.Count;
|
||||||
|
for (int i = 0; i < childrenCount; i++)
|
||||||
{
|
{
|
||||||
var child = Children[i];
|
var child = Children[i];
|
||||||
|
if (child is Panel panel)
|
||||||
|
{
|
||||||
|
panel.Visible = false;
|
||||||
|
}
|
||||||
if (!child.Visible)
|
if (!child.Visible)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
Float2 boxSize = GetBoxControlWidthHeight(child, boxLabelFont);
|
||||||
if (child is InputBox inputBox)
|
if (child is InputBox inputBox)
|
||||||
{
|
{
|
||||||
var boxWidth = boxLabelFont.MeasureText(inputBox.Text).X + 20;
|
leftWidth = Mathf.Max(leftWidth, boxSize.X);
|
||||||
if (inputBox.DefaultValueEditor != null)
|
leftHeight = Mathf.Max(leftHeight, boxSize.Y);
|
||||||
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)
|
else if (child is OutputBox outputBox)
|
||||||
{
|
{
|
||||||
rightWidth = Mathf.Max(rightWidth, boxLabelFont.MeasureText(outputBox.Text).X + 20);
|
rightWidth = Mathf.Max(rightWidth, boxSize.X);
|
||||||
rightHeight = Mathf.Max(rightHeight, outputBox.Archetype.Position.Y - Constants.NodeMarginY - Constants.NodeHeaderSize + 20.0f);
|
rightHeight = Mathf.Max(rightHeight, boxSize.Y);
|
||||||
}
|
}
|
||||||
else if (child is Control control)
|
else
|
||||||
{
|
{
|
||||||
if (control.AnchorPreset == AnchorPresets.TopLeft)
|
width = Mathf.Max(width, boxSize.X);
|
||||||
{
|
height = Mathf.Max(height, boxSize.Y);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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, leftWidth + rightWidth + 10);
|
||||||
width = Mathf.Max(width, titleLabelFont.MeasureText(Title).X + 30);
|
width = Mathf.Max(width, titleLabelFont.MeasureText(Title).X + 30);
|
||||||
height = Mathf.Max(height, Mathf.Max(leftHeight, rightHeight));
|
height = Mathf.Max(height, Mathf.Max(leftHeight, rightHeight));
|
||||||
Resize(width, height);
|
|
||||||
|
Float2 roundedSize = VisjectSurface.RoundToGrid(new Float2(width, height), true);
|
||||||
|
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>
|
/// <summary>
|
||||||
/// Creates an element from the archetype and adds the element to the node.
|
/// Creates an element from the archetype and adds the element to the node.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -310,6 +445,9 @@ namespace FlaxEditor.Surface
|
|||||||
Elements.Add(element);
|
Elements.Add(element);
|
||||||
if (element is Control control)
|
if (element is Control control)
|
||||||
AddChild(control);
|
AddChild(control);
|
||||||
|
|
||||||
|
if (!(element is Panel panel))
|
||||||
|
ResizeAuto();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user