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

180 lines
6.2 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01: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
2025-06-12 00:31:01 +02:00
public static readonly DependencyProperty MouseWheelZoomAnimatedProperty =
DependencyPropertyHelper.Register<Map, bool>(nameof(MouseWheelZoomAnimated), true);
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>
2025-06-12 00:31:01 +02:00
/// Gets or sets the amount by which the ZoomLevel property changes by a PointerWheelChanged event.
2024-05-19 23:23:27 +02:00
/// The default value is 0.25.
/// </summary>
public double MouseWheelZoomDelta
{
get => GetValue(MouseWheelZoomDeltaProperty);
set => SetValue(MouseWheelZoomDeltaProperty, value);
}
2025-06-12 00:31:01 +02:00
/// <summary>
/// Gets or sets a value that controls whether zooming by a PointerWheelChanged event is animated.
/// The default value is true.
/// </summary>
public bool MouseWheelZoomAnimated
{
get => (bool)GetValue(MouseWheelZoomAnimatedProperty);
set => SetValue(MouseWheelZoomAnimatedProperty, value);
}
2024-05-20 23:24:34 +02:00
protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
2024-05-19 23:23:27 +02:00
{
2025-06-12 00:31:01 +02:00
var delta = e.Delta.Y;
var zoomLevel = TargetZoomLevel + MouseWheelZoomDelta * delta;
var animated = false;
2024-08-24 21:06:12 +02:00
2025-06-12 00:31:01 +02:00
if (delta % 1d == 0d)
2024-08-24 21:06:12 +02:00
{
2025-06-12 00:31:01 +02:00
// Zoom to integer multiple of MouseWheelZoomDelta when delta is an integer value,
// 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.
2024-08-24 21:06:12 +02:00
//
2025-06-12 00:31:01 +02:00
zoomLevel = MouseWheelZoomDelta * Math.Round(zoomLevel / MouseWheelZoomDelta);
animated = MouseWheelZoomAnimated;
2024-08-24 21:06:12 +02:00
}
2024-05-25 14:22:25 +02:00
2025-06-12 00:31:01 +02:00
ZoomMap(e.GetPosition(this), zoomLevel, animated);
2024-05-25 14:22:25 +02:00
base.OnPointerWheelChanged(e);
2024-05-19 23:23:27 +02:00
}
2025-03-19 11:14:13 +01:00
protected override void OnPointerMoved(PointerEventArgs e)
2024-05-29 19:18:37 +02:00
{
var point = e.GetCurrentPoint(this);
2025-03-19 11:14:13 +01:00
if (point.Pointer == pointer1 || point.Pointer == pointer2)
{
if (pointer2 != null)
{
HandleManipulation(point.Pointer, point.Position);
}
else if (point.Pointer.Type == PointerType.Mouse ||
ManipulationModes.HasFlag(ManipulationModes.Translate))
2025-03-19 11:14:13 +01:00
{
TranslateMap(new Point(point.Position.X - position1.X, point.Position.Y - position1.Y));
position1 = point.Position;
}
}
else if (pointer1 == null &&
point.Pointer.Type == PointerType.Mouse &&
point.Properties.IsLeftButtonPressed &&
e.KeyModifiers == KeyModifiers.None ||
pointer2 == null &&
point.Pointer.Type == PointerType.Touch &&
ManipulationModes != ManipulationModes.None)
2024-05-29 19:18:37 +02:00
{
point.Pointer.Capture(this);
2025-01-06 15:33:12 +01:00
if (pointer1 == null)
{
pointer1 = point.Pointer;
position1 = point.Position;
}
else
{
pointer2 = point.Pointer;
position2 = point.Position;
}
2024-05-29 19:18:37 +02:00
}
2025-03-19 11:14:13 +01:00
base.OnPointerMoved(e);
2024-05-29 19:18:37 +02:00
}
2025-01-06 16:54:43 +01:00
protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e)
2024-05-29 19:18:37 +02:00
{
2025-01-06 16:54:43 +01:00
if (e.Pointer == pointer1 || e.Pointer == pointer2)
2024-05-29 19:18:37 +02:00
{
2025-01-06 16:54:43 +01:00
if (e.Pointer == pointer1)
2025-01-06 15:33:12 +01:00
{
pointer1 = pointer2;
position1 = position2;
}
pointer2 = null;
2024-05-29 19:18:37 +02:00
}
base.OnPointerCaptureLost(e);
}
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
}
}
}