コード例 #1
0
ファイル: NodeEditorGUI.cs プロジェクト: KurtGokhan/xNode
        public void DrawLinks()
        {
            Vector2 mousePos = Event.current.mousePosition;
            List <RerouteReference> selection = preBoxSelectionReroute != null ? new List <RerouteReference>(preBoxSelectionReroute) : new List <RerouteReference>();

            hoveredReroute = new RerouteReference();

            List <Vector2> gridPoints = new List <Vector2>(2);

            Color col = GUI.color;

            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;
                }

                var links = XNode.NodeDataCache.GetOutputLinks(node.GetType()).Select(x => new NodeLinkPort(node, x));

                // Draw full connections and output > reroute
                foreach (var output in links)
                {
                    //Needs cleanup. Null checks are ugly
                    Rect fromRect;
                    if (!linkConnectionPoints.TryGetValue(output, out fromRect))
                    {
                        continue;
                    }

                    Color           portColor   = graphEditor.GetLinkColor(output.Item2);
                    List <NodeLink> connections = output.GetConnections();

                    for (int k = 0; k < connections.Count; k++)
                    {
                        NodeLink link = connections[k];
                        if (!link)
                        {
                            Debug.LogWarning("There was a null entry in a node's connected links list. Removing.");
                            output.VerifyConnections();
                            connections.RemoveAt(k);
                            k--;
                            continue;
                        }
                        NodeLinkPort input = link.GetToPort();

                        Gradient     noodleGradient  = graphEditor.GetNoodleGradient(output, input);
                        float        noodleThickness = graphEditor.GetNoodleThickness(link);
                        NoodlePath   noodlePath      = graphEditor.GetNoodlePath(output, input);
                        NoodleStroke noodleStroke    = graphEditor.GetNoodleStroke(output, input);

                        // 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);
                        Rect toRect;
                        if (!linkConnectionPoints.TryGetValue(input, out toRect))
                        {
                            continue;
                        }

                        List <Vector2> reroutePoints = link.reroutePoints;

                        gridPoints.Clear();
                        gridPoints.Add(fromRect.center);
                        gridPoints.AddRange(reroutePoints);
                        gridPoints.Add(toRect.center);
                        DrawNoodle(noodleGradient, noodlePath, noodleStroke, noodleThickness, gridPoints, link);

                        // Loop through reroute points again and draw the points
                        for (int i = 0; i < reroutePoints.Count; i++)
                        {
                            //RerouteReference rerouteRef = new RerouteReference(output, k, i);
                            // Draw reroute point at position
                            Rect rect = new Rect(reroutePoints[i], new Vector2(12, 12));
                            rect.position = new Vector2(rect.position.x - 6, rect.position.y - 6);
                            rect          = GridToWindowRect(rect);

                            // TODO:
                            //// Draw selected reroute points with an outline
                            //if (selectedReroutes.Contains(rerouteRef)) {
                            //    GUI.color = NodeEditorPreferences.GetSettings().highlightColor;
                            //    GUI.DrawTexture(rect, NodeEditorResources.dotOuter);
                            //}

                            GUI.color = portColor;
                            GUI.DrawTexture(rect, NodeEditorResources.dot);
                            //if (rect.Overlaps(selectionBox)) selection.Add(rerouteRef);
                            //if (rect.Contains(mousePos)) hoveredReroute = rerouteRef;
                        }
                    }
                }
                GUI.color = col;
                if (Event.current.type != EventType.Layout && currentActivity == NodeActivity.DragGrid)
                {
                    selectedReroutes = selection;
                }
            }
        }