diff --git a/Source/Editor/Surface/VisjectSurface.Formatting.cs b/Source/Editor/Surface/VisjectSurface.Formatting.cs
index 723f8555f..e53ed4a3c 100644
--- a/Source/Editor/Surface/VisjectSurface.Formatting.cs
+++ b/Source/Editor/Surface/VisjectSurface.Formatting.cs
@@ -152,6 +152,7 @@ namespace FlaxEditor.Surface
/// The number of the maximum layer
private int SetLayers(Dictionary nodeData, List endNodes)
{
+ // Longest path layering
int maxLayer = 0;
var stack = new Stack(endNodes);
@@ -193,8 +194,9 @@ namespace FlaxEditor.Surface
/// The number of the maximum offset
private int SetOffsets(Dictionary nodeData, List endNodes, int maxLayer)
{
- // This piece of code should be explained a bit better, since it does some fairly fancy stuff
int maxOffset = 0;
+
+ // Keeps track of the largest offset (Y axis) for every layer
int[] offsets = new int[maxLayer + 1];
var visitedNodes = new HashSet();
@@ -203,14 +205,18 @@ namespace FlaxEditor.Surface
{
if (!nodeData.TryGetValue(node, out var data)) return;
+ // If we realize that the current node would collide with an already existing node in this layer
if (data.Layer >= 0 && offsets[data.Layer] > data.Offset)
{
+ // Move the entire sub-tree down
straightParentData.SubtreeOffset = Math.Max(straightParentData.SubtreeOffset, offsets[data.Layer] - data.Offset);
}
+ // Keeps track of the offset of the last direct child we visited
int childOffset = data.Offset;
bool straightChild = true;
+ // Run the algorithm for every child
for (int i = 0; i < node.Elements.Count; i++)
{
if (node.Elements[i] is InputBox box && box.HasAnyConnection)
@@ -229,6 +235,7 @@ namespace FlaxEditor.Surface
if (data.Layer >= 0)
{
+ // When coming out of the recursion, apply the extra subtree offsets
data.Offset += straightParentData.SubtreeOffset;
if (data.Offset > maxOffset)
{