浏览代码

Merge pull request #81 from patrickfournier/graph-view-copy-paste

Graph view copy paste, for trunk cfe3ca666188 and later
/main
GitHub 7 年前
当前提交
e10be928
共有 2 个文件被更改,包括 34 次插入71 次删除
  1. 66
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  2. 39
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialGraphView.cs

66
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs


using System.IO;
using System.Linq;
using System.Text;
using UnityEditor.Experimental.UIElements;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEditor.Graphing.Util;
using UnityEngine;

Undo.ClearUndo(graphObject);
DestroyImmediate(graphObject);
graphEditorView = null;
}
void OnGUI()
{
if (graphEditorView == null)
return;
var e = Event.current;
var graphView = graphEditorView.graphView;
var graphViewHasSelection = graphView.selection.Any();
if (e.type == EventType.ValidateCommand && (
e.commandName == "Copy" && graphViewHasSelection
|| e.commandName == "Paste" && CopyPasteGraph.FromJson(EditorGUIUtility.systemCopyBuffer) != null
|| e.commandName == "Duplicate" && graphViewHasSelection
|| e.commandName == "Cut" && graphViewHasSelection
|| (e.commandName == "Delete" || e.commandName == "SoftDelete") && graphViewHasSelection))
{
e.Use();
}
if (e.type == EventType.ExecuteCommand)
{
if (e.commandName == "Copy")
{
EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(graphView.SelectionAsCopyPasteGraph(), true);
}
if (e.commandName == "Paste")
{
graphObject.RegisterCompleteObjectUndo("Paste");
graphView.InsertCopyPasteGraph(CopyPasteGraph.FromJson(EditorGUIUtility.systemCopyBuffer));
}
if (e.commandName == "Duplicate")
{
graphObject.RegisterCompleteObjectUndo("Duplicate");
graphView.InsertCopyPasteGraph(CopyPasteGraph.FromJson(JsonUtility.ToJson(graphView.SelectionAsCopyPasteGraph(), false)));
}
if (e.commandName == "Cut")
{
graphObject.RegisterCompleteObjectUndo("Cut");
EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(graphView.SelectionAsCopyPasteGraph(), true);
graphObject.graph.RemoveElements(graphView.selection.OfType<MaterialNodeView>().Select(x => x.node as INode), graphView.selection.OfType<Edge>().Select(x => x.userData as IEdge));
graphObject.graph.ValidateGraph();
}
if (e.commandName == "Delete" || e.commandName == "SoftDelete")
{
// graphObject.RegisterCompleteObjectUndo("Delete");
// graphObject.graph.RemoveElements(graphView.selection.OfType<MaterialNodeView>().Select(x => x.node as INode), graphView.selection.OfType<Edge>().Select(x => x.userData as IEdge));
// graphObject.graph.ValidateGraph();
}
}
if (e.type == EventType.KeyDown)
{
if (e.keyCode == KeyCode.A)
graphView.FrameAll();
if (e.keyCode == KeyCode.F)
graphView.FrameSelection();
if (e.keyCode == KeyCode.O)
graphView.FrameOrigin();
if (e.keyCode == KeyCode.Tab)
graphView.FrameNext();
if (e.keyCode == KeyCode.Tab && (e.modifiers & EventModifiers.Shift) > 0)
graphView.FramePrev();
}
}
public void PingAsset()

39
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialGraphView.cs


public OnSelectionChanged onSelectionChanged;
public MaterialGraphView(AbstractMaterialGraph graph)
public MaterialGraphView()
{
serializeGraphElements = SerializeGraphElementsImplementation;
canPasteSerializedData = CanPasteSerializedDataImplementation;
unserializeAndPaste = UnserializeAndPasteImplementation;
deleteSelection = DeleteSelectionImplementation;
}
public MaterialGraphView(AbstractMaterialGraph graph) : this()
{
this.graph = graph;
}

base.ClearSelection();
SelectionChanged();
}
string SerializeGraphElementsImplementation(IEnumerable<GraphElement> elements)
{
var graph = new CopyPasteGraph(elements.OfType<MaterialNodeView>().Select(x => (INode)x.node), elements.OfType<Edge>().Select(x => x.userData).OfType<IEdge>());
return JsonUtility.ToJson(graph, true);
}
bool CanPasteSerializedDataImplementation(string serializedData)
{
return CopyPasteGraph.FromJson(serializedData) != null;
}
void UnserializeAndPasteImplementation(string operationName, string serializedData)
{
graph.owner.RegisterCompleteObjectUndo(operationName);
var pastedGraph = CopyPasteGraph.FromJson(serializedData);
this.InsertCopyPasteGraph(pastedGraph);
}
void DeleteSelectionImplementation(string operationName, GraphView.AskUser askUser)
{
graph.owner.RegisterCompleteObjectUndo(operationName);
graph.RemoveElements(selection.OfType<MaterialNodeView>().Select(x => (INode)x.node), selection.OfType<Edge>().Select(x => x.userData).OfType<IEdge>());
}
internal static CopyPasteGraph SelectionAsCopyPasteGraph(this MaterialGraphView graphView)
{
return new CopyPasteGraph(graphView.selection.OfType<MaterialNodeView>().Select(x => (INode) x.node), graphView.selection.OfType<Edge>().Select(x => x.userData).OfType<IEdge>());
}
internal static void InsertCopyPasteGraph(this MaterialGraphView graphView, CopyPasteGraph copyGraph)
{
if (copyGraph == null)

正在加载...
取消
保存