فهرست منبع

Path tool is in normal undo now

flabbet 8 ماه پیش
والد
کامیت
179eaa287c

+ 14 - 6
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/VectorPathToolExecutor.cs

@@ -13,6 +13,7 @@ using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces.Shapes;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.Controllers.InputDevice;
+using PixiEditor.Models.DocumentModels.Public;
 using PixiEditor.Models.DocumentModels.UpdateableChangeExecutors.Features;
 using PixiEditor.Models.Handlers.Toolbars;
 using PixiEditor.Models.Tools;
@@ -37,6 +38,7 @@ internal class VectorPathToolExecutor : UpdateableChangeExecutor, IPathExecutorF
 
     public bool CanUndo => document.PathOverlayHandler.HasUndo;
     public bool CanRedo => document.PathOverlayHandler.HasRedo;
+    public bool StopExecutionOnNormalUndo => false;
 
     public override bool BlocksOtherActions => false;
 
@@ -78,7 +80,7 @@ internal class VectorPathToolExecutor : UpdateableChangeExecutor, IPathExecutorF
                 return ExecutionState.Success;
             }
 
-            document.PathOverlayHandler.Show(startingPath, false);
+            document.PathOverlayHandler.Show(startingPath, false, AddToUndo);
             if (controller.LeftMousePressed)
             {
                 var snapped =
@@ -178,7 +180,7 @@ internal class VectorPathToolExecutor : UpdateableChangeExecutor, IPathExecutorF
     {
         if (document.PathOverlayHandler.IsActive)
         {
-            internals.ActionAccumulator.AddActions(new SetShapeGeometry_Action(member.Id, ConstructShapeData()));
+            internals.ActionAccumulator.AddActions(new SetShapeGeometry_Action(member.Id, ConstructShapeData(startingPath)));
         }
     }
 
@@ -190,9 +192,15 @@ internal class VectorPathToolExecutor : UpdateableChangeExecutor, IPathExecutorF
         internals.ActionAccumulator.AddFinishedActions(new EndSetShapeGeometry_Action());
     }
 
-    private PathVectorData ConstructShapeData()
+    private void AddToUndo(VectorPath path)
     {
-        if(startingPath == null)
+        internals.ActionAccumulator.AddFinishedActions(new EndSetShapeGeometry_Action(),
+            new SetShapeGeometry_Action(member.Id, ConstructShapeData(path)), new EndSetShapeGeometry_Action());
+    }
+
+    private PathVectorData ConstructShapeData(VectorPath? path)
+    {
+        if(path is null)
         {
             return new PathVectorData(new VectorPath() { FillType = (PathFillType)vectorPathToolHandler.FillMode })
             {
@@ -202,7 +210,7 @@ internal class VectorPathToolExecutor : UpdateableChangeExecutor, IPathExecutorF
             };
         }
         
-        return new PathVectorData(new VectorPath(startingPath) { FillType = (PathFillType)vectorPathToolHandler.FillMode })
+        return new PathVectorData(new VectorPath(path) { FillType = (PathFillType)vectorPathToolHandler.FillMode })
         {
             StrokeWidth = (float)toolbar.ToolSize,
             StrokeColor = toolbar.StrokeColor.ToColor(),
@@ -215,7 +223,7 @@ internal class VectorPathToolExecutor : UpdateableChangeExecutor, IPathExecutorF
         if (document.PathOverlayHandler.IsActive)
         {
             startingPath = path;
-            internals.ActionAccumulator.AddActions(new SetShapeGeometry_Action(member.Id, ConstructShapeData()));
+            internals.ActionAccumulator.AddActions(new SetShapeGeometry_Action(member.Id, ConstructShapeData(startingPath)));
         }
     }
 

+ 1 - 1
src/PixiEditor/Models/Handlers/IPathOverlayHandler.cs

@@ -4,7 +4,7 @@ namespace PixiEditor.Models.Handlers;
 
 public interface IPathOverlayHandler : IHandler
 {
-    public void Show(VectorPath path, bool showApplyButton);
+    public void Show(VectorPath path, bool showApplyButton, Action<VectorPath>? customAddToUndo = null);
     public void Hide();
     public event Action<VectorPath> PathChanged;
     public bool IsActive { get; }

+ 3 - 0
src/PixiEditor/Models/Handlers/IToolHandler.cs

@@ -63,4 +63,7 @@ internal interface IToolHandler : IHandler
     public void SetToolSetSettings(IToolSetHandler toolset, Dictionary<string, object>? settings);
     public void ApplyToolSetSettings(IToolSetHandler toolset);
     public void OnDeselecting(bool transient);
+    
+    public void OnPostUndo();
+    public void OnPostRedo();
 }

+ 2 - 0
src/PixiEditor/Models/Handlers/IToolsHandler.cs

@@ -31,4 +31,6 @@ internal interface IToolsHandler : IHandler
     public void UseToolEventInlet(VecD argsPositionOnCanvas, MouseButton argsButton);
     public T GetTool<T>() where T : IToolHandler;
     public void AddPropertyChangedCallback(string propertyName, Action callback);
+    public void OnPostUndoInlet();
+    public void OnPostRedoInlet();
 }

+ 16 - 4
src/PixiEditor/ViewModels/Document/TransformOverlays/PathOverlayViewModel.cs

@@ -2,6 +2,8 @@
 using CommunityToolkit.Mvvm.ComponentModel;
 using CommunityToolkit.Mvvm.Input;
 using Drawie.Backend.Core.Vector;
+using PixiEditor.ChangeableDocument.Actions.Generated;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Shapes.Data;
 using PixiEditor.Models.DocumentModels;
 using PixiEditor.Models.Handlers;
 
