Partial class Map

This commit is contained in:
ClemensFischer 2025-06-12 07:41:01 +02:00
parent cceb122486
commit 5777e9cff3
6 changed files with 101 additions and 158 deletions

View file

@ -12,68 +12,28 @@ namespace MapControl
All = Translate | Rotate | Scale
}
/// <summary>
/// MapBase with default input event handling.
/// </summary>
public class Map : MapBase
public partial class Map
{
public static readonly StyledProperty<ManipulationModes> ManipulationModesProperty =
DependencyPropertyHelper.Register<Map, ManipulationModes>(nameof(ManipulationModes), ManipulationModes.Translate | ManipulationModes.Scale);
public static readonly StyledProperty<ManipulationModes> ManipulationModeProperty =
DependencyPropertyHelper.Register<Map, ManipulationModes>(nameof(ManipulationMode), ManipulationModes.Translate | ManipulationModes.Scale);
public static readonly StyledProperty<double> MouseWheelZoomDeltaProperty =
DependencyPropertyHelper.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25);
public static readonly DependencyProperty MouseWheelZoomAnimatedProperty =
DependencyPropertyHelper.Register<Map, bool>(nameof(MouseWheelZoomAnimated), true);
/// <summary>
/// Gets or sets a value that specifies how the map control handles manipulations.
/// </summary>
public ManipulationModes ManipulationMode
{
get => GetValue(ManipulationModeProperty);
set => SetValue(ManipulationModeProperty, value);
}
private IPointer pointer1;
private IPointer pointer2;
private Point position1;
private Point position2;
public ManipulationModes ManipulationModes
{
get => GetValue(ManipulationModesProperty);
set => SetValue(ManipulationModesProperty, value);
}
/// <summary>
/// Gets or sets the amount by which the ZoomLevel property changes by a PointerWheelChanged event.
/// The default value is 0.25.
/// </summary>
public double MouseWheelZoomDelta
{
get => GetValue(MouseWheelZoomDeltaProperty);
set => SetValue(MouseWheelZoomDeltaProperty, value);
}
/// <summary>
/// Gets or sets a value that controls whether zooming by a PointerWheelChanged event is animated.
/// The default value is true.
/// </summary>
public bool MouseWheelZoomAnimated
{
get => (bool)GetValue(MouseWheelZoomAnimatedProperty);
set => SetValue(MouseWheelZoomAnimatedProperty, value);
}
protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
{
var delta = e.Delta.Y;
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * delta;
var animated = false;
if (delta % 1d == 0d)
{
// Zoom to integer multiple of MouseWheelZoomDelta when delta is an integer value,
// i.e. when the event was actually raised by a mouse wheel and not by a touch pad
// or a similar device with higher resolution.
//
zoomLevel = MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta);
animated = MouseWheelZoomAnimated;
}
ZoomMap(e.GetPosition(this), zoomLevel, animated);
OnMouseWheel(e.GetPosition(this), e.Delta.Y);
base.OnPointerWheelChanged(e);
}
@ -89,7 +49,7 @@ namespace MapControl
HandleManipulation(point.Pointer, point.Position);
}
else if (point.Pointer.Type == PointerType.Mouse ||
ManipulationModes.HasFlag(ManipulationModes.Translate))
ManipulationMode.HasFlag(ManipulationModes.Translate))
{
TranslateMap(new Point(point.Position.X - position1.X, point.Position.Y - position1.Y));
position1 = point.Position;
@ -101,7 +61,7 @@ namespace MapControl
e.KeyModifiers == KeyModifiers.None ||
pointer2 == null &&
point.Pointer.Type == PointerType.Touch &&
ManipulationModes != ManipulationModes.None)
ManipulationMode != ManipulationModes.None)
{
point.Pointer.Capture(this);
@ -156,19 +116,19 @@ namespace MapControl
var rotation = 0d;
var scale = 1d;
if (ManipulationModes.HasFlag(ManipulationModes.Translate))
if (ManipulationMode.HasFlag(ManipulationModes.Translate))
{
newOrigin = new Point((position1.X + position2.X) / 2d, (position1.Y + position2.Y) / 2d);
translation = newOrigin - oldOrigin;
}
if (ManipulationModes.HasFlag(ManipulationModes.Rotate))
if (ManipulationMode.HasFlag(ManipulationModes.Rotate))
{
rotation = 180d / Math.PI
* (Math.Atan2(newDistance.Y, newDistance.X) - Math.Atan2(oldDistance.Y, oldDistance.X));
}
if (ManipulationModes.HasFlag(ManipulationModes.Scale))
if (ManipulationMode.HasFlag(ManipulationModes.Scale))
{
scale = newDistance.Length / oldDistance.Length;
}

68
MapControl/Shared/Map.cs Normal file
View file

@ -0,0 +1,68 @@
using System;
#if WPF
using System.Windows;
#elif UWP
using Windows.UI.Xaml;
#elif WINUI
using Microsoft.UI.Xaml;
#endif
namespace MapControl
{
/// <summary>
/// MapBase with default input event handling.
/// </summary>
public partial class Map : MapBase
{
public static readonly DependencyProperty MouseWheelZoomDeltaProperty =
DependencyPropertyHelper.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25);
public static readonly DependencyProperty MouseWheelZoomAnimatedProperty =
DependencyPropertyHelper.Register<Map, bool>(nameof(MouseWheelZoomAnimated), true);
/// <summary>
/// Gets or sets the amount by which the ZoomLevel property changes by a MouseWheel event.
/// The default value is 0.25.
/// </summary>
public double MouseWheelZoomDelta
{
get => (double)GetValue(MouseWheelZoomDeltaProperty);
set => SetValue(MouseWheelZoomDeltaProperty, value);
}
/// <summary>
/// Gets or sets a value that specifies whether zooming by a MouseWheel event is animated.
/// The default value is true.
/// </summary>
public bool MouseWheelZoomAnimated
{
get => (bool)GetValue(MouseWheelZoomAnimatedProperty);
set => SetValue(MouseWheelZoomAnimatedProperty, value);
}
private void OnMouseWheel(Point position, int delta)
{
// Standard mouse wheel delta value is 120.
//
OnMouseWheel(position, delta / 120d);
}
private void OnMouseWheel(Point position, double delta)
{
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * delta;
var animated = false;
if (delta % 1d == 0d)
{
// Zoom to integer multiple of MouseWheelZoomDelta when delta is an integer value,
// i.e. when the event was actually raised by a mouse wheel and not by a touch pad
// or a similar device with higher resolution.
//
zoomLevel = MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta);
animated = MouseWheelZoomAnimated;
}
ZoomMap(position, zoomLevel, animated);
}
}
}

