From caa25cb47192baa00e8c8eb2afbc8ed2a0d8d548 Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Mon, 9 Sep 2024 21:50:29 +0200 Subject: [PATCH] MoveMap method with higher accuracy --- MapControl/Avalonia/Map.Avalonia.cs | 9 +++++++-- MapControl/Shared/MapBase.cs | 26 ++++++++++++++++++++++++-- MapControl/WPF/Map.WPF.cs | 18 +++++------------- MapControl/WinUI/Map.WinUI.cs | 24 ++++++++++++++++++------ 4 files changed, 54 insertions(+), 23 deletions(-) diff --git a/MapControl/Avalonia/Map.Avalonia.cs b/MapControl/Avalonia/Map.Avalonia.cs index 5c1c23e6..1f3f7251 100644 --- a/MapControl/Avalonia/Map.Avalonia.cs +++ b/MapControl/Avalonia/Map.Avalonia.cs @@ -74,6 +74,7 @@ namespace MapControl point.Pointer.Type == PointerType.Touch && HandleTouchPressed(point)) { point.Pointer.Capture(this); + SetTransformCenter(point.Position); } base.OnPointerPressed(e); @@ -83,6 +84,7 @@ namespace MapControl { if (HandlePointerReleased(e.Pointer)) { + EndMoveMap(); e.Pointer.Capture(null); } @@ -91,7 +93,10 @@ namespace MapControl protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e) { - HandlePointerReleased(e.Pointer); + if (HandlePointerReleased(e.Pointer)) + { + EndMoveMap(); + } base.OnPointerCaptureLost(e); } @@ -108,8 +113,8 @@ namespace MapControl } else if (e.Pointer.Type == PointerType.Mouse || ManipulationModes.HasFlag(ManipulationModes.Translate)) { - TranslateMap(position - position1); position1 = position; + MoveMap(position); } } diff --git a/MapControl/Shared/MapBase.cs b/MapControl/Shared/MapBase.cs index 9ed20aec..7fa2d9a2 100644 --- a/MapControl/Shared/MapBase.cs +++ b/MapControl/Shared/MapBase.cs @@ -278,15 +278,37 @@ namespace MapControl } /// - /// Changes the Center property according to the specified translation in view coordinates. + /// Moves the map by the difference of the specified position in view coordinates and a temporary + /// transform origin point that has been set before by a call to SetTransformCenter. Map movement + /// must be terminated by a call to EndMoveMap. MoveMap provides higher accuracy than TranslateMap. /// - public void TranslateMap(Point translation) + public void MoveMap(Point position) + { + if (transformCenter != null) + { + viewCenter = position; + UpdateTransform(); + } + } + + /// + /// Terminates map movement by the MoveMap method. + /// + public void EndMoveMap() { if (transformCenter != null) { ResetTransformCenter(); UpdateTransform(); } + } + + /// + /// Changes the Center property according to the specified translation in view coordinates. + /// + public void TranslateMap(Point translation) + { + EndMoveMap(); if (translation.X != 0d || translation.Y != 0d) { diff --git a/MapControl/WPF/Map.WPF.cs b/MapControl/WPF/Map.WPF.cs index 962d6e09..2a8ebd58 100644 --- a/MapControl/WPF/Map.WPF.cs +++ b/MapControl/WPF/Map.WPF.cs @@ -19,7 +19,6 @@ namespace MapControl public static readonly DependencyProperty ManipulationModeProperty = DependencyPropertyHelper.Register(nameof(ManipulationMode), ManipulationModes.Translate | ManipulationModes.Scale); - private Point? mousePosition; private double mouseWheelDelta; static Map() @@ -73,28 +72,21 @@ namespace MapControl { if (Keyboard.Modifiers == ModifierKeys.None && CaptureMouse()) { - mousePosition = e.GetPosition(this); + SetTransformCenter(e.GetPosition(this)); } } private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { - if (mousePosition.HasValue) - { - mousePosition = null; - ReleaseMouseCapture(); - } + EndMoveMap(); + ReleaseMouseCapture(); } private void OnMouseMove(object sender, MouseEventArgs e) { - if (mousePosition.HasValue) + if (e.LeftButton == MouseButtonState.Pressed) { - var position = e.GetPosition(this); - var translation = new Point(position.X - mousePosition.Value.X, position.Y - mousePosition.Value.Y); - mousePosition = position; - - TranslateMap(translation); + MoveMap(e.GetPosition(this)); } } diff --git a/MapControl/WinUI/Map.WinUI.cs b/MapControl/WinUI/Map.WinUI.cs index 41297eb5..4fc47c50 100644 --- a/MapControl/WinUI/Map.WinUI.cs +++ b/MapControl/WinUI/Map.WinUI.cs @@ -24,7 +24,7 @@ namespace MapControl public static readonly DependencyProperty MouseWheelZoomDeltaProperty = DependencyPropertyHelper.Register(nameof(MouseWheelZoomDelta), 0.25); - private bool manipulationEnabled; + private bool mouseMoveEnabled; private double mouseWheelDelta; public Map() @@ -53,7 +53,11 @@ namespace MapControl private void OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e) { - if (manipulationEnabled) + if (mouseMoveEnabled || e.Delta.Rotation == 0d && e.Delta.Scale == 1d) + { + MoveMap(e.Position); + } + else if (e.PointerDeviceType != PointerDeviceType.Mouse) { TransformMap(e.Position, e.Delta.Translation, e.Delta.Rotation, e.Delta.Scale); } @@ -61,14 +65,22 @@ namespace MapControl private void OnPointerPressed(object sender, PointerRoutedEventArgs e) { - manipulationEnabled = e.Pointer.PointerDeviceType != PointerDeviceType.Mouse || - e.KeyModifiers == VirtualKeyModifiers.None && - e.GetCurrentPoint(this).Properties.IsLeftButtonPressed; + var point = e.GetCurrentPoint(this); + + mouseMoveEnabled = e.Pointer.PointerDeviceType == PointerDeviceType.Mouse && + e.KeyModifiers == VirtualKeyModifiers.None && + point.Properties.IsLeftButtonPressed; + + if (mouseMoveEnabled || e.Pointer.PointerDeviceType != PointerDeviceType.Mouse) + { + SetTransformCenter(point.Position); + } } private void OnPointerReleased(object sender, PointerRoutedEventArgs e) { - manipulationEnabled = false; + mouseMoveEnabled = false; + EndMoveMap(); } private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)