Browse Source

Fixed scaling in preview painter

flabbet 7 months ago
parent
commit
9ba9cb340d

+ 6 - 1
src/PixiEditor/Models/Rendering/PreviewPainter.cs

@@ -1,6 +1,7 @@
 using Avalonia;
 using ChunkyImageLib.DataHolders;
 using Drawie.Backend.Core;
+using Drawie.Backend.Core.Numerics;
 using PixiEditor.ChangeableDocument.Changeables.Animations;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
 using Drawie.Backend.Core.Surfaces;
@@ -30,7 +31,7 @@ public class PreviewPainter
         DocumentSize = documentSize;
     }
 
-    public void Paint(DrawingSurface renderOn, VecI boundsSize) 
+    public void Paint(DrawingSurface renderOn, VecI boundsSize, Matrix3X3 matrix) 
     {
         if (PreviewRenderable == null)
         {
@@ -44,10 +45,14 @@ public class PreviewPainter
         }
         
         renderTexture.DrawingSurface.Canvas.Clear();
+        renderTexture.DrawingSurface.Canvas.Save();
+
+        renderTexture.DrawingSurface.Canvas.SetMatrix(matrix);
         
         RenderContext context = new(renderTexture.DrawingSurface, FrameTime, ChunkResolution.Full, DocumentSize, ProcessingColorSpace);
 
         PreviewRenderable.RenderPreview(renderTexture.DrawingSurface, context, ElementToRenderName);
+        renderTexture.DrawingSurface.Canvas.Restore();
         
         renderOn.Canvas.DrawSurface(renderTexture.DrawingSurface, 0, 0);
     }

+ 7 - 5
src/PixiEditor/Views/Visuals/PreviewPainterControl.cs

@@ -1,5 +1,6 @@
 using Avalonia;
 using ChunkyImageLib.DataHolders;
+using Drawie.Backend.Core.Numerics;
 using Drawie.Backend.Core.Surfaces;
 using Drawie.Interop.Avalonia.Core.Controls;
 using PixiEditor.Models.Rendering;
@@ -74,17 +75,18 @@ public class PreviewPainterControl : DrawieControl
 
         surface.Canvas.Save();
 
+        Matrix3X3 matrix = Matrix3X3.Identity;
         if (previewBounds != null)
         {
-            UniformScale(x, y, surface, previewBounds.Value);
+            matrix = UniformScale(x, y, previewBounds.Value);
         }
 
-        PreviewPainter.Paint(surface, new VecI((int)Bounds.Size.Width, (int)Bounds.Size.Height));
+        PreviewPainter.Paint(surface, new VecI((int)Bounds.Size.Width, (int)Bounds.Size.Height), matrix);
 
         surface.Canvas.Restore();
     }
 
-    private void UniformScale(float x, float y, DrawingSurface target, RectD previewBounds)
+    private Matrix3X3 UniformScale(float x, float y,  RectD previewBounds)
     {
         float scaleX = (float)Bounds.Width / x;
         float scaleY = (float)Bounds.Height / y;
@@ -93,7 +95,7 @@ public class PreviewPainterControl : DrawieControl
         dX -= (float)previewBounds.X;
         float dY = (float)Bounds.Height / 2 / scale - y / 2;
         dY -= (float)previewBounds.Y;
-        target.Canvas.Scale(scale, scale);
-        target.Canvas.Translate(dX, dY);
+        Matrix3X3 matrix = Matrix3X3.CreateScale(scale, scale);
+        return matrix.Concat(Matrix3X3.CreateTranslation(dX, dY));
     }
 }