Browse Source

Improved feeling of zoom

flabbet 5 years ago
parent
commit
93b419b130

+ 1 - 0
PixiEditor/Models/Controllers/BitmapManager.cs

@@ -132,6 +132,7 @@ namespace PixiEditor.Models.Controllers
 
 
         private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)
         private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)
         {
         {
+            SelectedTool.OnMouseMove(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
             if (Mouse.LeftButton == MouseButtonState.Pressed && !IsDraggingViewport() && ActiveDocument != null)
             if (Mouse.LeftButton == MouseButtonState.Pressed && !IsDraggingViewport() && ActiveDocument != null)
             {
             {
                 ExecuteTool(e.NewPosition, MouseController.ClickedOnCanvas);   
                 ExecuteTool(e.NewPosition, MouseController.ClickedOnCanvas);   

+ 2 - 0
PixiEditor/Models/Tools/Tool.cs

@@ -33,6 +33,8 @@ namespace PixiEditor.Models.Tools
 
 
         public virtual void OnMouseUp(MouseEventArgs e) { }
         public virtual void OnMouseUp(MouseEventArgs e) { }
 
 
+        public virtual void OnMouseMove(MouseEventArgs e) { }
+
         public virtual void AfterAddedUndo() { }
         public virtual void AfterAddedUndo() { }
     }
     }
 }
 }

+ 21 - 6
PixiEditor/Models/Tools/Tools/ZoomTool.cs

@@ -1,23 +1,28 @@
-using PixiEditor.Models.Position;
+using PixiEditor.Models.Controllers;
+using PixiEditor.Models.Position;
 using PixiEditor.ViewModels;
 using PixiEditor.ViewModels;
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Text;
 using System.Text;
+using System.Windows;
 using System.Windows.Input;
 using System.Windows.Input;
 
 
 namespace PixiEditor.Models.Tools.Tools
 namespace PixiEditor.Models.Tools.Tools
 {
 {
     public class ZoomTool : ReadonlyTool
     public class ZoomTool : ReadonlyTool
     {
     {
-        public const double DragZoomSpeed = 4.0;
+        public const float MaxZoomMultiplier = 5f;
         public override ToolType ToolType => ToolType.Zoom;
         public override ToolType ToolType => ToolType.Zoom;
         private double _startingX;
         private double _startingX;
+        private double _workAreaWidth = SystemParameters.WorkArea.Width;
+        private double _pixelsPerZoomMultiplier;
 
 
         public ZoomTool()
         public ZoomTool()
         {
         {
             HideHighlight = true;
             HideHighlight = true;
             CanStartOutsideCanvas = true;
             CanStartOutsideCanvas = true;
-            Tooltip = "Zooms viewport (Z). Click to zoom in, hold alt and click to zoom out.";            
+            Tooltip = "Zooms viewport (Z). Click to zoom in, hold alt and click to zoom out.";
+            _pixelsPerZoomMultiplier = _workAreaWidth / MaxZoomMultiplier; //Eg. 1200 px screen width / 5x zoom max = 240 px per 1x diff.
         }
         }
 
 
         public override void OnMouseDown(MouseEventArgs e)
         public override void OnMouseDown(MouseEventArgs e)
@@ -26,6 +31,19 @@ namespace PixiEditor.Models.Tools.Tools
             ViewModelMain.Current.ZoomPercentage = 100; //This resest the value, so callback in MainDrawingPanel can fire again later
             ViewModelMain.Current.ZoomPercentage = 100; //This resest the value, so callback in MainDrawingPanel can fire again later
         }
         }
 
 
+        public override void OnMouseMove(MouseEventArgs e)
+        {
+            if (e.LeftButton == MouseButtonState.Pressed)
+            {
+                double xPos = MousePositionConverter.GetCursorPosition().X;
+
+                double rawPrecentDifference = (xPos - _startingX) / _pixelsPerZoomMultiplier; //raw = 0-1 range
+                double normalizedPrecentDifference = rawPrecentDifference * 100.0; // To 0-100 range
+                double sumZoomPrecent = normalizedPrecentDifference + 100; // We are adding 100, so we can get the final zoom precent relative to original
+                Zoom(sumZoomPrecent);
+            }
+        }
+
         public override void OnMouseUp(MouseEventArgs e)
         public override void OnMouseUp(MouseEventArgs e)
         {
         {
             if (e.LeftButton == MouseButtonState.Released && e.RightButton == MouseButtonState.Released && 
             if (e.LeftButton == MouseButtonState.Released && e.RightButton == MouseButtonState.Released && 
@@ -49,9 +67,6 @@ namespace PixiEditor.Models.Tools.Tools
 
 
         public override void Use(Coordinates[] pixels)
         public override void Use(Coordinates[] pixels)
         {
         {
-            double xPos = MousePositionConverter.GetCursorPosition().X;
-
-            ViewModelMain.Current.ZoomPercentage = 100 + -((_startingX - xPos) / DragZoomSpeed);
         }
         }
     }
     }
 }
 }

+ 2 - 1
PixiEditor/ViewModels/ViewModelMain.cs

@@ -675,8 +675,9 @@ namespace PixiEditor.ViewModels
 
 
 
 
             if (BitmapManager.MouseController.IsRecordingChanges && Mouse.LeftButton == MouseButtonState.Pressed)
             if (BitmapManager.MouseController.IsRecordingChanges && Mouse.LeftButton == MouseButtonState.Pressed)
+            {
                 BitmapManager.MouseController.RecordMouseMovementChange(cords);
                 BitmapManager.MouseController.RecordMouseMovementChange(cords);
-            else
+            }
                 BitmapManager.MouseController.MouseMoved(cords);
                 BitmapManager.MouseController.MouseMoved(cords);
         }
         }
 
 

+ 3 - 3
PixiEditor/Views/MainDrawingPanel.xaml

@@ -7,10 +7,10 @@
              xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
              xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
              xmlns:helpers="clr-namespace:PixiEditor.Helpers"
              xmlns:helpers="clr-namespace:PixiEditor.Helpers"
              xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
              xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
-             mc:Ignorable="d"
+             mc:Ignorable="d" PreviewMouseDown="mainDrawingPanel_MouseDown" PreviewMouseUp="mainDrawingPanel_MouseUp"
              d:DesignHeight="450" d:DesignWidth="800" x:Name="mainDrawingPanel" PreviewMouseWheel="Zoombox_MouseWheel">
              d:DesignHeight="450" d:DesignWidth="800" x:Name="mainDrawingPanel" PreviewMouseWheel="Zoombox_MouseWheel">
-    <xctk:Zoombox PreviewMouseDown="Zoombox_MouseDown"  Cursor="{Binding Cursor}" Name="Zoombox" KeepContentInBounds="True"
-                  Loaded="Zoombox_Loaded" IsAnimated="True" CurrentViewChanged="Zoombox_CurrentViewChanged" AnimationDuration="00:00:0.2" DragModifiers="Shift" ZoomModifiers="None">
+    <xctk:Zoombox PreviewMouseDown="Zoombox_PreviewMouseDown" Cursor="{Binding Cursor}" Name="Zoombox" KeepContentInBounds="True"
+                  Loaded="Zoombox_Loaded" IsAnimated="False" CurrentViewChanged="Zoombox_CurrentViewChanged" DragModifiers="Shift" ZoomModifiers="None">
         <i:Interaction.Triggers>
         <i:Interaction.Triggers>
             <i:EventTrigger EventName="MouseMove">
             <i:EventTrigger EventName="MouseMove">
                 <i:InvokeCommandAction Command="{Binding MouseMoveCommand, ElementName=mainDrawingPanel, Mode=OneWay}" />
                 <i:InvokeCommandAction Command="{Binding MouseMoveCommand, ElementName=mainDrawingPanel, Mode=OneWay}" />

+ 28 - 13
PixiEditor/Views/MainDrawingPanel.xaml.cs

@@ -59,13 +59,12 @@ namespace PixiEditor.Views
             double percentage = (double)e.NewValue;
             double percentage = (double)e.NewValue;
             if(percentage == 100)
             if(percentage == 100)
             {
             {
-                panel.ClickScale = panel.Zoombox.Scale;
+                panel.SetClickValues();
             }
             }
             panel.Zoombox.ZoomTo(panel.ClickScale * ((double)e.NewValue / 100.0));
             panel.Zoombox.ZoomTo(panel.ClickScale * ((double)e.NewValue / 100.0));
         }
         }
 
 
         public double ClickScale;
         public double ClickScale;
