Skip to content

Commit

Permalink
Fix node overlap issue by skipping the original node from AutoLayout …
Browse files Browse the repository at this point in the history
…repositioning (DynamoDS#11414)

* prevent overlap
  • Loading branch information
zeusongit authored and Zeus1717 committed Mar 2, 2021
1 parent 1e4040c commit a7f2082
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
51 changes: 49 additions & 2 deletions src/DynamoCore/Graph/Workspaces/LayoutExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class LayoutExtensions
/// </summary>
/// <param name="workspace">Workspace on which graph layout will be performed.</param>
/// <param name="reuseUndoRedoGroup">If true, skip initializing new undo action group.</param>
internal static List<GraphLayout.Graph> DoGraphAutoLayout(this WorkspaceModel workspace, bool reuseUndoRedoGroup = false)
internal static List<GraphLayout.Graph> DoGraphAutoLayout(this WorkspaceModel workspace, bool reuseUndoRedoGroup = false, bool isNodeAutoComplete = false, Guid? originalNodeGUID = null)
{
if (workspace.Nodes.Count() < 2) return null;

Expand Down Expand Up @@ -50,7 +50,14 @@ public static class LayoutExtensions
// Run layout algorithm for each subgraph
layoutSubgraphs.Skip(1).ToList().ForEach(g => RunLayoutSubgraph(g, isGroupLayout));
AvoidSubgraphOverlap(layoutSubgraphs);
SaveLayoutGraph(workspace, layoutSubgraphs);

if (isNodeAutoComplete)
{
SaveLayoutGraphForNodeAutoComplete(workspace, layoutSubgraphs, originalNodeGUID);
}
else {
SaveLayoutGraph(workspace, layoutSubgraphs);
}

// Restore the workspace model selection information
selection.ToList().ForEach(x => x.Select());
Expand Down Expand Up @@ -426,6 +433,46 @@ private static void SaveLayoutGraph(this WorkspaceModel workspace, List<GraphLay
node.ReportPosition();
workspace.HasUnsavedChanges = true;

double noteOffset = -n.NotesHeight;
foreach (NoteModel note in n.LinkedNotes)
{
if (note.IsSelected || DynamoSelection.Instance.Selection.Count == 0)
{
note.X = node.X;
note.Y = node.Y + noteOffset;
noteOffset += note.Height + GraphLayout.Graph.VerticalNoteDistance;
note.ReportPosition();
}
}
}
}
}
/// <summary>
/// This method pushes changes from the GraphLayout.Graph objects
/// back to the workspace models, but only for nodes placed by NodeAutocomplete.
/// </summary>
private static void SaveLayoutGraphForNodeAutoComplete(this WorkspaceModel workspace, List<GraphLayout.Graph> layoutSubgraphs, Guid? originalNodeGUID)
{
// Assign coordinates to nodes outside groups
foreach (var node in workspace.Nodes)
{
GraphLayout.Graph graph = layoutSubgraphs
.FirstOrDefault(g => g.FindNode(node.GUID) != null);

if (graph != null)
{
GraphLayout.Node n = graph.FindNode(node.GUID);
double offsetY = graph.OffsetY;
//skipping the original node to avoid jumping of node
if (node.GUID != originalNodeGUID )
{
node.X = n.X;
node.Y = n.Y + n.NotesHeight + offsetY;

}
node.ReportPosition();
workspace.HasUnsavedChanges = true;

double noteOffset = -n.NotesHeight;
foreach (NoteModel note in n.LinkedNotes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,12 @@ protected virtual void CreateAndConnectToPort(object parameter)
dynamoViewModel.ExecuteCommand(new DynamoModel.CreateAndConnectNodeCommand(id, initialNode.GUID,
Model.CreationName, 0, portModel.Index, adjustedX, 0, createAsDownStreamNode, false, true));

// Clear current selections and select all input nodes as we need to perform Auto layout on only the input nodes.
DynamoSelection.Instance.ClearSelection();
var inputNodes = initialNode.InputNodes.Values.Where(x => x != null).Select(y => y.Item2);

// Clear current selections and select all input nodes as we need to perform Auto layout on only the input nodes.
DynamoSelection.Instance.ClearSelection();
DynamoSelection.Instance.Selection.AddRange(inputNodes);
DynamoSelection.Instance.Selection.Add(initialNode);
}

protected virtual bool CanCreateAndConnectToPort(object parameter)
Expand All @@ -305,8 +306,9 @@ private void AutoLayoutNodes(object sender, EventArgs e)
{
var nodeView = (NodeView) sender;
var dynamoViewModel = nodeView.ViewModel.DynamoViewModel;
var originalNodeId = nodeView.ViewModel.NodeModel.OutputNodes.Values.SelectMany(s => s.Select(t => t.Item2)).Distinct().FirstOrDefault().GUID;

dynamoViewModel.CurrentSpace.DoGraphAutoLayout(true);
dynamoViewModel.CurrentSpace.DoGraphAutoLayout(true, true, originalNodeId);

DynamoSelection.Instance.ClearSelection();

Expand Down

0 comments on commit a7f2082

Please sign in to comment.