Browse Source

Added tolerance to flood fill

flabbet 9 months ago
parent
commit
835e407e66

+ 2 - 1
src/PixiEditor.ChangeableDocument/Changes/Drawing/FloodFill/FloodFillHelper.cs

@@ -42,6 +42,7 @@ public static class FloodFillHelper
         VectorPath? selection,
         VecI startingPos,
         Color drawingColor,
+        float tolerance,
         int frame)
     {
         if (selection is not null && !selection.Contains(startingPos.X + 0.5f, startingPos.Y + 0.5f))
@@ -66,7 +67,7 @@ public static class FloodFillHelper
 
         // Pre-multiplies the color and convert it to floats. Since floats are imprecise, a range is used.
         // Used for faster pixel checking
-        ColorBounds colorRange = new(colorToReplace);
+        ColorBounds colorRange = new(colorToReplace, tolerance);
         ulong uLongColor = drawingColor.ToULong();
 
         Dictionary<VecI, Chunk> drawingChunks = new();

+ 4 - 2
src/PixiEditor.ChangeableDocument/Changes/Drawing/FloodFill/FloodFill_Change.cs

@@ -14,9 +14,10 @@ internal class FloodFill_Change : Change
     private readonly bool drawOnMask;
     private CommittedChunkStorage? chunkStorage = null;
     private int frame;
+    private float tolerance;
 
     [GenerateMakeChangeAction]
-    public FloodFill_Change(Guid memberGuid, VecI pos, Color color, bool referenceAll, bool drawOnMask, int frame)
+    public FloodFill_Change(Guid memberGuid, VecI pos, Color color, bool referenceAll, float tolerance, bool drawOnMask, int frame)
     {
         this.memberGuid = memberGuid;
         this.pos = pos;
@@ -24,6 +25,7 @@ internal class FloodFill_Change : Change
         this.referenceAll = referenceAll;
         this.drawOnMask = drawOnMask;
         this.frame = frame;
+        this.tolerance = tolerance;
     }
 
     public override bool InitializeAndValidate(Document target)
@@ -44,7 +46,7 @@ internal class FloodFill_Change : Change
             target.ForEveryReadonlyMember(member => membersToReference.Add(member.Id));
         else
             membersToReference.Add(memberGuid);
-        var floodFilledChunks = FloodFillHelper.FloodFill(membersToReference, target, selection, pos, color, frame);
+        var floodFilledChunks = FloodFillHelper.FloodFill(membersToReference, target, selection, pos, color, tolerance, frame);
         if (floodFilledChunks.Count == 0)
         {
             ignoreInUndo = true;

+ 4 - 2
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/FloodFillToolExecutor.cs

@@ -16,6 +16,7 @@ internal class FloodFillToolExecutor : UpdateableChangeExecutor
     private bool drawOnMask;
     private Guid memberGuid;
     private Color color;
+    private float tolerance;
 
     public override ExecutionState Start()
     {
@@ -37,15 +38,16 @@ internal class FloodFillToolExecutor : UpdateableChangeExecutor
         considerAllLayers = fillTool.ConsiderAllLayers;
         color = colorsVM.PrimaryColor;
         var pos = controller!.LastPixelPosition;
+        tolerance = fillTool.Tolerance;
 
-        internals!.ActionAccumulator.AddActions(new FloodFill_Action(memberGuid, pos, color, considerAllLayers, drawOnMask, document!.AnimationHandler.ActiveFrameBindable));
+        internals!.ActionAccumulator.AddActions(new FloodFill_Action(memberGuid, pos, color, considerAllLayers, tolerance, drawOnMask, document!.AnimationHandler.ActiveFrameBindable));
 
         return ExecutionState.Success;
     }
 
     public override void OnPixelPositionChange(VecI pos)
     {
-        internals!.ActionAccumulator.AddActions(new FloodFill_Action(memberGuid, pos, color, considerAllLayers, drawOnMask, document!.AnimationHandler.ActiveFrameBindable));
+        internals!.ActionAccumulator.AddActions(new FloodFill_Action(memberGuid, pos, color, considerAllLayers, tolerance, drawOnMask, document!.AnimationHandler.ActiveFrameBindable));
     }
 
     public override void OnLeftMouseButtonUp()

+ 1 - 0
src/PixiEditor/Models/Handlers/Tools/IFloodFillToolHandler.cs

@@ -3,4 +3,5 @@
 internal interface IFloodFillToolHandler : IToolHandler
 {
     public bool ConsiderAllLayers { get; }
+    public float Tolerance { get; }
 }

+ 5 - 0
src/PixiEditor/ViewModels/Tools/Tools/FloodFillToolViewModel.cs

@@ -7,6 +7,7 @@ using PixiEditor.Models.Handlers;
 using PixiEditor.Models.Handlers.Tools;
 using Drawie.Numerics;
 using PixiEditor.UI.Common.Fonts;
+using PixiEditor.ViewModels.Tools.ToolSettings.Toolbars;
 using PixiEditor.Views.Overlays.BrushShapeOverlay;
 
 namespace PixiEditor.ViewModels.Tools.Tools;
@@ -28,10 +29,14 @@ internal class FloodFillToolViewModel : ToolViewModel, IFloodFillToolHandler
 
     public bool ConsiderAllLayers { get; private set; }
 
+    [Settings.Percent("TOLERANCE_LABEL", ExposedByDefault = false)]
+    public float Tolerance => GetValue<float>();
+
     public override string Icon => PixiPerfectIcons.Bucket;
 
     public FloodFillToolViewModel()
     {
+        Toolbar = ToolbarFactory.Create(this);
         ActionDisplay = defaultActionDisplay;
     }