2025-02-27 18:46:32 +01:00
|
|
|
|
using System;
|
2013-05-07 18:12:25 +02:00
|
|
|
|
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
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2025-06-12 00:31:01 +02:00
|
|
|
|
public static readonly DependencyProperty ManipulationModeProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<Map, ManipulationModes>(nameof(ManipulationMode), ManipulationModes.Translate | ManipulationModes.Scale);
|
|
|
|
|
|
|
2024-05-23 18:08:14 +02:00
|
|
|
|
public static readonly DependencyProperty MouseWheelZoomDeltaProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25);
|
2020-05-13 00:03:50 +02:00
|
|
|
|
|
2025-06-12 00:31:01 +02:00
|
|
|
|
public static readonly DependencyProperty MouseWheelZoomAnimatedProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<Map, bool>(nameof(MouseWheelZoomAnimated), true);
|
2013-05-08 17:05:17 +02:00
|
|
|
|
|
2025-01-06 15:33:12 +01:00
|
|
|
|
private Point? mousePosition;
|
2013-05-07 18:12:25 +02:00
|
|
|
|
|
2013-05-08 17:05:17 +02:00
|
|
|
|
static Map()
|
|
|
|
|
|
{
|
|
|
|
|
|
IsManipulationEnabledProperty.OverrideMetadata(typeof(Map), new FrameworkPropertyMetadata(true));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-12 00:31:01 +02:00
|
|
|
|
/// <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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-05-08 17:05:17 +02:00
|
|
|
|
/// <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.
|
2013-05-08 17:05:17 +02:00
|
|
|
|
/// </summary>
|
2020-05-13 00:03:50 +02:00
|
|
|
|
public double MouseWheelZoomDelta
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => (double)GetValue(MouseWheelZoomDeltaProperty);
|
|
|
|
|
|
set => SetValue(MouseWheelZoomDeltaProperty, value);
|
2013-05-07 18:12:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-06-12 00:31:01 +02:00
|
|
|
|
/// Gets or sets a value that controls whether zooming by a MouseWheel event is animated.
|
|
|
|
|
|
/// The default value is true.
|
2013-05-07 18:12:25 +02:00
|
|
|
|
/// </summary>
|
2025-06-12 00:31:01 +02:00
|
|
|
|
public bool MouseWheelZoomAnimated
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2025-06-12 00:31:01 +02:00
|
|
|
|
get => (bool)GetValue(MouseWheelZoomAnimatedProperty);
|
|
|
|
|
|
set => SetValue(MouseWheelZoomAnimatedProperty, value);
|
2020-01-17 19:51:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-12 00:31:01 +02:00
|
|
|
|
protected override void OnMouseWheel(MouseWheelEventArgs e)
|
2020-01-17 19:51:56 +01:00
|
|
|
|
{
|
2025-06-12 00:31:01 +02:00
|
|
|
|
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * e.Delta / 120d;
|
|
|
|
|
|
var animated = false;
|
2025-03-19 11:14:13 +01:00
|
|
|
|
|
2025-06-12 00:31:01 +02:00
|
|
|
|
if (e.Delta % 120 == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Zoom to integer multiple of MouseWheelZoomDelta when delta is a multiple of 120,
|
|
|
|
|
|
// i.e. when the event was actually raised by a mouse wheel and not by a touch pad
|
|
|
|
|
|
// or a similar device with higher resolution.
|
|
|
|
|
|
//
|
|
|
|
|
|
zoomLevel = MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta);
|
|
|
|
|
|
animated = MouseWheelZoomAnimated;
|
|
|
|
|
|
}
|
2013-05-07 18:12:25 +02:00
|
|
|
|
|
2025-06-12 00:31:01 +02:00
|
|
|
|
ZoomMap(e.GetPosition(this), zoomLevel, animated);
|
2025-03-19 11:14:13 +01:00
|
|
|
|
|
2025-06-12 00:31:01 +02:00
|
|
|
|
base.OnMouseWheel(e);
|
2020-01-17 19:51:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-19 11:14:13 +01:00
|
|
|
|
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
|
2020-01-17 19:51:56 +01:00
|
|
|
|
{
|
2025-03-19 11:14:13 +01:00
|
|
|
|
if (Keyboard.Modifiers == ModifierKeys.None &&
|
|
|
|
|
|
CaptureMouse())
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2025-01-06 15:33:12 +01:00
|
|
|
|
mousePosition = e.GetPosition(this);
|
2013-05-07 18:12:25 +02:00
|
|
|
|
}
|
2025-06-12 00:31:01 +02:00
|
|
|
|
|
|
|
|
|
|
base.OnMouseLeftButtonDown(e);
|
2013-05-07 18:12:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-19 11:14:13 +01:00
|
|
|
|
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2025-01-06 15:33:12 +01:00
|
|
|
|
if (mousePosition.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
mousePosition = null;
|
|
|
|
|
|
ReleaseMouseCapture();
|
|
|
|
|
|
}
|
2025-06-12 00:31:01 +02:00
|
|
|
|
|
|
|
|
|
|
base.OnMouseLeftButtonUp(e);
|
2013-05-07 18:12:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-19 11:14:13 +01:00
|
|
|
|
protected override void OnMouseMove(MouseEventArgs e)
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2025-01-06 15:33:12 +01:00
|
|
|
|
if (mousePosition.HasValue)
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2025-01-06 15:33:12 +01:00
|
|
|
|
var p = e.GetPosition(this);
|
|
|
|
|
|
TranslateMap(new Point(p.X - mousePosition.Value.X, p.Y - mousePosition.Value.Y));
|
|
|
|
|
|
mousePosition = p;
|
2020-01-17 19:51:56 +01:00
|
|
|
|
}
|
2025-03-19 11:14:13 +01:00
|
|
|
|
else if (e.LeftButton == MouseButtonState.Pressed &&
|
|
|
|
|
|
Keyboard.Modifiers == ModifierKeys.None &&
|
|
|
|
|
|
CaptureMouse())
|
|
|
|
|
|
{
|
|
|
|
|
|
// Set mousePosition when no MouseLeftButtonDown event was received.
|
|
|
|
|
|
//
|
|
|
|
|
|
mousePosition = e.GetPosition(this);
|
|
|
|
|
|
}
|
2025-06-12 00:31:01 +02:00
|
|
|
|
|
|
|
|
|
|
base.OnMouseMove(e);
|
2013-05-08 17:05:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-12 00:31:01 +02:00
|
|
|
|
protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2025-06-12 00:31:01 +02:00
|
|
|
|
Manipulation.SetManipulationMode(this, ManipulationMode);
|
2025-03-19 11:14:13 +01:00
|
|
|
|
|
2025-06-12 00:31:01 +02:00
|
|
|
|
base.OnManipulationStarted(e);
|
|
|
|
|
|
}
|
2013-05-07 18:12:25 +02:00
|
|
|
|
|
2025-06-12 00:31:01 +02:00
|
|
|
|
protected override void OnManipulationDelta(ManipulationDeltaEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
TransformMap(e.ManipulationOrigin,
|
|
|
|
|
|
(Point)e.DeltaManipulation.Translation,
|
|
|
|
|
|
e.DeltaManipulation.Rotation,
|
|
|
|
|
|
e.DeltaManipulation.Scale.LengthSquared / 2d);
|
2022-11-12 01:14:07 +01:00
|
|
|
|
|
2025-06-12 00:31:01 +02:00
|
|
|
|
base.OnManipulationDelta(e);
|
2013-05-07 18:12:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|