/// <summary>
        /// Handler for the UndoRedoCommand.
        /// </summary>
        /// <param name="state">The state.</param>
        /// <param name="command">The command.</param>
        public static void UndoRedoCommandHandler(GraphToolState state, UndoRedoCommand command)
        {
            UndoRedoCommand.DefaultCommandHandler(state, command);
            var graphModel = state.WindowState.GraphModel;

            graphModel?.UndoRedoPerformed();
        }
        internal static void ResetSearcherSizes(this GraphToolState state)
        {
            var sizes = state.GetSizes();

            sizes.Clear();
            state.SaveSizes(sizes);
        }
예제 #3
0
        void UpdateBreadcrumbMenu(GraphToolState state)
        {
            bool isEnabled = state.WindowState.GraphModel != null;

            if (!isEnabled)
            {
                m_Breadcrumb.style.display = DisplayStyle.None;
                return;
            }
            m_Breadcrumb.style.display = StyleKeyword.Null;

            var i           = 0;
            var graphModels = state.WindowState.SubGraphStack;

            for (; i < graphModels.Count; i++)
            {
                var label = GetBreadcrumbLabel(state, i);
                m_Breadcrumb.CreateOrUpdateItem(i, label, BreadcrumbClickedEvent);
            }

            var newCurrentGraph = GetBreadcrumbLabel(state, -1);

            if (newCurrentGraph != null)
            {
                m_Breadcrumb.CreateOrUpdateItem(i, newCurrentGraph, BreadcrumbClickedEvent);
                i++;
            }

            m_Breadcrumb.TrimItems(i);
        }
예제 #4
0
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, ChangeVariableTypeCommand command)
        {
            var graphModel = graphToolState.GraphViewState.GraphModel;

            if (command.Type.IsValid)
            {
                graphToolState.PushUndo(command);

                using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
                {
                    if (command.VariableDeclarationModel.DataType != command.Type)
                    {
                        command.VariableDeclarationModel.CreateInitializationValue();
                    }

                    command.VariableDeclarationModel.DataType = command.Type;

                    var variableReferences = graphModel.FindReferencesInGraph <IVariableNodeModel>(command.VariableDeclarationModel).ToList();
                    foreach (var usage in variableReferences)
                    {
                        usage.UpdateTypeFromDeclaration();
                    }

                    graphUpdater.MarkChanged(variableReferences);
                    graphUpdater.MarkChanged(command.VariableDeclarationModel);
                }
            }
        }
예제 #5
0
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, CreateOppositePortalCommand command)
        {
            if (command.Models == null)
            {
                return;
            }

            var portalsToOpen = command.Models.Where(p => p.CanCreateOppositePortal()).ToList();

            if (!portalsToOpen.Any())
            {
                return;
            }

            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                foreach (var portalModel in portalsToOpen)
                {
                    var newPortal = graphToolState.GraphViewState.GraphModel.CreateOppositePortal(portalModel);
                    graphUpdater.MarkNew(newPortal);
                }
            }
        }
예제 #6
0
        static void ListenToSearcherSize(GraphToolState graphToolState, string usage, EditorWindow existingWindow = null)
        {
            // This is a retro engineering of the searcher to get changes in the window size and splitter position
            var searcherWindow  = existingWindow != null ? existingWindow : EditorWindow.GetWindow <Searcher.SearcherWindow>();
            var searcherResizer = searcherWindow.rootVisualElement.Q("windowResizer");
            var rightPanel      = searcherWindow.rootVisualElement.Q("windowDetailsVisualContainer");
            var leftPanel       = searcherWindow.rootVisualElement.Q("searcherVisualContainer");

            if (searcherResizer != null)
            {
                EventCallback <GeometryChangedEvent> callback = evt =>
                {
                    float ratio = 1.0f;
                    if (rightPanel != null && leftPanel != null)
                    {
                        ratio = rightPanel.resolvedStyle.flexGrow / leftPanel.resolvedStyle.flexGrow;
                    }

                    // PF FIXME Use command?
                    graphToolState.SetSearcherSize(usage ?? "", searcherWindow.position.size, ratio);
                };

                searcherWindow.rootVisualElement.RegisterCallback(callback);
                leftPanel?.RegisterCallback(callback);
            }
        }
