From 8c05f7d581794b11e7d33bfbd95058fad07dc918 Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Thu, 12 Jun 2025 22:10:55 +0200 Subject: [PATCH] Map input event handling --- MapControl/Avalonia/Map.Avalonia.cs | 22 +++++++------- MapControl/WPF/Map.WPF.cs | 46 ++++++++++++++--------------- MapControl/WinUI/Map.WinUI.cs | 4 +-- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/MapControl/Avalonia/Map.Avalonia.cs b/MapControl/Avalonia/Map.Avalonia.cs index 5bbe2075..45ef7b89 100644 --- a/MapControl/Avalonia/Map.Avalonia.cs +++ b/MapControl/Avalonia/Map.Avalonia.cs @@ -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) diff --git a/MapControl/WPF/Map.WPF.cs b/MapControl/WPF/Map.WPF.cs index 49bce609..926b2683 100644 --- a/MapControl/WPF/Map.WPF.cs +++ b/MapControl/WPF/Map.WPF.cs @@ -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); } } } diff --git a/MapControl/WinUI/Map.WinUI.cs b/MapControl/WinUI/Map.WinUI.cs index 0ce141d0..cce1da85 100644 --- a/MapControl/WinUI/Map.WinUI.cs +++ b/MapControl/WinUI/Map.WinUI.cs @@ -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.