Browse Source

Refactor MagicWand change to support updates

Renamed MagicWand_Change to MagicWand_UpdateableChange and made it inherit from UpdateableChange. Added support for updating the change with new points and refactored ApplyTemporarily and Revert methods. Updated MagicWandToolExecutor
Francespo 1 month ago
parent
commit
8470b494a8

+ 18 - 7
src/PixiEditor.ChangeableDocument/Changes/Selection/MagicWand/MagicWand_Change.cs

@@ -6,7 +6,7 @@ using Drawie.Numerics;
 
 
 namespace PixiEditor.ChangeableDocument.Changes.Selection.MagicWand;
 namespace PixiEditor.ChangeableDocument.Changes.Selection.MagicWand;
 
 
-internal class MagicWand_Change : Change
+internal class MagicWand_UpdateableChange : UpdateableChange
 {
 {
     private VectorPath? originalPath;
     private VectorPath? originalPath;
     private VectorPath path = new() { FillType = PathFillType.EvenOdd };
     private VectorPath path = new() { FillType = PathFillType.EvenOdd };
@@ -16,8 +16,8 @@ internal class MagicWand_Change : Change
     private int frame;
     private int frame;
     private double tolerance;
     private double tolerance;
 
 
-    [GenerateMakeChangeAction]
-    public MagicWand_Change(List<Guid> memberGuids, VecI point, SelectionMode mode, double tolerance, int frame)
+    [GenerateUpdateableChangeActions]
+    public MagicWand_UpdateableChange(List<Guid> memberGuids, VecI point, SelectionMode mode, double tolerance, int frame)
     {
     {
         path.MoveTo(point);
         path.MoveTo(point);
         this.mode = mode;
         this.mode = mode;
@@ -33,6 +33,12 @@ internal class MagicWand_Change : Change
         return true;
         return true;
     }
     }
 
 
+    [UpdateChangeMethod]
+    public void Update(VecI point)
+    {
+        this.point = point;
+    }
+
     public override OneOf<None, IChangeInfo, List<IChangeInfo>> Apply(Document target, bool firstApply, out bool ignoreInUndo)
     public override OneOf<None, IChangeInfo, List<IChangeInfo>> Apply(Document target, bool firstApply, out bool ignoreInUndo)
     {
     {
         HashSet<Guid> membersToReference = new();
         HashSet<Guid> membersToReference = new();
@@ -49,11 +55,9 @@ internal class MagicWand_Change : Change
         return CommonApply(target);
         return CommonApply(target);
     }
     }
 
 
-    public override OneOf<None, IChangeInfo, List<IChangeInfo>> Revert(Document target)
+    public override OneOf<None, IChangeInfo, List<IChangeInfo>> ApplyTemporarily(Document target)
     {
     {
-        (var toDispose, target.Selection.SelectionPath) = (target.Selection.SelectionPath, new VectorPath(originalPath!));
-        toDispose.Dispose();
-        return new Selection_ChangeInfo(new VectorPath(target.Selection.SelectionPath));
+        return CommonApply(target);
     }
     }
 
 
     private Selection_ChangeInfo CommonApply(Document target)
     private Selection_ChangeInfo CommonApply(Document target)
@@ -74,6 +78,13 @@ internal class MagicWand_Change : Change
         return new Selection_ChangeInfo(new VectorPath(target.Selection.SelectionPath));
         return new Selection_ChangeInfo(new VectorPath(target.Selection.SelectionPath));
     }
     }
 
 
+    public override OneOf<None, IChangeInfo, List<IChangeInfo>> Revert(Document target)
+    {
+        (var toDispose, target.Selection.SelectionPath) = (target.Selection.SelectionPath, new VectorPath(originalPath!));
+        toDispose.Dispose();
+        return new Selection_ChangeInfo(new VectorPath(target.Selection.SelectionPath));
+    }
+
     public override void Dispose()
     public override void Dispose()
     {
     {
         path.Dispose();
         path.Dispose();

+ 20 - 3
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/MagicWandToolExecutor.cs

@@ -33,19 +33,36 @@ internal class MagicWandToolExecutor : UpdateableChangeExecutor
         var pos = controller!.LastPixelPosition;
         var pos = controller!.LastPixelPosition;
         tolerance = (float)magicWand.Tolerance;
         tolerance = (float)magicWand.Tolerance;
 
 
-        internals!.ActionAccumulator.AddActions(new MagicWand_Action(memberGuids, pos, mode, tolerance, document!.AnimationHandler.ActiveFrameBindable));
+        AddUpdateAction(pos);
 
 
         return ExecutionState.Success;
         return ExecutionState.Success;
     }
     }
 
 
+    public override void OnPixelPositionChange(VecI pos)
+    {
+        AddUpdateAction(pos);
+        //internals!.ActionAccumulator.AddActions(new ChangeBoundary_Action());
+    }
+
     public override void OnLeftMouseButtonUp(VecD argsPositionOnCanvas)
     public override void OnLeftMouseButtonUp(VecD argsPositionOnCanvas)
     {
     {
-        internals!.ActionAccumulator.AddActions(new ChangeBoundary_Action());
+        AddFinishAction();
         onEnded!(this);
         onEnded!(this);
     }
     }
 
 
     public override void ForceStop()
     public override void ForceStop()
     {
     {
-        internals!.ActionAccumulator.AddActions(new ChangeBoundary_Action());
+        AddFinishAction();
+    }
+
+    private void AddUpdateAction(VecI pos)
+    {
+        var action = new MagicWand_Action(memberGuids, pos, mode, tolerance, document!.AnimationHandler.ActiveFrameBindable);
+        internals!.ActionAccumulator.AddActions(action);
+    }
+    private void AddFinishAction()
+    {
+        internals!.ActionAccumulator.AddActions(new EndMagicWand_Action());
+        //internals!.ActionAccumulator.AddActions(new ChangeBoundary_Action());
     }
     }
 }
 }