PlotBase.Events.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PlotBase.Events.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // Represents a control that displays a <see cref="PlotModel" />.
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. using Avalonia.Controls;
  10. namespace OxyPlot.Avalonia
  11. {
  12. using global::Avalonia.Input;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. /// <summary>
  17. /// Represents a control that displays a <see cref="PlotModel" />.
  18. /// </summary>
  19. public partial class PlotBase
  20. {
  21. /// <summary>
  22. /// Called before the <see cref="E:System.Windows.UIElement.KeyDown" /> event occurs.
  23. /// </summary>
  24. /// <param name="e">The data for the event.</param>
  25. protected override void OnKeyDown(KeyEventArgs e)
  26. {
  27. base.OnKeyDown(e);
  28. if (e.Handled)
  29. {
  30. return;
  31. }
  32. var args = new OxyKeyEventArgs { ModifierKeys = e.KeyModifiers.ToModifierKeys(), Key = e.Key.Convert() };
  33. e.Handled = ActualController.HandleKeyDown(this, args);
  34. }
  35. /// <summary>
  36. /// Called before the <see cref="E:System.Windows.UIElement.MouseWheel" /> event occurs to provide handling for the event in a derived class without attaching a delegate.
  37. /// </summary>
  38. /// <param name="e">A <see cref="T:System.Windows.Input.MouseWheelEventArgs" /> that contains the event data.</param>
  39. protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
  40. {
  41. base.OnPointerWheelChanged(e);
  42. if (e.Handled || !IsMouseWheelEnabled)
  43. {
  44. return;
  45. }
  46. e.Handled = ActualController.HandleMouseWheel(this, e.ToMouseWheelEventArgs(this));
  47. }
  48. /// <summary>
  49. /// Gets the dictionary of locations of touch pointers.
  50. /// </summary>
  51. private SortedDictionary<int, ScreenPoint> TouchPositions { get; } = new SortedDictionary<int, ScreenPoint>();
  52. /// <summary>
  53. /// Invoked when an unhandled MouseDown attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
  54. /// </summary>
  55. /// <param name="e">The <see cref="T:System.Windows.Input.MouseButtonEventArgs" /> that contains the event data. This event data reports details about the mouse button that was pressed and the handled state.</param>
  56. protected override void OnPointerPressed(PointerPressedEventArgs e)
  57. {
  58. base.OnPointerPressed(e);
  59. if (e.Handled)
  60. {
  61. return;
  62. }
  63. Focus();
  64. e.Pointer.Capture(this);
  65. if (e.Pointer.Type == PointerType.Touch)
  66. {
  67. var position = e.GetPosition(this).ToScreenPoint();
  68. var touchEventArgs = new OxyTouchEventArgs()
  69. {
  70. ModifierKeys = e.KeyModifiers.ToModifierKeys(),
  71. Position = position,
  72. DeltaTranslation = new ScreenVector(0, 0),
  73. DeltaScale = new ScreenVector(1, 1),
  74. };
  75. TouchPositions[e.Pointer.Id] = position;
  76. if (TouchPositions.Count == 1)
  77. {
  78. e.Handled = ActualController.HandleTouchStarted(this, touchEventArgs);
  79. }
  80. }
  81. else
  82. {
  83. // store the mouse down point, check it when mouse button is released to determine if the context menu should be shown
  84. mouseDownPoint = e.GetPosition(this).ToScreenPoint();
  85. e.Handled = ActualController.HandleMouseDown(this, e.ToMouseDownEventArgs(this));
  86. }
  87. }
  88. /// <summary>
  89. /// Invoked when an unhandled MouseMove attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
  90. /// </summary>
  91. /// <param name="e">The <see cref="T:System.Windows.Input.MouseEventArgs" /> that contains the event data.</param>
  92. protected override void OnPointerMoved(PointerEventArgs e)
  93. {
  94. base.OnPointerMoved(e);
  95. if (e.Handled)
  96. {
  97. return;
  98. }
  99. if (e.Pointer.Type == PointerType.Touch)
  100. {
  101. var point = e.GetPosition(this).ToScreenPoint();
  102. var oldTouchPoints = TouchPositions.Values.ToArray();
  103. TouchPositions[e.Pointer.Id] = point;
  104. var newTouchPoints = TouchPositions.Values.ToArray();
  105. var touchEventArgs = new OxyTouchEventArgs(newTouchPoints, oldTouchPoints);
  106. e.Handled = ActualController.HandleTouchDelta(this, touchEventArgs);
  107. }
  108. else
  109. {
  110. e.Handled = ActualController.HandleMouseMove(this, e.ToMouseEventArgs(this));
  111. }
  112. }
  113. /// <summary>
  114. /// Invoked when an unhandled MouseUp routed event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
  115. /// </summary>
  116. /// <param name="e">The <see cref="T:System.Windows.Input.MouseButtonEventArgs" /> that contains the event data. The event data reports that the mouse button was released.</param>
  117. protected override void OnPointerReleased(PointerReleasedEventArgs e)
  118. {
  119. base.OnPointerReleased(e);
  120. if (e.Handled)
  121. {
  122. return;
  123. }
  124. e.Pointer.Capture(null);
  125. if (e.Pointer.Type == PointerType.Touch)
  126. {
  127. var position = e.GetPosition(this).ToScreenPoint();
  128. var touchEventArgs = new OxyTouchEventArgs()
  129. {
  130. ModifierKeys = e.KeyModifiers.ToModifierKeys(),
  131. Position = position,
  132. DeltaTranslation = new ScreenVector(0, 0),
  133. DeltaScale = new ScreenVector(1, 1),
  134. };
  135. TouchPositions.Remove(e.Pointer.Id);
  136. if (TouchPositions.Count == 0)
  137. {
  138. e.Handled = ActualController.HandleTouchCompleted(this, touchEventArgs);
  139. }
  140. }
  141. else
  142. {
  143. e.Handled = ActualController.HandleMouseUp(this, e.ToMouseReleasedEventArgs(this));
  144. // Open the context menu
  145. var p = e.GetPosition(this).ToScreenPoint();
  146. var d = p.DistanceTo(mouseDownPoint);
  147. if (ContextMenu != null)
  148. {
  149. if (Math.Abs(d) < 1e-8 && e.InitialPressMouseButton == MouseButton.Right)
  150. {
  151. ContextMenu.DataContext = DataContext;
  152. ContextMenu.IsVisible = true;
  153. }
  154. else
  155. {
  156. ContextMenu.IsVisible = false;
  157. }
  158. }
  159. }
  160. }
  161. /// <summary>
  162. /// Invoked when an unhandled <see cref="E:System.Windows.Input.Mouse.MouseEnter" /> attached event is raised on this element. Implement this method to add class handling for this event.
  163. /// </summary>
  164. /// <param name="e">The <see cref="T:System.Windows.Input.MouseEventArgs" /> that contains the event data.</param>
  165. protected override void OnPointerEntered(PointerEventArgs e)
  166. {
  167. base.OnPointerEntered(e);
  168. if (e.Handled)
  169. {
  170. return;
  171. }
  172. e.Handled = ActualController.HandleMouseEnter(this, e.ToMouseEventArgs(this));
  173. }
  174. /// <summary>
  175. /// Invoked when an unhandled <see cref="E:System.Windows.Input.Mouse.MouseLeave" /> attached event is raised on this element. Implement this method to add class handling for this event.
  176. /// </summary>
  177. /// <param name="e">The <see cref="T:System.Windows.Input.MouseEventArgs" /> that contains the event data.</param>
  178. protected override void OnPointerExited(PointerEventArgs e)
  179. {
  180. base.OnPointerExited(e);
  181. if (e.Handled)
  182. {
  183. return;
  184. }
  185. e.Handled = ActualController.HandleMouseLeave(this, e.ToMouseEventArgs(this));
  186. }
  187. }
  188. }