private void ShowNodeContextMenu()
        {
            GenericMenu contextMenu     = new GenericMenu();
            bool        oneNodeSelected = Selection.objects.Length == 1 && Selection.activeObject is Node;
            Node        node            = oneNodeSelected ? (Node)Selection.activeObject : null;

            if (oneNodeSelected)
            {
                contextMenu.AddItem(new GUIContent(SEND_TO_FRONT_TEXT), false, () => Graph.SendNodeToFront(node));
                contextMenu.AddItem(new GUIContent(SEND_TO_BACK_TEXT), false, () => Graph.SendNodeToBack(node));
                contextMenu.AddItem(new GUIContent(SEND_FORWARD_TEXT), false, () => Graph.SendNodeForward(node));
                contextMenu.AddItem(new GUIContent(SEND_BACKWARD_TEXT), false, () => Graph.SendNodeBackward(node));
                contextMenu.AddSeparator(string.Empty);
                contextMenu.AddItem(new GUIContent(RENAME_TEXT), false, RenameSelectedNode);
            }

            contextMenu.AddItem(new GUIContent(DUPLICATE_TEXT), false, DuplicateSelected);
            contextMenu.AddItem(new GUIContent(REMOVE_TEXT), false, RemoveSelected);

            if (oneNodeSelected)
            {
                AddCustomContextMenuItems(contextMenu, node);
            }

            contextMenu.DropDown(new Rect(_mousePosition, Vector2.zero));
            NodeEditorUtilities.AutoSaveAssets();
        }
        private static IEnumerable <ScrObj> DuplicateNodesAndConnections(
            IEnumerable <Node> originalNodes, IEnumerable <Connection> originalConnections, NodeGraph target)
        {
            Dictionary <InputPort, InputPort>   inputs  = new Dictionary <InputPort, InputPort>();
            Dictionary <OutputPort, OutputPort> outputs = new Dictionary <OutputPort, OutputPort>();

            List <Node> duplicatedNodes = new List <Node>();

            foreach (Node original in originalNodes)
            {
                Node duplicate = DuplicateNode(original, target);
                duplicate.Position += NodePreferences.DUPLICATION_OFFSET;
                duplicatedNodes.Add(duplicate);

                //Cache ports locally.
                original.PortHandler.AttemptInputAction(inputNode =>
                                                        inputs[inputNode.InputPort] = duplicate.PortHandler.inputNode.InputPort);
                original.PortHandler.AttemptOutputAction(outputNode =>
                                                         outputs[outputNode.OutputPort] = duplicate.PortHandler.outputNode.OutputPort);

                if (original.PortHandler.multipleOutputNode == null)
                {
                    continue;
                }
                var mOutputsOriginal  = new List <OutputPort>(original.PortHandler.multipleOutputNode.GetOutputs());
                var mOutputsDuplicate = new List <OutputPort>(duplicate.PortHandler.multipleOutputNode.GetOutputs());
                for (int i = 0; i < mOutputsOriginal.Count; i++)
                {
                    outputs[mOutputsOriginal[i]] = mOutputsDuplicate[i];
                }
            }

            //Duplicate connections and match ports and reconnect
            List <Connection> duplicatedConnections = new List <Connection>();

            foreach (Connection original in originalConnections)
            {
                InputPort duplicatedInput;
                if (!inputs.TryGetValue(original.End, out duplicatedInput))
                {
                    continue;
                }
                OutputPort duplicatedOutput;
                if (!outputs.TryGetValue(original.Start, out duplicatedOutput))
                {
                    continue;
                }

                Connection connection = DuplicateConnection(original, target);
                if (!Connection.AttemptToConnect(connection, duplicatedInput, duplicatedOutput))
                {
                    continue;
                }

                duplicatedConnections.Add(connection);
            }

            NodeEditorUtilities.AutoSaveAssets();
            return(duplicatedNodes.Cast <ScrObj>().Concat(duplicatedConnections.Cast <ScrObj>()));
        }
        //private VariableInventoryEditor LVIEditor
        //{
        //    get
        //    {
        //        if (_localVariableInventoryEditor != null) return _localVariableInventoryEditor;

        //        if (Blackboard.CurrentLocalVariableInventory == null) return null;
        //        _localVariableInventoryEditor = VariableInventoryEditor.GetEditor(Blackboard.CurrentLocalVariableInventory);
        //        return _localVariableInventoryEditor;
        //    }
        //}

        protected override void OnFocus()
        {
            base.OnFocus();
            CurrentBlackboardEditorWindow = this;
            if (Blackboard != null)
            {
                NodeEditorUtilities.AutoSaveAssets();
            }
        }
        private Connection CreateConnection()
        {
            Connection connection = Graph.CreateAndAddConnection();

            //connection.name = "Connection";
            AssetDatabase.AddObjectToAsset(connection, Graph);
            NodeEditorUtilities.AutoSaveAssets();
            Repaint();

            return(connection);
        }
