Example #1
0
        /// <summary> Draws all connections </summary>
        public void DrawConnections()
        {
            foreach (Node node in graph.nodes)
            {
                //If a null node is found, return. This can happen if the nodes associated script is deleted. It is currently not possible in Unity to delete a null asset.
                if (node == null)
                {
                    continue;
                }

                foreach (NodePort output in node.Outputs)
                {
                    //Needs cleanup. Null checks are ugly
                    if (!portConnectionPoints.ContainsKey(output))
                    {
                        continue;
                    }
                    Vector2 from = _portConnectionPoints[output].center;
                    for (int k = 0; k < output.ConnectionCount; k++)
                    {
                        NodePort input = output.GetConnection(k);
                        if (input == null)
                        {
                            continue;                //If a script has been updated and the port doesn't exist, it is removed and null is returned. If this happens, return.
                        }
                        if (!input.IsConnectedTo(output))
                        {
                            input.Connect(output);
                        }
                        if (!_portConnectionPoints.ContainsKey(input))
                        {
                            continue;
                        }
                        Vector2 to = _portConnectionPoints[input].center;
                        Color   connectionColor = currentGraphEditor.GetTypeColor(output.ValueType);
                        DrawConnection(from, to, connectionColor);
                    }
                }
            }
        }
Example #2
0
        public void Controls()
        {
            wantsMouseMove = true;
            Event e = Event.current;

            switch (e.type)
            {
            case EventType.MouseMove:
                break;

            case EventType.ScrollWheel:
                if (e.delta.y > 0)
                {
                    zoom += 0.1f * zoom;
                }
                else
                {
                    zoom -= 0.1f * zoom;
                }
                break;

            case EventType.MouseDrag:
                if (e.button == 0)
                {
                    if (IsDraggingPort)
                    {
                        if (IsHoveringPort && hoveredPort.IsInput)
                        {
                            if (!draggedOutput.IsConnectedTo(hoveredPort))
                            {
                                draggedOutputTarget = hoveredPort;
                            }
                        }
                        else
                        {
                            draggedOutputTarget = null;
                        }
                        Repaint();
                    }
                    else if (IsDraggingNode)
                    {
                        draggedNode.position = WindowToGridPosition(e.mousePosition) + dragOffset;
                        Repaint();
                    }
                }
                else if (e.button == 1)
                {
                    panOffset += e.delta * zoom;
                    isPanning  = true;
                }
                break;

            case EventType.KeyDown:
                if (GUIUtility.keyboardControl == 0)
                {
                    break;
                }
                else if (e.keyCode == KeyCode.F)
                {
                    Home();
                }
                break;

            case EventType.MouseDown:
                Repaint();
                if (e.button == 0)
                {
                    SelectNode(hoveredNode);
                    if (IsHoveringPort)
                    {
                        if (hoveredPort.IsOutput)
                        {
                            draggedOutput = hoveredPort;
                        }
                        else
                        {
                            hoveredPort.VerifyConnections();
                            if (hoveredPort.IsConnected)
                            {
                                Node     node   = hoveredPort.node;
                                NodePort output = hoveredPort.Connection;
                                hoveredPort.Disconnect(output);
                                draggedOutput       = output;
                                draggedOutputTarget = hoveredPort;
                                if (NodeEditor.onUpdateNode != null)
                                {
                                    NodeEditor.onUpdateNode(node);
                                }
                            }
                        }
                    }
                    else if (IsHoveringNode && IsHoveringTitle(hoveredNode))
                    {
                        draggedNode = hoveredNode;
                        dragOffset  = hoveredNode.position - WindowToGridPosition(e.mousePosition);
                    }
                }
                break;

            case EventType.MouseUp:
                if (e.button == 0)
                {
                    //Port drag release
                    if (IsDraggingPort)
                    {
                        //If connection is valid, save it
                        if (draggedOutputTarget != null)
                        {
                            Node node = draggedOutputTarget.node;
                            if (graph.nodes.Count != 0)
                            {
                                draggedOutput.Connect(draggedOutputTarget);
                            }
                            if (NodeEditor.onUpdateNode != null)
                            {
                                NodeEditor.onUpdateNode(node);
                            }
                            EditorUtility.SetDirty(graph);
                        }
                        //Release dragged connection
                        draggedOutput       = null;
                        draggedOutputTarget = null;
                        EditorUtility.SetDirty(graph);
                        Repaint();
                        AssetDatabase.SaveAssets();
                    }
                    else if (IsDraggingNode)
                    {
                        draggedNode = null;
                        AssetDatabase.SaveAssets();
                    }
                    else if (GUIUtility.hotControl != 0)
                    {
                        AssetDatabase.SaveAssets();
                    }
                }
                else if (e.button == 1)
                {
                    if (!isPanning)
                    {
                        ShowContextMenu();
                    }
                    isPanning = false;
                }
                break;
            }
        }
Example #3
0
    public void Controls()
    {
        wantsMouseMove = true;

        Event e = Event.current;

        switch (e.type)
        {
        case EventType.MouseMove:
            UpdateHovered();
            break;

        case EventType.ScrollWheel:
            if (e.delta.y > 0)
            {
                zoom += 0.1f * zoom;
            }
            else
            {
                zoom -= 0.1f * zoom;
            }
            break;

        case EventType.MouseDrag:
            UpdateHovered();
            if (e.button == 0)
            {
                if (IsDraggingPort)
                {
                    if (IsHoveringPort && hoveredPort.IsInput)
                    {
                        if (!draggedOutput.IsConnectedTo(hoveredPort))
                        {
                            draggedOutputTarget = hoveredPort;
                        }
                    }
                    else
                    {
                        draggedOutputTarget = null;
                    }
                    Repaint();
                }
                else if (IsDraggingNode)
                {
                    draggedNode.rect.position = WindowToGridPosition(e.mousePosition) + dragOffset;
                    Repaint();
                }
            }
            else if (e.button == 1)
            {
                panOffset += e.delta * zoom;
                isPanning  = true;
            }
            break;

        case EventType.KeyDown:
            if (e.keyCode == KeyCode.F)
            {
                Home();
            }
            break;

        case EventType.MouseDown:
            UpdateHovered();
            Repaint();
            SelectNode(hoveredNode);
            if (IsHoveringPort)
            {
                if (hoveredPort.IsOutput)
                {
                    draggedOutput = hoveredPort;
                }
                else
                {
                    if (hoveredPort.IsConnected)
                    {
                        NodePort output = hoveredPort.Connection;
                        hoveredPort.Disconnect(output);
                        draggedOutput       = output;
                        draggedOutputTarget = hoveredPort;
                    }
                }
            }
            else if (IsHoveringNode && IsHoveringTitle(hoveredNode))
            {
                draggedNode = hoveredNode;
                dragOffset  = hoveredNode.rect.position - WindowToGridPosition(e.mousePosition);
            }
            break;

        case EventType.MouseUp:
            if (e.button == 0)
            {
                //Port drag release
                if (IsDraggingPort)
                {
                    //If connection is valid, save it
                    if (draggedOutputTarget != null)
                    {
                        if (graph.nodes.Count != 0)
                        {
                            draggedOutput.Connect(draggedOutputTarget);
                        }
                    }
                    //Release dragged connection
                    draggedOutput       = null;
                    draggedOutputTarget = null;
                    Repaint();
                }
                else if (IsDraggingNode)
                {
                    draggedNode = null;
                }
            }
            else if (e.button == 1)
            {
                if (!isPanning)
                {
                    ShowContextMenu();
                }
                isPanning = false;
            }
            UpdateHovered();
            break;
        }
    }