Browse Source

Copy visible copies from active viewport and output

Krzysztof Krysiński 4 months ago
parent
commit
a81edde4bf

+ 14 - 2
src/PixiEditor.ChangeableDocument/Rendering/DocumentRenderer.cs

@@ -203,7 +203,7 @@ public class DocumentRenderer : IPreviewRenderable
         return true;
     }
 
-    public void RenderDocument(DrawingSurface toRenderOn, KeyFrameTime frameTime, VecI renderSize)
+    public void RenderDocument(DrawingSurface toRenderOn, KeyFrameTime frameTime, VecI renderSize, string? customOutput = null)
     {
         IsBusy = true;
 
@@ -223,7 +223,19 @@ public class DocumentRenderer : IPreviewRenderable
         RenderContext context =
             new(renderTexture.DrawingSurface, frameTime, ChunkResolution.Full, Document.Size,
                 Document.ProcessingColorSpace) { FullRerender = true };
-        Document.NodeGraph.Execute(context);
+
+        bool hasCustomOutput = !string.IsNullOrEmpty(customOutput) && customOutput != "DEFAULT";
+
+        var graph = hasCustomOutput
+            ? RenderingUtils.SolveFinalNodeGraph(customOutput, Document)
+            : Document.NodeGraph;
+
+        if (hasCustomOutput)
+        {
+            context.TargetOutput = customOutput;
+        }
+
+        graph.Execute(context);
 
         toRenderOn.Canvas.DrawSurface(renderTexture.DrawingSurface, 0, 0);
 

+ 46 - 0
src/PixiEditor.ChangeableDocument/Rendering/RenderingUtils.cs

@@ -0,0 +1,46 @@
+using PixiEditor.ChangeableDocument.Changeables.Graph;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
+using PixiEditor.ChangeableDocument.Changeables.Interfaces;
+
+namespace PixiEditor.ChangeableDocument.Rendering;
+
+public static class RenderingUtils
+{
+    public static IReadOnlyNodeGraph SolveFinalNodeGraph(string? targetOutput, IReadOnlyDocument document)
+    {
+        if (targetOutput == null || targetOutput == "DEFAULT")
+        {
+            return document.NodeGraph;
+        }
+
+        CustomOutputNode[] outputNodes = document.NodeGraph.AllNodes.OfType<CustomOutputNode>().ToArray();
+
+        foreach (CustomOutputNode outputNode in outputNodes)
+        {
+            if (outputNode.OutputName.Value == targetOutput)
+            {
+                return GraphFromOutputNode(outputNode);
+            }
+        }
+
+        return document.NodeGraph;
+    }
+
+    public static IReadOnlyNodeGraph GraphFromOutputNode(CustomOutputNode outputNode)
+    {
+        NodeGraph graph = new();
+        outputNode.TraverseBackwards(n =>
+        {
+            if (n is Node node)
+            {
+                graph.AddNode(node);
+            }
+
+            return true;
+        });
+
+        graph.CustomOutputNode = outputNode;
+        return graph;
+    }
+}

+ 2 - 2
src/PixiEditor/Models/Controllers/ClipboardController.cs

@@ -128,7 +128,7 @@ internal static class ClipboardController
         await Clipboard.SetDataObjectAsync(data);
     }
 
-    public static async Task CopyVisibleToClipboard(DocumentViewModel document)
+    public static async Task CopyVisibleToClipboard(DocumentViewModel document, string? output = null)
     {
         await Clipboard.ClearAsync();
 
@@ -148,7 +148,7 @@ internal static class ClipboardController
         using Surface documentSurface = new Surface(document.SizeBindable);
 
         document.Renderer.RenderDocument(documentSurface.DrawingSurface,
-            document.AnimationDataViewModel.ActiveFrameTime, document.SizeBindable);
+            document.AnimationDataViewModel.ActiveFrameTime, document.SizeBindable, output);
 
         Surface surfaceToCopy = new Surface((VecI)copyArea.Size.Ceiling());
         using Paint paint = new Paint();

+ 2 - 37
src/PixiEditor/Models/Rendering/SceneRenderer.cs

@@ -40,7 +40,7 @@ internal class SceneRenderer
 
         string adjustedTargetOutput = targetOutput ?? "";
 
-        IReadOnlyNodeGraph finalGraph = SolveFinalNodeGraph(targetOutput);
+        IReadOnlyNodeGraph finalGraph = RenderingUtils.SolveFinalNodeGraph(targetOutput, Document);
         bool shouldRerender = ShouldRerender(target, resolution, adjustedTargetOutput, finalGraph);
         if (shouldRerender)
         {
@@ -184,42 +184,7 @@ internal class SceneRenderer
         return solveMatrixDiff;
     }
 
-    private IReadOnlyNodeGraph SolveFinalNodeGraph(string? targetOutput)
-    {
-        if (targetOutput == null)
-        {
-            return Document.NodeGraph;
-        }
-
-        CustomOutputNode[] outputNodes = Document.NodeGraph.AllNodes.OfType<CustomOutputNode>().ToArray();
-
-        foreach (CustomOutputNode outputNode in outputNodes)
-        {
-            if (outputNode.OutputName.Value == targetOutput)
-            {
-                return GraphFromOutputNode(outputNode);
-            }
-        }
-
-        return Document.NodeGraph;
-    }
-
-    private IReadOnlyNodeGraph GraphFromOutputNode(CustomOutputNode outputNode)
-    {
-        NodeGraph graph = new();
-        outputNode.TraverseBackwards(n =>
-        {
-            if (n is Node node)
-            {
-                graph.AddNode(node);
-            }
 
-            return true;
-        });
-
-        graph.CustomOutputNode = outputNode;
-        return graph;
-    }
 
     private bool HighDpiRenderNodePresent(IReadOnlyNodeGraph documentNodeGraph)
     {
@@ -246,7 +211,7 @@ internal class SceneRenderer
         double onionOpacity = animationData.OnionOpacity / 100.0;
         double alphaFalloffMultiplier = 1.0 / animationData.OnionFrames;
 
-        var finalGraph = SolveFinalNodeGraph(targetOutput);
+        var finalGraph = RenderingUtils.SolveFinalNodeGraph(targetOutput, Document);
 
         // Render previous frames'
         for (int i = 1; i <= animationData.OnionFrames; i++)

+ 4 - 1
src/PixiEditor/ViewModels/SubViewModels/ClipboardViewModel.cs

@@ -319,7 +319,10 @@ internal class ClipboardViewModel : SubViewModel<ViewModelMain>
         if (doc is null)
             return;
 
-        await ClipboardController.CopyVisibleToClipboard(doc);
+        await ClipboardController.CopyVisibleToClipboard(
+            doc, Owner.WindowSubViewModel.ActiveWindow is ViewportWindowViewModel viewportWindowViewModel
+                ? viewportWindowViewModel.RenderOutputName
+                : null);
 
         hasImageInClipboard = true;
     }