Ejemplo n.º 5
0
 public void Save()
 {
     if (AssetDatabase.Contains(_graph))
     {
         EditorUtility.SetDirty(_graph);
         NodeEditorUtilities.AutoSaveAssets();
     }
     else
     {
         SaveAs();
     }
 }
Ejemplo n.º 6
0
        private void SaveVariable()
        {
            _variable.name = _variableName;
            //variable.VariableInventory = _variableInventory;
            _variable.EnumType = _variableType;
            //variable.UncastValue = _variableValue;
            AssignValue(_variable);

            //AssetDatabase.AddObjectToAsset(variable, _variableInventory);
            NodeEditorUtilities.AutoSaveAssets();
            //Repaint();
        }
Ejemplo n.º 7
0
        private Value CreateValue()
        {
            Value value = ScriptableObject.CreateInstance <Value>();

            value.EnumType = _valueType;
            AssignValue(ref value);
            ConditionTarget.AddValue(value);

            AssetDatabase.AddObjectToAsset(value, SerializationTarget);
            NodeEditorUtilities.AutoSaveAssets();

            return(value);
            //Repaint();
        }
        private void AddVariable()
        {
            Variable variable = ScriptableObject.CreateInstance <Variable>();

            variable.name = _variableName;
            variable.VariableInventory = _variableInventory;
            variable.EnumType          = _variableType;
            //variable.UncastValue = _variableValue;
            AssignValue(variable);
            _variableInventory.AddVariable(variable);

            AssetDatabase.AddObjectToAsset(variable, _variableInventory);
            NodeEditorUtilities.AutoSaveAssets();
            //Repaint();
        }
        private void AddConnectionModifierToConnection(Connection connection, Type type)
        {
            if (!ReflectionUtilities.IsOfType(type, typeof(Instruction)))
            {
                return;
            }

            Instruction mod = connection.CreateAndAddModifier(type);

            mod.name = ObjectNames.NicifyVariableName(type.Name).Replace(" Modifier", "");

            AssetDatabase.AddObjectToAsset(mod, connection);
            NodeEditorUtilities.AutoSaveAssets();
            Repaint();
        }