예제 #7
0
        public static void ShowValues(GraphToolState graphToolState, string title, IEnumerable <string> values, Vector2 position,
                                      Action <string, int> callback)
        {
            var searcherSize = graphToolState.GetSearcherSize(Usage.k_Values);

            position += EditorWindow.focusedWindow.position.position;
            var rect = new Rect(position, searcherSize.Size);

            var items    = values.Select(v => new SearcherItem(v)).ToList();
            var database = new SearcherDatabase(items);
            var adapter  = new SimpleSearcherAdapter(title);

            adapter.SetInitialSplitterDetailRatio(searcherSize.RightLeftRatio);
            var searcher = new Searcher.Searcher(database, adapter);

            Searcher.SearcherWindow.Show(EditorWindow.focusedWindow, searcher, item =>
            {
                if (item == null)
                {
                    return(false);
                }

                callback(item.Name, item.Id);
                return(true);
            }, null, rect);
            ListenToSearcherSize(graphToolState, Usage.k_Values);
        }
예제 #8
0
        static void CheckGraphIntegrity(GraphToolState graphToolState)
        {
            var graphModel = graphToolState.WindowState.GraphModel;

            if (graphModel == null)
            {
                return;
            }

            var invalidNodeCount   = graphModel.NodeModels.Count(n => n == null);
            var invalidEdgeCount   = graphModel.EdgeModels.Count(n => n == null);
            var invalidStickyCount = graphToolState.WindowState.GraphModel.StickyNoteModels.Count(n => n == null);

            var countMessage = new StringBuilder();

            countMessage.Append(invalidNodeCount == 0 ? string.Empty : $"{invalidNodeCount} invalid node(s) found.\n");
            countMessage.Append(invalidEdgeCount == 0 ? string.Empty : $"{invalidEdgeCount} invalid edge(s) found.\n");
            countMessage.Append(invalidStickyCount == 0 ? string.Empty : $"{invalidStickyCount} invalid sticky note(s) found.\n");

            if (countMessage.ToString() != string.Empty)
            {
                if (EditorUtility.DisplayDialog("Invalid graph",
                                                $"Invalid elements found:\n{countMessage}\n" +
                                                $"Click the Clean button to remove all the invalid elements from the graph.",
                                                "Clean",
                                                "Cancel"))
                {
                    graphModel.Repair();
                }
            }
        }
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, MoveElementsCommand command)
        {
            if (command.Models == null || command.Value == Vector2.zero)
            {
                return;
            }

            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                var movingNodes = command.Models.OfType <INodeModel>().ToList();

                foreach (var movable in command.Models

                         // Only move an edge if it is connected on both ends to a moving node.
                         .Where(m => !(m is IEditableEdge e) ||
                                movingNodes.Contains(e.FromPort.NodeModel) &&
                                movingNodes.Contains(e.ToPort.NodeModel)))
                {
                    movable.Move(command.Value);
                }

                graphUpdater.MarkChanged(command.Models.OfType <IGraphElementModel>());
            }
        }
 static void SaveSizes(this GraphToolState state, SerializedValueDictionary <string, SearcherSize> sizes)
 {
     if (sizes != null)
     {
         var valueString = JsonUtility.ToJson(sizes);
         state.Preferences.SetString(StringPref.SearcherSize, valueString);
     }
 }
예제 #11
0
        public static void ShowVariableTypes(Stencil stencil, GraphToolState graphToolState, Vector2 position, Action <TypeHandle, int> callback)
        {
            var databases = stencil.GetSearcherDatabaseProvider()?.GetVariableTypesSearcherDatabases();

            if (databases != null)
            {
                ShowTypes(graphToolState, databases, position, callback);
            }
        }
예제 #12
0
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, CollapseVariableInBlackboard command)
        {
            graphToolState.PushUndo(command);

            using (var bbUpdater = graphToolState.BlackboardViewState.UpdateScope)
            {
                bbUpdater.SetVariableDeclarationModelExpanded(command.VariableDeclarationModel, !command.Collapse);
            }
        }
