public bool OnSelectEntry(SearchTreeEntry entry, SearchWindowContext context)
        {
            var nodeEntry = (NodeEntry)entry.userData;
            var node      = nodeEntry.node;

            var drawState           = node.drawState;
            var windowMousePosition = m_EditorWindow.GetRootVisualContainer().ChangeCoordinatesTo(m_EditorWindow.GetRootVisualContainer().parent, context.screenMousePosition - m_EditorWindow.position.position);
            var graphMousePosition  = m_GraphView.contentViewContainer.WorldToLocal(windowMousePosition);

            drawState.position = new Rect(graphMousePosition, Vector2.zero);
            node.drawState     = drawState;

            m_Graph.owner.RegisterCompleteObjectUndo("Add " + node.name);
            m_Graph.AddNode(node);

            if (connectedPort != null)
            {
                var connectedSlot           = connectedPort.slot;
                var connectedSlotReference  = connectedSlot.owner.GetSlotReference(connectedSlot.id);
                var compatibleSlotReference = node.GetSlotReference(nodeEntry.compatibleSlotId);

                var fromReference = connectedSlot.isOutputSlot ? connectedSlotReference : compatibleSlotReference;
                var toReference   = connectedSlot.isOutputSlot ? compatibleSlotReference : connectedSlotReference;
                m_Graph.Connect(fromReference, toReference);

                nodeNeedsRepositioning = true;
                targetSlotReference    = compatibleSlotReference;
                targetPosition         = graphMousePosition;
            }

            return(true);
        }
        public void OnDrop(GraphView graphView, Edge edge)
        {
            var leftSlot  = edge.output.GetSlot();
            var rightSlot = edge.input.GetSlot();

            if (leftSlot != null && rightSlot != null)
            {
                m_Graph.owner.RegisterCompleteObjectUndo("Connect Edge");
                m_Graph.Connect(leftSlot.slotReference, rightSlot.slotReference);
            }
        }
Ejemplo n.º 3
0
        GraphViewChange GraphViewChanged(GraphViewChange graphViewChange)
        {
            if (graphViewChange.edgesToCreate != null)
            {
                foreach (var edge in graphViewChange.edgesToCreate)
                {
                    var leftSlot  = edge.output.GetSlot();
                    var rightSlot = edge.input.GetSlot();
                    if (leftSlot != null && rightSlot != null)
                    {
                        m_Graph.owner.RegisterCompleteObjectUndo("Connect Edge");
                        m_Graph.Connect(leftSlot.slotReference, rightSlot.slotReference);
                    }
                }
                graphViewChange.edgesToCreate.Clear();
            }

            if (graphViewChange.movedElements != null)
            {
                foreach (var element in graphViewChange.movedElements)
                {
                    var node = element.userData as INode;
                    if (node == null)
                    {
                        continue;
                    }

                    var drawState = node.drawState;
                    drawState.position = element.GetPosition();
                    node.drawState     = drawState;
                }
            }

            var nodesToUpdate = m_NodeViewHashSet;

            nodesToUpdate.Clear();

            if (graphViewChange.elementsToRemove != null)
            {
                m_Graph.owner.RegisterCompleteObjectUndo("Remove Elements");
                m_Graph.RemoveElements(graphViewChange.elementsToRemove.OfType <MaterialNodeView>().Select(v => (INode)v.node),
                                       graphViewChange.elementsToRemove.OfType <Edge>().Select(e => (IEdge)e.userData));
                foreach (var edge in graphViewChange.elementsToRemove.OfType <Edge>())
                {
                    if (edge.input != null)
                    {
                        var materialNodeView = edge.input.node as MaterialNodeView;
                        if (materialNodeView != null)
                        {
                            nodesToUpdate.Add(materialNodeView);
                        }
                    }
                    if (edge.output != null)
                    {
                        var materialNodeView = edge.output.node as MaterialNodeView;
                        if (materialNodeView != null)
                        {
                            nodesToUpdate.Add(materialNodeView);
                        }
                    }
                }
            }

            foreach (var node in nodesToUpdate)
            {
                node.UpdatePortInputVisibilities();
            }

            UpdateEdgeColors(nodesToUpdate);

            return(graphViewChange);
        }