/// <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); } } }
/// <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); } } }
/// <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>()); } }
/// <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); } }
/// <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, 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, InitializeVariableCommand command) { graphToolState.PushUndo(command); using (var graphUpdater = graphToolState.GraphViewState.UpdateScope) { command.VariableDeclarationModel.CreateInitializationValue(); 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, AddControlPointOnEdgeCommand command) { graphToolState.PushUndo(command); using (var graphUpdater = graphToolState.GraphViewState.UpdateScope) { command.EdgeModel.InsertEdgeControlPoint(command.AtIndex, command.Position, 100); 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, 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, ExposeVariableCommand command) { graphToolState.PushUndo(command); using (var graphUpdater = graphToolState.GraphViewState.UpdateScope) { command.VariableDeclarationModel.IsExposed = command.Exposed; graphUpdater.MarkChanged(command.VariableDeclarationModel); } }
/// <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(); } } }
/// <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 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); } }
/// <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); } }
/// <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> /// 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); } } }
/// <summary> /// Default command handler for ChangeElementLayoutCommand. /// </summary> /// <param name="graphToolState">The state.</param> /// <param name="command">The command.</param> public static void DefaultCommandHandler(GraphToolState graphToolState, ChangeElementLayoutCommand command) { if (command.Model.PositionAndSize == command.Layout) { return; } graphToolState.PushUndo(command); using (var graphUpdater = graphToolState.GraphViewState.UpdateScope) { command.Model.PositionAndSize = command.Layout; graphUpdater.MarkChanged(command.Model as IGraphElementModel); } }
/// <summary> /// Default command handler. /// </summary> /// <param name="graphToolState">The state.</param> /// <param name="command">The command.</param> public static void DefaultCommandHandler(GraphToolState graphToolState, DeleteEdgeCommand command) { if (command.Models == null || !command.Models.Any()) { return; } graphToolState.PushUndo(command); using (var graphUpdater = graphToolState.GraphViewState.UpdateScope) { graphToolState.GraphViewState.GraphModel.DeleteEdges(command.Models); graphUpdater.MarkDeleted(command.Models); } }
/// <summary> /// Default command handler. /// </summary> /// <param name="graphToolState">The state.</param> /// <param name="command">The command.</param> public static void DefaultCommandHandler(GraphToolState graphToolState, PasteSerializedDataCommand command) { if (!command.Data.IsEmpty()) { graphToolState.PushUndo(command); using (var graphViewUpdater = graphToolState.GraphViewState.UpdateScope) using (var selectionUpdater = graphToolState.SelectionState.UpdateScope) { selectionUpdater.ClearSelection(graphToolState.GraphViewState.GraphModel); CopyPasteData.PasteSerializedData(graphToolState.GraphViewState.GraphModel, command.Delta, graphViewUpdater, selectionUpdater, command.Data); } } }
/// <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); } }
/// <summary> /// Default command handler. /// </summary> /// <param name="state">The state to modify.</param> /// <param name="command">The command to apply to the state.</param> public static void DefaultCommandHandler(GraphToolState state, ClearSelectionCommand command) { var currentSelection = state.SelectionState.GetSelection(state.GraphViewState.GraphModel); if (currentSelection.Count == 0) { return; } state.PushUndo(command); using (var selectionUpdater = state.SelectionState.UpdateScope) { selectionUpdater.ClearSelection(state.GraphViewState.GraphModel); } }
/// <summary> /// Default command handler for CreateNodeOnEdgeCommand. /// </summary> /// <param name="graphToolState">The current graph tool state.</param> /// <param name="command">The command to handle.</param> public static void DefaultCommandHandler(GraphToolState graphToolState, CreateNodeOnEdgeCommand command) { graphToolState.PushUndo(command); using (var graphUpdater = graphToolState.GraphViewState.UpdateScope) { var edgeInput = command.EdgeModel.ToPort; var edgeOutput = command.EdgeModel.FromPort; // Instantiate node var graphModel = graphToolState.GraphViewState.GraphModel; var position = command.Position - Vector2.up * EdgeCommandConfig.nodeOffset; var elementModels = command.SelectedItem.CreateElements.Invoke( new GraphNodeCreationData(graphModel, position, guid: command.Guid)); graphUpdater.MarkNew(elementModels); if (elementModels.Length == 0 || !(elementModels[0] is IInputOutputPortsNodeModel selectedNodeModel)) { return; } // Delete old edge var deletedModels = graphModel.DeleteEdge(command.EdgeModel); graphUpdater.MarkDeleted(deletedModels); // Connect input port var inputPortModel = selectedNodeModel.InputsByDisplayOrder.FirstOrDefault(p => p?.PortType == edgeOutput?.PortType); if (inputPortModel != null) { var newEdge = graphModel.CreateEdge(inputPortModel, edgeOutput); graphUpdater.MarkNew(newEdge); } // Find first matching output type and connect it var outputPortModel = selectedNodeModel.GetPortFitToConnectTo(edgeInput); if (outputPortModel != null) { var newEdge = graphModel.CreateEdge(edgeInput, outputPortModel); graphUpdater.MarkNew(newEdge); } } }
/// <summary> /// Default command handler. /// </summary> /// <param name="graphToolState">The state.</param> /// <param name="command">The command.</param> public static void DefaultCommandHandler(GraphToolState graphToolState, BypassNodesCommand command) { graphToolState.PushUndo(command); var graphModel = graphToolState.GraphViewState.GraphModel; using (var graphUpdater = graphToolState.GraphViewState.UpdateScope) { foreach (var model in command.NodesToBypass) { var inputEdgeModels = new List <IEdgeModel>(); foreach (var portModel in model.InputsByDisplayOrder) { inputEdgeModels.AddRange(graphModel.GetEdgesConnections(portModel)); } if (!inputEdgeModels.Any()) { continue; } var outputEdgeModels = new List <IEdgeModel>(); foreach (var portModel in model.OutputsByDisplayOrder) { outputEdgeModels.AddRange(graphModel.GetEdgesConnections(portModel)); } if (!outputEdgeModels.Any()) { continue; } graphModel.DeleteEdges(inputEdgeModels); graphModel.DeleteEdges(outputEdgeModels); var edge = graphModel.CreateEdge(outputEdgeModels[0].ToPort, inputEdgeModels[0].FromPort); graphUpdater.MarkDeleted(inputEdgeModels); graphUpdater.MarkDeleted(outputEdgeModels); graphUpdater.MarkNew(edge); } var deletedModels = graphModel.DeleteNodes(command.Models, deleteConnections: false); graphUpdater.MarkDeleted(deletedModels); } }
/// <summary> /// Default command handler. /// </summary> /// <param name="state">The state to modify.</param> /// <param name="command">The command to apply to the state.</param> public static void DefaultCommandHandler(GraphToolState state, ReframeGraphViewCommand command) { state.PushUndo(command); using (var selectionUpdater = state.SelectionState.UpdateScope) using (var graphUpdater = state.GraphViewState.UpdateScope) { graphUpdater.Position = command.Position; graphUpdater.Scale = command.Scale; if (command.NewSelection != null) { selectionUpdater.ClearSelection(state.GraphViewState.GraphModel); selectionUpdater.SelectElements(command.NewSelection, true); } } }
/// <summary> /// Default command 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, ResetElementColorCommand command) { graphToolState.PushUndo(command); if (command.Models != null) { using (var updater = graphToolState.GraphViewState.UpdateScope) { foreach (var model in command.Models.Where(c => c.IsColorable())) { model.ResetColor(); } updater.MarkChanged(command.Models); } } }
/// <summary> /// Default command handler. /// </summary> /// <param name="state">The state to modify.</param> /// <param name="command">The command to apply to the state.</param> public static void DefaultCommandHandler(GraphToolState state, SelectElementsCommand command) { switch (command.Mode) { case SelectionMode.Replace: var currentSelection = state.SelectionState.GetSelection(state.GraphViewState.GraphModel); if (currentSelection.SequenceEqual(command.Models)) { return; } break; case SelectionMode.Add when command.Models.Count == 0: case SelectionMode.Remove when command.Models.Count == 0: case SelectionMode.Toggle when command.Models.Count == 0: return; } state.PushUndo(command); using (var selectionUpdater = state.SelectionState.UpdateScope) { switch (command.Mode) { case SelectionMode.Replace: selectionUpdater.ClearSelection(state.GraphViewState.GraphModel); selectionUpdater.SelectElements(command.Models, true); break; case SelectionMode.Add: selectionUpdater.SelectElements(command.Models, true); break; case SelectionMode.Remove: selectionUpdater.SelectElements(command.Models, false); break; case SelectionMode.Toggle: var toSelect = command.Models.Where(m => !state.SelectionState.IsSelected(m)).ToList(); selectionUpdater.SelectElements(command.Models, false); selectionUpdater.SelectElements(toSelect, true); break; } } }
/// <summary> /// Default command handler. /// </summary> /// <param name="graphToolState">The state.</param> /// <param name="command">The command.</param> public static void DefaultCommandHandler(GraphToolState graphToolState, UpdatePortConstantCommand command) { graphToolState.PushUndo(command); using (var graphUpdater = graphToolState.GraphViewState.UpdateScope) { if (command.PortModel.EmbeddedValue is IStringWrapperConstantModel stringWrapperConstantModel) { stringWrapperConstantModel.StringValue = (string)command.NewValue; } else { command.PortModel.EmbeddedValue.ObjectValue = command.NewValue; } graphUpdater.MarkChanged(command.PortModel); } }
/// <summary> /// Default command handler. /// </summary> /// <param name="graphToolState">The state.</param> /// <param name="command">The command.</param> public static void DefaultCommandHandler(GraphToolState graphToolState, ReorderEdgeCommand command) { if (command.EdgeModel?.FromPort is IReorderableEdgesPortModel fromPort && fromPort.HasReorderableEdges) { var siblingEdges = fromPort.GetConnectedEdges().ToList(); var siblingEdgesCount = siblingEdges.Count; if (siblingEdgesCount > 1) { var index = siblingEdges.IndexOf(command.EdgeModel); Action <IEdgeModel> reorderAction = null; switch (command.Type) { case ReorderType.MoveFirst when index > 0: reorderAction = fromPort.MoveEdgeFirst; break; case ReorderType.MoveUp when index > 0: reorderAction = fromPort.MoveEdgeUp; break; case ReorderType.MoveDown when index < siblingEdgesCount - 1: reorderAction = fromPort.MoveEdgeDown; break; case ReorderType.MoveLast when index < siblingEdgesCount - 1: reorderAction = fromPort.MoveEdgeLast; break; } if (reorderAction != null) { graphToolState.PushUndo(command); using (var graphUpdater = graphToolState.GraphViewState.UpdateScope) { reorderAction(command.EdgeModel); graphUpdater.MarkChanged(siblingEdges); graphUpdater.MarkChanged(fromPort.NodeModel); } } } } }