MoveMap method with higher accuracy

This commit is contained in:
ClemensFischer 2024-09-09 21:50:29 +02:00
parent 94b6d47c4d
commit caa25cb471
4 changed files with 54 additions and 23 deletions

View file

@ -74,6 +74,7 @@ namespace MapControl
point.Pointer.Type == PointerType.Touch && HandleTouchPressed(point)) point.Pointer.Type == PointerType.Touch && HandleTouchPressed(point))
{ {
point.Pointer.Capture(this); point.Pointer.Capture(this);
SetTransformCenter(point.Position);
} }
base.OnPointerPressed(e); base.OnPointerPressed(e);
@ -83,6 +84,7 @@ namespace MapControl
{ {
if (HandlePointerReleased(e.Pointer)) if (HandlePointerReleased(e.Pointer))
{ {
EndMoveMap();
e.Pointer.Capture(null); e.Pointer.Capture(null);
} }
@ -91,7 +93,10 @@ namespace MapControl
protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e) protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e)
{ {
HandlePointerReleased(e.Pointer); if (HandlePointerReleased(e.Pointer))
{
EndMoveMap();
}
base.OnPointerCaptureLost(e); base.OnPointerCaptureLost(e);
} }
@ -108,8 +113,8 @@ namespace MapControl
} }
else if (e.Pointer.Type == PointerType.Mouse || ManipulationModes.HasFlag(ManipulationModes.Translate)) else if (e.Pointer.Type == PointerType.Mouse || ManipulationModes.HasFlag(ManipulationModes.Translate))
{ {
TranslateMap(position - position1);
position1 = position; position1 = position;
MoveMap(position);
} }
} }

View file

@ -278,15 +278,37 @@ namespace MapControl
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
public void TranslateMap(Point translation) public void MoveMap(Point position)
{
if (transformCenter != null)
{
viewCenter = position;
UpdateTransform();
}
}
/// <summary>
/// Terminates map movement by the MoveMap method.
/// </summary>
public void EndMoveMap()
{ {
if (transformCenter != null) if (transformCenter != null)
{ {
ResetTransformCenter(); ResetTransformCenter();
UpdateTransform(); UpdateTransform();
} }
}
/// <summary>
/// Changes the Center property according to the specified translation in view coordinates.
/// </summary>
public void TranslateMap(Point translation)
{
EndMoveMap();
if (translation.X != 0d || translation.Y != 0d) if (translation.X != 0d || translation.Y != 0d)
{ {

View file

@ -19,7 +19,6 @@ namespace MapControl
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);
private Point? mousePosition;
private double mouseWheelDelta; private double mouseWheelDelta;
static Map() static Map()
@ -73,28 +72,21 @@ namespace MapControl
{ {
if (Keyboard.Modifiers == ModifierKeys.None && CaptureMouse()) if (Keyboard.Modifiers == ModifierKeys.None && CaptureMouse())
{ {
mousePosition = e.GetPosition(this); SetTransformCenter(e.GetPosition(this));
} }
} }
private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{ {
if (mousePosition.HasValue) EndMoveMap();
{ ReleaseMouseCapture();
mousePosition = null;
ReleaseMouseCapture();
}
} }
private void OnMouseMove(object sender, MouseEventArgs e) private void OnMouseMove(object sender, MouseEventArgs e)
{ {
if (mousePosition.HasValue) if (e.LeftButton == MouseButtonState.Pressed)
{ {
var position = e.GetPosition(this); MoveMap(e.GetPosition(this));
var translation = new Point(position.X - mousePosition.Value.X, position.Y - mousePosition.Value.Y);
mousePosition = position;
TranslateMap(translation);
} }
} }

View file

@ -24,7 +24,7 @@ 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 bool manipulationEnabled; private bool mouseMoveEnabled;
private double mouseWheelDelta; private double mouseWheelDelta;
public Map() public Map()
@ -53,7 +53,11 @@ namespace MapControl
private void OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e) 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); 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) private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
{ {
manipulationEnabled = e.Pointer.PointerDeviceType != PointerDeviceType.Mouse || var point = e.GetCurrentPoint(this);
e.KeyModifiers == VirtualKeyModifiers.None &&
e.GetCurrentPoint(this).Properties.IsLeftButtonPressed; 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) private void OnPointerReleased(object sender, PointerRoutedEventArgs e)
{ {
manipulationEnabled = false; mouseMoveEnabled = false;
EndMoveMap();
} }
private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e) private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)