예제 #1
0
        public void DisconnectView(EdgeView e, bool refreshPorts = true)
        {
            if (e == null)
            {
                return;
            }

            RemoveElement(e);

            if (e?.input?.node is BaseNodeView inputNodeView)
            {
                e.input.Disconnect(e);
                if (refreshPorts)
                {
                    inputNodeView.RefreshPorts();
                }
            }
            if (e?.output?.node is BaseNodeView outputNodeView)
            {
                e.output.Disconnect(e);
                if (refreshPorts)
                {
                    outputNodeView.RefreshPorts();
                }
            }

            edgeViews.Remove(e);
        }
        public void Disconnect(EdgeView e, bool serializeToGraph = true)
        {
            var serializableEdge = e.userData as SerializableEdge;

            RemoveElement(e);

            if (e?.input?.node != null)
            {
                var inputNodeView = e.input.node as BaseNodeView;
                e.input.Disconnect(e);
                inputNodeView.nodeTarget.OnEdgeDisonnected(e.serializedEdge);
                inputNodeView.RefreshPorts();
            }
            if (e?.output?.node != null)
            {
                var outputNodeView = e.output.node as BaseNodeView;
                e.output.Disconnect(e);
                outputNodeView.nodeTarget.OnEdgeDisonnected(e.serializedEdge);
                outputNodeView.RefreshPorts();
            }

            // Remove the serialized edge if there was one
            if (serializableEdge != null)
            {
                if (serializeToGraph)
                {
                    graph.Disconnect(serializableEdge.GUID);
                }
                UpdateComputeOrder();
            }
        }
        public bool Connect(EdgeView e, bool autoDisconnectInputs = true)
        {
            if (!ConnectView(e, autoDisconnectInputs))
            {
                return(false);
            }

            var inputPortView  = e.input as PortView;
            var outputPortView = e.output as PortView;
            var inputNodeView  = inputPortView.node as BaseNodeView;
            var outputNodeView = outputPortView.node as BaseNodeView;
            var inputPort      = inputNodeView.nodeTarget.GetPort(inputPortView.fieldName, inputPortView.portData.identifier);
            var outputPort     = outputNodeView.nodeTarget.GetPort(outputPortView.fieldName, outputPortView.portData.identifier);

            e.userData = graph.Connect(inputPort, outputPort, autoDisconnectInputs);

            UpdateComputeOrder();

            inputNodeView.nodeTarget.UpdateAllPorts();
            outputNodeView.nodeTarget.UpdateAllPorts();
            inputNodeView.RefreshPorts();
            outputNodeView.RefreshPorts();

            return(true);
        }
        public bool ConnectView(EdgeView e, bool autoDisconnectInputs = true)
        {
            if (e.input == null || e.output == null)
            {
                return(false);
            }

            var inputPortView  = e.input as PortView;
            var outputPortView = e.output as PortView;
            var inputNodeView  = inputPortView.node as BaseNodeView;
            var outputNodeView = outputPortView.node as BaseNodeView;

            if (inputNodeView == null || outputNodeView == null)
            {
                Debug.LogError("Connect aborted !");
                return(false);
            }

            //If the input port does not support multi-connection, we remove them
            if (autoDisconnectInputs && !(e.input as PortView).portData.acceptMultipleEdges)
            {
                foreach (var edge in edgeViews.Where(ev => ev.input == e.input).ToList())
                {
                    // TODO: do not disconnect them if the connected port is the same than the old connected
                    DisconnectView(edge);
                }
            }
            // same for the output port:
            if (autoDisconnectInputs && !(e.output as PortView).portData.acceptMultipleEdges)
            {
                foreach (var edge in edgeViews.Where(ev => ev.output == e.output).ToList())
                {
                    // TODO: do not disconnect them if the connected port is the same than the old connected
                    DisconnectView(edge);
                }
            }

            AddElement(e);

            e.input.Connect(e);
            e.output.Connect(e);

            // If the input port have been removed by the custom port behavior
            // we try to find if it's still here
            if (e.input == null)
            {
                e.input = inputNodeView.GetPortViewFromFieldName(inputPortView.fieldName, inputPortView.portData.identifier);
            }
            if (e.output == null)
            {
                e.output = inputNodeView.GetPortViewFromFieldName(outputPortView.fieldName, outputPortView.portData.identifier);
            }

            edgeViews.Add(e);

            e.isConnected = true;

            return(true);
        }
        void ShowNodeCreationMenuFromEdge(EdgeView edgeView, Vector2 position)
        {
            if (edgeNodeCreateMenuWindow == null)
            {
                edgeNodeCreateMenuWindow = ScriptableObject.CreateInstance <CreateNodeMenuWindow>();
            }

            edgeNodeCreateMenuWindow.Initialize(graphView, EditorWindow.focusedWindow, edgeView);
            SearchWindow.Open(new SearchWindowContext(position + EditorWindow.focusedWindow.position.position), edgeNodeCreateMenuWindow);
        }
        public void Connect(EdgeView e, bool serializeToGraph = true, bool autoDisconnectInputs = true)
        {
            if (e.input == null || e.output == null)
            {
                return;
            }

            //If the input port does not support multi-connection, we remove them
            if (autoDisconnectInputs && !(e.input as PortView).isMultiple)
            {
                foreach (var edge in edgeViews.Where(ev => ev.input == e.input))
                {
                    // TODO: do not disconnect them if the connected port is the same than the old connected
                    Disconnect(edge, serializeToGraph);
                }
            }

            AddElement(e);

            e.input.Connect(e);
            e.output.Connect(e);

            var inputNodeView  = e.input.node as BaseNodeView;
            var outputNodeView = e.output.node as BaseNodeView;

            if (inputNodeView == null || outputNodeView == null)
            {
                Debug.LogError("Connect aborted !");
                return;
            }

            edgeViews.Add(e);

            if (serializeToGraph)
            {
                e.userData = graph.Connect(
                    inputNodeView.nodeTarget, (e.input as PortView).fieldName,
                    outputNodeView.nodeTarget, (e.output as PortView).fieldName
                    );
            }

            inputNodeView.RefreshPorts();
            outputNodeView.RefreshPorts();

            inputNodeView.nodeTarget.OnEdgeConnected(e.userData as SerializableEdge);
            outputNodeView.nodeTarget.OnEdgeConnected(e.userData as SerializableEdge);

            e.isConnected = true;

            if (serializeToGraph)
            {
                UpdateComputeOrder();
            }
        }
        public void Disconnect(EdgeView e, bool refreshPorts = true)
        {
            DisconnectView(e, refreshPorts);

            // Remove the serialized edge if there is one
            if (e.userData is SerializableEdge serializableEdge)
            {
                graph.Disconnect(serializableEdge.GUID);
                UpdateComputeOrder();
            }
        }
