Browse Source

Implemented Magic Wand Tool

CPKreuz 4 years ago
parent
commit
6665be8be4

+ 8 - 4
PixiEditor/Models/Controllers/BitmapManager.cs

@@ -123,14 +123,18 @@ namespace PixiEditor.Models.Controllers
                     startPosition = newPosition;
                     startPosition = newPosition;
                 }
                 }
 
 
-                if (IsOperationTool(SelectedTool))
+                if (SelectedTool is BitmapOperationTool operationTool)
                 {
                 {
-                    BitmapOperations.ExecuteTool(newPosition, MouseController.LastMouseMoveCoordinates, (BitmapOperationTool)SelectedTool);
+                    BitmapOperations.ExecuteTool(newPosition, MouseController.LastMouseMoveCoordinates, operationTool);
                 }
                 }
-                else
+                else if (SelectedTool is ReadonlyTool readonlyTool)
                 {
                 {
-                    ReadonlyToolUtility.ExecuteTool(MouseController.LastMouseMoveCoordinates, (ReadonlyTool)SelectedTool);
+                    ReadonlyToolUtility.ExecuteTool(MouseController.LastMouseMoveCoordinates, readonlyTool);
                 }
                 }
+                else
+                {
+                    throw new InvalidOperationException($"'{SelectedTool.GetType().Name}' is either not a Tool or can't inherit '{nameof(Tool)}' directly.\nChanges the base type to either '{nameof(BitmapOperationTool)}' or '{nameof(ReadonlyTool)}'");
+                }
             }
             }
         }
         }
 
 

+ 0 - 2
PixiEditor/Models/Tools/Tool.cs

@@ -49,8 +49,6 @@ namespace PixiEditor.Models.Tools
 
 
         public Toolbar Toolbar { get; set; } = new EmptyToolbar();
         public Toolbar Toolbar { get; set; } = new EmptyToolbar();
 
 
-        public IServiceProvider Services { get; set; }
-
         public bool CanStartOutsideCanvas { get; set; } = false;
         public bool CanStartOutsideCanvas { get; set; } = false;
 
 
         public virtual void OnMouseDown(MouseEventArgs e)
         public virtual void OnMouseDown(MouseEventArgs e)

+ 6 - 2
PixiEditor/Models/Tools/ToolSettings/Toolbars/SelectToolToolbar.cs

@@ -5,10 +5,14 @@ namespace PixiEditor.Models.Tools.ToolSettings.Toolbars
 {
 {
     public class SelectToolToolbar : Toolbar
     public class SelectToolToolbar : Toolbar
     {
     {
-        public SelectToolToolbar()
+        public SelectToolToolbar(bool includeSelectionShape = true)
         {
         {
             Settings.Add(new EnumSetting<SelectionType>("SelectMode", "Selection type"));
             Settings.Add(new EnumSetting<SelectionType>("SelectMode", "Selection type"));
-            Settings.Add(new EnumSetting<SelectionShape>("SelectShape", "Selection shape"));
+
+            if (includeSelectionShape)
+            {
+                Settings.Add(new EnumSetting<SelectionShape>("SelectShape", "Selection shape"));
+            }
         }
         }
     }
     }
 }
 }

+ 61 - 0
PixiEditor/Models/Tools/Tools/MagicWandTool.cs

@@ -0,0 +1,61 @@
+using PixiEditor.Helpers;
+using PixiEditor.Helpers.Extensions;
+using PixiEditor.Models.Controllers;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Enums;
+using PixiEditor.Models.Position;
+using PixiEditor.Models.Tools.ToolSettings.Toolbars;
+using PixiEditor.ViewModels;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Linq;
+using System.Windows.Input;
+
+namespace PixiEditor.Models.Tools.Tools
+{
+    public class MagicWandTool : ReadonlyTool
+    {
+        private readonly FloodFill floodFill;
+
+        private static Selection ActiveSelection { get => ViewModelMain.Current.BitmapManager.ActiveDocument.ActiveSelection; }
+
+        private BitmapManager BitmapManager { get; }
+
+        private IEnumerable<Coordinates> oldSelection;
+
+        public SelectionType SelectionType { get; set; } = SelectionType.Add;
+
+        public override void OnRecordingLeftMouseDown(MouseEventArgs e)
+        {
+            SelectionType = Toolbar.GetEnumSetting<SelectionType>("SelectMode").Value;
+
+            oldSelection = new ReadOnlyCollection<Coordinates>(ActiveSelection.SelectedPoints);
+        }
+
+        public override void OnStoppedRecordingMouseUp(MouseEventArgs e)
+        {
+            if (ActiveSelection.SelectedPoints.Count <= 1)
+            {
+                // If we have not selected multiple points, clear the selection
+                ActiveSelection.Clear();
+            }
+
+            SelectionHelpers.AddSelectionUndoStep(ViewModelMain.Current.BitmapManager.ActiveDocument, oldSelection, SelectionType);
+        }
+
+        public MagicWandTool(BitmapManager manager)
+        {
+            floodFill = new FloodFill(manager);
+            BitmapManager = manager;
+
+            Toolbar = new SelectToolToolbar(false);
+        }
+
+        public override void Use(List<Coordinates> pixels)
+        {
+            Selection selection = BitmapManager.ActiveDocument.ActiveSelection;
+
+            selection.SetSelection(floodFill.ForestFire(BitmapManager.ActiveLayer, pixels.First(), System.Windows.Media.Colors.White).ChangedPixels.Keys, Toolbar.GetEnumSetting<SelectionType>("SelectMode").Value);
+        }
+    }
+}

+ 5 - 7
PixiEditor/ViewModels/SubViewModels/Main/ToolsViewModel.cs

@@ -40,14 +40,12 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
 
         public void SetupTools(IServiceProvider services)
         public void SetupTools(IServiceProvider services)
         {
         {
-            ToolBuilder builder = new ToolBuilder(services);
-
-            builder
+            ToolSet = new ObservableCollection<Tool>(
+                new ToolBuilder(services)
                 .Add<MoveViewportTool>().Add<MoveTool>().Add<PenTool>().Add<SelectTool>().Add<FloodFill>()
                 .Add<MoveViewportTool>().Add<MoveTool>().Add<PenTool>().Add<SelectTool>().Add<FloodFill>()
-                .Add<LineTool>().Add<CircleTool>().Add<RectangleTool>().Add<EraserTool>().Add<ColorPickerTool>().Add<BrightnessTool>()
-                .Add<ZoomTool>();
-
-            ToolSet = new(builder.Build());
+                .Add<LineTool>().Add<CircleTool>().Add<RectangleTool>().Add<EraserTool>().Add<ColorPickerTool>()
+                .Add<BrightnessTool>().Add<ZoomTool>().Add<MagicWandTool>()
+                .Build());
 
 
             SetActiveTool<MoveViewportTool>();
             SetActiveTool<MoveViewportTool>();
         }
         }