浏览代码

Remove command handling from MaterialGraphPresenter

/main
Peter Bay Bastian 7 年前
当前提交
c6b895e7
共有 5 个文件被更改,包括 142 次插入142 次删除
  1. 59
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  2. 110
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs
  3. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs
  4. 44
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialGraphView.cs
  5. 69
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/CopyPasteGraph.cs

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


var presenter = graphEditorView.graphPresenter;
var e = Event.current;
var graphViewHasSelection = graphEditorView.graphView.selection.Any();
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

if (e.type == EventType.ExecuteCommand)
{
if (e.commandName == "Copy")
presenter.Copy();
{
EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(graphView.SelectionAsCopyPasteGraph(), true);
}
presenter.Paste();
{
graphObject.RegisterCompleteObjectUndo("Paste");
graphView.InsertCopyPasteGraph(CopyPasteGraph.FromJson(EditorGUIUtility.systemCopyBuffer));
}
presenter.Duplicate();
{
graphObject.RegisterCompleteObjectUndo("Duplicate");
graphView.InsertCopyPasteGraph(CopyPasteGraph.FromJson(JsonUtility.ToJson(graphView.SelectionAsCopyPasteGraph(), false)));
}
presenter.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();
}
presenter.Delete();
{
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();
}
graphEditorView.graphView.FrameAll();
graphView.FrameAll();
graphEditorView.graphView.FrameSelection();
graphView.FrameSelection();
graphEditorView.graphView.FrameOrigin();
graphView.FrameOrigin();
graphEditorView.graphView.FrameNext();
graphView.FrameNext();
graphEditorView.graphView.FramePrev();
graphView.FramePrev();
}
}

var graphPresenter = graphEditorView.graphPresenter;
var graphView = graphEditorView.graphView;
var selection = graphView.selection.OfType<GraphElement>();
var copyPasteGraph = new CopyPasteGraph();
foreach (var element in selection)
{
var nodeView = element as MaterialNodeView;
if (nodeView != null)
{
if (!(nodeView.node is PropertyNode))
copyPasteGraph.AddNode(nodeView.node);
}
var edgeView = element as Edge;
if (edgeView != null)
copyPasteGraph.AddEdge(edgeView.userData as IEdge);
}
var copyPasteGraph = new CopyPasteGraph(
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));
var deserialized = CopyPasteGraph.FromJson(JsonUtility.ToJson(copyPasteGraph, false));
if (deserialized == null)

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


[Serializable]
public class MaterialGraphPresenter
{
private GraphView m_GraphView;
private MaterialGraphView m_GraphView;
bool m_AddToSelection;
public IGraph graph { get; private set; }

// if (scope == ModificationScope.Topological)
}
public void Initialize(GraphView graphView, IGraph graph, PreviewSystem previewSystem)
public void Initialize(MaterialGraphView graphView, IGraph graph, PreviewSystem previewSystem)
{
m_GraphView = graphView;
m_PreviewSystem = previewSystem;

nodeView.userData = change.node;
change.node.onModified += OnNodeChanged;
m_GraphView.AddElement(nodeView);
if (m_AddToSelection)
m_GraphView.AddToSelection(nodeView);
}
void NodeRemoved(NodeRemovedGraphChange change)

edgeView.input = targetAnchor;
edgeView.input.Connect(edgeView);
m_GraphView.AddElement(edgeView);
if (m_AddToSelection)
m_GraphView.AddToSelection(edgeView);
}
void EdgeRemoved(EdgeRemovedGraphChange change)

