Browse Source

Fixed copying area

flabbet 10 months ago
parent
commit
f64d66ddb5

+ 5 - 182
src/PixiEditor.ChangeableDocument/Rendering/DocumentRenderer.cs

@@ -25,49 +25,7 @@ public class DocumentRenderer : IPreviewRenderable
     }
 
     private IReadOnlyDocument Document { get; }
-    //public Texture OnionSkinTexture { get; set; }
 
-    /*public Texture RenderDocument(KeyFrameTime frameTime, ChunkResolution resolution, Texture toDrawOn = null,
-        Paint paint = null)
-    {
-        VecI sizeInChunks = Document.Size / resolution.PixelSize();
-
-        sizeInChunks = new VecI(
-            Math.Max(1, sizeInChunks.X),
-            Math.Max(1, sizeInChunks.Y));
-
-        if (toDrawOn is null)
-        {
-            VecI size = new VecI(
-                Math.Min(Document.Size.X, resolution.PixelSize() * sizeInChunks.X),
-                Math.Min(Document.Size.Y, resolution.PixelSize() * sizeInChunks.Y));
-            toDrawOn = new Texture(size);
-        }
-
-        for (int x = 0; x < sizeInChunks.X; x++)
-        {
-            for (int y = 0; y < sizeInChunks.Y; y++)
-            {
-                VecI chunkPos = new(x, y);
-                OneOf<Chunk, EmptyChunk> chunk = RenderChunk(chunkPos, resolution, frameTime);
-                if (chunk.IsT0)
-                {
-                    toDrawOn.DrawingSurface.Canvas.DrawSurface(
-                        chunk.AsT0.Surface.DrawingSurface,
-                        chunkPos.Multiply(new VecI(resolution.PixelSize())), paint);
-                }
-                else
-                {
-                    var pos = chunkPos * resolution.PixelSize();
-                    toDrawOn.DrawingSurface.Canvas.DrawRect(pos.X, pos.Y, resolution.PixelSize(),
-                        resolution.PixelSize(), ClearPaint);
-                }
-            }
-        }
-
-        return toDrawOn;
-    }*/
-    
     public void RenderChunk(VecI chunkPos, ChunkResolution resolution, KeyFrameTime frameTime)
     {
         try
@@ -85,34 +43,6 @@ public class DocumentRenderer : IPreviewRenderable
         }
     }
 
-    public OneOf<Chunk, EmptyChunk> RenderChunk(VecI chunkPos, ChunkResolution resolution,
-        IReadOnlyNode node, KeyFrameTime frameTime, RectI? globalClippingRect = null)
-    {
-        //using RenderingContext context = new(frameTime, chunkPos, resolution, Document.Size);
-        try
-        {
-            /*RectI? transformedClippingRect = TransformClipRect(globalClippingRect, resolution, chunkPos);
-
-            Texture? evaluated = node.Execute(context);
-            if (evaluated is null)
-            {
-                return new EmptyChunk();
-            }
-
-            var result = ChunkFromResult(resolution, transformedClippingRect, evaluated.DrawingSurface.Snapshot(),
-                context);
-            evaluated.Dispose();
-
-            return result;*/
-            return new EmptyChunk();
-        }
-        catch (ObjectDisposedException)
-        {
-            return new EmptyChunk();
-        }
-    }
-
-    
     private static RectI? TransformClipRect(RectI? globalClippingRect, ChunkResolution resolution, VecI chunkPos)
     {
         if (globalClippingRect is not RectI rect)
@@ -140,99 +70,20 @@ public class DocumentRenderer : IPreviewRenderable
     }
     
     
