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

@ -73,16 +73,13 @@ namespace MapControl
#if WINUI #if WINUI
private readonly DispatcherQueueTimer updateTimer; private readonly DispatcherQueueTimer updateTimer;
#else #else
private readonly DispatcherTimer updateTimer = new DispatcherTimer(); private readonly DispatcherTimer updateTimer;
#endif #endif
private bool updateInProgress; private bool updateInProgress;
public MapImageLayer() public MapImageLayer()
{ {
#if WINUI updateTimer = this.CreateTimer(UpdateInterval);
updateTimer = DispatcherQueue.CreateTimer();
#endif
updateTimer.Interval = UpdateInterval;
updateTimer.Tick += async (s, e) => await UpdateImageAsync(); updateTimer.Tick += async (s, e) => await UpdateImageAsync();
} }
@ -227,12 +224,7 @@ namespace MapControl
base.OnViewportChanged(e); base.OnViewportChanged(e);
if (!UpdateWhileViewportChanging) updateTimer.Run(!UpdateWhileViewportChanging);
{
updateTimer.Stop(); // restart
}
updateTimer.Start();
} }
} }
@ -242,7 +234,7 @@ namespace MapControl
if (updateInProgress) if (updateInProgress)
{ {
updateTimer.Start(); // update image on next timer tick updateTimer.Run(); // update image on next timer tick
} }
else if (ParentMap != null && ParentMap.RenderSize.Width > 0 && ParentMap.RenderSize.Height > 0) else if (ParentMap != null && ParentMap.RenderSize.Width > 0 && ParentMap.RenderSize.Height > 0)
{ {

View file

@ -61,7 +61,7 @@ namespace MapControl
#if WINUI #if WINUI
private readonly DispatcherQueueTimer updateTimer; private readonly DispatcherQueueTimer updateTimer;
#else #else
private readonly DispatcherTimer updateTimer = new DispatcherTimer(); private readonly DispatcherTimer updateTimer;
#endif #endif
private MapBase parentMap; private MapBase parentMap;
@ -69,10 +69,8 @@ namespace MapControl
{ {
RenderTransform = new MatrixTransform(); RenderTransform = new MatrixTransform();
TileImageLoader = tileImageLoader; TileImageLoader = tileImageLoader;
#if WINUI
updateTimer = DispatcherQueue.CreateTimer(); updateTimer = this.CreateTimer(UpdateInterval);
#endif
updateTimer.Interval = UpdateInterval;
updateTimer.Tick += async (s, e) => await Update(); updateTimer.Tick += async (s, e) => await Update();
#if WINUI || UWP #if WINUI || UWP
@ -172,7 +170,7 @@ namespace MapControl
parentMap.ViewportChanged += OnViewportChanged; parentMap.ViewportChanged += OnViewportChanged;
} }
updateTimer.Start(); updateTimer.Run();
} }
} }
@ -207,12 +205,7 @@ namespace MapControl
{ {
SetRenderTransform(); SetRenderTransform();
if (!UpdateWhileViewportChanging) updateTimer.Run(!UpdateWhileViewportChanging);
{
updateTimer.Stop(); // restart
}
updateTimer.Start();
} }
} }
} }

View file

@ -227,6 +227,9 @@
<Compile Include="..\WinUI\Vector.WinUI.cs"> <Compile Include="..\WinUI\Vector.WinUI.cs">
<Link>Vector.WinUI.cs</Link> <Link>Vector.WinUI.cs</Link>
</Compile> </Compile>
<Compile Include="..\WPF\Timer.WPF.cs">
<Link>Timer.WPF.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TileImageLoader.UWP.cs" /> <Compile Include="TileImageLoader.UWP.cs" />
<EmbeddedResource Include="Properties\MapControl.UWP.rd.xml" /> <EmbeddedResource Include="Properties\MapControl.UWP.rd.xml" />

View file

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

View file

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