浏览代码

Make TypeMapper data-driven rather than generic and remove old inspector things from GraphInspectorPresenter

/main
Peter Bay Bastian 7 年前
当前提交
c884fe33
共有 2 个文件被更改,包括 24 次插入33 次删除
  1. 34
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/Presenters/GraphInspectorPresenter.cs
  2. 23
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/TypeMapper.cs

34
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/Presenters/GraphInspectorPresenter.cs


public class GraphInspectorPresenter : ScriptableObject
{
[SerializeField]
List<AbstractNodeInspector> m_Inspectors;
[SerializeField]
public List<AbstractNodeInspector> inspectors
{
get { return m_Inspectors; }
set { m_Inspectors = value; }
}
public List<AbstractNodeEditorPresenter> editors
{

set { m_Title = value; }
}
TypeMapper<INode, AbstractNodeInspector> m_InspectorMapper;
TypeMapper m_TypeMapper;
inspectors = new List<AbstractNodeInspector>();
m_InspectorMapper = new TypeMapper<INode, AbstractNodeInspector>(typeof(BasicNodeInspector))
{
{typeof(AbstractSurfaceMasterNode), typeof(SurfaceMasterNodeInspector)},
{typeof(PropertyNode), typeof(PropertyNodeInspector)},
{typeof(SubGraphInputNode), typeof(SubgraphInputNodeInspector)},
{typeof(SubGraphOutputNode), typeof(SubgraphOutputNodeInspector)}
};
m_TypeMapper = new TypeMapper(typeof(INode), typeof(AbstractNodeEditorPresenter), typeof(StandardNodeEditorPresenter));
// m_InspectorMapper = new TypeMapper<INode, AbstractNodeInspector>(typeof(BasicNodeInspector))
// {
// {typeof(AbstractSurfaceMasterNode), typeof(SurfaceMasterNodeInspector)},
// {typeof(PropertyNode), typeof(PropertyNodeInspector)},
// {typeof(SubGraphInputNode), typeof(SubgraphInputNodeInspector)},
// {typeof(SubGraphOutputNode), typeof(SubgraphOutputNodeInspector)}
// };
m_Inspectors.Clear();
var inspector = (AbstractNodeInspector) CreateInstance(m_InspectorMapper.MapType(node.GetType()));
inspector.Initialize(node);
m_Inspectors.Add(inspector);
var editor = CreateInstance<StandardNodeEditorPresenter>();
var editor = (AbstractNodeEditorPresenter) CreateInstance(m_TypeMapper.MapType(node.GetType()));
editor.Initialize(node);
editors.Add(editor);
}

23
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/TypeMapper.cs


using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class TypeMapper<TFrom, TTo> : IEnumerable<TypeMapping>
public class TypeMapper : IEnumerable<TypeMapping>
readonly Type m_FromBaseType;
readonly Type m_ToBaseType;
public TypeMapper(Type fallbackType = null)
public TypeMapper(Type fromBaseType = null, Type toBaseType = null, Type fallbackType = null)
if (fallbackType != null && !(fallbackType.IsSubclassOf(typeof(TFrom)) || fallbackType.GetInterfaces().Contains(typeof(TFrom))))
throw new ArgumentException(string.Format("{0} does not implement or derive from {1}.", fallbackType.Name, typeof(TFrom).Name), "fallbackType");
if (fallbackType != null && toBaseType != null && !fallbackType.IsSubclassOf(toBaseType))
throw new ArgumentException(string.Format("{0} does not implement or derive from {1}.", fallbackType.Name, toBaseType.Name), "fallbackType");
m_FromBaseType = fromBaseType ?? typeof(object);
m_ToBaseType = toBaseType;
m_FallbackType = fallbackType;
}

public void Add(Type fromType, Type toType)
{
if (!fromType.IsSubclassOf(typeof(TFrom)) && !fromType.GetInterfaces().Contains(typeof(TFrom)))
if (m_FromBaseType != typeof(object) && !fromType.IsSubclassOf(m_FromBaseType) && !fromType.GetInterfaces().Contains(m_FromBaseType))
throw new ArgumentException(string.Format("{0} does not implement or derive from {1}.", fromType.Name, typeof(TFrom).Name), "fromType");
throw new ArgumentException(string.Format("{0} does not implement or derive from {1}.", fromType.Name, m_FromBaseType.Name), "fromType");
if (!toType.IsSubclassOf(typeof(TTo)))
if (m_ToBaseType != null && !toType.IsSubclassOf(m_ToBaseType))
throw new ArgumentException(string.Format("{0} does not derive from {1}.", toType.Name, typeof(TTo).Name), "toType");
throw new ArgumentException(string.Format("{0} does not derive from {1}.", toType.Name, m_ToBaseType.Name), "toType");
}
m_Mappings[fromType] = toType;

{
Type toType = null;
while (toType == null && fromType != null && fromType != typeof(TFrom))
while (toType == null && fromType != null && fromType != m_FromBaseType)
{
if (!m_Mappings.TryGetValue(fromType, out toType))
fromType = fromType.BaseType;

正在加载...
取消
保存