浏览代码

Fix integration with Undo by introducing IGraphObject

/main
Peter Bay Bastian 7 年前
当前提交
c5a7e594
共有 8 个文件被更改,包括 110 次插入68 次删除
  1. 2
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraph.cs
  2. 1
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Interfaces/IGraph.cs
  3. 32
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphEditWindow.cs
  4. 64
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs
  5. 65
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraphObject.cs
  6. 3
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraphObject.cs.meta
  7. 8
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Interfaces/IGraphObject.cs
  8. 3
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Interfaces/IGraphObject.cs.meta

2
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraph.cs


public OnGraphChange onChange { get; set; }
public IGraphObject owner { get; set; }
protected void NotifyChange(GraphChange change)
{
if (onChange != null)

1
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Interfaces/IGraph.cs


void ValidateGraph();
void ReplaceWith(IGraph other);
OnGraphChange onChange { get; set; }
IGraphObject owner { get; set; }
}
}

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


{
public override AbstractMaterialGraph GetMaterialGraph()
{
return inMemoryAsset;
return inMemoryAsset.graph as AbstractMaterialGraph;
}
}

{
return inMemoryAsset;
return inMemoryAsset.graph as AbstractMaterialGraph;
}
}

{
return inMemoryAsset;
return inMemoryAsset.graph as AbstractMaterialGraph;
}
}

Object m_Selected;
[SerializeField]
TGraphType m_InMemoryAsset;
SerializableGraphObject m_InMemoryAsset;
GraphEditorView m_GraphEditorView;
GraphEditorView graphEditorView

}
}
protected TGraphType inMemoryAsset
protected SerializableGraphObject inMemoryAsset
set { m_InMemoryAsset = value; }
set
{
if (m_InMemoryAsset != null)
DestroyImmediate(m_InMemoryAsset);
m_InMemoryAsset = value;
}
}
public override Object selected

void Update()
{
if (graphEditorView == null || graphEditorView.graphPresenter == null)
graphEditorView = new GraphEditorView(inMemoryAsset, this, selected.name);
graphEditorView = new GraphEditorView(GetMaterialGraph(), this, selected.name);
if (graphEditorView != null)
graphEditorView.previewSystem.Update();
}

{
UpdateAsset();
}
DestroyImmediate(inMemoryAsset);
graphEditorView = null;
}

