PanAndZoomGestureRecognizer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using Avalonia;
  2. using Avalonia.Input;
  3. using Avalonia.Input.GestureRecognizers;
  4. using Avalonia.Interactivity;
  5. namespace InABox.Avalonia.Components.ImageEditing;
  6. public class PanAndZoomEventArgs : RoutedEventArgs
  7. {
  8. public PanAndZoomEventArgs(double scale, Point scaleOrigin, Vector pan) : base(PanAndZoomGestureRecognizer.PanAndZoomEvent)
  9. {
  10. Scale = scale;
  11. ScaleOrigin = scaleOrigin;
  12. Pan = pan;
  13. }
  14. public double Scale { get; } = 1;
  15. public Point ScaleOrigin { get; }
  16. public Vector Pan { get; set; }
  17. }
  18. public class PanAndZoomEndedEventArgs : RoutedEventArgs
  19. {
  20. public PanAndZoomEndedEventArgs() : base(PanAndZoomGestureRecognizer.PanAndZoomEndedEvent)
  21. {
  22. }
  23. }
  24. // Adapted from the PinchGestureRecognizer built in to Avalonia, but with panning added.
  25. public class PanAndZoomGestureRecognizer : GestureRecognizer
  26. {
  27. public static readonly RoutedEvent<PanAndZoomEventArgs> PanAndZoomEvent =
  28. RoutedEvent.Register<PanAndZoomEventArgs>(
  29. "PanAndZoom", RoutingStrategies.Bubble, typeof(PanAndZoomGestureRecognizer));
  30. public static readonly RoutedEvent<PanAndZoomEndedEventArgs> PanAndZoomEndedEvent =
  31. RoutedEvent.Register<PanAndZoomEndedEventArgs>(
  32. "PanAndZoomEnded", RoutingStrategies.Bubble, typeof(PanAndZoomGestureRecognizer));
  33. private float _initialDistance;
  34. private IPointer? _firstContact;
  35. private Point _firstPoint;
  36. private IPointer? _secondContact;
  37. private Point _secondPoint;
  38. private Point _previousOrigin;
  39. protected override void PointerCaptureLost(IPointer pointer)
  40. {
  41. RemoveContact(pointer);
  42. }
  43. protected override void PointerMoved(PointerEventArgs e)
  44. {
  45. if (Target is Visual visual)
  46. {
  47. if (_firstContact == e.Pointer)
  48. {
  49. _firstPoint = e.GetPosition(visual);
  50. }
  51. else if (_secondContact == e.Pointer)
  52. {
  53. _secondPoint = e.GetPosition(visual);
  54. }
  55. else
  56. {
  57. return;
  58. }
  59. if (_firstContact != null && _secondContact != null)
  60. {
  61. var distance = GetDistance(_firstPoint, _secondPoint);
  62. var scale = distance / _initialDistance;
  63. var origin = (_firstPoint + _secondPoint) / 2;
  64. var pinchEventArgs = new PanAndZoomEventArgs(scale, origin, origin - _previousOrigin);
  65. _previousOrigin = origin;
  66. Target?.RaiseEvent(pinchEventArgs);
  67. e.Handled = pinchEventArgs.Handled;
  68. e.PreventGestureRecognition();
  69. }
  70. }
  71. }
  72. protected override void PointerPressed(PointerPressedEventArgs e)
  73. {
  74. if (Target is Visual visual && (e.Pointer.Type == PointerType.Touch || e.Pointer.Type == PointerType.Pen))
  75. {
  76. if (_firstContact == null)
  77. {
  78. _firstContact = e.Pointer;
  79. _firstPoint = e.GetPosition(visual);
  80. return;
  81. }
  82. else if (_secondContact == null && _firstContact != e.Pointer)
  83. {
  84. _secondContact = e.Pointer;
  85. _secondPoint = e.GetPosition(visual);
  86. }
  87. else
  88. {
  89. return;
  90. }
  91. if (_firstContact != null && _secondContact != null)
  92. {
  93. _initialDistance = GetDistance(_firstPoint, _secondPoint);
  94. _previousOrigin = new Point((_firstPoint.X + _secondPoint.X) / 2.0f, (_firstPoint.Y + _secondPoint.Y) / 2.0f);
  95. Capture(_firstContact);
  96. Capture(_secondContact);
  97. e.PreventGestureRecognition();
  98. }
  99. }
  100. }
  101. protected override void PointerReleased(PointerReleasedEventArgs e)
  102. {
  103. if(RemoveContact(e.Pointer))
  104. {
  105. e.PreventGestureRecognition();
  106. }
  107. }
  108. private bool RemoveContact(IPointer pointer)
  109. {
  110. if (_firstContact == pointer || _secondContact == pointer)
  111. {
  112. if (_secondContact == pointer)
  113. {
  114. _secondContact = null;
  115. }
  116. if (_firstContact == pointer)
  117. {
  118. _firstContact = _secondContact;
  119. _secondContact = null;
  120. }
  121. Target?.RaiseEvent(new PanAndZoomEndedEventArgs());
  122. return true;
  123. }
  124. return false;
  125. }
  126. private static float GetDistance(Point a, Point b)
  127. {
  128. var length = b - a;
  129. return (float)new Vector(length.X, length.Y).Length;
  130. }
  131. }