GesturePage.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Input;
  5. using Avalonia.LogicalTree;
  6. using Avalonia.Rendering.Composition;
  7. using Avalonia.Utilities;
  8. namespace ControlCatalog.Pages
  9. {
  10. public partial class GesturePage : UserControl
  11. {
  12. private bool _isInit;
  13. private double _currentScale;
  14. public GesturePage()
  15. {
  16. InitializeComponent();
  17. }
  18. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  19. {
  20. base.OnAttachedToVisualTree(e);
  21. if (_isInit)
  22. {
  23. return;
  24. }
  25. _isInit = true;
  26. SetPullHandlers(TopPullZone, false);
  27. SetPullHandlers(BottomPullZone, true);
  28. SetPullHandlers(RightPullZone, true);
  29. SetPullHandlers(LeftPullZone, false);
  30. var image = PinchImage;
  31. SetPinchHandlers(image);
  32. var reset = ResetButton;
  33. reset.Click += (_, _) =>
  34. {
  35. var compositionVisual = ElementComposition.GetElementVisual(image);
  36. if (compositionVisual != null)
  37. {
  38. _currentScale = 1;
  39. compositionVisual.Scale = new(1, 1, 1);
  40. compositionVisual.Offset = default;
  41. image.InvalidateMeasure();
  42. }
  43. };
  44. RotationGesture.AddHandler(Gestures.PinchEvent, (s, e) =>
  45. {
  46. AngleSlider.Value = e.Angle;
  47. });
  48. }
  49. private void SetPinchHandlers(Control? control)
  50. {
  51. if (control == null)
  52. {
  53. return;
  54. }
  55. _currentScale = 1;
  56. Vector3D currentOffset = default;
  57. CompositionVisual? compositionVisual = null;
  58. void InitComposition(Control visual)
  59. {
  60. if (compositionVisual != null)
  61. {
  62. return;
  63. }
  64. compositionVisual = ElementComposition.GetElementVisual(visual);
  65. }
  66. control.LayoutUpdated += (s, e) =>
  67. {
  68. InitComposition(control!);
  69. if (compositionVisual != null)
  70. {
  71. compositionVisual.Scale = new(_currentScale, _currentScale, 1);
  72. if (currentOffset == default)
  73. {
  74. currentOffset = compositionVisual.Offset;
  75. }
  76. }
  77. };
  78. control.AddHandler(Gestures.PinchEvent, (s, e) =>
  79. {
  80. InitComposition(control!);
  81. if (compositionVisual != null)
  82. {
  83. var scale = _currentScale * (float)e.Scale;
  84. if (scale <= 1)
  85. {
  86. scale = 1;
  87. compositionVisual.Offset = default;
  88. }
  89. compositionVisual.Scale = new(scale, scale, 1);
  90. e.Handled = true;
  91. }
  92. });
  93. control.AddHandler(Gestures.PinchEndedEvent, (s, e) =>
  94. {
  95. InitComposition(control!);
  96. if (compositionVisual != null)
  97. {
  98. _currentScale = compositionVisual.Scale.X;
  99. }
  100. });
  101. control.AddHandler(Gestures.ScrollGestureEvent, (s, e) =>
  102. {
  103. InitComposition(control!);
  104. if (compositionVisual != null && _currentScale != 1)
  105. {
  106. currentOffset += new Vector3D(e.Delta.X, e.Delta.Y, 0);
  107. var currentSize = control.Bounds.Size * _currentScale;
  108. currentOffset = new Vector3D(MathUtilities.Clamp(currentOffset.X, 0, currentSize.Width - control.Bounds.Width),
  109. (float)MathUtilities.Clamp(currentOffset.Y, 0, currentSize.Height - control.Bounds.Height),
  110. 0);
  111. compositionVisual.Offset = currentOffset * -1;
  112. e.Handled = true;
  113. }
  114. });
  115. }
  116. private void SetPullHandlers(Control control, bool inverse)
  117. {
  118. var ball = control.FindLogicalDescendantOfType<Border>();
  119. Vector3D defaultOffset = default;
  120. CompositionVisual? ballCompositionVisual = null;
  121. if (ball != null)
  122. {
  123. InitComposition(ball);
  124. }
  125. else
  126. {
  127. return;
  128. }
  129. control.LayoutUpdated += (s, e) =>
  130. {
  131. InitComposition(ball!);
  132. if (ballCompositionVisual != null)
  133. {
  134. defaultOffset = ballCompositionVisual.Offset;
  135. }
  136. };
  137. control.AddHandler(Gestures.PullGestureEvent, (s, e) =>
  138. {
  139. Vector3D center = new((float)control.Bounds.Center.X, (float)control.Bounds.Center.Y, 0);
  140. InitComposition(ball!);
  141. if (ballCompositionVisual != null)
  142. {
  143. ballCompositionVisual.Offset = defaultOffset + new Vector3D(e.Delta.X * 0.4f, e.Delta.Y * 0.4f, 0) * (inverse ? -1 : 1);
  144. e.Handled = true;
  145. }
  146. });
  147. control.AddHandler(Gestures.PullGestureEndedEvent, (s, e) =>
  148. {
  149. InitComposition(ball!);
  150. if (ballCompositionVisual != null)
  151. {
  152. ballCompositionVisual.Offset = defaultOffset;
  153. }
  154. });
  155. void InitComposition(Control control)
  156. {
  157. ballCompositionVisual = ElementComposition.GetElementVisual(ball);
  158. if (ballCompositionVisual != null)
  159. {
  160. var offsetAnimation = ballCompositionVisual.Compositor.CreateVector3KeyFrameAnimation();
  161. offsetAnimation.Target = "Offset";
  162. offsetAnimation.InsertExpressionKeyFrame(1.0f, "this.FinalValue");
  163. offsetAnimation.Duration = TimeSpan.FromMilliseconds(100);
  164. var implicitAnimations = ballCompositionVisual.Compositor.CreateImplicitAnimationCollection();
  165. implicitAnimations["Offset"] = offsetAnimation;
  166. ballCompositionVisual.ImplicitAnimations = implicitAnimations;
  167. }
  168. }
  169. }
  170. }
  171. }