AxesTransfer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Numerics;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. namespace Veldrid.Common.Tools
  8. {
  9. internal static class AxesTransfer
  10. {
  11. public static PointF LocalPointToVirtualPoint<T>(this T render, Vector2 point) where T : BaseVeldridRender
  12. {
  13. return LocalPointToVirtualPoint(render.Margin, render.Range, render.WindowSize, point);
  14. }
  15. internal static PointF LocalPointToVirtualPoint(this BaseVeldridRender render, Vector2 point)
  16. {
  17. return LocalPointToVirtualPoint(render.Margin, render.Range, render.WindowSize, point);
  18. }
  19. private static PointF LocalPointToVirtualPoint(Padding margin, LineRange range, Vector2 windowsize, Vector2 point)
  20. {
  21. float left = point.X - margin.Left;
  22. float top = point.Y - margin.Top;
  23. left = range.XLenght / (windowsize.X - margin.Left - margin.Right) * left + range.MinX;
  24. top = range.YLenght / (windowsize.Y - margin.Top - margin.Bottom) * top;
  25. top = range.MaxY - top;
  26. return new PointF(left, top);
  27. }
  28. public static Vector2 LocalSizeToVirtualSize<T>(this T render, Vector2 size) where T : BaseVeldridRender
  29. {
  30. SizeF sizeF = render.LocalSizeToVirtualSize(size.X, size.Y);
  31. return Unsafe.As<SizeF, Vector2>(ref sizeF);
  32. }
  33. public static Vector2 VirtualSizeToLocalSize<T>(this T render, float width, float height) where T : BaseVeldridRender
  34. {
  35. return VirtualSizeToLocalSize(render.Margin, render.Range, render.WindowSize, width, height);
  36. }
  37. internal static Vector2 LocalSizeToVirtualSize(this BaseVeldridRender render, Vector2 size)
  38. {
  39. SizeF sizeF = render.LocalSizeToVirtualSize(size.X, size.Y);
  40. return Unsafe.As<SizeF, Vector2>(ref sizeF);
  41. }
  42. internal static Vector2 VirtualSizeToLocalSize(this BaseVeldridRender render, float width, float height)
  43. {
  44. return VirtualSizeToLocalSize(render.Margin, render.Range, render.WindowSize, width, height);
  45. }
  46. internal static Vector2 VirtualSizeToLocalSize(Padding margin, LineRange range, Vector2 windowsize, float width, float height)
  47. {
  48. Vector2 rect = new Vector2(windowsize.X - margin.Left - margin.Right, windowsize.Y - margin.Top - margin.Bottom);
  49. float w = width / range.XLenght * (rect.X / windowsize.X) * 2;
  50. float h = height / range.YLenght * (rect.Y / windowsize.Y) * 2;
  51. return new Vector2(w, h);
  52. }
  53. public static Vector2 VirtualPointToLocalPoint<T>(this T render, float x, float y) where T : BaseVeldridRender
  54. {
  55. return VirtualPointToLocalPoint(render.OrthographicMatrix, render.GetLineMatrix(), x, y);
  56. }
  57. internal static Vector2 VirtualPointToLocalPoint(this BaseVeldridRender render, float x, float y)
  58. {
  59. return VirtualPointToLocalPoint(render.OrthographicMatrix, render.GetLineMatrix(), x, y);
  60. }
  61. internal static Vector2 VirtualPointToLocalPoint(Matrix4x4 orth, Matrix4x4 view, float x, float y)
  62. {
  63. Matrix4x4 matrix = new Matrix4x4();
  64. matrix.M11 = x;
  65. matrix.M21 = y;
  66. matrix.M41 = 1;
  67. matrix = view * orth * matrix;
  68. return new Vector2(matrix.M11 - 1, matrix.M21 * 2 + 1);
  69. }
  70. public static SizeF LocalSizeToVirtualSize<T>(this T render, float width, float height) where T : BaseVeldridRender
  71. {
  72. return LocalSizeToVirtualSize(render.Rectangle, render.Range, width, height);
  73. }
  74. internal static SizeF LocalSizeToVirtualSize(this BaseVeldridRender render, float width, float height)
  75. {
  76. return LocalSizeToVirtualSize(render.Rectangle, render.Range, width, height);
  77. }
  78. private static SizeF LocalSizeToVirtualSize(RectangleF rect, LineRange range, float width, float height)
  79. {
  80. return new SizeF(width / rect.Width * (range.MaxX - range.MinX), height / rect.Height * (range.MaxY - range.MinY));
  81. }
  82. }
  83. }