TrackerControl.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="TrackerControl.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // The tracker control.
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. using Avalonia;
  10. namespace OxyPlot.Avalonia
  11. {
  12. using global::Avalonia.Controls;
  13. using global::Avalonia.Controls.Presenters;
  14. using global::Avalonia.Controls.Primitives;
  15. using global::Avalonia.Controls.Shapes;
  16. using global::Avalonia.Layout;
  17. using global::Avalonia.Media;
  18. using global::Avalonia.VisualTree;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. /// <summary>
  22. /// The tracker control.
  23. /// </summary>
  24. public class TrackerControl : ContentControl
  25. {
  26. /// <summary>
  27. /// Identifies the <see cref="HorizontalLineVisibility"/> dependency property.
  28. /// </summary>
  29. public static readonly StyledProperty<bool> HorizontalLineVisibilityProperty = AvaloniaProperty.Register<TrackerControl, bool>(nameof(HorizontalLineVisibility), true);
  30. /// <summary>
  31. /// Identifies the <see cref="VerticalLineVisibility"/> dependency property.
  32. /// </summary>
  33. public static readonly StyledProperty<bool> VerticalLineVisibilityProperty = AvaloniaProperty.Register<TrackerControl, bool>(nameof(VerticalLineVisibility), true);
  34. /// <summary>
  35. /// Identifies the <see cref="LineStroke"/> dependency property.
  36. /// </summary>
  37. public static readonly StyledProperty<IBrush> LineStrokeProperty = AvaloniaProperty.Register<TrackerControl, IBrush>(nameof(LineStroke));
  38. /// <summary>
  39. /// Identifies the <see cref="LineExtents"/> dependency property.
  40. /// </summary>
  41. public static readonly StyledProperty<OxyRect> LineExtentsProperty = AvaloniaProperty.Register<TrackerControl, OxyRect>(nameof(LineExtents), new OxyRect());
  42. /// <summary>
  43. /// Identifies the <see cref="LineDashArray"/> dependency property.
  44. /// </summary>
  45. public static readonly StyledProperty<List<double>> LineDashArrayProperty = AvaloniaProperty.Register<TrackerControl, List<double>>(nameof(LineDashArray));
  46. /// <summary>
  47. /// Identifies the <see cref="ShowPointer"/> dependency property.
  48. /// </summary>
  49. public static readonly StyledProperty<bool> ShowPointerProperty = AvaloniaProperty.Register<TrackerControl, bool>(nameof(ShowPointer), true);
  50. /// <summary>
  51. /// Identifies the <see cref="CornerRadius"/> dependency property.
  52. /// </summary>
  53. public static new readonly StyledProperty<double> CornerRadiusProperty = AvaloniaProperty.Register<TrackerControl, double>(nameof(CornerRadius), 0.0);
  54. /// <summary>
  55. /// Identifies the <see cref="Distance"/> dependency property.
  56. /// </summary>
  57. public static readonly StyledProperty<double> DistanceProperty = AvaloniaProperty.Register<TrackerControl, double>(nameof(Distance), 7.0);
  58. /// <summary>
  59. /// Identifies the <see cref="CanCenterHorizontally"/> dependency property.
  60. /// </summary>
  61. public static readonly StyledProperty<bool> CanCenterHorizontallyProperty = AvaloniaProperty.Register<TrackerControl, bool>(nameof(CanCenterHorizontally), true);
  62. /// <summary>
  63. /// Identifies the <see cref="CanCenterVertically"/> dependency property.
  64. /// </summary>
  65. public static readonly StyledProperty<bool> CanCenterVerticallyProperty = AvaloniaProperty.Register<TrackerControl, bool>(nameof(CanCenterVertically), true);
  66. /// <summary>
  67. /// Identifies the <see cref="Position"/> dependency property.
  68. /// </summary>
  69. public static readonly StyledProperty<ScreenPoint> PositionProperty = AvaloniaProperty.Register<TrackerControl, ScreenPoint>(nameof(Position), new ScreenPoint());
  70. /// <summary>
  71. /// The path part string.
  72. /// </summary>
  73. private const string PartPath = "PART_Path";
  74. /// <summary>
  75. /// The content part string.
  76. /// </summary>
  77. private const string PartContent = "PART_Content";
  78. /// <summary>
  79. /// The content container part string.
  80. /// </summary>
  81. private const string PartContentContainer = "PART_ContentContainer";
  82. /// <summary>
  83. /// The horizontal line part string.
  84. /// </summary>
  85. private const string PartHorizontalLine = "PART_HorizontalLine";
  86. /// <summary>
  87. /// The vertical line part string.
  88. /// </summary>
  89. private const string PartVerticalLine = "PART_VerticalLine";
  90. /// <summary>
  91. /// The content.
  92. /// </summary>
  93. private ContentPresenter content;
  94. /// <summary>
  95. /// The horizontal line.
  96. /// </summary>
  97. private Line horizontalLine;
  98. /// <summary>
  99. /// The path.
  100. /// </summary>
  101. private Path path;
  102. /// <summary>
  103. /// The content container.
  104. /// </summary>
  105. private Panel contentContainer;
  106. /// <summary>
  107. /// The vertical line.
  108. /// </summary>
  109. private Line verticalLine;
  110. /// <summary>
  111. /// Initializes static members of the <see cref = "TrackerControl" /> class.
  112. /// </summary>
  113. static TrackerControl()
  114. {
  115. ClipToBoundsProperty.OverrideDefaultValue<TrackerControl>(false);
  116. PositionProperty.Changed.AddClassHandler<TrackerControl>(PositionChanged);
  117. }
  118. /// <summary>
  119. /// Gets or sets HorizontalLineVisibility.
  120. /// </summary>
  121. public bool HorizontalLineVisibility
  122. {
  123. get
  124. {
  125. return GetValue(HorizontalLineVisibilityProperty);
  126. }
  127. set
  128. {
  129. SetValue(HorizontalLineVisibilityProperty, value);
  130. }
  131. }
  132. /// <summary>
  133. /// Gets or sets VerticalLineVisibility.
  134. /// </summary>
  135. public bool VerticalLineVisibility
  136. {
  137. get
  138. {
  139. return GetValue(VerticalLineVisibilityProperty);
  140. }
  141. set
  142. {
  143. SetValue(VerticalLineVisibilityProperty, value);
  144. }
  145. }
  146. /// <summary>
  147. /// Gets or sets LineStroke.
  148. /// </summary>
  149. public IBrush LineStroke
  150. {
  151. get
  152. {
  153. return GetValue(LineStrokeProperty);
  154. }
  155. set
  156. {
  157. SetValue(LineStrokeProperty, value);
  158. }
  159. }
  160. /// <summary>
  161. /// Gets or sets LineExtents.
  162. /// </summary>
  163. public OxyRect LineExtents
  164. {
  165. get
  166. {
  167. return GetValue(LineExtentsProperty);
  168. }
  169. set
  170. {
  171. SetValue(LineExtentsProperty, value);
  172. }
  173. }
  174. /// <summary>
  175. /// Gets or sets LineDashArray.
  176. /// </summary>
  177. public List<double> LineDashArray
  178. {
  179. get
  180. {
  181. return GetValue(LineDashArrayProperty);
  182. }
  183. set
  184. {
  185. SetValue(LineDashArrayProperty, value);
  186. }
  187. }
  188. /// <summary>
  189. /// Gets or sets a value indicating whether to show a 'pointer' on the border.
  190. /// </summary>
  191. public bool ShowPointer
  192. {
  193. get
  194. {
  195. return GetValue(ShowPointerProperty);
  196. }
  197. set
  198. {
  199. SetValue(ShowPointerProperty, value);
  200. }
  201. }
  202. /// <summary>
  203. /// Gets or sets the corner radius (only used when ShowPoint=<c>false</c>).
  204. /// </summary>
  205. public new double CornerRadius
  206. {
  207. get
  208. {
  209. return GetValue(CornerRadiusProperty);
  210. }
  211. set
  212. {
  213. SetValue(CornerRadiusProperty, value);
  214. }
  215. }
  216. /// <summary>
  217. /// Gets or sets the distance of the content container from the trackers Position.
  218. /// </summary>
  219. public double Distance
  220. {
  221. get
  222. {
  223. return GetValue(DistanceProperty);
  224. }
  225. set
  226. {
  227. SetValue(DistanceProperty, value);
  228. }
  229. }
  230. /// <summary>
  231. /// Gets or sets a value indicating whether the tracker can center its content box horizontally.
  232. /// </summary>
  233. public bool CanCenterHorizontally
  234. {
  235. get
  236. {
  237. return GetValue(CanCenterHorizontallyProperty);
  238. }
  239. set
  240. {
  241. SetValue(CanCenterHorizontallyProperty, value);
  242. }
  243. }
  244. /// <summary>
  245. /// Gets or sets a value indicating whether the tracker can center its content box vertically.
  246. /// </summary>
  247. public bool CanCenterVertically
  248. {
  249. get
  250. {
  251. return GetValue(CanCenterVerticallyProperty);
  252. }
  253. set
  254. {
  255. SetValue(CanCenterVerticallyProperty, value);
  256. }
  257. }
  258. /// <summary>
  259. /// Gets or sets Position of the tracker.
  260. /// </summary>
  261. public ScreenPoint Position
  262. {
  263. get
  264. {
  265. return GetValue(PositionProperty);
  266. }
  267. set
  268. {
  269. SetValue(PositionProperty, value);
  270. }
  271. }
  272. protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
  273. {
  274. base.OnApplyTemplate(e);
  275. path = e.NameScope.Get<Path>(PartPath);
  276. content = e.NameScope.Get<ContentPresenter>(PartContent);
  277. contentContainer = e.NameScope.Get<Panel>(PartContentContainer);
  278. horizontalLine = e.NameScope.Find<Line>(PartHorizontalLine);
  279. verticalLine = e.NameScope.Find<Line>(PartVerticalLine);
  280. UpdatePositionAndBorder();
  281. }
  282. /// <summary>
  283. /// Called when the position is changed.
  284. /// </summary>
  285. /// <param name="sender">The sender.</param>
  286. /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
  287. private static void PositionChanged(AvaloniaObject sender, AvaloniaPropertyChangedEventArgs e)
  288. {
  289. ((TrackerControl)sender).OnPositionChanged(e);
  290. }
  291. /// <summary>
  292. /// Called when the position is changed.
  293. /// </summary>
  294. /// <param name="dependencyPropertyChangedEventArgs">The dependency property changed event args.</param>
  295. // ReSharper disable once UnusedParameter.Local
  296. private void OnPositionChanged(AvaloniaPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
  297. {
  298. UpdatePositionAndBorder();
  299. }
  300. /// <summary>
  301. /// Update the position and border of the tracker.
  302. /// </summary>
  303. private void UpdatePositionAndBorder()
  304. {
  305. if (contentContainer == null)
  306. {
  307. return;
  308. }
  309. var pos = this.Position;
  310. var lineExtents = this.LineExtents;
  311. Canvas.SetLeft(this, pos.X);
  312. Canvas.SetTop(this, pos.Y);
  313. Control parent = this;
  314. while (!(parent is Canvas) && parent != null)
  315. {
  316. parent = parent.GetVisualParent() as Control;
  317. }
  318. if (parent == null)
  319. {
  320. return;
  321. }
  322. // throw new InvalidOperationException("The TrackerControl must have a Canvas parent.");
  323. var canvasWidth = parent.Bounds.Width;
  324. var canvasHeight = parent.Bounds.Height;
  325. content.Measure(new Size(canvasWidth, canvasHeight));
  326. content.Arrange(new Rect(0, 0, content.DesiredSize.Width, content.DesiredSize.Height));
  327. var contentWidth = content.DesiredSize.Width;
  328. var contentHeight = content.DesiredSize.Height;
  329. // Minimum allowed margins around the tracker
  330. const double MarginLimit = 10;
  331. var ha = HorizontalAlignment.Center;
  332. if (CanCenterHorizontally)
  333. {
  334. if (pos.X - (contentWidth / 2) < MarginLimit)
  335. {
  336. ha = HorizontalAlignment.Left;
  337. }
  338. if (pos.X + (contentWidth / 2) > canvasWidth - MarginLimit)
  339. {
  340. ha = HorizontalAlignment.Right;
  341. }
  342. }
  343. else
  344. {
  345. ha = pos.X < canvasWidth / 2 ? HorizontalAlignment.Left : HorizontalAlignment.Right;
  346. }
  347. var va = VerticalAlignment.Center;
  348. if (CanCenterVertically)
  349. {
  350. if (pos.Y - (contentHeight / 2) < MarginLimit)
  351. {
  352. va = VerticalAlignment.Top;
  353. }
  354. if (ha == HorizontalAlignment.Center)
  355. {
  356. va = VerticalAlignment.Bottom;
  357. if (pos.Y - contentHeight < MarginLimit)
  358. {
  359. va = VerticalAlignment.Top;
  360. }
  361. }
  362. if (va == VerticalAlignment.Center && pos.Y + (contentHeight / 2) > canvasHeight - MarginLimit)
  363. {
  364. va = VerticalAlignment.Bottom;
  365. }
  366. if (va == VerticalAlignment.Top && pos.Y + contentHeight > canvasHeight - MarginLimit)
  367. {
  368. va = VerticalAlignment.Bottom;
  369. }
  370. }
  371. else
  372. {
  373. va = pos.Y < canvasHeight / 2 ? VerticalAlignment.Top : VerticalAlignment.Bottom;
  374. }
  375. var dx = ha == HorizontalAlignment.Center ? -0.5 : ha == HorizontalAlignment.Left ? 0 : -1;
  376. var dy = va == VerticalAlignment.Center ? -0.5 : va == VerticalAlignment.Top ? 0 : -1;
  377. path.Data = ShowPointer ? CreatePointerBorderGeometry(ha, va, contentWidth, contentHeight, out var margin)
  378. : CreateBorderGeometry(ha, va, contentWidth, contentHeight, out margin);
  379. content.Margin = margin;
  380. contentContainer.Measure(new Size(canvasWidth, canvasHeight));
  381. var contentSize = contentContainer.DesiredSize;
  382. contentContainer.RenderTransform = new TranslateTransform
  383. {
  384. X = dx * contentSize.Width,
  385. Y = dy * contentSize.Height
  386. };
  387. if (horizontalLine != null)
  388. {
  389. horizontalLine.StartPoint = new Point(lineExtents.Left - pos.X, 0);
  390. horizontalLine.EndPoint = new Point(lineExtents.Right - pos.X, 0);
  391. }
  392. if (verticalLine != null)
  393. {
  394. verticalLine.StartPoint = new Point(0, lineExtents.Top - pos.Y);
  395. verticalLine.EndPoint = new Point(0, lineExtents.Bottom - pos.Y);
  396. }
  397. }
  398. /// <summary>
  399. /// Create the border geometry.
  400. /// </summary>
  401. /// <param name="ha">The horizontal alignment.</param>
  402. /// <param name="va">The vertical alignment.</param>
  403. /// <param name="width">The width.</param>
  404. /// <param name="height">The height.</param>
  405. /// <param name="margin">The margin.</param>
  406. /// <returns>The border geometry.</returns>
  407. private Geometry CreateBorderGeometry(
  408. HorizontalAlignment ha, VerticalAlignment va, double width, double height, out Thickness margin)
  409. {
  410. var m = Distance;
  411. var rect = new Rect(
  412. ha == HorizontalAlignment.Left ? m : 0, va == VerticalAlignment.Top ? m : 0, width, height);
  413. margin = new Thickness(
  414. ha == HorizontalAlignment.Left ? m : 0,
  415. va == VerticalAlignment.Top ? m : 0,
  416. ha == HorizontalAlignment.Right ? m : 0,
  417. va == VerticalAlignment.Bottom ? m : 0);
  418. return new RectangleGeometry(rect)/* { RadiusX = this.CornerRadius, RadiusY = this.CornerRadius }*/;
  419. }
  420. /// <summary>
  421. /// Create a border geometry with a 'pointer'.
  422. /// </summary>
  423. /// <param name="ha">The horizontal alignment.</param>
  424. /// <param name="va">The vertical alignment.</param>
  425. /// <param name="width">The width.</param>
  426. /// <param name="height">The height.</param>
  427. /// <param name="margin">The margin.</param>
  428. /// <returns>The border geometry.</returns>
  429. private Geometry CreatePointerBorderGeometry(
  430. HorizontalAlignment ha, VerticalAlignment va, double width, double height, out Thickness margin)
  431. {
  432. Point[] points = null;
  433. var m = Distance;
  434. margin = new Thickness();
  435. if (ha == HorizontalAlignment.Center && va == VerticalAlignment.Bottom)
  436. {
  437. var x1 = width;
  438. var x2 = x1 / 2;
  439. var y1 = height;
  440. margin = new Thickness(0, 0, 0, m);
  441. points = new[]
  442. {
  443. new Point(0, 0), new Point(x1, 0), new Point(x1, y1), new Point(x2 + (m / 2), y1),
  444. new Point(x2, y1 + m), new Point(x2 - (m / 2), y1), new Point(0, y1)
  445. };
  446. }
  447. else if (ha == HorizontalAlignment.Center && va == VerticalAlignment.Top)
  448. {
  449. var x1 = width;
  450. var x2 = x1 / 2;
  451. var y0 = m;
  452. var y1 = m + height;
  453. margin = new Thickness(0, m, 0, 0);
  454. points = new[]
  455. {
  456. new Point(0, y0), new Point(x2 - (m / 2), y0), new Point(x2, 0), new Point(x2 + (m / 2), y0),
  457. new Point(x1, y0), new Point(x1, y1), new Point(0, y1)
  458. };
  459. }
  460. else if (ha == HorizontalAlignment.Left && va == VerticalAlignment.Center)
  461. {
  462. var x0 = m;
  463. var x1 = m + width;
  464. var y1 = height;
  465. var y2 = y1 / 2;
  466. margin = new Thickness(m, 0, 0, 0);
  467. points = new[]
  468. {
  469. new Point(0, y2), new Point(x0, y2 - (m / 2)), new Point(x0, 0), new Point(x1, 0),
  470. new Point(x1, y1), new Point(x0, y1), new Point(x0, y2 + (m / 2))
  471. };
  472. }
  473. else if (ha == HorizontalAlignment.Right && va == VerticalAlignment.Center)
  474. {
  475. var x1 = width;
  476. var y1 = height;
  477. var y2 = y1 / 2;
  478. margin = new Thickness(0, 0, m, 0);
  479. points = new[]
  480. {
  481. new Point(x1 + m, y2), new Point(x1, y2 + (m / 2)), new Point(x1, y1), new Point(0, y1),
  482. new Point(0, 0), new Point(x1, 0), new Point(x1, y2 - (m / 2))
  483. };
  484. }
  485. else if (ha == HorizontalAlignment.Left && va == VerticalAlignment.Top)
  486. {
  487. m *= 0.67;
  488. var x0 = m;
  489. var x1 = m + width;
  490. var y0 = m;
  491. var y1 = m + height;
  492. margin = new Thickness(m, m, 0, 0);
  493. points = new[]
  494. {
  495. new Point(0, 0), new Point(m * 2, y0), new Point(x1, y0), new Point(x1, y1), new Point(x0, y1),
  496. new Point(x0, m * 2)
  497. };
  498. }
  499. else if (ha == HorizontalAlignment.Right && va == VerticalAlignment.Top)
  500. {
  501. m *= 0.67;
  502. var x1 = width;
  503. var y0 = m;
  504. var y1 = m + height;
  505. margin = new Thickness(0, m, m, 0);
  506. points = new[]
  507. {
  508. new Point(x1 + m, 0), new Point(x1, y0 + m), new Point(x1, y1), new Point(0, y1),
  509. new Point(0, y0), new Point(x1 - m, y0)
  510. };
  511. }
  512. else if (ha == HorizontalAlignment.Left && va == VerticalAlignment.Bottom)
  513. {
  514. m *= 0.67;
  515. var x0 = m;
  516. var x1 = m + width;
  517. var y1 = height;
  518. margin = new Thickness(m, 0, 0, m);
  519. points = new[]
  520. {
  521. new Point(0, y1 + m), new Point(x0, y1 - m), new Point(x0, 0), new Point(x1, 0),
  522. new Point(x1, y1), new Point(x0 + m, y1)
  523. };
  524. }
  525. else if (ha == HorizontalAlignment.Right && va == VerticalAlignment.Bottom)
  526. {
  527. m *= 0.67;
  528. var x1 = width;
  529. var y1 = height;
  530. margin = new Thickness(0, 0, m, m);
  531. points = new[]
  532. {
  533. new Point(x1 + m, y1 + m), new Point(x1 - m, y1), new Point(0, y1), new Point(0, 0),
  534. new Point(x1, 0), new Point(x1, y1 - m)
  535. };
  536. }
  537. if (points == null)
  538. {
  539. return null;
  540. }
  541. var pc = new List<Point>(points.Length);
  542. foreach (var p in points)
  543. {
  544. pc.Add(p);
  545. }
  546. var segments = new PathSegments();
  547. segments.AddRange(pc.Select(p => new LineSegment { Point = p }));
  548. var pf = new PathFigure { StartPoint = points[0], Segments = segments, IsClosed = true };
  549. return new PathGeometry { Figures = new PathFigures { pf } };
  550. }
  551. }
  552. }