Ejemplo n.º 10
0
        //public static void RepaintAll()
        //{
        //    NodeEditorWindow[] windows = Resources.FindObjectsOfTypeAll<NodeEditorWindow>();
        //    foreach (var window in windows) window.Repaint();
        //}

        protected override void OnFocus()
        {
            base.OnFocus();
            CurrentNodeEditorWindow = this;

            if (_graph == null)
            {
                return;
            }
            _graphEditor = NodeGraphEditor.GetEditor(_graph);
            //Blackboard.Instance.CurrentLocalVariableInventory = _graph.GetVariableInventory();
            if (_graphEditor != null)
            {
                NodeEditorUtilities.AutoSaveAssets();
            }
        }
        private static IEnumerable <Instruction> DuplicateConnectionModifiers(
            IEnumerable <Instruction> originalConnectionModifiers, Connection target = null)
        {
            List <Instruction> duplicates = new List <Instruction>();

            foreach (Instruction original in originalConnectionModifiers)
            {
                Connection  localTarget = target ?? original.Connection;
                Instruction mod         = DuplicateConnectionModifier(original, localTarget);
                duplicates.Add(mod);
            }

            NodeEditorUtilities.AutoSaveAssets();
            return(duplicates);
            //Repaint();
        }
        private void CreateNode(Type type, Vector2 position)
        {
            if (!ReflectionUtilities.IsOfType(type, typeof(Node)))
            {
                return;
            }

            Node node = Graph.CreateAndAddNode(type);

            node.Position = position;
            node.name     = ObjectNames.NicifyVariableName(type.Name);

            AssetDatabase.AddObjectToAsset(node, Graph);
            NodeEditorUtilities.AutoSaveAssets();
            Repaint();
        }
        private static IEnumerable <Node> DuplicateNodes(IEnumerable <Node> originalNodes, NodeGraph target = null)
        {
            List <Node> duplicates = new List <Node>();

            foreach (Node original in originalNodes)
            {
                NodeGraph localTarget = target ?? original.Graph;
                Node      duplicate   = DuplicateNode(original, localTarget);
                duplicate.Position += NodePreferences.DUPLICATION_OFFSET;
                duplicates.Add(duplicate);
            }

            NodeEditorUtilities.AutoSaveAssets();
            return(duplicates);
            //Repaint();
        }
        private void DuplicateSelected()
        {
            //Get selected objects
            Node[]        nodes       = GetSelected <Node>();
            Connection[]  connections = GetSelected <Connection>();
            Instruction[] modifiers   = GetSelected <Instruction>();
            ClearSelection();

            bool selectedNodes       = nodes.Length != 0;
            bool selectedConnections = connections.Length != 0;
            bool selectedMods        = modifiers.Length != 0;

            //Determine course of action
            if (selectedNodes && selectedConnections)
            {
                Select(DuplicateNodesAndConnections(nodes, connections, Graph).Cast <Object>(), false);

                //Remove already duplicated modifiers
                //TODO Might remove and/or structure later
                if (selectedMods)
                {
                    List <Instruction> allConnectionModifiers      = new List <Instruction>();
                    List <Instruction> leftoverConnectionModifiers = new List <Instruction>(modifiers);
                    foreach (Connection connection in connections)
                    {
                        allConnectionModifiers.AddRange(connection.GetInstructions());
                    }
                    allConnectionModifiers = allConnectionModifiers.Distinct().ToList();
                    leftoverConnectionModifiers.RemoveAll(allConnectionModifiers.Contains);
                    modifiers    = leftoverConnectionModifiers.ToArray();
                    selectedMods = modifiers.Length != 0;
                }
            }
            else if (selectedNodes)
            {
                Select(DuplicateNodes(nodes, Graph).Cast <Object>(), false);
            }

            if (selectedMods)
            {
                Select(DuplicateConnectionModifiers(modifiers).Cast <Object>(), true);
            }

            NodeEditorUtilities.AutoSaveAssets();
            Repaint();
        }
Ejemplo n.º 15
0
        public void SaveAs()
        {
            string path = EditorUtility.SaveFilePanelInProject("Save Node Graph", "NewNodeGraph", "asset", "");

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            NodeGraph existingGraph = AssetDatabase.LoadAssetAtPath <NodeGraph>(path);

            if (existingGraph != null)
            {
                AssetDatabase.DeleteAsset(path);
            }
            AssetDatabase.CreateAsset(_graph, path);
            EditorUtility.SetDirty(_graph);
            NodeEditorUtilities.AutoSaveAssets();
        }