private void UpdateShaderGraphOnDisk(string path)
{
var graph = inMemoryAsset as UnityEngine.MaterialGraph.MaterialGraph;
var graph = inMemoryAsset.graph as UnityEngine.MaterialGraph.MaterialGraph;
if (graph == null)
return;

AssetDatabase.ImportAsset(path);
}
public override void ChangeSelection(Object newSelection)
{
if (!EditorUtility.IsPersistent(newSelection))

var path = AssetDatabase.GetAssetPath(newSelection);
var textGraph = File.ReadAllText(path, Encoding.UTF8);
inMemoryAsset = JsonUtility.FromJson<TGraphType>(textGraph);
inMemoryAsset.OnEnable();
inMemoryAsset.ValidateGraph();
inMemoryAsset = CreateInstance<SerializableGraphObject>();
inMemoryAsset.graph = JsonUtility.FromJson<TGraphType>(textGraph);
inMemoryAsset.graph.OnEnable();
inMemoryAsset.graph.ValidateGraph();
graphEditorView = new GraphEditorView(inMemoryAsset, this, selected.name);
graphEditorView = new GraphEditorView(GetMaterialGraph(), this, selected.name);
titleContent = new GUIContent(selected.name);
Repaint();

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


namespace UnityEditor.MaterialGraph.Drawing
{
public class MaterialGraphObject : ScriptableObject
{
[SerializeField]
public string serializedGraph;
[SerializeField]
public int version;
}
[Serializable]
public class MaterialGraphPresenter : GraphViewPresenter
{

[SerializeField]
IMaterialGraphEditWindow m_Container;
[SerializeField]
MaterialGraphObject m_GraphObject;
[SerializeField]
int m_GraphVersion;
protected MaterialGraphPresenter()
{

{
m_PreviewSystem = previewSystem;
this.graph = graph;
m_Container = container;
if (graph == null)
return;

EdgeAdded(new EdgeAddedGraphChange(edge));
this.graph.onChange += OnChange;
m_GraphObject = CreateInstance<MaterialGraphObject>();
Undo.undoRedoPerformed += UndoRedoPerformed;
RecordState();
}
void UndoRedoPerformed()
{
if (m_GraphObject.version != m_GraphVersion)
{
var targetGraph = JsonUtility.FromJson(m_GraphObject.serializedGraph, graph.GetType()) as IGraph;
graph.ReplaceWith(targetGraph);
m_GraphVersion = m_GraphObject.version;
}
}
void RecordState()
{
m_GraphObject.serializedGraph = JsonUtility.ToJson(graph, false);
m_GraphObject.version++;
m_GraphVersion = m_GraphObject.version;
}
void OnChange(GraphChange change)

public void AddNode(INode node)
{
Undo.RecordObject(m_GraphObject, "Add " + node.name);
graph.owner.RegisterCompleteObjectUndo("Add " + node.name);
RecordState();
}
public void RemoveElements(IEnumerable<MaterialNodePresenter> nodes, IEnumerable<GraphEdgePresenter> edges)

{
if (left != null && right != null)
{
Undo.RecordObject(m_GraphObject, "Connect Edge");
graph.owner.RegisterCompleteObjectUndo("Connect Edge");
RecordState();
}
}

public void Cut()
{
Copy();
Undo.RecordObject(m_GraphObject, "Cut");
graph.owner.RegisterCompleteObjectUndo("Cut");
RecordState();
}
public bool canPaste

public void Paste()
{
var pastedGraph = DeserializeCopyBuffer(EditorGUIUtility.systemCopyBuffer);
Undo.RecordObject(m_GraphObject, "Paste");
graph.owner.RegisterCompleteObjectUndo("Paste");
RecordState();
}
public bool canDuplicate

public void Duplicate()
{
var graph = DeserializeCopyBuffer(JsonUtility.ToJson(CreateCopyPasteGraph(elements.Where(e => e.selected)), true));
Undo.RecordObject(m_GraphObject, "Duplicate");
InsertCopyPasteGraph(graph);
RecordState();
var deserializedGraph = DeserializeCopyBuffer(JsonUtility.ToJson(CreateCopyPasteGraph(elements.Where(e => e.selected)), true));
graph.owner.RegisterCompleteObjectUndo("Duplicate");
InsertCopyPasteGraph(deserializedGraph);
}
public bool canDelete

public void Delete()
{
RecordState();
Undo.RecordObject(m_GraphObject, "Delete");
graph.owner.RegisterCompleteObjectUndo("Delete");
RecordState();
}
public override void AddElement(EdgePresenter edge)

65
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraphObject.cs


using System;
using UnityEditor;
namespace UnityEngine.Graphing
{
public class SerializableGraphObject : ScriptableObject, IGraphObject, ISerializationCallbackReceiver
{
[SerializeField]
SerializationHelper.JSONSerializedElement m_SerializedGraph;
IGraph m_Graph;
IGraph m_DeserializedGraph;
public IGraph graph
{
get { return m_Graph; }
set
{
if (m_Graph != null)
m_Graph.owner = null;
m_Graph = value;
if (m_Graph != null)
m_Graph.owner = this;
}
}
public void RegisterCompleteObjectUndo(string name)
{
Undo.RegisterCompleteObjectUndo(this, name);
}
public void OnBeforeSerialize()
{
m_SerializedGraph = SerializationHelper.Serialize(graph);
}
public void OnAfterDeserialize()
{
var deserializedGraph = SerializationHelper.Deserialize<IGraph>(m_SerializedGraph, null);
if (graph == null)
graph = m_DeserializedGraph;
else
m_DeserializedGraph = deserializedGraph; // graph.ReplaceWith(m_DeserializedGraph);
}
void OnEnable()
{
Undo.undoRedoPerformed += UndoRedoPerformed;
}
void OnDisable()
{
Undo.undoRedoPerformed -= UndoRedoPerformed;
}
void UndoRedoPerformed()
{
if (m_DeserializedGraph != null)
{
graph.ReplaceWith(m_DeserializedGraph);
m_DeserializedGraph = null;
}
}
}
}

3
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraphObject.cs.meta


fileFormatVersion: 2
guid: 463ff292c8554dc18fb52fb725707a21
timeCreated: 1508144011

8
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Interfaces/IGraphObject.cs


namespace UnityEngine.Graphing
{
public interface IGraphObject
{
IGraph graph { get; set; }
void RegisterCompleteObjectUndo(string name);
}
}

3
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Interfaces/IGraphObject.cs.meta


fileFormatVersion: 2
guid: 59661073fa1046e9b3aa3fea5c7f5c54
timeCreated: 1508144485
正在加载...
取消
保存