XAML-Map-Control/MapControl/Avalonia/Map.Avalonia.cs

209 lines
6.3 KiB
C#
Raw Normal View History

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)
2024-05-22 21:20:49 +02:00
using System;
2024-05-19 23:23:27 +02:00
namespace MapControl
{
2024-05-28 22:16:29 +02:00
[Flags]
public enum ManipulationModes
{
None = 0,
Translate = 1,
Rotate = 2,
Scale = 4,
All = Translate | Rotate | Scale
}
2024-05-19 23:23:27 +02:00
/// <summary>
/// MapBase with default input event handling.
/// </summary>
public class Map : MapBase
{
2024-05-28 22:16:29 +02:00
public static readonly StyledProperty<ManipulationModes> ManipulationModesProperty =
DependencyPropertyHelper.Register<Map, ManipulationModes>(nameof(ManipulationModes), ManipulationModes.Translate | ManipulationModes.Scale);
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
2024-05-28 22:16:29 +02:00
public ManipulationModes ManipulationModes
{
get => GetValue(ManipulationModesProperty);
set => SetValue(ManipulationModesProperty, value);
}
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
ZoomMap(e.GetPosition(this), TargetZoomLevel + MouseWheelZoomDelta * e.Delta.Y);
2024-05-25 14:22:25 +02:00
e.Handled = true;
base.OnPointerWheelChanged(e);
2024-05-19 23:23:27 +02:00
}
2024-05-29 19:18:37 +02:00
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
var point = e.GetCurrentPoint(this);
if (point.Pointer.Type == PointerType.Mouse && HandleMousePressed(point) ||
point.Pointer.Type == PointerType.Touch && HandleTouchPressed(point))
{
point.Pointer.Capture(this);
e.Handled = true;
}
base.OnPointerPressed(e);
}
protected override void OnPointerReleased(PointerReleasedEventArgs e)
{
if (HandlePointerReleased(e.Pointer))
{
e.Pointer.Capture(null);
e.Handled = true;
}
base.OnPointerReleased(e);
}
protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e)
{
if (HandlePointerReleased(e.Pointer))
{
e.Handled = true;
}
base.OnPointerCaptureLost(e);
}
protected override void OnPointerMoved(PointerEventArgs e)
{
if (e.Pointer == pointer1 || e.Pointer == pointer2)
{
var position = e.GetPosition(this);
if (pointer2 != null)
{
HandleManipulation(e.Pointer, position);
}
else if (e.Pointer.Type == PointerType.Mouse || ManipulationModes.HasFlag(ManipulationModes.Translate))
{
TranslateMap(position - position1);
position1 = position;
}
e.Handled = true;
}
base.OnPointerMoved(e);
}
2024-05-28 22:16:29 +02:00
private bool HandleMousePressed(PointerPoint point)
2024-05-19 23:23:27 +02:00
{
2024-05-28 22:16:29 +02:00
var handled = pointer1 == null && point.Properties.IsLeftButtonPressed;
2024-05-29 19:18:37 +02:00
2024-05-28 22:16:29 +02:00
if (handled)
2024-05-19 23:23:27 +02:00
{
2024-05-28 22:16:29 +02:00
pointer1 = point.Pointer;
position1 = point.Position;
}
return handled;
}
private bool HandleTouchPressed(PointerPoint point)
{
var handled = pointer2 == null && ManipulationModes != ManipulationModes.None;
2024-05-22 21:20:49 +02:00
2024-05-28 22:16:29 +02:00
if (handled)
{
2024-05-22 21:20:49 +02:00
if (pointer1 == null)
{
pointer1 = point.Pointer;
position1 = point.Position;
}
else
{
pointer2 = point.Pointer;
position2 = point.Position;
}
2024-05-28 22:16:29 +02:00
}
return handled;
}
2024-05-29 19:18:37 +02:00
private bool HandlePointerReleased(IPointer pointer)
2024-05-28 22:16:29 +02:00
{
2024-05-29 19:18:37 +02:00
var handled = pointer == pointer1 || pointer == pointer2;
2024-05-25 14:22:25 +02:00
2024-05-29 19:18:37 +02:00
if (handled)
2024-05-19 23:23:27 +02:00
{
2024-05-29 19:18:37 +02:00
if (pointer == pointer1)
2024-05-22 21:20:49 +02:00
{
pointer1 = pointer2;
position1 = position2;
}
pointer2 = null;
2024-05-19 23:23:27 +02:00
}
2024-05-25 14:22:25 +02:00
2024-05-29 19:18:37 +02:00
return handled;
2024-05-19 23:23:27 +02:00
}
2024-05-29 19:18:37 +02:00
private void HandleManipulation(IPointer pointer, Point position)
2024-05-28 22:16:29 +02:00
{
2024-05-29 19:18:37 +02:00
var oldDistance = new Vector(position2.X - position1.X, position2.Y - position1.Y);
var oldOrigin = new Point((position1.X + position2.X) / 2d, (position1.Y + position2.Y) / 2d);
2024-05-28 22:16:29 +02:00
2024-05-29 19:18:37 +02:00
if (pointer == pointer1)
{
position1 = position;
}
else
{
position2 = position;
2024-05-28 22:16:29 +02:00
}
2024-05-29 19:18:37 +02:00
var newDistance = new Vector(position2.X - position1.X, position2.Y - position1.Y);
var newOrigin = oldOrigin;
var translation = new Point();
var rotation = 0d;
var scale = 1d;
2024-05-28 22:16:29 +02:00
2024-05-29 19:18:37 +02:00
if (ManipulationModes.HasFlag(ManipulationModes.Translate))
2024-05-19 23:23:27 +02:00
{
2024-05-29 19:18:37 +02:00
newOrigin = new Point((position1.X + position2.X) / 2d, (position1.Y + position2.Y) / 2d);
translation = newOrigin - oldOrigin;
}
2024-05-28 22:16:29 +02:00
2024-05-29 19:18:37 +02:00
if (ManipulationModes.HasFlag(ManipulationModes.Rotate))
{
rotation = 180d / Math.PI
* (Math.Atan2(newDistance.Y, newDistance.X) - Math.Atan2(oldDistance.Y, oldDistance.X));
}
2024-05-25 14:22:25 +02:00
2024-05-29 19:18:37 +02:00
if (ManipulationModes.HasFlag(ManipulationModes.Scale))
{
scale = newDistance.Length / oldDistance.Length;
2024-05-19 23:23:27 +02:00
}
2024-05-25 14:22:25 +02:00
2024-05-29 19:18:37 +02:00
TransformMap(newOrigin, translation, rotation, scale);
2024-05-19 23:23:27 +02:00
}
}
}