Updated mouse wheel event handling

This commit is contained in:
ClemensFischer 2025-06-12 00:31:01 +02:00
parent 140f800f33
commit cceb122486
4 changed files with 122 additions and 74 deletions

View file

@ -23,11 +23,13 @@ namespace MapControl
public static readonly StyledProperty<double> MouseWheelZoomDeltaProperty = public static readonly StyledProperty<double> MouseWheelZoomDeltaProperty =
DependencyPropertyHelper.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25); DependencyPropertyHelper.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25);
public static readonly DependencyProperty MouseWheelZoomAnimatedProperty =
DependencyPropertyHelper.Register<Map, bool>(nameof(MouseWheelZoomAnimated), true);
private IPointer pointer1; private IPointer pointer1;
private IPointer pointer2; private IPointer pointer2;
private Point position1; private Point position1;
private Point position2; private Point position2;
private double mouseWheelDelta;
public ManipulationModes ManipulationModes public ManipulationModes ManipulationModes
{ {
@ -36,7 +38,7 @@ namespace MapControl
} }
/// <summary> /// <summary>
/// Gets or sets the amount by which the ZoomLevel property changes by a MouseWheel event. /// Gets or sets the amount by which the ZoomLevel property changes by a PointerWheelChanged event.
/// The default value is 0.25. /// The default value is 0.25.
/// </summary> /// </summary>
public double MouseWheelZoomDelta public double MouseWheelZoomDelta
@ -45,20 +47,34 @@ namespace MapControl
set => SetValue(MouseWheelZoomDeltaProperty, value); set => SetValue(MouseWheelZoomDeltaProperty, value);
} }
/// <summary>
/// Gets or sets a value that controls whether zooming by a PointerWheelChanged event is animated.
/// The default value is true.
/// </summary>
public bool MouseWheelZoomAnimated
{
get => (bool)GetValue(MouseWheelZoomAnimatedProperty);
set => SetValue(MouseWheelZoomAnimatedProperty, value);
}
protected override void OnPointerWheelChanged(PointerWheelEventArgs e) protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
{ {
mouseWheelDelta += e.Delta.Y; var delta = e.Delta.Y;
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * delta;
var animated = false;
if (Math.Abs(mouseWheelDelta) >= 1d) if (delta % 1d == 0d)
{ {
// Zoom to integer multiple of MouseWheelZoomDelta. // 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.
// //
ZoomMap(e.GetPosition(this), zoomLevel = MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta);
MouseWheelZoomDelta * Math.Round(TargetZoomLevel / MouseWheelZoomDelta + mouseWheelDelta)); animated = MouseWheelZoomAnimated;
mouseWheelDelta = 0d;
} }
ZoomMap(e.GetPosition(this), zoomLevel, animated);
base.OnPointerWheelChanged(e); base.OnPointerWheelChanged(e);
} }

View file

