Browse Source

Fix pen highlight not lining up with what gets drawn

Equbuxu 3 years ago
parent
commit
545e098c1f

+ 4 - 4
src/ChunkyImageLib/Operations/EllipseOperation.cs

@@ -10,8 +10,8 @@ internal class EllipseOperation : IDrawOperation
     private readonly SKColor strokeColor;
     private readonly SKColor fillColor;
     private readonly int strokeWidth;
-    private bool init = false;
     private readonly SKPaint paint;
+    private bool init = false;
     private SKPath? outerPath;
     private SKPath? innerPath;
     private SKPoint[]? ellipse;
@@ -34,7 +34,7 @@ internal class EllipseOperation : IDrawOperation
         {
             var ellipseList = EllipseHelper.GenerateEllipseFromRect(location);
             ellipse = ellipseList.Select(a => (SKPoint)a).ToArray();
-            if (fillColor.Alpha > 0)
+            if (fillColor.Alpha > 0 || paint.BlendMode != SKBlendMode.SrcOver)
             {
                 (var fill, ellipseFillRect) = EllipseHelper.SplitEllipseIntoRegions(ellipseList, location);
                 ellipseFill = fill.Select(a => (SKPoint)a).ToArray();
@@ -62,7 +62,7 @@ internal class EllipseOperation : IDrawOperation
 
         if (strokeWidth == 1)
         {
-            if (fillColor.Alpha > 0)
+            if (fillColor.Alpha > 0 || paint.BlendMode != SKBlendMode.SrcOver)
             {
                 paint.Color = fillColor;
                 surf.Canvas.DrawPoints(SKPointMode.Lines, ellipseFill, paint);
@@ -73,7 +73,7 @@ internal class EllipseOperation : IDrawOperation
         }
         else
         {
-            if (fillColor.Alpha > 0)
+            if (fillColor.Alpha > 0 || paint.BlendMode != SKBlendMode.SrcOver)
             {
                 surf.Canvas.Save();
                 surf.Canvas.ClipPath(innerPath);

+ 25 - 3
src/PixiEditor.ChangeableDocument/Changes/Drawing/LineBasedPen_UpdateableChange.cs

@@ -8,6 +8,7 @@ internal class LineBasedPen_UpdateableChange : UpdateableChange
     private readonly int strokeWidth;
     private readonly bool replacing;
     private readonly bool drawOnMask;
+    private readonly SKPaint srcPaint = new SKPaint() { BlendMode = SKBlendMode.Src };
 
     private CommittedChunkStorage? storedChunks;
     private readonly List<VecI> points = new();
@@ -51,9 +52,15 @@ internal class LineBasedPen_UpdateableChange : UpdateableChange
         int opCount = image.QueueLength;
 
         if (strokeWidth == 1)
+        {
             image.EnqueueDrawBresenhamLine(from, to, color, SKBlendMode.Src);
+        }
         else
-            image.EnqueueDrawSkiaLine(from, to, SKStrokeCap.Round, strokeWidth, color, SKBlendMode.Src);
+        {
+            var rect = new RectI(to - new VecI(strokeWidth / 2), new VecI(strokeWidth));
+            image.EnqueueDrawEllipse(rect, color, color, 1, srcPaint);
+            image.EnqueueDrawSkiaLine(from, to, SKStrokeCap.Butt, strokeWidth, color, SKBlendMode.Src);
+        }
         var affChunks = image.FindAffectedChunks(opCount);
 
         return DrawingChangeHelper.CreateChunkChangeInfo(memberGuid, affChunks, drawOnMask);
@@ -64,17 +71,32 @@ internal class LineBasedPen_UpdateableChange : UpdateableChange
         if (points.Count == 1)
         {
             if (strokeWidth == 1)
+            {
                 targetImage.EnqueueDrawBresenhamLine(points[0], points[0], color, SKBlendMode.Src);
+            }
             else
-                targetImage.EnqueueDrawSkiaLine(points[0], points[0], SKStrokeCap.Round, strokeWidth, color, SKBlendMode.Src);
+            {
+                var rect = new RectI(points[0] - new VecI(strokeWidth / 2), new VecI(strokeWidth));
+                targetImage.EnqueueDrawEllipse(rect, color, color, 1, srcPaint);
+            }
             return;
         }
+
+        var firstRect = new RectI(points[0] - new VecI(strokeWidth / 2), new VecI(strokeWidth));
+        targetImage.EnqueueDrawEllipse(firstRect, color, color, 1, srcPaint);
+
         for (int i = 1; i < points.Count; i++)
         {
             if (strokeWidth == 1)
+            {
                 targetImage.EnqueueDrawBresenhamLine(points[i - 1], points[i], color, SKBlendMode.Src);
+            }
             else
-                targetImage.EnqueueDrawSkiaLine(points[i - 1], points[i], SKStrokeCap.Round, strokeWidth, color, SKBlendMode.Src);
+            {
+                var rect = new RectI(points[i] - new VecI(strokeWidth / 2), new VecI(strokeWidth));
+                targetImage.EnqueueDrawEllipse(rect, color, color, 1, srcPaint);
+                targetImage.EnqueueDrawSkiaLine(points[i - 1], points[i], SKStrokeCap.Butt, strokeWidth, color, SKBlendMode.Src);
+            }
         }
     }