Map and MapItem input handling

This commit is contained in:
ClemensFischer 2025-03-19 11:14:13 +01:00
parent 9da6a13950
commit 74d3a4adfe
8 changed files with 241 additions and 104 deletions

View file

@ -20,8 +20,8 @@ namespace MapControl
public static readonly DependencyProperty MouseWheelZoomDeltaProperty =
DependencyPropertyHelper.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25);
private uint capturedPointerId;
private double mouseWheelDelta;
private bool? manipulationEnabled;
public Map()
{
@ -32,8 +32,10 @@ namespace MapControl
| ManipulationModes.TranslateInertia;
ManipulationDelta += OnManipulationDelta;
ManipulationCompleted += OnManipulationCompleted;
PointerPressed += OnPointerPressed;
PointerCaptureLost += OnPointerCaptureLost;
PointerReleased += OnPointerReleased;
PointerMoved += OnPointerMoved;
PointerWheelChanged += OnPointerWheelChanged;
}
@ -49,32 +51,46 @@ namespace MapControl
private void OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
if (capturedPointerId != 0)
if (manipulationEnabled.HasValue && manipulationEnabled.Value)
{
TranslateMap(e.Delta.Translation);
}
else if (e.PointerDeviceType != PointerDeviceType.Mouse)
{
TransformMap(e.Position, e.Delta.Translation, e.Delta.Rotation, e.Delta.Scale);
if (e.PointerDeviceType == PointerDeviceType.Mouse)
{
TranslateMap(e.Delta.Translation);
}
else
{
TransformMap(e.Position, e.Delta.Translation, e.Delta.Rotation, e.Delta.Scale);
}
}
}
private void OnManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
manipulationEnabled = null;
}
private void OnPointerReleased(object sender, PointerRoutedEventArgs e)
{
manipulationEnabled = null;
}
private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse &&
e.KeyModifiers == VirtualKeyModifiers.None &&
// IsLeftButtonPressed: input was triggered by the primary action mode of an input device.
//
manipulationEnabled =
e.GetCurrentPoint(this).Properties.IsLeftButtonPressed &&
CapturePointer(e.Pointer))
{
capturedPointerId = e.Pointer.PointerId;
}
e.KeyModifiers == VirtualKeyModifiers.None;
}
private void OnPointerCaptureLost(object sender, PointerRoutedEventArgs e)
private void OnPointerMoved(object sender, PointerRoutedEventArgs e)
{
if (capturedPointerId == e.Pointer.PointerId)
// Set manipulationEnabled when no PointerPressed was received.
//
if (!manipulationEnabled.HasValue &&
e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
capturedPointerId = 0;
manipulationEnabled = e.KeyModifiers == VirtualKeyModifiers.None;
}
}

View file

@ -1,10 +1,13 @@
using Windows.System;
using System;
using Windows.System;
#if UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
#else
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
#endif
@ -25,25 +28,44 @@ namespace MapControl
item.UpdateMapTransform(newValue);
});
// Used to detect pointer movement between PointerPressed and
// PointerReleased in order to possibly cancel item selection.
//
private const float pointerMovementThreshold = 2f;
private Windows.Foundation.Point pointerPressedPosition;
public MapItem()
{
DefaultStyleKey = typeof(MapItem);
MapPanel.InitMapElement(this);
}
protected override void OnPointerPressed(PointerRoutedEventArgs e)
{
base.OnPointerPressed(e);
pointerPressedPosition = e.GetCurrentPoint(null).Position;
}
protected override void OnPointerReleased(PointerRoutedEventArgs e)
{
// In contrast to WPF and Avalonia, item selection is done on PointerReleased.
//
if (e.KeyModifiers.HasFlag(VirtualKeyModifiers.Shift))
var p = e.GetCurrentPoint(null).Position;
if (Math.Abs(p.X - pointerPressedPosition.X) <= pointerMovementThreshold &&
Math.Abs(p.Y - pointerPressedPosition.Y) <= pointerMovementThreshold &&
ItemsControl.ItemsControlFromItemContainer(this) is MapItemsControl mapItemsControl)
{
e.Handled = true;
MapItemsControl.SelectItemsInRange(this);
}
else
{
base.OnPointerReleased(e);
if (mapItemsControl.SelectionMode == SelectionMode.Extended &&
e.KeyModifiers.HasFlag(VirtualKeyModifiers.Shift))
{
mapItemsControl.SelectItemsInRange(this);
}
else
{
base.OnPointerReleased(e);
}
}
e.Handled = true;
}
protected override void OnApplyTemplate()