当前提交
9fb975a4
共有 15 个文件被更改,包括 827 次插入 和 1190 次删除
-
496MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/CopySelected.cs
-
120MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/DeleteSelected.cs
-
421MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/DrawableNode.cs
-
486MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/GraphDataSource.cs
-
135MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/NullInputProxy.cs
-
12MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/MyNodeAdapters.cs.meta
-
12MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/NodeAnchor.cs.meta
-
60MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/MyNodeAdapters.cs
-
101MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/NodeAnchor.cs
-
16MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/DrawableEdge.cs
-
12MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/DrawableEdge.cs.meta
-
125MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/SimpleWidgets.cs
-
12MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/SimpleWidgets.cs.meta
-
9MaterialGraphProject/Assets/Canvas2D.meta
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using UnityEditor.Experimental; |
|||
using UnityEngine; |
|||
using UnityEngine.Graphing; |
|||
|
|||
namespace UnityEditor.Graphing.Drawing |
|||
{ |
|||
[Serializable] |
|||
internal class CopyPasteGraph : ISerializationCallbackReceiver |
|||
{ |
|||
[NonSerialized] |
|||
private HashSet<IEdge> m_Edges = new HashSet<IEdge>(); |
|||
|
|||
[NonSerialized] |
|||
private HashSet<INode> m_Nodes = new HashSet<INode>(); |
|||
|
|||
[SerializeField] |
|||
List<SerializationHelper.JSONSerializedElement> m_SerializableNodes = new List<SerializationHelper.JSONSerializedElement>(); |
|||
|
|||
[SerializeField] |
|||
List<SerializationHelper.JSONSerializedElement> m_SerializableEdges = new List<SerializationHelper.JSONSerializedElement>(); |
|||
|
|||
public virtual void AddNode(INode node) |
|||
{ |
|||
m_Nodes.Add(node); |
|||
} |
|||
|
|||
public void AddEdge(IEdge edge) |
|||
{ |
|||
m_Edges.Add(edge); |
|||
} |
|||
|
|||
public IEnumerable<T> GetNodes<T>() where T : INode |
|||
{ |
|||
return m_Nodes.OfType<T>(); |
|||
} |
|||
|
|||
public IEnumerable<IEdge> edges |
|||
{ |
|||
get { return m_Edges; } |
|||
} |
|||
|
|||
public virtual void OnBeforeSerialize() |
|||
{ |
|||
m_SerializableNodes = SerializationHelper.Serialize<INode>(m_Nodes); |
|||
m_SerializableEdges = SerializationHelper.Serialize<IEdge>(m_Edges); |
|||
} |
|||
|
|||
public virtual void OnAfterDeserialize() |
|||
{ |
|||
var nodes = SerializationHelper.Deserialize<INode>(m_SerializableNodes); |
|||
m_Nodes.Clear(); |
|||
foreach (var node in nodes) |
|||
m_Nodes.Add(node); |
|||
m_SerializableNodes = null; |
|||
|
|||
var edges = SerializationHelper.Deserialize<IEdge>(m_SerializableEdges); |
|||
m_Edges.Clear(); |
|||
foreach (var edge in edges) |
|||
m_Edges.Add(edge); |
|||
m_SerializableEdges = null; |
|||
} |
|||
} |
|||
|
|||
internal class CopySelected : IManipulate |
|||
{ |
|||
public delegate void CopyElements(List<CanvasElement> elements); |
|||
|
|||
public bool GetCaps(ManipulatorCapability cap) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
public void AttachTo(CanvasElement element) |
|||
{ |
|||
element.ValidateCommand += Validate; |
|||
element.ExecuteCommand += CopyPaste; |
|||
} |
|||
|
|||
private bool Validate(CanvasElement element, Event e, Canvas2D parent) |
|||
{ |
|||
if (e.type == EventType.Used) |
|||
return false; |
|||
|
|||
if (e.commandName != "Copy" && e.commandName != "Paste" && e.commandName != "Duplicate") |
|||
return false; |
|||
|
|||
e.Use(); |
|||
return true; |
|||
} |
|||
|
|||
private bool CopyPaste(CanvasElement element, Event e, Canvas2D parent) |
|||
{ |
|||
if (e.type == EventType.Used) |
|||
return false; |
|||
|
|||
if (e.commandName != "Copy" && e.commandName != "Paste" && e.commandName != "Duplicate") |
|||
return false; |
|||
|
|||
if (e.commandName == "Copy" || e.commandName == "Duplicate") |
|||
DoCopy(parent); |
|||
|
|||
if (e.commandName == "Paste" || e.commandName == "Duplicate") |
|||
DoPaste(parent); |
|||
|
|||
e.Use(); |
|||
return true; |
|||
} |
|||
|
|||
private static void DoCopy(Canvas2D parent) |
|||
{ |
|||
EditorGUIUtility.systemCopyBuffer = SerializeSelectedElements(parent); |
|||
} |
|||
|
|||
public static string SerializeSelectedElements(Canvas2D parent) |
|||
{ |
|||
var selectedElements = parent.selection; |
|||
|
|||
// build a graph to serialize (will just contain the
|
|||
// nodes and edges we are interested in.
|
|||
var graph = new CopyPasteGraph(); |
|||
foreach (var thing in selectedElements) |
|||
{ |
|||
var dNode = thing as DrawableNode; |
|||
if (dNode != null) |
|||
{ |
|||
graph.AddNode(dNode.m_Node); |
|||
foreach (var edge in NodeUtils.GetAllEdges(dNode.m_Node)) |
|||
graph.AddEdge(edge); |
|||
} |
|||
|
|||
var dEdge = thing as DrawableEdge<NodeAnchor>; |
|||
if (dEdge != null) |
|||
{ |
|||
graph.AddEdge(dEdge.m_Edge); |
|||
} |
|||
} |
|||
// serialize then break references
|
|||
var serialized = JsonUtility.ToJson(graph, true); |
|||
return serialized; |
|||
} |
|||
|
|||
public static CopyPasteGraph DeserializeSelectedElements(string toDeserialize) |
|||
{ |
|||
try |
|||
{ |
|||
return JsonUtility.FromJson<CopyPasteGraph>(toDeserialize); |
|||
} |
|||
catch |
|||
{ |
|||
// ignored. just means copy buffer was not a graph :(
|
|||
return null; |
|||
} |
|||
} |
|||
|
|||
private static void DoPaste(Canvas2D parent) |
|||
{ |
|||
var copyText = EditorGUIUtility.systemCopyBuffer; |
|||
if (string.IsNullOrEmpty(copyText)) |
|||
return; |
|||
|
|||
var pastedGraph = DeserializeSelectedElements(copyText); |
|||
if (pastedGraph == null) |
|||
return; |
|||
|
|||
if (parent.dataSource == null) |
|||
return; |
|||
|
|||
var dataSource = parent.dataSource as GraphDataSource; |
|||
if (dataSource == null) |
|||
return; |
|||
|
|||
var asset = dataSource.graphAsset; |
|||
if (asset == null) |
|||
return; |
|||
|
|||
var graph = asset.graph; |
|||
if (graph == null) |
|||
return; |
|||
|
|||
var addedNodes = new List<INode>(); |
|||
|
|||
var nodeGuidMap = new Dictionary<Guid, Guid>(); |
|||
foreach (var node in pastedGraph.GetNodes<INode>()) |
|||
{ |
|||
var oldGuid = node.guid; |
|||
var newGuid = node.RewriteGuid(); |
|||
nodeGuidMap[oldGuid] = newGuid; |
|||
|
|||
var drawState = node.drawState; |
|||
var position = drawState.position; |
|||
position.x += 30; |
|||
position.y += 30; |
|||
drawState.position = position; |
|||
node.drawState = drawState; |
|||
graph.AddNode(node); |
|||
addedNodes.Add(node); |
|||
} |
|||
|
|||
// only connect edges within pasted elements, discard
|
|||
// external edges.
|
|||
var addedEdges = new List<IEdge>(); |
|||
|
|||
foreach (var edge in pastedGraph.edges) |
|||
{ |
|||
var outputSlot = edge.outputSlot; |
|||
var inputSlot = edge.inputSlot; |
|||
|
|||
Guid remappedOutputNodeGuid; |
|||
Guid remappedInputNodeGuid; |
|||
if (nodeGuidMap.TryGetValue(outputSlot.nodeGuid, out remappedOutputNodeGuid) |
|||
&& nodeGuidMap.TryGetValue(inputSlot.nodeGuid, out remappedInputNodeGuid)) |
|||
{ |
|||
var outputSlotRef = new SlotReference(remappedOutputNodeGuid, outputSlot.slotId); |
|||
var inputSlotRef = new SlotReference(remappedInputNodeGuid, inputSlot.slotId); |
|||
addedEdges.Add(graph.Connect(outputSlotRef, inputSlotRef)); |
|||
} |
|||
} |
|||
|
|||
graph.ValidateGraph(); |
|||
parent.ReloadData(); |
|||
parent.Invalidate(); |
|||
|
|||
parent.selection.Clear(); |
|||
foreach (var element in parent.elements) |
|||
{ |
|||
var drawableNode = element as DrawableNode; |
|||
if (drawableNode != null && addedNodes.Any(x => x == drawableNode.m_Node)) |
|||
{ |
|||
drawableNode.selected = true; |
|||
parent.selection.Add(drawableNode); |
|||
continue; |
|||
} |
|||
|
|||
var drawableEdge = element as DrawableEdge<NodeAnchor>; |
|||
if (drawableEdge != null && addedEdges.Any(x => x == drawableEdge.m_Edge)) |
|||
{ |
|||
drawableEdge.selected = true; |
|||
parent.selection.Add(drawableEdge); |
|||
} |
|||
} |
|||
|
|||
parent.Repaint(); |
|||
} |
|||
} |
|||
} |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using UnityEngine; |
|||
using UnityEngine.Graphing; |
|||
|
|||
/* |
|||
namespace UnityEditor.Graphing.Drawing |
|||
{ |
|||
[Serializable] |
|||
internal class CopyPasteGraph : ISerializationCallbackReceiver |
|||
{ |
|||
[NonSerialized] |
|||
private HashSet<IEdge> m_Edges = new HashSet<IEdge>(); |
|||
|
|||
[NonSerialized] |
|||
private HashSet<INode> m_Nodes = new HashSet<INode>(); |
|||
|
|||
[SerializeField] |
|||
List<SerializationHelper.JSONSerializedElement> m_SerializableNodes = new List<SerializationHelper.JSONSerializedElement>(); |
|||
|
|||
[SerializeField] |
|||
List<SerializationHelper.JSONSerializedElement> m_SerializableEdges = new List<SerializationHelper.JSONSerializedElement>(); |
|||
|
|||
public virtual void AddNode(INode node) |
|||
{ |
|||
m_Nodes.Add(node); |
|||
} |
|||
|
|||
public void AddEdge(IEdge edge) |
|||
{ |
|||
m_Edges.Add(edge); |
|||
} |
|||
|
|||
public IEnumerable<T> GetNodes<T>() where T : INode |
|||
{ |
|||
return m_Nodes.OfType<T>(); |
|||
} |
|||
|
|||
public IEnumerable<IEdge> edges |
|||
{ |
|||
get { return m_Edges; } |
|||
} |
|||
|
|||
public virtual void OnBeforeSerialize() |
|||
{ |
|||
m_SerializableNodes = SerializationHelper.Serialize<INode>(m_Nodes); |
|||
m_SerializableEdges = SerializationHelper.Serialize<IEdge>(m_Edges); |
|||
} |
|||
|
|||
public virtual void OnAfterDeserialize() |
|||
{ |
|||
var nodes = SerializationHelper.Deserialize<INode>(m_SerializableNodes); |
|||
m_Nodes.Clear(); |
|||
foreach (var node in nodes) |
|||
m_Nodes.Add(node); |
|||
m_SerializableNodes = null; |
|||
|
|||
var edges = SerializationHelper.Deserialize<IEdge>(m_SerializableEdges); |
|||
m_Edges.Clear(); |
|||
foreach (var edge in edges) |
|||
m_Edges.Add(edge); |
|||
m_SerializableEdges = null; |
|||
} |
|||
} |
|||
|
|||
internal class CopySelected : IManipulate |
|||
{ |
|||
public delegate void CopyElements(List<CanvasElement> elements); |
|||
|
|||
public bool GetCaps(ManipulatorCapability cap) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
public void AttachTo(CanvasElement element) |
|||
{ |
|||
element.ValidateCommand += Validate; |
|||
element.ExecuteCommand += CopyPaste; |
|||
} |
|||
|
|||
private bool Validate(CanvasElement element, Event e, Canvas2D parent) |
|||
{ |
|||
if (e.type == EventType.Used) |
|||
return false; |
|||
|
|||
if (e.commandName != "Copy" && e.commandName != "Paste" && e.commandName != "Duplicate") |
|||
return false; |
|||
|
|||
e.Use(); |
|||
return true; |
|||
} |
|||
|
|||
private bool CopyPaste(CanvasElement element, Event e, Canvas2D parent) |
|||
{ |
|||
if (e.type == EventType.Used) |
|||
return false; |
|||
|
|||
if (e.commandName != "Copy" && e.commandName != "Paste" && e.commandName != "Duplicate") |
|||
return false; |
|||
|
|||
if (e.commandName == "Copy" || e.commandName == "Duplicate") |
|||
DoCopy(parent); |
|||
|
|||
if (e.commandName == "Paste" || e.commandName == "Duplicate") |
|||
DoPaste(parent); |
|||
|
|||
e.Use(); |
|||
return true; |
|||
} |
|||
|
|||
private static void DoCopy(Canvas2D parent) |
|||
{ |
|||
EditorGUIUtility.systemCopyBuffer = SerializeSelectedElements(parent); |
|||
} |
|||
|
|||
public static string SerializeSelectedElements(Canvas2D parent) |
|||
{ |
|||
var selectedElements = parent.selection; |
|||
|
|||
// build a graph to serialize (will just contain the
|
|||
// nodes and edges we are interested in.
|
|||
var graph = new CopyPasteGraph(); |
|||
foreach (var thing in selectedElements) |
|||
{ |
|||
var dNode = thing as DrawableNode; |
|||
if (dNode != null) |
|||
{ |
|||
graph.AddNode(dNode.m_Node); |
|||
foreach (var edge in NodeUtils.GetAllEdges(dNode.m_Node)) |
|||
graph.AddEdge(edge); |
|||
} |
|||
|
|||
var dEdge = thing as DrawableEdge<NodeAnchor>; |
|||
if (dEdge != null) |
|||
{ |
|||
graph.AddEdge(dEdge.m_Edge); |
|||
} |
|||
} |
|||
// serialize then break references
|
|||
var serialized = JsonUtility.ToJson(graph, true); |
|||
return serialized; |
|||
} |
|||
|
|||
public static CopyPasteGraph DeserializeSelectedElements(string toDeserialize) |
|||
{ |
|||
try |
|||
{ |
|||
return JsonUtility.FromJson<CopyPasteGraph>(toDeserialize); |
|||
} |
|||
catch |
|||
{ |
|||
// ignored. just means copy buffer was not a graph :(
|
|||
return null; |
|||
} |
|||
} |
|||
|
|||
private static void DoPaste(Canvas2D parent) |
|||
{ |
|||
var copyText = EditorGUIUtility.systemCopyBuffer; |
|||
if (string.IsNullOrEmpty(copyText)) |
|||
return; |
|||
|
|||
var pastedGraph = DeserializeSelectedElements(copyText); |
|||
if (pastedGraph == null) |
|||
return; |
|||
|
|||
if (parent.dataSource == null) |
|||
return; |
|||
|
|||
var dataSource = parent.dataSource as GraphDataSource; |
|||
if (dataSource == null) |
|||
return; |
|||
|
|||
var asset = dataSource.graphAsset; |
|||
if (asset == null) |
|||
return; |
|||
|
|||
var graph = asset.graph; |
|||
if (graph == null) |
|||
return; |
|||
|
|||
var addedNodes = new List<INode>(); |
|||
|
|||
var nodeGuidMap = new Dictionary<Guid, Guid>(); |
|||
foreach (var node in pastedGraph.GetNodes<INode>()) |
|||
{ |
|||
var oldGuid = node.guid; |
|||
var newGuid = node.RewriteGuid(); |
|||
nodeGuidMap[oldGuid] = newGuid; |
|||
|
|||
var drawState = node.drawState; |
|||
var position = drawState.position; |
|||
position.x += 30; |
|||
position.y += 30; |
|||
drawState.position = position; |
|||
node.drawState = drawState; |
|||
graph.AddNode(node); |
|||
addedNodes.Add(node); |
|||
} |
|||
|
|||
// only connect edges within pasted elements, discard
|
|||
// external edges.
|
|||
var addedEdges = new List<IEdge>(); |
|||
|
|||
foreach (var edge in pastedGraph.edges) |
|||
{ |
|||
var outputSlot = edge.outputSlot; |
|||
var inputSlot = edge.inputSlot; |
|||
|
|||
Guid remappedOutputNodeGuid; |
|||
Guid remappedInputNodeGuid; |
|||
if (nodeGuidMap.TryGetValue(outputSlot.nodeGuid, out remappedOutputNodeGuid) |
|||
&& nodeGuidMap.TryGetValue(inputSlot.nodeGuid, out remappedInputNodeGuid)) |
|||
{ |
|||
var outputSlotRef = new SlotReference(remappedOutputNodeGuid, outputSlot.slotId); |
|||
var inputSlotRef = new SlotReference(remappedInputNodeGuid, inputSlot.slotId); |
|||
addedEdges.Add(graph.Connect(outputSlotRef, inputSlotRef)); |
|||
} |
|||
} |
|||
|
|||
graph.ValidateGraph(); |
|||
parent.ReloadData(); |
|||
parent.Invalidate(); |
|||
|
|||
parent.selection.Clear(); |
|||
foreach (var element in parent.elements) |
|||
{ |
|||
var drawableNode = element as DrawableNode; |
|||
if (drawableNode != null && addedNodes.Any(x => x == drawableNode.m_Node)) |
|||
{ |
|||
drawableNode.selected = true; |
|||
parent.selection.Add(drawableNode); |
|||
continue; |
|||
} |
|||
|
|||
var drawableEdge = element as DrawableEdge<NodeAnchor>; |
|||
if (drawableEdge != null && addedEdges.Any(x => x == drawableEdge.m_Edge)) |
|||
{ |
|||
drawableEdge.selected = true; |
|||
parent.selection.Add(drawableEdge); |
|||
} |
|||
} |
|||
|
|||
parent.Repaint(); |
|||
} |
|||
} |
|||
}*/ |
|
|||
using System.Collections.Generic; |
|||
using UnityEditor.Experimental; |
|||
using UnityEngine; |
|||
|
|||
namespace UnityEditor.Graphing.Drawing |
|||
{ |
|||
internal class DeleteSelected : IManipulate |
|||
{ |
|||
public delegate void DeleteElements(List<CanvasElement> elements); |
|||
|
|||
private readonly DeleteElements m_DeletionCallback; |
|||
|
|||
public DeleteSelected(DeleteElements deletionCallback) |
|||
{ |
|||
m_DeletionCallback = deletionCallback; |
|||
} |
|||
|
|||
public bool GetCaps(ManipulatorCapability cap) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
public void AttachTo(CanvasElement element) |
|||
{ |
|||
element.ValidateCommand += Validate; |
|||
element.ExecuteCommand += Delete; |
|||
} |
|||
|
|||
private bool Validate(CanvasElement element, Event e, Canvas2D parent) |
|||
{ |
|||
if (e.type == EventType.Used) |
|||
return false; |
|||
|
|||
if (e.commandName != "Delete" && e.commandName != "SoftDelete") |
|||
return false; |
|||
|
|||
e.Use(); |
|||
return true; |
|||
} |
|||
|
|||
private bool Delete(CanvasElement element, Event e, Canvas2D parent) |
|||
{ |
|||
if (e.type == EventType.Used) |
|||
return false; |
|||
|
|||
if (e.commandName != "Delete" && e.commandName != "SoftDelete") |
|||
return false; |
|||
|
|||
|
|||
if (m_DeletionCallback != null) |
|||
{ |
|||
m_DeletionCallback(parent.selection); |
|||
parent.ReloadData(); |
|||
parent.Repaint(); |
|||
} |
|||
e.Use(); |
|||
return true; |
|||
} |
|||
} |
|||
} |
|||
/*using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
namespace UnityEditor.Graphing.Drawing |
|||
{ |
|||
internal class DeleteSelected : IManipulate |
|||
{ |
|||
public delegate void DeleteElements(List<CanvasElement> elements); |
|||
|
|||
private readonly DeleteElements m_DeletionCallback; |
|||
|
|||
public DeleteSelected(DeleteElements deletionCallback) |
|||
{ |
|||
m_DeletionCallback = deletionCallback; |
|||
} |
|||
|
|||
public bool GetCaps(ManipulatorCapability cap) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
public void AttachTo(CanvasElement element) |
|||
{ |
|||
element.ValidateCommand += Validate; |
|||
element.ExecuteCommand += Delete; |
|||
} |
|||
|
|||
private bool Validate(CanvasElement element, Event e, Canvas2D parent) |
|||
{ |
|||
if (e.type == EventType.Used) |
|||
return false; |
|||
|
|||
if (e.commandName != "Delete" && e.commandName != "SoftDelete") |
|||
return false; |
|||
|
|||
e.Use(); |
|||
return true; |
|||
} |
|||
|
|||
private bool Delete(CanvasElement element, Event e, Canvas2D parent) |
|||
{ |
|||
if (e.type == EventType.Used) |
|||
return false; |
|||
|
|||
if (e.commandName != "Delete" && e.commandName != "SoftDelete") |
|||
return false; |
|||
|
|||
|
|||
if (m_DeletionCallback != null) |
|||
{ |
|||
m_DeletionCallback(parent.selection); |
|||
parent.ReloadData(); |
|||
parent.Repaint(); |
|||
} |
|||
e.Use(); |
|||
return true; |
|||
} |
|||
} |
|||
} |
|||
*/ |
|
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using UnityEditor.Experimental; |
|||
using UnityEditor.Experimental.Graph; |
|||
using UnityEngine; |
|||
using UnityEngine.Graphing; |
|||
using UnityEngine.MaterialGraph; |
|||
|
|||
namespace UnityEditor.Graphing.Drawing |
|||
{ |
|||
public class DrawableNode : CanvasElement |
|||
{ |
|||
private readonly GraphDataSource m_Data; |
|||
|
|||
private readonly Rect m_CustomUiRect; |
|||
public readonly INode m_Node; |
|||
private readonly ICustomNodeUi m_Ui; |
|||
|
|||
private const int kDefaultWidth = 200; |
|||
|
|||
public DrawableNode(INode node, ICustomNodeUi ui, GraphDataSource data) |
|||
{ |
|||
var drawData = node.drawState; |
|||
translation = drawData.position.min; |
|||
var width = ui != null ? ui.GetNodeWidth() : kDefaultWidth; |
|||
scale = new Vector2(width, width); |
|||
|
|||
m_Node = node; |
|||
m_Ui = ui; |
|||
m_Data = data; |
|||
|
|||
const float yStart = 10.0f; |
|||
var vector3 = new Vector3(5.0f, yStart, 0.0f); |
|||
Vector3 pos = vector3; |
|||
|
|||
// input slots
|
|||
foreach (var slot in node.GetInputSlots<ISlot>().OrderBy(x => x.priority)) |
|||
{ |
|||
pos.y += 22; |
|||
AddChild(new NodeAnchor(pos, typeof(Vector4), node, slot, data, Direction.Input)); |
|||
} |
|||
var inputYMax = pos.y + 22; |
|||
|
|||
// output port
|
|||
pos.x = width; |
|||
pos.y = yStart; |
|||
bool first = true; |
|||
foreach (var slot in node.GetOutputSlots<ISlot>().OrderBy(x => x.priority).ThenBy(x => x.id)) |
|||
{ |
|||
var edges = node.owner.GetEdges(node.GetSlotReference(slot.id)); |
|||
// don't show empty output slots in collapsed mode
|
|||
if (!node.drawState.expanded && !edges.Any() && !first) |
|||
continue; |
|||
|
|||
pos.y += 22; |
|||
AddChild(new NodeAnchor(pos, typeof(Vector4), node, slot, data, Direction.Output)); |
|||
first = false; |
|||
} |
|||
pos.y += 22; |
|||
|
|||
pos.y = Mathf.Max(pos.y, inputYMax); |
|||
|
|||
if (ui != null) |
|||
{ |
|||
var customUiHeight = ui.GetNodeUiHeight(width); |
|||
m_CustomUiRect = new Rect(10, pos.y, width - 20, customUiHeight); |
|||
pos.y += customUiHeight; |
|||
} |
|||
|
|||
scale = new Vector3(pos.x, pos.y + 10.0f, 0.0f); |
|||
|
|||
OnWidget += InvalidateUIIfNeedsTime; |
|||
|
|||
AddManipulator(new ImguiContainer()); |
|||
AddManipulator(new Draggable()); |
|||
} |
|||
|
|||
private bool InvalidateUIIfNeedsTime(CanvasElement element, Event e, Canvas2D parent) |
|||
{ |
|||
var childrenNodes = ListPool<INode>.Get(); |
|||
NodeUtils.DepthFirstCollectNodesFromNode(childrenNodes, m_Node); |
|||
if (childrenNodes.OfType<IRequiresTime>().Any()) |
|||
Invalidate(); |
|||
ListPool<INode>.Release(childrenNodes); |
|||
return true; |
|||
} |
|||
|
|||
public override void UpdateModel(UpdateType t) |
|||
{ |
|||
base.UpdateModel(t); |
|||
var drawState = m_Node.drawState; |
|||
var pos = drawState.position; |
|||
pos.min = translation; |
|||
drawState.position = pos; |
|||
m_Node.drawState = drawState; |
|||
} |
|||
|
|||
public override void Render(Rect parentRect, Canvas2D canvas) |
|||
{ |
|||
Color backgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.7f); |
|||
Color selectedColor = new Color(1.0f, 0.7f, 0.0f, 0.7f); |
|||
EditorGUI.DrawRect(new Rect(0, 0, scale.x, scale.y), m_Node.hasError ? Color.red : selected ? selectedColor : backgroundColor); |
|||
GUI.Label(new Rect(0, 0, scale.x, 26f), GUIContent.none, new GUIStyle("preToolbar")); |
|||
GUI.Label(new Rect(10, 2, scale.x - 20.0f, 16.0f), m_Node.name, EditorStyles.toolbarTextField); |
|||
var drawState = m_Node.drawState; |
|||
if (GUI.Button(new Rect(scale.x - 20f, 3f, 14f, 14f), drawState.expanded ? "-" : "+")) |
|||
{ |
|||
drawState.expanded = !drawState.expanded; |
|||
m_Node.drawState = drawState; |
|||
ParentCanvas().ReloadData(); |
|||
ParentCanvas().Repaint(); |
|||
return; |
|||
} |
|||
|
|||
if (m_Ui != null) |
|||
{ |
|||
var modificationType = m_Ui.Render(m_CustomUiRect); |
|||
|
|||
if (modificationType != GUIModificationType.None) |
|||
m_Data.MarkDirty(); |
|||
|
|||
if (modificationType == GUIModificationType.ModelChanged) |
|||
{ |
|||
m_Node.owner.ValidateGraph(); |
|||
ParentCanvas().Invalidate(); |
|||
ParentCanvas().ReloadData(); |
|||
ParentCanvas().Repaint(); |
|||
return; |
|||
} |
|||
|
|||
if (modificationType == GUIModificationType.DataChanged) |
|||
{ |
|||
ValidateDependentNodes(m_Node); |
|||
RepaintDependentNodes(m_Node); |
|||
} |
|||
else if (modificationType == GUIModificationType.Repaint) |
|||
{ |
|||
// if we were changed, we need to redraw all the
|
|||
// dependent nodes.
|
|||
RepaintDependentNodes(m_Node); |
|||
} |
|||
} |
|||
base.Render(parentRect, canvas); |
|||
} |
|||
|
|||
private void RepaintDependentNodes(INode theNode) |
|||
{ |
|||
var dependentNodes = new List<INode>(); |
|||
NodeUtils.CollectNodesNodeFeedsInto(dependentNodes, theNode); |
|||
foreach (var node in dependentNodes) |
|||
{ |
|||
foreach (var drawableNode in m_Data.lastGeneratedNodes.Where(x => x.m_Node == node)) |
|||
drawableNode.Invalidate(); |
|||
} |
|||
} |
|||
|
|||
private void ValidateDependentNodes(INode theNode) |
|||
{ |
|||
var dependentNodes = new List<INode>(); |
|||
NodeUtils.CollectNodesNodeFeedsInto(dependentNodes, theNode); |
|||
foreach (var node in dependentNodes) |
|||
node.ValidateNode(); |
|||
} |
|||
|
|||
/* |
|||
public static void OnGUI(List<CanvasElement> selection) |
|||
{ |
|||
var drawableMaterialNode = selection.OfType<DrawableMaterialNode>().FirstOrDefault(); |
|||
if (drawableMaterialNode != null && drawableMaterialNode.m_Node.OnGUI()) |
|||
{ |
|||
// if we were changed, we need to redraw all the
|
|||
// dependent nodes.
|
|||
RepaintDependentNodes(drawableMaterialNode.m_Node); |
|||
} |
|||
}*/ |
|||
|
|||
/* public virtual GUIModificationType NodeUI(Rect drawArea) |
|||
{ |
|||
return GUIModificationType.None; |
|||
} |
|||
|
|||
public virtual bool OnGUI() |
|||
{ |
|||
GUILayout.Label("MaterialSlot Defaults", EditorStyles.boldLabel); |
|||
var modified = false; |
|||
foreach (var slot in inputSlots) |
|||
{ |
|||
if (!owner.GetEdges(GetSlotReference(slot.name)).Any()) |
|||
modified |= DoSlotUI(this, slot); |
|||
} |
|||
|
|||
return modified; |
|||
} |
|||
|
|||
public static bool DoSlotUI(SerializableNode node, ISlot slot) |
|||
{ |
|||
GUILayout.BeginHorizontal( /*EditorStyles.inspectorBig*); |
|||
GUILayout.BeginVertical(); |
|||
GUILayout.BeginHorizontal(); |
|||
GUILayout.Label("MaterialSlot " + slot.name, EditorStyles.largeLabel); |
|||
GUILayout.FlexibleSpace(); |
|||
GUILayout.EndHorizontal(); |
|||
GUILayout.EndVertical(); |
|||
GUILayout.EndHorizontal(); |
|||
|
|||
//TODO: fix this
|
|||
return false; |
|||
//return slot.OnGUI();
|
|||
}*/ |
|||
} |
|||
} |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using UnityEngine; |
|||
using UnityEngine.Graphing; |
|||
using UnityEngine.MaterialGraph; |
|||
|
|||
/* |
|||
namespace UnityEditor.Graphing.Drawing |
|||
{ |
|||
public class DrawableNode : CanvasElement |
|||
{ |
|||
private readonly GraphDataSource m_Data; |
|||
|
|||
private readonly Rect m_CustomUiRect; |
|||
public readonly INode m_Node; |
|||
private readonly ICustomNodeUi m_Ui; |
|||
|
|||
private const int kDefaultWidth = 200; |
|||
|
|||
public DrawableNode(INode node, ICustomNodeUi ui, GraphDataSource data) |
|||
{ |
|||
var drawData = node.drawState; |
|||
translation = drawData.position.min; |
|||
var width = ui != null ? ui.GetNodeWidth() : kDefaultWidth; |
|||
scale = new Vector2(width, width); |
|||
|
|||
m_Node = node; |
|||
m_Ui = ui; |
|||
m_Data = data; |
|||
|
|||
const float yStart = 10.0f; |
|||
var vector3 = new Vector3(5.0f, yStart, 0.0f); |
|||
Vector3 pos = vector3; |
|||
|
|||
// input slots
|
|||
foreach (var slot in node.GetInputSlots<ISlot>().OrderBy(x => x.priority)) |
|||
{ |
|||
pos.y += 22; |
|||
AddChild(new NodeAnchor(pos, typeof(Vector4), node, slot, data, Direction.Input)); |
|||
} |
|||
var inputYMax = pos.y + 22; |
|||
|
|||
// output port
|
|||
pos.x = width; |
|||
pos.y = yStart; |
|||
bool first = true; |
|||
foreach (var slot in node.GetOutputSlots<ISlot>().OrderBy(x => x.priority).ThenBy(x => x.id)) |
|||
{ |
|||
var edges = node.owner.GetEdges(node.GetSlotReference(slot.id)); |
|||
// don't show empty output slots in collapsed mode
|
|||
if (!node.drawState.expanded && !edges.Any() && !first) |
|||
continue; |
|||
|
|||
pos.y += 22; |
|||
AddChild(new NodeAnchor(pos, typeof(Vector4), node, slot, data, Direction.Output)); |
|||
first = false; |
|||
} |
|||
pos.y += 22; |
|||
|
|||
pos.y = Mathf.Max(pos.y, inputYMax); |
|||
|
|||
if (ui != null) |
|||
{ |
|||
var customUiHeight = ui.GetNodeUiHeight(width); |
|||
m_CustomUiRect = new Rect(10, pos.y, width - 20, customUiHeight); |
|||
pos.y += customUiHeight; |
|||
} |
|||
|
|||
scale = new Vector3(pos.x, pos.y + 10.0f, 0.0f); |
|||
|
|||
OnWidget += InvalidateUIIfNeedsTime; |
|||
|
|||
AddManipulator(new ImguiContainer()); |
|||
AddManipulator(new Draggable()); |
|||
} |
|||
|
|||
private bool InvalidateUIIfNeedsTime(CanvasElement element, Event e, Canvas2D parent) |
|||
{ |
|||
var childrenNodes = ListPool<INode>.Get(); |
|||
NodeUtils.DepthFirstCollectNodesFromNode(childrenNodes, m_Node); |
|||
if (childrenNodes.OfType<IRequiresTime>().Any()) |
|||
Invalidate(); |
|||
ListPool<INode>.Release(childrenNodes); |
|||
return true; |
|||
} |
|||
|
|||
public override void UpdateModel(UpdateType t) |
|||
{ |
|||
base.UpdateModel(t); |
|||
var drawState = m_Node.drawState; |
|||
var pos = drawState.position; |
|||
pos.min = translation; |
|||
drawState.position = pos; |
|||
m_Node.drawState = drawState; |
|||
} |
|||
|
|||
public override void Render(Rect parentRect, Canvas2D canvas) |
|||
{ |
|||
Color backgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.7f); |
|||
Color selectedColor = new Color(1.0f, 0.7f, 0.0f, 0.7f); |
|||
EditorGUI.DrawRect(new Rect(0, 0, scale.x, scale.y), m_Node.hasError ? Color.red : selected ? selectedColor : backgroundColor); |
|||
GUI.Label(new Rect(0, 0, scale.x, 26f), GUIContent.none, new GUIStyle("preToolbar")); |
|||
GUI.Label(new Rect(10, 2, scale.x - 20.0f, 16.0f), m_Node.name, EditorStyles.toolbarTextField); |
|||
var drawState = m_Node.drawState; |
|||
if (GUI.Button(new Rect(scale.x - 20f, 3f, 14f, 14f), drawState.expanded ? "-" : "+")) |
|||
{ |
|||
drawState.expanded = !drawState.expanded; |
|||
m_Node.drawState = drawState; |
|||
ParentCanvas().ReloadData(); |
|||
ParentCanvas().Repaint(); |
|||
return; |
|||
} |
|||
|
|||
if (m_Ui != null) |
|||
{ |
|||
var modificationType = m_Ui.Render(m_CustomUiRect); |
|||
|
|||
if (modificationType != GUIModificationType.None) |
|||
m_Data.MarkDirty(); |
|||
|
|||
if (modificationType == GUIModificationType.ModelChanged) |
|||
{ |
|||
m_Node.owner.ValidateGraph(); |
|||
ParentCanvas().Invalidate(); |
|||
ParentCanvas().ReloadData(); |
|||
ParentCanvas().Repaint(); |
|||
return; |
|||
} |
|||
|
|||
if (modificationType == GUIModificationType.DataChanged) |
|||
{ |
|||
ValidateDependentNodes(m_Node); |
|||
RepaintDependentNodes(m_Node); |
|||
} |
|||
else if (modificationType == GUIModificationType.Repaint) |
|||
{ |
|||
// if we were changed, we need to redraw all the
|
|||
// dependent nodes.
|
|||
RepaintDependentNodes(m_Node); |
|||
} |
|||
} |
|||
base.Render(parentRect, canvas); |
|||
} |
|||
|
|||
private void RepaintDependentNodes(INode theNode) |
|||
{ |
|||
var dependentNodes = new List<INode>(); |
|||
NodeUtils.CollectNodesNodeFeedsInto(dependentNodes, theNode); |
|||
foreach (var node in dependentNodes) |
|||
{ |
|||
foreach (var drawableNode in m_Data.lastGeneratedNodes.Where(x => x.m_Node == node)) |
|||
drawableNode.Invalidate(); |
|||
} |
|||
} |
|||
|
|||
private void ValidateDependentNodes(INode theNode) |
|||
{ |
|||
var dependentNodes = new List<INode>(); |
|||
NodeUtils.CollectNodesNodeFeedsInto(dependentNodes, theNode); |
|||
foreach (var node in dependentNodes) |
|||
node.ValidateNode(); |
|||
} |
|||
|
|||
/* |
|||
public static void OnGUI(List<CanvasElement> selection) |
|||
{ |
|||
var drawableMaterialNode = selection.OfType<DrawableMaterialNode>().FirstOrDefault(); |
|||
if (drawableMaterialNode != null && drawableMaterialNode.m_Node.OnGUI()) |
|||
{ |
|||
// if we were changed, we need to redraw all the
|
|||
// dependent nodes.
|
|||
RepaintDependentNodes(drawableMaterialNode.m_Node); |
|||
} |
|||
}*/ |
|||
|
|||
/* public virtual GUIModificationType NodeUI(Rect drawArea) |
|||
{ |
|||
return GUIModificationType.None; |
|||
} |
|||
|
|||
public virtual bool OnGUI() |
|||
{ |
|||
GUILayout.Label("MaterialSlot Defaults", EditorStyles.boldLabel); |
|||
var modified = false; |
|||
foreach (var slot in inputSlots) |
|||
{ |
|||
if (!owner.GetEdges(GetSlotReference(slot.name)).Any()) |
|||
modified |= DoSlotUI(this, slot); |
|||
} |
|||
|
|||
return modified; |
|||
} |
|||
|
|||
public static bool DoSlotUI(SerializableNode node, ISlot slot) |
|||
{ |
|||
GUILayout.BeginHorizontal( /*EditorStyles.inspectorBig*); |
|||
GUILayout.BeginVertical(); |
|||
GUILayout.BeginHorizontal(); |
|||
GUILayout.Label("MaterialSlot " + slot.name, EditorStyles.largeLabel); |
|||
GUILayout.FlexibleSpace(); |
|||
GUILayout.EndHorizontal(); |
|||
GUILayout.EndVertical(); |
|||
GUILayout.EndHorizontal(); |
|||
|
|||
//TODO: fix this
|
|||
return false; |
|||
//return slot.OnGUI();
|
|||
}* |
|||
} |
|||
}*/ |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
using UnityEditor.Experimental; |
|||
using UnityEditor.Experimental.Graph; |
|||
using UnityEngine; |
|||
using UnityEngine.Graphing; |
|||
|
|||
namespace UnityEditor.Graphing.Drawing |
|||
{ |
|||
[AttributeUsage(AttributeTargets.Class)] |
|||
sealed class CustomNodeUI : Attribute |
|||
{ |
|||
private Type m_ModeToDrawFor; |
|||
|
|||
public CustomNodeUI(Type nodeToDrawFor) |
|||
{ |
|||
m_ModeToDrawFor = nodeToDrawFor; |
|||
} |
|||
|
|||
public Type nodeToDrawFor |
|||
{ |
|||
get { return m_ModeToDrawFor; } } |
|||
} |
|||
|
|||
public class GraphDataSource : ICanvasDataSource |
|||
{ |
|||
readonly List<DrawableNode> m_DrawableNodes = new List<DrawableNode>(); |
|||
|
|||
public IGraphAsset graphAsset { get; set; } |
|||
|
|||
public ICollection<DrawableNode> lastGeneratedNodes |
|||
{ |
|||
get { return m_DrawableNodes; } |
|||
} |
|||
|
|||
private static Type[] GetTypesFromAssembly(Assembly assembly) |
|||
{ |
|||
if (assembly == null) |
|||
return new Type[] {}; |
|||
try |
|||
{ |
|||
return assembly.GetTypes(); |
|||
} |
|||
catch (ReflectionTypeLoadException) |
|||
{ |
|||
return new Type[] {}; |
|||
} |
|||
} |
|||
|
|||
private static Dictionary<Type, Type> s_DrawerUI; |
|||
|
|||
private static Dictionary<Type, Type> drawerUI |
|||
{ |
|||
get |
|||
{ |
|||
if (s_DrawerUI == null) |
|||
{ |
|||
s_DrawerUI = new Dictionary<Type, Type>(); |
|||
var loadedTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => GetTypesFromAssembly(x)); |
|||
|
|||
foreach (var type in loadedTypes) |
|||
{ |
|||
var attribute = type.GetCustomAttributes(true).OfType<CustomNodeUI>().FirstOrDefault(); |
|||
if (attribute != null && typeof(ICustomNodeUi).IsAssignableFrom(type)) |
|||
s_DrawerUI.Add(attribute.nodeToDrawFor, type); |
|||
} |
|||
} |
|||
return s_DrawerUI; |
|||
} |
|||
} |
|||
|
|||
public CanvasElement[] FetchElements() |
|||
{ |
|||
m_DrawableNodes.Clear(); |
|||
var graph = graphAsset.graph; |
|||
Debug.LogFormat("Trying to convert: {0}", graphAsset.graph); |
|||
foreach (var node in graph.GetNodes<INode>()) |
|||
{ |
|||
var nodeType = node.GetType(); |
|||
Type draweruiType = null; |
|||
|
|||
while (draweruiType == null && nodeType != null) |
|||
{ |
|||
draweruiType = drawerUI.FirstOrDefault(x => x.Key == nodeType).Value; |
|||
nodeType = nodeType.BaseType; |
|||
} |
|||
|
|||
ICustomNodeUi customUI = null; |
|||
if (draweruiType != null) |
|||
{ |
|||
try |
|||
{ |
|||
customUI = Activator.CreateInstance(draweruiType) as ICustomNodeUi; |
|||
customUI.node = node; |
|||
} |
|||
catch (Exception e) |
|||
{ |
|||
Debug.LogWarningFormat("Could not construct instance of: {0} - {1}", draweruiType, e); |
|||
} |
|||
} |
|||
|
|||
// add the nodes
|
|||
m_DrawableNodes.Add(new DrawableNode(node, customUI, this)); |
|||
} |
|||
|
|||
// Add the edges now
|
|||
var drawableEdges = new List<DrawableEdge<NodeAnchor>>(); |
|||
foreach (var drawableMaterialNode in m_DrawableNodes) |
|||
{ |
|||
var baseNode = drawableMaterialNode.m_Node; |
|||
foreach (var slot in baseNode.GetOutputSlots<ISlot>()) |
|||
{ |
|||
var sourceAnchor = (NodeAnchor)drawableMaterialNode.Children().FirstOrDefault(x => x is NodeAnchor && ((NodeAnchor)x).m_Slot == slot); |
|||
|
|||
var edges = baseNode.owner.GetEdges(new SlotReference(baseNode.guid, slot.id)); |
|||
foreach (var edge in edges) |
|||
{ |
|||
var toNode = baseNode.owner.GetNodeFromGuid(edge.inputSlot.nodeGuid); |
|||
var toSlot = toNode.FindInputSlot<ISlot>(edge.inputSlot.slotId); |
|||
var targetNode = m_DrawableNodes.FirstOrDefault(x => x.m_Node == toNode); |
|||
var targetAnchor = (NodeAnchor)targetNode.Children().FirstOrDefault(x => x is NodeAnchor && ((NodeAnchor)x).m_Slot == toSlot); |
|||
drawableEdges.Add(new DrawableEdge<NodeAnchor>(edge, this, sourceAnchor, targetAnchor)); |
|||
} |
|||
} |
|||
} |
|||
|
|||
// Add proxy inputs for when edges are not connect
|
|||
var nullInputSlots = new List<NullInputProxy>(); |
|||
foreach (var drawableMaterialNode in m_DrawableNodes) |
|||
{ |
|||
var baseNode = drawableMaterialNode.m_Node; |
|||
// grab the input slots where there are no edges
|
|||
foreach (var slot in baseNode.GetInputsWithNoConnection()) |
|||
{ |
|||
// if there is no anchor, continue
|
|||
// this can happen if we are in collapsed mode
|
|||
var sourceAnchor = (NodeAnchor)drawableMaterialNode.Children().FirstOrDefault(x => x is NodeAnchor && ((NodeAnchor)x).m_Slot == slot); |
|||
if (sourceAnchor == null) |
|||
continue; |
|||
|
|||
nullInputSlots.Add(new NullInputProxy(baseNode, slot, sourceAnchor)); |
|||
} |
|||
} |
|||
var toReturn = new List<CanvasElement>(); |
|||
toReturn.AddRange(m_DrawableNodes.Select(x => (CanvasElement)x)); |
|||
toReturn.AddRange(drawableEdges.Select(x => (CanvasElement)x)); |
|||
toReturn.AddRange(nullInputSlots.Select(x => (CanvasElement)x)); |
|||
|
|||
//toReturn.Add(new FloatingPreview(new Rect(Screen.width - 300, Screen.height - 300, 300, 300), pixelGraph.nodes.FirstOrDefault(x => x is PixelShaderNode)));
|
|||
|
|||
Debug.LogFormat("Returning {0} nodes", m_DrawableNodes.Count); |
|||
Debug.LogFormat("Returning {0} drawableEdges", drawableEdges.Count); |
|||
Debug.LogFormat("Returning {0} nullInputSlots", nullInputSlots.Count); |
|||
return toReturn.ToArray(); |
|||
} |
|||
|
|||
public void DeleteElement(CanvasElement e) |
|||
{ |
|||
// do nothing here, we want to use delete elements.
|
|||
// delete elements ensures that edges are deleted before nodes.
|
|||
} |
|||
|
|||
public void DeleteElements(List<CanvasElement> elements) |
|||
{ |
|||
var graph = graphAsset.graph; |
|||
var toRemoveEdge = new List<IEdge>(); |
|||
// delete selected edges first
|
|||
foreach (var e in elements.OfType<Edge<NodeAnchor>>()) |
|||
{ |
|||
//find the edge
|
|||
var edge = graph.edges.FirstOrDefault(x => graph.GetNodeFromGuid(x.outputSlot.nodeGuid).FindOutputSlot<ISlot>(x.outputSlot.slotId) == e.Left.m_Slot |
|||
&& graph.GetNodeFromGuid(x.inputSlot.nodeGuid).FindInputSlot<ISlot>(x.inputSlot.slotId) == e.Right.m_Slot); |
|||
|
|||
toRemoveEdge.Add(edge); |
|||
} |
|||
|
|||
var toRemoveNode = new List<INode>(); |
|||
// now delete the nodes
|
|||
foreach (var e in elements.OfType<DrawableNode>()) |
|||
{ |
|||
if (!e.m_Node.canDeleteNode) |
|||
continue; |
|||
toRemoveNode.Add(e.m_Node); |
|||
} |
|||
graph.RemoveElements(toRemoveNode, toRemoveEdge); |
|||
MarkDirty(); |
|||
} |
|||
|
|||
public void Connect(NodeAnchor a, NodeAnchor b) |
|||
{ |
|||
var graph = graphAsset.graph; |
|||
graph.Connect(a.m_Node.GetSlotReference(a.m_Slot.id), b.m_Node.GetSlotReference(b.m_Slot.id)); |
|||
MarkDirty(); |
|||
} |
|||
|
|||
public void Addnode(INode node) |
|||
{ |
|||
var graph = graphAsset.graph; |
|||
graph.AddNode(node); |
|||
MarkDirty(); |
|||
} |
|||
|
|||
public void MarkDirty() |
|||
{ |
|||
EditorUtility.SetDirty(graphAsset.GetScriptableObject()); |
|||
} |
|||
|
|||
/* |
|||
} |
|||
|
|||
public class FloatingPreview : CanvasElement |
|||
{ |
|||
private AbstractMaterialNode m_Node; |
|||
|
|||
public FloatingPreview(Rect position, AbstractMaterialNode node) |
|||
{ |
|||
m_Node = node as AbstractMaterialNode; |
|||
m_Translation = new Vector2(position.x, position.y); |
|||
m_Scale = new Vector3(position.width, position.height, 1); |
|||
m_Caps |= Capabilities.Floating | Capabilities.Unselectable; |
|||
} |
|||
|
|||
public override void Render(Rect parentRect, Canvas2D canvas) |
|||
{ |
|||
var drawArea = new Rect(0, 0, scale.x, scale.y); |
|||
Color backgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.7f); |
|||
EditorGUI.DrawRect(drawArea, backgroundColor); |
|||
|
|||
drawArea.width -= 10; |
|||
drawArea.height -= 10; |
|||
drawArea.x += 5; |
|||
drawArea.y += 5; |
|||
|
|||
GL.sRGBWrite = (QualitySettings.activeColorSpace == ColorSpace.Linear); |
|||
GUI.DrawTexture(drawArea, m_Node.RenderPreview(new Rect(0, 0, drawArea.width, drawArea.height)), ScaleMode.StretchToFill, false); |
|||
GL.sRGBWrite = false; |
|||
|
|||
Invalidate(); |
|||
canvas.Repaint(); |
|||
}*/ |
|||
} |
|||
} |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
using UnityEngine; |
|||
using UnityEngine.Graphing; |
|||
|
|||
namespace UnityEditor.Graphing.Drawing |
|||
{ |
|||
[AttributeUsage(AttributeTargets.Class)] |
|||
sealed class CustomNodeUI : Attribute |
|||
{ |
|||
private Type m_ModeToDrawFor; |
|||
|
|||
public CustomNodeUI(Type nodeToDrawFor) |
|||
{ |
|||
m_ModeToDrawFor = nodeToDrawFor; |
|||
} |
|||
|
|||
public Type nodeToDrawFor |
|||
{ |
|||
get { return m_ModeToDrawFor; } } |
|||
} |
|||
/* |
|||
public class GraphDataSource : ICanvasDataSource |
|||
{ |
|||
readonly List<DrawableNode> m_DrawableNodes = new List<DrawableNode>(); |
|||
|
|||
public IGraphAsset graphAsset { get; set; } |
|||
|
|||
public ICollection<DrawableNode> lastGeneratedNodes |
|||
{ |
|||
get { return m_DrawableNodes; } |
|||
} |
|||
|
|||
private static Type[] GetTypesFromAssembly(Assembly assembly) |
|||
{ |
|||
if (assembly == null) |
|||
return new Type[] {}; |
|||
try |
|||
{ |
|||
return assembly.GetTypes(); |
|||
} |
|||
catch (ReflectionTypeLoadException) |
|||
{ |
|||
return new Type[] {}; |
|||
} |
|||
} |
|||
|
|||
private static Dictionary<Type, Type> s_DrawerUI; |
|||
|
|||
private static Dictionary<Type, Type> drawerUI |
|||
{ |
|||
get |
|||
{ |
|||
if (s_DrawerUI == null) |
|||
{ |
|||
s_DrawerUI = new Dictionary<Type, Type>(); |
|||
var loadedTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => GetTypesFromAssembly(x)); |
|||
|
|||
foreach (var type in loadedTypes) |
|||
{ |
|||
var attribute = type.GetCustomAttributes(true).OfType<CustomNodeUI>().FirstOrDefault(); |
|||
if (attribute != null && typeof(ICustomNodeUi).IsAssignableFrom(type)) |
|||
s_DrawerUI.Add(attribute.nodeToDrawFor, type); |
|||
} |
|||
} |
|||
return s_DrawerUI; |
|||
} |
|||
} |
|||
|
|||
public CanvasElement[] FetchElements() |
|||
{ |
|||
m_DrawableNodes.Clear(); |
|||
var graph = graphAsset.graph; |
|||
Debug.LogFormat("Trying to convert: {0}", graphAsset.graph); |
|||
foreach (var node in graph.GetNodes<INode>()) |
|||
{ |
|||
var nodeType = node.GetType(); |
|||
Type draweruiType = null; |
|||
|
|||
while (draweruiType == null && nodeType != null) |
|||
{ |
|||
draweruiType = drawerUI.FirstOrDefault(x => x.Key == nodeType).Value; |
|||
nodeType = nodeType.BaseType; |
|||
} |
|||
|
|||
ICustomNodeUi customUI = null; |
|||
if (draweruiType != null) |
|||
{ |
|||
try |
|||
{ |
|||
customUI = Activator.CreateInstance(draweruiType) as ICustomNodeUi; |
|||
customUI.node = node; |
|||
} |
|||
catch (Exception e) |
|||
{ |
|||
Debug.LogWarningFormat("Could not construct instance of: {0} - {1}", draweruiType, e); |
|||
} |
|||
} |
|||
|
|||
// add the nodes
|
|||
m_DrawableNodes.Add(new DrawableNode(node, customUI, this)); |
|||
} |
|||
|
|||
// Add the edges now
|
|||
var drawableEdges = new List<DrawableEdge<NodeAnchor>>(); |
|||
foreach (var drawableMaterialNode in m_DrawableNodes) |
|||
{ |
|||
var baseNode = drawableMaterialNode.m_Node; |
|||
foreach (var slot in baseNode.GetOutputSlots<ISlot>()) |
|||
{ |
|||
var sourceAnchor = (NodeAnchor)drawableMaterialNode.Children().FirstOrDefault(x => x is NodeAnchor && ((NodeAnchor)x).m_Slot == slot); |
|||
|
|||
var edges = baseNode.owner.GetEdges(new SlotReference(baseNode.guid, slot.id)); |
|||
foreach (var edge in edges) |
|||
{ |
|||
var toNode = baseNode.owner.GetNodeFromGuid(edge.inputSlot.nodeGuid); |
|||
var toSlot = toNode.FindInputSlot<ISlot>(edge.inputSlot.slotId); |
|||
var targetNode = m_DrawableNodes.FirstOrDefault(x => x.m_Node == toNode); |
|||
var targetAnchor = (NodeAnchor)targetNode.Children().FirstOrDefault(x => x is NodeAnchor && ((NodeAnchor)x).m_Slot == toSlot); |
|||
drawableEdges.Add(new DrawableEdge<NodeAnchor>(edge, this, sourceAnchor, targetAnchor)); |
|||
} |
|||
} |
|||
} |
|||
|
|||
// Add proxy inputs for when edges are not connect
|
|||
var nullInputSlots = new List<NullInputProxy>(); |
|||
foreach (var drawableMaterialNode in m_DrawableNodes) |
|||
{ |
|||
var baseNode = drawableMaterialNode.m_Node; |
|||
// grab the input slots where there are no edges
|
|||
foreach (var slot in baseNode.GetInputsWithNoConnection()) |
|||
{ |
|||
// if there is no anchor, continue
|
|||
// this can happen if we are in collapsed mode
|
|||
var sourceAnchor = (NodeAnchor)drawableMaterialNode.Children().FirstOrDefault(x => x is NodeAnchor && ((NodeAnchor)x).m_Slot == slot); |
|||
if (sourceAnchor == null) |
|||
continue; |
|||
|
|||
nullInputSlots.Add(new NullInputProxy(baseNode, slot, sourceAnchor)); |
|||
} |
|||
} |
|||
var toReturn = new List<CanvasElement>(); |
|||
toReturn.AddRange(m_DrawableNodes.Select(x => (CanvasElement)x)); |
|||
toReturn.AddRange(drawableEdges.Select(x => (CanvasElement)x)); |
|||
toReturn.AddRange(nullInputSlots.Select(x => (CanvasElement)x)); |
|||
|
|||
//toReturn.Add(new FloatingPreview(new Rect(Screen.width - 300, Screen.height - 300, 300, 300), pixelGraph.nodes.FirstOrDefault(x => x is PixelShaderNode)));
|
|||
|
|||
Debug.LogFormat("Returning {0} nodes", m_DrawableNodes.Count); |
|||
Debug.LogFormat("Returning {0} drawableEdges", drawableEdges.Count); |
|||
Debug.LogFormat("Returning {0} nullInputSlots", nullInputSlots.Count); |
|||
return toReturn.ToArray(); |
|||
} |
|||
|
|||
public void DeleteElement(CanvasElement e) |
|||
{ |
|||
// do nothing here, we want to use delete elements.
|
|||
// delete elements ensures that edges are deleted before nodes.
|
|||
} |
|||
|
|||
public void DeleteElements(List<CanvasElement> elements) |
|||
{ |
|||
var graph = graphAsset.graph; |
|||
var toRemoveEdge = new List<IEdge>(); |
|||
// delete selected edges first
|
|||
foreach (var e in elements.OfType<Edge<NodeAnchor>>()) |
|||
{ |
|||
//find the edge
|
|||
var edge = graph.edges.FirstOrDefault(x => graph.GetNodeFromGuid(x.outputSlot.nodeGuid).FindOutputSlot<ISlot>(x.outputSlot.slotId) == e.Left.m_Slot |
|||
&& graph.GetNodeFromGuid(x.inputSlot.nodeGuid).FindInputSlot<ISlot>(x.inputSlot.slotId) == e.Right.m_Slot); |
|||
|
|||
toRemoveEdge.Add(edge); |
|||
} |
|||
|
|||
var toRemoveNode = new List<INode>(); |
|||
// now delete the nodes
|
|||
foreach (var e in elements.OfType<DrawableNode>()) |
|||
{ |
|||
if (!e.m_Node.canDeleteNode) |
|||
continue; |
|||
toRemoveNode.Add(e.m_Node); |
|||
} |
|||
graph.RemoveElements(toRemoveNode, toRemoveEdge); |
|||
MarkDirty(); |
|||
} |
|||
|
|||
public void Connect(NodeAnchor a, NodeAnchor b) |
|||
{ |
|||
var graph = graphAsset.graph; |
|||
graph.Connect(a.m_Node.GetSlotReference(a.m_Slot.id), b.m_Node.GetSlotReference(b.m_Slot.id)); |
|||
MarkDirty(); |
|||
} |
|||
|
|||
public void Addnode(INode node) |
|||
{ |
|||
var graph = graphAsset.graph; |
|||
graph.AddNode(node); |
|||
MarkDirty(); |
|||
} |
|||
|
|||
public void MarkDirty() |
|||
{ |
|||
EditorUtility.SetDirty(graphAsset.GetScriptableObject()); |
|||
} |
|||
|
|||
/* |
|||
} |
|||
|
|||
public class FloatingPreview : CanvasElement |
|||
{ |
|||
private AbstractMaterialNode m_Node; |
|||
|
|||
public FloatingPreview(Rect position, AbstractMaterialNode node) |
|||
{ |
|||
m_Node = node as AbstractMaterialNode; |
|||
m_Translation = new Vector2(position.x, position.y); |
|||
m_Scale = new Vector3(position.width, position.height, 1); |
|||
m_Caps |= Capabilities.Floating | Capabilities.Unselectable; |
|||
} |
|||
|
|||
public override void Render(Rect parentRect, Canvas2D canvas) |
|||
{ |
|||
var drawArea = new Rect(0, 0, scale.x, scale.y); |
|||
Color backgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.7f); |
|||
EditorGUI.DrawRect(drawArea, backgroundColor); |
|||
|
|||
drawArea.width -= 10; |
|||
drawArea.height -= 10; |
|||
drawArea.x += 5; |
|||
drawArea.y += 5; |
|||
|
|||
GL.sRGBWrite = (QualitySettings.activeColorSpace == ColorSpace.Linear); |
|||
GUI.DrawTexture(drawArea, m_Node.RenderPreview(new Rect(0, 0, drawArea.width, drawArea.height)), ScaleMode.StretchToFill, false); |
|||
GL.sRGBWrite = false; |
|||
|
|||
Invalidate(); |
|||
canvas.Repaint(); |
|||
}* |
|||
}*/ |
|||
} |
|
|||
using UnityEditor.Experimental; |
|||
using UnityEngine; |
|||
using UnityEngine.Graphing; |
|||
|
|||
namespace UnityEditor.Graphing.Drawing |
|||
{ |
|||
public class NullInputProxy : CanvasElement |
|||
{ |
|||
private ISlot m_InputSlot; |
|||
private INode m_Node; |
|||
private NodeAnchor m_NodeAnchor; |
|||
|
|||
private const int kWidth = 180; |
|||
|
|||
public NullInputProxy(INode node, ISlot inputSlot, NodeAnchor nodeAnchor) |
|||
{ |
|||
m_InputSlot = inputSlot; |
|||
m_Node = node; |
|||
m_NodeAnchor = nodeAnchor; |
|||
|
|||
var size = m_NodeAnchor.scale; |
|||
size.x = kWidth; |
|||
scale = size; |
|||
|
|||
nodeAnchor.AddDependency(this); |
|||
UpdateModel(UpdateType.Update); |
|||
|
|||
var position = m_NodeAnchor.canvasBoundingRect.min; |
|||
position.x -= kWidth; |
|||
translation = position; |
|||
AddManipulator(new ImguiContainer()); |
|||
} |
|||
|
|||
public override void Render(Rect parentRect, Canvas2D canvas) |
|||
{ |
|||
base.Render(parentRect, canvas); |
|||
|
|||
var size = m_NodeAnchor.scale; |
|||
size.x = kWidth; |
|||
scale = size; |
|||
|
|||
var position = m_NodeAnchor.canvasBoundingRect.min; |
|||
position.x -= kWidth; |
|||
translation = position; |
|||
|
|||
var rect = new Rect(0, 0, scale.x, scale.y); |
|||
EditorGUI.DrawRect(rect, new Color(0.0f, 0.0f, 0.0f, 0.7f)); |
|||
|
|||
//TODO: FIX
|
|||
/*var changed = m_Node.DrawSlotDefaultInput(rect, m_InputSlot); |
|||
if (changed) |
|||
DrawableMaterialNode.RepaintDependentNodes(m_Node);*/ |
|||
} |
|||
|
|||
public override void UpdateModel(UpdateType t) |
|||
{ |
|||
var size = m_NodeAnchor.scale; |
|||
size.x = kWidth; |
|||
scale = size; |
|||
|
|||
var position = m_NodeAnchor.canvasBoundingRect.min; |
|||
position.x -= kWidth; |
|||
translation = position; |
|||
|
|||
base.UpdateModel(t); |
|||
} |
|||
} |
|||
} |
|||
using UnityEngine; |
|||
using UnityEngine.Graphing; |
|||
/* |
|||
namespace UnityEditor.Graphing.Drawing |
|||
{ |
|||
public class NullInputProxy : CanvasElement |
|||
{ |
|||
private ISlot m_InputSlot; |
|||
private INode m_Node; |
|||
private NodeAnchor m_NodeAnchor; |
|||
|
|||
private const int kWidth = 180; |
|||
|
|||
public NullInputProxy(INode node, ISlot inputSlot, NodeAnchor nodeAnchor) |
|||
{ |
|||
m_InputSlot = inputSlot; |
|||
m_Node = node; |
|||
m_NodeAnchor = nodeAnchor; |
|||
|
|||
var size = m_NodeAnchor.scale; |
|||
size.x = kWidth; |
|||
scale = size; |
|||
|
|||
nodeAnchor.AddDependency(this); |
|||
UpdateModel(UpdateType.Update); |
|||
|
|||
var position = m_NodeAnchor.canvasBoundingRect.min; |
|||
position.x -= kWidth; |
|||
translation = position; |
|||
AddManipulator(new ImguiContainer()); |
|||
} |
|||
|
|||
public override void Render(Rect parentRect, Canvas2D canvas) |
|||
{ |
|||
base.Render(parentRect, canvas); |
|||
|
|||
var size = m_NodeAnchor.scale; |
|||
size.x = kWidth; |
|||
scale = size; |
|||
|
|||
var position = m_NodeAnchor.canvasBoundingRect.min; |
|||
position.x -= kWidth; |
|||
translation = position; |
|||
|
|||
var rect = new Rect(0, 0, scale.x, scale.y); |
|||
EditorGUI.DrawRect(rect, new Color(0.0f, 0.0f, 0.0f, 0.7f)); |
|||
|
|||
//TODO: FIX
|
|||
/*var changed = m_Node.DrawSlotDefaultInput(rect, m_InputSlot); |
|||
if (changed) |
|||
DrawableMaterialNode.RepaintDependentNodes(m_Node);* |
|||
} |
|||
|
|||
public override void UpdateModel(UpdateType t) |
|||
{ |
|||
var size = m_NodeAnchor.scale; |
|||
size.x = kWidth; |
|||
scale = size; |
|||
|
|||
var position = m_NodeAnchor.canvasBoundingRect.min; |
|||
position.x -= kWidth; |
|||
translation = position; |
|||
|
|||
base.UpdateModel(t); |
|||
} |
|||
|
|||
}*/ |
|
|||
fileFormatVersion: 2 |
|||
guid: 27057ec4cbcfc30409186b9319c5f8c8 |
|||
timeCreated: 1445503903 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: e1fc1c5ab58718146be7e985ba8d2c8e |
|||
timeCreated: 1445503903 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UnityEditor.Experimental.Graph; |
|||
using UnityEngine; |
|||
|
|||
namespace UnityEditor.Graphing.Drawing |
|||
{ |
|||
internal class PortSource<T> |
|||
{ |
|||
} |
|||
|
|||
internal static class MyNodeAdapters |
|||
{ |
|||
internal static bool Adapt(this NodeAdapter value, PortSource<int> a, PortSource<int> b) |
|||
{ |
|||
// run adapt code for int to int connections
|
|||
return true; |
|||
} |
|||
|
|||
internal static bool Adapt(this NodeAdapter value, PortSource<float> a, PortSource<float> b) |
|||
{ |
|||
// run adapt code for float to float connections
|
|||
return true; |
|||
} |
|||
|
|||
internal static bool Adapt(this NodeAdapter value, PortSource<int> a, PortSource<float> b) |
|||
{ |
|||
// run adapt code for int to float connections, perhaps by insertion a conversion node
|
|||
return true; |
|||
} |
|||
|
|||
internal static bool Adapt(this NodeAdapter value, PortSource<Vector3> a, PortSource<Vector3> b) |
|||
{ |
|||
// run adapt code for vec3 to vec3 connections
|
|||
return true; |
|||
} |
|||
|
|||
internal static bool Adapt(this NodeAdapter value, PortSource<Color> a, PortSource<Color> b) |
|||
{ |
|||
// run adapt code for Color to Color connections
|
|||
return true; |
|||
} |
|||
|
|||
internal static bool Adapt(this NodeAdapter value, PortSource<Vector3> a, PortSource<Color> b) |
|||
{ |
|||
// run adapt code for vec3 to Color connections
|
|||
return true; |
|||
} |
|||
|
|||
internal static bool Adapt(this NodeAdapter value, PortSource<Vector4> a, PortSource<Vector4> b) |
|||
{ |
|||
// run adapt code for vec3 to Color connections
|
|||
return true; |
|||
} |
|||
|
|||
internal static bool Adapt(this NodeAdapter value, PortSource<Color> a, PortSource<Vector3> b) |
|||
{ |
|||
// run adapt code for Color to vec3 connections
|
|||
return true; |
|||
} |
|||
} |
|||
} |
|
|||
using System; |
|||
using UnityEditor.Experimental; |
|||
using UnityEditor.Experimental.Graph; |
|||
using UnityEngine; |
|||
using UnityEngine.Graphing; |
|||
|
|||
namespace UnityEditor.Graphing.Drawing |
|||
{ |
|||
public class NodeAnchor : CanvasElement, IConnect |
|||
{ |
|||
protected Type m_Type; |
|||
protected object m_Source; |
|||
protected Direction m_Direction; |
|||
private GraphDataSource m_Data; |
|||
public ISlot m_Slot; |
|||
public INode m_Node; |
|||
|
|||
public NodeAnchor(Vector3 position, Type type, INode node, ISlot slot, GraphDataSource data, Direction direction) |
|||
{ |
|||
m_Type = type; |
|||
scale = new Vector3(15.0f, 15.0f, 1.0f); |
|||
translation = position; |
|||
AddManipulator(new EdgeConnector<NodeAnchor>()); |
|||
m_Direction = direction; |
|||
|
|||
Type genericClass = typeof(PortSource<>); |
|||
Type constructedClass = genericClass.MakeGenericType(type); |
|||
m_Source = Activator.CreateInstance(constructedClass); |
|||
m_Data = data; |
|||
m_Node = node; |
|||
m_Slot = slot; |
|||
} |
|||
|
|||
public Orientation GetOrientation() |
|||
{ |
|||
return Orientation.Horizontal; |
|||
} |
|||
|
|||
public override void Render(Rect parentRect, Canvas2D canvas) |
|||
{ |
|||
var anchorColor = Color.yellow; |
|||
anchorColor.a = 0.7f; |
|||
base.Render(parentRect, canvas); |
|||
EditorGUI.DrawRect(new Rect(translation.x, translation.y, scale.x, scale.y), anchorColor); |
|||
string text = m_Slot.displayName; |
|||
Rect labelRect; |
|||
if (m_Direction == Direction.Input) |
|||
{ |
|||
labelRect = new Rect(translation.x + scale.x + 10.0f, translation.y, parentRect.width, 20.0f); |
|||
} |
|||
else |
|||
{ |
|||
Vector2 sizeOfText = GUIStyle.none.CalcSize(new GUIContent(text)); |
|||
labelRect = new Rect(translation.x - sizeOfText.x - 4.0f, translation.y, sizeOfText.x + 4.0f, sizeOfText.y + 4.0f); |
|||
} |
|||
GUI.Label(labelRect, text); |
|||
} |
|||
|
|||
// IConnect
|
|||
public Direction GetDirection() |
|||
{ |
|||
return m_Direction; |
|||
} |
|||
|
|||
public void Highlight(bool highlighted) |
|||
{ |
|||
} |
|||
|
|||
public void RenderOverlay(Canvas2D canvas) |
|||
{ |
|||
Rect thisRect = canvasBoundingRect; |
|||
thisRect.x += 4; |
|||
thisRect.y += 4; |
|||
thisRect.width -= 8; |
|||
thisRect.height -= 8; |
|||
thisRect = canvas.CanvasToScreen(thisRect); |
|||
EditorGUI.DrawRect(thisRect, new Color(0.0f, 0.0f, 0.8f)); |
|||
} |
|||
|
|||
public object Source() |
|||
{ |
|||
return m_Source; |
|||
} |
|||
|
|||
public Vector3 ConnectPosition() |
|||
{ |
|||
return canvasBoundingRect.center; |
|||
} |
|||
|
|||
public void OnConnect(IConnect other) |
|||
{ |
|||
if (other == null) |
|||
return; |
|||
|
|||
NodeAnchor otherAnchor = other as NodeAnchor; |
|||
m_Data.Connect(this, otherAnchor); |
|||
|
|||
ParentCanvas().ReloadData(); |
|||
} |
|||
}; |
|||
} |
|
|||
using UnityEditor.Experimental.Graph; |
|||
using UnityEditor.Experimental; |
|||
using UnityEngine.Graphing; |
|||
|
|||
namespace UnityEditor.Graphing.Drawing |
|||
{ |
|||
internal class DrawableEdge<T> : Edge<T> where T : CanvasElement, IConnect |
|||
{ |
|||
public readonly IEdge m_Edge; |
|||
|
|||
public DrawableEdge(IEdge edge, ICanvasDataSource data, T left, T right) : base(data, left, right) |
|||
{ |
|||
m_Edge = edge; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 3af037115cde5d64383adcc76ce18b5b |
|||
timeCreated: 1468575128 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UnityEditor.Experimental; |
|||
using UnityEngine; |
|||
|
|||
namespace UnityEditor.MaterialGraph |
|||
{ |
|||
public class SimpleBox : CanvasElement |
|||
{ |
|||
protected string m_Title = "simpleBox"; |
|||
protected bool m_Expanded = true; |
|||
|
|||
protected bool error = false; |
|||
|
|||
public SimpleBox(Vector2 position, float width) |
|||
{ |
|||
translation = position; |
|||
scale = new Vector2(width, width); |
|||
} |
|||
|
|||
public override void Render(Rect parentRect, Canvas2D canvas) |
|||
{ |
|||
Color backgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.7f); |
|||
Color selectedColor = new Color(1.0f, 0.7f, 0.0f, 0.7f); |
|||
EditorGUI.DrawRect(new Rect(0, 0, scale.x, scale.y), error ? Color.red : selected ? selectedColor : backgroundColor); |
|||
GUI.Label(new Rect(0, 0, scale.x, 26f), GUIContent.none, new GUIStyle("preToolbar")); |
|||
GUI.Label(new Rect(10, 2, scale.x - 20.0f, 16.0f), m_Title, EditorStyles.toolbarTextField); |
|||
|
|||
base.Render(parentRect, canvas); |
|||
} |
|||
} |
|||
|
|||
public class MoveableBox : SimpleBox |
|||
{ |
|||
public MoveableBox(Vector2 position, float width) |
|||
: base(position, width) |
|||
{ |
|||
m_Title = "Drag me!"; |
|||
AddManipulator(new Draggable()); |
|||
} |
|||
} |
|||
|
|||
internal class ResizableBox : SimpleBox |
|||
{ |
|||
public ResizableBox(Vector2 position, float width) |
|||
: base(position, width) |
|||
{ |
|||
m_Title = "Resize me!"; |
|||
AddManipulator(new Resizable()); |
|||
AddManipulator(new Draggable()); |
|||
} |
|||
} |
|||
|
|||
internal class WWWImageBox : SimpleBox |
|||
{ |
|||
private Texture2D m_WWWTexture = new Texture2D(4, 4, TextureFormat.DXT1, false); |
|||
private WWW www = null; |
|||
private float timeToNextPicture = 0.0f; |
|||
|
|||
public WWWImageBox(Vector2 position, float width) |
|||
: base(position, width) |
|||
{ |
|||
m_Title = "I cause repaints every frame!"; |
|||
AddManipulator(new Draggable()); |
|||
} |
|||
|
|||
public override void Render(Rect parentRect, Canvas2D canvas) |
|||
{ |
|||
if (www != null && www.isDone) |
|||
{ |
|||
www.LoadImageIntoTexture(m_WWWTexture); |
|||
www = null; |
|||
timeToNextPicture = 3.0f; |
|||
} |
|||
|
|||
timeToNextPicture -= Time.deltaTime; |
|||
if (timeToNextPicture < 0.0f) |
|||
{ |
|||
timeToNextPicture = 99999.0f; |
|||
www = new WWW("http://lorempixel.com/200/200"); |
|||
} |
|||
|
|||
base.Render(parentRect, canvas); |
|||
|
|||
GUI.DrawTexture(new Rect(0, 20, 200, 200), m_WWWTexture); |
|||
Invalidate(); |
|||
canvas.Repaint(); |
|||
} |
|||
} |
|||
|
|||
internal class IMGUIControls : SimpleBox |
|||
{ |
|||
private string m_Text1 = "this is a text field"; |
|||
private string m_Text2 = "this is a text field"; |
|||
private bool m_Toggle = true; |
|||
private Texture2D m_aTexture = null; |
|||
|
|||
public IMGUIControls(Vector2 position, float width) |
|||
: base(position, width) |
|||
{ |
|||
m_Caps = Capabilities.Unselectable; |
|||
|
|||
m_Title = "modal"; |
|||
AddManipulator(new Draggable()); |
|||
AddManipulator(new Resizable()); |
|||
AddManipulator(new ImguiContainer()); |
|||
} |
|||
|
|||
public override void Render(Rect parentRect, Canvas2D canvas) |
|||
{ |
|||
base.Render(parentRect, canvas); |
|||
|
|||
int currentY = 22; |
|||
|
|||
m_Text1 = GUI.TextField(new Rect(0, currentY, 80, 20), m_Text1); |
|||
currentY += 22; |
|||
|
|||
m_Toggle = GUI.Toggle(new Rect(0, currentY, 10, 10), m_Toggle, GUIContent.none); |
|||
currentY += 22; |
|||
|
|||
m_Text2 = GUI.TextField(new Rect(0, currentY, 80, 20), m_Text2); |
|||
currentY += 22; |
|||
|
|||
m_aTexture = EditorGUI.ObjectField(new Rect(0, currentY, 80, 100), m_aTexture, typeof(Texture2D), false) as Texture2D; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 3b7b58fd48ad82c49916e721dd90507a |
|||
timeCreated: 1445420224 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 1b5ae96a5eb352841afac01a561f9f20 |
|||
folderAsset: yes |
|||
timeCreated: 1453889577 |
|||
licenseType: Pro |
|||
DefaultImporter: |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue