Browse Source

Refactor Magic Wand selection mode handling

Renamed SelectMode to ResultingSelectionMode in IMagicWandToolHandler and related classes. Updated MagicWandToolViewModel to determine selection mode based on key modifiers. Adjusted MagicWandToolExecutor to use the new property and fixed update/finish action order.
Francespo 1 month ago
parent
commit
3d74a31fea

+ 2 - 1
src/PixiEditor.ChangeableDocument/Changes/Selection/MagicWand/MagicWand_UpdateableChange.cs

@@ -71,7 +71,8 @@ internal class MagicWand_UpdateableChange : UpdateableChange
         }
         else
         {
-            target.Selection.SelectionPath = originalPath!.Op(path, mode.ToVectorPathOp());
+            originalPath = originalPath!.Op(path, mode.ToVectorPathOp());
+            target.Selection.SelectionPath = originalPath;
         }
         toDispose.Dispose();
 

+ 4 - 4
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/MagicWandToolExecutor.cs

@@ -25,7 +25,7 @@ internal class MagicWandToolExecutor : UpdateableChangeExecutor
         if (magicWand is null || members.Count == 0)
             return ExecutionState.Error;
 
-        mode = magicWand.SelectMode;
+        mode = magicWand.ResultingSelectionMode;
         memberGuids = members;
         considerAllLayers = magicWand.DocumentScope == DocumentScope.Canvas;
         if (considerAllLayers)
@@ -40,21 +40,21 @@ internal class MagicWandToolExecutor : UpdateableChangeExecutor
 
     public override void OnPixelPositionChange(VecI pos)
     {
-        AddUpdateAction(pos);
         internals!.ActionAccumulator.AddActions(new ChangeBoundary_Action());
+        AddUpdateAction(pos);
     }
 
     public override void OnLeftMouseButtonUp(VecD argsPositionOnCanvas)
     {
-        AddFinishAction();
         internals!.ActionAccumulator.AddActions(new ChangeBoundary_Action());
+        AddFinishAction();
         onEnded!(this);
     }
 
     public override void ForceStop()
     {
-        AddFinishAction();
         internals!.ActionAccumulator.AddActions(new ChangeBoundary_Action());
+        AddFinishAction();
     }
 
     private void AddUpdateAction(VecI pos)

+ 1 - 1
src/PixiEditor/Models/Handlers/Tools/IMagicWandToolHandler.cs

@@ -5,7 +5,7 @@ namespace PixiEditor.Models.Handlers.Tools;
 
 internal interface IMagicWandToolHandler : IToolHandler
 {
-    public SelectionMode SelectMode { get; }
+    public SelectionMode ResultingSelectionMode { get; }
     public DocumentScope DocumentScope { get; }
     public float Tolerance { get; }
 }

+ 23 - 3
src/PixiEditor/ViewModels/Tools/Tools/MagicWandToolViewModel.cs

@@ -17,11 +17,12 @@ namespace PixiEditor.ViewModels.Tools.Tools;
 internal class MagicWandToolViewModel : ToolViewModel, IMagicWandToolHandler
 {
     public override LocalizedString Tooltip => new LocalizedString("MAGIC_WAND_TOOL_TOOLTIP", Shortcut);
-
+    private string defaultActionDisplay = "MAGIC_WAND_TOOL_ACTION_DISPLAY_DEFAULT";
     public override string ToolNameLocalizationKey => "MAGIC_WAND_TOOL";
     public override BrushShape FinalBrushShape => BrushShape.Pixel;
-    public override Type[]? SupportedLayerTypes { get; } = { typeof(IRasterLayerHandler) }; 
-
+    public override Type[]? SupportedLayerTypes { get; } = { typeof(IRasterLayerHandler) };
+    private SelectionMode KeyModifierselectionMode = SelectionMode.New;
+    public SelectionMode ResultingSelectionMode => KeyModifierselectionMode != SelectionMode.New ? KeyModifierselectionMode : SelectMode;
     [Settings.Enum("MODE_LABEL")]
     public SelectionMode SelectMode => GetValue<SelectionMode>();
 
@@ -45,4 +46,23 @@ internal class MagicWandToolViewModel : ToolViewModel, IMagicWandToolHandler
     {
         ViewModelMain.Current?.DocumentManagerSubViewModel.ActiveDocument?.Tools.UseMagicWandTool();
     }
+
+    public override void KeyChanged(bool ctrlIsDown, bool shiftIsDown, bool altIsDown, Key argsKey)
+    {
+        if (shiftIsDown)
+        {
+            ActionDisplay = new LocalizedString("MAGIC_WAND_TOOL_ACTION_DISPLAY_SHIFT");
+            KeyModifierselectionMode = SelectionMode.Add;
+        }
+        else if (ctrlIsDown)
+        {
+            ActionDisplay = new LocalizedString("MAGIC_WAND_TOOL_ACTION_DISPLAY_CTRL");
+            KeyModifierselectionMode = SelectionMode.Subtract;
+        }
+        else
+        {
+            ActionDisplay = defaultActionDisplay;
+            KeyModifierselectionMode = SelectionMode.New;
+        }
+    }
 }