1
1
Эх сурвалжийг харах

Fix zoom panning when image is rotated

Ruben 4 сар өмнө
parent
commit
380a6de58b

+ 29 - 5
src/PicView.Avalonia/ImageTransformations/Zoom.cs

@@ -232,7 +232,6 @@ public static class Zoom
         }
         
         vm.ZoomValue = 1;
-        vm.RotationAngle = 0;
         TooltipHelper.StopTooltipMessage();
         TitleManager.SetTitle(vm);
     }
@@ -265,11 +264,36 @@ public static class Zoom
         {
             return;
         }
-
+        
         var dragMousePosition = _start - e.GetPosition(imageViewer);
-    
-        var newXproperty = _origin.X - dragMousePosition.X;
-        var newYproperty = _origin.Y - dragMousePosition.Y;
+
+        // Get the current rotation angle from the ViewModel
+        var vm = imageViewer.DataContext as MainViewModel;
+        var rotationAngle = vm?.RotationAngle ?? 0;
+
+        // Apply rotation transformation to the mouse movement
+        var rotationRadians = rotationAngle * Math.PI / 180.0;
+        var cos = Math.Cos(rotationRadians);
+        var sin = Math.Sin(rotationRadians);
+
+        double rotatedX;
+        double rotatedY;
+
+        switch (rotationAngle)
+        {
+            case 90:
+            case 270:
+                rotatedX = -(dragMousePosition.X * cos - dragMousePosition.Y * sin);
+                rotatedY = -(dragMousePosition.X * sin + dragMousePosition.Y * cos);
+                break;
+            default:
+                rotatedX = dragMousePosition.X * cos - dragMousePosition.Y * sin;
+                rotatedY = dragMousePosition.X * sin + dragMousePosition.Y * cos;
+                break;
+        }
+
+        var newXproperty = _origin.X - rotatedX;
+        var newYproperty = _origin.Y - rotatedY;
         
         // #185
         if (Settings.WindowProperties.Fullscreen || Settings.WindowProperties.Maximized || !Settings.WindowProperties.AutoFit)

+ 6 - 13
src/PicView.Avalonia/Views/ImageViewer.axaml.cs

@@ -219,20 +219,13 @@ public partial class ImageViewer : UserControl
         {
             return;
         }
-        if (RotationHelper.IsValidRotation(vm.RotationAngle))
+        var nextAngle = RotationHelper.Rotate(vm.RotationAngle, clockWise);
+        vm.RotationAngle = nextAngle switch
         {
-            var nextAngle = RotationHelper.Rotate(vm.RotationAngle, clockWise);
-            vm.RotationAngle = nextAngle switch
-            {
-                360 => 0,
-                -90 => 270,
-                _ => nextAngle
-            };
-        }
-        else
-        {
-            vm.RotationAngle = RotationHelper.NextRotationAngle(vm.RotationAngle, true);
-        }
+            360 => 0,
+            -90 => 270,
+            _ => nextAngle
+        };
 
         var rotateTransform = new RotateTransform(vm.RotationAngle);