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.

134 lines
5.2KB

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