2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2022-01-14 20:22:56 +01:00
|
|
|
|
// © 2022 Clemens Fischer
|
2012-11-22 21:42:29 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
2021-01-07 19:27:25 +01:00
|
|
|
|
using System;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
#if WINUI
|
2022-01-24 23:29:35 +01:00
|
|
|
|
using Microsoft.UI.Input;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
using Microsoft.UI.Xaml;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Input;
|
|
|
|
|
|
#else
|
2022-01-24 23:29:35 +01:00
|
|
|
|
using Windows.Devices.Input;
|
2020-05-13 00:03:50 +02:00
|
|
|
|
using Windows.UI.Xaml;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using Windows.UI.Xaml.Input;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
#endif
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2020-05-13 00:03:50 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// MapBase with default input event handling.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class Map : MapBase
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2020-05-13 00:03:50 +02:00
|
|
|
|
public static readonly DependencyProperty MouseWheelZoomDeltaProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(MouseWheelZoomDelta), typeof(double), typeof(Map), new PropertyMetadata(1d));
|
|
|
|
|
|
|
2022-01-12 23:56:05 +01:00
|
|
|
|
private Point? mousePosition;
|
|
|
|
|
|
|
2012-11-26 19:17:12 +01:00
|
|
|
|
public Map()
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2020-01-17 19:51:56 +01:00
|
|
|
|
ManipulationMode = ManipulationModes.Scale
|
|
|
|
|
|
| ManipulationModes.TranslateX
|
|
|
|
|
|
| ManipulationModes.TranslateY
|
|
|
|
|
|
| ManipulationModes.TranslateInertia;
|
2013-10-10 00:46:47 +02:00
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
PointerWheelChanged += OnPointerWheelChanged;
|
2022-01-12 23:56:05 +01:00
|
|
|
|
PointerPressed += OnPointerPressed;
|
|
|
|
|
|
PointerReleased += OnPointerReleased;
|
|
|
|
|
|
PointerMoved += OnPointerMoved;
|
|
|
|
|
|
ManipulationDelta += OnManipulationDelta;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-13 00:03:50 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the amount by which the ZoomLevel property changes during a MouseWheel event.
|
|
|
|
|
|
/// The default value is 1.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double MouseWheelZoomDelta
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => (double)GetValue(MouseWheelZoomDeltaProperty);
|
|
|
|
|
|
set => SetValue(MouseWheelZoomDeltaProperty, value);
|
2020-05-13 00:03:50 +02:00
|
|
|
|
}
|
2016-08-08 20:36:02 +02:00
|
|
|
|
|
2020-01-17 19:51:56 +01:00
|
|
|
|
private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
|
|
|
|
|
|
{
|
2022-01-25 17:43:19 +01:00
|
|
|
|
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
|
|
|
|
|
|
{
|
|
|
|
|
|
var point = e.GetCurrentPoint(this);
|
|
|
|
|
|
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * Math.Sign(point.Properties.MouseWheelDelta);
|
2020-01-17 19:51:56 +01:00
|
|
|
|
|
2022-01-25 17:43:19 +01:00
|
|
|
|
ZoomMap(point.Position, MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta));
|
|
|
|
|
|
}
|
2020-01-17 19:51:56 +01:00
|
|
|
|
}
|
2022-01-12 23:56:05 +01:00
|
|
|
|
|
|
|
|
|
|
private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
|
|
|
|
|
|
{
|
2022-01-24 23:29:35 +01:00
|
|
|
|
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse &&
|
|
|
|
|
|
CapturePointer(e.Pointer))
|
2022-01-12 23:56:05 +01:00
|
|
|
|
{
|
|
|
|
|
|
mousePosition = e.GetCurrentPoint(this).Position;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnPointerReleased(object sender, PointerRoutedEventArgs e)
|
|
|
|
|
|
{
|
2022-01-24 23:29:35 +01:00
|
|
|
|
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse &&
|
|
|
|
|
|
mousePosition.HasValue)
|
2022-01-12 23:56:05 +01:00
|
|
|
|
{
|
|
|
|
|
|
mousePosition = null;
|
|
|
|
|
|
ReleasePointerCaptures();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnPointerMoved(object sender, PointerRoutedEventArgs e)
|
|
|
|
|
|
{
|
2022-01-24 23:29:35 +01:00
|
|
|
|
// Perform translation by explicit Mouse input because with Manipulation pointer capture is
|
|
|
|
|
|
// lost when Map content changes, e.g. when a MapTileLayer or WmsImageLayer loads new images.
|
|
|
|
|
|
|
|
|
|
|
|
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse &&
|
|
|
|
|
|
mousePosition.HasValue)
|
2022-01-12 23:56:05 +01:00
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|