Selaa lähdekoodia

Rotation works finally

flabbet 3 vuotta sitten
vanhempi
commit
c9a4745956

+ 20 - 17
PixiEditor/Models/DataHolders/Document/Document.Operations.cs

@@ -69,6 +69,9 @@ namespace PixiEditor.Models.DataHolders
             int oldWidth = Width;
             int oldHeight = Height;
 
+            int biggerMaxSize = Math.Max(Width, Height);
+
+
             foreach (var layer in Layers)
             {
                 using (new SKAutoCanvasRestore(layer.LayerBitmap.SkiaSurface.Canvas, true))
@@ -76,29 +79,29 @@ namespace PixiEditor.Models.DataHolders
                     var copy = layer.LayerBitmap.SkiaSurface.Snapshot();
                     layer.LayerBitmap.SkiaSurface.Canvas.Clear();
 
-                    int oldMaxWidth = layer.MaxWidth;
-                    int oldMaxHeight = layer.MaxHeight;
-
-                    int biggerSize = Math.Max(layer.Width, layer.Height);
-                    int biggerMaxSize = Math.Max(layer.MaxWidth, layer.MaxHeight);
-
-                    layer.Width = biggerSize;
-                    layer.Height = biggerSize;
+                    double radians = Math.PI * degrees / 180;
+                    float sine = (float)Math.Abs(Math.Sin(radians));
+                    float cosine = (float)Math.Abs(Math.Cos(radians));
+                    int originalWidth = layer.Width;
+                    int originalHeight = layer.Height;
+                    int rotatedWidth = (int)(cosine * originalWidth + sine * originalHeight);
+                    int rotatedHeight = (int)(cosine * originalHeight + sine * originalWidth);
 
-                    layer.MaxHeight = biggerMaxSize;
-                    layer.MaxWidth = biggerMaxSize;
+                    layer.CreateNewBitmap(rotatedWidth, rotatedHeight);
 
-                    var canvas = layer.LayerBitmap.SkiaSurface.Canvas;
-                    canvas.RotateDegrees(degrees, oldMaxWidth / 2, oldMaxHeight / 2);
-                    canvas.DrawImage(copy, new SKPoint(0, 0));
+                    var surface = layer.LayerBitmap.SkiaSurface.Canvas;
 
-                    layer.Width = oldHeight;
-                    layer.Height = oldWidth;
+                    surface.Translate(rotatedWidth / 2, rotatedHeight / 2);
+                    surface.RotateDegrees((float)degrees);
+                    surface.Translate(-originalWidth / 2, -originalHeight / 2);
+                    surface.DrawImage(copy, new SKPoint());
 
-                    layer.MaxHeight = oldMaxWidth;
-                    layer.MaxWidth = oldMaxHeight;
+                    
+                    layer.MaxHeight = oldWidth;
+                    layer.MaxWidth = oldHeight;
 
                     copy.Dispose();
+                    
                 }
 
                 layer.InvokeLayerBitmapChange();

+ 10 - 1
PixiEditor/Models/Layers/Layer.cs

@@ -1,4 +1,4 @@
-using PixiEditor.Helpers.Extensions;
+using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Undo;
@@ -402,6 +402,15 @@ namespace PixiEditor.Models.Layers
             return result;
         }
 
+        public void CreateNewBitmap(int width, int height)
+        {
+            LayerBitmap = new Surface(width, height);
+            
+            Width = width;
+            Height = height;
+        }
+
+
         /// <summary>
         ///     Resizes canvas to fit pixels outside current bounds. Clamped to MaxHeight and MaxWidth.
         /// </summary>