public void Draw(Rect area) { GUILayout.BeginArea(area); _scrollPos = GUILayout.BeginScrollView(_scrollPos, GUIStyle.none, GUI.skin.verticalScrollbar); //Debug.LogFormat("scroll pos: {0:0.00}, {1:0.00}", _scrollPos.x, _scrollPos.y); { GUIStyle s = new GUIStyle(); s.fixedHeight = _appearance.LineHeight * (m_lines.Count + 1); s.stretchWidth = true; Rect r = EditorGUILayout.BeginVertical(s); { // this silly line (empty label) is required by Unity to ensure the scroll bar appear as expected. PAEditorUtil.DrawLabel("", _appearance.Style_Line); DrawTitle(r.width); // these first/last calculatings are for smart clipping int firstLine = Mathf.Max((int)(_scrollPos.y / _appearance.LineHeight) - 1, 0); _shownLineCount = (int)(area.height / _appearance.LineHeight) + 2; int lastLine = Mathf.Min(firstLine + _shownLineCount, m_lines.Count); for (int i = firstLine; i < lastLine; i++) { DrawLine(i + 1, m_lines[i], r.width); } } EditorGUILayout.EndVertical(); } GUILayout.EndScrollView(); GUILayout.EndArea(); }
public void Draw(Rect area) { GUILayout.BeginArea(area); _scrollPos = GUILayout.BeginScrollView(_scrollPos, GUIStyle.none, GUI.skin.verticalScrollbar); //Debug.LogFormat("scroll pos: {0:0.00}, {1:0.00}", _scrollPos.x, _scrollPos.y); { float lineHeight = area.width / ColumnCount; GUIStyle s = new GUIStyle(); s.fixedHeight = lineHeight * (m_objects.Count / ColumnCount + 2); s.stretchWidth = true; Rect r = EditorGUILayout.BeginVertical(s); { // this silly line (empty label) is required by Unity to ensure the scroll bar appear as expected. PAEditorUtil.DrawLabel("", _appearance.Style_Line); // these first/last calculations are for smart clipping int firstLine = Mathf.Max((int)(_scrollPos.y / lineHeight) - 1, 0); int shownLineCount = (int)(area.height / lineHeight) + 3; int lastLine = Mathf.Min(firstLine + shownLineCount, m_objects.Count / ColumnCount + 2); for (int i = firstLine; i < lastLine; i++) { if (i * ColumnCount > m_objects.Count - 1) { break; } DrawLine(i * (int)lineHeight, i * ColumnCount, r.width, lineHeight); } } EditorGUILayout.EndVertical(); } GUILayout.EndScrollView(); GUILayout.EndArea(); }
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(); } }