PointersPage.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Reactive.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Threading;
  8. using Avalonia;
  9. using Avalonia.Controls;
  10. using Avalonia.Input;
  11. using Avalonia.Layout;
  12. using Avalonia.Media;
  13. using Avalonia.Media.Immutable;
  14. using Avalonia.Threading;
  15. using Avalonia.VisualTree;
  16. namespace ControlCatalog.Pages;
  17. public class PointersPage : Decorator
  18. {
  19. public PointersPage()
  20. {
  21. Child = new TabControl
  22. {
  23. Items = new[]
  24. {
  25. new TabItem() { Header = "Contacts", Content = new PointerContactsTab() },
  26. new TabItem() { Header = "IntermediatePoints", Content = new PointerIntermediatePointsTab() }
  27. }
  28. };
  29. }
  30. class PointerContactsTab : Control
  31. {
  32. class PointerInfo
  33. {
  34. public Point Point { get; set; }
  35. public Color Color { get; set; }
  36. }
  37. private static Color[] AllColors = new[]
  38. {
  39. Colors.Aqua,
  40. Colors.Beige,
  41. Colors.Chartreuse,
  42. Colors.Coral,
  43. Colors.Fuchsia,
  44. Colors.Crimson,
  45. Colors.Lavender,
  46. Colors.Orange,
  47. Colors.Orchid,
  48. Colors.ForestGreen,
  49. Colors.SteelBlue,
  50. Colors.PapayaWhip,
  51. Colors.PaleVioletRed,
  52. Colors.Goldenrod,
  53. Colors.Maroon,
  54. Colors.Moccasin,
  55. Colors.Navy,
  56. Colors.Wheat,
  57. Colors.Violet,
  58. Colors.Sienna,
  59. Colors.Indigo,
  60. Colors.Honeydew
  61. };
  62. private Dictionary<IPointer, PointerInfo> _pointers = new Dictionary<IPointer, PointerInfo>();
  63. public PointerContactsTab()
  64. {
  65. ClipToBounds = true;
  66. }
  67. void UpdatePointer(PointerEventArgs e)
  68. {
  69. if (!_pointers.TryGetValue(e.Pointer, out var info))
  70. {
  71. if (e.RoutedEvent == PointerMovedEvent)
  72. return;
  73. var colors = AllColors.Except(_pointers.Values.Select(c => c.Color)).ToArray();
  74. var color = colors[new Random().Next(0, colors.Length - 1)];
  75. _pointers[e.Pointer] = info = new PointerInfo {Color = color};
  76. }
  77. info.Point = e.GetPosition(this);
  78. InvalidateVisual();
  79. }
  80. protected override void OnPointerPressed(PointerPressedEventArgs e)
  81. {
  82. UpdatePointer(e);
  83. e.Pointer.Capture(this);
  84. e.Handled = true;
  85. base.OnPointerPressed(e);
  86. }
  87. protected override void OnPointerMoved(PointerEventArgs e)
  88. {
  89. UpdatePointer(e);
  90. e.Handled = true;
  91. base.OnPointerMoved(e);
  92. }
  93. protected override void OnPointerReleased(PointerReleasedEventArgs e)
  94. {
  95. _pointers.Remove(e.Pointer);
  96. e.Handled = true;
  97. InvalidateVisual();
  98. }
  99. protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e)
  100. {
  101. _pointers.Remove(e.Pointer);
  102. InvalidateVisual();
  103. }
  104. public override void Render(DrawingContext context)
  105. {
  106. context.FillRectangle(Brushes.Transparent, new Rect(default, Bounds.Size));
  107. foreach (var pt in _pointers.Values)
  108. {
  109. var brush = new ImmutableSolidColorBrush(pt.Color);
  110. context.DrawEllipse(brush, null, pt.Point, 75, 75);
  111. }
  112. }
  113. }
  114. public class PointerIntermediatePointsTab : Decorator
  115. {
  116. public PointerIntermediatePointsTab()
  117. {
  118. this[TextBlock.ForegroundProperty] = Brushes.Black;
  119. var slider = new Slider
  120. {
  121. Margin = new Thickness(5),
  122. Minimum = 0,
  123. Maximum = 500
  124. };
  125. var status = new TextBlock()
  126. {
  127. HorizontalAlignment = HorizontalAlignment.Left,
  128. VerticalAlignment = VerticalAlignment.Top,
  129. };
  130. Child = new Grid
  131. {
  132. Children =
  133. {
  134. new PointerCanvas(slider, status),
  135. new Border
  136. {
  137. Background = Brushes.LightYellow,
  138. Child = new StackPanel
  139. {
  140. Children =
  141. {
  142. new StackPanel
  143. {
  144. Orientation = Orientation.Horizontal,
  145. Children =
  146. {
  147. new TextBlock { Text = "Thread sleep:" },
  148. new TextBlock()
  149. {
  150. [!TextBlock.TextProperty] =slider.GetObservable(Slider.ValueProperty)
  151. .Select(x=>x.ToString()).ToBinding()
  152. }
  153. }
  154. },
  155. slider
  156. }
  157. },
  158. HorizontalAlignment = HorizontalAlignment.Right,
  159. VerticalAlignment = VerticalAlignment.Top,
  160. Width = 300,
  161. Height = 60
  162. },
  163. status
  164. }
  165. };
  166. }
  167. class PointerCanvas : Control
  168. {
  169. private readonly Slider _slider;
  170. private readonly TextBlock _status;
  171. private int _events;
  172. private Stopwatch _stopwatch = Stopwatch.StartNew();
  173. private Dictionary<int, PointerPoints> _pointers = new();
  174. class PointerPoints
  175. {
  176. struct CanvasPoint
  177. {
  178. public IBrush Brush;
  179. public Point Point;
  180. public double Radius;
  181. }
  182. readonly CanvasPoint[] _points = new CanvasPoint[1000];
  183. int _index;
  184. public void Render(DrawingContext context)
  185. {
  186. CanvasPoint? prev = null;
  187. for (var c = 0; c < _points.Length; c++)
  188. {
  189. var i = (c + _index) % _points.Length;
  190. var pt = _points[i];
  191. if (prev.HasValue && prev.Value.Brush != null && pt.Brush != null)
  192. context.DrawLine(new Pen(Brushes.Black), prev.Value.Point, pt.Point);
  193. prev = pt;
  194. if (pt.Brush != null)
  195. context.DrawEllipse(pt.Brush, null, pt.Point, pt.Radius, pt.Radius);
  196. }
  197. }
  198. void AddPoint(Point pt, IBrush brush, double radius)
  199. {
  200. _points[_index] = new CanvasPoint { Point = pt, Brush = brush, Radius = radius };
  201. _index = (_index + 1) % _points.Length;
  202. }
  203. public void HandleEvent(PointerEventArgs e, Visual v)
  204. {
  205. e.Handled = true;
  206. if (e.RoutedEvent == PointerPressedEvent)
  207. AddPoint(e.GetPosition(v), Brushes.Green, 10);
  208. else if (e.RoutedEvent == PointerReleasedEvent)
  209. AddPoint(e.GetPosition(v), Brushes.Red, 10);
  210. else
  211. {
  212. var pts = e.GetIntermediatePoints(v);
  213. for (var c = 0; c < pts.Count; c++)
  214. {
  215. var pt = pts[c];
  216. AddPoint(pt.Position, c == pts.Count - 1 ? Brushes.Blue : Brushes.Black,
  217. c == pts.Count - 1 ? 5 : 2);
  218. }
  219. }
  220. }
  221. }
  222. public PointerCanvas(Slider slider, TextBlock status)
  223. {
  224. _slider = slider;
  225. _status = status;
  226. DispatcherTimer.Run(() =>
  227. {
  228. if (_stopwatch.Elapsed.TotalSeconds > 1)
  229. {
  230. _status.Text = "Events per second: " + (_events / _stopwatch.Elapsed.TotalSeconds);
  231. _stopwatch.Restart();
  232. _events = 0;
  233. }
  234. return this.GetVisualRoot() != null;
  235. }, TimeSpan.FromMilliseconds(10));
  236. }
  237. void HandleEvent(PointerEventArgs e)
  238. {
  239. _events++;
  240. Thread.Sleep((int)_slider.Value);
  241. InvalidateVisual();
  242. if (e.RoutedEvent == PointerReleasedEvent && e.Pointer.Type == PointerType.Touch)
  243. {
  244. _pointers.Remove(e.Pointer.Id);
  245. return;
  246. }
  247. if (!_pointers.TryGetValue(e.Pointer.Id, out var pt))
  248. _pointers[e.Pointer.Id] = pt = new PointerPoints();
  249. pt.HandleEvent(e, this);
  250. }
  251. public override void Render(DrawingContext context)
  252. {
  253. context.FillRectangle(Brushes.White, Bounds);
  254. foreach(var pt in _pointers.Values)
  255. pt.Render(context);
  256. base.Render(context);
  257. }
  258. protected override void OnPointerPressed(PointerPressedEventArgs e)
  259. {
  260. if (e.ClickCount == 2)
  261. {
  262. _pointers.Clear();
  263. InvalidateVisual();
  264. return;
  265. }
  266. HandleEvent(e);
  267. base.OnPointerPressed(e);
  268. }
  269. protected override void OnPointerMoved(PointerEventArgs e)
  270. {
  271. HandleEvent(e);
  272. base.OnPointerMoved(e);
  273. }
  274. protected override void OnPointerReleased(PointerReleasedEventArgs e)
  275. {
  276. HandleEvent(e);
  277. base.OnPointerReleased(e);
  278. }
  279. }
  280. }
  281. }