Browse Source

Some improvements, I guess

flabbet 3 years ago
parent
commit
82c24498bf

+ 27 - 2
PixiEditor/Models/DataHolders/Document/Document.Operations.cs

@@ -1,6 +1,7 @@
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.Enums;
 using PixiEditor.Models.Layers;
+using PixiEditor.Models.Position;
 using PixiEditor.Models.Undo;
 using SkiaSharp;
 using System;
@@ -89,17 +90,41 @@ namespace PixiEditor.Models.DataHolders
 
                     var canvas = layer.LayerBitmap.SkiaSurface.Canvas;
 
+                    layer.ClipCanvas();
+
                     if (flip == FlipType.Horizontal)
                     {
-                        canvas.Translate(layer.MaxWidth, 0);
+                        canvas.Translate(layer.Width, 0);
                         canvas.Scale(-1, 1, 0, 0);
                     }
                     else
                     {
-                        canvas.Translate(0, layer.MaxHeight);
+                        canvas.Translate(0, layer.Width);
                         canvas.Scale(1, -1, 0, 0);
                     }
 
+                    // Flip offset based on document and layer center point
+                    var documentCenter = new Coordinates(Width / 2, Height / 2);
+                    var layerCenter = new Coordinates(layer.Width / 2, layer.Height / 2);
+
+                    int newOffsetX = layer.OffsetX;
+                    int newOffsetY = layer.OffsetY;
+
+                    if (flip == FlipType.Horizontal)
+                    {
+                        newOffsetX += layerCenter.X;
+                        int diff = documentCenter.X - newOffsetX;
+                        newOffsetX = layer.OffsetX + (diff * 2);
+                    }
+                    else if(flip == FlipType.Vertical)
+                    {
+                        newOffsetY += layerCenter.Y;
+                        int diff = documentCenter.Y - newOffsetY;
+                        newOffsetY = layer.OffsetY + (diff * 2);
+                    }
+
+                    layer.Offset = new Thickness(newOffsetX, newOffsetY, 0, 0);
+
                     canvas.DrawImage(copy, default(SKPoint));
                     copy.Dispose();
                 }

+ 16 - 8
PixiEditor/Models/Layers/Layer.cs

@@ -405,7 +405,7 @@ namespace PixiEditor.Models.Layers
         public void CreateNewBitmap(int width, int height)
         {
             LayerBitmap = new Surface(width, height);
-            
+
             Width = width;
             Height = height;
         }
@@ -476,10 +476,7 @@ namespace PixiEditor.Models.Layers
             }
         }
 
-        /// <summary>
-        ///     Changes size of bitmap to fit content.
-        /// </summary>
-        public void ClipCanvas()
+        public Int32Rect GetContentDimensions()
         {
             DoubleCords points = GetEdgePoints();
             int smallestX = points.Coords1.X;
@@ -489,13 +486,24 @@ namespace PixiEditor.Models.Layers
 
             if (smallestX < 0 && smallestY < 0 && biggestX < 0 && biggestY < 0)
             {
-                return;
+                return Int32Rect.Empty;
             }
 
             int width = biggestX - smallestX + 1;
             int height = biggestY - smallestY + 1;
-            ResizeCanvas(0, 0, smallestX, smallestY, width, height);
-            Offset = new Thickness(OffsetX + smallestX, OffsetY + smallestY, 0, 0);
+            return new Int32Rect(smallestX, smallestY, width, height);
+        }
+
+        /// <summary>
+        ///     Changes size of bitmap to fit content.
+        /// </summary>
+        public void ClipCanvas()
+        {
+            var dimensions = GetContentDimensions();
+            if (dimensions == Int32Rect.Empty) return;
+
+            ResizeCanvas(0, 0, dimensions.X, dimensions.Y, dimensions.Width, dimensions.Height);
+            Offset = new Thickness(OffsetX + dimensions.X, OffsetY + dimensions.Y, 0, 0);
         }
 
         public void Reset()

+ 5 - 0
PixiEditor/Models/Position/Coordinates.cs

@@ -29,6 +29,11 @@ namespace PixiEditor.Models.Position
             return new Coordinates(coordiantes.X - size, coordiantes.Y - size);
         }
 
+        public static Coordinates operator -(Coordinates coordiantes1, Coordinates coordinates2)
+        {
+            return new Coordinates(coordiantes1.X - coordinates2.X, coordiantes1.Y - coordinates2.Y);
+        }
+
         public static bool operator ==(Coordinates c1, Coordinates c2)
         {
             return c2.X == c1.X && c2.Y == c1.Y;