Beispiel #1
0
 /// <summary> Draws an input and an output port on the same line </summary>
 public static void PortPair(AnimationBaker.StateMachine.XNode.NodePort input, AnimationBaker.StateMachine.XNode.NodePort output)
 {
     GUILayout.BeginHorizontal();
     NodeEditorGUILayout.PortField(input, GUILayout.MinWidth(0));
     NodeEditorGUILayout.PortField(output, GUILayout.MinWidth(0));
     GUILayout.EndHorizontal();
 }
Beispiel #2
0
        /// <summary> Make a simple port field. </summary>
        public static void PortField(GUIContent label, AnimationBaker.StateMachine.XNode.NodePort port, params GUILayoutOption[] options)
        {
            if (port == null)
            {
                return;
            }
            if (options == null)
            {
                options = new GUILayoutOption[] { GUILayout.MinWidth(30) }
            }
            ;
            Rect       rect    = new Rect();
            GUIContent content = label != null ? label : new GUIContent(ObjectNames.NicifyVariableName(port.fieldName));

            // If property is an input, display a regular property field and put a port handle on the left side
            if (port.direction == AnimationBaker.StateMachine.XNode.NodePort.IO.Input)
            {
                // Display a label
                EditorGUILayout.LabelField(content, options);

                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 == AnimationBaker.StateMachine.XNode.NodePort.IO.Output)
            {
                // Display a label
                EditorGUILayout.LabelField(content, NodeEditorResources.OutputPort, options);

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

            if (NodeEditorWindow.nodeTint.ContainsKey(port.node.GetType()))
            {
                backgroundColor *= NodeEditorWindow.nodeTint[port.node.GetType()];
            }
            Color col = NodeEditorWindow.current.graphEditor.GetTypeColor(port.ValueType);

            DrawPortHandle(rect, backgroundColor, col);

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

            if (NodeEditor.portPositions.ContainsKey(port))
            {
                NodeEditor.portPositions[port] = portPos;
            }
            else
            {
                NodeEditor.portPositions.Add(port, portPos);
            }
        }
Beispiel #3
0
        /// <summary> Dublicate selected nodes and select the dublicates </summary>
        public void DublicateSelectedNodes()
        {
            UnityEngine.Object[] newNodes = new UnityEngine.Object[Selection.objects.Length];
            Dictionary <AnimationBaker.StateMachine.XNode.Node, AnimationBaker.StateMachine.XNode.Node> substitutes = new Dictionary <AnimationBaker.StateMachine.XNode.Node, AnimationBaker.StateMachine.XNode.Node>();

            for (int i = 0; i < Selection.objects.Length; i++)
            {
                if (Selection.objects[i] is AnimationBaker.StateMachine.XNode.Node)
                {
                    AnimationBaker.StateMachine.XNode.Node srcNode = Selection.objects[i] as AnimationBaker.StateMachine.XNode.Node;
                    if (srcNode.graph != graph)
                    {
                        continue;                         // ignore nodes selected in another graph
                    }
                    AnimationBaker.StateMachine.XNode.Node newNode = graphEditor.CopyNode(srcNode);
                    substitutes.Add(srcNode, newNode);
                    newNode.position = srcNode.position + new Vector2(30, 30);
                    newNodes[i]      = newNode;
                }
            }

            // Walk through the selected nodes again, recreate connections, using the new nodes
            for (int i = 0; i < Selection.objects.Length; i++)
            {
                if (Selection.objects[i] is AnimationBaker.StateMachine.XNode.Node)
                {
                    AnimationBaker.StateMachine.XNode.Node srcNode = Selection.objects[i] as AnimationBaker.StateMachine.XNode.Node;
                    if (srcNode.graph != graph)
                    {
                        continue;                         // ignore nodes selected in another graph
                    }
                    foreach (AnimationBaker.StateMachine.XNode.NodePort port in srcNode.Ports)
                    {
                        for (int c = 0; c < port.ConnectionCount; c++)
                        {
                            AnimationBaker.StateMachine.XNode.NodePort inputPort  = port.direction == AnimationBaker.StateMachine.XNode.NodePort.IO.Input ? port : port.GetConnectionPort(c);
                            AnimationBaker.StateMachine.XNode.NodePort outputPort = port.direction == AnimationBaker.StateMachine.XNode.NodePort.IO.Output ? port : port.GetConnectionPort(c);

                            if (substitutes.ContainsKey(inputPort.node) && substitutes.ContainsKey(outputPort.node))
                            {
                                AnimationBaker.StateMachine.XNode.Node newNodeIn  = substitutes[inputPort.node];
                                AnimationBaker.StateMachine.XNode.Node newNodeOut = substitutes[outputPort.node];
                                newNodeIn.UpdateStaticPorts();
                                newNodeOut.UpdateStaticPorts();
                                inputPort  = newNodeIn.GetInputPort(inputPort.fieldName);
                                outputPort = newNodeOut.GetOutputPort(outputPort.fieldName);
                            }
                            if (!inputPort.IsConnectedToPort(outputPort))
                            {
                                inputPort.Connect(outputPort);
                            }
                        }
                    }
                }
            }
            Selection.objects = newNodes;
        }
Beispiel #4
0
 /// <summary> Make a field for a serialized property. Automatically displays relevant node port. </summary>
 public static void PropertyField(SerializedProperty property, GUIContent label, bool includeChildren = true, params GUILayoutOption[] options)
 {
     if (property == null)
     {
         throw new NullReferenceException();
     }
     AnimationBaker.StateMachine.XNode.Node     node = property.serializedObject.targetObject as AnimationBaker.StateMachine.XNode.Node;
     AnimationBaker.StateMachine.XNode.NodePort port = node.GetPort(property.name);
     PropertyField(property, label, port, includeChildren);
 }
Beispiel #5
0
        /// <summary> Show right-click context menu for hovered port </summary>
        void ShowPortContextMenu(AnimationBaker.StateMachine.XNode.NodePort hoveredPort)
        {
            GenericMenu contextMenu = new GenericMenu();

            contextMenu.AddItem(new GUIContent("Clear Connections"), false, () => hoveredPort.ClearConnections());
            contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
            if (NodeEditorPreferences.GetSettings().autoSave)
            {
                AssetDatabase.SaveAssets();
            }
        }
Beispiel #6
0
        /// <summary> Make a simple port field. </summary>
        public static void VerticalPortField(GUIContent label, AnimationBaker.StateMachine.XNode.NodePort port, params GUILayoutOption[] options)
        {
            if (port == null)
            {
                return;
            }
            if (options == null)
            {
                options = new GUILayoutOption[] { GUILayout.MinWidth(30) }
            }
            ;
            Rect rect = GUILayoutUtility.GetLastRect();

            if (port.direction == AnimationBaker.StateMachine.XNode.NodePort.IO.Input)
            {
                rect.position += new Vector2((rect.width / 2) - 8, -7);
            }
            else if (port.direction == AnimationBaker.StateMachine.XNode.NodePort.IO.Output)
            {
                rect.position += new Vector2((rect.width / 2) - 8, rect.height);
            }

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

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

            if (NodeEditorWindow.nodeTint.ContainsKey(port.node.GetType()))
            {
                backgroundColor *= NodeEditorWindow.nodeTint[port.node.GetType()];
            }
            Color col = NodeEditorWindow.current.graphEditor.GetTypeColor(port.ValueType);

            DrawPortHandle(rect, backgroundColor, col);

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

            if (NodeEditor.portPositions.ContainsKey(port))
            {
                NodeEditor.portPositions[port] = portPos;
            }
            else
            {
                NodeEditor.portPositions.Add(port, portPos);
            }
        }
Beispiel #7
0
        /// <summary> Draws all connections </summary>
        public void DrawConnections()
        {
            Vector2 mousePos = Event.current.mousePosition;
            List <RerouteReference> selection = preBoxSelectionReroute != null ? new List <RerouteReference>(preBoxSelectionReroute) : new List <RerouteReference>();

            hoveredReroute = new RerouteReference();

            Color col = GUI.color;

            foreach (AnimationBaker.StateMachine.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;
                }

                // Draw full connections and output > reroute
                foreach (AnimationBaker.StateMachine.XNode.NodePort output in node.Outputs)
                {
                    //Needs cleanup. Null checks are ugly
                    if (!portConnectionPoints.ContainsKey(output))
                    {
                        continue;
                    }

                    Color connectionColor = Color.red;

                    for (int k = 0; k < output.ConnectionCount; k++)
                    {
                        AnimationBaker.StateMachine.XNode.NodePort input = output.GetConnectionPort(k);

                        var connection = output.Connections[k];
                        if (graph.isPlaying)
                        {
                            if (!connection.Cleared)
                            {
                                connectionColor = Color.grey;
                            }
                            else
                            {
                                connectionColor = Color.green;
                            }
                        }

                        // var connection = output.GetConnection(input);

                        // if (connection != null)
                        // {
                        //     // Debug.Log(connection.rules.Count);
                        // }

                        // 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.IsConnectedToPort(output))
                        {
                            input.Connect(output);
                        }
                        if (!_portConnectionPoints.ContainsKey(input))
                        {
                            continue;
                        }

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

                        // 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);

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

                            GUI.color = connectionColor;
                            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;
            }
        }
Beispiel #8
0
        /// <summary> Make a field for a serialized property. Manual node port override. </summary>
        public static void PropertyField(SerializedProperty property, GUIContent label, AnimationBaker.StateMachine.XNode.NodePort port, bool includeChildren = true, params GUILayoutOption[] options)
        {
            if (property == null)
            {
                throw new NullReferenceException();
            }

            // If property is not a port, display a regular property field
            if (port == null)
            {
                EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
            }
            else
            {
                Rect 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 == AnimationBaker.StateMachine.XNode.NodePort.IO.Input)
                {
                    // Get data from [Input] attribute
                    AnimationBaker.StateMachine.XNode.Node.ShowBackingValue showBacking = AnimationBaker.StateMachine.XNode.Node.ShowBackingValue.Unconnected;
                    AnimationBaker.StateMachine.XNode.Node.InputAttribute   inputAttribute;
                    if (NodeEditorUtilities.GetAttrib(port.node.GetType(), property.name, out inputAttribute))
                    {
                        showBacking = inputAttribute.backingValue;
                    }

                    switch (showBacking)
                    {
                    case AnimationBaker.StateMachine.XNode.Node.ShowBackingValue.Unconnected:
                        // Display a label if port is connected
                        if (port.IsConnected)
                        {
                            EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName));
                        }
                        // Display an editable property field if port is not connected
                        else
                        {
                            EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
                        }
                        break;

                    case AnimationBaker.StateMachine.XNode.Node.ShowBackingValue.Never:
                        // Display a label
                        EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName));
                        break;

                    case AnimationBaker.StateMachine.XNode.Node.ShowBackingValue.Always:
                        // Display an editable property field
                        EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
                        break;
                    }

                    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 == AnimationBaker.StateMachine.XNode.NodePort.IO.Output)
                {
                    // Get data from [Output] attribute
                    AnimationBaker.StateMachine.XNode.Node.ShowBackingValue showBacking = AnimationBaker.StateMachine.XNode.Node.ShowBackingValue.Unconnected;
                    AnimationBaker.StateMachine.XNode.Node.OutputAttribute  outputAttribute;
                    if (NodeEditorUtilities.GetAttrib(port.node.GetType(), property.name, out outputAttribute))
                    {
                        showBacking = outputAttribute.backingValue;
                    }

                    switch (showBacking)
                    {
                    case AnimationBaker.StateMachine.XNode.Node.ShowBackingValue.Unconnected:
                        // Display a label if port is connected
                        if (port.IsConnected)
                        {
                            EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName), NodeEditorResources.OutputPort, GUILayout.MinWidth(30));
                        }
                        // Display an editable property field if port is not connected
                        else
                        {
                            EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
                        }
                        break;

                    case AnimationBaker.StateMachine.XNode.Node.ShowBackingValue.Never:
                        // Display a label
                        EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName), NodeEditorResources.OutputPort, GUILayout.MinWidth(30));
                        break;

                    case AnimationBaker.StateMachine.XNode.Node.ShowBackingValue.Always:
                        // Display an editable property field
                        EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
                        break;
                    }

                    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);
                if (NodeEditorWindow.nodeTint.ContainsKey(port.node.GetType()))
                {
                    backgroundColor *= NodeEditorWindow.nodeTint[port.node.GetType()];
                }
                Color col = NodeEditorWindow.current.graphEditor.GetTypeColor(port.ValueType);
                DrawPortHandle(rect, backgroundColor, col);

                // Register the handle position
                Vector2 portPos = rect.center;
                if (NodeEditor.portPositions.ContainsKey(port))
                {
                    NodeEditor.portPositions[port] = portPos;
                }
                else
                {
                    NodeEditor.portPositions.Add(port, portPos);
                }
            }
        }
