Пример #1
0
 public float GetMax(string subgraph)
 {
     if (mSharedYAxis)
     {
         bool  max_set = false;
         float max     = 0;
         foreach (KeyValuePair <string, GraphItDataInternal> entry in mData)
         {
             GraphItDataInternal g = entry.Value;
             if (!max_set)
             {
                 max     = g.mMax;
                 max_set = true;
             }
             max = Math.Max(max, g.mMax);
         }
         return(max);
     }
     else
     {
         if (!mData.ContainsKey(subgraph))
         {
             mData[subgraph] = new GraphItDataInternal(mData.Count);
         }
         return(mData[subgraph].mMax);
     }
 }
Пример #2
0
 public float GetMin(string subgraph)
 {
     if (mSharedYAxis)
     {
         bool  min_set = false;
         float min     = 0;
         foreach (KeyValuePair <string, GraphItDataInternal> entry in mData)
         {
             GraphItDataInternal g = entry.Value;
             if (!min_set)
             {
                 min     = g.mMin;
                 min_set = true;
             }
             min = Math.Min(min, g.mMin);
         }
         return(min);
     }
     else
     {
         if (!mData.ContainsKey(subgraph))
         {
             mData[subgraph] = new GraphItDataInternal(mData.Count);
         }
         return(mData[subgraph].mMin);
     }
 }
Пример #3
0
 public float GetMin(string subgraph)
 {
     if (!mData.ContainsKey(subgraph))
     {
         mData[subgraph] = new GraphItDataInternal();
     }
     return(mData[subgraph].mMin);
 }
Пример #4
0
    public float GetMax(string subgraph)
    {
        bool  max_set = false;
        float max     = 0;

        foreach (KeyValuePair <string, GraphItDataInternal> entry in mData)
        {
            GraphItDataInternal g = entry.Value;
            if (!max_set)
            {
                max     = g.mMax;
                max_set = true;
            }
            max = Math.Max(max, g.mMax);
        }
        return(max);
    }
Пример #5
0
    /// <summary>
    /// Optional setup function that allows you to specify how many samples to track.
    /// </summary>
    /// <param name="graph"></param>
    /// <param name="sample_window"></param>
    public static void GraphSetupSampleWindowSize(string graph, int sample_window)
    {
#if UNITY_EDITOR
        if (!Instance.Graphs.ContainsKey(graph))
        {
            Instance.Graphs[graph] = new GraphItData(graph);
        }

        GraphItData g       = Instance.Graphs[graph];
        int         samples = Math.Max(sample_window, GraphItData.RECENT_WINDOW_SIZE + 1);
        g.mWindowSize = samples;
        foreach (KeyValuePair <string, GraphItDataInternal> entry in g.mData)
        {
            GraphItDataInternal _g = entry.Value;
            _g.mDataPoints = new float[samples];
        }
#endif
    }
Пример #6
0
    void StepGraphInternal(GraphItData graph)
    {
#if UNITY_EDITOR
        foreach (KeyValuePair <string, GraphItDataInternal> entry in graph.mData)
        {
            GraphItDataInternal g = entry.Value;

            g.mDataPoints[graph.mCurrentIndex] = g.mCounter;
            g.mCounter = 0.0f;
        }

        graph.mCurrentIndex = (graph.mCurrentIndex + 1) % graph.mWindowSize;
        if (graph.mCurrentIndex == 0)
        {
            graph.mFullArray = true;
        }

        foreach (KeyValuePair <string, GraphItDataInternal> entry in graph.mData)
        {
            GraphItDataInternal g = entry.Value;

            float sum = g.mDataPoints[0];
            float min = g.mDataPoints[0];
            float max = g.mDataPoints[0];
            for (int i = 1; i < graph.GraphLength(); ++i)
            {
                sum += g.mDataPoints[i];
                min  = Mathf.Min(min, g.mDataPoints[i]);
                max  = Mathf.Max(max, g.mDataPoints[i]);
            }
            if (graph.mInclude0)
            {
                min = Mathf.Min(min, 0.0f);
                max = Mathf.Max(max, 0.0f);
            }

            //Calculate the recent average
            int recent_start = graph.mCurrentIndex - GraphItData.RECENT_WINDOW_SIZE;
            int recent_count = GraphItData.RECENT_WINDOW_SIZE;
            if (recent_start < 0)
            {
                if (graph.mFullArray)
                {
                    recent_start += g.mDataPoints.Length;
                }
                else
                {
                    recent_count = graph.GraphLength();
                    recent_start = 0;
                }
            }

            float recent_sum = 0.0f;
            for (int i = 0; i < recent_count; ++i)
            {
                recent_sum  += g.mDataPoints[recent_start];
                recent_start = (recent_start + 1) % g.mDataPoints.Length;
            }

            g.mMin     = min;
            g.mMax     = max;
            g.mAvg     = sum / graph.GraphLength();
            g.mFastAvg = recent_sum / recent_count;
        }
#endif
    }
