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

120 lines
4 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2023-01-03 15:12:53 +01:00
// Copyright © 2023 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
2021-01-07 19:27:25 +01:00
using System;
using System.Windows;
using System.Windows.Input;
namespace MapControl
{
2020-05-13 00:03:50 +02:00
/// <summary>
/// MapBase with default input event handling.
/// </summary>
public class Map : MapBase
{
2020-05-13 00:03:50 +02:00
public static readonly DependencyProperty MouseWheelZoomDeltaProperty = DependencyProperty.Register(
2022-11-12 01:14:07 +01:00
nameof(MouseWheelZoomDelta), typeof(double), typeof(Map), new PropertyMetadata(0.25));
2020-05-13 00:03:50 +02:00
public static readonly DependencyProperty ManipulationModeProperty = DependencyProperty.Register(
nameof(ManipulationMode), typeof(ManipulationModes), typeof(Map),
new PropertyMetadata(ManipulationModes.Scale | ManipulationModes.Translate));
private Point? mousePosition;
2022-11-12 01:14:07 +01:00
private double mouseWheelDelta;
static Map()
{
IsManipulationEnabledProperty.OverrideMetadata(typeof(Map), new FrameworkPropertyMetadata(true));
}
public Map()
{
ManipulationStarted += OnManipulationStarted;
ManipulationDelta += OnManipulationDelta;
MouseLeftButtonDown += OnMouseLeftButtonDown;
MouseLeftButtonUp += OnMouseLeftButtonUp;
MouseMove += OnMouseMove;
MouseWheel += OnMouseWheel;
}
/// <summary>
2022-11-12 01:14:07 +01:00
/// Gets or sets the amount by which the ZoomLevel property changes by a MouseWheel event.
/// The default value is 0.25.
/// </summary>
2020-05-13 00:03:50 +02:00
public double MouseWheelZoomDelta
{
2022-08-06 10:40:59 +02:00
get => (double)GetValue(MouseWheelZoomDeltaProperty);
set => SetValue(MouseWheelZoomDeltaProperty, value);
}
/// <summary>
2020-05-13 00:03:50 +02:00
/// Gets or sets a value that specifies how the map control handles manipulations.
/// </summary>
2020-05-13 00:03:50 +02:00
public ManipulationModes ManipulationMode
{
2022-08-06 10:40:59 +02:00
get => (ManipulationModes)GetValue(ManipulationModeProperty);
set => SetValue(ManipulationModeProperty, value);
}
private void OnManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
Manipulation.SetManipulationMode(this, ManipulationMode);
}
2020-05-13 00:03:50 +02:00
private void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
2020-05-13 00:03:50 +02:00
TransformMap(e.ManipulationOrigin,
(Point)e.DeltaManipulation.Translation,
2020-05-13 00:03:50 +02:00
e.DeltaManipulation.Rotation,
e.DeltaManipulation.Scale.LengthSquared / 2d);
}
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (CaptureMouse())
{
mousePosition = e.GetPosition(this);
}
}
private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (mousePosition.HasValue)
{
mousePosition = null;
ReleaseMouseCapture();
}
}
2020-05-13 00:03:50 +02:00
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (mousePosition.HasValue)
{
var position = e.GetPosition(this);
2020-05-13 00:03:50 +02:00
var translation = position - mousePosition.Value;
mousePosition = position;
TranslateMap((Point)translation);
}
}
private void OnMouseWheel(object sender, MouseWheelEventArgs e)
{
2022-11-30 22:18:45 +01:00
// Standard mouse wheel delta value is 120.
//
mouseWheelDelta += e.Delta / 120d;
2022-11-12 01:14:07 +01:00
if (Math.Abs(mouseWheelDelta) >= 1d)
{
// Zoom to integer multiple of MouseWheelZoomDelta.
ZoomMap(e.GetPosition(this),
MouseWheelZoomDelta * Math.Round(TargetZoomLevel / MouseWheelZoomDelta + mouseWheelDelta));
mouseWheelDelta = 0d;
}
}
}
}