@@ -44,7 +46,13 @@ internal class PathOverlayViewModel : ObservableObject, IPathOverlayHandler
     public bool HasUndo => undoStack.UndoCount > 0;
     public bool HasRedo => undoStack.RedoCount > 0;
 
-    public RelayCommand<VectorPath> AddToUndoCommand { get; }
+    private RelayCommand<VectorPath> addToUndoCommand;
+
+    public RelayCommand<VectorPath> AddToUndoCommand
+    {
+        get => addToUndoCommand;
+        set => SetProperty(ref addToUndoCommand, value);
+    }
     
     private bool showApplyButton;
 
@@ -55,17 +63,20 @@ internal class PathOverlayViewModel : ObservableObject, IPathOverlayHandler
     }
 
     private bool suppressUndo = false;
+    private RelayCommand<VectorPath> embeddedAddToUndo;
 
     public PathOverlayViewModel(DocumentViewModel documentViewModel, DocumentInternalParts internals)
     {
         this.documentViewModel = documentViewModel;
         this.internals = internals;
 
-        AddToUndoCommand = new RelayCommand<VectorPath>(AddToUndo);
+        embeddedAddToUndo = new RelayCommand<VectorPath>(AddToUndo);
+
+        AddToUndoCommand = embeddedAddToUndo;
         undoStack = new PathOverlayUndoStack<VectorPath>();
     }
 
-    public void Show(VectorPath newPath, bool showApplyButton)
+    public void Show(VectorPath newPath, bool showApplyButton, Action<VectorPath>? customAddToUndo = null)
     {
         if (IsActive)
         {
@@ -78,6 +89,7 @@ internal class PathOverlayViewModel : ObservableObject, IPathOverlayHandler
         Path = newPath;
         IsActive = true;
         ShowApplyButton = showApplyButton;
+        AddToUndoCommand = customAddToUndo != null ? new RelayCommand<VectorPath>(customAddToUndo) : embeddedAddToUndo;
     }
 
     public void Hide()
@@ -113,6 +125,6 @@ internal class PathOverlayViewModel : ObservableObject, IPathOverlayHandler
 
     private void PathDataChanged(VectorPath path)
     {
-        AddToUndo(path);
+        AddToUndoCommand.Execute(path);
     }
 }

+ 10 - 0
src/PixiEditor/ViewModels/SubViewModels/ToolsViewModel.cs

@@ -417,6 +417,16 @@ internal class ToolsViewModel : SubViewModel<ViewModelMain>, IToolsHandler
     {
         ActiveTool?.ModifierKeyChanged(args.IsCtrlDown, args.IsShiftDown, args.IsAltDown);
     }
+    
+    public void OnPostUndoInlet()
+    {
+        ActiveTool?.OnPostUndo();
+    }
+    
+    public void OnPostRedoInlet()
+    {
+        ActiveTool?.OnPostRedo();
+    }
 
     private void ToolbarSettingChanged(string settingName, object value)
     {

+ 8 - 0
src/PixiEditor/ViewModels/SubViewModels/UndoViewModel.cs

@@ -25,6 +25,10 @@ internal class UndoViewModel : SubViewModel<ViewModelMain>
         if (doc is null || (!doc.IsChangeFeatureActive<IMidChangeUndoableExecutor>() && !doc.HasSavedRedo))
             return;
         doc.Operations.Redo();
+        doc.Operations.InvokeCustomAction(() =>
+        {
+            Owner.ToolsSubViewModel.OnPostRedoInlet();
+        });
     }
 
     /// <summary>
@@ -38,6 +42,10 @@ internal class UndoViewModel : SubViewModel<ViewModelMain>
         if (doc is null || (!doc.IsChangeFeatureActive<IMidChangeUndoableExecutor>() && !doc.HasSavedUndo))
             return;
         doc.Operations.Undo();
+        doc.Operations.InvokeCustomAction(() =>
+        {
+            Owner.ToolsSubViewModel.OnPostUndoInlet();
+        });
     }
 
     /// <summary>

+ 3 - 0
src/PixiEditor/ViewModels/Tools/ToolViewModel.cs

@@ -158,6 +158,9 @@ internal abstract class ToolViewModel : ObservableObject, IToolHandler
     public virtual void OnDeselecting(bool transient)
     {
     }
+    
+    public virtual void OnPostUndo() { }
+    public virtual void OnPostRedo() { }
 
     public void SetToolSetSettings(IToolSetHandler toolset, Dictionary<string, object>? settings)
     {

+ 18 - 2
src/PixiEditor/ViewModels/Tools/Tools/VectorPathToolViewModel.cs

@@ -97,7 +97,7 @@ internal class VectorPathToolViewModel : ShapeTool, IVectorPathToolHandler
             ActionDisplay = actionDisplayDefault;
         }
     }
-
+    
     public override void OnSelected(bool restoring)
     {
         if (restoring) return;
@@ -105,7 +105,7 @@ internal class VectorPathToolViewModel : ShapeTool, IVectorPathToolHandler
         ViewModelMain.Current?.DocumentManagerSubViewModel.ActiveDocument?.Tools.UseVectorPathTool();
         isActivated = true;
     }
-
+    
     public override void OnDeselecting(bool transient)
     {
         if (!transient)
@@ -115,6 +115,22 @@ internal class VectorPathToolViewModel : ShapeTool, IVectorPathToolHandler
         }
     }
 
+    public override void OnPostUndo()
+    {
+        if (isActivated)
+        {
+            OnSelected(false);
+        }
+    }
+
+    public override void OnPostRedo()
+    {
+        if (isActivated)
+        {
+            OnSelected(false);
+        }
+    }
+
     protected override void OnSelectedLayersChanged(IStructureMemberHandler[] layers)
     {
         OnDeselecting(false);