浏览代码

Get rid of presenter for graph inspector

/main
Peter Bay Bastian 7 年前
当前提交
145b4d5f
共有 4 个文件被更改,包括 53 次插入203 次删除
  1. 129
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  2. 21
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs
  3. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorPresenter.cs.meta
  4. 94
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorPresenter.cs

129
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Graphing.Util;
using UnityEngine;

{
public class GraphInspectorView : VisualElement, IDisposable
{
[SerializeField]
GraphInspectorPresenter m_Presenter;
VisualElement m_Title;
VisualElement m_PropertyItems;
VisualElement m_ContentContainer;
AbstractNodeEditorView m_EditorView;

VisualElement m_TopContainer;
AbstractMaterialGraph m_Graph;
PreviewData m_PreviewHandle;
public GraphInspectorView()
public GraphInspectorView(string assetName, PreviewSystem previewSystem, AbstractMaterialGraph graph)
m_Graph = graph;
var masterNode = graph.GetNodes<MasterNode>().FirstOrDefault();
if (masterNode != null)
{
m_PreviewHandle = previewSystem.GetPreview(masterNode);
m_PreviewHandle.onPreviewChanged += OnPreviewChanged;
}
m_SelectedNodes = new List<INode>();
m_TopContainer = new VisualElement { name = "top" };
var topContainer = new VisualElement { name = "top" };
m_Title = new VisualElement() { name = "title" };
headerContainer.Add(m_Title);
headerContainer.Add(new VisualElement { name = "title", text = assetName });
m_TopContainer.Add(headerContainer);
topContainer.Add(headerContainer);
m_TopContainer.Add(m_ContentContainer);
topContainer.Add(m_ContentContainer);
Add(m_TopContainer);
Add(topContainer);
var bottomContainer = new VisualElement { name = "bottom" };
{

}
Add(bottomContainer);
foreach (var property in m_Graph.properties)
m_PropertyItems.Add(new ShaderPropertyView(m_Graph, property));
m_Graph.onChange += OnGraphChange;
// Nodes missing custom editors:
// - PropertyNode
// - SubGraphInputNode

// { typeof(AbstractSurfaceMasterNode), typeof(SurfaceMasterNodeEditorView) }
};
}
List<INode> m_SelectedNodes;
void OnAddProperty()
{

void AddProperty(IShaderProperty property)
{
m_Presenter.graph.owner.RegisterCompleteObjectUndo("Add Property");
m_Presenter.graph.AddShaderProperty(property);
m_Graph.owner.RegisterCompleteObjectUndo("Add Property");
m_Graph.AddShaderProperty(property);
public void OnChange(GraphInspectorPresenter.ChangeType changeType)
void OnPreviewChanged()
if (presenter == null)
{
m_ContentContainer.Clear();
m_SelectionHash = 0;
return;
}
m_Preview.image = m_PreviewHandle.texture ?? Texture2D.blackTexture;
}
if ((changeType & GraphInspectorPresenter.ChangeType.AssetName) != 0)
m_Title.text = presenter.assetName;
public void UpdateSelection(IEnumerable<INode> nodes)
{
m_SelectedNodes.Clear();
m_SelectedNodes.AddRange(nodes);
if ((changeType & GraphInspectorPresenter.ChangeType.SelectedNodes) != 0)
var selectionHash = UIUtilities.GetHashCode(m_SelectedNodes.Count, m_SelectedNodes != null ? m_SelectedNodes.FirstOrDefault() : null);
if (selectionHash != m_SelectionHash)
var selectionHash = UIUtilities.GetHashCode(presenter.selectedNodes.Count, presenter.selectedNodes != null ? presenter.selectedNodes.FirstOrDefault() : null);
if (selectionHash != m_SelectionHash)
m_SelectionHash = selectionHash;
m_ContentContainer.Clear();
if (m_SelectedNodes.Count > 1)
m_SelectionHash = selectionHash;
m_ContentContainer.Clear();
if (presenter.selectedNodes.Count > 1)
{
var element = new VisualElement { name = "selectionCount", text = string.Format("{0} nodes selected.", presenter.selectedNodes.Count) };
m_ContentContainer.Add(element);
}
else if (presenter.selectedNodes.Count == 1)
{
var node = presenter.selectedNodes.First();
var view = (AbstractNodeEditorView)Activator.CreateInstance(m_TypeMapper.MapType(node.GetType()));
view.node = node;
m_ContentContainer.Add(view);
}
}
}
if ((changeType & GraphInspectorPresenter.ChangeType.PreviewTexture) != 0)
{
m_Preview.image = presenter.previewTexture ?? Texture2D.blackTexture;
}
if ((changeType & GraphInspectorPresenter.ChangeType.Graph) != 0)
{
if (m_Graph != null)
{
m_Graph.onChange -= OnGraphChange;
m_PropertyItems.Clear();
m_Graph = null;
var element = new VisualElement { name = "selectionCount", text = string.Format("{0} nodes selected.", m_SelectedNodes.Count) };
m_ContentContainer.Add(element);
if (m_Presenter.graph != null)
else if (m_SelectedNodes.Count == 1)
m_Graph = m_Presenter.graph;
foreach (var property in m_Graph.properties)
m_PropertyItems.Add(new ShaderPropertyView(m_Graph, property));
m_Graph.onChange += OnGraphChange;
var node = m_SelectedNodes.First();
var view = (AbstractNodeEditorView)Activator.CreateInstance(m_TypeMapper.MapType(node.GetType()));
view.node = node;
m_ContentContainer.Add(view);
}
}
}

}
}
AbstractMaterialGraph m_Graph;
public GraphInspectorPresenter presenter
public void Dispose()
get { return m_Presenter; }
set
if (m_PreviewHandle != null)
if (m_Presenter == value)
return;
if (m_Presenter != null)
m_Presenter.onChange -= OnChange;
m_Presenter = value;
OnChange(GraphInspectorPresenter.ChangeType.All);
m_Presenter.onChange += OnChange;
m_PreviewHandle.onPreviewChanged -= OnPreviewChanged;
m_PreviewHandle = null;
}
public void Dispose()
{
if (m_Presenter != null)
m_Presenter.onChange -= OnChange;
}
}
}

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


