Fixed MouseWheel handling

This commit is contained in:
ClemensFischer 2022-11-12 01:14:07 +01:00
parent 367e1a6b3a
commit e2acf854f0
4 changed files with 40 additions and 21 deletions

View file

@ -52,7 +52,7 @@ namespace MapControl
new PropertyMetadata(TimeSpan.FromSeconds(0.2), (o, e) => ((MapTileLayerBase)o).updateTimer.Interval = (TimeSpan)e.NewValue));
public static readonly DependencyProperty UpdateWhileViewportChangingProperty = DependencyProperty.Register(
nameof(UpdateWhileViewportChanging), typeof(bool), typeof(MapTileLayerBase), new PropertyMetadata(true));
nameof(UpdateWhileViewportChanging), typeof(bool), typeof(MapTileLayerBase), new PropertyMetadata(false));
public static readonly DependencyProperty MapBackgroundProperty = DependencyProperty.Register(
nameof(MapBackground), typeof(Brush), typeof(MapTileLayerBase), new PropertyMetadata(null));

View file

@ -14,13 +14,14 @@ namespace MapControl
public class Map : MapBase
{
public static readonly DependencyProperty MouseWheelZoomDeltaProperty = DependencyProperty.Register(
nameof(MouseWheelZoomDelta), typeof(double), typeof(Map), new PropertyMetadata(1d));
nameof(MouseWheelZoomDelta), typeof(double), typeof(Map), new PropertyMetadata(0.25));
public static readonly DependencyProperty ManipulationModeProperty = DependencyProperty.Register(
nameof(ManipulationMode), typeof(ManipulationModes), typeof(Map),
new PropertyMetadata(ManipulationModes.Scale | ManipulationModes.Translate));
private Point? mousePosition;
private double mouseWheelDelta;
static Map()
{
@ -38,8 +39,8 @@ namespace MapControl
}
/// <summary>
/// Gets or sets the amount by which the ZoomLevel property changes during a MouseWheel event.
/// The default value is 1.
/// Gets or sets the amount by which the ZoomLevel property changes by a MouseWheel event.
/// The default value is 0.25.
/// </summary>
public double MouseWheelZoomDelta
{
@ -100,9 +101,17 @@ namespace MapControl
private void OnMouseWheel(object sender, MouseWheelEventArgs e)
{
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * Math.Sign(e.Delta);
mouseWheelDelta += e.Delta / 120d; // standard mouse wheel delta
ZoomMap(e.GetPosition(this), MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta));
if (Math.Abs(mouseWheelDelta) >= 1d)
{
// Zoom to integer multiple of MouseWheelZoomDelta.
ZoomMap(e.GetPosition(this),
MouseWheelZoomDelta * Math.Round(TargetZoomLevel / MouseWheelZoomDelta + mouseWheelDelta));
mouseWheelDelta = 0d;
}
}
}
}

View file

@ -21,9 +21,10 @@ namespace MapControl
public class Map : MapBase
{
public static readonly DependencyProperty MouseWheelZoomDeltaProperty = DependencyProperty.Register(
nameof(MouseWheelZoomDelta), typeof(double), typeof(Map), new PropertyMetadata(1d));
nameof(MouseWheelZoomDelta), typeof(double), typeof(Map), new PropertyMetadata(0.25));
private Point? mousePosition;
private double mouseWheelDelta;
public Map()
{
@ -32,16 +33,16 @@ namespace MapControl
| ManipulationModes.TranslateY
| ManipulationModes.TranslateInertia;
PointerWheelChanged += OnPointerWheelChanged;
ManipulationDelta += OnManipulationDelta;
PointerPressed += OnPointerPressed;
PointerReleased += OnPointerReleased;
PointerMoved += OnPointerMoved;
ManipulationDelta += OnManipulationDelta;
PointerWheelChanged += OnPointerWheelChanged;
}
/// <summary>
/// Gets or sets the amount by which the ZoomLevel property changes during a MouseWheel event.
/// The default value is 1.
/// Gets or sets the amount by which the ZoomLevel property changes by a PointerWheelChanged event.
/// The default value is 0.25.
/// </summary>
public double MouseWheelZoomDelta
{
@ -49,14 +50,11 @@ namespace MapControl
set => SetValue(MouseWheelZoomDeltaProperty, value);
}
private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
private void OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
if (!mousePosition.HasValue)
{
var point = e.GetCurrentPoint(this);
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * Math.Sign(point.Properties.MouseWheelDelta);
ZoomMap(point.Position, MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta));
TransformMap(e.Position, e.Delta.Translation, e.Delta.Rotation, e.Delta.Scale);
}
}
@ -95,11 +93,23 @@ namespace MapControl
}
}
private void OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
{
if (!mousePosition.HasValue)
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{
TransformMap(e.Position, e.Delta.Translation, e.Delta.Rotation, e.Delta.Scale);
var point = e.GetCurrentPoint(this);
mouseWheelDelta += point.Properties.MouseWheelDelta / 120d; // standard mouse wheel delta
if (Math.Abs(mouseWheelDelta) >= 1d)
{
// Zoom to integer multiple of MouseWheelZoomDelta.
ZoomMap(point.Position,
MouseWheelZoomDelta * Math.Round(TargetZoomLevel / MouseWheelZoomDelta + mouseWheelDelta));
mouseWheelDelta = 0d;
}
}
}
}

View file

@ -114,7 +114,7 @@
<Grid>
<map:Map x:Name="map"
MinZoomLevel="2" MaxZoomLevel="21" ZoomLevel="11" MouseWheelZoomDelta="1"
MinZoomLevel="2" MaxZoomLevel="21" ZoomLevel="11"
Center="53.5,8.2"
MouseLeftButtonDown="MapMouseLeftButtonDown"
MouseMove="MapMouseMove" MouseLeave="MapMouseLeave"