-        public Point ClickPoint;
 
 
         public MainDrawingPanel()
         public MainDrawingPanel()
         {
         {
@@ -76,27 +75,24 @@ namespace PixiEditor.Views
         private void Zoombox_CurrentViewChanged(object sender, ZoomboxViewChangedEventArgs e)
         private void Zoombox_CurrentViewChanged(object sender, ZoomboxViewChangedEventArgs e)
         {
         {
             Zoombox.MinScale = 32 / ((FrameworkElement)Item).Width;
             Zoombox.MinScale = 32 / ((FrameworkElement)Item).Width;
-            if(Zoombox.Scale > Zoombox.MinScale * 35)
-            {
-                Zoombox.KeepContentInBounds = false;
-            }
-            else
-            {
-                Zoombox.KeepContentInBounds = true;
-            }
+            Zoombox.KeepContentInBounds = !(Zoombox.Scale > Zoombox.MinScale * 35.0);
         }
         }
 
 
-        private void Zoombox_MouseDown(object sender, MouseButtonEventArgs e)
+        private void Zoombox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
         {
         {
-            SetClickValues();
+            if (ZoomPercentage == 100)
+            {
+                SetClickValues();
+            }
         }
         }
 
 
         private void SetClickValues()
         private void SetClickValues()
         {
         {
             ClickScale = Zoombox.Scale;
             ClickScale = Zoombox.Scale;
             var item = (FrameworkElement)Item;
             var item = (FrameworkElement)Item;
+            if (item == null) return;
             var mousePos = Mouse.GetPosition(item);
             var mousePos = Mouse.GetPosition(item);
-            Zoombox.ZoomOrigin = new Point(mousePos.X / item.Width, mousePos.Y / item.Height);
+            Zoombox.ZoomOrigin = new Point(Math.Clamp(mousePos.X / item.Width, 0, 1), Math.Clamp(mousePos.Y / item.Height,0,1));
         }
         }
 
 
         public bool Center
         public bool Center
@@ -152,6 +148,14 @@ namespace PixiEditor.Views
         }
         }
 
 
         private void Zoombox_MouseWheel(object sender, MouseWheelEventArgs e)
         private void Zoombox_MouseWheel(object sender, MouseWheelEventArgs e)
+        {
+            if (ZoomPercentage == 100)
+            {
+                SetClickValues();
+            }
+        }
+
+        public void ResetView()
         {
         {
             Point point = new Point(0.5, 0.5);
             Point point = new Point(0.5, 0.5);
             if (Zoombox.ZoomOrigin != point)
             if (Zoombox.ZoomOrigin != point)
@@ -160,5 +164,16 @@ namespace PixiEditor.Views
                 Zoombox.ZoomOrigin = point;
                 Zoombox.ZoomOrigin = point;
             }
             }
         }
         }
+
+        private void mainDrawingPanel_MouseDown(object sender, MouseButtonEventArgs e)
+        {
+            ((MainDrawingPanel)sender).CaptureMouse();
+            ResetView();
+        }
+
+        private void mainDrawingPanel_MouseUp(object sender, MouseButtonEventArgs e)
+        {
+            ReleaseMouseCapture();
+        }
     }
     }
 }
 }