Browse Source

Improved zoom feeling

flabbet 5 years ago
parent
commit
31e932691c

+ 3 - 10
PixiEditor/Models/Tools/Tool.cs

@@ -29,17 +29,10 @@ namespace PixiEditor.Models.Tools
         private bool _isActive;
         public bool CanStartOutsideCanvas { get; set; } = false;
 
-        public virtual void OnMouseDown(MouseEventArgs e)
-        {
-            
-        }
+        public virtual void OnMouseDown(MouseEventArgs e) { }
 
-        public virtual void OnMouseUp(MouseEventArgs e)
-        {
-        }
+        public virtual void OnMouseUp(MouseEventArgs e) { }
 
-        public virtual void AfterAddedUndo()
-        {
-        }
+        public virtual void AfterAddedUndo() { }
     }
 }

+ 2 - 1
PixiEditor/Models/Tools/Tools/ZoomTool.cs

@@ -9,6 +9,7 @@ namespace PixiEditor.Models.Tools.Tools
 {
     public class ZoomTool : ReadonlyTool
     {
+        public const double DragZoomSpeed = 4.0;
         public override ToolType ToolType => ToolType.Zoom;
         private double _startingX;
 
@@ -50,7 +51,7 @@ namespace PixiEditor.Models.Tools.Tools
         {
             double xPos = MousePositionConverter.GetCursorPosition().X;
 
-            ViewModelMain.Current.ZoomPercentage = 100 + ((xPos - _startingX) / 5.0);
+            ViewModelMain.Current.ZoomPercentage = 100 + -((_startingX - xPos) / DragZoomSpeed);
         }
     }
 }

+ 2 - 2
PixiEditor/Views/MainDrawingPanel.xaml

@@ -9,8 +9,8 @@
              xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
              mc:Ignorable="d"
              d:DesignHeight="450" d:DesignWidth="800" x:Name="mainDrawingPanel">
-    <xctk:Zoombox PreviewMouseDown="Zoombox_MouseDown"  Cursor="{Binding Cursor}" IsAnimated="False" Name="Zoombox" KeepContentInBounds="True"
-                  Loaded="Zoombox_Loaded" DragModifiers="Shift" ZoomModifiers="None">
+    <xctk:Zoombox PreviewMouseDown="Zoombox_MouseDown" MouseUp="Zoombox_MouseUp"  Cursor="{Binding Cursor}" Name="Zoombox" KeepContentInBounds="True"
+                  Loaded="Zoombox_Loaded" IsAnimated="True" CurrentViewChanged="Zoombox_CurrentViewChanged" AnimationDuration="00:00:0.2" DragModifiers="Shift" ZoomModifiers="None">
         <i:Interaction.Triggers>
             <i:EventTrigger EventName="MouseMove">
                 <i:InvokeCommandAction Command="{Binding MouseMoveCommand, ElementName=mainDrawingPanel, Mode=OneWay}" />

+ 32 - 3
PixiEditor/Views/MainDrawingPanel.xaml.cs

@@ -1,5 +1,7 @@
-using System;
+using PixiEditor.ViewModels;
+using System;
 using System.Windows;
+using System.Windows.Automation;
 using System.Windows.Controls;
 using System.Windows.Input;
 using Xceed.Wpf.Toolkit.Core.Input;
@@ -37,7 +39,7 @@ namespace PixiEditor.Views
 
         // Using a DependencyProperty as the backing store for Item.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty ItemProperty =
-            DependencyProperty.Register("Item", typeof(object), typeof(MainDrawingPanel), new PropertyMetadata(0));
+            DependencyProperty.Register("Item", typeof(object), typeof(MainDrawingPanel), new PropertyMetadata(default(FrameworkElement)));
 
 
 
@@ -63,17 +65,38 @@ namespace PixiEditor.Views
         }
 
         public double ClickScale;
+        public Point ClickPoint;
 
         public MainDrawingPanel()
         {
             InitializeComponent();
             Zoombox.ZoomToSelectionModifiers = new KeyModifierCollection() { KeyModifier.RightAlt };
-            ClickScale = Zoombox.Scale;
+        }
+
+        private void Zoombox_CurrentViewChanged(object sender, ZoomboxViewChangedEventArgs e)
+        {
+            Zoombox.MinScale = 32 / ((FrameworkElement)Item).Width;
+            if(Zoombox.Scale > Zoombox.MinScale * 35)
+            {
+                Zoombox.KeepContentInBounds = false;
+            }
+            else
+            {
+                Zoombox.KeepContentInBounds = true;
+            }
         }
 
         private void Zoombox_MouseDown(object sender, MouseButtonEventArgs e)
+        {
+            SetClickValues();
+        }
+
+        private void SetClickValues()
         {
             ClickScale = Zoombox.Scale;
+            var item = (FrameworkElement)Item;
+            var mousePos = Mouse.GetPosition(item);
+            Zoombox.ZoomOrigin = new Point(mousePos.X / item.Width, mousePos.Y / item.Height);
         }
 
         public bool Center
@@ -125,6 +148,12 @@ namespace PixiEditor.Views
         private void Zoombox_Loaded(object sender, RoutedEventArgs e)
         {
             if (CenterOnStart) ((Zoombox) sender).CenterContent();
+            ClickScale = Zoombox.Scale;
+        }
+
+        private void Zoombox_MouseUp(object sender, MouseButtonEventArgs e)
+        {
+
         }
     }
 }