예제 #8
0
        void InitializeEdgeViews()
        {
            foreach (var serializedEdge in graph.edges)
            {
                var inputNodeView  = nodeViewsPerNode[serializedEdge.inputNode];
                var outputNodeView = nodeViewsPerNode[serializedEdge.outputNode];
                var edgeView       = new EdgeView()
                {
                    userData = serializedEdge,
                    input    = inputNodeView.GetPortViewFromFieldName(serializedEdge.inputFieldName, serializedEdge.inputPortIdentifier),
                    output   = outputNodeView.GetPortViewFromFieldName(serializedEdge.outputFieldName, serializedEdge.outputPortIdentifier)
                };

                Connect(edgeView, false);
            }
        }
        public void Initialize(BaseGraphView graphView, EditorWindow window, EdgeView edgeFilter = null)
        {
            this.graphView      = graphView;
            this.window         = window;
            this.edgeFilter     = edgeFilter;
            this.inputPortView  = edgeFilter?.input as PortView;
            this.outputPortView = edgeFilter?.output as PortView;

            // Transparent icon to trick search window into indenting items
            if (icon == null)
            {
                icon = new Texture2D(1, 1);
            }
            icon.SetPixel(0, 0, new Color(0, 0, 0, 0));
            icon.Apply();
        }