예제 #13
0
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, ExposeVariableCommand command)
        {
            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                command.VariableDeclarationModel.IsExposed = command.Exposed;
                graphUpdater.MarkChanged(command.VariableDeclarationModel);
            }
        }
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, CreateStickyNoteCommand command)
        {
            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                var stickyNote = graphToolState.GraphViewState.GraphModel.CreateStickyNote(command.Position);
                graphUpdater.MarkNew(stickyNote);
            }
        }
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, SetEdgeEditModeCommand command)
        {
            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                command.EdgeModel.EditMode = command.Value;
                graphUpdater.MarkChanged(command.EdgeModel);
            }
        }
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, RemoveEdgeControlPointCommand command)
        {
            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                command.EdgeModel.RemoveEdgeControlPoint(command.EdgeIndex);
                graphUpdater.MarkChanged(command.EdgeModel);
            }
        }
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, MoveEdgeControlPointCommand command)
        {
            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                command.EdgeModel.ModifyEdgeControlPoint(command.EdgeIndex, command.Position, command.Tightness);
                graphUpdater.MarkChanged(command.EdgeModel);
            }
        }
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, AddControlPointOnEdgeCommand command)
        {
            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                command.EdgeModel.InsertEdgeControlPoint(command.AtIndex, command.Position, 100);
                graphUpdater.MarkChanged(command.EdgeModel);
            }
        }
예제 #19
0
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, InitializeVariableCommand command)
        {
            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                command.VariableDeclarationModel.CreateInitializationValue();
                graphUpdater.MarkChanged(command.VariableDeclarationModel);
            }
        }
예제 #20
0
        /// <summary>
        /// Default command handler for CreateNodeFromSearcherCommand.
        /// </summary>
        /// <param name="graphToolState">The current graph tool state.</param>
        /// <param name="command">The command to handle.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, CreateNodeFromSearcherCommand command)
        {
            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                var newModels = command.SelectedItem.CreateElements.Invoke(
                    new GraphNodeCreationData(graphToolState.GraphViewState.GraphModel, command.Position, guid: command.Guid));

                graphUpdater.MarkNew(newModels);
            }
        }
예제 #21
0
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, CollapsePlacematCommand command)
        {
            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                command.PlacematModel.Collapsed      = command.Collapse;
                command.PlacematModel.HiddenElements = command.PlacematModel.Collapsed ? command.CollapsedElements : null;

                graphUpdater.MarkChanged(command.PlacematModel);
            }
        }
예제 #22
0
 /// <summary>
 /// Default handler.
 /// </summary>
 /// <param name="graphToolState">The state to modify.</param>
 /// <param name="command">The command to apply to the state.</param>
 public static void DefaultCommandHandler(GraphToolState graphToolState, AlignNodesCommand command)
 {
     if (command.Nodes.Any())
     {
         graphToolState.PushUndo(command);
         using (var stateUpdater = graphToolState.GraphViewState.UpdateScope)
         {
             command.GraphView.PositionDependenciesManager.AlignNodes(command.Follow, command.Nodes, stateUpdater);
             stateUpdater.ForceCompleteUpdate();
         }
     }
 }
예제 #23
0
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, ReorderGraphVariableDeclarationCommand command)
        {
            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                graphToolState.GraphViewState.GraphModel.MoveAfter(command.VariableDeclarationModels, command.InsertAfter);

                // Since potentially the index of every VD changed, let's mark them all as changed.
                graphUpdater.MarkChanged(graphToolState.GraphViewState.GraphModel.VariableDeclarations);
            }
        }
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, UpdateConstantValueCommand command)
        {
            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                command.Constant.ObjectValue = command.Value;
                if (command.OwnerModel != null)
                {
                    graphUpdater.MarkChanged(command.OwnerModel);
                }
            }
        }
        /// <summary>
        /// Gets the searcher window rect and left right ratio for <see cref="sizeName"/>.
        /// </summary>
        /// <param name="state">The state that contains size information.</param>
        /// <param name="sizeName">A string for the usage of the searcher.</param>
        public static SearcherSize GetSearcherSize(this GraphToolState state, string sizeName)
        {
            var sizes = state.GetSizes();

            if (string.IsNullOrEmpty(sizeName) || !sizes.TryGetValue(sizeName, out var size))
            {
                if (!sizes.TryGetValue("", out size))
                {
                    size = SearcherSize.defaultSearcherSize;
                }
            }
            return(size);
        }
        static SerializedValueDictionary <string, SearcherSize> GetSizes(this GraphToolState state)
        {
            SerializedValueDictionary <string, SearcherSize> sizes = null;
            var valueString = state.Preferences.GetString(StringPref.SearcherSize);

            if (valueString != null)
            {
                sizes = JsonUtility.FromJson <SerializedValueDictionary <string, SearcherSize> >(valueString);
            }

            sizes ??= new SerializedValueDictionary <string, SearcherSize>();
            return(sizes);
        }
        /// <summary>
        /// Sets searcher window size and left-right ratio for <see cref="sizeName"/>, if it is not already set.
        /// </summary>
        /// <param name="state">The state that contains size information.</param>
        /// <param name="sizeName">A string for the usage of the searcher. Passing null for the usage will define the default for any searcher window.</param>
        /// <param name="size">The size of the window.</param>
        /// <param name="rightLeftRatio">The ratio between the left size and the right size (details) of the searcher.</param>
        public static void SetInitialSearcherSize(this GraphToolState state, string sizeName, Vector2 size, float rightLeftRatio = 1.0f)
        {
            sizeName ??= "";

            var sizes = state.GetSizes();

            if (!sizes.TryGetValue(sizeName, out _))
            {
                sizes[sizeName] = new SearcherSize {
                    Size = size, RightLeftRatio = rightLeftRatio
                };
                state.SaveSizes(sizes);
            }
        }
