// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2022 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Windows;
using System.Windows.Input;
namespace MapControl
{
///
/// MapBase with default input event handling.
///
public class Map : MapBase
{
public static readonly DependencyProperty MouseWheelZoomDeltaProperty = DependencyProperty.Register(
nameof(MouseWheelZoomDelta), typeof(double), typeof(Map), new PropertyMetadata(1d));
public static readonly DependencyProperty ManipulationModeProperty = DependencyProperty.Register(
nameof(ManipulationMode), typeof(ManipulationModes), typeof(Map), new PropertyMetadata(ManipulationModes.All));
private Point? mousePosition;
static Map()
{
IsManipulationEnabledProperty.OverrideMetadata(typeof(Map), new FrameworkPropertyMetadata(true));
}
public Map()
{
ManipulationStarted += OnManipulationStarted;
ManipulationDelta += OnManipulationDelta;
MouseLeftButtonDown += OnMouseLeftButtonDown;
MouseLeftButtonUp += OnMouseLeftButtonUp;
MouseMove += OnMouseMove;
MouseWheel += OnMouseWheel;
}
///
/// Gets or sets the amount by which the ZoomLevel property changes during a MouseWheel event.
/// The default value is 1.
///
public double MouseWheelZoomDelta
{
get { return (double)GetValue(MouseWheelZoomDeltaProperty); }
set { SetValue(MouseWheelZoomDeltaProperty, value); }
}
///
/// Gets or sets a value that specifies how the map control handles manipulations.
///
public ManipulationModes ManipulationMode
{
get { return (ManipulationModes)GetValue(ManipulationModeProperty); }
set { SetValue(ManipulationModeProperty, value); }
}
private void OnManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
Manipulation.SetManipulationMode(this, ManipulationMode);
}
private void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
TransformMap(e.ManipulationOrigin,
e.DeltaManipulation.Translation,
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();
}
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (mousePosition.HasValue)
{
var position = e.GetPosition(this);
var translation = position - mousePosition.Value;
mousePosition = position;
TranslateMap(translation);
}
}
private void OnMouseWheel(object sender, MouseWheelEventArgs e)
{
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * Math.Sign(e.Delta);
ZoomMap(e.GetPosition(this), MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta));
}
}
}