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;
|
2024-05-22 21:20:49 +02:00
|
|
|
|
using System;
|
2024-05-19 23:23:27 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// MapBase with default input event handling.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class Map : MapBase
|
|
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
public static readonly StyledProperty<double> MouseWheelZoomDeltaProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<Map, double>(nameof(MouseWheelZoomDelta), 0.25);
|
2024-05-19 23:23:27 +02:00
|
|
|
|
|
2024-05-22 21:20:49 +02:00
|
|
|
|
private IPointer pointer1;
|
|
|
|
|
|
private IPointer pointer2;
|
|
|
|
|
|
private Point position1;
|
|
|
|
|
|
private Point position2;
|
2024-05-19 23:23:27 +02:00
|
|
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
|
|
2024-05-22 21:20:49 +02:00
|
|
|
|
if (pointer2 == null &&
|
|
|
|
|
|
(point.Properties.IsLeftButtonPressed || point.Pointer.Type == PointerType.Touch))
|
2024-05-19 23:23:27 +02:00
|
|
|
|
{
|
2024-05-22 21:20:49 +02:00
|
|
|
|
point.Pointer.Capture(this);
|
|
|
|
|
|
|
|
|
|
|
|
if (pointer1 == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
pointer1 = point.Pointer;
|
|
|
|
|
|
position1 = point.Position;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
pointer2 = point.Pointer;
|
|
|
|
|
|
position2 = point.Position;
|
|
|
|
|
|
}
|
2024-05-19 23:23:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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-22 21:20:49 +02:00
|
|
|
|
if (e.Pointer == pointer1 || e.Pointer == pointer2)
|
2024-05-19 23:23:27 +02:00
|
|
|
|
{
|
|
|
|
|
|
e.Pointer.Capture(null);
|
2024-05-22 21:20:49 +02:00
|
|
|
|
|
|
|
|
|
|
if (e.Pointer == pointer1)
|
|
|
|
|
|
{
|
|
|
|
|
|
pointer1 = pointer2;
|
|
|
|
|
|
position1 = position2;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pointer2 = null;
|
2024-05-19 23:23:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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-22 21:20:49 +02:00
|
|
|
|
if (e.Pointer == pointer1 || e.Pointer == pointer2)
|
2024-05-19 23:23:27 +02:00
|
|
|
|
{
|
|
|
|
|
|
var position = e.GetPosition(this);
|
2024-05-22 21:20:49 +02:00
|
|
|
|
|
|
|
|
|
|
if (pointer2 == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
TranslateMap(position - position1);
|
|
|
|
|
|
position1 = position;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Point oldOrigin = new((position1.X + position2.X) / 2d, (position1.Y + position2.Y) / 2d);
|
|
|
|
|
|
Vector oldDistance = position2 - position1;
|
|
|
|
|
|
|
|
|
|
|
|
if (e.Pointer == pointer1)
|
|
|
|
|
|
{
|
|
|
|
|
|
position1 = position;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
position2 = position;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Point newOrigin = new((position1.X + position2.X) / 2d, (position1.Y + position2.Y) / 2d);
|
|
|
|
|
|
Vector newDistance = position2 - position1;
|
|
|
|
|
|
|
|
|
|
|
|
var oldAngle = Math.Atan2(oldDistance.Y, oldDistance.X) * 180d / Math.PI;
|
|
|
|
|
|
var newAngle = Math.Atan2(newDistance.Y, newDistance.X) * 180d / Math.PI;
|
|
|
|
|
|
var scale = newDistance.Length / oldDistance.Length;
|
|
|
|
|
|
|
|
|
|
|
|
TransformMap(newOrigin, newOrigin - oldOrigin, newAngle - oldAngle, scale);
|
|
|
|
|
|
}
|
2024-05-19 23:23:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|