File size: 4,685 Bytes
18a519f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using UnityEngine;

namespace UnityEditor.Performance.ProfileAnalyzer
{
    internal class Histogram
    {
        Draw2D m_2D;
        Color m_ColorBarBackground;
        DisplayUnits m_Units;

        public void SetUnits(Units units)
        {
            m_Units = new DisplayUnits(units);
        }

        public Histogram(Draw2D draw2D, Units units)
        {
            m_2D = draw2D;
            SetUnits(units);
            m_ColorBarBackground = new Color(0.5f, 0.5f, 0.5f);
        }

        public Histogram(Draw2D draw2D, Units units, Color barBackground)
        {
            m_2D = draw2D;
            SetUnits(units);
            m_ColorBarBackground = barBackground;
        }

        public void DrawStart(float width)
        {
            EditorGUILayout.BeginHorizontal(GUILayout.Width(width + 10));

            EditorGUILayout.BeginVertical();
        }

        public void DrawEnd(float width, float min, float max, float spacing)
        {
            EditorGUILayout.BeginHorizontal();
            float halfWidth = width / 2;
            GUIStyle leftAlignStyle = new GUIStyle(GUI.skin.label);
            leftAlignStyle.contentOffset = new Vector2(-5, 0);
            leftAlignStyle.alignment = TextAnchor.MiddleLeft;
            GUIStyle rightAlignStyle = new GUIStyle(GUI.skin.label);
            rightAlignStyle.contentOffset = new Vector2(-5, 0);
            rightAlignStyle.alignment = TextAnchor.MiddleRight;
            EditorGUILayout.LabelField(m_Units.ToString(min, false, 5, true), leftAlignStyle, GUILayout.Width(halfWidth));
            EditorGUILayout.LabelField(m_Units.ToString(max, false, 5, true), rightAlignStyle, GUILayout.Width(halfWidth));
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.EndVertical();

            EditorGUILayout.EndHorizontal();
        }

        public void DrawBackground(float width, float height, int bucketCount, float min, float max, float spacing)
        {
            //bucketCount = (range == 0f) ? 1 : bucketCount;

            float x = (spacing / 2);
            float y = 0;
            float w = ((width + spacing) / bucketCount) - spacing;
            float h = height;

            for (int i = 0; i < bucketCount; i++)
            {
                m_2D.DrawFilledBox(x, y, w, h, m_ColorBarBackground);
                x += w;
                x += spacing;
            }
        }

        public void DrawData(float width, float height, int[] buckets, int totalFrameCount, float min, float max, Color barColor, float spacing)
        {
            float range = max - min;

            //int bucketCount = (range == 0f) ? 1 : buckets.Length;
            int bucketCount = buckets.Length;

            float x = (spacing / 2);
            float y = 0;
            float w = ((width + spacing) / bucketCount) - spacing;
            float h = height;

            float bucketWidth = (range / bucketCount);
            Rect rect = GUILayoutUtility.GetLastRect();
            for (int bucketAt = 0; bucketAt < bucketCount; bucketAt++)
            {
                var count = buckets[bucketAt];

                float barHeight = (h * count) / totalFrameCount;
                if (count > 0)  // Make sure we always slow a small bar if non zero
                    barHeight = Mathf.Max(1.0f, barHeight);
                m_2D.DrawFilledBox(x, y, w, barHeight, barColor);

                float bucketStart = min + (bucketAt * bucketWidth);
                float bucketEnd = bucketStart + bucketWidth;

                var tooltip = string.Format("{0}-{1}\n{2} {3}\n\nBar width: {4}",
                    m_Units.ToTooltipString(bucketStart, false),
                    m_Units.ToTooltipString(bucketEnd, true),
                    count,
                    count == 1 ? "frame" : "frames",
                    m_Units.ToTooltipString(bucketWidth, true)
                );

                var content = new GUIContent("", tooltip);
                GUI.Label(new Rect(rect.x + x, rect.y + y, w, h), content);

                x += w;
                x += spacing;
            }
        }

        public void Draw(float width, float height, int[] buckets, int totalFrameCount, float min, float max, Color barColor)
        {
            DrawStart(width);

            float spacing = 2;

            if (m_2D.DrawStart(width, height, Draw2D.Origin.BottomLeft))
            {
                DrawBackground(width, height, buckets.Length, min, max, spacing);
                DrawData(width, height, buckets, totalFrameCount, min, max, barColor, spacing);
                m_2D.DrawEnd();
            }

            DrawEnd(width, min, max, spacing);
        }
    }
}