Пример #7
0
    public void DrawGraphs(Rect rect)
    {
        if (GraphIt.Instance != null)
        {
            InitializeStyles();
            CreateLineMaterial();

            mLineMaterial.SetPass(0);

            int graph_index = 0;

            //use this to get the starting y position for the GL rendering
            Rect find_y = EditorGUILayout.BeginVertical(GUIStyle.none);
            EditorGUILayout.EndVertical();

            if (Event.current.type == EventType.Repaint)
            {
                GL.PushMatrix();
                float start_y = find_y.y;
                GL.Viewport(new Rect(0, 0, rect.width, rect.height - start_y));
                GL.LoadPixelMatrix(0, rect.width, rect.height - start_y, 0);


                //Draw grey BG
                GL.Begin(GL.QUADS);
                GL.Color(new Color(0.2f, 0.2f, 0.2f));

                float scrolled_y_pos = y_offset - mScrollPos.y;
                foreach (KeyValuePair <string, GraphItData> kv in GraphIt.Instance.Graphs)
                {
                    if (kv.Value.GetHidden())
                    {
                        continue;
                    }

                    float height = kv.Value.GetHeight();

                    GL.Vertex3(x_offset, scrolled_y_pos, 0);
                    GL.Vertex3(x_offset + mWidth, scrolled_y_pos, 0);
                    GL.Vertex3(x_offset + mWidth, scrolled_y_pos + height, 0);
                    GL.Vertex3(x_offset, scrolled_y_pos + height, 0);

                    scrolled_y_pos += (height + y_gap);
                }
                GL.End();

                //Draw Lines
                GL.Begin(GL.LINES);
                scrolled_y_pos = y_offset - mScrollPos.y;

                foreach (KeyValuePair <string, GraphItData> kv in GraphIt.Instance.Graphs)
                {
                    if (kv.Value.GetHidden())
                    {
                        continue;
                    }
                    graph_index++;

                    float x_step = mWidth / kv.Value.GraphFullLength();

                    float height = kv.Value.GetHeight();
                    DrawGraphGridLines(scrolled_y_pos, mWidth, height, mMouseOverGraphIndex != -1);

                    if (kv.Value.GraphLength() > 0)
                    {
                        foreach (KeyValuePair <string, GraphItDataInternal> entry in kv.Value.mData)
                        {
                            GraphItDataInternal g = entry.Value;

                            float y_min   = kv.Value.GetMin(entry.Key);
                            float y_max   = kv.Value.GetMax(entry.Key);
                            float y_range = Mathf.Max(y_max - y_min, 0.00001f);

                            //draw the 0 line
                            if (y_max > 0.0f && y_min < 0.0f)
                            {
                                GL.Color(g.mColor * 0.5f);
                                float y = scrolled_y_pos + height * (1 - (0.0f - y_min) / y_range);
                                Plot(x_offset, y, x_offset + mWidth, y);
                            }


                            GL.Color(g.mColor);

                            float previous_value = 0;
                            int   start_index    = (kv.Value.mCurrentIndex) % kv.Value.GraphLength();
                            for (int i = 0; i < kv.Value.GraphLength(); ++i)
                            {
                                float value = g.mDataPoints[start_index];
                                if (i >= 1)
                                {
                                    float x0 = x_offset + (i - 1) * x_step;
                                    float y0 = scrolled_y_pos + height * (1 - (previous_value - y_min) / y_range);

                                    float x1 = x_offset + i * x_step;
                                    float y1 = scrolled_y_pos + height * (1 - (value - y_min) / y_range);

                                    Plot(x0, y0, x1, y1);
                                }
                                previous_value = value;
                                start_index    = (start_index + 1) % kv.Value.GraphFullLength();
                            }
                        }
                    }

                    scrolled_y_pos += (height + y_gap);
                }
                GL.End();

                GL.PopMatrix();

                GL.Viewport(new Rect(0, 0, rect.width, rect.height));
                GL.LoadPixelMatrix(0, rect.width, rect.height, 0);
            }

            mScrollPos  = EditorGUILayout.BeginScrollView(mScrollPos, GUIStyle.none);
            graph_index = 0;
            if (Event.current.type == EventType.Repaint)
            {
                mMouseOverGraphIndex = -1; //clear it out every repaint to ensure when the mouse leaves we don't leave the pointer around
            }
            foreach (KeyValuePair <string, GraphItData> kv in GraphIt.Instance.Graphs)
            {
                if (kv.Value.GetHidden())
                {
                    continue;
                }
                graph_index++;

                float height = kv.Value.GetHeight();

                GUIStyle s = new GUIStyle();
                s.fixedHeight  = height + y_gap;
                s.stretchWidth = true;
                Rect r = EditorGUILayout.BeginVertical(s);
                if (r.width != -0)
                {
                    mWidth = r.width - 2 * x_offset;
                }

                string fmt    = "###,###,###,##0.###";
                string fu_str = " " + (kv.Value.mFixedUpdate ? "(FixedUpdate)" : "");

                NameLabel.normal.textColor = Color.white;
                PAEditorUtil.DrawLabel(kv.Key + fu_str, NameLabel);

                foreach (KeyValuePair <string, GraphItDataInternal> entry in kv.Value.mData)
                {
                    GraphItDataInternal g = entry.Value;
                    if (kv.Value.mData.Count > 1 || entry.Key != GraphIt.BASE_GRAPH)
                    {
                        NameLabel.normal.textColor = g.mColor;
                        PAEditorUtil.DrawLabel(entry.Key, NameLabel);
                    }

                    if (g.mDataPoints.Length > 0)
                    {
                        int index = kv.Value.mCurrentIndex == 0 ? g.mDataPoints.Length - 1 : (kv.Value.mCurrentIndex - 1) % g.mDataPoints.Length;
                        try
                        {
                            PAEditorUtil.DrawLabel(g.mDataPoints[index].ToString(fmt), SmallLabel);
                        }
                        catch (System.Exception)
                        {
                            Debug.LogWarningFormat("[CoGraph] invalid index (mCurrentIndex: {0}, modulized {1})", kv.Value.mCurrentIndex, index);
                        }
                    }
                }

                if (Event.current.type == EventType.MouseDrag && r.Contains(Event.current.mousePosition - Event.current.delta))
                {
                    if (Event.current.type == EventType.MouseDrag && Event.current.button == 0)
                    {
                        kv.Value.DoHeightDelta(Event.current.delta.y);
                    }
                }

                if (Event.current.type != EventType.Layout && r.Contains(Event.current.mousePosition))
                {
                    if (Event.current.type == EventType.Repaint || Event.current.type == EventType.MouseDown || Event.current.type == EventType.MouseDrag)
                    {
                        mMouseOverGraphIndex = graph_index;
                        mMouseX = Event.current.mousePosition.x;

                        float x_step         = mWidth / kv.Value.GraphFullLength();
                        float hover_y_offset = 0;
                        if (kv.Value.GraphLength() > 0)
                        {
                            foreach (KeyValuePair <string, GraphItDataInternal> entry in kv.Value.mData)
                            {
                                GraphItDataInternal g = entry.Value;

                                //walk through the data points to find the correct index matching the mouse position value
                                //potential optimization here to find the index algebraically.
                                int start_index = (kv.Value.mCurrentIndex) % kv.Value.GraphLength();
                                for (int i = 0; i < kv.Value.GraphLength(); ++i)
                                {
                                    float value = g.mDataPoints[start_index];
                                    if (i >= 1)
                                    {
                                        float x0 = x_offset + (i - 1) * x_step;
                                        float x1 = x_offset + i * x_step;
                                        if (x0 < mMouseX && mMouseX <= x1)
                                        {
                                            //found this mouse positions step

                                            if (Event.current.type == EventType.MouseDown || Event.current.type == EventType.MouseDrag)
                                            {
                                                mSelectedXLeft = x0;
                                                mSelectedX     = x1;

                                                if (_selectionChanged != null)
                                                {
                                                    _selectionChanged(start_index);
                                                }
                                            }

                                            string text = value.ToString(fmt);

                                            Rect tooltip_r = new Rect(Event.current.mousePosition + new Vector2(10, 2 - hover_y_offset), new Vector2(50, 20));
                                            HoverText.normal.textColor = g.mColor;
                                            GUI.Label(tooltip_r, text, HoverText);

                                            hover_y_offset += 13;
                                            break;
                                        }
                                    }
                                    start_index = (start_index + 1) % kv.Value.GraphFullLength();
                                }
                            }
                        }
                    }
                }

                EditorGUILayout.EndVertical();
            }
            EditorGUILayout.EndScrollView();
        }
    }
