XAML-Map-Control/MapControl/WinUI/Map.WinUI.cs

76 lines
2.4 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2022-01-14 20:22:56 +01:00
// © 2022 Clemens Fischer
// 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;
using Windows.UI.Xaml.Input;
2021-06-14 21:41:37 +02:00
#endif
namespace MapControl
{
2020-05-13 00:03:50 +02:00
/// <summary>
/// MapBase with default input event handling.
/// </summary>
public class Map : MapBase
{
2020-05-13 00:03:50 +02:00
public static readonly DependencyProperty MouseWheelZoomDeltaProperty = DependencyProperty.Register(
2022-11-12 01:14:07 +01:00
nameof(MouseWheelZoomDelta), typeof(double), typeof(Map), new PropertyMetadata(0.25));
2020-05-13 00:03:50 +02:00
2022-11-12 01:14:07 +01:00
private double mouseWheelDelta;
2022-01-12 23:56:05 +01:00
public Map()
{
2022-11-12 01:15:14 +01:00
ManipulationMode
= ManipulationModes.Scale
| ManipulationModes.TranslateX
| ManipulationModes.TranslateY
| ManipulationModes.TranslateInertia;
2022-11-12 01:14:07 +01:00
ManipulationDelta += OnManipulationDelta;
PointerWheelChanged += OnPointerWheelChanged;
}
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
{
2022-08-06 10:40:59 +02:00
get => (double)GetValue(MouseWheelZoomDeltaProperty);
set => SetValue(MouseWheelZoomDeltaProperty, value);
2020-05-13 00:03:50 +02:00
}
2022-11-12 01:14:07 +01:00
private void OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
2022-11-12 11:08:10 +01:00
TransformMap(e.Position, e.Delta.Translation, e.Delta.Rotation, e.Delta.Scale);
2022-01-12 23:56:05 +01:00
}
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);
mouseWheelDelta += point.Properties.MouseWheelDelta / 120d; // standard mouse wheel delta
if (Math.Abs(mouseWheelDelta) >= 1d)
{
// Zoom to integer multiple of MouseWheelZoomDelta.
ZoomMap(point.Position,
MouseWheelZoomDelta * Math.Round(TargetZoomLevel / MouseWheelZoomDelta + mouseWheelDelta));
mouseWheelDelta = 0d;
}
2022-01-12 23:56:05 +01:00
}
}
}
}