예제 #10
0
        public bool CanConnectEdge(EdgeView e, bool autoDisconnectInputs = true)
        {
            if (e.input == null || e.output == null)
            {
                return(false);
            }

            var inputPortView  = e.input as PortView;
            var outputPortView = e.output as PortView;
            var inputNodeView  = inputPortView.node as BaseNodeView;
            var outputNodeView = outputPortView.node as BaseNodeView;

            if (inputNodeView == null || outputNodeView == null)
            {
                Debug.LogError("Connect aborted !");
                return(false);
            }
            return(true);
        }
예제 #11
0
        void InitializeEdgeViews()
        {
            foreach (var serializedEdge in graph.edges)
            {
                nodeViewsPerNode.TryGetValue(serializedEdge.inputNode, out var inputNodeView);
                nodeViewsPerNode.TryGetValue(serializedEdge.outputNode, out var outputNodeView);
                if (inputNodeView == null || outputNodeView == null)
                {
                    continue;
                }

                var edgeView = new EdgeView()
                {
                    userData = serializedEdge,
                    input    = inputNodeView.GetPortViewFromFieldName(serializedEdge.inputFieldName, serializedEdge.inputPortIdentifier),
                    output   = outputNodeView.GetPortViewFromFieldName(serializedEdge.outputFieldName, serializedEdge.outputPortIdentifier)
                };

                ConnectView(edgeView);
            }
        }
예제 #12
0
        public bool Connect(PortView inputPortView, PortView outputPortView, bool autoDisconnectInputs = true)
        {
            var inputPort  = inputPortView.owner.nodeTarget.GetPort(inputPortView.fieldName, inputPortView.portData.identifier);
            var outputPort = outputPortView.owner.nodeTarget.GetPort(outputPortView.fieldName, outputPortView.portData.identifier);

            // Checks that the node we are connecting still exists
            if (inputPortView.owner.parent == null || outputPortView.owner.parent == null)
            {
                return(false);
            }

            var newEdge = SerializableEdge.CreateNewEdge(graph, inputPort, outputPort);

            var edgeView = new EdgeView()
            {
                userData = newEdge,
                input    = inputPortView,
                output   = outputPortView,
            };

            return(Connect(edgeView));
        }
예제 #13
0
        void InitializeEdgeViews()
        {
            // Sanitize edges in case a node broke something while loading
            graph.edges.RemoveAll(edge => edge == null || edge.inputNode == null || edge.outputNode == null);

            foreach (var serializedEdge in graph.edges)
            {
                nodeViewsPerNode.TryGetValue(serializedEdge.inputNode, out var inputNodeView);
                nodeViewsPerNode.TryGetValue(serializedEdge.outputNode, out var outputNodeView);
                if (inputNodeView == null || outputNodeView == null)
                {
                    continue;
                }

                var edgeView = new EdgeView()
                {
                    userData = serializedEdge,
                    input    = inputNodeView.GetPortViewFromFieldName(serializedEdge.inputFieldName, serializedEdge.inputPortIdentifier),
                    output   = outputNodeView.GetPortViewFromFieldName(serializedEdge.outputFieldName, serializedEdge.outputPortIdentifier)
                };

                ConnectView(edgeView);
            }
        }