@ -75,7 +75,7 @@ namespace MapControl
} }
/// <summary> /// <summary>
/// Gets or sets the Duration of the Center, ZoomLevel and Heading animations. /// Gets or sets the duration of the Center, ZoomLevel and Heading animations.
/// The default value is 0.3 seconds. /// The default value is 0.3 seconds.
/// </summary> /// </summary>
public TimeSpan AnimationDuration public TimeSpan AnimationDuration
@ -303,21 +303,18 @@ namespace MapControl
else else
{ {
SetTransformCenter(center); SetTransformCenter(center);
viewCenter = new Point(viewCenter.X + translation.X, viewCenter.Y + translation.Y); viewCenter = new Point(viewCenter.X + translation.X, viewCenter.Y + translation.Y);
if (rotation != 0d) if (rotation != 0d)
{ {
var heading = (((Heading - rotation) % 360d) + 360d) % 360d; var heading = CoerceHeadingProperty(Heading - rotation);
SetValueInternal(HeadingProperty, heading); SetValueInternal(HeadingProperty, heading);
SetValueInternal(TargetHeadingProperty, heading); SetValueInternal(TargetHeadingProperty, heading);
} }
if (scale != 1d) if (scale != 1d)
{ {
var zoomLevel = Math.Min(Math.Max(ZoomLevel + Math.Log(scale, 2d), MinZoomLevel), MaxZoomLevel); var zoomLevel = CoerceZoomLevelProperty(ZoomLevel + Math.Log(scale, 2d));
SetValueInternal(ZoomLevelProperty, zoomLevel); SetValueInternal(ZoomLevelProperty, zoomLevel);
SetValueInternal(TargetZoomLevelProperty, zoomLevel); SetValueInternal(TargetZoomLevelProperty, zoomLevel);
} }
@ -327,18 +324,30 @@ namespace MapControl
} }
/// <summary> /// <summary>
/// Sets the value of the TargetZoomLevel property while retaining the specified center point /// Sets the ZoomLevel or TargetZoomLevel property while retaining
/// in view coordinates. /// the specified center point in view coordinates.
/// </summary> /// </summary>
public void ZoomMap(Point center, double zoomLevel) public void ZoomMap(Point center, double zoomLevel, bool animated = true)
{ {
zoomLevel = CoerceZoomLevelProperty(zoomLevel); zoomLevel = CoerceZoomLevelProperty(zoomLevel);
if (TargetZoomLevel != zoomLevel) if (animated)
{ {
SetTransformCenter(center); if (TargetZoomLevel != zoomLevel)
{
TargetZoomLevel = zoomLevel; SetTransformCenter(center);
TargetZoomLevel = zoomLevel;
}
}
else
{
if (ZoomLevel != zoomLevel)
{
SetTransformCenter(center);
SetValueInternal(ZoomLevelProperty, zoomLevel);
SetValueInternal(TargetZoomLevelProperty, zoomLevel);
UpdateTransform(true);
}
} }
} }
@ -358,7 +367,6 @@ namespace MapControl
if (targetCenter != null) if (targetCenter != null)
{ {
var scale = Math.Min(ActualWidth / mapRect.Value.Width, ActualHeight / mapRect.Value.Height); var scale = Math.Min(ActualWidth / mapRect.Value.Width, ActualHeight / mapRect.Value.Height);
TargetZoomLevel = ScaleToZoomLevel(scale); TargetZoomLevel = ScaleToZoomLevel(scale);
TargetCenter = targetCenter; TargetCenter = targetCenter;
TargetHeading = 0d; TargetHeading = 0d;
@ -428,9 +436,7 @@ namespace MapControl
private void SetValueInternal(DependencyProperty property, object value) private void SetValueInternal(DependencyProperty property, object value)
{ {
internalPropertyChange = true; internalPropertyChange = true;
SetValue(property, value); SetValue(property, value);
internalPropertyChange = false; internalPropertyChange = false;
} }

View file

@ -9,20 +9,31 @@ namespace MapControl
/// </summary> /// </summary>
public class Map : MapBase public class Map : MapBase
{ {
public static readonly DependencyProperty MouseWheelZoomDeltaProperty =
DependencyPropertyHelper.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25);
public static readonly DependencyProperty ManipulationModeProperty = public static readonly DependencyProperty ManipulationModeProperty =
DependencyPropertyHelper.Register<Map, ManipulationModes>(nameof(ManipulationMode), ManipulationModes.Translate | ManipulationModes.Scale); DependencyPropertyHelper.Register<Map, ManipulationModes>(nameof(ManipulationMode), ManipulationModes.Translate | ManipulationModes.Scale);
public static readonly DependencyProperty MouseWheelZoomDeltaProperty =
DependencyPropertyHelper.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25);
public static readonly DependencyProperty MouseWheelZoomAnimatedProperty =
DependencyPropertyHelper.Register<Map, bool>(nameof(MouseWheelZoomAnimated), true);
private Point? mousePosition; private Point? mousePosition;
private double mouseWheelDelta;
static Map() static Map()
{ {
IsManipulationEnabledProperty.OverrideMetadata(typeof(Map), new FrameworkPropertyMetadata(true)); IsManipulationEnabledProperty.OverrideMetadata(typeof(Map), new FrameworkPropertyMetadata(true));
} }
/// <summary>
/// Gets or sets a value that specifies how the map control handles manipulations.
/// </summary>
public ManipulationModes ManipulationMode
{
get => (ManipulationModes)GetValue(ManipulationModeProperty);
set => SetValue(ManipulationModeProperty, value);
}
/// <summary> /// <summary>
/// Gets or sets the amount by which the ZoomLevel property changes by a MouseWheel event. /// Gets or sets the amount by which the ZoomLevel property changes by a MouseWheel event.
/// The default value is 0.25. /// The default value is 0.25.
@ -34,57 +45,59 @@ namespace MapControl
} }
/// <summary> /// <summary>
/// Gets or sets a value that specifies how the map control handles manipulations. /// Gets or sets a value that controls whether zooming by a MouseWheel event is animated.
/// The default value is true.
/// </summary> /// </summary>
public ManipulationModes ManipulationMode public bool MouseWheelZoomAnimated
{ {
get => (ManipulationModes)GetValue(ManipulationModeProperty); get => (bool)GetValue(MouseWheelZoomAnimatedProperty);
set => SetValue(ManipulationModeProperty, value); set => SetValue(MouseWheelZoomAnimatedProperty, value);
} }
protected override void OnManipulationStarted(ManipulationStartedEventArgs e) protected override void OnMouseWheel(MouseWheelEventArgs e)
{ {
base.OnManipulationStarted(e); var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * e.Delta / 120d;
var animated = false;
Manipulation.SetManipulationMode(this, ManipulationMode); 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;
}
protected override void OnManipulationDelta(ManipulationDeltaEventArgs e) ZoomMap(e.GetPosition(this), zoomLevel, animated);
{
base.OnManipulationDelta(e);
TransformMap(e.ManipulationOrigin, base.OnMouseWheel(e);
(Point)e.DeltaManipulation.Translation,
e.DeltaManipulation.Rotation,
e.DeltaManipulation.Scale.LengthSquared / 2d);
} }
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{ {
base.OnMouseLeftButtonDown(e);
if (Keyboard.Modifiers == ModifierKeys.None && if (Keyboard.Modifiers == ModifierKeys.None &&
CaptureMouse()) CaptureMouse())
{ {
mousePosition = e.GetPosition(this); mousePosition = e.GetPosition(this);
} }
base.OnMouseLeftButtonDown(e);
} }
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{ {
base.OnMouseLeftButtonUp(e);
if (mousePosition.HasValue) if (mousePosition.HasValue)
{ {
mousePosition = null; mousePosition = null;
ReleaseMouseCapture(); ReleaseMouseCapture();
} }
base.OnMouseLeftButtonUp(e);
} }
protected override void OnMouseMove(MouseEventArgs e) protected override void OnMouseMove(MouseEventArgs e)
{ {
base.OnMouseMove(e);
if (mousePosition.HasValue) if (mousePosition.HasValue)
{ {
var p = e.GetPosition(this); var p = e.GetPosition(this);
@ -99,25 +112,25 @@ namespace MapControl
// //
mousePosition = e.GetPosition(this); mousePosition = e.GetPosition(this);
} }
base.OnMouseMove(e);
} }
protected override void OnMouseWheel(MouseWheelEventArgs e) protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
{ {
base.OnMouseWheel(e); Manipulation.SetManipulationMode(this, ManipulationMode);
// Standard mouse wheel delta value is 120. base.OnManipulationStarted(e);
// }
mouseWheelDelta += e.Delta / 120d;
if (Math.Abs(mouseWheelDelta) >= 1d) protected override void OnManipulationDelta(ManipulationDeltaEventArgs e)
{ {
// Zoom to integer multiple of MouseWheelZoomDelta. TransformMap(e.ManipulationOrigin,
// (Point)e.DeltaManipulation.Translation,
ZoomMap(e.GetPosition(this), e.DeltaManipulation.Rotation,
MouseWheelZoomDelta * Math.Round(TargetZoomLevel / MouseWheelZoomDelta + mouseWheelDelta)); e.DeltaManipulation.Scale.LengthSquared / 2d);
mouseWheelDelta = 0d; base.OnManipulationDelta(e);
}
} }
} }
} }

