浏览代码

Implemented copy/paste functionality for graph (currently uses shift rather than ctrl, as the latter does not fire the event)

/main
Peter Bay Bastian 8 年前
当前提交
ce272542
共有 4 个文件被更改,包括 185 次插入2 次删除
  1. 85
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphDataSource.cs
  2. 24
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/SerializableGraphView.cs
  3. 66
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/CopyPasteGraph.cs
  4. 12
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/CopyPasteGraph.cs.meta

85
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphDataSource.cs


}
}
public void Copy(IEnumerable<GraphElementPresenter> selection)
{
var graph = new CopyPasteGraph();
foreach (var presenter in selection)
{
var nodeDrawData = presenter as NodeDrawData;
if (nodeDrawData != null)
{
graph.AddNode(nodeDrawData.node);
foreach (var edge in NodeUtils.GetAllEdges(nodeDrawData.node))
graph.AddEdge(edge);
}
var edgeDrawData = presenter as EdgeDrawData;
if (edgeDrawData != null)
graph.AddEdge(edgeDrawData.edge);
}
EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(graph, true);
}
private CopyPasteGraph DeserializeCopyBuffer()
{
try
{
return JsonUtility.FromJson<CopyPasteGraph>(EditorGUIUtility.systemCopyBuffer);
}
catch
{
// ignored. just means copy buffer was not a graph :(
return null;
}
}
public void Paste()
{
var pastedGraph = DeserializeCopyBuffer();
if (pastedGraph == null || graphAsset == null || graphAsset.graph == null)
return;
var addedNodes = new List<INode>();
var nodeGuidMap = new Dictionary<Guid, Guid>();
foreach (var node in pastedGraph.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;
graphAsset.graph.AddNode(node);
addedNodes.Add(node);
}
// only connect edges within pasted elements, discard
// external edges.
var addedEdges = new List<IEdge>();
foreach (var edge in pastedGraph.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);
addedEdges.Add(graphAsset.graph.Connect(outputSlotRef, inputSlotRef));
}
}
graphAsset.graph.ValidateGraph();
UpdateData();
graphAsset.drawingData.selection = addedNodes.Select(n => n.guid);
}
public override void AddElement(EdgePresenter edge)
{
Connect(edge.output as AnchorDrawData, edge.input as AnchorDrawData);

24
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/SerializableGraphView.cs


using System.Collections;
using System.Collections.Generic;
using System.Linq;
using RMGUI.GraphView;

{Event.KeyboardEvent("o"), FrameOrigin},
{Event.KeyboardEvent("delete"), DeleteSelection},
{Event.KeyboardEvent("#tab"), FramePrev},
{Event.KeyboardEvent("tab"), FrameNext}
{Event.KeyboardEvent("tab"), FrameNext},
{Event.KeyboardEvent("#c"), CopySelection},
{Event.KeyboardEvent("#v"), Paste}
}));
AddManipulator(new ClickGlobalSelector());

return;
var graphAsset = graphDataSource.graphAsset;
if (graphAsset == null || selection.Count != 0 || !graphAsset.drawingData.selection.Any()) return;
if (graphAsset == null || graphAsset.drawingData.selection.SequenceEqual(selection.OfType<NodeDrawer>().Select(d => ((NodeDrawData) d.presenter).node.guid))) return;
var selectedDrawers = graphDataSource.graphAsset.drawingData.selection
.Select(guid => contentViewContainer.children

ClearSelection();
foreach (var drawer in selectedDrawers)
AddToSelection(drawer);
}

{
base.ClearSelection();
PropagateSelection();
}
public EventPropagation CopySelection()
{
var graphDataSource = GetPresenter<AbstractGraphDataSource>();
if (selection.Any() && graphDataSource != null)
graphDataSource.Copy(selection.OfType<GraphElement>().Select(ge => ge.presenter));
return EventPropagation.Stop;
}
public EventPropagation Paste()
{
var graphDataSource = GetPresenter<AbstractGraphDataSource>();
if (graphDataSource != null)
graphDataSource.Paste();
return EventPropagation.Stop;
}
}
}

66
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/CopyPasteGraph.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Graphing;
namespace UnityEditor.Graphing.Util
{
[Serializable]
internal class CopyPasteGraph : ISerializationCallbackReceiver
{
[NonSerialized]
private HashSet<IEdge> m_Edges = new HashSet<IEdge>();
[NonSerialized]
private HashSet<INode> m_Nodes = new HashSet<INode>();
[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializableNodes = new List<SerializationHelper.JSONSerializedElement>();
[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializableEdges = new List<SerializationHelper.JSONSerializedElement>();
public virtual void AddNode(INode node)
{
m_Nodes.Add(node);
}
public void AddEdge(IEdge edge)
{
m_Edges.Add(edge);
}
public IEnumerable<T> GetNodes<T>() where T : INode
{
return m_Nodes.OfType<T>();
}
public IEnumerable<IEdge> edges
{
get { return m_Edges; }
}
public virtual void OnBeforeSerialize()
{
m_SerializableNodes = SerializationHelper.Serialize<INode>(m_Nodes);
m_SerializableEdges = SerializationHelper.Serialize<IEdge>(m_Edges);
}
public virtual void OnAfterDeserialize()
{
var nodes = SerializationHelper.Deserialize<INode>(m_SerializableNodes, null);
m_Nodes.Clear();
foreach (var node in nodes)
m_Nodes.Add(node);
m_SerializableNodes = null;
var edges = SerializationHelper.Deserialize<IEdge>(m_SerializableEdges, null);
m_Edges.Clear();
foreach (var edge in edges)
m_Edges.Add(edge);
m_SerializableEdges = null;
}
}
}

12
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/CopyPasteGraph.cs.meta


fileFormatVersion: 2
guid: 187365d075d49dc46a3e09b81f0d6fdd
timeCreated: 1483699278
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存