2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2020-01-09 19:40:10 +01:00
|
|
|
|
// © 2020 Clemens Fischer
|
2013-05-07 18:12:25 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
2020-01-17 19:51:56 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2013-05-07 18:12:25 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Input;
|
2020-01-17 19:51:56 +01:00
|
|
|
|
using System.Windows.Threading;
|
2013-05-07 18:12:25 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2020-01-17 19:51:56 +01:00
|
|
|
|
public partial class Map
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2013-05-08 17:05:17 +02:00
|
|
|
|
public static readonly DependencyProperty ManipulationModeProperty = DependencyProperty.Register(
|
2017-06-25 23:05:48 +02:00
|
|
|
|
nameof(ManipulationMode), typeof(ManipulationModes), typeof(Map), new PropertyMetadata(ManipulationModes.All));
|
2013-05-08 17:05:17 +02:00
|
|
|
|
|
2020-01-17 19:51:56 +01:00
|
|
|
|
public static readonly DependencyProperty TransformDelayProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(TransformDelay), typeof(TimeSpan), typeof(Map), new PropertyMetadata(TimeSpan.FromMilliseconds(50)));
|
2013-05-08 17:05:17 +02:00
|
|
|
|
|
2013-05-07 18:12:25 +02:00
|
|
|
|
private Point? mousePosition;
|
|
|
|
|
|
|
2013-05-08 17:05:17 +02:00
|
|
|
|
static Map()
|
|
|
|
|
|
{
|
|
|
|
|
|
IsManipulationEnabledProperty.OverrideMetadata(typeof(Map), new FrameworkPropertyMetadata(true));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-17 19:51:56 +01:00
|
|
|
|
public Map()
|
|
|
|
|
|
{
|
|
|
|
|
|
ManipulationStarted += OnManipulationStarted;
|
|
|
|
|
|
ManipulationDelta += OnManipulationDelta;
|
|
|
|
|
|
MouseLeftButtonDown += OnMouseLeftButtonDown;
|
|
|
|
|
|
MouseLeftButtonUp += OnMouseLeftButtonUp;
|
|
|
|
|
|
MouseMove += OnMouseMove;
|
|
|
|
|
|
MouseWheel += OnMouseWheel;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-05-08 17:05:17 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets a value that specifies how the map control handles manipulations.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public ManipulationModes ManipulationMode
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2013-05-08 17:05:17 +02:00
|
|
|
|
get { return (ManipulationModes)GetValue(ManipulationModeProperty); }
|
|
|
|
|
|
set { SetValue(ManipulationModeProperty, value); }
|
2013-05-07 18:12:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-01-17 19:51:56 +01:00
|
|
|
|
/// Gets or sets a delay interval between adjacent calls to TranslateMap or TransformMap during mouse pan and manipulation.
|
|
|
|
|
|
/// The default value is 50 milliseconds.
|
2013-05-07 18:12:25 +02:00
|
|
|
|
/// </summary>
|
2020-01-17 19:51:56 +01:00
|
|
|
|
public TimeSpan TransformDelay
|
2013-05-08 17:05:17 +02:00
|
|
|
|
{
|
2020-01-17 19:51:56 +01:00
|
|
|
|
get { return (TimeSpan)GetValue(TransformDelayProperty); }
|
|
|
|
|
|
set { SetValue(TransformDelayProperty, value); }
|
2013-05-08 17:05:17 +02:00
|
|
|
|
}
|
2013-05-07 18:12:25 +02:00
|
|
|
|
|
2020-01-17 19:51:56 +01:00
|
|
|
|
private async Task InvokeTransformAsync(Action action)
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2020-01-17 19:51:56 +01:00
|
|
|
|
if (!transformPending)
|
|
|
|
|
|
{
|
|
|
|
|
|
transformPending = true;
|
2013-05-07 18:12:25 +02:00
|
|
|
|
|
2020-01-17 19:51:56 +01:00
|
|
|
|
if (TransformDelay > TimeSpan.Zero)
|
|
|
|
|
|
{
|
|
|
|
|
|
await Task.Delay(TransformDelay);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await Dispatcher.InvokeAsync(action);
|
|
|
|
|
|
|
|
|
|
|
|
ResetTransform();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnManipulationStarted(object sender, ManipulationStartedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Manipulation.SetManipulationMode(this, ManipulationMode);
|
2013-05-07 18:12:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-17 19:51:56 +01:00
|
|
|
|
private async void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2020-01-17 19:51:56 +01:00
|
|
|
|
translation.X += e.DeltaManipulation.Translation.X;
|
|
|
|
|
|
translation.Y += e.DeltaManipulation.Translation.Y;
|
|
|
|
|
|
rotation += e.DeltaManipulation.Rotation;
|
2020-01-19 21:36:34 +01:00
|
|
|
|
scale *= e.DeltaManipulation.Scale.LengthSquared / 2d;
|
2013-05-07 18:12:25 +02:00
|
|
|
|
|
2020-01-17 19:51:56 +01:00
|
|
|
|
await InvokeTransformAsync(() => TransformMap(e.ManipulationOrigin, translation, rotation, scale));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
|
|
|
|
|
{
|
2013-05-07 18:12:25 +02:00
|
|
|
|
if (CaptureMouse())
|
|
|
|
|
|
{
|
|
|
|
|
|
mousePosition = e.GetPosition(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-17 19:51:56 +01:00
|
|
|
|
private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (mousePosition.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
mousePosition = null;
|
|
|
|
|
|
ReleaseMouseCapture();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-17 19:51:56 +01:00
|
|
|
|
private async void OnMouseMove(object sender, MouseEventArgs e)
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (mousePosition.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
var position = e.GetPosition(this);
|
2020-01-17 19:51:56 +01:00
|
|
|
|
translation += position - mousePosition.Value;
|
2013-05-07 18:12:25 +02:00
|
|
|
|
mousePosition = position;
|
|
|
|
|
|
|
2020-01-17 19:51:56 +01:00
|
|
|
|
await InvokeTransformAsync(() => TranslateMap(translation));
|
|
|
|
|
|
}
|
2013-05-08 17:05:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-17 19:51:56 +01:00
|
|
|
|
private void OnMouseWheel(object sender, MouseWheelEventArgs e)
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2020-01-17 19:51:56 +01:00
|
|
|
|
var zoomDelta = MouseWheelZoomDelta * e.Delta / 120d;
|
2013-05-07 18:12:25 +02:00
|
|
|
|
|
2020-01-17 19:51:56 +01:00
|
|
|
|
ZoomMap(e.GetPosition(this), TargetZoomLevel + zoomDelta);
|
2013-05-07 18:12:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|