|
|
|
|
|
|
} |
|
|
|
|
|
|
|
foreach (var node in graph.GetNodes<INode>()) |
|
|
|
{ |
|
|
|
NodeAddedGraphChange change = new NodeAddedGraphChange(node); |
|
|
|
var nodeView = new MaterialNodeView(change.node as AbstractMaterialNode, m_PreviewSystem); |
|
|
|
nodeView.userData = change.node; |
|
|
|
change.node.onModified += OnNodeChanged; |
|
|
|
m_GraphView.AddElement(nodeView); |
|
|
|
} |
|
|
|
AddNode(node); |
|
|
|
|
|
|
|
{ |
|
|
|
var edge1 = new EdgeAddedGraphChange(edge).edge; |
|
|
|
|
|
|
|
var sourceNode = m_Graph.GetNodeFromGuid(edge1.outputSlot.nodeGuid); |
|
|
|
var sourceSlot = sourceNode.FindOutputSlot<ISlot>(edge1.outputSlot.slotId); |
|
|
|
|
|
|
|
var targetNode = m_Graph.GetNodeFromGuid(edge1.inputSlot.nodeGuid); |
|
|
|
var targetSlot = targetNode.FindInputSlot<ISlot>(edge1.inputSlot.slotId); |
|
|
|
|
|
|
|
var sourceNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == sourceNode); |
|
|
|
if (sourceNodeView != null) |
|
|
|
{ |
|
|
|
var sourceAnchor = sourceNodeView.outputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(sourceSlot)); |
|
|
|
|
|
|
|
var targetNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == targetNode); |
|
|
|
var targetAnchor = targetNodeView.inputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(targetSlot)); |
|
|
|
|
|
|
|
var edgeView = new Edge(); |
|
|
|
edgeView.userData = edge1; |
|
|
|
edgeView.output = sourceAnchor; |
|
|
|
edgeView.output.Connect(edgeView); |
|
|
|
edgeView.input = targetAnchor; |
|
|
|
edgeView.input.Connect(edgeView); |
|
|
|
m_GraphView.AddElement(edgeView); |
|
|
|
sourceNodeView.RefreshAnchors(); |
|
|
|
targetNodeView.RefreshAnchors(); |
|
|
|
} |
|
|
|
} |
|
|
|
AddEdge(edge); |
|
|
|
|
|
|
|
Add(content); |
|
|
|
} |
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
HashSet<MaterialNodeView> m_NodeViewHashSet = new HashSet<MaterialNodeView>(); |
|
|
|
|
|
|
|
public void HandleGraphChanges() |
|
|
|
{ |
|
|
|
foreach (var node in m_Graph.removedNodes) |
|
|
|
|
|
|
if (nodeView != null) |
|
|
|
{ |
|
|
|
nodeView.Dispose(); |
|
|
|
nodeView.userData = null; |
|
|
|
{ |
|
|
|
var nodeView = new MaterialNodeView(node as AbstractMaterialNode, m_PreviewSystem) { userData = node }; |
|
|
|
node.onModified += OnNodeChanged; |
|
|
|
m_GraphView.AddElement(nodeView); |
|
|
|
} |
|
|
|
AddNode(node); |
|
|
|
|
|
|
|
var nodesToUpdate = m_NodeViewHashSet; |
|
|
|
nodesToUpdate.Clear(); |
|
|
|
|
|
|
|
foreach (var edge in m_Graph.removedEdges) |
|
|
|
{ |
|
|
|
|
|
|
var nodeView = edgeView.input.node as MaterialNodeView; |
|
|
|
if (nodeView != null && nodeView.node != null) |
|
|
|
{ |
|
|
|
nodesToUpdate.Add(nodeView); |
|
|
|
} |
|
|
|
edgeView.output.Disconnect(edgeView); |
|
|
|
edgeView.input.Disconnect(edgeView); |
|
|
|
m_GraphView.RemoveElement(edgeView); |
|
|
|
|
|
|
foreach (var edge in m_Graph.addedEdges) |
|
|
|
{ |
|
|
|
var sourceNode = m_Graph.GetNodeFromGuid(edge.outputSlot.nodeGuid); |
|
|
|
var sourceSlot = sourceNode.FindOutputSlot<ISlot>(edge.outputSlot.slotId); |
|
|
|
var edgeView = AddEdge(edge); |
|
|
|
if (edgeView != null) |
|
|
|
nodesToUpdate.Add((MaterialNodeView)edgeView.input.node); |
|
|
|
} |
|
|
|
|
|
|
|
UpdateEdgeColors(nodesToUpdate); |
|
|
|
} |
|
|
|
|
|
|
|
void AddNode(INode node) |
|
|
|
{ |
|
|
|
var nodeView = new MaterialNodeView(node as AbstractMaterialNode, m_PreviewSystem) { userData = node }; |
|
|
|
node.onModified += OnNodeChanged; |
|
|
|
m_GraphView.AddElement(nodeView); |
|
|
|
} |
|
|
|
|
|
|
|
Edge AddEdge(IEdge edge) |
|
|
|
{ |
|
|
|
var sourceNode = m_Graph.GetNodeFromGuid(edge.outputSlot.nodeGuid); |
|
|
|
var sourceSlot = sourceNode.FindOutputSlot<MaterialSlot>(edge.outputSlot.slotId); |
|
|
|
|
|
|
|
var targetNode = m_Graph.GetNodeFromGuid(edge.inputSlot.nodeGuid); |
|
|
|
var targetSlot = targetNode.FindInputSlot<MaterialSlot>(edge.inputSlot.slotId); |
|
|
|
|
|
|
|
var sourceNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == sourceNode); |
|
|
|
if (sourceNodeView != null) |
|
|
|
{ |
|
|
|
var sourceAnchor = sourceNodeView.outputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(sourceSlot)); |
|
|
|
var targetNode = m_Graph.GetNodeFromGuid(edge.inputSlot.nodeGuid); |
|
|
|
var targetSlot = targetNode.FindInputSlot<ISlot>(edge.inputSlot.slotId); |
|
|
|
var targetNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == targetNode); |
|
|
|
var targetAnchor = targetNodeView.inputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(targetSlot)); |
|
|
|
var sourceNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == sourceNode); |
|
|
|
if (sourceNodeView != null) |
|
|
|
var edgeView = new Edge |
|
|
|
var sourceAnchor = sourceNodeView.outputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(sourceSlot)); |
|
|
|
userData = edge, |
|
|
|
output = sourceAnchor, |
|
|
|
input = targetAnchor |
|
|
|
}; |
|
|
|
edgeView.AddToClassList(sourceSlot.concreteValueType.ToString()); |
|
|
|
edgeView.output.Connect(edgeView); |
|
|
|
edgeView.input.Connect(edgeView); |
|
|
|
m_GraphView.AddElement(edgeView); |
|
|
|
sourceNodeView.RefreshAnchors(); |
|
|
|
targetNodeView.RefreshAnchors(); |
|
|
|
var targetNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == targetNode); |
|
|
|
var targetAnchor = targetNodeView.inputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(targetSlot)); |
|
|
|
return edgeView; |
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
Stack<MaterialNodeView> m_NodeStack = new Stack<MaterialNodeView>(); |
|
|
|
var edgeView = new Edge(); |
|
|
|
edgeView.userData = edge; |
|
|
|
edgeView.output = sourceAnchor; |
|
|
|
edgeView.output.Connect(edgeView); |
|
|
|
edgeView.input = targetAnchor; |
|
|
|
edgeView.input.Connect(edgeView); |
|
|
|
m_GraphView.AddElement(edgeView); |
|
|
|
sourceNodeView.RefreshAnchors(); |
|
|
|
targetNodeView.RefreshAnchors(); |
|
|
|
void UpdateEdgeColors(HashSet<MaterialNodeView> nodeViews) |
|
|
|
{ |
|
|
|
var nodeStack = m_NodeStack; |
|
|
|
nodeStack.Clear(); |
|
|
|
foreach (var nodeView in nodeViews) |
|
|
|
nodeStack.Push(nodeView); |
|
|
|
while (nodeStack.Any()) |
|
|
|
{ |
|
|
|
var nodeView = nodeStack.Pop(); |
|
|
|
nodeViews.Add(nodeView); |
|
|
|
foreach (var child in nodeView.outputContainer.Children()) |
|
|
|
{ |
|
|
|
var anchorView = child as NodeAnchor; |
|
|
|
if (anchorView == null) |
|
|
|
continue; |
|
|
|
var slot = (MaterialSlot)anchorView.userData; |
|
|
|
var className = slot.concreteValueType.ToString(); |
|
|
|
foreach (var connection in anchorView.connections) |
|
|
|
{ |
|
|
|
connection.ClearClassList(); |
|
|
|
connection.AddToClassList("edge"); |
|
|
|
connection.AddToClassList(className); |
|
|
|
var connectedNodeView = connection.input.node as MaterialNodeView; |
|
|
|
if (connectedNodeView != null && !nodeViews.Contains(connectedNodeView)) |
|
|
|
nodeStack.Push(connectedNodeView); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
{ |
|
|
|
foreach (var node in m_GraphView.Children().OfType<MaterialNodeView>()) |
|
|
|
node.Dispose(); |
|
|
|
m_GraphView = null; |
|
|
|
} |
|
|
|
if (m_GraphInspectorView != null) m_GraphInspectorView.Dispose(); |
|
|
|
if (previewSystem != null) |
|
|
|