예제 #1
0
        public override void Connect(Edge edge)
        {
            // Copy the connect onto the linked data
            // TODO: This happens also when we load the graph for every node.
            // How can this be optimized out?
            if (direction == Direction.Input)
            {
                target.Connect((edge.output as PortView).target);
            }
            else
            {
                target.Connect((edge.input as PortView).target);
            }

            base.Connect(edge);
        }
예제 #2
0
        protected override void RemoveAt(TList collection, int index)
        {
            NodePort indexPort = GetNodePort(index);

            if (indexPort == null)
            {
                Debug.LogWarning($"No port found at index {index}");
            }

            lastRemovedConnections.Clear();
            if (indexPort != null)
            {
                lastRemovedConnections.AddRange(indexPort.GetConnections());

                // Clear the removed ports connections
                indexPort.ClearConnections();
            }

            // Cache the last port because I'm about to remove it
            NodePort lastPort = GetNodePort(ChildCount - 1);

            // Move following connections one step up to replace the missing connection
            for (int k = index + 1; k < ChildCount; k++)
            {
                NodePort kPort = GetNodePort(k);
                if (kPort == null)
                {
                    continue;
                }

                NodePort k1Port = GetNodePort(k - 1);
                // Port k - 1 missing means I need to actually rename a port instead
                // Create k -1, add all the correct connections ... leave K alone because he existed
                if (k1Port == null)
                {
                    k1Port = ReplaceNodeForRemove(k - 1);
                }

                for (int j = 0; j < kPort.ConnectionCount; j++)
                {
                    NodePort other = kPort.GetConnection(j);
                    kPort.Disconnect(other);

                    k1Port.Connect(other);
                }
            }

            // Remove the last dynamic port, to avoid messing up the indexing
            if (lastPort != null)
            {
                lastPort.node.RemoveDynamicPort(lastPort);
            }

            base.RemoveAt(collection, index);

            this.ForceUpdateChildCount();
        }
예제 #3
0
        protected override void InsertAt(TList collection, int index, object value)
        {
            int newChildCount = this.ChildCount + 1;
            int nextId        = this.ChildCount;

            // Remove happens before insert and we lose all the connections
            // Add a new port at the end
            if (nodePortInfo.Port.IsInput)
            {
                nodePortInfo.Node.AddDynamicInput(typeof(TElement), nodePortInfo.ConnectionType, nodePortInfo.TypeConstraint, string.Format("{0} {1}", nodePortInfo.BaseFieldName, nextId));
            }
            else
            {
                nodePortInfo.Node.AddDynamicOutput(typeof(TElement), nodePortInfo.ConnectionType, nodePortInfo.TypeConstraint, string.Format("{0} {1}", nodePortInfo.BaseFieldName, nextId));
            }

            // Move everything down to make space - if something is missing just pretend we moved it?
            for (int k = newChildCount - 1; k > index; --k)
            {
                NodePort k1Port = GetNodePort(k - 1);
                if (k1Port == null)                   // It is missing, I have nothing to move
                {
                    continue;
                }

                for (int j = 0; j < k1Port.ConnectionCount; j++)
                {
                    NodePort other = k1Port.GetConnection(j);
                    k1Port.Disconnect(other);

                    NodePort kPort = GetNodePort(k);
                    if (kPort == null)
                    {
                        continue;
                    }

                    kPort.Connect(other);
                }
            }

            // Let's just re-add connections to this node that were probably his
            foreach (var c in lastRemovedConnections)
            {
                NodePort indexPort = GetNodePort(index);
                if (indexPort != null)
                {
                    indexPort.Connect(c);
                }
            }

            lastRemovedConnections.Clear();

            //if ( noDataResolver == null )
            base.InsertAt(collection, index, value);

            this.ForceUpdateChildCount();
        }
예제 #4
0
        public void AddChildConnection(NodePort connection)
        {
            if (connection.ValueType != typeof(FTConnection))
            {
                return;
            }

            NodePort newport = AddDynamicOutput(typeof(FTConnection), Node.ConnectionType.Override);

            _children.Add(new FTConnection(this, newport.fieldName));
            newport.Connect(connection);
        }
