浏览代码

Merge pull request #257 from Unity-Technologies/copy-paste-fix

[FIX] #251 Nodes are now selected again after pasted
/main
GitHub 7 年前
当前提交
96c8a0c7
共有 4 个文件被更改,包括 66 次插入45 次删除
  1. 59
      com.unity.shadergraph/Editor/Data/Graphs/AbstractMaterialGraph.cs
  2. 8
      com.unity.shadergraph/Editor/Drawing/Views/GraphEditorView.cs
  3. 2
      com.unity.shadergraph/Editor/Drawing/Views/MaterialGraphView.cs
  4. 42
      com.unity.shadergraph/Editor/Util/CopyPasteGraph.cs

59
com.unity.shadergraph/Editor/Data/Graphs/AbstractMaterialGraph.cs


using System.Text.RegularExpressions;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.Graphing.Util;
namespace UnityEditor.ShaderGraph
{

get { return m_RemovedNodes; }
}
[NonSerialized]
List<INode> m_PastedNodes = new List<INode>();
public IEnumerable<INode> pastedNodes
{
get { return m_PastedNodes; }
}
#endregion
#region Edge data

{
m_AddedNodes.Clear();
m_RemovedNodes.Clear();
m_PastedNodes.Clear();
m_AddedEdges.Clear();
m_RemovedEdges.Clear();
m_AddedProperties.Clear();

public INode GetNodeFromTempId(Identifier tempId)
{
if (tempId.index > m_Nodes.Count)
throw new ArgumentException("Trying to retrieve a node using an identifier that does not exist.");
throw new Exception("Index does not contain a node.");
throw new Exception("Trying to retrieve a node using an identifier that does not exist.");
if (node.tempId.version != tempId.version)
throw new Exception("Trying to retrieve a node that was removed from the graph.");
return node;

outputSlot = outputNode.FindOutputSlot<MaterialSlot>(edge.outputSlot.slotId);
inputSlot = inputNode.FindInputSlot<MaterialSlot>(edge.inputSlot.slotId);
}
if (outputNode == null
|| inputNode == null
|| outputSlot == null

foreach (var edge in other.edges)
ConnectNoValidate(edge.outputSlot, edge.inputSlot);
ValidateGraph();
}
internal void PasteGraph(CopyPasteGraph graphToPaste, List<INode> remappedNodes, List<IEdge> remappedEdges)
{
var nodeGuidMap = new Dictionary<Guid, Guid>();
foreach (var node in graphToPaste.GetNodes<INode>())
{
var oldGuid = node.guid;
var newGuid = node.RewriteGuid();
nodeGuidMap[oldGuid] = newGuid;
var drawState = node.drawState;
var position = drawState.position;
position.x += 30;
position.y += 30;
drawState.position = position;
node.drawState = drawState;
remappedNodes.Add(node);
AddNode(node);
// add the node to the pasted node list
m_PastedNodes.Add(node);
}
// only connect edges within pasted elements, discard
// external edges.
foreach (var edge in graphToPaste.edges)
{
var outputSlot = edge.outputSlot;
var inputSlot = edge.inputSlot;
Guid remappedOutputNodeGuid;
Guid remappedInputNodeGuid;
if (nodeGuidMap.TryGetValue(outputSlot.nodeGuid, out remappedOutputNodeGuid)
&& nodeGuidMap.TryGetValue(inputSlot.nodeGuid, out remappedInputNodeGuid))
{
var outputSlotRef = new SlotReference(remappedOutputNodeGuid, outputSlot.slotId);
var inputSlotRef = new SlotReference(remappedInputNodeGuid, inputSlot.slotId);
remappedEdges.Add(Connect(outputSlotRef, inputSlotRef));
}
}
ValidateGraph();
}

8
com.unity.shadergraph/Editor/Drawing/Views/GraphEditorView.cs


}
foreach (var node in m_Graph.addedNodes)
{
}
foreach (var node in m_Graph.pastedNodes)
{
var nodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(p => p.node != null && p.node.guid == node.guid);
m_GraphView.AddToSelection(nodeView);
}
var nodesToUpdate = m_NodeViewHashSet;
nodesToUpdate.Clear();

2
com.unity.shadergraph/Editor/Drawing/Views/MaterialGraphView.cs


{
var remappedNodes = remappedNodesDisposable.value;
var remappedEdges = remappedEdgesDisposable.value;
copyGraph.InsertInGraph(graphView.graph, remappedNodes, remappedEdges);
graphView.graph.PasteGraph(copyGraph, remappedNodes, remappedEdges);
// Add new elements to selection
graphView.ClearSelection();

42
com.unity.shadergraph/Editor/Util/CopyPasteGraph.cs


get { return m_Edges; }
}
public void InsertInGraph(IGraph graph, List<INode> remappedNodes, List<IEdge> remappedEdges)
{
var nodeGuidMap = new Dictionary<Guid, Guid>();
foreach (var node in GetNodes<INode>())
{
var oldGuid = node.guid;
var newGuid = node.RewriteGuid();
nodeGuidMap[oldGuid] = newGuid;
var drawState = node.drawState;
var position = drawState.position;
position.x += 30;
position.y += 30;
drawState.position = position;
node.drawState = drawState;
remappedNodes.Add(node);
graph.AddNode(node);
}
// only connect edges within pasted elements, discard
// external edges.
foreach (var edge in edges)
{
var outputSlot = edge.outputSlot;
var inputSlot = edge.inputSlot;
Guid remappedOutputNodeGuid;
Guid remappedInputNodeGuid;
if (nodeGuidMap.TryGetValue(outputSlot.nodeGuid, out remappedOutputNodeGuid)
&& nodeGuidMap.TryGetValue(inputSlot.nodeGuid, out remappedInputNodeGuid))
{
var outputSlotRef = new SlotReference(remappedOutputNodeGuid, outputSlot.slotId);
var inputSlotRef = new SlotReference(remappedInputNodeGuid, inputSlot.slotId);
remappedEdges.Add(graph.Connect(outputSlotRef, inputSlotRef));
}
}
m_Nodes.Clear();
m_Edges.Clear();
graph.ValidateGraph();
}
public void OnBeforeSerialize()
{
m_SerializableNodes = SerializationHelper.Serialize<INode>(m_Nodes);

正在加载...
取消
保存