2024-05-19 23:23:27 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
|
|
|
|
// Copyright © 2024 Clemens Fischer
|
|
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using Avalonia.Input;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// MapBase with default input event handling.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class Map : MapBase
|
|
|
|
|
|
{
|
|
|
|
|
|
public static readonly StyledProperty<double> MouseWheelZoomDeltaProperty
|
|
|
|
|
|
= AvaloniaProperty.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25);
|
|
|
|
|
|
|
|
|
|
|
|
private Point? mousePosition;
|
|
|
|
|
|
|
|
|
|
|
|
/// <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 => GetValue(MouseWheelZoomDeltaProperty);
|
|
|
|
|
|
set => SetValue(MouseWheelZoomDeltaProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
|
2024-05-19 23:23:27 +02:00
|
|
|
|
{
|
2024-05-20 23:24:34 +02:00
|
|
|
|
base.OnPointerWheelChanged(e);
|
2024-05-19 23:23:27 +02:00
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
ZoomMap(e.GetPosition(this), TargetZoomLevel + MouseWheelZoomDelta * e.Delta.Y);
|
2024-05-19 23:23:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
protected override void OnPointerPressed(PointerPressedEventArgs e)
|
2024-05-19 23:23:27 +02:00
|
|
|
|
{
|
2024-05-20 23:24:34 +02:00
|
|
|
|
base.OnPointerPressed(e);
|
|
|
|
|
|
|
2024-05-19 23:23:27 +02:00
|
|
|
|
var point = e.GetCurrentPoint(this);
|
|
|
|
|
|
|
|
|
|
|
|
if (point.Properties.IsLeftButtonPressed)
|
|
|
|
|
|
{
|
|
|
|
|
|
e.Pointer.Capture(this);
|
|
|
|
|
|
mousePosition = point.Position;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
protected override void OnPointerReleased(PointerReleasedEventArgs e)
|
2024-05-19 23:23:27 +02:00
|
|
|
|
{
|
2024-05-20 23:24:34 +02:00
|
|
|
|
base.OnPointerReleased(e);
|
|
|
|
|
|
|
2024-05-19 23:23:27 +02:00
|
|
|
|
if (mousePosition.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
e.Pointer.Capture(null);
|
|
|
|
|
|
mousePosition = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
protected override void OnPointerMoved(PointerEventArgs e)
|
2024-05-19 23:23:27 +02:00
|
|
|
|
{
|
2024-05-20 23:24:34 +02:00
|
|
|
|
base.OnPointerMoved(e);
|
|
|
|
|
|
|
2024-05-19 23:23:27 +02:00
|
|
|
|
if (mousePosition.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
var position = e.GetPosition(this);
|
|
|
|
|
|
TranslateMap(position - mousePosition.Value);
|
|
|
|
|
|
mousePosition = position;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|