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

85 lines
2.8 KiB
C#
Raw Normal View History

2025-06-12 07:41:01 +02: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;
using Windows.UI.Xaml.Input;
2024-05-22 11:25:32 +02:00
#else
using Microsoft.UI.Input;
using Microsoft.UI.Xaml.Input;
2021-06-14 21:41:37 +02:00
#endif
namespace MapControl
{
2025-06-12 07:41:01 +02:00
public partial class Map
{
public Map()
{
2022-11-12 01:15:14 +01:00
ManipulationMode
= ManipulationModes.Scale
| ManipulationModes.TranslateX
| ManipulationModes.TranslateY
| ManipulationModes.TranslateInertia;
2025-06-11 22:39:54 +02:00
PointerWheelChanged += OnPointerWheelChanged;
PointerPressed += OnPointerPressed;
PointerMoved += OnPointerMoved;
2025-06-11 22:39:54 +02:00
ManipulationDelta += OnManipulationDelta;
ManipulationCompleted += OnManipulationCompleted;
}
2025-06-11 22:39:54 +02:00
private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
{
2025-06-11 22:39:54 +02:00
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
2024-09-09 21:50:29 +02:00
{
2025-06-11 22:39:54 +02:00
var point = e.GetCurrentPoint(this);
2025-06-12 00:31:01 +02:00
2025-06-12 07:43:55 +02:00
// Standard mouse wheel delta value is 120.
//
OnMouseWheel(point.Position, point.Properties.MouseWheelDelta / 120d);
}
}
2025-06-12 22:10:55 +02:00
private bool? manipulationEnabled;
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;
}
private void OnPointerMoved(object sender, PointerRoutedEventArgs e)
{
2025-03-19 20:11:05 +01:00
// Set manipulationEnabled before ManipulationStarted when no PointerPressed was received.
//
if (!manipulationEnabled.HasValue &&
e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
manipulationEnabled = e.KeyModifiers == VirtualKeyModifiers.None;
}
}
2025-06-11 22:39:54 +02:00
private void OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
2022-01-12 23:56:05 +01:00
{
2025-06-11 22:39:54 +02:00
if (manipulationEnabled.HasValue && manipulationEnabled.Value)
2022-01-12 23:56:05 +01:00
{
2025-06-11 22:39:54 +02:00
if (e.PointerDeviceType == PointerDeviceType.Mouse)
2022-11-12 01:14:07 +01:00
{
2025-06-11 22:39:54 +02:00
TranslateMap(e.Delta.Translation);
}
else
{
TransformMap(e.Position, e.Delta.Translation, e.Delta.Rotation, e.Delta.Scale);
2022-11-12 01:14:07 +01:00
}
2022-01-12 23:56:05 +01:00
}
}
2025-06-11 22:39:54 +02:00
private void OnManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
manipulationEnabled = null;
}
}
}