-    public Texture? RenderLayer(Guid nodeId, ChunkResolution resolution, KeyFrameTime frameTime)
+    public void RenderLayer(DrawingSurface renderOn, Guid nodeId, ChunkResolution resolution, KeyFrameTime frameTime)
     {
         var node = Document.FindNode(nodeId);
         
         if (node is null)
         {
-            return null;
+            return;
         }
         
-        VecI sizeInChunks = Document.Size / resolution.PixelSize();
-        
-        sizeInChunks = new VecI(
-            Math.Max(1, sizeInChunks.X),
-            Math.Max(1, sizeInChunks.Y));
-        
-        VecI size = new VecI(
-            Math.Min(Document.Size.X, resolution.PixelSize() * sizeInChunks.X),
-            Math.Min(Document.Size.Y, resolution.PixelSize() * sizeInChunks.Y));
-        
-        Texture texture = new(size);
-        
-        for (int x = 0; x < sizeInChunks.X; x++)
-        {
-            for (int y = 0; y < sizeInChunks.Y; y++)
-            {
-                VecI chunkPos = new(x, y);
-                RectI globalClippingRect = new(0, 0, Document.Size.X, Document.Size.Y);
-                OneOf<Chunk, EmptyChunk> chunk = RenderChunk(chunkPos, resolution, node, frameTime, globalClippingRect);
-                if (chunk.IsT0)
-                {
-                    VecI pos = chunkPos * resolution.PixelSize(); 
-                    texture.DrawingSurface.Canvas.DrawSurface(
-                        chunk.AsT0.Surface.DrawingSurface,
-                        pos.X, pos.Y, null);
-                }
-            }
-        }
+        using RenderContext context = new(renderOn, frameTime, resolution, Document.Size);
+        context.IsExportRender = true;
         
-        return texture;
-    }
-
-    /*
-    private static OneOf<Chunk, EmptyChunk> RenderChunkOnGraph(VecI chunkPos, ChunkResolution resolution,
-        RectI? globalClippingRect,
-        IReadOnlyNodeGraph graph, RenderingContext context)
-    {
-        RectI? transformedClippingRect = TransformClipRect(globalClippingRect, resolution, chunkPos);
-
-        Texture? evaluated = graph.Execute(context);
-        if (evaluated is null)
-        {
-            return new EmptyChunk();
-        }
-
-        Chunk chunk = Chunk.Create(resolution);
-
-        chunk.Surface.DrawingSurface.Canvas.Save();
-        chunk.Surface.DrawingSurface.Canvas.Clear();
-
-        if (transformedClippingRect is not null)
-        {
-            chunk.Surface.DrawingSurface.Canvas.ClipRect((RectD)transformedClippingRect);
-        }
-
-        VecD pos = chunkPos;
-        int x = (int)(pos.X * ChunkyImage.FullChunkSize * resolution.Multiplier());
-        int y = (int)(pos.Y * ChunkyImage.FullChunkSize * resolution.Multiplier());
-        int width = (int)(ChunkyImage.FullChunkSize * resolution.Multiplier());
-        int height = (int)(ChunkyImage.FullChunkSize * resolution.Multiplier());
-
-        RectD sourceRect = new(x, y, width, height);
-
-        RectD availableRect = new(0, 0, evaluated.Size.X, evaluated.Size.Y);
-
-        sourceRect = sourceRect.Intersect(availableRect);
-
-        if (sourceRect.IsZeroOrNegativeArea)
-        {
-            chunk.Dispose();
-            return new EmptyChunk();
-        }
-
-        using var chunkSnapshot = evaluated.DrawingSurface.Snapshot((RectI)sourceRect);
-
-        if (context.IsDisposed) return new EmptyChunk();
-
-        chunk.Surface.DrawingSurface.Canvas.DrawImage(chunkSnapshot, 0, 0, context.ReplacingPaintWithOpacity);
-
-        chunk.Surface.DrawingSurface.Canvas.Restore();
-
-        return chunk;
+        node.Execute(context);
     }
-    */
 
     public static IReadOnlyNodeGraph ConstructMembersOnlyGraph(IReadOnlyNodeGraph fullGraph)
     {
@@ -272,34 +123,6 @@ public class DocumentRenderer : IPreviewRenderable
         return membersOnlyGraph;
     }
 
