浏览代码

TypeMapper and ScriptableObjectFactory

Only used in AbstractGraphInspector for now
/main
Peter Bay Bastian 8 年前
当前提交
ab990b62
共有 8 个文件被更改,包括 151 次插入16 次删除
  1. 25
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphInspector.cs
  2. 21
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphInspector.cs
  3. 24
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/ScriptableObjectFactory.cs
  4. 12
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/ScriptableObjectFactory.cs.meta
  5. 45
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapper.cs
  6. 12
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapper.cs.meta
  7. 16
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapping.cs
  8. 12
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapping.cs.meta

25
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphInspector.cs


{
public abstract class AbstractGraphInspector : Editor
{
protected GraphTypeMapper typeMapper { get; set; }
private ScriptableObjectFactory<INode, AbstractNodeInspector, BasicNodeInspector> m_InspectorFactory;
private List<INode> m_SelectedNodes = new List<INode>();
protected List<INode> m_SelectedNodes = new List<INode>();
protected IEnumerable<INode> selectedNodes
{
get { return m_SelectedNodes; }
}
protected List<AbstractNodeInspector> m_Inspectors = new List<AbstractNodeInspector>();
private List<AbstractNodeInspector> m_Inspectors = new List<AbstractNodeInspector>();
protected IGraphAsset graphAsset
{

protected AbstractGraphInspector()
protected AbstractGraphInspector(IEnumerable<TypeMapping> typeMappings)
typeMapper = new GraphTypeMapper(typeof(BasicNodeInspector));
m_InspectorFactory = new ScriptableObjectFactory<INode, AbstractNodeInspector, BasicNodeInspector>(typeMappings);
}
public override void OnInspectorGUI()

if (graphAsset == null)
return;
using (var selectedNodes = ListPool<INode>.GetDisposable())
using (var nodes = ListPool<INode>.GetDisposable())
selectedNodes.value.AddRange(graphAsset.drawingData.selection.Select(graphAsset.graph.GetNodeFromGuid));
if (m_SelectedNodes == null || m_Inspectors.Any(i => i.node == null) || !selectedNodes.value.SequenceEqual(m_SelectedNodes))
OnSelectionChanged(selectedNodes.value);
nodes.value.AddRange(graphAsset.drawingData.selection.Select(graphAsset.graph.GetNodeFromGuid));
if (m_SelectedNodes == null || m_Inspectors.Any(i => i.node == null) || !nodes.value.SequenceEqual(m_SelectedNodes))
OnSelectionChanged(nodes.value);
}
}

m_Inspectors.Clear();
foreach (var node in m_SelectedNodes.OfType<SerializableNode>())
{
var inspector = (AbstractNodeInspector)typeMapper.Create(node);
var inspector = m_InspectorFactory.Create(node);
inspector.Initialize(node);
m_Inspectors.Add(inspector);
}

21
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphInspector.cs


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

}
}
protected AbstractMaterialGraphInspector()
protected AbstractMaterialGraphInspector() : base(typeMappings)
{
}
private static IEnumerable<TypeMapping> typeMappings
typeMapper[typeof(AbstractSurfaceMasterNode)] = typeof(SurfaceMasterNodeInspector);
typeMapper[typeof(SubGraphInputNode)] = typeof(SubgraphInputNodeInspector);
typeMapper[typeof(SubGraphOutputNode)] = typeof(SubgraphOutputNodeInspector);
get
{
yield return new TypeMapping(typeof(AbstractSurfaceMasterNode), typeof(SurfaceMasterNodeInspector));
yield return new TypeMapping(typeof(SubGraphInputNode), typeof(SubgraphInputNodeInspector));
yield return new TypeMapping(typeof(SubGraphOutputNode), typeof(SubgraphOutputNodeInspector));
}
}
private void OnPreviewNodeModified(INode node, ModificationScope scope)

public override void OnPreviewSettings()
{
GUI.enabled = m_SelectedNodes.Count <= 1 && m_SelectedNodes.FirstOrDefault() != previewNode;
GUI.enabled = selectedNodes.Count() <= 1 && selectedNodes.FirstOrDefault() != previewNode;
previewNode = m_SelectedNodes.FirstOrDefault() as AbstractMaterialNode;
previewNode = selectedNodes.FirstOrDefault() as AbstractMaterialNode;
GUI.enabled = true;
}

24
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/ScriptableObjectFactory.cs


using System.Collections.Generic;
using UnityEngine;
namespace UnityEditor.Graphing.Util
{
public class ScriptableObjectFactory<TFrom, TTo, TFallback>
where TTo : ScriptableObject
where TFallback : TTo
{
private readonly TypeMapper<TFrom, TTo, TFallback> m_TypeMapper = new TypeMapper<TFrom, TTo, TFallback>();
public ScriptableObjectFactory(IEnumerable<TypeMapping> typeMappings)
{
foreach (var typeMapping in typeMappings)
m_TypeMapper.AddMapping(typeMapping);
}
public TTo Create(TFrom from)
{
var toType = m_TypeMapper.MapType(from.GetType());
return ScriptableObject.CreateInstance(toType) as TTo;
}
}
}

12
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/ScriptableObjectFactory.cs.meta


fileFormatVersion: 2
guid: 889e2994de7c9164eaf187757dee6bee
timeCreated: 1485164640
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

45
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapper.cs


using System;
using System.Collections.Generic;
using System.Linq;
namespace UnityEditor.Graphing.Util
{
public class TypeMapper<TFrom, TTo, TFallback>
where TFallback : TTo
{
private readonly Dictionary<Type, Type> m_Mappings = new Dictionary<Type, Type>();
public void AddMapping(TypeMapping mapping)
{
AddMapping(mapping.fromType, mapping.toType);
}
public void AddMapping(Type fromType, Type toType)
{
if (!fromType.IsSubclassOf(typeof(TFrom)) && !fromType.GetInterfaces().Contains(typeof(TFrom)))
{
throw new ArgumentException(string.Format("{0} does not implement or derive from {1}.", fromType.Name, typeof(TFrom).Name), "fromType");
}
if (!toType.IsSubclassOf(typeof(TTo)))
{
throw new ArgumentException(string.Format("{0} does not derive from {1}.", toType.Name, typeof(TTo).Name), "toType");
}
m_Mappings[fromType] = toType;
}
public Type MapType(Type fromType)
{
Type toType = null;
while (toType == null && fromType != null && fromType != typeof(TFrom))
{
if (!m_Mappings.TryGetValue(fromType, out toType))
fromType = fromType.BaseType;
}
return toType ?? typeof(TFallback);
}
}
}

12
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapper.cs.meta


fileFormatVersion: 2
guid: f9c0c57b328abb94982f26ec960996d2
timeCreated: 1485161458
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

16
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapping.cs


using System;
namespace UnityEditor.Graphing.Util
{
public class TypeMapping
{
public Type fromType { get; private set; }
public Type toType { get; private set; }
public TypeMapping(Type fromType, Type toType)
{
this.fromType = fromType;
this.toType = toType;
}
}
}

12
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapping.cs.meta


fileFormatVersion: 2
guid: 53ba6b433697da6468377c205573f728
timeCreated: 1485164640
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存