private void DeleteNodes()
 {
     GraphUtility.RemoveNodes(this.m_Graph, this.m_Selection.ToArray());
     this.m_Selection.Clear();
     GraphUtility.Save(this.m_Graph);
     PrefabUtility.RecordPrefabInstancePropertyModifications(this.m_Target);
 }
Beispiel #2
0
        private void OnGUI()
        {
            this.m_GraphView.OnGUI(new Rect(0, 0, position.width, position.height));
            Event currentEvent = Event.current;

            if (currentEvent.type == EventType.ValidateCommand && commandNames.Contains(currentEvent.commandName))
            {
                currentEvent.Use();
            }
            if (currentEvent.type == EventType.ExecuteCommand)
            {
                string command = currentEvent.commandName;
                switch (command)
                {
                case "OnStartDrag":
                    break;

                case "OnEndDrag":
                    GraphUtility.Save(m_Behavior.GetGraph());
                    PrefabUtility.RecordPrefabInstancePropertyModifications(this.m_TargetObject);
                    break;
                }
            }

            if (Event.current.type == EventType.MouseMove)
            {
                Repaint();
            }
        }
Beispiel #3
0
        private void CopyNodes()
        {
            Graph copy = new Graph();

            copy.serializationData = this.m_Graph.serializationData;
            for (int i = 0; i < this.m_Selection.Count; i++)
            {
                copy.serializationData.Replace(this.m_Selection[i].id, System.Guid.NewGuid().ToString());
            }
            GraphUtility.Load(copy);
            Node[] toDelete = copy.nodes.Where(x => !this.m_Selection.Exists(y => x.id == y.id)).ToArray();
            GraphUtility.RemoveNodes(copy, toDelete.Cast <FlowNode>().ToArray());
            GraphUtility.Save(copy);
            this.m_Copy = copy.serializationData;
        }
        private void PasteNodes(Vector2 position)
        {
            if (!string.IsNullOrEmpty(this.m_Copy))
            {
                this.m_Selection.Clear();
                Graph graph = new Graph();
                graph.serializationData = this.m_Copy;
                GraphUtility.Load(graph);

                for (int i = 0; i < graph.nodes.Count; i++)
                {
                    Node node = graph.nodes[i];
                    if (position == Vector2.zero)
                    {
                        position = node.position + new Vector2(20, 15);
                    }
                    node.position += position - node.position;
                    this.m_Graph.nodes.Add(node);
                    this.m_Selection.Add((FlowNode)node);
                }
                GraphUtility.Save(this.m_Graph);
                PrefabUtility.RecordPrefabInstancePropertyModifications(this.m_Target);
            }
        }
 public void OnAfterDeserialize()
 {
     GraphUtility.Load(this);
 }
        protected override void ConnectNodes(FlowNode[] nodes, Vector2 offset)
        {
            Event currentEvent = Event.current;
            int   controlID    = GUIUtility.GetControlID(FocusType.Passive);

            switch (currentEvent.type)
            {
            case EventType.MouseDown:
                for (int i = 0; i < nodes.Length; i++)
                {
                    for (int j = 0; j < nodes[i].Ports.Count; j++)
                    {
                        Rect portRect = GetPortRect(nodes[i].Ports[j]);
                        if (portRect.Contains(Event.current.mousePosition))
                        {
                            m_ConnectingPort      = nodes[i].GetPort(j);
                            GUIUtility.hotControl = controlID;
                            currentEvent.Use();
                        }
                    }
                }
                break;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == controlID)
                {
                    bool connected = false;
                    for (int i = 0; i < nodes.Length; i++)
                    {
                        for (int j = 0; j < nodes[i].Ports.Count; j++)
                        {
                            Rect portRect = GetPortRect(nodes[i].Ports[j]);
                            if (portRect.Contains(currentEvent.mousePosition))
                            {
                                if (m_ConnectingPort.node == nodes[i])
                                {
                                    Debug.LogWarning("You can't connect to same node.");
                                    break;
                                }

                                if (m_ConnectingPort.direction == nodes[i].GetPort(j).direction)
                                {
                                    Debug.LogWarning("You can't connect two " + m_ConnectingPort.direction.ToString() + " ports.");
                                    break;
                                }
                                this.m_ConnectingPort.Connect(nodes[i].GetPort(j));
                                connected = true;
                                break;
                            }
                        }
                    }

                    if (!connected && this.m_ConnectingPort != null)
                    {
                        this.m_ConnectingPort.DisconnectAll();
                    }
                    GraphUtility.Save(this.m_Graph);
                    PrefabUtility.RecordPrefabInstancePropertyModifications(this.m_Target);
                    m_ConnectingPort      = null;
                    GUIUtility.hotControl = 0;
                    Event.current.Use();
                }
                break;
            }

            if (m_ConnectingPort != null && this.m_ConnectingPort.node != null)
            {
                DrawConnection(GetPortRect(this.m_ConnectingPort).center, currentEvent.mousePosition, ConnectionStyle.Line, new Color(0.506f, 0.62f, 0.702f, 1f));
                m_Host.Repaint();
            }
        }
        private void AddNode(Type type)
        {
            Node node = GraphUtility.AddNode(this.m_Graph, type);

            node.position = this.m_CreateNodePosition;
        }
        private void DrawPortFields(Rect rect, FlowNode node)
        {
            float labelWidth = GetLabelWidth(node);
            float fieldWidth = GetFieldWidth(node);

            for (int i = 0; i < node.Ports.Count; i++)
            {
                Port port = node.GetPort(i);
                if (port.direction == PortDirection.Input)
                {
                    Rect labelRect = new Rect(rect.x + NODE_CONTENT_OFFSET, rect.y + NODE_LINE_HEIGHT * i + GetHeaderRect(node, this.m_GraphOffset).height + (NODE_LINE_HEIGHT - NODE_FIELD_HEIGHT) * 0.5f, labelWidth, NODE_FIELD_HEIGHT);

                    if (port.Connections.Count == 0)
                    {
                        FieldInfo field = port.node.GetType().GetField(port.fieldName);
                        object    value = field.GetValue(port.node);
                        if (port.label)
                        {
                            GUI.Label(labelRect, ObjectNames.NicifyVariableName(port.fieldName), Styles.nodeText);
                        }

                        Rect fieldRect = new Rect(rect.x + NODE_CONTENT_OFFSET + (port.label?labelWidth + NODE_CONTENT_OFFSET:0f), rect.y + NODE_LINE_HEIGHT * i + GetHeaderRect(node, this.m_GraphOffset).height + (NODE_LINE_HEIGHT - NODE_FIELD_HEIGHT) * 0.5f, fieldWidth, NODE_FIELD_HEIGHT);
                        EditorGUI.BeginChangeCheck();
                        if (port.fieldType == typeof(float))
                        {
                            value = EditorGUI.FloatField(fieldRect, (float)value);
                        }
                        else if (port.fieldType == typeof(string))
                        {
                            value = EditorGUI.TextField(fieldRect, (string)value);
                        }
                        else if (port.fieldType == typeof(AnimationCurve))
                        {
                            value = EditorGUI.CurveField(fieldRect, (AnimationCurve)value);
                        }
                        if (EditorGUI.EndChangeCheck())
                        {
                            field.SetValue(port.node, value);
                            GraphUtility.Save(this.m_Graph);
                            PrefabUtility.RecordPrefabInstancePropertyModifications(this.m_Target);
                        }
                    }
                    else
                    {
                        GUI.Label(labelRect, ObjectNames.NicifyVariableName(port.fieldName), Styles.nodeText);
                    }
                }

                NodeStyleAttribute nodeStyle = node.GetType().GetCustomAttribute <NodeStyleAttribute>();
                if (nodeStyle == null)
                {
                    nodeStyle = new NodeStyleAttribute(true);
                }
                Texture2D icon = Resources.Load <Texture2D>(nodeStyle.iconPath);
                if (!nodeStyle.displayHeader && icon != null)
                {
                    GUI.Label(new Rect(rect.x + rect.width - NODE_CONTENT_OFFSET - icon.width, rect.y + rect.height * 0.5f - icon.height * 0.5f, icon.width, icon.height), icon);
                }


                if (Event.current.type == EventType.Repaint) // && port.drawPort)
                {
                    if (i > 0 && i < node.Ports.Count - 1)
                    {
                        Styles.seperator.Draw(new Rect(rect.x, rect.y + GetHeaderRect(node, this.m_GraphOffset).height + i * NODE_LINE_HEIGHT, !nodeStyle.displayHeader && icon != null ? GetContentWidth(node) + NODE_CONTENT_OFFSET * 1.6f:rect.width, 1f), false, false, false, false);
                    }
                }
            }
        }