diff --git a/MapControl/Avalonia/Map.Avalonia.cs b/MapControl/Avalonia/Map.Avalonia.cs index 1b13c4e8..5bbe2075 100644 --- a/MapControl/Avalonia/Map.Avalonia.cs +++ b/MapControl/Avalonia/Map.Avalonia.cs @@ -12,68 +12,28 @@ namespace MapControl All = Translate | Rotate | Scale } - /// - /// MapBase with default input event handling. - /// - public class Map : MapBase + public partial class Map { - public static readonly StyledProperty ManipulationModesProperty = - DependencyPropertyHelper.Register(nameof(ManipulationModes), ManipulationModes.Translate | ManipulationModes.Scale); + public static readonly StyledProperty ManipulationModeProperty = + DependencyPropertyHelper.Register(nameof(ManipulationMode), ManipulationModes.Translate | ManipulationModes.Scale); - public static readonly StyledProperty MouseWheelZoomDeltaProperty = - DependencyPropertyHelper.Register(nameof(MouseWheelZoomDelta), 0.25); - - public static readonly DependencyProperty MouseWheelZoomAnimatedProperty = - DependencyPropertyHelper.Register(nameof(MouseWheelZoomAnimated), true); + /// + /// Gets or sets a value that specifies how the map control handles manipulations. + /// + public ManipulationModes ManipulationMode + { + get => GetValue(ManipulationModeProperty); + set => SetValue(ManipulationModeProperty, value); + } private IPointer pointer1; private IPointer pointer2; private Point position1; private Point position2; - public ManipulationModes ManipulationModes - { - get => GetValue(ManipulationModesProperty); - set => SetValue(ManipulationModesProperty, value); - } - - /// - /// Gets or sets the amount by which the ZoomLevel property changes by a PointerWheelChanged event. - /// The default value is 0.25. - /// - public double MouseWheelZoomDelta - { - get => GetValue(MouseWheelZoomDeltaProperty); - set => SetValue(MouseWheelZoomDeltaProperty, value); - } - - /// - /// Gets or sets a value that controls whether zooming by a PointerWheelChanged event is animated. - /// The default value is true. - /// - public bool MouseWheelZoomAnimated - { - get => (bool)GetValue(MouseWheelZoomAnimatedProperty); - set => SetValue(MouseWheelZoomAnimatedProperty, value); - } - protected override void OnPointerWheelChanged(PointerWheelEventArgs e) { - var delta = e.Delta.Y; - var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * delta; - var animated = false; - - if (delta % 1d == 0d) - { - // Zoom to integer multiple of MouseWheelZoomDelta when delta is an integer value, - // i.e. when the event was actually raised by a mouse wheel and not by a touch pad - // or a similar device with higher resolution. - // - zoomLevel = MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta); - animated = MouseWheelZoomAnimated; - } - - ZoomMap(e.GetPosition(this), zoomLevel, animated); + OnMouseWheel(e.GetPosition(this), e.Delta.Y); base.OnPointerWheelChanged(e); } @@ -89,7 +49,7 @@ namespace MapControl HandleManipulation(point.Pointer, point.Position); } else if (point.Pointer.Type == PointerType.Mouse || - ManipulationModes.HasFlag(ManipulationModes.Translate)) + ManipulationMode.HasFlag(ManipulationModes.Translate)) { TranslateMap(new Point(point.Position.X - position1.X, point.Position.Y - position1.Y)); position1 = point.Position; @@ -101,7 +61,7 @@ namespace MapControl e.KeyModifiers == KeyModifiers.None || pointer2 == null && point.Pointer.Type == PointerType.Touch && - ManipulationModes != ManipulationModes.None) + ManipulationMode != ManipulationModes.None) { point.Pointer.Capture(this); @@ -156,19 +116,19 @@ namespace MapControl var rotation = 0d; var scale = 1d; - if (ManipulationModes.HasFlag(ManipulationModes.Translate)) + if (ManipulationMode.HasFlag(ManipulationModes.Translate)) { newOrigin = new Point((position1.X + position2.X) / 2d, (position1.Y + position2.Y) / 2d); translation = newOrigin - oldOrigin; } - if (ManipulationModes.HasFlag(ManipulationModes.Rotate)) + if (ManipulationMode.HasFlag(ManipulationModes.Rotate)) { rotation = 180d / Math.PI * (Math.Atan2(newDistance.Y, newDistance.X) - Math.Atan2(oldDistance.Y, oldDistance.X)); } - if (ManipulationModes.HasFlag(ManipulationModes.Scale)) + if (ManipulationMode.HasFlag(ManipulationModes.Scale)) { scale = newDistance.Length / oldDistance.Length; } diff --git a/MapControl/Shared/Map.cs b/MapControl/Shared/Map.cs new file mode 100644 index 00000000..3ecf8dfb --- /dev/null +++ b/MapControl/Shared/Map.cs @@ -0,0 +1,68 @@ +using System; +#if WPF +using System.Windows; +#elif UWP +using Windows.UI.Xaml; +#elif WINUI +using Microsoft.UI.Xaml; +#endif + +namespace MapControl +{ + /// + /// MapBase with default input event handling. + /// + public partial class Map : MapBase + { + public static readonly DependencyProperty MouseWheelZoomDeltaProperty = + DependencyPropertyHelper.Register(nameof(MouseWheelZoomDelta), 0.25); + + public static readonly DependencyProperty MouseWheelZoomAnimatedProperty = + DependencyPropertyHelper.Register(nameof(MouseWheelZoomAnimated), true); + + /// + /// Gets or sets the amount by which the ZoomLevel property changes by a MouseWheel event. + /// The default value is 0.25. + /// + public double MouseWheelZoomDelta + { + get => (double)GetValue(MouseWheelZoomDeltaProperty); + set => SetValue(MouseWheelZoomDeltaProperty, value); + } + + /// + /// Gets or sets a value that specifies whether zooming by a MouseWheel event is animated. + /// The default value is true. + /// + public bool MouseWheelZoomAnimated + { + get => (bool)GetValue(MouseWheelZoomAnimatedProperty); + set => SetValue(MouseWheelZoomAnimatedProperty, value); + } + + private void OnMouseWheel(Point position, int delta) + { + // Standard mouse wheel delta value is 120. + // + OnMouseWheel(position, delta / 120d); + } + + private void OnMouseWheel(Point position, double delta) + { + var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * delta; + var animated = false; + + if (delta % 1d == 0d) + { + // Zoom to integer multiple of MouseWheelZoomDelta when delta is an integer value, + // i.e. when the event was actually raised by a mouse wheel and not by a touch pad + // or a similar device with higher resolution. + // + zoomLevel = MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta); + animated = MouseWheelZoomAnimated; + } + + ZoomMap(position, zoomLevel, animated); + } + } +} diff --git a/MapControl/UWP/MapControl.UWP.csproj b/MapControl/UWP/MapControl.UWP.csproj index 3b27814d..094a008f 100644 --- a/MapControl/UWP/MapControl.UWP.csproj +++ b/MapControl/UWP/MapControl.UWP.csproj @@ -101,6 +101,9 @@ LocationCollection.cs + + Map.cs + MapBase.cs diff --git a/MapControl/WPF/Map.WPF.cs b/MapControl/WPF/Map.WPF.cs index 36ebfdb2..2542349f 100644 --- a/MapControl/WPF/Map.WPF.cs +++ b/MapControl/WPF/Map.WPF.cs @@ -1,30 +1,18 @@ -using System; -using System.Windows; +using System.Windows; using System.Windows.Input; namespace MapControl { - /// - /// MapBase with default input event handling. - /// - public class Map : MapBase + public partial class Map { - public static readonly DependencyProperty ManipulationModeProperty = - DependencyPropertyHelper.Register(nameof(ManipulationMode), ManipulationModes.Translate | ManipulationModes.Scale); - - public static readonly DependencyProperty MouseWheelZoomDeltaProperty = - DependencyPropertyHelper.Register(nameof(MouseWheelZoomDelta), 0.25); - - public static readonly DependencyProperty MouseWheelZoomAnimatedProperty = - DependencyPropertyHelper.Register(nameof(MouseWheelZoomAnimated), true); - - private Point? mousePosition; - static Map() { IsManipulationEnabledProperty.OverrideMetadata(typeof(Map), new FrameworkPropertyMetadata(true)); } + public static readonly DependencyProperty ManipulationModeProperty = + DependencyPropertyHelper.Register(nameof(ManipulationMode), ManipulationModes.Translate | ManipulationModes.Scale); + /// /// Gets or sets a value that specifies how the map control handles manipulations. /// @@ -34,42 +22,11 @@ namespace MapControl set => SetValue(ManipulationModeProperty, value); } - /// - /// Gets or sets the amount by which the ZoomLevel property changes by a MouseWheel event. - /// The default value is 0.25. - /// - public double MouseWheelZoomDelta - { - get => (double)GetValue(MouseWheelZoomDeltaProperty); - set => SetValue(MouseWheelZoomDeltaProperty, value); - } - - /// - /// Gets or sets a value that controls whether zooming by a MouseWheel event is animated. - /// The default value is true. - /// - public bool MouseWheelZoomAnimated - { - get => (bool)GetValue(MouseWheelZoomAnimatedProperty); - set => SetValue(MouseWheelZoomAnimatedProperty, value); - } + private Point? mousePosition; protected override void OnMouseWheel(MouseWheelEventArgs e) { - var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * e.Delta / 120d; - var animated = false; - - if (e.Delta % 120 == 0) - { - // Zoom to integer multiple of MouseWheelZoomDelta when delta is a multiple of 120, - // i.e. when the event was actually raised by a mouse wheel and not by a touch pad - // or a similar device with higher resolution. - // - zoomLevel = MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta); - animated = MouseWheelZoomAnimated; - } - - ZoomMap(e.GetPosition(this), zoomLevel, animated); + OnMouseWheel(e.GetPosition(this), e.Delta); base.OnMouseWheel(e); } diff --git a/MapControl/WinUI/Map.WinUI.cs b/MapControl/WinUI/Map.WinUI.cs index 4a3e227f..636594cd 100644 --- a/MapControl/WinUI/Map.WinUI.cs +++ b/MapControl/WinUI/Map.WinUI.cs @@ -1,30 +1,16 @@ -using System; -using Windows.System; +using Windows.System; #if UWP using Windows.Devices.Input; -using Windows.UI.Xaml; using Windows.UI.Xaml.Input; #else using Microsoft.UI.Input; -using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Input; #endif namespace MapControl { - /// - /// MapBase with default input event handling. - /// - public class Map : MapBase + public partial class Map { - public static readonly DependencyProperty MouseWheelZoomDeltaProperty = - DependencyPropertyHelper.Register(nameof(MouseWheelZoomDelta), 0.25); - - public static readonly DependencyProperty MouseWheelZoomAnimatedProperty = - DependencyPropertyHelper.Register(nameof(MouseWheelZoomAnimated), true); - - private bool? manipulationEnabled; - public Map() { ManipulationMode @@ -40,46 +26,15 @@ namespace MapControl ManipulationCompleted += OnManipulationCompleted; } - /// - /// Gets or sets the amount by which the ZoomLevel property changes by a PointerWheelChanged event. - /// The default value is 0.25. - /// - public double MouseWheelZoomDelta - { - get => (double)GetValue(MouseWheelZoomDeltaProperty); - set => SetValue(MouseWheelZoomDeltaProperty, value); - } - - /// - /// Gets or sets a value that controls whether zooming by a PointerWheelChanged event is animated. - /// The default value is true. - /// - public bool MouseWheelZoomAnimated - { - get => (bool)GetValue(MouseWheelZoomAnimatedProperty); - set => SetValue(MouseWheelZoomAnimatedProperty, value); - } + private bool? manipulationEnabled; private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e) { if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse) { var point = e.GetCurrentPoint(this); - var delta = point.Properties.MouseWheelDelta; - var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * delta / 120d; - var animated = false; - if (delta % 120 == 0) - { - // Zoom to integer multiple of MouseWheelZoomDelta when delta is a multiple of 120, - // i.e. when the event was actually raised by a mouse wheel and not by a touch pad - // or a similar device with higher resolution. - // - zoomLevel = MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta); - animated = MouseWheelZoomAnimated; - } - - ZoomMap(point.Position, zoomLevel, animated); + OnMouseWheel(point.Position, point.Properties.MouseWheelDelta); } } diff --git a/SampleApps/AvaloniaApp/MainWindow.axaml b/SampleApps/AvaloniaApp/MainWindow.axaml index 31e133cb..5248c9b4 100644 --- a/SampleApps/AvaloniaApp/MainWindow.axaml +++ b/SampleApps/AvaloniaApp/MainWindow.axaml @@ -13,7 +13,7 @@ - +