Пример #8
0
    public static void DrawGraphs(Rect rect, GraphItWindow window)
    {
        if (GraphIt.Instance)
        {
            InitializeStyles();
            CreateLineMaterial();

            EditorGUILayout.BeginHorizontal(GUIStyle.none);
            EditorGUILayout.BeginVertical(FracGS, GUILayout.Height(18));
            precision_slider = EditorGUILayout.IntSlider("Fractional Digits", precision_slider, 0, 15);
            EditorGUILayout.EndVertical();
            if (GUILayout.Button("Show All Graphs"))
            {
                foreach (KeyValuePair <string, GraphItData> kv in GraphIt.Instance.Graphs)
                {
                    kv.Value.SetHidden(false);
                }
            }

            /*
             * if (GUILayout.Button("?"))
             * {
             *  foreach (KeyValuePair<string, GraphItData> kv in GraphIt.Instance.Graphs)
             *  {
             *      kv.Value.SetHidden(false);
             *  }
             * }*/
            //EditorGUILayout.LabelField("Left click+drag on graph to resize. Right click to hide graph.", EditorStyles.helpBox );
            EditorGUILayout.EndHorizontal();

            mLineMaterial.SetPass(0);

            int graph_index = 0;

            //use this to get the starting y position for the GL rendering
            Rect find_y = EditorGUILayout.BeginVertical(GUIStyle.none);
            EditorGUILayout.EndVertical();

            if (Event.current.type == EventType.Repaint)
            {
                GL.PushMatrix();
                float start_y = find_y.y;
                GL.Viewport(new Rect(0, 0, rect.width, rect.height - start_y));
                GL.LoadPixelMatrix(0, rect.width, rect.height - start_y, 0);


                //Draw grey BG
                GL.Begin(GL.QUADS);
                GL.Color(new Color(0.2f, 0.2f, 0.2f));

                float scrolled_y_pos = y_offset - mScrollPos.y;
                foreach (KeyValuePair <string, GraphItData> kv in GraphIt.Instance.Graphs)
                {
                    if (kv.Value.GetHidden())
                    {
                        continue;
                    }

                    float height = kv.Value.GetHeight();

                    GL.Vertex3(x_offset, scrolled_y_pos, 0);
                    GL.Vertex3(x_offset + mWidth, scrolled_y_pos, 0);
                    GL.Vertex3(x_offset + mWidth, scrolled_y_pos + height, 0);
                    GL.Vertex3(x_offset, scrolled_y_pos + height, 0);

                    scrolled_y_pos += (height + y_gap);
                }
                GL.End();

                //Draw Lines
                GL.Begin(GL.LINES);
                scrolled_y_pos = y_offset - mScrollPos.y;

                foreach (KeyValuePair <string, GraphItData> kv in GraphIt.Instance.Graphs)
                {
                    if (kv.Value.GetHidden())
                    {
                        continue;
                    }
                    graph_index++;

                    float x_step = mWidth / kv.Value.GraphFullLength();

                    float height = kv.Value.GetHeight();
                    DrawGraphGridLines(scrolled_y_pos, mWidth, height, graph_index == mMouseOverGraphIndex);

                    if (kv.Value.GraphLength() > 0)
                    {
                        foreach (KeyValuePair <string, GraphItDataInternal> entry in kv.Value.mData)
                        {
                            GraphItDataInternal g = entry.Value;

                            float y_min   = kv.Value.GetMin(entry.Key);
                            float y_max   = kv.Value.GetMax(entry.Key);
                            float y_range = Mathf.Max(y_max - y_min, 0.00001f);

                            //draw the 0 line
                            if (y_max > 0.0f && y_min < 0.0f)
                            {
                                GL.Color(g.mColor * 0.5f);
                                float y = scrolled_y_pos + height * (1 - (0.0f - y_min) / y_range);
                                Plot(x_offset, y, x_offset + mWidth, y);
                            }


                            GL.Color(g.mColor);

                            float previous_value = 0;
                            int   start_index    = (kv.Value.mCurrentIndex) % kv.Value.GraphLength();
                            for (int i = 0; i < kv.Value.GraphLength(); ++i)
                            {
                                float value = g.mDataPoints[start_index];
                                if (i >= 1)
                                {
                                    float x0 = x_offset + (i - 1) * x_step;
                                    float y0 = scrolled_y_pos + height * (1 - (previous_value - y_min) / y_range);

                                    float x1 = x_offset + i * x_step;
                                    float y1 = scrolled_y_pos + height * (1 - (value - y_min) / y_range);

                                    Plot(x0, y0, x1, y1);
                                }
                                previous_value = value;
                                start_index    = (start_index + 1) % kv.Value.GraphFullLength();
                            }
                        }
                    }

                    scrolled_y_pos += (height + y_gap);
                }
                GL.End();

                GL.PopMatrix();

                GL.Viewport(new Rect(0, 0, rect.width, rect.height));
                GL.LoadPixelMatrix(0, rect.width, rect.height, 0);
            }

            mScrollPos  = EditorGUILayout.BeginScrollView(mScrollPos, GUIStyle.none);
            graph_index = 0;
            if (Event.current.type == EventType.Repaint)
            {
                mMouseOverGraphIndex = -1; //clear it out every repaint to ensure when the mouse leaves we don't leave the pointer around
            }
            foreach (KeyValuePair <string, GraphItData> kv in GraphIt.Instance.Graphs)
            {
                if (kv.Value.GetHidden())
                {
                    continue;
                }
                graph_index++;

                float height = kv.Value.GetHeight();

                GUIStyle s = new GUIStyle();
                s.fixedHeight  = height + y_gap;
                s.stretchWidth = true;
                Rect r = EditorGUILayout.BeginVertical(s);
                if (r.width != -0)
                {
                    mWidth = r.width - 2 * x_offset;
                }

                //Determine if we can fit all of the text
                float row_size        = 18;
                float text_block_size = row_size * 4;
                if (kv.Value.mData.Count == 1)
                {
                    text_block_size = row_size * 3;
                }
                bool show_full_text = (kv.Value.mData.Count * text_block_size + row_size) < height;

                string num_format = "###,###,###,##0.";
                for (int i = 0; i < precision_slider; i++)
                {
                    num_format += "#";
                }

                string fu_str = " " + (kv.Value.mFixedUpdate ? "(FixedUpdate)" : "");

                //skip subgraph title if only one, and it's the same.
                if (show_full_text)
                {
                    NameLabel.normal.textColor = Color.white;
                    EditorGUILayout.LabelField(kv.Key + fu_str, NameLabel);
                }

                foreach (KeyValuePair <string, GraphItDataInternal> entry in kv.Value.mData)
                {
                    GraphItDataInternal g = entry.Value;
                    if (show_full_text)
                    {
                        if (kv.Value.mData.Count > 1 || entry.Key != GraphIt.BASE_GRAPH)
                        {
                            NameLabel.normal.textColor = g.mColor;
                            EditorGUILayout.LabelField(entry.Key, NameLabel);
                        }
                        EditorGUILayout.LabelField("Avg: " + g.mAvg.ToString(num_format) + " (" + g.mFastAvg.ToString(num_format) + ")", SmallLabel);
                        EditorGUILayout.LabelField("Min: " + g.mMin.ToString(num_format), SmallLabel);
                        EditorGUILayout.LabelField("Max: " + g.mMax.ToString(num_format), SmallLabel);
                    }
                    else
                    {
                        //fit each line manually or drop it
                        height -= row_size;
                        if (height >= 0)
                        {
                            if (kv.Value.mData.Count > 1)
                            {
                                NameLabel.normal.textColor = g.mColor;
                            }
                            else
                            {
                                NameLabel.normal.textColor = Color.white;
                            }
                            string text = entry.Key;
                            if (text == GraphIt.BASE_GRAPH)
                            {
                                text = kv.Key;
                            }
                            EditorGUILayout.LabelField(text + fu_str, NameLabel);
                        }
                        height -= row_size;
                        if (height >= 0)
                        {
                            EditorGUILayout.LabelField("Avg: " + g.mAvg.ToString(num_format) + " (" + g.mFastAvg.ToString(num_format) +
                                                       ")  Min: " + g.mMin.ToString(num_format) +
                                                       "  Max: " + g.mMax.ToString(num_format)
                                                       , SmallLabel);
                        }
                    }
                }

                //Respond to mouse input!
                if (Event.current.type == EventType.MouseDrag && r.Contains(Event.current.mousePosition - Event.current.delta))
                {
                    if (Event.current.type == EventType.MouseDrag && Event.current.button == 0)
                    {
                        kv.Value.DoHeightDelta(Event.current.delta.y);
                    }
                }
                else if (Event.current.type != EventType.Layout && r.Contains(Event.current.mousePosition))
                {
                    if (Event.current.type == EventType.Repaint)
                    {
                        mMouseOverGraphIndex = graph_index;
                        mMouseX = Event.current.mousePosition.x;

                        float x_step         = mWidth / kv.Value.GraphFullLength();
                        float hover_y_offset = 0;
                        if (kv.Value.GraphLength() > 0)
                        {
                            foreach (KeyValuePair <string, GraphItDataInternal> entry in kv.Value.mData)
                            {
                                GraphItDataInternal g = entry.Value;

                                //walk through the data points to find the correct index matching the mouse position value
                                //potential optimization here to find the index algebraically.
                                int start_index = (kv.Value.mCurrentIndex) % kv.Value.GraphLength();
                                for (int i = 0; i < kv.Value.GraphLength(); ++i)
                                {
                                    float value = g.mDataPoints[start_index];
                                    if (i >= 1)
                                    {
                                        float x0 = x_offset + (i - 1) * x_step;
                                        float x1 = x_offset + i * x_step;
                                        if (x0 < mMouseX && mMouseX <= x1)
                                        {
                                            //found this mouse positions step
                                            string text = value.ToString(num_format);

                                            Rect tooltip_r = new Rect(Event.current.mousePosition - new Vector2(250, 2 - hover_y_offset), new Vector2(250, 20));
                                            HoverText.normal.textColor = g.mColor;
                                            GUI.Label(tooltip_r, text, HoverText);

                                            hover_y_offset += 13;
                                            break;
                                        }
                                    }
                                    start_index = (start_index + 1) % kv.Value.GraphFullLength();
                                }
                            }
                        }
                    }

                    if (Event.current.type == EventType.MouseDown && Event.current.button == 1)
                    {
                        kv.Value.SetHidden(true);
                        window.Repaint();
                    }
                }

                EditorGUILayout.EndVertical();
            }
            EditorGUILayout.EndScrollView();
        }
    }