PanAndZoomGestureRecognizer.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using Avalonia;
  2. using Avalonia.Input;
  3. using Avalonia.Input.GestureRecognizers;
  4. using Avalonia.Interactivity;
  5. namespace InABox.Avalonia.Components;
  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. public static readonly StyledProperty<bool> IsEnabledProperty =
  34. AvaloniaProperty.Register<PanAndZoomGestureRecognizer, bool>(nameof(IsEnabled), defaultValue: true);
  35. public static readonly StyledProperty<bool> SingleTouchPanningProperty =
  36. AvaloniaProperty.Register<PanAndZoomGestureRecognizer, bool>(nameof(SingleTouchPanning), defaultValue: true);
  37. public bool IsEnabled
  38. {
  39. get => GetValue(IsEnabledProperty);
  40. set => SetValue(IsEnabledProperty, value);
  41. }
  42. public bool SingleTouchPanning
  43. {
  44. get => GetValue(SingleTouchPanningProperty);
  45. set => SetValue(SingleTouchPanningProperty, value);
  46. }
  47. private float _initialDistance;
  48. private IPointer? _firstContact;
  49. private Point _firstPoint;
  50. private Point _initialFirstPoint;
  51. private IPointer? _secondContact;
  52. private Point _secondPoint;
  53. private Point _previousOrigin;
  54. protected override void PointerCaptureLost(IPointer pointer)
  55. {
  56. RemoveContact(pointer);
  57. }
  58. protected override void PointerMoved(PointerEventArgs e)
  59. {
  60. if (!IsEnabled) return;
  61. if (Target is Visual visual)
  62. {
  63. if (_firstContact == e.Pointer)
  64. {
  65. _firstPoint = e.GetPosition(visual);
  66. }
  67. else if (_secondContact == e.Pointer)
  68. {
  69. _secondPoint = e.GetPosition(visual);
  70. }
  71. else
  72. {
  73. return;
  74. }
  75. if (_firstContact != null && _secondContact != null)
  76. {
  77. var distance = GetDistance(_firstPoint, _secondPoint);
  78. var scale = distance / _initialDistance;
  79. var origin = (_firstPoint + _secondPoint) / 2;
  80. var pinchEventArgs = new PanAndZoomEventArgs(scale, origin, origin - _previousOrigin);
  81. _previousOrigin = origin;
  82. Target?.RaiseEvent(pinchEventArgs);
  83. e.Handled = pinchEventArgs.Handled;
  84. e.PreventGestureRecognition();
  85. }
  86. else if(_firstContact != null && SingleTouchPanning)
  87. {
  88. var pinchEventArgs = new PanAndZoomEventArgs(1, _firstPoint, _firstPoint - _initialFirstPoint);
  89. _initialFirstPoint = _firstPoint;
  90. Target?.RaiseEvent(pinchEventArgs);
  91. e.Handled = pinchEventArgs.Handled;
  92. e.PreventGestureRecognition();
  93. }
  94. }
  95. }
  96. protected override void PointerPressed(PointerPressedEventArgs e)
  97. {
  98. if (!IsEnabled) return;
  99. if (Target is Visual visual && (e.Pointer.Type == PointerType.Touch || e.Pointer.Type == PointerType.Pen))
  100. {
  101. if (_firstContact == null)
  102. {
  103. _firstContact = e.Pointer;
  104. _firstPoint = e.GetPosition(visual);
  105. }
  106. else if (_secondContact == null && _firstContact != e.Pointer)
  107. {
  108. _secondContact = e.Pointer;
  109. _secondPoint = e.GetPosition(visual);
  110. }
  111. else
  112. {
  113. return;
  114. }
  115. if (_firstContact != null && _secondContact != null)
  116. {
  117. _initialDistance = GetDistance(_firstPoint, _secondPoint);
  118. _previousOrigin = new Point((_firstPoint.X + _secondPoint.X) / 2.0f, (_firstPoint.Y + _secondPoint.Y) / 2.0f);
  119. Capture(_firstContact);
  120. Capture(_secondContact);
  121. e.PreventGestureRecognition();
  122. }
  123. else if(_firstContact != null && SingleTouchPanning)
  124. {
  125. _initialFirstPoint = _firstPoint;
  126. Capture(_firstContact);
  127. e.PreventGestureRecognition();
  128. }
  129. }
  130. }
  131. protected override void PointerReleased(PointerReleasedEventArgs e)
  132. {
  133. if(RemoveContact(e.Pointer))
  134. {
  135. e.PreventGestureRecognition();
  136. }
  137. }
  138. private bool RemoveContact(IPointer pointer)
  139. {
  140. if (_firstContact == pointer || _secondContact == pointer)
  141. {
  142. if (_secondContact == pointer)
  143. {
  144. _secondContact = null;
  145. _initialFirstPoint = _firstPoint;
  146. }
  147. if (_firstContact == pointer)
  148. {
  149. _firstContact = _secondContact;
  150. _initialFirstPoint = _secondPoint;
  151. _secondContact = null;
  152. }
  153. Target?.RaiseEvent(new PanAndZoomEndedEventArgs());
  154. return true;
  155. }
  156. return false;
  157. }
  158. private static float GetDistance(Point a, Point b)
  159. {
  160. var distX = a.X - b.X;
  161. var distY = a.Y - b.Y;
  162. return (float)Math.Sqrt(distX * distX + distY * distY);
  163. }
  164. }