예제 #28
0
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, UpdateTooltipCommand command)
        {
            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                command.VariableDeclarationModel.Tooltip = command.Tooltip;

                var graphModel = graphToolState.GraphViewState.GraphModel;
                var references = graphModel.FindReferencesInGraph <IVariableNodeModel>(command.VariableDeclarationModel);
                graphUpdater.MarkChanged(references);
                graphUpdater.MarkChanged(command.VariableDeclarationModel);
            }
        }
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, CreateEdgeCommand command)
        {
            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                var graphModel = graphToolState.GraphViewState.GraphModel;

                var fromPortModel = command.FromPortModel;
                var toPortModel   = command.ToPortModel;

                var edgesToDelete = command.EdgeModelsToDelete ?? new List <IEdgeModel>();

                // Delete previous connections
                if (toPortModel != null && toPortModel.Capacity != PortCapacity.Multi)
                {
                    edgesToDelete = edgesToDelete.Concat(toPortModel.GetConnectedEdges()).ToList();
                }

                if (command.EdgeModelsToDelete != null)
                {
                    graphModel.DeleteEdges(edgesToDelete);
                    graphUpdater.MarkDeleted(edgesToDelete);
                }

                // Auto-itemization preferences will determine if a new node is created or not
                if ((fromPortModel.NodeModel is IConstantNodeModel && graphToolState.Preferences.GetBool(BoolPref.AutoItemizeConstants)) ||
                    (fromPortModel.NodeModel is IVariableNodeModel && graphToolState.Preferences.GetBool(BoolPref.AutoItemizeVariables)))
                {
                    var itemizedNode = graphModel.CreateItemizedNode(EdgeCommandConfig.nodeOffset, ref fromPortModel);
                    if (itemizedNode != null)
                    {
                        graphUpdater.MarkNew(itemizedNode);
                    }
                }

                var edgeModel = graphModel.CreateEdge(toPortModel, fromPortModel);
                graphUpdater.MarkNew(edgeModel);

                if (toPortModel != null && command.PortAlignment.HasFlag(PortDirection.Input))
                {
                    graphUpdater.MarkModelToAutoAlign(toPortModel.NodeModel);
                }
                if (fromPortModel != null && command.PortAlignment.HasFlag(PortDirection.Output))
                {
                    graphUpdater.MarkModelToAutoAlign(fromPortModel.NodeModel);
                }
            }
        }
예제 #30
0
        /// <summary>
        /// Default command handler.
        /// </summary>
        /// <param name="graphToolState">The state.</param>
        /// <param name="command">The command.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, CreatePlacematCommand command)
        {
            graphToolState.PushUndo(command);

            using (var graphUpdater = graphToolState.GraphViewState.UpdateScope)
            {
                var placematModel = graphToolState.GraphViewState.GraphModel.CreatePlacemat(command.Position);
                if (command.Title != null)
                {
                    placematModel.Title = command.Title;
                }

                graphUpdater.MarkNew(placematModel);
            }
        }