View file

@ -20,7 +20,9 @@ namespace MapControl
public static readonly DependencyProperty MouseWheelZoomDeltaProperty = public static readonly DependencyProperty MouseWheelZoomDeltaProperty =
DependencyPropertyHelper.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25); DependencyPropertyHelper.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25);
private double mouseWheelDelta; public static readonly DependencyProperty MouseWheelZoomAnimatedProperty =
DependencyPropertyHelper.Register<Map, bool>(nameof(MouseWheelZoomAnimated), true);
private bool? manipulationEnabled; private bool? manipulationEnabled;
public Map() public Map()
@ -48,25 +50,36 @@ namespace MapControl
set => SetValue(MouseWheelZoomDeltaProperty, value); set => SetValue(MouseWheelZoomDeltaProperty, value);
} }
/// <summary>
/// Gets or sets a value that controls whether zooming by a PointerWheelChanged event is animated.
/// The default value is true.
/// </summary>
public bool MouseWheelZoomAnimated
{
get => (bool)GetValue(MouseWheelZoomAnimatedProperty);
set => SetValue(MouseWheelZoomAnimatedProperty, value);
}
private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e) private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
{ {
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse) if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{ {
var point = e.GetCurrentPoint(this); var point = e.GetCurrentPoint(this);
var delta = point.Properties.MouseWheelDelta;
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * delta / 120d;
var animated = false;
// Standard mouse wheel delta value is 120. if (delta % 120 == 0)
//
mouseWheelDelta += point.Properties.MouseWheelDelta / 120d;
if (Math.Abs(mouseWheelDelta) >= 1d)
{ {
// Zoom to integer multiple of MouseWheelZoomDelta. // 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.
// //
ZoomMap(point.Position, zoomLevel = MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta);
MouseWheelZoomDelta * Math.Round(TargetZoomLevel / MouseWheelZoomDelta + mouseWheelDelta)); animated = MouseWheelZoomAnimated;
mouseWheelDelta = 0d;
} }
ZoomMap(point.Position, zoomLevel, animated);
} }
} }