ConverterExtensions.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="ConverterExtensions.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // Extension method used to convert to/from Windows/Windows.Media classes.
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. namespace OxyPlot.Avalonia
  10. {
  11. using global::Avalonia;
  12. using global::Avalonia.Input;
  13. using global::Avalonia.Media;
  14. using global::Avalonia.Media.Immutable;
  15. using System;
  16. /// <summary>
  17. /// Extension method used to convert to/from Windows/Windows.Media classes.
  18. /// </summary>
  19. public static class ConverterExtensions
  20. {
  21. /// <summary>
  22. /// Calculate the distance between two points.
  23. /// </summary>
  24. /// <param name="p1">The first point.</param>
  25. /// <param name="p2">The second point.</param>
  26. /// <returns>The distance.</returns>
  27. public static double DistanceTo(this Point p1, Point p2)
  28. {
  29. var dx = p1.X - p2.X;
  30. var dy = p1.Y - p2.Y;
  31. return Math.Sqrt((dx * dx) + (dy * dy));
  32. }
  33. /// <summary>
  34. /// Converts an <see cref="OxyColor" /> to a <see cref="Brush" />.
  35. /// </summary>
  36. /// <param name="c">The color.</param>
  37. /// <returns>A <see cref="SolidColorBrush" />.</returns>
  38. public static IBrush ToBrush(this OxyColor c)
  39. {
  40. return !c.IsUndefined() ? new SolidColorBrush(c.ToColor()) : null;
  41. }
  42. /// <summary>
  43. /// Converts an <see cref="OxyColor" /> to a <see cref="Color" />.
  44. /// </summary>
  45. /// <param name="c">The color.</param>
  46. /// <returns>A Color.</returns>
  47. public static Color ToColor(this OxyColor c)
  48. {
  49. return Color.FromArgb(c.A, c.R, c.G, c.B);
  50. }
  51. /// <summary>
  52. /// Converts an OxyThickness to a Thickness.
  53. /// </summary>
  54. /// <param name="c">The thickness.</param>
  55. /// <returns>A <see cref="Thickness" /> instance.</returns>
  56. public static Thickness ToThickness(this OxyThickness c)
  57. {
  58. return new Thickness(c.Left, c.Top, c.Right, c.Bottom);
  59. }
  60. /// <summary>
  61. /// Converts a ScreenVector to a Vector.
  62. /// </summary>
  63. /// <param name="c">The c.</param>
  64. /// <returns>A <see cref="Vector" /> instance.</returns>
  65. public static Vector ToVector(this ScreenVector c)
  66. {
  67. return new Vector(c.X, c.Y);
  68. }
  69. /// <summary>
  70. /// Converts a HorizontalAlignment to a HorizontalAlignment.
  71. /// </summary>
  72. /// <param name="alignment">The alignment.</param>
  73. /// <returns>A HorizontalAlignment.</returns>
  74. public static HorizontalAlignment ToHorizontalAlignment(this global::Avalonia.Layout.HorizontalAlignment alignment)
  75. {
  76. switch (alignment)
  77. {
  78. case global::Avalonia.Layout.HorizontalAlignment.Center:
  79. return HorizontalAlignment.Center;
  80. case global::Avalonia.Layout.HorizontalAlignment.Right:
  81. return HorizontalAlignment.Right;
  82. default:
  83. return HorizontalAlignment.Left;
  84. }
  85. }
  86. /// <summary>
  87. /// Converts a HorizontalAlignment to a VerticalAlignment.
  88. /// </summary>
  89. /// <param name="alignment">The alignment.</param>
  90. /// <returns>A VerticalAlignment.</returns>
  91. public static VerticalAlignment ToVerticalAlignment(this global::Avalonia.Layout.VerticalAlignment alignment)
  92. {
  93. switch (alignment)
  94. {
  95. case global::Avalonia.Layout.VerticalAlignment.Center:
  96. return VerticalAlignment.Middle;
  97. case global::Avalonia.Layout.VerticalAlignment.Top:
  98. return VerticalAlignment.Top;
  99. default:
  100. return VerticalAlignment.Bottom;
  101. }
  102. }
  103. /// <summary>
  104. /// Converts a Color to an OxyColor.
  105. /// </summary>
  106. /// <param name="color">The color.</param>
  107. /// <returns>An OxyColor.</returns>
  108. public static OxyColor ToOxyColor(this Color color)
  109. {
  110. return OxyColor.FromArgb(color.A, color.R, color.G, color.B);
  111. }
  112. /// <summary>
  113. /// Converts a <see cref="Brush" /> to an <see cref="OxyColor" />.
  114. /// </summary>
  115. /// <param name="brush">The brush.</param>
  116. /// <returns>An <see cref="OxyColor" />.</returns>
  117. public static OxyColor ToOxyColor(this IBrush brush)
  118. {
  119. if (brush is ImmutableSolidColorBrush iscb)
  120. {
  121. return iscb.Color.ToOxyColor();
  122. }
  123. else if (brush is SolidColorBrush scb)
  124. {
  125. return scb.Color.ToOxyColor();
  126. }
  127. return OxyColors.Undefined;
  128. }
  129. /// <summary>
  130. /// Converts a Thickness to an <see cref="OxyThickness" />.
  131. /// </summary>
  132. /// <param name="t">The thickness.</param>
  133. /// <returns>An <see cref="OxyThickness" />.</returns>
  134. public static OxyThickness ToOxyThickness(this Thickness t)
  135. {
  136. return new OxyThickness(t.Left, t.Top, t.Right, t.Bottom);
  137. }
  138. /// <summary>
  139. /// Converts a <see cref="Point" /> to a <see cref="ScreenPoint" />.
  140. /// </summary>
  141. /// <param name="pt">The point.</param>
  142. /// <returns>A <see cref="ScreenPoint" />.</returns>
  143. public static ScreenPoint ToScreenPoint(this Point pt)
  144. {
  145. return new ScreenPoint(pt.X, pt.Y);
  146. }
  147. /// <summary>
  148. /// Converts a Point array to a ScreenPoint array.
  149. /// </summary>
  150. /// <param name="points">The points.</param>
  151. /// <returns>A ScreenPoint array.</returns>
  152. public static ScreenPoint[] ToScreenPointArray(this Point[] points)
  153. {
  154. if (points == null)
  155. {
  156. return null;
  157. }
  158. var pts = new ScreenPoint[points.Length];
  159. for (int i = 0; i < points.Length; i++)
  160. {
  161. pts[i] = points[i].ToScreenPoint();
  162. }
  163. return pts;
  164. }
  165. /// <summary>
  166. /// Converts the specified vector to a ScreenVector.
  167. /// </summary>
  168. /// <param name="vector">The vector.</param>
  169. /// <returns>A <see cref="ScreenVector" />.</returns>
  170. public static ScreenVector ToScreenVector(this Vector vector)
  171. {
  172. return new ScreenVector(vector.X, vector.Y);
  173. }
  174. /// <summary>
  175. /// Converts the specified key.
  176. /// </summary>
  177. /// <param name="k">The key to convert.</param>
  178. /// <returns>The converted key.</returns>
  179. public static OxyKey Convert(this Key k)
  180. {
  181. switch (k)
  182. {
  183. case Key.A:
  184. return OxyKey.A;
  185. case Key.Add:
  186. return OxyKey.Add;
  187. case Key.B:
  188. return OxyKey.B;
  189. case Key.Back:
  190. return OxyKey.Backspace;
  191. case Key.C:
  192. return OxyKey.C;
  193. case Key.D:
  194. return OxyKey.D;
  195. case Key.D0:
  196. return OxyKey.D0;
  197. case Key.D1:
  198. return OxyKey.D1;
  199. case Key.D2:
  200. return OxyKey.D2;
  201. case Key.D3:
  202. return OxyKey.D3;
  203. case Key.D4:
  204. return OxyKey.D4;
  205. case Key.D5:
  206. return OxyKey.D5;
  207. case Key.D6:
  208. return OxyKey.D6;
  209. case Key.D7:
  210. return OxyKey.D7;
  211. case Key.D8:
  212. return OxyKey.D8;
  213. case Key.D9:
  214. return OxyKey.D9;
  215. case Key.Decimal:
  216. return OxyKey.Decimal;
  217. case Key.Delete:
  218. return OxyKey.Delete;
  219. case Key.Divide:
  220. return OxyKey.Divide;
  221. case Key.Down:
  222. return OxyKey.Down;
  223. case Key.E:
  224. return OxyKey.E;
  225. case Key.End:
  226. return OxyKey.End;
  227. case Key.Enter:
  228. return OxyKey.Enter;
  229. case Key.Escape:
  230. return OxyKey.Escape;
  231. case Key.F:
  232. return OxyKey.F;
  233. case Key.F1:
  234. return OxyKey.F1;
  235. case Key.F10:
  236. return OxyKey.F10;
  237. case Key.F11:
  238. return OxyKey.F11;
  239. case Key.F12:
  240. return OxyKey.F12;
  241. case Key.F2:
  242. return OxyKey.F2;
  243. case Key.F3:
  244. return OxyKey.F3;
  245. case Key.F4:
  246. return OxyKey.F4;
  247. case Key.F5:
  248. return OxyKey.F5;
  249. case Key.F6:
  250. return OxyKey.F6;
  251. case Key.F7:
  252. return OxyKey.F7;
  253. case Key.F8:
  254. return OxyKey.F8;
  255. case Key.F9:
  256. return OxyKey.F9;
  257. case Key.G:
  258. return OxyKey.G;
  259. case Key.H:
  260. return OxyKey.H;
  261. case Key.Home:
  262. return OxyKey.Home;
  263. case Key.I:
  264. return OxyKey.I;
  265. case Key.Insert:
  266. return OxyKey.Insert;
  267. case Key.J:
  268. return OxyKey.J;
  269. case Key.K:
  270. return OxyKey.K;
  271. case Key.L:
  272. return OxyKey.L;
  273. case Key.Left:
  274. return OxyKey.Left;
  275. case Key.M:
  276. return OxyKey.M;
  277. case Key.Multiply:
  278. return OxyKey.Multiply;
  279. case Key.N:
  280. return OxyKey.N;
  281. case Key.NumPad0:
  282. return OxyKey.NumPad0;
  283. case Key.NumPad1:
  284. return OxyKey.NumPad1;
  285. case Key.NumPad2:
  286. return OxyKey.NumPad2;
  287. case Key.NumPad3:
  288. return OxyKey.NumPad3;
  289. case Key.NumPad4:
  290. return OxyKey.NumPad4;
  291. case Key.NumPad5:
  292. return OxyKey.NumPad5;
  293. case Key.NumPad6:
  294. return OxyKey.NumPad6;
  295. case Key.NumPad7:
  296. return OxyKey.NumPad7;
  297. case Key.NumPad8:
  298. return OxyKey.NumPad8;
  299. case Key.NumPad9:
  300. return OxyKey.NumPad9;
  301. case Key.O:
  302. return OxyKey.O;
  303. case Key.P:
  304. return OxyKey.P;
  305. case Key.PageDown:
  306. return OxyKey.PageDown;
  307. case Key.PageUp:
  308. return OxyKey.PageUp;
  309. case Key.Q:
  310. return OxyKey.Q;
  311. case Key.R:
  312. return OxyKey.R;
  313. case Key.Right:
  314. return OxyKey.Right;
  315. case Key.S:
  316. return OxyKey.S;
  317. case Key.Space:
  318. return OxyKey.Space;
  319. case Key.Subtract:
  320. return OxyKey.Subtract;
  321. case Key.T:
  322. return OxyKey.T;
  323. case Key.Tab:
  324. return OxyKey.Tab;
  325. case Key.U:
  326. return OxyKey.U;
  327. case Key.Up:
  328. return OxyKey.Up;
  329. case Key.V:
  330. return OxyKey.V;
  331. case Key.W:
  332. return OxyKey.W;
  333. case Key.X:
  334. return OxyKey.X;
  335. case Key.Y:
  336. return OxyKey.Y;
  337. case Key.Z:
  338. return OxyKey.Z;
  339. default:
  340. return OxyKey.Unknown;
  341. }
  342. }
  343. /// <summary>
  344. /// Converts the specified button.
  345. /// </summary>
  346. /// <param name="button">The button to convert.</param>
  347. /// <returns>The converted mouse button.</returns>
  348. [Obsolete]
  349. public static OxyMouseButton Convert(this MouseButton button)
  350. {
  351. switch (button)
  352. {
  353. case MouseButton.Left:
  354. return OxyMouseButton.Left;
  355. case MouseButton.Middle:
  356. return OxyMouseButton.Middle;
  357. case MouseButton.Right:
  358. return OxyMouseButton.Right;
  359. default:
  360. return OxyMouseButton.None;
  361. }
  362. }
  363. public static OxyMouseButton Convert(this PointerUpdateKind pointerUpdateKind)
  364. {
  365. switch (pointerUpdateKind)
  366. {
  367. case PointerUpdateKind.LeftButtonPressed:
  368. return OxyMouseButton.Left;
  369. case PointerUpdateKind.MiddleButtonPressed:
  370. return OxyMouseButton.Middle;
  371. case PointerUpdateKind.RightButtonPressed:
  372. return OxyMouseButton.Right;
  373. case PointerUpdateKind.XButton1Pressed:
  374. return OxyMouseButton.XButton1;
  375. case PointerUpdateKind.XButton2Pressed:
  376. return OxyMouseButton.XButton2;
  377. default:
  378. return OxyMouseButton.None;
  379. }
  380. }
  381. public static OxyModifierKeys ToModifierKeys(this KeyModifiers modifiers)
  382. {
  383. var modifierKeys = OxyModifierKeys.None;
  384. if ((modifiers & KeyModifiers.Shift) != 0)
  385. {
  386. modifierKeys |= OxyModifierKeys.Shift;
  387. }
  388. if ((modifiers & KeyModifiers.Control) != 0)
  389. {
  390. modifierKeys |= OxyModifierKeys.Control;
  391. }
  392. if ((modifiers & KeyModifiers.Alt) != 0)
  393. {
  394. modifierKeys |= OxyModifierKeys.Alt;
  395. }
  396. if ((modifiers & KeyModifiers.Meta) != 0)
  397. {
  398. modifierKeys |= OxyModifierKeys.Windows;
  399. }
  400. return modifierKeys;
  401. }
  402. /// <summary>
  403. /// Converts <see cref="MouseWheelEventArgs" /> to <see cref="OxyMouseWheelEventArgs" /> for a mouse wheel event.
  404. /// </summary>
  405. /// <param name="e">The <see cref="MouseWheelEventArgs" /> instance containing the event data.</param>
  406. /// <param name="relativeTo">The <see cref="IInputElement" /> that the event is relative to.</param>
  407. /// <returns>A <see cref="OxyMouseWheelEventArgs" /> containing the converted event arguments.</returns>
  408. public static OxyMouseWheelEventArgs ToMouseWheelEventArgs(this PointerWheelEventArgs e, IInputElement relativeTo)
  409. {
  410. return new OxyMouseWheelEventArgs
  411. {
  412. Position = e.GetPosition(relativeTo as Visual).ToScreenPoint(),
  413. ModifierKeys = e.KeyModifiers.ToModifierKeys(),
  414. Delta = (int)(e.Delta.Y + e.Delta.X) * 120
  415. };
  416. }
  417. /// <summary>
  418. /// Converts <see cref="MouseButtonEventArgs" /> to <see cref="OxyMouseEventArgs" /> for a mouse down event.
  419. /// </summary>
  420. /// <param name="e">The <see cref="MouseButtonEventArgs" /> instance containing the event data.</param>
  421. /// <param name="relativeTo">The <see cref="IInputElement" /> that the event is relative to.</param>
  422. /// <returns>A <see cref="OxyMouseEventArgs" /> containing the converted event arguments.</returns>
  423. public static OxyMouseDownEventArgs ToMouseDownEventArgs(this PointerPressedEventArgs e, IInputElement relativeTo)
  424. {
  425. var point = e.GetCurrentPoint(relativeTo as Visual);
  426. return new OxyMouseDownEventArgs
  427. {
  428. ChangedButton = point.Properties.PointerUpdateKind.Convert(),
  429. #pragma warning disable CS0618 // Type or member is obsolete
  430. ClickCount = e.ClickCount,
  431. #pragma warning restore CS0618 // Type or member is obsolete
  432. Position = e.GetPosition(relativeTo as Visual).ToScreenPoint(),
  433. ModifierKeys = e.KeyModifiers.ToModifierKeys()
  434. };
  435. }
  436. /// <summary>
  437. /// Converts <see cref="MouseButtonEventArgs" /> to <see cref="OxyMouseEventArgs" /> for a mouse up event.
  438. /// </summary>
  439. /// <param name="e">The <see cref="MouseButtonEventArgs" /> instance containing the event data.</param>
  440. /// <param name="relativeTo">The <see cref="IInputElement" /> that the event is relative to.</param>
  441. /// <returns>A <see cref="OxyMouseEventArgs" /> containing the converted event arguments.</returns>
  442. public static OxyMouseEventArgs ToMouseReleasedEventArgs(this PointerReleasedEventArgs e, IInputElement relativeTo)
  443. {
  444. return new OxyMouseEventArgs
  445. {
  446. Position = e.GetPosition(relativeTo as Visual).ToScreenPoint(),
  447. ModifierKeys = e.KeyModifiers.ToModifierKeys()
  448. };
  449. }
  450. /// <summary>
  451. /// Converts <see cref="MouseEventArgs" /> to <see cref="OxyMouseEventArgs" /> for a mouse event.
  452. /// </summary>
  453. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  454. /// <param name="relativeTo">The <see cref="IInputElement" /> that the event is relative to.</param>
  455. /// <returns>A <see cref="OxyMouseEventArgs" /> containing the converted event arguments.</returns>
  456. public static OxyMouseEventArgs ToMouseEventArgs(this PointerEventArgs e, IInputElement relativeTo)
  457. {
  458. return new OxyMouseEventArgs
  459. {
  460. Position = e.GetPosition(relativeTo as Visual).ToScreenPoint(),
  461. ModifierKeys = e.KeyModifiers.ToModifierKeys()
  462. };
  463. }
  464. /** Touch Events currently not supported in Avalonia
  465. /// <summary>
  466. /// Converts <see cref="ManipulationStartedEventArgs" /> to <see cref="OxyMouseEventArgs" /> for a touch started event.
  467. /// </summary>
  468. /// <param name="e">The <see cref="ManipulationStartedEventArgs" /> instance containing the event data.</param>
  469. /// <param name="relativeTo">The <see cref="UIElement" /> that the event is relative to.</param>
  470. /// <returns>A <see cref="OxyMouseEventArgs" /> containing the converted event arguments.</returns>
  471. public static OxyTouchEventArgs ToTouchEventArgs(this ManipulationStartedEventArgs e, UIElement relativeTo)
  472. {
  473. return new OxyTouchEventArgs
  474. {
  475. Position = e.ManipulationOrigin.ToScreenPoint(),
  476. };
  477. }
  478. /// <summary>
  479. /// Converts <see cref="ManipulationDeltaEventArgs" /> to <see cref="OxyMouseEventArgs" /> for a touch delta event.
  480. /// </summary>
  481. /// <param name="e">The <see cref="ManipulationDeltaEventArgs" /> instance containing the event data.</param>
  482. /// <param name="relativeTo">The <see cref="UIElement" /> that the event is relative to.</param>
  483. /// <returns>A <see cref="OxyMouseEventArgs" /> containing the converted event arguments.</returns>
  484. public static OxyTouchEventArgs ToTouchEventArgs(this ManipulationDeltaEventArgs e, UIElement relativeTo)
  485. {
  486. return new OxyTouchEventArgs
  487. {
  488. Position = e.ManipulationOrigin.ToScreenPoint(),
  489. DeltaTranslation = e.DeltaManipulation.Translation.ToScreenVector(),
  490. DeltaScale = e.DeltaManipulation.Scale.ToScreenVector()
  491. };
  492. }
  493. /// <summary>
  494. /// Converts <see cref="ManipulationCompletedEventArgs" /> to <see cref="OxyMouseEventArgs" /> for a touch completed event.
  495. /// </summary>
  496. /// <param name="e">The <see cref="ManipulationCompletedEventArgs" /> instance containing the event data.</param>
  497. /// <param name="relativeTo">The <see cref="UIElement" /> that the event is relative to.</param>
  498. /// <returns>A <see cref="OxyMouseEventArgs" /> containing the converted event arguments.</returns>
  499. public static OxyTouchEventArgs ToTouchEventArgs(this ManipulationCompletedEventArgs e, UIElement relativeTo)
  500. {
  501. return new OxyTouchEventArgs
  502. {
  503. Position = e.ManipulationOrigin.ToScreenPoint()
  504. };
  505. }
  506. */
  507. }
  508. }