2025-02-27 18:46:32 +01:00
|
|
|
|
using System;
|
2023-01-06 18:12:29 +01:00
|
|
|
|
using Windows.System;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if UWP
|
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;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#else
|
|
|
|
|
|
using Microsoft.UI.Input;
|
|
|
|
|
|
using Microsoft.UI.Xaml;
|
|
|
|
|
|
using Microsoft.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
|
|
|
|
{
|
2024-05-23 18:08:14 +02:00
|
|
|
|
public static readonly DependencyProperty MouseWheelZoomDeltaProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25);
|
2020-05-13 00:03:50 +02:00
|
|
|
|
|
2022-11-12 01:14:07 +01:00
|
|
|
|
private double mouseWheelDelta;
|
2025-03-19 16:06:33 +01:00
|
|
|
|
private bool? manipulationEnabled;
|
2022-01-12 23:56:05 +01:00
|
|
|
|
|
2012-11-26 19:17:12 +01:00
|
|
|
|
public Map()
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2022-11-12 01:15:14 +01:00
|
|
|
|
ManipulationMode
|
|
|
|
|
|
= ManipulationModes.Scale
|
2020-01-17 19:51:56 +01:00
|
|
|
|
| ManipulationModes.TranslateX
|
|
|
|
|
|
| ManipulationModes.TranslateY
|
|
|
|
|
|
| ManipulationModes.TranslateInertia;
|
2013-10-10 00:46:47 +02:00
|
|
|
|
|
2022-11-12 01:14:07 +01:00
|
|
|
|
ManipulationDelta += OnManipulationDelta;
|
2025-03-19 11:14:13 +01:00
|
|
|
|
ManipulationCompleted += OnManipulationCompleted;
|
2023-01-06 15:59:06 +01:00
|
|
|
|
PointerPressed += OnPointerPressed;
|
2025-03-19 16:06:33 +01:00
|
|
|
|
PointerMoved += OnPointerMoved;
|
2022-11-12 01:14:07 +01:00
|
|
|
|
PointerWheelChanged += OnPointerWheelChanged;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-13 00:03:50 +02:00
|
|
|
|
/// <summary>
|
2022-11-12 01:14:07 +01:00
|
|
|
|
/// Gets or sets the amount by which the ZoomLevel property changes by a PointerWheelChanged event.
|
|
|
|
|
|
/// The default value is 0.25.
|
2020-05-13 00:03:50 +02:00
|
|
|
|
/// </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
|
|
|
|
|
2022-11-12 01:14:07 +01:00
|
|
|
|
private void OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
|
2020-01-17 19:51:56 +01:00
|
|
|
|
{
|
2025-03-19 16:06:33 +01:00
|
|
|
|
if (manipulationEnabled.HasValue && manipulationEnabled.Value)
|
2024-09-09 21:50:29 +02:00
|
|
|
|
{
|
2025-03-19 11:14:13 +01:00
|
|
|
|
if (e.PointerDeviceType == PointerDeviceType.Mouse)
|
|
|
|
|
|
{
|
|
|
|
|
|
TranslateMap(e.Delta.Translation);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
TransformMap(e.Position, e.Delta.Translation, e.Delta.Rotation, e.Delta.Scale);
|
|
|
|
|
|
}
|
2023-01-06 15:59:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-19 11:14:13 +01:00
|
|
|
|
private void OnManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
|
|
|
|
|
|
{
|
2025-03-19 16:06:33 +01:00
|
|
|
|
manipulationEnabled = null;
|
2025-03-19 11:14:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-06 15:59:06 +01:00
|
|
|
|
private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
|
|
|
|
|
|
{
|
2025-03-19 20:11:05 +01:00
|
|
|
|
// Set manipulationEnabled before ManipulationStarted.
|
2025-03-19 11:14:13 +01:00
|
|
|
|
// IsLeftButtonPressed: input was triggered by the primary action mode of an input device.
|
|
|
|
|
|
//
|
|
|
|
|
|
manipulationEnabled =
|
2025-01-06 16:54:43 +01:00
|
|
|
|
e.GetCurrentPoint(this).Properties.IsLeftButtonPressed &&
|
2025-03-19 11:14:13 +01:00
|
|
|
|
e.KeyModifiers == VirtualKeyModifiers.None;
|
2023-01-06 15:59:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-19 16:06:33 +01:00
|
|
|
|
private void OnPointerMoved(object sender, PointerRoutedEventArgs e)
|
|
|
|
|
|
{
|
2025-03-19 20:11:05 +01:00
|
|
|
|
// Set manipulationEnabled before ManipulationStarted when no PointerPressed was received.
|
2025-03-19 16:06:33 +01:00
|
|
|
|
//
|
|
|
|
|
|
if (!manipulationEnabled.HasValue &&
|
|
|
|
|
|
e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
|
|
|
|
|
|
{
|
|
|
|
|
|
manipulationEnabled = e.KeyModifiers == VirtualKeyModifiers.None;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-12 01:14:07 +01:00
|
|
|
|
private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
|
2022-01-12 23:56:05 +01:00
|
|
|
|
{
|
2022-11-12 01:14:07 +01:00
|
|
|
|
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
|
2022-01-12 23:56:05 +01:00
|
|
|
|
{
|
2022-11-12 01:14:07 +01:00
|
|
|
|
var point = e.GetCurrentPoint(this);
|
|
|
|
|
|
|
2022-11-30 22:18:45 +01:00
|
|
|
|
// Standard mouse wheel delta value is 120.
|
|
|
|
|
|
//
|
|
|
|
|
|
mouseWheelDelta += point.Properties.MouseWheelDelta / 120d;
|
2022-11-12 01:14:07 +01:00
|
|
|
|
|
|
|
|
|
|
if (Math.Abs(mouseWheelDelta) >= 1d)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Zoom to integer multiple of MouseWheelZoomDelta.
|
2024-08-24 21:06:12 +02:00
|
|
|
|
//
|
2022-11-12 01:14:07 +01:00
|
|
|
|
ZoomMap(point.Position,
|
|
|
|
|
|
MouseWheelZoomDelta * Math.Round(TargetZoomLevel / MouseWheelZoomDelta + mouseWheelDelta));
|
|
|
|
|
|
|
|
|
|
|
|
mouseWheelDelta = 0d;
|
|
|
|
|
|
}
|
2022-01-12 23:56:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|