View file

@ -101,6 +101,9 @@
<Compile Include="..\Shared\LocationCollection.cs">
<Link>LocationCollection.cs</Link>
</Compile>
<Compile Include="..\Shared\Map.cs">
<Link>Map.cs</Link>
</Compile>
<Compile Include="..\Shared\MapBase.cs">
<Link>MapBase.cs</Link>
</Compile>

View file

@ -1,30 +1,18 @@
using System;
using System.Windows;
using System.Windows;
using System.Windows.Input;
namespace MapControl
{
/// <summary>
/// MapBase with default input event handling.
/// </summary>
public class Map : MapBase
public partial class Map
{
public static readonly DependencyProperty ManipulationModeProperty =
DependencyPropertyHelper.Register<Map, ManipulationModes>(nameof(ManipulationMode), ManipulationModes.Translate | ManipulationModes.Scale);
public static readonly DependencyProperty MouseWheelZoomDeltaProperty =
DependencyPropertyHelper.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25);
public static readonly DependencyProperty MouseWheelZoomAnimatedProperty =
DependencyPropertyHelper.Register<Map, bool>(nameof(MouseWheelZoomAnimated), true);
private Point? mousePosition;
static Map()
{
IsManipulationEnabledProperty.OverrideMetadata(typeof(Map), new FrameworkPropertyMetadata(true));
}
public static readonly DependencyProperty ManipulationModeProperty =
DependencyPropertyHelper.Register<Map, ManipulationModes>(nameof(ManipulationMode), ManipulationModes.Translate | ManipulationModes.Scale);
/// <summary>
/// Gets or sets a value that specifies how the map control handles manipulations.
/// </summary>
@ -34,42 +22,11 @@ namespace MapControl
set => SetValue(ManipulationModeProperty, value);
}
/// <summary>
/// Gets or sets the amount by which the ZoomLevel property changes by a MouseWheel event.
/// The default value is 0.25.
/// </summary>
public double MouseWheelZoomDelta
{
get => (double)GetValue(MouseWheelZoomDeltaProperty);
set => SetValue(MouseWheelZoomDeltaProperty, value);
}
/// <summary>
/// Gets or sets a value that controls whether zooming by a MouseWheel event is animated.
/// The default value is true.
/// </summary>
public bool MouseWheelZoomAnimated
{
get => (bool)GetValue(MouseWheelZoomAnimatedProperty);
set => SetValue(MouseWheelZoomAnimatedProperty, value);
}
private Point? mousePosition;
protected override void OnMouseWheel(MouseWheelEventArgs e)
{
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * e.Delta / 120d;
var animated = false;
if (e.Delta % 120 == 0)
{
// Zoom to integer multiple of MouseWheelZoomDelta when delta is a multiple of 120,
// i.e. when the event was actually raised by a mouse wheel and not by a touch pad
// or a similar device with higher resolution.
//
zoomLevel = MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta);
animated = MouseWheelZoomAnimated;
}
ZoomMap(e.GetPosition(this), zoomLevel, animated);
OnMouseWheel(e.GetPosition(this), e.Delta);
base.OnMouseWheel(e);
}

