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.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);
}
}

View file

@ -278,15 +278,37 @@ namespace MapControl
}
/// <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>
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)
{
ResetTransformCenter();
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)
{

View file

@ -19,7 +19,6 @@ namespace MapControl
public static readonly DependencyProperty ManipulationModeProperty =
DependencyPropertyHelper.Register<Map, ManipulationModes>(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;
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));
}
}

View file

@ -24,7 +24,7 @@ namespace MapControl
public static readonly DependencyProperty MouseWheelZoomDeltaProperty =
DependencyPropertyHelper.Register<Map, double>(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 ||
var point = e.GetCurrentPoint(this);
mouseMoveEnabled = e.Pointer.PointerDeviceType == PointerDeviceType.Mouse &&
e.KeyModifiers == VirtualKeyModifiers.None &&
e.GetCurrentPoint(this).Properties.IsLeftButtonPressed;
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)