예제 #5
0
 public void OnEndDrag(PointerEventData eventData)
 {
     if (tempConnection == null)
     {
         return;
     }
     if (tempHovered)
     {
         startPort.Connect(tempHovered.port);
         graph.GetRuntimeNode(tempHovered.node).UpdateGUI();
     }
     Destroy(tempConnection.gameObject);
 }
예제 #6
0
        /// <summary>Add a new exit connection.</summary>
        public void AddExitConnection(NodePort connection)
        {
            // Check port type.
            if (connection.ValueType != typeof(FSMConnection))
            {
                return;
            }
            // Create new exit port.
            NodePort newport = AddInstanceOutput(typeof(FSMConnection), Node.ConnectionType.Override);

            exits.Add(new FSMConnection(this, newport.fieldName));
            // Add connection.
            newport.Connect(connection);
        }
예제 #7
0
        /// <summary>Adds a new children connection.</summary>
        /// <param name="connection">The NodePort to connect the new child connection to.</param>
        public virtual void AddChildConnection(NodePort connection)
        {
            // Check port type.
            if (connection.ValueType != typeof(BTConnection))
            {
                return;
            }
            // Create new exit port.
            NodePort newport = AddInstanceOutput(typeof(BTConnection), Node.ConnectionType.Override);

            _children.Add(new BTConnection(this, newport.fieldName));
            // Add connection.
            newport.Connect(connection);
        }
예제 #8
0
    /// <summary>
    /// Handles the internal behavior for when a node is connected to another node.
    /// </summary>
    /// <param name="from">Start of connection</param>
    /// <param name="to">End of connection</param>
    public override void OnCreateConnection(NodePort from, NodePort to)
    {
        if (from.ConnectionCount > 1)
        {
            from.ClearConnections();
            from.Connect(to);
        }

        // This gets the port index
        string indexName = from.fieldName.Replace("childNodes ", "");
        int    index     = int.Parse(indexName);

        // Sets the target ID in the child node
        ((DialogueNode)from.node).childNodes[index].targetID = ((DialogueNode)to.node).GetID();

        base.OnCreateConnection(from, to);
    }
예제 #9
0
 private void CreateNodeFromMenu(Type type, Vector2 pos, NodePort draggedOutput)
 {
     CreateNode(type, pos, node =>
     {
         node.name = NodeName(type);
         // Try connect created node to current dragged output
         if (draggedOutput != null)
         {
             foreach (NodePort input in node.Inputs)
             {
                 if (draggedOutput.CanConnectTo(input))
                 {
                     draggedOutput.Connect(input);
                     break;
                 }
             }
         }
     });
 }
예제 #10
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);
                    }
                }
            }
        }
        protected void StopDisconnection()
        {
            if (portName == "TargetValues")
            {
                NodePort portConnect = GetInputPort(portName);
                for (int i = 0; i < targetPortList.Count; i++)
                {
                    portConnect.Connect(targetPortList[i]);
                    targetPortList[i].Connect(portConnect);
                }
            }

            if (portName == "InputFeatures")
            {
                NodePort portConnect = GetInputPort(portName);
                for (int i = 0; i < inputPortList.Count; i++)
                {
                    portConnect.Connect(inputPortList[i]);
                    inputPortList[i].Connect(portConnect);
                }
            }

            badRemove = false;
        }
예제 #12
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;
            }
        }
