浏览代码

Move copy paste boilerplate and event handling to GraphView.

/main
Patrick Fournier 7 年前
当前提交
26e0dad5
共有 4 个文件被更改,包括 96 次插入147 次删除
  1. 65
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphEditWindow.cs
  2. 97
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs
  3. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs
  4. 65
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialGraphView.cs

65
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphEditWindow.cs


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

m_GraphEditorView.onConvertToSubgraphClick += ToSubGraph;
m_GraphEditorView.onShowInProjectClick += PingAsset;
m_GraphEditorView.RegisterCallback<PostLayoutEvent>(OnPostLayout);
m_GraphEditorView.RegisterCallback<AttachToPanelEvent, GraphEditorView>(OnEnterPanel, m_GraphEditorView);
m_GraphEditorView.RegisterCallback<DetachFromPanelEvent, GraphEditorView>(OnLeavePanel, m_GraphEditorView);
rootVisualContainer.Add(graphEditorView);
}
}

graphEditorView = null;
}
void OnGUI()
{
if (graphEditorView == null)
return;
var presenter = graphEditorView.graphPresenter;
var e = Event.current;
if (e.type == EventType.ValidateCommand && (
e.commandName == "Copy" && presenter.canCopy
|| e.commandName == "Paste" && presenter.canPaste
|| e.commandName == "Duplicate" && presenter.canDuplicate
|| e.commandName == "Cut" && presenter.canCut
|| (e.commandName == "Delete" || e.commandName == "SoftDelete") && presenter.canDelete))
{
e.Use();
}
if (e.type == EventType.ExecuteCommand)
{
if (e.commandName == "Copy")
presenter.Copy();
if (e.commandName == "Paste")
presenter.Paste();
if (e.commandName == "Duplicate")
presenter.Duplicate();
if (e.commandName == "Cut")
presenter.Cut();
if (e.commandName == "Delete" || e.commandName == "SoftDelete")
presenter.Delete();
}
if (e.type == EventType.KeyDown)
{
if (e.keyCode == KeyCode.A)
graphEditorView.graphView.FrameAll();
if (e.keyCode == KeyCode.F)
graphEditorView.graphView.FrameSelection();
if (e.keyCode == KeyCode.O)
graphEditorView.graphView.FrameOrigin();
if (e.keyCode == KeyCode.Tab)
graphEditorView.graphView.FrameNext();
if (e.keyCode == KeyCode.Tab && e.modifiers == EventModifiers.Shift)
graphEditorView.graphView.FramePrev();
}
}
public override void PingAsset()
{
if (selected != null)

}
}
var deserialized = MaterialGraphPresenter.DeserializeCopyBuffer(JsonUtility.ToJson(MaterialGraphPresenter.CreateCopyPasteGraph(filtered)));
var deserialized = MaterialGraphView.DeserializeCopyBuffer(JsonUtility.ToJson(MaterialGraphView.CreateCopyPasteGraph(filtered)));
if (deserialized == null)
return;

{
graphEditorView.UnregisterCallback<PostLayoutEvent>(OnPostLayout);
graphEditorView.graphView.FrameAll();
}
void OnEnterPanel(AttachToPanelEvent e, GraphEditorView view)
{
VisualElement rootVisualContainer = UIElementsEntryPoint.GetRootVisualContainer(this);
rootVisualContainer.parent.AddManipulator(view.shortcutHandler);
}
void OnLeavePanel(DetachFromPanelEvent e, GraphEditorView view)
{
VisualElement rootVisualContainer = UIElementsEntryPoint.GetRootVisualContainer(this);
rootVisualContainer.parent.RemoveManipulator(view.shortcutHandler);
}
}
}