View file

@ -1,30 +1,16 @@
using System;
using Windows.System;
using Windows.System;
#if UWP
using Windows.Devices.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input;
#else
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Input;
#endif
namespace MapControl
{
/// <summary>
/// MapBase with default input event handling.
/// </summary>
public class Map : MapBase
public partial class Map
{
public static readonly DependencyProperty MouseWheelZoomDeltaProperty =
DependencyPropertyHelper.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25);
public static readonly DependencyProperty MouseWheelZoomAnimatedProperty =
DependencyPropertyHelper.Register<Map, bool>(nameof(MouseWheelZoomAnimated), true);
private bool? manipulationEnabled;
public Map()
{
ManipulationMode
@ -40,46 +26,15 @@ namespace MapControl
ManipulationCompleted += OnManipulationCompleted;
}
/// <summary>
/// Gets or sets the amount by which the ZoomLevel property changes by a PointerWheelChanged event.
/// The default value is 0.25.
/// </summary>
public double MouseWheelZoomDelta
{
get => (double)GetValue(MouseWheelZoomDeltaProperty);
set => SetValue(MouseWheelZoomDeltaProperty, value);
}
/// <summary>
/// Gets or sets a value that controls whether zooming by a PointerWheelChanged event is animated.
/// The default value is true.
/// </summary>
public bool MouseWheelZoomAnimated
{
get => (bool)GetValue(MouseWheelZoomAnimatedProperty);
set => SetValue(MouseWheelZoomAnimatedProperty, value);
}
private bool? manipulationEnabled;
private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{
var point = e.GetCurrentPoint(this);
var delta = point.Properties.MouseWheelDelta;
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * delta / 120d;
var animated = false;
if (delta % 120 == 0)
{
// Zoom to integer multiple of MouseWheelZoomDelta when delta is a multiple of 120,
// i.e. when the event was actually raised by a mouse wheel and not by a touch pad
// or a similar device with higher resolution.
//
zoomLevel = MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta);
animated = MouseWheelZoomAnimated;
}
ZoomMap(point.Position, zoomLevel, animated);
OnMouseWheel(point.Position, point.Properties.MouseWheelDelta);
}
}

View file

@ -13,7 +13,7 @@
<map:Map x:Name="map"
ZoomLevel="11" MinZoomLevel="3"
Center="53.5,8.2"
ManipulationModes="All"
ManipulationMode="All"
DoubleTapped="MapDoubleTapped"
PointerPressed="MapPointerPressed"
PointerReleased="MapPointerReleased"
@ -188,7 +188,7 @@
MinZoomLevel="9" MaxZoomLevel="18"/>
</tools:MapOverlayMenuItem>
<tools:MapOverlayMenuItem Text="Graticule">
<map:MapGraticule Opacity="0.7"/>
<map:MapGraticule Opacity="0.7" StrokeThickness="0.3"/>
</tools:MapOverlayMenuItem>
<tools:MapOverlayMenuItem Text="Scale">
<map:MapScale HorizontalAlignment="Center" VerticalAlignment="Bottom"/>