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

62 lines
1.9 KiB
C#
Raw Normal View History

2025-06-12 07:41:01 +02:00
using System;
#if WPF
using System.Windows;
#elif UWP
using Windows.UI.Xaml;
#elif WINUI
using Microsoft.UI.Xaml;
2025-08-19 19:43:02 +02:00
#elif AVALONIA
using Avalonia;
2025-06-12 07:41:01 +02:00
#endif
2026-04-13 17:14:49 +02:00
namespace MapControl;
/// <summary>
/// MapBase with default input event handling.
/// </summary>
public partial class Map : MapBase
2025-06-12 07:41:01 +02:00
{
2026-04-13 17:14:49 +02:00
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);
2025-06-12 07:41:01 +02:00
/// <summary>
2026-04-13 17:14:49 +02:00
/// Gets or sets the amount by which the ZoomLevel property changes by a MouseWheel event.
/// The default value is 0.25.
2025-06-12 07:41:01 +02:00
/// </summary>
2026-04-13 17:14:49 +02:00
public double MouseWheelZoomDelta
2025-06-12 07:41:01 +02:00
{
2026-04-13 17:14:49 +02:00
get => (double)GetValue(MouseWheelZoomDeltaProperty);
set => SetValue(MouseWheelZoomDeltaProperty, value);
}
2025-06-12 07:41:01 +02:00
2026-04-13 17:14:49 +02:00
/// <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);
}
2025-06-12 07:41:01 +02:00
2026-04-13 17:14:49 +02:00
private void OnMouseWheel(Point position, double delta)
{
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * delta;
var animated = false;
2025-06-12 07:41:01 +02:00
2026-04-13 17:14:49 +02:00
if (delta <= -1d || delta >= 1d)
2025-06-12 07:41:01 +02:00
{
2026-04-13 17:14:49 +02:00
// Zoom to integer multiple of MouseWheelZoomDelta when the event was raised by a
// mouse wheel or by a large movement on a touch pad or other high resolution device.
//
zoomLevel = MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta);
animated = MouseWheelZoomAnimated;
2025-06-12 07:41:01 +02:00
}
2026-04-13 17:14:49 +02:00
ZoomMap(position, zoomLevel, animated);
2025-06-12 07:41:01 +02:00
}
}