PointersPage.cs 10 KB

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