예제 #13
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;
        }
    }
        public override NodeGraphData ImportData(string serializedData)
        {
            NodeGraphData returnData = null;


            JSONNode root = JSON.Parse(serializedData);

            Dictionary <int, object>            references = new Dictionary <int, object>();
            Dictionary <NodePort, NodePortData> portDatas  = new Dictionary <NodePort, NodePortData>();

            List <string> ignoredFields = new List <string> {
                "name", "graph", "ports", "nodes"
            };
            JSONObject graphJObject;

            if (root.HasKey("graph"))
            {
                graphJObject = root["graph"].AsObject;
                int    id         = graphJObject["id"];
                string graphTypeS = graphJObject["type"];
                Type   graphType  = Type.GetType(graphTypeS);

                NodeGraph graph = (NodeGraph)Activator.CreateInstance(graphType);
                graph.name = graphJObject["name"];

                references.Add(id, graph);
                returnData = new NodeGraphData(graph);

                //Debug.Log("Basic graph OK!");
            }
            else
            {
                Debug.LogWarning("Basic graph KO!");
                return(returnData);
            }

            if (root.HasKey("nodes"))
            {
                JSONArray nodesJArray = root["nodes"].AsArray;

                foreach (JSONObject nodeJObject in nodesJArray.Values)
                {
                    int    id        = nodeJObject["id"];
                    string nodeTypeS = nodeJObject["type"];
                    Type   nodeType  = Type.GetType(nodeTypeS);

                    Node node = (Node)Activator.CreateInstance(nodeType);
                    node.name = nodeJObject["name"];
                    object nodeOBJ = (object)node;

                    SimpleJSONExtension.FromJSON(ref nodeOBJ, nodeType, nodeJObject, ignoredFields, references);
                    node.graph = returnData.graph;
                    NodeData nodeData = new NodeData(node);

                    JSONArray nodePortsArray = nodeJObject["ports"].AsArray;
                    foreach (var nodePort in nodePortsArray.Values)
                    {
                        bool     dynamic  = nodePort["dynamic"].AsBool;
                        string   portName = nodePort["name"];
                        NodePort port     = null;
                        int      portId   = 0;

                        if (dynamic)
                        {
                            if (!node.HasPort(portName))
                            {
                                Type dynamicType = Type.GetType(nodePort["valueType"]);
                                Node.TypeConstraint constraint     = (Node.TypeConstraint)nodePort["typeConstraint"].AsInt;
                                Node.ConnectionType connectionType = (Node.ConnectionType)nodePort["connectionType"].AsInt;
                                NodePort.IO         direction      = (NodePort.IO)nodePort["direction"].AsInt;

                                if (direction == NodePort.IO.Input)
                                {
                                    port = node.AddInstanceInput(dynamicType, connectionType, constraint, portName);
                                }
                                else
                                {
                                    port = node.AddInstanceOutput(dynamicType, connectionType, constraint, portName);
                                }
                            }
                            else
                            {
                                Debug.LogWarning("Ignoring port bc is already created");
                            }
                        }

                        port = node.GetPort(nodePort["name"]);

                        if (port == null)
                        {
                            Debug.Log("Port is null? " + node.name);

                            foreach (var p in node.Ports)
                            {
                                Debug.Log(p.fieldName);
                            }
                        }

                        portId = nodePort["id"];

                        NodePortData portData = new NodePortData(nodeData, port);

                        nodeData.ports.Add(portData);
                        portDatas.Add(port, portData);
                        references.Add(portId, port);
                    }

                    references.Add(id, node);
                    returnData.nodes.Add(nodeData);
                }

                //Debug.Log("Basic Nodes OK!");
            }
            else
            {
                Debug.LogWarning("Basic Nodes KO!");
                return(returnData);
            }


            if (root.HasKey("connections"))
            {
                JSONArray connectionsJArray = root["connections"].AsArray;

                foreach (JSONObject connectionJObject in connectionsJArray.Values)
                {
                    int port1ID = connectionJObject["port1ID"].AsInt;
                    int port2ID = connectionJObject["port2ID"].AsInt;

                    if (references.ContainsKey(port1ID) && references.ContainsKey(port2ID))
                    {
                        NodePort p1 = (NodePort)references[port1ID];
                        NodePort p2 = (NodePort)references[port2ID];

                        p1.Connect(p2);
                    }
                    else
                    {
                        Debug.LogWarning("Error recovering one connection");
                    }
                }
                //Debug.Log("Connections OK!");
            }
            else
            {
                Debug.LogWarning("Connections KO!");
                return(returnData);
            }


            object graphObject = returnData.graph;

            SimpleJSONExtension.FromJSON(ref graphObject, returnData.graph.GetType(), graphJObject, ignoredFields, references);


            return(returnData);
        }