Browse Source

Added DrawUpToDateChunkyImage method

flabbet 1 year ago
parent
commit
2d3e29faef

+ 10 - 4
src/ChunkyImageLib/ChunkyImage.cs

@@ -664,16 +664,22 @@ public class ChunkyImage : IReadOnlyChunkyImage, IDisposable, ICloneable
     }
 
     /// <exception cref="ObjectDisposedException">This image is disposed</exception>
-    public void EnqueueDrawChunkyImage(VecI pos, ChunkyImage image, bool flipHor = false, bool flipVer = false)
+    public void EnqueueDrawCommitedChunkyImage(VecI pos, ChunkyImage image, bool flipHor = false, bool flipVer = false)
     {
         lock (lockObject)
         {
             ThrowIfDisposed();
-            ChunkyImageOperation operation = new(image, pos, flipHor, flipVer);
+            ChunkyImageOperation operation = new(image, pos, flipHor, flipVer, false);
             EnqueueOperation(operation);
         }
     }
-
+    public void EnqueueDrawUpToDateChunkyImage(VecI pos, ChunkyImage image, bool flipHor = false, bool flipVer = false)
+    {
+        ThrowIfDisposed();
+        ChunkyImageOperation operation = new(image, pos, flipHor, flipVer, true);
+        EnqueueOperation(operation);
+    }
+    
     /// <exception cref="ObjectDisposedException">This image is disposed</exception>
     public void EnqueueClearRegion(RectI region)
     {
@@ -1323,5 +1329,5 @@ public class ChunkyImage : IReadOnlyChunkyImage, IDisposable, ICloneable
             ChunkyImage clone = CloneFromCommitted();
             return clone;
         }
-    }
+    } 
 }

+ 22 - 11
src/ChunkyImageLib/Operations/ChunkyImageOperation.cs

@@ -1,5 +1,7 @@
 using ChunkyImageLib.DataHolders;
 using PixiEditor.DrawingApi.Core.Numerics;
+using PixiEditor.DrawingApi.Core.Surface;
+using PixiEditor.DrawingApi.Core.Surface.PaintImpl;
 using PixiEditor.Numerics;
 
 namespace ChunkyImageLib.Operations;
@@ -9,15 +11,18 @@ internal class ChunkyImageOperation : IMirroredDrawOperation
     private readonly VecI targetPos;
     private readonly bool mirrorHorizontal;
     private readonly bool mirrorVertical;
+    private readonly bool drawUpToDate;
 
     public bool IgnoreEmptyChunks => false;
 
-    public ChunkyImageOperation(ChunkyImage imageToDraw, VecI targetPos, bool mirrorHorizontal, bool mirrorVertical)
+    public ChunkyImageOperation(ChunkyImage imageToDraw, VecI targetPos, bool mirrorHorizontal, bool mirrorVertical,
+        bool drawUpToDate)
     {
         this.imageToDraw = imageToDraw;
         this.targetPos = targetPos;
         this.mirrorHorizontal = mirrorHorizontal;
         this.mirrorVertical = mirrorVertical;
+        this.drawUpToDate = drawUpToDate;
     }
 
     public void DrawOnChunk(Chunk targetChunk, VecI chunkPos)
@@ -28,7 +33,7 @@ internal class ChunkyImageOperation : IMirroredDrawOperation
             VecI topLeftImageCorner = GetTopLeft();
             RectD clippingRect = RectD.Create(
                 OperationHelper.ConvertForResolution(topLeftImageCorner - pixelPos, targetChunk.Resolution),
-                OperationHelper.ConvertForResolution(imageToDraw.CommittedSize, targetChunk.Resolution));
+                OperationHelper.ConvertForResolution(drawUpToDate ? imageToDraw.LatestSize : imageToDraw.CommittedSize, targetChunk.Resolution));
             targetChunk.Surface.DrawingSurface.Canvas.ClipRect(clippingRect);
         }
 
@@ -58,36 +63,41 @@ internal class ChunkyImageOperation : IMirroredDrawOperation
         VecI bottomLeft = OperationHelper.GetChunkPos(
             new VecI(chunkCenterOnImage.X - halfChunk.X, chunkCenterOnImage.Y + halfChunk.Y), ChunkyImage.FullChunkSize);
 