Beispiel #9
0
 /// <summary> Make a field for a serialized property. Manual node port override. </summary>
 public static void PropertyField(SerializedProperty property, AnimationBaker.StateMachine.XNode.NodePort port, bool includeChildren = true, params GUILayoutOption[] options)
 {
     PropertyField(property, null, port, includeChildren, options);
 }
Beispiel #10
0
        /// <summary> Draw an editable list of instance ports. Port names are named as "[fieldName] [index]" </summary>
        /// <param name="fieldName">Supply a list for editable values</param>
        /// <param name="type">Value type of added instance ports</param>
        /// <param name="serializedObject">The serializedObject of the node</param>
        /// <param name="connectionType">Connection type of added instance ports</param>
        public static void InstancePortList(string fieldName, Type type, SerializedObject serializedObject, AnimationBaker.StateMachine.XNode.Node.ConnectionType connectionType = AnimationBaker.StateMachine.XNode.Node.ConnectionType.Multiple)
        {
            AnimationBaker.StateMachine.XNode.Node node = serializedObject.targetObject as AnimationBaker.StateMachine.XNode.Node;
            SerializedProperty arrayData = serializedObject.FindProperty(fieldName);
            bool hasArrayData            = arrayData != null && arrayData.isArray;
            int  arraySize = hasArrayData ? arrayData.arraySize : 0;

            List <AnimationBaker.StateMachine.XNode.NodePort> instancePorts = node.InstancePorts.Where(x => x.fieldName.StartsWith(fieldName)).OrderBy(x => x.fieldName).ToList();

            for (int i = 0; i < instancePorts.Count(); i++)
            {
                GUILayout.BeginHorizontal();
                if (GUILayout.Button("-", GUILayout.Width(30)))
                {
                    // Clear the removed ports connections
                    instancePorts[i].ClearConnections();
                    // Move following connections one step up to replace the missing connection
                    for (int k = i + 1; k < instancePorts.Count(); k++)
                    {
                        for (int j = 0; j < instancePorts[k].ConnectionCount; j++)
                        {
                            AnimationBaker.StateMachine.XNode.NodePort other = instancePorts[k].GetConnectionPort(j);
                            instancePorts[k].DisconnectPort(other);
                            instancePorts[k - 1].Connect(other);
                        }
                    }
                    // Remove the last instance port, to avoid messing up the indexing
                    node.RemoveInstancePort(instancePorts[instancePorts.Count() - 1].fieldName);
                    serializedObject.Update();
                    // EditorUtility.SetDirty(node);
                    node.IsDirty = true;
                    if (hasArrayData)
                    {
                        arrayData.DeleteArrayElementAtIndex(i);
                        arraySize--;
                    }
                    i--;
                }
                else
                {
                    if (hasArrayData)
                    {
                        if (i < arraySize)
                        {
                            SerializedProperty itemData = arrayData.GetArrayElementAtIndex(i);
                            if (itemData != null)
                            {
                                EditorGUILayout.PropertyField(itemData, new GUIContent(ObjectNames.NicifyVariableName(fieldName) + " " + i));
                            }
                            else
                            {
                                EditorGUILayout.LabelField("[Missing array data]");
                            }
                        }
                        else
                        {
                            EditorGUILayout.LabelField("[Out of bounds]");
                        }
                    }
                    else
                    {
                        EditorGUILayout.LabelField(instancePorts[i].fieldName);
                    }
                    NodeEditorGUILayout.PortField(new GUIContent(), node.GetPort(instancePorts[i].fieldName), GUILayout.Width(-4));
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.BeginHorizontal();
            EditorGUILayout.Space();
            if (GUILayout.Button("+", GUILayout.Width(30)))
            {
                string newName = fieldName + " 0";
                int    i       = 0;
                while (node.HasPort(newName))
                {
                    newName = fieldName + " " + (++i);
                }

                instancePorts.Add(node.AddInstanceOutput(type, connectionType, newName));
                serializedObject.Update();
                // EditorUtility.SetDirty(node);
                node.IsDirty = true;
                if (hasArrayData)
                {
                    arrayData.InsertArrayElementAtIndex(arraySize);
                }
            }
            GUILayout.EndHorizontal();
        }
Beispiel #11
0
 /// <summary> Draws an input and an output port on the same line </summary>
 public static void VerticalPortPair(AnimationBaker.StateMachine.XNode.NodePort input, AnimationBaker.StateMachine.XNode.NodePort output)
 {
     NodeEditorGUILayout.VerticalPortField(input, GUILayout.MinWidth(0));
     NodeEditorGUILayout.VerticalPortField(output, GUILayout.MinWidth(0));
 }
Beispiel #12
0
 /// <summary> Make a simple port field. </summary>
 public static void VerticalPortField(AnimationBaker.StateMachine.XNode.NodePort port, params GUILayoutOption[] options)
 {
     VerticalPortField(null, port, options);
 }
Beispiel #13
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.IsConnectedToPort(hoveredPort))
                            {
                                draggedOutputTarget = hoveredPort;
                            }
                        }
                        else
                        {
                            draggedOutputTarget = null;
                        }
                        Repaint();
                    }
                    else if (currentActivity == NodeActivity.HoldNode)
                    {
                        RecalculateDragOffsets(e);
                        currentActivity = NodeActivity.DragNode;
                        Repaint();
                    }
                    if (currentActivity == NodeActivity.DragNode)
                    {
                        // Holding ctrl inverts grid snap
                        bool gridSnap = NodeEditorPreferences.GetSettings().gridSnap;
                        if (e.control)
                        {
                            gridSnap = !gridSnap;
                        }

                        Vector2 mousePos = WindowToGridPosition(e.mousePosition);
                        // Move selected nodes with offset
                        for (int i = 0; i < Selection.objects.Length; i++)
                        {
                            if (Selection.objects[i] is AnimationBaker.StateMachine.XNode.Node)
                            {
                                AnimationBaker.StateMachine.XNode.Node node = Selection.objects[i] as AnimationBaker.StateMachine.XNode.Node;
                                node.position = mousePos + dragOffset[i];
                                if (gridSnap)
                                {
                                    node.position.x = (Mathf.Round((node.position.x + 8) / 16) * 16) - 8;
                                    node.position.y = (Mathf.Round((node.position.y + 8) / 16) * 16) - 8;
                                }
                            }
                        }
                        // Move selected reroutes with offset
                        for (int i = 0; i < selectedReroutes.Count; i++)
                        {
                            Vector2 pos = mousePos + dragOffset[Selection.objects.Length + i];
                            pos.x -= 8;
                            pos.y -= 8;
                            if (gridSnap)
                            {
                                pos.x = (Mathf.Round((pos.x + 8) / 16) * 16);
                                pos.y = (Mathf.Round((pos.y + 8) / 16) * 16);
                            }
                            selectedReroutes[i].SetPoint(pos);
                        }
                        Repaint();
                    }
                    else if (currentActivity == NodeActivity.HoldGrid)
                    {
                        currentActivity        = NodeActivity.DragGrid;
                        preBoxSelection        = Selection.objects;
                        preBoxSelectionReroute = selectedReroutes.ToArray();
                        dragBoxStart           = WindowToGridPosition(e.mousePosition);
                        Repaint();
                    }
                    else if (currentActivity == NodeActivity.DragGrid)
                    {
                        Vector2 boxStartPos = GridToWindowPosition(dragBoxStart);
                        Vector2 boxSize     = e.mousePosition - boxStartPos;
                        if (boxSize.x < 0)
                        {
                            boxStartPos.x += boxSize.x; boxSize.x = Mathf.Abs(boxSize.x);
                        }
                        if (boxSize.y < 0)
                        {
                            boxStartPos.y += boxSize.y; boxSize.y = Mathf.Abs(boxSize.y);
                        }
                        selectionBox = new Rect(boxStartPos, boxSize);
                        Repaint();
                    }
                }
                else if (e.button == 1 || e.button == 2)
                {
                    Vector2 tempOffset = panOffset;
                    tempOffset += e.delta * zoom;
                    // Round value to increase crispyness of UI text
                    tempOffset.x       = Mathf.Round(tempOffset.x);
                    tempOffset.y       = Mathf.Round(tempOffset.y);
                    panOffset          = tempOffset;
                    isPanning          = true;
                    graph.dragPosition = panOffset;
                }
                break;

            case EventType.MouseDown:
                Repaint();
                if (e.button == 0)
                {
                    draggedOutputReroutes.Clear();

                    if (IsHoveringPort)
                    {
                        if (hoveredPort.IsOutput)
                        {
                            draggedOutput = hoveredPort;
                        }
                        else
                        {
                            hoveredPort.VerifyConnections();
                            if (hoveredPort.IsConnected)
                            {
                                AnimationBaker.StateMachine.XNode.Node     node   = hoveredPort.node;
                                AnimationBaker.StateMachine.XNode.NodePort output = hoveredPort.Connection;
                                int outputConnectionIndex = output.GetConnectionPortIndex(hoveredPort);
                                draggedOutputReroutes = output.GetReroutePoints(outputConnectionIndex);
                                hoveredPort.DisconnectPort(output);
                                draggedOutput       = output;
                                draggedOutputTarget = hoveredPort;
                                if (NodeEditor.onUpdateNode != null)
                                {
                                    NodeEditor.onUpdateNode(node);
                                }
                            }
                        }
                    }
                    else if (IsHoveringNode && IsHoveringTitle(hoveredNode))
                    {
                        // If mousedown on node header, select or deselect
                        if (!Selection.Contains(hoveredNode))
                        {
                            SelectNode(hoveredNode, e.control || e.shift);
                            if (!e.control && !e.shift)
                            {
                                selectedReroutes.Clear();
                            }
                        }
                        else if (e.control || e.shift)
                        {
                            DeselectNode(hoveredNode);
                        }
                        e.Use();
                        currentActivity = NodeActivity.HoldNode;
                    }
                    else if (IsHoveringReroute)
                    {
                        // If reroute isn't selected
                        if (!selectedReroutes.Contains(hoveredReroute))
                        {
                            // Add it
                            if (e.control || e.shift)
                            {
                                selectedReroutes.Add(hoveredReroute);
                            }
                            // Select it
                            else
                            {
                                selectedReroutes = new List <RerouteReference>()
                                {
                                    hoveredReroute
                                };
                                Selection.activeObject = null;
                            }
                        }
                        // Deselect
                        else if (e.control || e.shift)
                        {
                            selectedReroutes.Remove(hoveredReroute);
                        }
                        e.Use();
                        currentActivity = NodeActivity.HoldNode;
                    }
                    // If mousedown on grid background, deselect all
                    else if (!IsHoveringNode)
                    {
                        currentActivity = NodeActivity.HoldGrid;
                        if (!e.control && !e.shift)
                        {
                            selectedReroutes.Clear();
                            Selection.activeObject = null;
                            if (Selection.activeObject != graph)
                            {
                                Selection.activeObject = graph;
                            }
                        }
                    }
                }
                break;

            case EventType.MouseUp:
                if (e.button == 0)
                {
                    //Port drag release
                    if (IsDraggingPort)
                    {
                        //If connection is valid, save it
                        if (draggedOutputTarget != null)
                        {
                            AnimationBaker.StateMachine.XNode.Node node = draggedOutputTarget.node;
                            if (graph.nodes.Count != 0)
                            {
                                draggedOutput.Connect(draggedOutputTarget);
                            }

                            // ConnectionIndex can be -1 if the connection is removed instantly after creation
                            int connectionIndex = draggedOutput.GetConnectionPortIndex(draggedOutputTarget);
                            if (connectionIndex != -1)
                            {
                                draggedOutput.GetReroutePoints(connectionIndex).AddRange(draggedOutputReroutes);
                                if (NodeEditor.onUpdateNode != null)
                                {
                                    NodeEditor.onUpdateNode(node);
                                }
                                // EditorUtility.SetDirty(graph);
                                graph.IsDirty = true;
                            }
                        }
                        //Release dragged connection
                        draggedOutput       = null;
                        draggedOutputTarget = null;
                        // EditorUtility.SetDirty(graph);
                        graph.IsDirty = true;
                        if (NodeEditorPreferences.GetSettings().autoSave)
                        {
                            AssetDatabase.SaveAssets();
                        }
                    }
                    else if (currentActivity == NodeActivity.DragNode)
                    {
                        IEnumerable <AnimationBaker.StateMachine.XNode.Node> nodes = Selection.objects.Where(x => x is AnimationBaker.StateMachine.XNode.Node).Select(x => x as AnimationBaker.StateMachine.XNode.Node);
                        foreach (AnimationBaker.StateMachine.XNode.Node node in nodes)
                        {
                            node.IsDirty = true;
                        }
                        if (NodeEditorPreferences.GetSettings().autoSave)
                        {
                            AssetDatabase.SaveAssets();
                        }
                    }
                    else if (!IsHoveringNode)
                    {
                        // If click outside node, release field focus
                        if (!isPanning)
                        {
                            EditorGUI.FocusTextInControl(null);
                        }
                        if (NodeEditorPreferences.GetSettings().autoSave)
                        {
                            AssetDatabase.SaveAssets();
                        }
                    }

                    // If click node header, select it.
                    if (currentActivity == NodeActivity.HoldNode && !(e.control || e.shift))
                    {
                        selectedReroutes.Clear();
                        SelectNode(hoveredNode, false);
                    }

                    // If click reroute, select it.
                    if (IsHoveringReroute && !(e.control || e.shift))
                    {
                        selectedReroutes = new List <RerouteReference>()
                        {
                            hoveredReroute
                        };
                        Selection.activeObject = null;
                    }

                    Repaint();
                    currentActivity = NodeActivity.Idle;
                }
                else if (e.button == 1 || e.button == 2)
                {
                    if (!isPanning)
                    {
                        if (IsDraggingPort)
                        {
                            draggedOutputReroutes.Add(WindowToGridPosition(e.mousePosition));
                        }
                        else if (currentActivity == NodeActivity.DragNode && Selection.activeObject == null && selectedReroutes.Count == 1)
                        {
                            selectedReroutes[0].InsertPoint(selectedReroutes[0].GetPoint());
                            selectedReroutes[0] = new RerouteReference(selectedReroutes[0].port, selectedReroutes[0].connectionIndex, selectedReroutes[0].pointIndex + 1);
                        }
                        // else if (IsHoveringReroute)
                        // {
                        //     ShowRerouteContextMenu(hoveredReroute);
                        // }
                        // else if (IsHoveringPort)
                        // {
                        //     ShowPortContextMenu(hoveredPort);
                        // }
                        // else if (IsHoveringNode && IsHoveringTitle(hoveredNode))
                        // {
                        //     if (!Selection.Contains(hoveredNode)) SelectNode(hoveredNode, false);
                        //     ShowNodeContextMenu();
                        // }
                        // else if (!IsHoveringNode)
                        // {
                        //     ShowGraphContextMenu();
                        // }
                    }
                    isPanning = false;
                }
                break;

            case EventType.KeyDown:
                if (EditorGUIUtility.editingTextField)
                {
                    break;
                }
                else if (e.keyCode == KeyCode.F)
                {
                    Home();
                }
                if (SystemInfo.operatingSystemFamily == OperatingSystemFamily.MacOSX)
                {
                    if (e.keyCode == KeyCode.Return)
                    {
                        RenameSelectedNode();
                    }
                }
                else
                {
                    if (e.keyCode == KeyCode.F2)
                    {
                        RenameSelectedNode();
                    }
                }
                break;

            case EventType.ValidateCommand:
                if (e.commandName == "SoftDelete")
                {
                    RemoveSelectedNodes();
                }
                else if (e.commandName == "Duplicate")
                {
                    DublicateSelectedNodes();
                }
                Repaint();
                break;

            case EventType.Ignore:
                // If release mouse outside window
                if (e.rawType == EventType.MouseUp && currentActivity == NodeActivity.DragGrid)
                {
                    Repaint();
                    currentActivity = NodeActivity.Idle;
                }
                break;
            }
        }
Beispiel #14
0
 public RerouteReference(AnimationBaker.StateMachine.XNode.NodePort port, int connectionIndex, int pointIndex)
 {
     this.port            = port;
     this.connectionIndex = connectionIndex;
     this.pointIndex      = pointIndex;
 }