예제 #14
0
        void UnserializeAndPasteCallback(string operationName, string serializedData)
        {
            var data = JsonUtility.FromJson <CopyPasteHelper>(serializedData);

            RegisterCompleteObjectUndo(operationName);

            Dictionary <string, BaseNode> copiedNodesMap = new Dictionary <string, BaseNode>();

            foreach (var serializedNode in data.copiedNodes)
            {
                var node = JsonSerializer.DeserializeNode(serializedNode);

                if (node == null)
                {
                    continue;
                }

                string sourceGUID = node.GUID;
                graph.nodesPerGUID.TryGetValue(sourceGUID, out var sourceNode);
                //Call OnNodeCreated on the new fresh copied node
                node.OnNodeCreated();
                //And move a bit the new node
                node.position.position += new Vector2(20, 20);

                var newNodeView = AddNode(node);

                // If the nodes were copied from another graph, then the source is null
                if (sourceNode != null)
                {
                    nodeDuplicated?.Invoke(sourceNode, node);
                }
                copiedNodesMap[sourceGUID] = node;

                //Select the new node
                AddToSelection(nodeViewsPerNode[node]);
            }

            foreach (var serializedGroup in data.copiedGroups)
            {
                var group = JsonSerializer.Deserialize <Group>(serializedGroup);

                //Same than for node
                group.OnCreated();

                // try to centre the created node in the screen
                group.position.position += new Vector2(20, 20);

                var oldGUIDList = group.innerNodeGUIDs.ToList();
                group.innerNodeGUIDs.Clear();
                foreach (var guid in oldGUIDList)
                {
                    graph.nodesPerGUID.TryGetValue(guid, out var node);

                    // In case group was copied from another graph
                    if (node == null)
                    {
                        copiedNodesMap.TryGetValue(guid, out node);
                        group.innerNodeGUIDs.Add(node.GUID);
                    }
                    else
                    {
                        group.innerNodeGUIDs.Add(copiedNodesMap[guid].GUID);
                    }
                }

                AddGroup(group);
            }

            foreach (var serializedEdge in data.copiedEdges)
            {
                var edge = JsonSerializer.Deserialize <SerializableEdge>(serializedEdge);

                edge.Deserialize();

                // Find port of new nodes:
                copiedNodesMap.TryGetValue(edge.inputNode.GUID, out var oldInputNode);
                copiedNodesMap.TryGetValue(edge.outputNode.GUID, out var oldOutputNode);

                // We avoid to break the graph by replacing unique connections:
                if (oldInputNode == null && !edge.inputPort.portData.acceptMultipleEdges || !edge.outputPort.portData.acceptMultipleEdges)
                {
                    continue;
                }

                oldInputNode  = oldInputNode ?? edge.inputNode;
                oldOutputNode = oldOutputNode ?? edge.outputNode;

                var inputPort  = oldInputNode.GetPort(edge.inputPort.fieldName, edge.inputPortIdentifier);
                var outputPort = oldOutputNode.GetPort(edge.outputPort.fieldName, edge.outputPortIdentifier);

                var newEdge = SerializableEdge.CreateNewEdge(graph, inputPort, outputPort);

                if (nodeViewsPerNode.ContainsKey(oldInputNode) && nodeViewsPerNode.ContainsKey(oldOutputNode))
                {
                    var edgeView = new EdgeView()
                    {
                        userData = newEdge,
                        input    = nodeViewsPerNode[oldInputNode].GetPortViewFromFieldName(newEdge.inputFieldName, newEdge.inputPortIdentifier),
                        output   = nodeViewsPerNode[oldOutputNode].GetPortViewFromFieldName(newEdge.outputFieldName, newEdge.outputPortIdentifier)
                    };

                    Connect(edgeView);
                }
            }
        }
