您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

133 行
4.4 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEngine;
using UnityEngine.Graphing;
using UnityEngine.MaterialGraph;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEngine.Experimental.UIElements;
using Edge = UnityEditor.Experimental.UIElements.GraphView.Edge;
using MouseButton = UnityEngine.Experimental.UIElements.MouseButton;
namespace UnityEditor.MaterialGraph.Drawing
{
public sealed class MaterialGraphView : GraphView
{
public MaterialGraphView()
{
RegisterCallback<MouseUpEvent>(DoContextMenu, Capture.Capture);
SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale);
this.AddManipulator(new ContentDragger());
this.AddManipulator(new RectangleSelector());
this.AddManipulator(new SelectionDragger());
this.AddManipulator(new ClickSelector());
Insert(0, new GridBackground());
typeFactory[typeof(MaterialNodePresenter)] = typeof(MaterialNodeView);
typeFactory[typeof(GraphAnchorPresenter)] = typeof(NodeAnchor);
typeFactory[typeof(EdgePresenter)] = typeof(Edge);
AddStyleSheetPath("Styles/MaterialGraph");
}
public bool CanAddToNodeMenu(Type type)
{
return true;
}
void DoContextMenu(MouseUpEvent evt)
{
if (evt.button == (int)MouseButton.RightMouse)
{
var gm = new GenericMenu();
foreach (Type type in Assembly.GetAssembly(typeof(AbstractMaterialNode)).GetTypes())
{
if (type.IsClass && !type.IsAbstract && (type.IsSubclassOf(typeof(AbstractMaterialNode))))
{
var attrs = type.GetCustomAttributes(typeof(TitleAttribute), false) as TitleAttribute[];
if (attrs != null && attrs.Length > 0 && CanAddToNodeMenu(type))
{
gm.AddItem(new GUIContent(attrs[0].m_Title), false, AddNode, new AddNodeCreationObject(type, evt.mousePosition));
}
}
}
gm.ShowAsContext();
}
evt.StopPropagation();
}
class AddNodeCreationObject
{
public Vector2 m_Pos;
public readonly Type m_Type;
public AddNodeCreationObject(Type t, Vector2 p)
{
m_Type = t;
m_Pos = p;
}
};
void AddNode(object obj)
{
var posObj = obj as AddNodeCreationObject;
if (posObj == null)
return;
INode node;
try
{
node = Activator.CreateInstance(posObj.m_Type) as INode;
}
catch (Exception e)
{
Debug.LogErrorFormat("Could not construct instance of: {0} - {1}", posObj.m_Type, e);
return;
}
if (node == null)
return;
var drawstate = node.drawState;
Vector3 localPos = contentViewContainer.transform.matrix.inverse.MultiplyPoint3x4(posObj.m_Pos);
drawstate.position = new Rect(localPos.x, localPos.y, 0, 0);
node.drawState = drawstate;
var graphDataSource = GetPresenter<MaterialGraphPresenter>();
graphDataSource.AddNode(node);
}
void PropagateSelection()
{
var graphPresenter = GetPresenter<MaterialGraphPresenter>();
if (graphPresenter == null)
return;
var selectedNodes = selection.OfType<MaterialNodeView>().Select(x => (MaterialNodePresenter)x.presenter);
graphPresenter.UpdateSelection(selectedNodes);
}
public override void AddToSelection(ISelectable selectable)
{
base.AddToSelection(selectable);
PropagateSelection();
}
public override void RemoveFromSelection(ISelectable selectable)
{
base.RemoveFromSelection(selectable);
PropagateSelection();
}
public override void ClearSelection()
{
base.ClearSelection();
PropagateSelection();
}
}
}