Ejemplo n.º 1
0
        /// <summary> Draw a connection as we are dragging it </summary>
        public void DrawDraggedConnection()
        {
            if (IsDraggingPort)
            {
                Color col = NodeEditorPreferences.GetTypeColor(draggedOutput.ValueType);

                if (!_portConnectionPoints.ContainsKey(draggedOutput))
                {
                    return;
                }
                col.a = 0.6f;
                Vector2 from = _portConnectionPoints[draggedOutput].center;
                Vector2 to   = Vector2.zero;
                for (int i = 0; i < draggedOutputReroutes.Count; i++)
                {
                    to = draggedOutputReroutes[i];
                    DrawConnection(from, to, col);
                    from = to;
                }
                to = draggedOutputTarget != null ? portConnectionPoints[draggedOutputTarget].center : WindowToGridPosition(Event.current.mousePosition);
                DrawConnection(from, to, col);

                Color bgcol = Color.black;
                Color frcol = col;
                bgcol.a = 0.6f;
                frcol.a = 0.6f;

                // Loop through reroute points again and draw the points
                for (int i = 0; i < draggedOutputReroutes.Count; i++)
                {
                    // Draw reroute point at position
                    Rect rect = new Rect(draggedOutputReroutes[i], new Vector2(16, 16));
                    rect.position = new Vector2(rect.position.x - 8, rect.position.y - 8);
                    rect          = GridToWindowRect(rect);

                    NodeEditorGUILayout.DrawPortHandle(rect, bgcol, frcol);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary> Draw a connection as we are dragging it </summary>
        public void DrawDraggedConnection()
        {
            if (IsDraggingPort)
            {
                Gradient     gradient  = graphEditor.GetNoodleGradient(draggedOutput, null);
                float        thickness = graphEditor.GetNoodleThickness(draggedOutput, null);
                NoodlePath   path      = graphEditor.GetNoodlePath(draggedOutput, null);
                NoodleStroke stroke    = graphEditor.GetNoodleStroke(draggedOutput, null);

                Rect fromRect;
                if (!portConnectionPoints.TryGetValue(draggedOutput, out fromRect))
                {
                    return;
                }
                List <Vector2> gridPoints = new List <Vector2>();
                gridPoints.Add(fromRect.center);
                for (int i = 0; i < draggedOutputReroutes.Count; i++)
                {
                    gridPoints.Add(draggedOutputReroutes[i]);
                }
                if (draggedOutputTarget != null)
                {
                    gridPoints.Add(portConnectionPoints[draggedOutputTarget].center);
                }
                else
                {
                    gridPoints.Add(WindowToGridPosition(Event.current.mousePosition));
                }

                DrawNoodle(gradient, path, stroke, thickness, gridPoints);

                Color bgcol = Color.black;
                Color frcol = gradient.colorKeys[0].color;
                bgcol.a = 0.6f;
                frcol.a = 0.6f;

                // Loop through reroute points again and draw the points
                for (int i = 0; i < draggedOutputReroutes.Count; i++)
                {
                    // Draw reroute point at position
                    Rect rect = new Rect(draggedOutputReroutes[i], new Vector2(16, 16));
                    rect.position = new Vector2(rect.position.x - 8, rect.position.y - 8);
                    rect          = GridToWindowRect(rect);

                    NodeEditorGUILayout.DrawPortHandle(rect, bgcol, frcol);
                }
            }

            if (IsDraggingLink)
            {
                Gradient     gradient  = graphEditor.GetNoodleGradient(draggedOutputLink, null);
                float        thickness = graphEditor.GetNoodleThickness(null);
                NoodlePath   path      = graphEditor.GetNoodlePath(draggedOutputLink, null);
                NoodleStroke stroke    = graphEditor.GetNoodleStroke(draggedOutputLink, null);

                Rect fromRect;
                if (!linkConnectionPoints.TryGetValue(draggedOutputLink, out fromRect))
                {
                    return;
                }
                List <Vector2> gridPoints = new List <Vector2>();
                gridPoints.Add(fromRect.center);
                if (draggedOutputTargetLink != null)
                {
                    gridPoints.Add(linkConnectionPoints[draggedOutputTargetLink].center);
                }
                else
                {
                    gridPoints.Add(WindowToGridPosition(Event.current.mousePosition));
                }

                DrawNoodle(gradient, path, stroke, thickness, gridPoints);

                Color bgcol = Color.black;
                Color frcol = gradient.colorKeys[0].color;
                bgcol.a = 0.6f;
                frcol.a = 0.6f;

                // TODO:
                //// Loop through reroute points again and draw the points
                //for (int i = 0; i < draggedOutputReroutes.Count; i++)
                //{
                //    // Draw reroute point at position
                //    Rect rect = new Rect(draggedOutputReroutes[i], new Vector2(16, 16));
                //    rect.position = new Vector2(rect.position.x - 8, rect.position.y - 8);
                //    rect = GridToWindowRect(rect);

                //    NodeEditorGUILayout.DrawPortHandle(rect, bgcol, frcol);
                //}
            }
        }
Ejemplo n.º 3
0
        /// <summary> Draws all connections </summary>
        public void DrawConnections()
        {
            Vector2 mousePos = Event.current.mousePosition;
            List <RerouteReference> selection = preBoxSelectionReroute != null ? new List <RerouteReference>(preBoxSelectionReroute) : new List <RerouteReference>();

            hoveredReroute = new RerouteReference();

            foreach (XNode.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;
                }

                // Draw full connections and output > reroute
                foreach (XNode.NodePort output in node.Outputs)
                {
                    //Needs cleanup. Null checks are ugly
                    if (!portConnectionPoints.ContainsKey(output))
                    {
                        continue;
                    }

                    Color connectionColor = graphEditor.GetTypeColor(output.ValueType);

                    for (int k = 0; k < output.ConnectionCount; k++)
                    {
                        XNode.NodePort input = output.GetConnection(k);

                        // Error handling
                        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        from          = _portConnectionPoints[output].center;
                        Vector2        to            = Vector2.zero;
                        List <Vector2> reroutePoints = output.GetReroutePoints(k);
                        // Loop through reroute points and draw the path
                        for (int i = 0; i < reroutePoints.Count; i++)
                        {
                            to = reroutePoints[i];
                            DrawConnection(from, to, connectionColor);
                            from = to;
                        }
                        to = _portConnectionPoints[input].center;
                        DrawConnection(from, to, connectionColor);

                        // Loop through reroute points again and draw the points
                        for (int i = 0; i < reroutePoints.Count; i++)
                        {
                            // Draw reroute point at position
                            Rect rect = new Rect(reroutePoints[i], new Vector2(16, 16));
                            rect.position = new Vector2(rect.position.x - 8, rect.position.y - 8);
                            rect          = GridToWindowRect(rect);
                            Color bgcol = new Color32(90, 97, 105, 255);;
                            if (selectedReroutes.Contains(new RerouteReference(output, k, i)))
                            {
                                bgcol = Color.yellow;
                            }
                            NodeEditorGUILayout.DrawPortHandle(rect, bgcol, connectionColor);

                            if (rect.Overlaps(selectionBox))
                            {
                                selection.Add(new RerouteReference(output, k, i));
                            }
                            if (rect.Contains(mousePos))
                            {
                                hoveredReroute = new RerouteReference(output, k, i);
                            }
                        }
                    }
                }
            }
            if (Event.current.type != EventType.Layout && currentActivity == NodeActivity.DragGrid)
            {
                selectedReroutes = selection;
            }
        }