{
graph.RemoveElements(nodes.Select(x => x.node as INode), edges.Select(x => x.userData as IEdge));
graph.ValidateGraph();
}
static CopyPasteGraph CreateCopyPasteGraph(IEnumerable<ISelectable> 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;
}
void InsertCopyPasteGraph(CopyPasteGraph copyGraph)
{
if (copyGraph == null || graph == null)
return;
m_GraphView.ClearSelection();
m_AddToSelection = true;
var addedNodes = new List<INode>();
var nodeGuidMap = new Dictionary<Guid, Guid>();
foreach (var node in copyGraph.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;
graph.AddNode(node);
addedNodes.Add(node);
}
// only connect edges within pasted elements, discard
// external edges.
foreach (var edge in copyGraph.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);
graph.Connect(outputSlotRef, inputSlotRef);
}
}
graph.ValidateGraph();
m_AddToSelection = false;
}
public void Copy()
{
EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(CreateCopyPasteGraph(m_GraphView.selection), true);
}
public void Cut()
{
Copy();
graph.owner.RegisterCompleteObjectUndo("Cut");
RemoveElements(
m_GraphView.selection.OfType<MaterialNodeView>(),
m_GraphView.selection.OfType<Edge>());
}
public void Paste()
{
graph.owner.RegisterCompleteObjectUndo("Paste");
InsertCopyPasteGraph(CopyPasteGraph.FromJson(EditorGUIUtility.systemCopyBuffer));
}
public void Duplicate()
{
graph.owner.RegisterCompleteObjectUndo("Duplicate");
InsertCopyPasteGraph(CopyPasteGraph.FromJson(JsonUtility.ToJson(CreateCopyPasteGraph(m_GraphView.selection), false)));
}
public void Delete()
{
graph.owner.RegisterCompleteObjectUndo("Delete");
RemoveElements(
m_GraphView.selection.OfType<MaterialNodeView>(),
m_GraphView.selection.OfType<Edge>());
}
}
}

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


var content = new VisualElement { name = "content" };
{
m_GraphView = new MaterialGraphView() { name = "GraphView" };
m_GraphView = new MaterialGraphView(graph) { name = "GraphView" };
m_GraphView.SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale);
m_GraphView.AddManipulator(new ContentDragger());
m_GraphView.AddManipulator(new RectangleSelector());

44
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;
using MouseButton = UnityEngine.Experimental.UIElements.MouseButton;
namespace UnityEditor.MaterialGraph.Drawing

public AbstractMaterialGraph graph { get; private set; }
public override List<NodeAnchor> GetCompatibleAnchors(NodeAnchor startAnchor, NodeAdapter nodeAdapter)
{
var compatibleAnchors = new List<NodeAnchor>();

public OnSelectionChanged onSelectionChanged;
public MaterialGraphView(AbstractMaterialGraph graph)
{
this.graph = graph;
}
void SelectionChanged()
{
var selectedNodes = selection.OfType<MaterialNodeView>().Where(x => x.userData is INode);

{
base.ClearSelection();
SelectionChanged();
}
}
public static class GraphViewExtensions
{
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)
return;
using (var remappedNodesDisposable = ListPool<INode>.GetDisposable())
using (var remappedEdgesDisposable = ListPool<IEdge>.GetDisposable())
{
var remappedNodes = remappedNodesDisposable.value;
var remappedEdges = remappedEdgesDisposable.value;
copyGraph.InsertInGraph(graphView.graph, remappedNodes, remappedEdges);
// Add new elements to selection
graphView.ClearSelection();
graphView.graphElements.ForEach(element =>
{
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);
});
}
}
}
}

69
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/CopyPasteGraph.cs


namespace UnityEditor.Graphing.Util
{
[Serializable]
internal class CopyPasteGraph : ISerializationCallbackReceiver
sealed class CopyPasteGraph : ISerializationCallbackReceiver
private HashSet<IEdge> m_Edges = new HashSet<IEdge>();
HashSet<IEdge> m_Edges = new HashSet<IEdge>();
private HashSet<INode> m_Nodes = new HashSet<INode>();
HashSet<INode> m_Nodes = new HashSet<INode>();
[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializableNodes = new List<SerializationHelper.JSONSerializedElement>();

public virtual void AddNode(INode node)
public CopyPasteGraph() { }
public CopyPasteGraph(IEnumerable<INode> nodes, IEnumerable<IEdge> edges)
{
foreach (var node in nodes)
{
AddNode(node);
foreach (var edge in NodeUtils.GetAllEdges(node))
AddEdge(edge);
}
foreach (var edge in edges)
AddEdge(edge);
}
public void AddNode(INode node)
{
m_Nodes.Add(node);
}

get { return m_Edges; }
}
public virtual void OnBeforeSerialize()
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()
public virtual void OnAfterDeserialize()
public void OnAfterDeserialize()
{
var nodes = SerializationHelper.Deserialize<INode>(m_SerializableNodes, null);
m_Nodes.Clear();

正在加载...
取消
保存