public static Color GetMainPortColor(INodePort port)
        {
            if (port == null)
            {
                return(Color.magenta);
            }

            return(GameFlowPreferences.GetTypeColor(port.ValueType));
        }
        private static void DrawFieldPort(
            SerializedProperty property,
            GUIContent label,
            INode node,
            INodePort port,
            bool includeChildren = true,
            params GUILayoutOption[] options)
        {
            var rect = new Rect();

            var data     = node.GetPortData(node.GetType(), property.name);
            var portData = data.PortData;

            if (portData.InstancePortList)
            {
                InstancePortList(property.name,
                                 port.ValueType,
                                 property.serializedObject,
                                 portData.Direction,
                                 portData.ConnectionType);
                return;
            }

            ShowBackingValueField(data, property, port.IsConnected, includeChildren, null, GUILayout.MinWidth(30));

            rect          = GUILayoutUtility.GetLastRect();
            rect.position = port.Direction == PortIO.Input ? rect.position - new Vector2(16, 0) : rect.position + new Vector2(rect.width, 0);
            rect.size     = new Vector2(16, 16);

            Color backgroundColor = new Color32(90, 97, 105, 255);

            if (NodeEditorWindow.nodeTint.TryGetValue(port.Node.GetType(), out var tint))
            {
                backgroundColor *= tint;
            }

            Profiler.BeginSample("DrawPortHandle");
            var col = GameFlowPreferences.GetTypeColor(port.ValueType);

            DrawPortHandle(rect, backgroundColor, col);
            Profiler.EndSample();

            // Register the handle position
            var portPos = rect.center;

            //TODO REMOTE
            NodeEditor.PortPositions[port] = portPos;

            portData.Despawn();
        }
        /// <summary> Add a port field to previous layout element. </summary>
        public static void AddPortField(NodePort port)
        {
            if (port == null)
            {
                return;
            }
            var rect = new Rect();

            // If property is an input, display a regular property field and put a port handle on the left side
            if (port.Direction == PortIO.Input)
            {
                rect          = GUILayoutUtility.GetLastRect();
                rect.position = rect.position - new Vector2(16, 0);
                // If property is an output, display a text label and put a port handle on the right side
            }
            else if (port.Direction == PortIO.Output)
            {
                rect          = GUILayoutUtility.GetLastRect();
                rect.position = rect.position + new Vector2(rect.width, 0);
            }

            rect.size = new Vector2(16, 16);

            Color backgroundColor = new Color32(90, 97, 105, 255);
            Color tint;

            if (NodeEditorWindow.nodeTint.TryGetValue(port.Node.GetType(), out tint))
            {
                backgroundColor *= tint;
            }
            var col = GameFlowPreferences.GetTypeColor(port.ValueType);

            DrawPortHandle(rect, backgroundColor, col);

            // Register the handle position
            var portPos = rect.center;

            if (NodeEditor.PortPositions.ContainsKey(port))
            {
                NodeEditor.PortPositions[port] = portPos;
            }
            else
            {
                NodeEditor.PortPositions.Add(port, portPos);
            }
        }
Example #4
0
        /// <summary>
        /// Draw a connection as we are dragging it
        /// </summary>
        public void DrawDraggedConnection()
        {
            if (IsDraggingPort)
            {
                var col = GameFlowPreferences.GetTypeColor(draggedOutput.ValueType);

                if (!_portConnectionPoints.TryGetValue(draggedOutput, out var fromRect))
                {
                    return;
                }
                var from = fromRect.center;
                col.a = 0.6f;
                var to = Vector2.zero;
                for (var i = 0; i < draggedOutputReroutes.Count; i++)
                {
                    to = draggedOutputReroutes[i];
                    DrawConnection(from, to, col);
                    from = to;
                }

                to = draggedOutputTarget != null &&
                     PortConnectionPoints.TryGetValue(draggedOutputTarget, out var targetRect) ?
                     targetRect.center :
                     WindowToGridPosition(Event.current.mousePosition);

                DrawConnection(from, to, col);

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

                // Loop through reroute points again and draw the points
                for (var i = 0; i < draggedOutputReroutes.Count; i++)
                {
                    // Draw reroute point at position
                    var 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);
                }
            }
        }
Example #5
0
        /// <summary> Draws all connections </summary>
        public void DrawConnections()
        {
            var mousePos     = Event.current.mousePosition;
            var preSelection = preBoxSelectionReroute != null
                ? new List <RerouteReference>(preBoxSelectionReroute)
                : new List <RerouteReference>();

            hoveredReroute = new RerouteReference();

            var col = GUI.color;

            foreach (var node in ActiveGraph.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 (var output in node.Outputs)
                {
                    //Needs cleanup. Null checks are ugly
                    var item = _portConnectionPoints.FirstOrDefault(x => x.Key.IsEqual(output));

                    if (item.Key == null)
                    {
                        continue;
                    }
                    if (output == null)
                    {
                        continue;
                    }

                    var types           = output.ValueTypes;
                    var fromRect        = item.Value;
                    var connectionColor = GameFlowPreferences.GetTypeColor(types.FirstOrDefault());

                    for (var k = 0; k < output.ConnectionCount; k++)
                    {
                        var 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);
                        }

                        Rect toRect;
                        if (!PortConnectionPoints.TryGetValue(input, out toRect))
                        {
                            continue;
                        }

                        var from          = fromRect.center;
                        var to            = Vector2.zero;
                        var reroutePoints = output.GetReroutePoints(k);
                        // Loop through reroute points and draw the path
                        for (var i = 0; i < reroutePoints.Count; i++)
                        {
                            to = reroutePoints[i];
                            DrawConnection(from, to, connectionColor);
                            from = to;
                        }

                        to = toRect.center;

                        DrawConnection(from, to, connectionColor);

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

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

                            GUI.color = connectionColor;
                            GUI.DrawTexture(rect, NodeEditorResources.dot);
                            if (rect.Overlaps(selectionBox))
                            {
                                preSelection.Add(rerouteRef);
                            }
                            if (rect.Contains(mousePos))
                            {
                                hoveredReroute = rerouteRef;
                            }
                        }
                    }
                }
            }

            GUI.color = col;
            if (Event.current.type != EventType.Layout && currentActivity == NodeActivity.DragGrid)
            {
                selectedReroutes = preSelection;
            }
        }