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

108 lines
3.6 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2020 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(
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;
}
/// <summary>
2020-05-13 00:03:50 +02:00
/// Gets or sets the amount by which the ZoomLevel property changes during a MouseWheel event.
/// The default value is 1.
/// </summary>
2020-05-13 00:03:50 +02:00
public double MouseWheelZoomDelta
{
2020-05-13 00:03:50 +02:00
get { return (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
{
2020-05-13 00:03:50 +02:00
get { return (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,
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();
}
}
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;
2020-05-13 00:03:50 +02:00
TranslateMap(translation);
}
}
private void OnMouseWheel(object sender, MouseWheelEventArgs e)
{
2021-01-07 19:27:25 +01:00
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * Math.Sign(e.Delta);
2021-01-07 19:27:25 +01:00
ZoomMap(e.GetPosition(this), MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta));
}
}
}