-    private static OneOf<Chunk, EmptyChunk> ChunkFromResult(
-        ChunkResolution resolution,
-        RectI? transformedClippingRect, Image evaluated,
-        RenderContext context)
-    {
-        Chunk chunk = Chunk.Create(resolution);
-
-        chunk.Surface.DrawingSurface.Canvas.Save();
-        chunk.Surface.DrawingSurface.Canvas.Clear();
-
-        int x = 0;
-        int y = 0;
-
-        if (transformedClippingRect is not null)
-        {
-            chunk.Surface.DrawingSurface.Canvas.ClipRect((RectD)transformedClippingRect);
-            x = transformedClippingRect.Value.X;
-            y = transformedClippingRect.Value.Y;
-        }
-
-        chunk.Surface.DrawingSurface.Canvas.DrawImage(evaluated, x, y,
-            context.ReplacingPaintWithOpacity);
-
-        chunk.Surface.DrawingSurface.Canvas.Restore();
-
-        return chunk;
-    }
-
     public RectD? GetPreviewBounds(int frame, string elementNameToRender = "") => 
         new(0, 0, Document.Size.X, Document.Size.Y); 
 

+ 2 - 4
src/PixiEditor/ViewModels/Document/DocumentViewModel.Serialization.cs

@@ -210,10 +210,8 @@ internal partial class DocumentViewModel
         Image toSave = null;
         DrawingBackendApi.Current.RenderingDispatcher.Invoke(() =>
         {
-            using Texture rendered = Renderer.RenderLayer(imageNode.Id, ChunkResolution.Full, atTime.Frame);
-
-            using Surface surface = new Surface(rendered.Size);
-            surface.DrawingSurface.Canvas.DrawImage(rendered.DrawingSurface.Snapshot(), 0, 0);
+            using Surface surface = new Surface(SizeBindable); 
+            Renderer.RenderLayer(surface.DrawingSurface, imageNode.Id, ChunkResolution.Full, atTime.Frame);
 
             toSave = surface.DrawingSurface.Snapshot((RectI)tightBounds.Value);
         });

+ 13 - 6
src/PixiEditor/ViewModels/Document/DocumentViewModel.cs

@@ -522,7 +522,7 @@ internal partial class DocumentViewModel : PixiObservableObject, IDocument
             Surface finalSurface = new Surface(SizeBindable);
             DrawingBackendApi.Current.RenderingDispatcher.Invoke(() =>
             {
-                Renderer.RenderDocument(finalSurface.DrawingSurface, frameTime); 
+                Renderer.RenderDocument(finalSurface.DrawingSurface, frameTime);
             });
 
             return finalSurface;
@@ -550,7 +550,7 @@ internal partial class DocumentViewModel : PixiObservableObject, IDocument
 
         for (int i = 0; i < selectedLayers.Count; i++)
         {
-            var layerVm = StructureHelper.Find(selectedLayers[i]); 
+            var layerVm = StructureHelper.Find(selectedLayers[i]);
             IReadOnlyStructureNode? layer = Internals.Tracker.Document.FindMember(layerVm.Id);
             if (layer is null)
                 return new Error();
@@ -601,6 +601,7 @@ internal partial class DocumentViewModel : PixiObservableObject, IDocument
         {
             output.DrawingSurface.Canvas.ClipPath(clipPath);
         }
+
         using Paint paint = new Paint() { BlendMode = BlendMode.SrcOver };
 
         foreach (var layer in selectedLayers)
@@ -608,10 +609,16 @@ internal partial class DocumentViewModel : PixiObservableObject, IDocument
             try
             {
                 var layerVm = Internals.Tracker.Document.FindMember(layer);
-                using Texture rendered = Renderer.RenderLayer(layerVm.Id, ChunkResolution.Full,
-                    AnimationDataViewModel.ActiveFrameTime);
-                using Image snapshot = rendered.DrawingSurface.Snapshot(bounds);
-                output.DrawingSurface.Canvas.DrawImage(snapshot, 0, 0, paint);
+
+                DrawingBackendApi.Current.RenderingDispatcher.Invoke(() =>
+                {
+                    using Surface toPaintOn = new Surface(SizeBindable);
+
+                    Renderer.RenderLayer(toPaintOn.DrawingSurface, layerVm.Id, ChunkResolution.Full,
+                        AnimationDataViewModel.ActiveFrameTime);
+                    using Image snapshot = toPaintOn.DrawingSurface.Snapshot(bounds);
+                    output.DrawingSurface.Canvas.DrawImage(snapshot, 0, 0, paint);
+                });
             }
             catch (ObjectDisposedException)
             {