mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
124 lines
4 KiB
C#
124 lines
4 KiB
C#
using System;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
|
|
namespace MapControl
|
|
{
|
|
/// <summary>
|
|
/// 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 ManipulationModeProperty =
|
|
DependencyPropertyHelper.Register<Map, ManipulationModes>(nameof(ManipulationMode), ManipulationModes.Translate | ManipulationModes.Scale);
|
|
|
|
private Point? mousePosition;
|
|
private double mouseWheelDelta;
|
|
|
|
static Map()
|
|
{
|
|
IsManipulationEnabledProperty.OverrideMetadata(typeof(Map), new FrameworkPropertyMetadata(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 how the map control handles manipulations.
|
|
/// </summary>
|
|
public ManipulationModes ManipulationMode
|
|
{
|
|
get => (ManipulationModes)GetValue(ManipulationModeProperty);
|
|
set => SetValue(ManipulationModeProperty, value);
|
|
}
|
|
|
|
protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
|
|
{
|
|
base.OnManipulationStarted(e);
|
|
|
|
Manipulation.SetManipulationMode(this, ManipulationMode);
|
|
}
|
|
|
|
protected override void OnManipulationDelta(ManipulationDeltaEventArgs e)
|
|
{
|
|
base.OnManipulationDelta(e);
|
|
|
|
TransformMap(e.ManipulationOrigin,
|
|
(Point)e.DeltaManipulation.Translation,
|
|
e.DeltaManipulation.Rotation,
|
|
e.DeltaManipulation.Scale.LengthSquared / 2d);
|
|
}
|
|
|
|
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
|
|
{
|
|
base.OnMouseLeftButtonDown(e);
|
|
|
|
if (Keyboard.Modifiers == ModifierKeys.None &&
|
|
CaptureMouse())
|
|
{
|
|
mousePosition = e.GetPosition(this);
|
|
}
|
|
}
|
|
|
|
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
|
|
{
|
|
base.OnMouseLeftButtonUp(e);
|
|
|
|
if (mousePosition.HasValue)
|
|
{
|
|
mousePosition = null;
|
|
ReleaseMouseCapture();
|
|
}
|
|
}
|
|
|
|
protected override void OnMouseMove(MouseEventArgs e)
|
|
{
|
|
base.OnMouseMove(e);
|
|
|
|
if (mousePosition.HasValue)
|
|
{
|
|
var p = e.GetPosition(this);
|
|
TranslateMap(new Point(p.X - mousePosition.Value.X, p.Y - mousePosition.Value.Y));
|
|
mousePosition = p;
|
|
}
|
|
else if (e.LeftButton == MouseButtonState.Pressed &&
|
|
Keyboard.Modifiers == ModifierKeys.None &&
|
|
CaptureMouse())
|
|
{
|
|
// Set mousePosition when no MouseLeftButtonDown event was received.
|
|
//
|
|
mousePosition = e.GetPosition(this);
|
|
}
|
|
}
|
|
|
|
protected override void OnMouseWheel(MouseWheelEventArgs e)
|
|
{
|
|
base.OnMouseWheel(e);
|
|
|
|
// Standard mouse wheel delta value is 120.
|
|
//
|
|
mouseWheelDelta += e.Delta / 120d;
|
|
|
|
if (Math.Abs(mouseWheelDelta) >= 1d)
|
|
{
|
|
// Zoom to integer multiple of MouseWheelZoomDelta.
|
|
//
|
|
ZoomMap(e.GetPosition(this),
|
|
MouseWheelZoomDelta * Math.Round(TargetZoomLevel / MouseWheelZoomDelta + mouseWheelDelta));
|
|
|
|
mouseWheelDelta = 0d;
|
|
}
|
|
}
|
|
}
|
|
}
|