Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
5.2KB

  1. using System.Linq;
  2. using UnityEditor;
  3. using UnityEngine;
  4. //This profiler is based on the Entitas Visual Debugging tool
  5. //https://github.com/sschmid/Entitas-CSharp
  6. namespace Svelto.ECS.Profiler
  7. {
  8. public class EnginesMonitor
  9. {
  10. public float xBorder = 48;
  11. public float yBorder = 20;
  12. public int rightLinePadding = -15;
  13. public string labelFormat = "{0:0.0}";
  14. public string axisFormat = "{0:0.0}";
  15. public int gridLines = 1;
  16. public float axisRounding = 5f;
  17. public float anchorRadius = 1f;
  18. public Color lineColor = Color.magenta;
  19. readonly GUIStyle _labelTextStyle;
  20. readonly GUIStyle _centeredStyle;
  21. readonly Vector3[] _cachedLinePointVerticies;
  22. readonly Vector3[] _linePoints;
  23. public EnginesMonitor(int dataLength)
  24. {
  25. _labelTextStyle = new GUIStyle(GUI.skin.label);
  26. _labelTextStyle.alignment = TextAnchor.UpperRight;
  27. _centeredStyle = new GUIStyle();
  28. _centeredStyle.alignment = TextAnchor.UpperCenter;
  29. _centeredStyle.normal.textColor = Color.white;
  30. _linePoints = new Vector3[dataLength];
  31. _cachedLinePointVerticies = new[]
  32. {
  33. new Vector3(-1, 1, 0)*anchorRadius,
  34. new Vector3(1, 1, 0)*anchorRadius,
  35. new Vector3(1, -1, 0)*anchorRadius,
  36. new Vector3(-1, -1, 0)*anchorRadius,
  37. };
  38. }
  39. public void Draw(float[] data, float height, float axisUpperBounds)
  40. {
  41. axisRounding = axisUpperBounds;
  42. var rect = GUILayoutUtility.GetRect(EditorGUILayout.GetControlRect().width, height);
  43. var top = rect.y + yBorder;
  44. var floor = rect.y + rect.height - yBorder;
  45. var availableHeight = floor - top;
  46. var max = data.Length != 0 ? data.Max() : 0f;
  47. if (max%axisRounding != 0)
  48. {
  49. max = max + axisRounding - (max%axisRounding);
  50. }
  51. drawGridLines(top, rect.width, availableHeight, max);
  52. drawLine(data, floor, rect.width, availableHeight, max);
  53. }
  54. void drawGridLines(float top, float width, float availableHeight, float max)
  55. {
  56. var handleColor = Handles.color;
  57. Handles.color = Color.grey;
  58. var n = gridLines + 1;
  59. var lineSpacing = availableHeight/n;
  60. for (int i = 0; i <= n; i++)
  61. {
  62. var lineY = top + (lineSpacing*i);
  63. Handles.DrawLine(
  64. new Vector2(xBorder, lineY),
  65. new Vector2(width - rightLinePadding, lineY)
  66. );
  67. GUI.Label(
  68. new Rect(0, lineY - 8, xBorder - 2, 50),
  69. string.Format(axisFormat, max*(1f - ((float) i/(float) n))),
  70. _labelTextStyle
  71. );
  72. }
  73. Handles.color = handleColor;
  74. }
  75. void drawLine(float[] data, float floor, float width, float availableHeight, float max)
  76. {
  77. var lineWidth = (float) (width - xBorder - rightLinePadding)/data.Length;
  78. var handleColor = Handles.color;
  79. var labelRect = new Rect();
  80. Vector2 newLine;
  81. bool mousePositionDiscovered = false;
  82. float mouseHoverDataValue = 0;
  83. float linePointScale;
  84. Handles.color = lineColor;
  85. Handles.matrix = Matrix4x4.identity;
  86. HandleUtility.handleMaterial.SetPass(0);
  87. for (int i = 0; i < data.Length; i++)
  88. {
  89. var value = data[i];
  90. var lineTop = floor - (availableHeight*(value/max));
  91. newLine = new Vector2(xBorder + (lineWidth*i), lineTop);
  92. _linePoints[i] = new Vector3(newLine.x, newLine.y, 0);
  93. linePointScale = 1f;
  94. if (!mousePositionDiscovered)
  95. {
  96. var anchorPosRadius3 = anchorRadius*3;
  97. var anchorPosRadius6 = anchorRadius*6;
  98. var anchorPos = newLine - (Vector2.up*0.5f);
  99. labelRect = new Rect(anchorPos.x - anchorPosRadius3, anchorPos.y - anchorPosRadius3,
  100. anchorPosRadius6, anchorPosRadius6);
  101. if (labelRect.Contains(Event.current.mousePosition))
  102. {
  103. mousePositionDiscovered = true;
  104. mouseHoverDataValue = value;
  105. linePointScale = 3f;
  106. }
  107. }
  108. Handles.matrix = Matrix4x4.TRS(_linePoints[i], Quaternion.identity, Vector3.one*linePointScale);
  109. Handles.DrawAAConvexPolygon(_cachedLinePointVerticies);
  110. }
  111. Handles.matrix = Matrix4x4.identity;
  112. Handles.DrawAAPolyLine(2f, data.Length, _linePoints);
  113. if (mousePositionDiscovered)
  114. {
  115. labelRect.y -= 16;
  116. labelRect.width += 50;
  117. labelRect.x -= 25;
  118. GUI.Label(labelRect, string.Format(labelFormat, mouseHoverDataValue), _centeredStyle);
  119. }
  120. Handles.color = handleColor;
  121. }
  122. }
  123. }