Map input event handling

This commit is contained in:
ClemensFischer 2025-06-12 22:10:55 +02:00
parent b605433929
commit 8c05f7d581
3 changed files with 35 additions and 37 deletions

View file

@ -26,20 +26,22 @@ namespace MapControl
set => SetValue(ManipulationModeProperty, value);
}
protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
{
base.OnPointerWheelChanged(e);
OnMouseWheel(e.GetPosition(this), e.Delta.Y);
}
private IPointer pointer1;
private IPointer pointer2;
private Point position1;
private Point position2;
protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
{
OnMouseWheel(e.GetPosition(this), e.Delta.Y);
base.OnPointerWheelChanged(e);
}
protected override void OnPointerMoved(PointerEventArgs e)
{
base.OnPointerMoved(e);
var point = e.GetCurrentPoint(this);
if (point.Pointer == pointer1 || point.Pointer == pointer2)
@ -76,12 +78,12 @@ namespace MapControl
position2 = point.Position;
}
}
base.OnPointerMoved(e);
}
protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e)
{
base.OnPointerCaptureLost(e);
if (e.Pointer == pointer1 || e.Pointer == pointer2)
{
if (e.Pointer == pointer1)
@ -92,8 +94,6 @@ namespace MapControl
pointer2 = null;
}
base.OnPointerCaptureLost(e);
}
private void HandleManipulation(IPointer pointer, Point position)

View file

@ -22,57 +22,55 @@ namespace MapControl
set => SetValue(ManipulationModeProperty, value);
}
private Point? mousePosition;
protected override void OnMouseWheel(MouseWheelEventArgs e)
{
base.OnMouseWheel(e);
// Standard mouse wheel delta value is 120.
//
OnMouseWheel(e.GetPosition(this), e.Delta / 120d);
base.OnMouseWheel(e);
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
private Point? mousePosition;
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.None &&
CaptureMouse())
base.OnPreviewMouseLeftButtonDown(e);
if (Keyboard.Modifiers == ModifierKeys.None)
{
// Do not call CaptureMouse here because it avoids MapItem selection.
//
mousePosition = e.GetPosition(this);
}
base.OnMouseLeftButtonDown(e);
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnPreviewMouseLeftButtonUp(e);
if (mousePosition.HasValue)
{
mousePosition = null;
ReleaseMouseCapture();
}
base.OnMouseLeftButtonUp(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (mousePosition.HasValue)
{
if (!IsMouseCaptured)
{
CaptureMouse();
}
var p = e.GetPosition(this);
TranslateMap(new Point(p.X - mousePosition.Value.X, p.Y - mousePosition.Value.Y));
mousePosition = p;
}
else if (e.LeftButton == MouseButtonState.Pressed &&
Keyboard.Modifiers == ModifierKeys.None &&
CaptureMouse())
{
// Set mousePosition when no MouseLeftButtonDown event was received.
//
mousePosition = e.GetPosition(this);
}
base.OnMouseMove(e);
}
protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
@ -84,12 +82,12 @@ namespace MapControl
protected override void OnManipulationDelta(ManipulationDeltaEventArgs e)
{
base.OnManipulationDelta(e);
TransformMap(e.ManipulationOrigin,
(Point)e.DeltaManipulation.Translation,
e.DeltaManipulation.Rotation,
e.DeltaManipulation.Scale.LengthSquared / 2d);
base.OnManipulationDelta(e);
}
}
}

View file

@ -26,8 +26,6 @@ namespace MapControl
ManipulationCompleted += OnManipulationCompleted;
}
private bool? manipulationEnabled;
private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
@ -40,6 +38,8 @@ namespace MapControl
}
}
private bool? manipulationEnabled;
private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
// Set manipulationEnabled before ManipulationStarted.