97
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs


}
}
internal static CopyPasteGraph CreateCopyPasteGraph(IEnumerable<GraphElement> selection)
{
var graph = new CopyPasteGraph();
foreach (var element in selection)
{
var nodeView = element as MaterialNodeView;
if (nodeView != null)
{
graph.AddNode(nodeView.node);
foreach (var edge in NodeUtils.GetAllEdges(nodeView.userData as INode))
graph.AddEdge(edge);
}
var edgeView = element as Edge;
if (edgeView != null)
graph.AddEdge(edgeView.userData as IEdge);
}
return graph;
}
internal static CopyPasteGraph DeserializeCopyBuffer(string copyBuffer)
{
try
{
return JsonUtility.FromJson<CopyPasteGraph>(copyBuffer);
}
catch
{
// ignored. just means copy buffer was not a graph :(
return null;
}
}
void InsertCopyPasteGraph(CopyPasteGraph copyGraph)
internal void InsertCopyPasteGraph(CopyPasteGraph copyGraph)
{
if (copyGraph == null || graph == null)
return;

graph.ValidateGraph();
if (onSelectionChanged != null)
onSelectionChanged(addedNodes);
}
public bool canCopy
{
get { return elements.Any(e => e.selected) || (m_GraphView != null && m_GraphView.selection.OfType<GraphElement>().Any(e => e.selected)); }
}
public void Copy()
{
var graph = CreateCopyPasteGraph(m_GraphView.selection.OfType<GraphElement>());
EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(graph, true);
}
public bool canCut
{
get { return canCopy; }
}
public void Cut()
{
Copy();
graph.owner.RegisterCompleteObjectUndo("Cut");
RemoveElements(
m_GraphView.selection.OfType<MaterialNodeView>(),
m_GraphView.selection.OfType<Edge>());
}
public bool canPaste
{
get { return DeserializeCopyBuffer(EditorGUIUtility.systemCopyBuffer) != null; }
}
public void Paste()
{
var pastedGraph = DeserializeCopyBuffer(EditorGUIUtility.systemCopyBuffer);
graph.owner.RegisterCompleteObjectUndo("Paste");
InsertCopyPasteGraph(pastedGraph);
}
public bool canDuplicate
{
get { return canCopy; }
}
public void Duplicate()
{
var deserializedGraph = DeserializeCopyBuffer(JsonUtility.ToJson(CreateCopyPasteGraph(m_GraphView.selection.OfType<GraphElement>()), true));
graph.owner.RegisterCompleteObjectUndo("Duplicate");
InsertCopyPasteGraph(deserializedGraph);
}
public bool canDelete
{
get { return canCopy; }
}
public void Delete()
{
graph.owner.RegisterCompleteObjectUndo("Delete");
RemoveElements(
m_GraphView.selection.OfType<MaterialNodeView>(),
m_GraphView.selection.OfType<Edge>());
}
public delegate void OnSelectionChanged(IEnumerable<INode> nodes);

16
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs


using System;
using System.Linq;
using System.Collections.Generic;
using UnityEditor.MaterialGraph.Drawing;
using Object = UnityEngine.Object;
namespace UnityEditor.MaterialGraph.Drawing
{

get { return m_PreviewSystem; }
set { m_PreviewSystem = value; }
}
public ShortcutHandler shortcutHandler { get; set; }
public GraphEditorView(AbstractMaterialGraph graph, HelperMaterialGraphEditWindow container, string assetName)
{

m_GraphView = new MaterialGraphView { name = "GraphView", presenter = m_GraphPresenter };
m_GraphInspectorView = new GraphInspectorView(assetName, previewSystem, graph) { name = "inspector" };
m_GraphPresenter.onSelectionChanged += m_GraphInspectorView.UpdateSelection;
shortcutHandler = new ShortcutHandler(
new Dictionary<Event, ShortcutDelegate>
{
{ Event.KeyboardEvent("a"), m_GraphView.FrameAll },
{ Event.KeyboardEvent("o"), m_GraphView.FrameOrigin },
{ Event.KeyboardEvent("["), m_GraphView.FramePrev },
{ Event.KeyboardEvent("]"), m_GraphView.FrameNext }
});
content.Add(m_GraphView);
content.Add(m_GraphInspectorView);

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


using System.Linq;
using System.Reflection;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEditor.Graphing.Util;
using Edge = UnityEditor.Experimental.UIElements.GraphView.Edge;
namespace UnityEditor.MaterialGraph.Drawing
{

{
focusIndex = 0;
RegisterCallback<MouseUpEvent>(DoContextMenu, Capture.Capture);
SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale);
this.AddManipulator(new ContentDragger());

base.ClearSelection();
PropagateSelection();
}
protected override string SerializeCopyPasteData(IEnumerable<GraphElement> elements)
{
var graph = CreateCopyPasteGraph(elements);
return JsonUtility.ToJson(graph, true);
}
protected override bool CanPasteSerializedData(string serializedData)
{
return DeserializeCopyBuffer(serializedData) != null;
}
protected override void UnserializeAndPasteOperation(string operationName, string serializedData)
{
var mgp = GetPresenter<MaterialGraphPresenter>();
mgp.graph.owner.RegisterCompleteObjectUndo(operationName);
var pastedGraph = DeserializeCopyBuffer(serializedData);
mgp.InsertCopyPasteGraph(pastedGraph);
}
protected override void DeleteSelectionOperation(string operationName)
{
var mgp = GetPresenter<MaterialGraphPresenter>();
mgp.graph.owner.RegisterCompleteObjectUndo(operationName);
mgp.RemoveElements(selection.OfType<MaterialNodeView>(), selection.OfType<Edge>());
}
internal static CopyPasteGraph CreateCopyPasteGraph(IEnumerable<GraphElement> selection)
{
var graph = new CopyPasteGraph();
foreach (var element in selection)
{
var nodeView = element as MaterialNodeView;
if (nodeView != null)
{
graph.AddNode(nodeView.node);
foreach (var edge in NodeUtils.GetAllEdges(nodeView.userData as INode))
graph.AddEdge(edge);
}
var edgeView = element as Edge;
if (edgeView != null)
graph.AddEdge(edgeView.userData as IEdge);
}
return graph;
}
internal static CopyPasteGraph DeserializeCopyBuffer(string copyBuffer)
{
try
{
return JsonUtility.FromJson<CopyPasteGraph>(copyBuffer);
}
catch
{
// ignored. just means copy buffer was not a graph :(
return null;
}
}
}
}
正在加载...
取消
保存