Bläddra i källkod

Added flood fill

flabbet 5 år sedan
förälder
incheckning
7f386c50bb

+ 2 - 1
PixiEditorDotNetCore3/Models/Tools/Tool.cs

@@ -8,6 +8,7 @@ namespace PixiEditorDotNetCore3.Models.Tools
     public abstract class Tool
     {
         public abstract BitmapPixelChanges Use(Layer layer, Coordinates startingCoords, Color color, int toolSize);
-        public abstract ToolType GetToolType();
+        public abstract ToolType GetToolType { get; }
+        public bool ExecutesItself = false;
     }
 }

+ 7 - 24
PixiEditorDotNetCore3/Models/Tools/ToolSet.cs

@@ -1,13 +1,8 @@
-using PixiEditor.Helpers;
-using System;
+using System;
 using System.Collections.Generic;
-using System.Diagnostics;
-using System.Linq;
-using System.Text;
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Media;
-using System.Windows.Controls;
 using System.Windows.Input;
 using System.Windows.Media.Imaging;
 
@@ -17,7 +12,7 @@ namespace PixiEditorDotNetCore3.Models.Tools
     {
         public List<Tool> Tools { get; set; } = new List<Tool>();
         private Coordinates _activeCoordinates = new Coordinates();
-        private bool _toolIsExecuting = false;
+        private bool _toolIsExecuting;
         private int _asyncDelay = 15;
 
 
@@ -41,7 +36,7 @@ namespace PixiEditorDotNetCore3.Models.Tools
             BitmapPixelChanges changes;
 
 
-            Tool selectedTool = Tools.Find(x => x.GetToolType() == tool);
+            Tool selectedTool = Tools.Find(x => x.GetToolType == tool);
             changes = selectedTool.Use(layer, startingCoords, color, toolSize);
 
             if (tool != ToolType.ColorPicker)
@@ -50,23 +45,11 @@ namespace PixiEditorDotNetCore3.Models.Tools
                     $"{tool.ToString()} Tool.");
             }
 
-            layer.ApplyPixels(changes, color);
-
-        }
+            if (selectedTool.ExecutesItself == false)
+            {
+                layer.ApplyPixels(changes, color);
+            }
 
-        /// <summary>
-        /// Not working yet.
-        /// </summary>
-        /// <param name="bitmap"></param>
-        /// <param name="pixelCoordinates"></param>
-        /// <param name="color"></param>
-        /// <param name="highlightThickness"></param>
-        public static void HighlightPixel(WriteableBitmap bitmap,Coordinates pixelCoordinates, Color color, int highlightThickness)
-        {
-            bitmap.Clear();
-            bitmap.Blit(new Rect(new Size(bitmap.Width, bitmap.Height)), bitmap, new Rect(new Size(bitmap.Width, bitmap.Height)), WriteableBitmapExtensions.BlendMode.Additive);
-            DoubleCords centerCords = CoordinatesCalculator.CalculateThicknessCenter(pixelCoordinates, highlightThickness);
-            bitmap.FillRectangle(centerCords.Coords1.X, centerCords.Coords1.Y, centerCords.Coords2.X, centerCords.Coords2.Y, color);
         }
 
         /// <summary>

+ 51 - 0
PixiEditorDotNetCore3/Models/Tools/Tools/FloodFill.cs

@@ -0,0 +1,51 @@
+using System;
+using System.Collections.Generic;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+
+namespace PixiEditorDotNetCore3.Models.Tools.Tools
+{
+    public class FloodFill : Tool
+    {
+        public override ToolType GetToolType => ToolType.Bucket;
+
+        public FloodFill()
+        {
+            ExecutesItself = true;
+        }
+
+        public override BitmapPixelChanges Use(Layer layer, Coordinates startingCoords, Color color, int toolSize)
+        {
+            return ForestFire(layer, startingCoords, color);
+        }
+
+        public BitmapPixelChanges ForestFire(Layer layer, Coordinates startingCoords, Color newColor)
+        {
+            List<Coordinates> changedCoords = new List<Coordinates>();
+
+            Color colorToReplace = layer.LayerBitmap.GetPixel(startingCoords.X, startingCoords.Y);
+
+            var stack = new Stack<Tuple<int, int>>();
+            stack.Push(Tuple.Create(startingCoords.X, startingCoords.Y));
+
+            while (stack.Count > 0)
+            {
+                var point = stack.Pop();
+                if (point.Item1 < 0 || point.Item1 > layer.Height - 1) continue;
+                if (point.Item2 < 0 || point.Item2 > layer.Width - 1) continue;
+                if (layer.LayerBitmap.GetPixel(point.Item1, point.Item2) == newColor) continue;
+
+                if (layer.LayerBitmap.GetPixel(point.Item1, point.Item2) == colorToReplace)
+                {
+                    changedCoords.Add(new Coordinates(point.Item1, point.Item2));
+                    layer.LayerBitmap.SetPixel(point.Item1, point.Item2, newColor);
+                    stack.Push(Tuple.Create(point.Item1, point.Item2 - 1));
+                    stack.Push(Tuple.Create(point.Item1 + 1, point.Item2));
+                    stack.Push(Tuple.Create(point.Item1, point.Item2 + 1));
+                    stack.Push(Tuple.Create(point.Item1 - 1, point.Item2));
+                }
+            }
+            return new BitmapPixelChanges(changedCoords.ToArray(), newColor);
+        }
+    }
+}

+ 3 - 5
PixiEditorDotNetCore3/Models/Tools/Tools/Pen.cs

@@ -7,6 +7,9 @@ namespace PixiEditorDotNetCore3.Models.Tools.Tools
 {
     public class Pen : Tool
     {
+        public override ToolType GetToolType => ToolType.Pen;
+
+
         public override BitmapPixelChanges Use(Layer layer, Coordinates startingCoords, Color color, int toolSize)
         {
             return Draw(startingCoords, color, toolSize);
@@ -22,10 +25,5 @@ namespace PixiEditorDotNetCore3.Models.Tools.Tools
             y2 = centeredCoords.Coords2.Y;
             return new BitmapPixelChanges(CoordinatesCalculator.RectangleToCoordinates(x1, y1, x2, y2), color);
         }
-
-        public override ToolType GetToolType()
-        {
-            return ToolType.Pen;
-        }
     }
 }

+ 3 - 2
PixiEditorDotNetCore3/ViewModels/ViewModelMain.cs

@@ -21,7 +21,8 @@ using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using Xceed.Wpf.Toolkit.Zoombox;
-
+using PixiTools = PixiEditorDotNetCore3.Models.Tools.Tools;
+
 namespace PixiEditor.ViewModels
 {
     class ViewModelMain : ViewModelBase
@@ -155,7 +156,7 @@ namespace PixiEditor.ViewModels
             MouseUpCommand = new RelayCommand(MouseUp);
             RecenterZoomboxCommand = new RelayCommand(RecenterZoombox);
             OpenFileCommand = new RelayCommand(OpenFile);
-            primaryToolSet = new ToolSet(new List<Tool> { new PixiEditorDotNetCore3.Models.Tools.Tools.Pen() });
+            primaryToolSet = new ToolSet(new List<Tool> { new PixiTools.Pen(), new PixiTools.FloodFill() });
             UndoManager.SetMainRoot(this);
         }