浏览代码

Make nodes spawn in the center of the view when copied from another graph

/main
jensgholm15@gmail.com 7 年前
当前提交
ed51c9ec
共有 4 个文件被更改,包括 61 次插入10 次删除
  1. 5
      com.unity.shadergraph/Editor/Data/Graphs/SerializableGuid.cs
  2. 1
      com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs
  3. 47
      com.unity.shadergraph/Editor/Drawing/Views/MaterialGraphView.cs
  4. 18
      com.unity.shadergraph/Editor/Util/CopyPasteGraph.cs

5
com.unity.shadergraph/Editor/Data/Graphs/SerializableGuid.cs


m_Guid = Guid.NewGuid();
}
public SerializableGuid(Guid guid)
{
m_Guid = guid;
}
[NonSerialized]
private Guid m_Guid;

1
com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs


var metaProperties = graphView.graph.properties.Where(x => propertyNodeGuids.Contains(x.guid));
var copyPasteGraph = new CopyPasteGraph(
graphView.graph.guid,
graphView.selection.OfType<MaterialNodeView>().Where(x => !(x.node is PropertyNode)).Select(x => x.node as INode),
graphView.selection.OfType<Edge>().Select(x => x.userData as IEdge),
graphView.selection.OfType<BlackboardField>().Select(x => x.userData as IShaderProperty),

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


var propertyNodeGuids = nodes.OfType<PropertyNode>().Select(x => x.propertyGuid);
var metaProperties = this.graph.properties.Where(x => propertyNodeGuids.Contains(x.guid));
var graph = new CopyPasteGraph(nodes, edges, properties, metaProperties);
var graph = new CopyPasteGraph(this.graph.guid, nodes, edges, properties, metaProperties);
return JsonUtility.ToJson(graph, true);
}

}
using (var remappedNodesDisposable = ListPool<INode>.GetDisposable())
{
if (graphView.graph.guid != copyGraph.sourceGraphGuid)
{
// Compute the mean of the copied nodes.
Vector2 centroid = Vector2.zero;
var count = 1;
foreach (var node in remappedNodes)
{
var position = node.drawState.position.position;
centroid = centroid + (position - centroid) / count;
++count;
}
// Get the center of the current view
var viewCenter = graphView.contentViewContainer.WorldToLocal(graphView.layout.center);
foreach (var node in remappedNodes)
{
var drawState = node.drawState;
var positionRect = drawState.position;
var position = positionRect.position;
position += viewCenter - centroid;
positionRect.position = position;
drawState.position = positionRect;
node.drawState = drawState;
}
}
{
var edge = element as Edge;
if (edge != null && remappedEdges.Contains(edge.userData as IEdge))
graphView.AddToSelection(edge);
{
var edge = element as Edge;
if (edge != null && remappedEdges.Contains(edge.userData as IEdge))
graphView.AddToSelection(edge);
var nodeView = element as MaterialNodeView;
if (nodeView != null && remappedNodes.Contains(nodeView.node))
graphView.AddToSelection(nodeView);
});
var nodeView = element as MaterialNodeView;
if (nodeView != null && remappedNodes.Contains(nodeView.node))
graphView.AddToSelection(nodeView);
});
}
}
}
}

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


[NonSerialized]
HashSet<IShaderProperty> m_MetaProperties = new HashSet<IShaderProperty>();
[NonSerialized]
SerializableGuid m_SourceGraphGuid;
[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializableNodes = new List<SerializationHelper.JSONSerializedElement>();

[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializableMetaProperties = new List<SerializationHelper.JSONSerializedElement>();
[SerializeField]
SerializationHelper.JSONSerializedElement m_SerializeableSourceGraphGuid = new SerializationHelper.JSONSerializedElement();
public CopyPasteGraph(IEnumerable<INode> nodes, IEnumerable<IEdge> edges, IEnumerable<IShaderProperty> properties, IEnumerable<IShaderProperty> metaProperties)
public CopyPasteGraph(Guid sourceGraphGuid, IEnumerable<INode> nodes, IEnumerable<IEdge> edges, IEnumerable<IShaderProperty> properties, IEnumerable<IShaderProperty> metaProperties)
m_SourceGraphGuid = new SerializableGuid(sourceGraphGuid);
foreach (var node in nodes)
{
AddNode(node);

get { return m_MetaProperties; }
}
public Guid sourceGraphGuid
{
get { return m_SourceGraphGuid.guid; }
}
m_SerializeableSourceGraphGuid = SerializationHelper.Serialize(m_SourceGraphGuid);
m_SerializableNodes = SerializationHelper.Serialize<INode>(m_Nodes);
m_SerializableEdges = SerializationHelper.Serialize<IEdge>(m_Edges);
m_SerilaizeableProperties = SerializationHelper.Serialize<IShaderProperty>(m_Properties);

public void OnAfterDeserialize()
{
m_SourceGraphGuid = SerializationHelper.Deserialize<SerializableGuid>(m_SerializeableSourceGraphGuid, GraphUtil.GetLegacyTypeRemapping());
var nodes = SerializationHelper.Deserialize<INode>(m_SerializableNodes, GraphUtil.GetLegacyTypeRemapping());
m_Nodes.Clear();
foreach (var node in nodes)

正在加载...
取消
保存