mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
108 lines
3.6 KiB
C#
108 lines
3.6 KiB
C#
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
// © 2021 Clemens Fischer
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
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 = 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>
|
|
/// Gets or sets the amount by which the ZoomLevel property changes during a MouseWheel event.
|
|
/// The default value is 1.
|
|
/// </summary>
|
|
public double MouseWheelZoomDelta
|
|
{
|
|
get { return (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 { 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));
|
|
}
|
|
}
|
|
}
|