Exemplo n.º 1
0
        internal static void InsertCopyPasteGraph(this MaterialGraphView graphView, CopyPasteGraph copyGraph)
        {
            if (copyGraph == null)
            {
                return;
            }

            using (var remappedNodesDisposable = ListPool <INode> .GetDisposable())
                using (var remappedEdgesDisposable = ListPool <IEdge> .GetDisposable())
                {
                    var remappedNodes = remappedNodesDisposable.value;
                    var remappedEdges = remappedEdgesDisposable.value;
                    graphView.graph.PasteGraph(copyGraph, remappedNodes, remappedEdges);

                    // Add new elements to selection
                    graphView.ClearSelection();
                    graphView.graphElements.ForEach(element =>
                    {
                        var edge = element as Edge;
                        if (edge != null && remappedEdges.Contains(edge.userData as IEdge))
                        {
                            graphView.AddToSelection(edge);
                        }

                        var nodeView = element as MaterialNodeView;
                        if (nodeView != null && remappedNodes.Contains(nodeView.node))
                        {
                            graphView.AddToSelection(nodeView);
                        }
                    });
                }
        }
Exemplo n.º 2
0
        internal static void InsertCopyPasteGraph(this MaterialGraphView graphView, CopyPasteGraph copyGraph)
        {
            if (copyGraph == null)
            {
                return;
            }

            // Make new properties from the copied graph
            foreach (AbstractShaderProperty property in copyGraph.properties)
            {
                string propertyName = graphView.graph.SanitizePropertyName(property.displayName);
                AbstractShaderProperty copiedProperty = property.Copy();
                copiedProperty.displayName = propertyName;
                graphView.graph.AddShaderProperty(copiedProperty);

                // Update the property nodes that depends on the copied node
                var dependentPropertyNodes = copyGraph.GetNodes <PropertyNode>().Where(x => x.propertyGuid == property.guid);
                foreach (var node in dependentPropertyNodes)
                {
                    node.owner        = graphView.graph;
                    node.propertyGuid = copiedProperty.guid;
                }
            }

            using (var remappedNodesDisposable = ListPool <AbstractMaterialNode> .GetDisposable())
            {
                using (var remappedEdgesDisposable = ListPool <IEdge> .GetDisposable())
                {
                    var remappedNodes = remappedNodesDisposable.value;
                    var remappedEdges = remappedEdgesDisposable.value;
                    graphView.graph.PasteGraph(copyGraph, remappedNodes, remappedEdges);

                    if (graphView.graph.assetGuid != copyGraph.sourceGraphGuid)
                    {
                        // Compute the mean of the copied nodes.
                        Vector2 centroid = Vector2.zero;
                        var     count    = 1;
                        foreach (var node in remappedNodes)
                        {
                            var position = node.drawState.position.position;
                            centroid = centroid + (position - centroid) / count;
                            ++count;
                        }

                        // Get the center of the current view
                        var viewCenter = graphView.contentViewContainer.WorldToLocal(graphView.layout.center);

                        foreach (var node in remappedNodes)
                        {
                            var drawState    = node.drawState;
                            var positionRect = drawState.position;
                            var position     = positionRect.position;
                            position += viewCenter - centroid;
                            positionRect.position = position;
                            drawState.position    = positionRect;
                            node.drawState        = drawState;
                        }
                    }

                    // Add new elements to selection
                    graphView.ClearSelection();
                    graphView.graphElements.ForEach(element =>
                    {
                        var edge = element as Edge;
                        if (edge != null && remappedEdges.Contains(edge.userData as IEdge))
                        {
                            graphView.AddToSelection(edge);
                        }

                        var nodeView = element as MaterialNodeView;
                        if (nodeView != null && remappedNodes.Contains(nodeView.node))
                        {
                            graphView.AddToSelection(nodeView);
                        }
                    });
                }
            }
        }
        internal static void InsertCopyPasteGraph(this MaterialGraphView graphView, CopyPasteGraph copyGraph)
        {
            if (copyGraph == null)
            {
                return;
            }

            // Keywords need to be tested against variant limit based on multiple factors
            bool keywordsDirty = false;

            // Make new inputs from the copied graph
            foreach (ShaderInput input in copyGraph.inputs)
            {
                ShaderInput copiedInput = input.Copy();
                graphView.graph.SanitizeGraphInputName(copiedInput);
                graphView.graph.SanitizeGraphInputReferenceName(copiedInput, input.overrideReferenceName);
                graphView.graph.AddGraphInput(copiedInput);

                switch (input)
                {
                case AbstractShaderProperty property:
                    // Update the property nodes that depends on the copied node
                    var dependentPropertyNodes = copyGraph.GetNodes <PropertyNode>().Where(x => x.propertyGuid == input.guid);
                    foreach (var node in dependentPropertyNodes)
                    {
                        node.owner        = graphView.graph;
                        node.propertyGuid = copiedInput.guid;
                    }
                    break;

                case ShaderKeyword shaderKeyword:
                    // Update the keyword nodes that depends on the copied node
                    var dependentKeywordNodes = copyGraph.GetNodes <KeywordNode>().Where(x => x.keywordGuid == input.guid);
                    foreach (var node in dependentKeywordNodes)
                    {
                        node.owner       = graphView.graph;
                        node.keywordGuid = copiedInput.guid;
                    }

                    // Pasting a new Keyword so need to test against variant limit
                    keywordsDirty = true;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            // Pasting a Sub Graph node that contains Keywords so need to test against variant limit
            foreach (SubGraphNode subGraphNode in copyGraph.GetNodes <SubGraphNode>())
            {
                if (subGraphNode.asset.keywords.Count > 0)
                {
                    keywordsDirty = true;
                }
            }

            // Test Keywords against variant limit
            if (keywordsDirty)
            {
                graphView.graph.OnKeywordChangedNoValidate();
            }

            using (var remappedNodesDisposable = ListPool <AbstractMaterialNode> .GetDisposable())
            {
                using (var remappedEdgesDisposable = ListPool <IEdge> .GetDisposable())
                {
                    var remappedNodes = remappedNodesDisposable.value;
                    var remappedEdges = remappedEdgesDisposable.value;
                    graphView.graph.PasteGraph(copyGraph, remappedNodes, remappedEdges);

                    if (graphView.graph.assetGuid != copyGraph.sourceGraphGuid)
                    {
                        // Compute the mean of the copied nodes.
                        Vector2 centroid = Vector2.zero;
                        var     count    = 1;
                        foreach (var node in remappedNodes)
                        {
                            var position = node.drawState.position.position;
                            centroid = centroid + (position - centroid) / count;
                            ++count;
                        }

                        // Get the center of the current view
                        var viewCenter = graphView.contentViewContainer.WorldToLocal(graphView.layout.center);

                        foreach (var node in remappedNodes)
                        {
                            var drawState    = node.drawState;
                            var positionRect = drawState.position;
                            var position     = positionRect.position;
                            position += viewCenter - centroid;
                            positionRect.position = position;
                            drawState.position    = positionRect;
                            node.drawState        = drawState;
                        }
                    }

                    // Add new elements to selection
                    graphView.ClearSelection();
                    graphView.graphElements.ForEach(element =>
                    {
                        if (element is Edge edge && remappedEdges.Contains(edge.userData as IEdge))
                        {
                            graphView.AddToSelection(edge);
                        }

                        if (element is IShaderNodeView nodeView && remappedNodes.Contains(nodeView.node))
                        {
                            graphView.AddToSelection((Node)nodeView);
                        }
                    });
                }
            }
        }
Exemplo n.º 4
0
 private void SetSelfSelected()
 {
     m_GraphView.ClearSelection();
     m_GraphView.AddToSelection(this);
 }