[SerializeField]
MaterialGraphPresenter m_GraphPresenter;
[SerializeField]
GraphInspectorPresenter m_GraphInspectorPresenter;
public Action onUpdateAssetClick { get; set; }
public Action onConvertToSubgraphClick { get; set; }
public Action onShowInProjectClick { get; set; }

set { m_GraphPresenter = value; }
}
public GraphInspectorPresenter graphInspectorPresenter
{
get { return m_GraphInspectorPresenter; }
set { m_GraphInspectorPresenter = value; }
}
public PreviewSystem previewSystem
{
get { return m_PreviewSystem; }

previewSystem = new PreviewSystem(graph);
m_GraphInspectorPresenter = ScriptableObject.CreateInstance<GraphInspectorPresenter>();
m_GraphInspectorPresenter.Initialize(assetName, previewSystem, container);
m_GraphPresenter.onSelectionChanged += m_GraphInspectorPresenter.UpdateSelection;
m_ToolbarView = new ToolbarView { name = "TitleBar" };
{

content.name = "content";
{
m_GraphView = new MaterialGraphView { name = "GraphView", presenter = m_GraphPresenter };
m_GraphInspectorView = new GraphInspectorView() { name = "inspector", presenter = m_GraphInspectorPresenter};
m_GraphInspectorView = new GraphInspectorView(assetName, previewSystem, graph) { name = "inspector" };
m_GraphPresenter.onSelectionChanged += m_GraphInspectorView.UpdateSelection;
content.Add(m_GraphView);
content.Add(m_GraphInspectorView);
}

onConvertToSubgraphClick = null;
onShowInProjectClick = null;
if (m_GraphInspectorView != null) m_GraphInspectorView.Dispose();
if (m_GraphInspectorPresenter != null)
{
m_GraphInspectorPresenter.Dispose();
m_GraphInspectorPresenter = null;
}
if (previewSystem != null)
{
previewSystem.Dispose();

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorPresenter.cs.meta


fileFormatVersion: 2
guid: d8c67032914554203be73439532c83b8
timeCreated: 1502956093
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

94
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorPresenter.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Graphing;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph.Drawing.Inspector
{
public class GraphInspectorPresenter : ScriptableObject, IDisposable
{
PreviewData m_PreviewHandle;
public string assetName { get; set; }
public List<INode> selectedNodes { get; set; }
public Texture previewTexture { get; private set; }
[SerializeField]
private int m_Version;
[Flags]
public enum ChangeType
{
Graph = 1 << 0,
SelectedNodes = 1 << 1,
AssetName = 1 << 2,
PreviewTexture = 1 << 3,
All = -1
}
public delegate void OnChange(ChangeType changeType);
public OnChange onChange;
[SerializeField]
private HelperMaterialGraphEditWindow m_Owner;
public AbstractMaterialGraph graph
{
get { return m_Owner.GetMaterialGraph(); }
}
public void Dirty()
{
m_Version++;
}
public void Initialize(string assetName, PreviewSystem previewSystem, HelperMaterialGraphEditWindow window)
{
m_Owner = window;
var masterNode = graph.GetNodes<MasterNode>().FirstOrDefault();
if (masterNode != null)
{
m_PreviewHandle = previewSystem.GetPreview(masterNode);
m_PreviewHandle.onPreviewChanged += OnPreviewChanged;
}
this.assetName = assetName;
selectedNodes = new List<INode>();
NotifyChange(ChangeType.Graph | ChangeType.SelectedNodes | ChangeType.AssetName);
}
void OnPreviewChanged()
{
previewTexture = m_PreviewHandle.texture;
NotifyChange(ChangeType.PreviewTexture);
}
public void UpdateSelection(IEnumerable<INode> nodes)
{
selectedNodes.Clear();
selectedNodes.AddRange(nodes);
NotifyChange(ChangeType.SelectedNodes);
}
void NotifyChange(ChangeType changeType)
{
if (onChange != null)
onChange(changeType);
}
public void Dispose()
{
if (m_PreviewHandle != null)
{
m_PreviewHandle.onPreviewChanged -= OnPreviewChanged;
m_PreviewHandle = null;
};
}
}
}
正在加载...
取消
保存