2025-08-19 19:43:02 +02:00
|
|
|
|
using Avalonia;
|
|
|
|
|
|
using Avalonia.Input;
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-12 07:41:01 +02:00
|
|
|
|
public partial class Map
|
2024-05-19 23:23:27 +02:00
|
|
|
|
{
|
2025-06-12 07:41:01 +02:00
|
|
|
|
public static readonly StyledProperty<ManipulationModes> ManipulationModeProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<Map, ManipulationModes>(nameof(ManipulationMode), ManipulationModes.Translate | ManipulationModes.Scale);
|
2024-05-28 22:16:29 +02:00
|
|
|
|
|
2024-05-19 23:23:27 +02:00
|
|
|
|
/// <summary>
|
2025-06-12 07:41:01 +02:00
|
|
|
|
/// Gets or sets a value that specifies how the map control handles manipulations.
|
2024-05-19 23:23:27 +02:00
|
|
|
|
/// </summary>
|
2025-06-12 07:41:01 +02:00
|
|
|
|
public ManipulationModes ManipulationMode
|
2024-05-19 23:23:27 +02:00
|
|
|
|
{
|
2025-06-12 07:41:01 +02:00
|
|
|
|
get => GetValue(ManipulationModeProperty);
|
|
|
|
|
|
set => SetValue(ManipulationModeProperty, value);
|
2024-05-19 23:23:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
|
2024-05-19 23:23:27 +02:00
|
|
|
|
{
|
2024-05-25 14:22:25 +02:00
|
|
|
|
base.OnPointerWheelChanged(e);
|
2025-06-12 22:10:55 +02:00
|
|
|
|
|
|
|
|
|
|
OnMouseWheel(e.GetPosition(this), e.Delta.Y);
|
2024-05-19 23:23:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-12 22:10:55 +02:00
|
|
|
|
private IPointer pointer1;
|
|
|
|
|
|
private IPointer pointer2;
|
|
|
|
|
|
private Point position1;
|
|
|
|
|
|
private Point position2;
|
|
|
|
|
|
|
2025-03-19 11:14:13 +01:00
|
|
|
|
protected override void OnPointerMoved(PointerEventArgs e)
|
2024-05-29 19:18:37 +02:00
|
|
|
|
{
|
2025-06-12 22:10:55 +02:00
|
|
|
|
base.OnPointerMoved(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);
|
|
|
|
|
|
}
|
2025-03-19 17:16:22 +01:00
|
|
|
|
else if (point.Pointer.Type == PointerType.Mouse ||
|
2025-06-12 07:41:01 +02:00
|
|
|
|
ManipulationMode.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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-03-19 17:16:22 +01:00
|
|
|
|
else if (pointer1 == null &&
|
|
|
|
|
|
point.Pointer.Type == PointerType.Mouse &&
|
|
|
|
|
|
point.Properties.IsLeftButtonPressed &&
|
|
|
|
|
|
e.KeyModifiers == KeyModifiers.None ||
|
|
|
|
|
|
pointer2 == null &&
|
|
|
|
|
|
point.Pointer.Type == PointerType.Touch &&
|
2025-06-12 07:41:01 +02:00
|
|
|
|
ManipulationMode != 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-01-06 16:54:43 +01:00
|
|
|
|
protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e)
|
2024-05-29 19:18:37 +02:00
|
|
|
|
{
|
2025-06-12 22:10:55 +02:00
|
|
|
|
base.OnPointerCaptureLost(e);
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2025-06-12 07:41:01 +02:00
|
|
|
|
if (ManipulationMode.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
|
|
|
|
|
2025-06-12 07:41:01 +02:00
|
|
|
|
if (ManipulationMode.HasFlag(ManipulationModes.Rotate))
|
2024-05-29 19:18:37 +02:00
|
|
|
|
{
|
|
|
|
|
|
rotation = 180d / Math.PI
|
|
|
|
|
|
* (Math.Atan2(newDistance.Y, newDistance.X) - Math.Atan2(oldDistance.Y, oldDistance.X));
|
|
|
|
|
|
}
|
2024-05-25 14:22:25 +02:00
|
|
|
|
|
2025-06-12 07:41:01 +02:00
|
|
|
|
if (ManipulationMode.HasFlag(ManipulationModes.Scale))
|
2024-05-29 19:18:37 +02:00
|
|
|
|
{
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|