-        imageToDraw.DrawCommittedChunkOn(
+        Func<VecI, ChunkResolution, DrawingSurface, VecI, Paint?, bool> drawMethod = drawUpToDate ? imageToDraw.DrawMostUpToDateChunkOn : imageToDraw.DrawCommittedChunkOn;
+        
+        drawMethod(
             topLeft,
             targetChunk.Resolution,
             targetChunk.Surface.DrawingSurface,
-            (VecI)((topLeft * ChunkyImage.FullChunkSize - chunkCenterOnImage).Add(ChunkyImage.FullChunkSize / 2) * targetChunk.Resolution.Multiplier()));
+            (VecI)((topLeft * ChunkyImage.FullChunkSize - chunkCenterOnImage).Add(ChunkyImage.FullChunkSize / 2) * targetChunk.Resolution.Multiplier()), null);
 
         VecI gridShift = targetPos % ChunkyImage.FullChunkSize;
         if (gridShift.X != 0)
         {
-            imageToDraw.DrawCommittedChunkOn(
+            drawMethod(
             topRight,
             targetChunk.Resolution,
             targetChunk.Surface.DrawingSurface,
-            (VecI)((topRight * ChunkyImage.FullChunkSize - chunkCenterOnImage).Add(ChunkyImage.FullChunkSize / 2) * targetChunk.Resolution.Multiplier()));
+            (VecI)((topRight * ChunkyImage.FullChunkSize - chunkCenterOnImage).Add(ChunkyImage.FullChunkSize / 2) * targetChunk.Resolution.Multiplier()),
+            null);
         }
         if (gridShift.Y != 0)
         {
-            imageToDraw.DrawCommittedChunkOn(
+            drawMethod(
             bottomLeft,
             targetChunk.Resolution,
             targetChunk.Surface.DrawingSurface,
-            (VecI)((bottomLeft * ChunkyImage.FullChunkSize - chunkCenterOnImage).Add(ChunkyImage.FullChunkSize / 2) * targetChunk.Resolution.Multiplier()));
+            (VecI)((bottomLeft * ChunkyImage.FullChunkSize - chunkCenterOnImage).Add(ChunkyImage.FullChunkSize / 2) * targetChunk.Resolution.Multiplier()),
+            null);
         }
         if (gridShift.X != 0 && gridShift.Y != 0)
         {
-            imageToDraw.DrawCommittedChunkOn(
+            drawMethod(
             bottomRight,
             targetChunk.Resolution,
             targetChunk.Surface.DrawingSurface,
-            (VecI)((bottomRight * ChunkyImage.FullChunkSize - chunkCenterOnImage).Add(ChunkyImage.FullChunkSize / 2) * targetChunk.Resolution.Multiplier()));
+            (VecI)((bottomRight * ChunkyImage.FullChunkSize - chunkCenterOnImage).Add(ChunkyImage.FullChunkSize / 2) * targetChunk.Resolution.Multiplier()),
+            null);
         }
 
         targetChunk.Surface.DrawingSurface.Canvas.Restore();
@@ -116,7 +126,8 @@ internal class ChunkyImageOperation : IMirroredDrawOperation
             newPos = (VecI)newPos.ReflectX((double)verAxisX).Round();
         if (horAxisY is not null)
             newPos = (VecI)newPos.ReflectY((double)horAxisY).Round();
-        return new ChunkyImageOperation(imageToDraw, newPos, mirrorHorizontal ^ (verAxisX is not null), mirrorVertical ^ (horAxisY is not null));
+        return new ChunkyImageOperation(imageToDraw, newPos, mirrorHorizontal ^ (verAxisX is not null), mirrorVertical ^ (horAxisY is not null),
+            drawUpToDate);
     }
 
     public void Dispose() { }

+ 2 - 2
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/ImageLayerNode.cs

@@ -35,8 +35,8 @@ public class ImageLayerNode : LayerNode, IReadOnlyImageNode
         {
             VecI size = GetBiggerSize(frameImage.LatestSize, Background.Value.LatestSize);
             ChunkyImage combined = new(size);
-            combined.EnqueueDrawChunkyImage(VecI.Zero, Background.Value);
-            combined.EnqueueDrawChunkyImage(VecI.Zero, frameImage);
+            combined.EnqueueDrawUpToDateChunkyImage(VecI.Zero, Background.Value);
+            combined.EnqueueDrawUpToDateChunkyImage(VecI.Zero, frameImage);
             combined.CommitChanges();
             
             Output.Value = combined;

+ 2 - 2
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/MergeNode.cs

@@ -34,12 +34,12 @@ public class MergeNode : Node
         
         if (Bottom.Value != null)
         {
-            Output.Value.EnqueueDrawChunkyImage(VecI.Zero, Bottom.Value);
+            Output.Value.EnqueueDrawCommitedChunkyImage(VecI.Zero, Bottom.Value);
         }
         
         if (Top.Value != null)
         {
-            Output.Value.EnqueueDrawChunkyImage(VecI.Zero, Top.Value);
+            Output.Value.EnqueueDrawCommitedChunkyImage(VecI.Zero, Top.Value);
         }
         
         Output.Value.CommitChanges();

+ 1 - 1
src/PixiEditor.ChangeableDocument/Changes/Drawing/ApplyLayerMask_Change.cs

@@ -39,7 +39,7 @@ internal class ApplyLayerMask_Change : Change
         var layerImage = layer.GetLayerImageAtFrame(frame);
         ChunkyImage newLayerImage = new ChunkyImage(target.Size);
         newLayerImage.AddRasterClip(layer.Mask.Value);
-        newLayerImage.EnqueueDrawChunkyImage(VecI.Zero, layerImage);
+        newLayerImage.EnqueueDrawCommitedChunkyImage(VecI.Zero, layerImage);
         newLayerImage.CommitChanges();
 
         var affectedChunks = layerImage.FindAllChunks();

+ 1 - 1
src/PixiEditor.ChangeableDocument/Changes/Drawing/ShiftLayerHelper.cs

@@ -13,7 +13,7 @@ internal static class ShiftLayerHelper
         targetImage.CancelChanges();
         if (!keepOriginal)
             targetImage.EnqueueClear();
-        targetImage.EnqueueDrawChunkyImage(delta, targetImage, false, false);
+        targetImage.EnqueueDrawCommitedChunkyImage(delta, targetImage, false, false);
         var curArea = targetImage.FindAffectedArea();
 
         curArea.UnionWith(prevArea);

+ 1 - 1
src/PixiEditor.ChangeableDocument/Changes/Root/ResizeBasedChangeBase.cs

@@ -33,7 +33,7 @@ internal abstract class ResizeBasedChangeBase : Change
     {
         img.EnqueueResize(size);
         img.EnqueueClear();
-        img.EnqueueDrawChunkyImage(offset, img);
+        img.EnqueueDrawCommitedChunkyImage(offset, img);
 
         deletedChunksDict.Add(memberGuid, new CommittedChunkStorage(img, img.FindAffectedArea().Chunks));
         img.CommitChanges();