Browse Source

Added Eraser tool

flabbet 3 years ago
parent
commit
0fea264768

+ 61 - 0
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/EraserToolExecutor.cs

@@ -0,0 +1,61 @@
+#nullable enable
+using ChunkyImageLib.DataHolders;
+using PixiEditor.ChangeableDocument.Actions;
+using PixiEditor.Models.Enums;
+using PixiEditor.ViewModels.SubViewModels.Document;
+using PixiEditor.ViewModels.SubViewModels.Tools.Tools;
+using PixiEditor.ViewModels.SubViewModels.Tools.ToolSettings.Toolbars;
+
+namespace PixiEditor.Models.DocumentModels.UpdateableChangeExecutors;
+
+internal class EraserToolExecutor : UpdateableChangeExecutor
+{
+    private Guid guidValue;
+    private SKColor color;
+    private int toolSize;
+    private bool drawOnMask;
+
+    public override ExecutionState Start()
+    {
+        ViewModelMain? vm = ViewModelMain.Current;
+        StructureMemberViewModel? member = document!.SelectedStructureMember;
+        EraserToolViewModel? eraserTool = (EraserToolViewModel?)(vm?.ToolsSubViewModel.GetTool<EraserToolViewModel>());
+        BasicToolbar? toolbar = eraserTool?.Toolbar as BasicToolbar;
+        if (vm is null || eraserTool is null || member is null || toolbar is null)
+            return ExecutionState.Error;
+        drawOnMask = member.ShouldDrawOnMask;
+        if (drawOnMask && !member.HasMaskBindable)
+            return ExecutionState.Error;
+        if (!drawOnMask && member is not LayerViewModel)
+            return ExecutionState.Error;
+
+        guidValue = member.GuidValue;
+        color = vm.ColorsSubViewModel.PrimaryColor;
+        toolSize = toolbar.ToolSize;
+
+        vm.ColorsSubViewModel.AddSwatch(color);
+        IAction? action = new LineBasedPen_Action(guidValue, SKColors.Transparent, controller!.LastPixelPosition, toolSize, true,
+            drawOnMask);
+        helpers!.ActionAccumulator.AddActions(action);
+
+        return ExecutionState.Success;
+    }
+
+    public override void OnPixelPositionChange(VecI pos)
+    {
+        IAction? action = new LineBasedPen_Action(guidValue, SKColors.Transparent, pos, toolSize, true, drawOnMask);
+        helpers!.ActionAccumulator.AddActions(action);
+    }
+
+    public override void OnLeftMouseButtonUp()
+    {
+        helpers!.ActionAccumulator.AddFinishedActions(new EndLineBasedPen_Action());
+        onEnded?.Invoke(this);
+    }
+
+    public override void ForceStop()
+    {
+        IAction? action = new EndLineBasedPen_Action();
+        helpers!.ActionAccumulator.AddFinishedActions(action);
+    }
+}

+ 1 - 0
src/PixiEditor/ViewModels/SubViewModels/Document/DocumentViewModel.cs

@@ -301,6 +301,7 @@ internal class DocumentViewModel : NotifyableObject
     public void UseOpacitySlider() => Helpers.ChangeController.TryStartUpdateableChange<StructureMemberOpacityExecutor>();
 
     public void UsePenTool() => Helpers.ChangeController.TryStartUpdateableChange<PenToolExecutor>();
+    public void UseEraserTool() => Helpers.ChangeController.TryStartUpdateableChange<EraserToolExecutor>();
 
     public void UseRectangleTool() => Helpers.ChangeController.TryStartUpdateableChange<RectangleToolExecutor>();
     

+ 6 - 0
src/PixiEditor/ViewModels/SubViewModels/Tools/Tools/EraserToolViewModel.cs

@@ -1,4 +1,5 @@
 using System.Windows.Input;
+using ChunkyImageLib.DataHolders;
 using PixiEditor.Models.Commands.Attributes.Commands;
 using PixiEditor.ViewModels.SubViewModels.Tools.ToolSettings.Toolbars;
 
@@ -14,4 +15,9 @@ internal class EraserToolViewModel : ToolViewModel
     }
 
     public override string Tooltip => $"Erasers color from pixel. ({Shortcut})";
+    
+    public override void OnLeftMouseButtonDown(VecD pos)
+    {
+        ViewModelMain.Current?.DocumentManagerSubViewModel.ActiveDocument?.UseEraserTool();
+    }
 }