예제 #15
0
        public bool Connect(EdgeView e, bool serializeToGraph = true, bool autoDisconnectInputs = true)
        {
            if (e.input == null || e.output == null)
            {
                return(false);
            }

            //If the input port does not support multi-connection, we remove them
            if (autoDisconnectInputs && !(e.input as PortView).portData.acceptMultipleEdges)
            {
                foreach (var edge in edgeViews.Where(ev => ev.input == e.input))
                {
                    // TODO: do not disconnect them if the connected port is the same than the old connected
                    Disconnect(edge, serializeToGraph);
                }
            }

            AddElement(e);

            var inputNodeView        = e.input.node as BaseNodeView;
            var outputNodeView       = e.output.node as BaseNodeView;
            var inputPortView        = e.input as PortView;
            var outputPortView       = e.output as PortView;
            var inputPortIdentifier  = inputPortView.portData.identifier;
            var outputPortIdentifier = outputPortView.portData.identifier;
            var inputPortFieldName   = inputPortView.fieldName;
            var outputPortFieldName  = outputPortView.fieldName;

            if (serializeToGraph)
            {
                NodePort inputPort  = inputNodeView.nodeTarget.inputPorts.FirstOrDefault(o => o.portData.identifier == inputPortView.portData.identifier);
                NodePort outputPort = outputNodeView.nodeTarget.outputPorts.FirstOrDefault(o => o.portData == outputPortView.portData);

                e.userData = graph.Connect(inputPort, outputPort);
            }

            // Add the edge to the list of connected edges in the nodes
            inputNodeView.nodeTarget.OnEdgeConnected(e.userData as SerializableEdge);
            outputNodeView.nodeTarget.OnEdgeConnected(e.userData as SerializableEdge);

            e.input.Connect(e);
            e.output.Connect(e);

            // If the input port have been removed by the custom port behavior
            // we try to find if it's still here
            if (e.input == null)
            {
                e.input = inputNodeView.GetPortViewFromFieldName(inputPortFieldName, inputPortIdentifier);
            }
            if (e.output == null)
            {
                e.output = inputNodeView.GetPortViewFromFieldName(outputPortFieldName, outputPortIdentifier);
            }

            if (inputNodeView == null || outputNodeView == null)
            {
                Debug.LogError("Connect aborted !");
                return(false);
            }

            edgeViews.Add(e);

            inputNodeView.RefreshPorts();
            outputNodeView.RefreshPorts();

            e.isConnected = true;

            if (serializeToGraph)
            {
                UpdateComputeOrder();
            }

            return(true);
        }
예제 #16
0
        public bool ConnectView(EdgeView e, bool autoDisconnectInputs = true)
        {
            if (!CanConnectEdge(e, autoDisconnectInputs))
            {
                return(false);
            }

            var inputPortView  = e.input as PortView;
            var outputPortView = e.output as PortView;
            var inputNodeView  = inputPortView.node as BaseNodeView;
            var outputNodeView = outputPortView.node as BaseNodeView;

            //If the input port does not support multi-connection, we remove them
            if (autoDisconnectInputs && !(e.input as PortView).portData.acceptMultipleEdges)
            {
                foreach (var edge in edgeViews.Where(ev => ev.input == e.input).ToList())
                {
                    // TODO: do not disconnect them if the connected port is the same than the old connected
                    DisconnectView(edge);
                }
            }
            // same for the output port:
            if (autoDisconnectInputs && !(e.output as PortView).portData.acceptMultipleEdges)
            {
                foreach (var edge in edgeViews.Where(ev => ev.output == e.output).ToList())
                {
                    // TODO: do not disconnect them if the connected port is the same than the old connected
                    DisconnectView(edge);
                }
            }

            AddElement(e);

            e.input.Connect(e);
            e.output.Connect(e);

            // If the input port have been removed by the custom port behavior
            // we try to find if it's still here
            if (e.input == null)
            {
                e.input = inputNodeView.GetPortViewFromFieldName(inputPortView.fieldName, inputPortView.portData.identifier);
            }
            if (e.output == null)
            {
                e.output = inputNodeView.GetPortViewFromFieldName(outputPortView.fieldName, outputPortView.portData.identifier);
            }

            edgeViews.Add(e);

            inputNodeView.RefreshPorts();
            outputNodeView.RefreshPorts();

            // In certain cases the edge color is wrong so we patch it
            schedule.Execute(() => {
                e.UpdateEdgeControl();
            }).ExecuteLater(1);

            e.isConnected = true;

            if (outputPortView.portType == typeof(ConditionalLink))
            {
                e.AddToClassList("conditional-link");
            }

            return(true);
        }