Ejemplo n.º 16
0
        private void DrawButtons()
        {
            EditorGUILayout.BeginHorizontal();

            if (GUILayout.Button("Add +"))
            {
                Undo.RecordObject(Target, string.Format("Add Variable to {0} {1}", Target.name, Target.GetHashCode()));
                //ConditionNode._CreateDefaultCondition();
                //NodeEditorWindow.CurrentNodeEditorWindow.OpenSubWindow<CreateVariableWindow>(
                //    new Vector2(_cachedRect.x, _cachedRect.max.y), Target);
                Window.GetUtilityWindow <CreateVariableEditor>(Target);
                //EditorUtility.SetDirty(ConditionNode);
            }

            if (GUILayout.Button("Remove -"))
            {
                Variable var = Target.GetVariable(Target.VariableCount - 1);
                if (var != null)
                {
                    Undo.RecordObject(Target, string.Format("Remove Variable {2} from {0} {1}", Target.name, Target.GetHashCode(), var.name));
                    Target.RemoveVariable(var);
                    UnityEngine.Object.DestroyImmediate(var, true);
                    NodeEditorUtilities.AutoSaveAssets();
                }
                //EditorUtility.SetDirty(ConditionNode);
            }

            if (GUILayout.Button("Edit *"))
            {
                Variable var = Target.GetVariable(Target.VariableCount - 1);
                if (var != null)
                {
                    Undo.RecordObject(Target, string.Format("Edit Variable {2} from {0} {1}", Target.name, Target.GetHashCode(), var.name));
                    Window.GetUtilityWindow <EditVariableEditor>(var);
                    //OtherUtilities.AutoSaveAssets();
                }
            }

            EditorGUILayout.EndHorizontal();
        }
        private void MouseUp()
        {
            if (!_leftMouseButtonUsed)
            {
                if (_rightMouseButtonUsed || _middleMouseButtonUsed)
                {
                    ContextClick();
                }
                return;
            }
            if (IsDraggingPort)
            {
                //If the connection is valid, save it
                if (DraggedOutputTarget != null)
                {
                    //DraggedOutput.Connect(DraggedOutputTarget)
                    if (Graph.NodeCount != 0)
                    {
                        Connect(DraggedOutput, DraggedOutputTarget);
                    }
                    NodeEditor.UpdateCallback(DraggedOutputTarget.Node);
                    NodeEditor.UpdateCallback(DraggedOutput.Node);
                }

                //Release the dragged connection
                DraggedOutput       = null;
                DraggedOutputTarget = null;
                EditorUtility.SetDirty(Graph);
                NodeEditorUtilities.AutoSaveAssets();
            }
            else if (_currentActivity == Activity.Dragging)
            {
                Node[] nodes = GetSelected <Node>();
                foreach (Node node in nodes)
                {
                    EditorUtility.SetDirty(node);
                }
                NodeEditorUtilities.AutoSaveAssets();
            }
            else if (!IsHoveringConnectionModifier)
            {
                //If clicking outside the Con. Mod, release the field focus
                if (!IsPanning)
                {
                    EditorGUI.FocusTextInControl(null);
                }
                NodeEditorUtilities.AutoSaveAssets();
            }
            else if (!IsHoveringNode)
            {
                //If clicking outside the node, release the field focus
                if (!IsPanning)
                {
                    EditorGUI.FocusTextInControl(null);
                }
                NodeEditorUtilities.AutoSaveAssets();
            }

            if (_currentActivity == Activity.Holding && !(_cachedEvent.control || _cachedEvent.shift))
            {
                if (IsHoveringNode)
                {
                    Select(_hoveredNode, false);
                }
                else if (IsHoveringConnectionModifier)
                {
                    Select(_hoveredInstruction, false);
                }
            }

            Repaint();
            _currentActivity = Activity.Idle;
        }
        private void ShowPortContextMenu(Port port)
        {
            GenericMenu contextMenu = new GenericMenu();

            contextMenu.AddItem(new GUIContent(CLEAR_CONNECTIONS_TEXT), false, port.Disconnect);

            InputPort input = port as InputPort;

            if (input != null)
            {
                List <NodePortSelector> namedNodes   = new List <NodePortSelector>();
                List <NodePortSelector> unnamedNodes = new List <NodePortSelector>();

                for (int i = 0; i < input.ConnectionsCount; i++)
                {
                    Connection connection = input.GetConnection(i);
                    if (connection == null)
                    {
                        continue;
                    }
                    OutputPort output = connection.Start;
                    if (output == null)
                    {
                        continue;
                    }
                    Node node = output.Node;
                    if (node == null)
                    {
                        continue;
                    }

                    NodePortSelector nps = new NodePortSelector()
                    {
                        actualIndex = i, node = node, output = output
                    };
                    if (node.name == ObjectNames.NicifyVariableName(node.GetType().Name))
                    {
                        unnamedNodes.Add(nps);
                    }
                    else
                    {
                        namedNodes.Add(nps);
                    }
                }

                if (namedNodes.Count != 0 || unnamedNodes.Count != 0)
                {
                    contextMenu.AddSeparator(string.Empty);

                    //TODO: Test this
                    foreach (NodePortSelector namedNode in namedNodes)
                    {
                        NodePortSelector captured = namedNode;
                        contextMenu.AddItem(new GUIContent(string.Format("Select \"{0}\" port.", namedNode.node.name)),
                                            false,
                                            () =>
                        {
                            HoveredPort = captured.output;
                            DraggedPort = captured.output;
                        });
                    }

                    foreach (NodePortSelector unnamedNode in unnamedNodes)
                    {
                        NodePortSelector captured = unnamedNode;
                        contextMenu.AddItem(
                            new GUIContent(string.Format("Select \"{0} {1}\" port.",
                                                         unnamedNode.node.name, unnamedNode.actualIndex)),
                            false,
                            () =>
                        {
                            HoveredPort = captured.output;
                            DraggedPort = captured.output;
                        });
                    }
                }
            }

            AddCustomContextMenuItems(contextMenu, port);
            contextMenu.DropDown(new Rect(_mousePosition, Vector2.zero));
            NodeEditorUtilities.AutoSaveAssets();
        }