Fixed panning for UWP/WinUI

This commit is contained in:
Clemens 2022-01-12 23:56:05 +01:00
parent 2ac4985c47
commit e2f4fc13b1
6 changed files with 128 additions and 30 deletions

View file

@ -21,6 +21,8 @@ namespace MapControl
public static readonly DependencyProperty MouseWheelZoomDeltaProperty = DependencyProperty.Register(
nameof(MouseWheelZoomDelta), typeof(double), typeof(Map), new PropertyMetadata(1d));
private Point? mousePosition;
public Map()
{
ManipulationMode = ManipulationModes.Scale
@ -28,8 +30,11 @@ namespace MapControl
| ManipulationModes.TranslateY
| ManipulationModes.TranslateInertia;
ManipulationDelta += OnManipulationDelta;
PointerWheelChanged += OnPointerWheelChanged;
PointerPressed += OnPointerPressed;
PointerReleased += OnPointerReleased;
PointerMoved += OnPointerMoved;
ManipulationDelta += OnManipulationDelta;
}
/// <summary>
@ -42,11 +47,6 @@ namespace MapControl
set { SetValue(MouseWheelZoomDeltaProperty, value); }
}
private void OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
TransformMap(e.Position, e.Delta.Translation, e.Delta.Rotation, e.Delta.Scale);
}
private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
{
var point = e.GetCurrentPoint(this);
@ -54,5 +54,42 @@ namespace MapControl
ZoomMap(point.Position, MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta));
}
private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
if (CapturePointer(e.Pointer))
{
mousePosition = e.GetCurrentPoint(this).Position;
}
}
private void OnPointerReleased(object sender, PointerRoutedEventArgs e)
{
if (mousePosition.HasValue)
{
mousePosition = null;
ReleasePointerCaptures();
}
}
private void OnPointerMoved(object sender, PointerRoutedEventArgs e)
{
if (mousePosition.HasValue)
{
Point position = e.GetCurrentPoint(this).Position;
var translation = position - mousePosition.Value;
mousePosition = position;
TranslateMap(translation);
}
}
private void OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
if (!mousePosition.HasValue)
{
TransformMap(e.Position, e.Delta.Translation, e.Delta.Rotation, e.Delta.Scale);
}
}
}
}

View file

@ -0,0 +1,33 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2021 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using System;
namespace MapControl
{
internal static class Timer
{
public static DispatcherQueueTimer CreateTimer(this DependencyObject obj, TimeSpan interval)
{
var timer = obj.DispatcherQueue.CreateTimer();
timer.Interval = interval;
return timer;
}
public static void Run(this DispatcherQueueTimer timer, bool restart = false)
{
if (restart)
{
timer.Stop();
}
if (!timer.IsRunning)
{
timer.Start();
}
}
}
}