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 All = Translate | Rotate | Scale
} }
/// <summary> public partial class Map
/// MapBase with default input event handling.
/// </summary>
public class Map : MapBase
{ {
public static readonly StyledProperty<ManipulationModes> ManipulationModesProperty = public static readonly StyledProperty<ManipulationModes> ManipulationModeProperty =
DependencyPropertyHelper.Register<Map, ManipulationModes>(nameof(ManipulationModes), ManipulationModes.Translate | ManipulationModes.Scale); DependencyPropertyHelper.Register<Map, ManipulationModes>(nameof(ManipulationMode), ManipulationModes.Translate | ManipulationModes.Scale);
public static readonly StyledProperty<double> MouseWheelZoomDeltaProperty = /// <summary>
DependencyPropertyHelper.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25); /// Gets or sets a value that specifies how the map control handles manipulations.
/// </summary>
public static readonly DependencyProperty MouseWheelZoomAnimatedProperty = public ManipulationModes ManipulationMode
DependencyPropertyHelper.Register<Map, bool>(nameof(MouseWheelZoomAnimated), true); {
get => GetValue(ManipulationModeProperty);
set => SetValue(ManipulationModeProperty, value);
}
private IPointer pointer1; private IPointer pointer1;
private IPointer pointer2; private IPointer pointer2;
private Point position1; private Point position1;
private Point position2; 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) protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
{ {
var delta = e.Delta.Y; OnMouseWheel(e.GetPosition(this), 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);
base.OnPointerWheelChanged(e); base.OnPointerWheelChanged(e);
} }
@ -89,7 +49,7 @@ namespace MapControl
HandleManipulation(point.Pointer, point.Position); HandleManipulation(point.Pointer, point.Position);
} }
else if (point.Pointer.Type == PointerType.Mouse || 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)); TranslateMap(new Point(point.Position.X - position1.X, point.Position.Y - position1.Y));
position1 = point.Position; position1 = point.Position;
@ -101,7 +61,7 @@ namespace MapControl
e.KeyModifiers == KeyModifiers.None || e.KeyModifiers == KeyModifiers.None ||
pointer2 == null && pointer2 == null &&
point.Pointer.Type == PointerType.Touch && point.Pointer.Type == PointerType.Touch &&
ManipulationModes != ManipulationModes.None) ManipulationMode != ManipulationModes.None)
{ {
point.Pointer.Capture(this); point.Pointer.Capture(this);
@ -156,19 +116,19 @@ namespace MapControl
var rotation = 0d; var rotation = 0d;
var scale = 1d; 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); newOrigin = new Point((position1.X + position2.X) / 2d, (position1.Y + position2.Y) / 2d);
translation = newOrigin - oldOrigin; translation = newOrigin - oldOrigin;
} }
if (ManipulationModes.HasFlag(ManipulationModes.Rotate)) if (ManipulationMode.HasFlag(ManipulationModes.Rotate))
{ {
rotation = 180d / Math.PI rotation = 180d / Math.PI
* (Math.Atan2(newDistance.Y, newDistance.X) - Math.Atan2(oldDistance.Y, oldDistance.X)); * (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; 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"> <Compile Include="..\Shared\LocationCollection.cs">
<Link>LocationCollection.cs</Link> <Link>LocationCollection.cs</Link>
</Compile> </Compile>
<Compile Include="..\Shared\Map.cs">
<Link>Map.cs</Link>
</Compile>
<Compile Include="..\Shared\MapBase.cs"> <Compile Include="..\Shared\MapBase.cs">
<Link>MapBase.cs</Link> <Link>MapBase.cs</Link>
</Compile> </Compile>

View file

@ -1,30 +1,18 @@
using System; using System.Windows;
using System.Windows;
using System.Windows.Input; using System.Windows.Input;
namespace MapControl namespace MapControl
{ {
/// <summary> public partial class Map
/// MapBase with default input event handling.
/// </summary>
public class Map : MapBase
{ {
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() static Map()
{ {
IsManipulationEnabledProperty.OverrideMetadata(typeof(Map), new FrameworkPropertyMetadata(true)); IsManipulationEnabledProperty.OverrideMetadata(typeof(Map), new FrameworkPropertyMetadata(true));
} }
public static readonly DependencyProperty ManipulationModeProperty =
DependencyPropertyHelper.Register<Map, ManipulationModes>(nameof(ManipulationMode), ManipulationModes.Translate | ManipulationModes.Scale);
/// <summary> /// <summary>
/// Gets or sets a value that specifies how the map control handles manipulations. /// Gets or sets a value that specifies how the map control handles manipulations.
/// </summary> /// </summary>
@ -34,42 +22,11 @@ namespace MapControl
set => SetValue(ManipulationModeProperty, value); set => SetValue(ManipulationModeProperty, value);
} }
/// <summary> private Point? mousePosition;
/// 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);
}
protected override void OnMouseWheel(MouseWheelEventArgs e) protected override void OnMouseWheel(MouseWheelEventArgs e)
{ {
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * e.Delta / 120d; OnMouseWheel(e.GetPosition(this), e.Delta);
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);
base.OnMouseWheel(e); base.OnMouseWheel(e);
} }

View file

@ -1,30 +1,16 @@
using System; using Windows.System;
using Windows.System;
#if UWP #if UWP
using Windows.Devices.Input; using Windows.Devices.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Input;
#else #else
using Microsoft.UI.Input; using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Input; using Microsoft.UI.Xaml.Input;
#endif #endif
namespace MapControl namespace MapControl
{ {
/// <summary> public partial class Map
/// MapBase with default input event handling.
/// </summary>
public 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);
private bool? manipulationEnabled;
public Map() public Map()
{ {
ManipulationMode ManipulationMode
@ -40,46 +26,15 @@ namespace MapControl
ManipulationCompleted += OnManipulationCompleted; ManipulationCompleted += OnManipulationCompleted;
} }
/// <summary> private bool? manipulationEnabled;
/// 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 void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e) private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
{ {
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse) if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{ {
var point = e.GetCurrentPoint(this); var point = e.GetCurrentPoint(this);
var delta = point.Properties.MouseWheelDelta;
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * delta / 120d;
var animated = false;
if (delta % 120 == 0) OnMouseWheel(point.Position, point.Properties.MouseWheelDelta);
{
// 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);
} }
} }

View file

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