/// <summary> /// Graph the equation. /// </summary> private void Graph() { try { Equation equation = Equation.ParseEquation(_equation); Equation xmin = Equation.ParseEquation(_xmin); Equation xmax = Equation.ParseEquation(_xmax); _history.Add(_equation); double min = xmin.GetValue(); double max = xmax.GetValue(); double ymin = Double.NaN; double ymax = Double.NaN; _graph = new Graph(250, 250); double delta = (max - min) / 250.0d; if (max < min) { Debug.Log("Max Less Than Min:" + max + " < " + min); return; } for (double x = min; x <= max; x += delta) { double y = equation.GetValue(x); if (Double.IsNaN(y)) { continue; } if (Double.IsNaN(ymin) || y < ymin) { ymin = y; } if (Double.IsNaN(ymax) || y > ymax) { ymax = y; } } _ymax = ymax; _ymin = ymin; _gxmin = min; _gxmax = max; Debug.Log("Y min:" + ymin + " max:" + ymax + " delta:" + delta); _graph.drawGraph(x => equation.GetValue(min + x * delta), ymax, ymin, 250); _geq = equation; } catch (ParsingException e) { Debug.Log(_equation + ":" + e.Message); _history.Add(_equation + "\n" + e.Message); } }
/// <summary> /// Gets the value after any evaluation and equation parsing. /// </summary> /// <returns>The value.</returns> /// <param name="content">Content.</param> private string GetValue(string content) { if (content.StartsWith("=")) { string equation = EvalCell(content); Equation eq = Equation.ParseEquation(equation); double value = eq.GetValue(); if (value > 1e12d || value < 1e-3d) { return(value.ToString("e6")); } return(value.ToString("0.####")); } return(content); }
/// <summary> /// Calculate the equation. /// </summary> private void Calculate() { double value = 0.0d; try { Equation equation = Equation.ParseEquation(_equation); value = equation.GetValue(); _history.Add(_equation + "=" + value.ToString("G5")); _windowPos.height = 0.0f; _windowPos.width = 0.0f; if (_history.Count > MAX_HISTORY) { _history.RemoveRange(0, _history.Count - MAX_HISTORY); } } catch (ParsingException e) { Debug.Log(_equation + ":" + e.Message); _history.Add(_equation + "\n" + e.Message); } }
/// <summary> /// Raises the window event. /// </summary> /// <param name="windowId">Window identifier.</param> private void OnWindow(int windowId) { GUILayout.BeginHorizontal(GUILayout.MinWidth(300.0f)); GUILayout.BeginVertical(GUILayout.MaxWidth(300.0f + (_width * 50.0f))); GUILayout.BeginHorizontal(); GUILayout.Label("History:", _labelStyle); if (GUILayout.Button("Clear", _buttonStyle)) { _history.Clear(); _windowPos.height = 0.0f; _windowPos.width = 0.0f; } GUILayout.EndHorizontal(); GUILayout.TextArea(String.Join("\n", _history.ToArray()), _textAreaStyle); GUILayout.BeginHorizontal(GUILayout.MinWidth(300.0f + (_width * 50.0f))); GUILayout.Label("Equation Input:", _labelStyle); if (GUILayout.Button("Wider", _buttonStyle) && _width < 6) { _width++; } if (GUILayout.Button("Narrower", _buttonStyle) && _width > 0) { _width--; _windowPos.width = 0.0f; } GUILayout.EndHorizontal(); GUI.SetNextControlName("EquationIn"); _equation = GUILayout.TextField(_equation, _textFieldStyle); if (Event.current.isKey && GUI.GetNameOfFocusedControl() == "EquationIn") { if (Event.current.keyCode == KeyCode.UpArrow || Event.current.keyCode == KeyCode.DownArrow) { if (Event.current.keyCode == KeyCode.UpArrow) { if (_hIdx < 0) { _hIdx = _history.Count - 1; } else { _hIdx--; } } else if (Event.current.keyCode == KeyCode.DownArrow) { _hIdx++; if (_hIdx >= _history.Count) { _hIdx = -1; } } if (_hIdx < 0) { _hIdx = -1; _equation = ""; } else { string hist = _history [_hIdx]; if (hist.IndexOf('=') > 0) { hist = hist.Remove(hist.IndexOf('=')); } if (hist.IndexOf('\n') > 0) { hist = hist.Remove(hist.IndexOf('\n')); } _equation = hist; } } else if (Event.current.keyCode == KeyCode.Return) { Calculate(); } } GUILayout.BeginHorizontal(); if (GUILayout.Button("Calculate", _buttonStyle)) { Calculate(); } if (GUILayout.Button(_simple ? "More" : "Less", _buttonStyle)) { _simple = !_simple; _windowPos.width = 0.0f; _windowPos.height = 0.0f; } if (GUILayout.Button("Close", _buttonStyle)) { Hide(); } GUILayout.EndHorizontal(); GUILayout.EndVertical(); if (!_simple) { GUILayout.BeginVertical(GUILayout.MaxWidth(300.0f)); int oldMenu = _menuSelection; _menuSelection = GUILayout.SelectionGrid(_menuSelection, new string[] { "Graph", "Cheat Sheet", "Notes", "Help" }, 2, _buttonStyle, GUILayout.MinWidth(300.0f)); if (_menuSelection == 0) { GUILayout.BeginHorizontal(); GUILayout.Label("X Min:", _labelStyle, GUILayout.MaxWidth(50.0f)); _xmin = GUILayout.TextField(_xmin, _textFieldStyle, GUILayout.MaxWidth(250f)); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.Label("X Max:", _labelStyle, GUILayout.MaxWidth(50.0f)); _xmax = GUILayout.TextField(_xmax, _textFieldStyle, GUILayout.MaxWidth(250f)); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); if (GUILayout.Button("Graph", _buttonStyle)) { Graph(); } if (GUILayout.Button("Reset Graph", _buttonStyle)) { _graph = null; _geq = null; _windowPos.height = 0.0f; _windowPos.width = 0.0f; } GUILayout.EndHorizontal(); if (_graph != null) { GUILayout.Label("Y max:" + _ymax.ToString("G10"), _labelStyle); GUILayout.Box(_graph.getImage()); Rect boxRect = GUILayoutUtility.GetLastRect(); string mouseOverMessage = ""; if (boxRect.Contains(Event.current.mousePosition)) { float x = Event.current.mousePosition.x - boxRect.x - 25.0f; if (x >= 0 && x <= 250.0f) { double delta = (_gxmax - _gxmin) / 250.0d; mouseOverMessage = "X:" + (_gxmin + x * delta).ToString("G5") + " Y:" + _geq.GetValue(_gxmin + x * delta).ToString("G5"); } } GUILayout.Label("Y min:" + _ymin.ToString("G10"), _labelStyle); GUILayout.Label(mouseOverMessage, _labelStyle); } } else if (_menuSelection == 1) { GUILayout.Label("Cheat Sheet", _labelStyle); GUILayout.TextArea(CHEAT_SHEET, _textAreaStyle); } else if (_menuSelection == 2) { GUILayout.Label("Notes", _labelStyle); _globalScratchPad = GUILayout.TextArea(_globalScratchPad, _textAreaStyle); } else if (_menuSelection == 3) { GUILayout.Label("Help", _labelStyle); GUILayout.TextArea(HELP, _textAreaStyle); } if (_menuSelection != oldMenu) { _windowPos.height = 0.0f; } GUILayout.EndVertical(); } GUILayout.EndHorizontal(); GUI.DragWindow(); }
private void OnGraphWindow(int windowId) { bool draw = false; GUILayout.BeginVertical(); GUILayout.BeginHorizontal(); GUILayout.Label("Lines", _labelStyle); if (GUILayout.Button("+", _buttonStyle) && _graphLineCnt < _lineColor.Length) { _graphLineCnt++; } if (GUILayout.Button("-", _buttonStyle) && _graphLineCnt > 1) { _graphLineCnt--; _graphPos.height = 0.0f; if (_graphLines.ContainsKey(_graphLineCnt)) { _graphLines.Remove(_graphLineCnt); } draw = true; } GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); bool lockX = GUILayout.Toggle(_lockX, "Lock X"); if (lockX != _lockX) { _refreshGraph = true; _lockX = lockX; } bool lockY = GUILayout.Toggle(_lockY, "Lock Y"); if (lockY != _lockY) { _refreshGraph = true; _lockY = lockY; } GUILayout.EndHorizontal(); try { if (_lockX) { GUILayout.BeginHorizontal(); string updated = ""; string minPos = "XMin"; GUILayout.Label("Xmin:", _labelStyle, GUILayout.Width(40.0f)); GUI.SetNextControlName(minPos); updated = GUILayout.TextField( _graphLineEdit == -1 ? _xmin : GetValue(_xmin), _textFieldStyle, GUILayout.MinWidth(40.0f)); if (GUI.GetNameOfFocusedControl() == minPos) { if (_graphLineEdit == -1 && updated != _xmin) { _xmin = updated; draw = true; } _graphLineEdit = -1; } GUILayout.Label("Xmax:", _labelStyle, GUILayout.Width(40.0f)); string maxPos = "XMax"; GUI.SetNextControlName(maxPos); updated = GUILayout.TextField( _graphLineEdit == -1 ? _xmax : GetValue(_xmax), _textFieldStyle, GUILayout.MinWidth(40.0f)); if (GUI.GetNameOfFocusedControl() == maxPos) { if (_graphLineEdit == -1 && updated != _xmax) { _xmax = updated; draw = true; } _graphLineEdit = -1; } GUILayout.EndHorizontal(); } for (int line = 0; line < _graphLineCnt; line++) { if (!_graphLines.ContainsKey(line)) { _graphLines.Add(line, new GraphLine()); _graphLines [line].LineColor = _lineColor [line]; } GUILayout.BeginHorizontal(); GUIStyle temp = new GUIStyle(_labelStyle); Texture2D color = new Texture2D(1, 1); color.wrapMode = TextureWrapMode.Repeat; color.SetPixel(0, 0, _graphLines [line].LineColor); color.Apply(); temp.normal.background = color; GUILayout.Label("", temp, GUILayout.Width(20.0f)); string cPos = "CG" + line; GUI.SetNextControlName(cPos); string updated = GUILayout.TextField( _graphLines [line].Content, _textFieldStyle, GUILayout.MinWidth(150.0f)); if (GUI.GetNameOfFocusedControl() == cPos) { if (_graphLineEdit == line) { _graphLines [line].Content = updated; } _graphLineEdit = line; } if (!_lockX) { string minPos = "MinG" + line; GUI.SetNextControlName(minPos); updated = GUILayout.TextField( _graphLineEdit == line ? _graphLines [line].XMin : GetValue(_graphLines [line].XMin), _textFieldStyle, GUILayout.MinWidth(20.0f)); if (GUI.GetNameOfFocusedControl() == minPos) { if (_graphLineEdit == line) { _graphLines [line].XMin = updated; } _graphLineEdit = line; } string maxPos = "MaxG" + line; GUI.SetNextControlName(maxPos); updated = GUILayout.TextField( _graphLineEdit == line ? _graphLines [line].XMax : GetValue(_graphLines [line].XMax), _textFieldStyle, GUILayout.MinWidth(20.0f)); if (GUI.GetNameOfFocusedControl() == maxPos) { if (_graphLineEdit == line) { _graphLines [line].XMax = updated; } _graphLineEdit = line; } } else { _graphLines [line].XMin = _xmin; _graphLines [line].XMax = _xmax; } GUILayout.EndHorizontal(); if (_slider > 1e-2d) { GUILayout.BeginHorizontal(); GUILayout.Label("X:" + _graphLines[line].x.ToString("0.0###"), _labelStyle); GUILayout.Label("Y:" + _graphLines[line].y.ToString("0.0###"), _labelStyle); GUILayout.EndHorizontal(); } draw = draw || _graphLines [line].Dirty; } float slider = GUILayout.HorizontalSlider(_slider, 0.0f, 300.0f); if (slider != _slider) { _slider = slider; draw = true; _graphPos.height = 0.0f; } if (draw) { _ymax = Double.NaN; _ymin = Double.NaN; } if (draw || _refreshGraph) { _refreshGraph = false; _graph.reset(); for (int line = 0; line < _graphLineCnt; line++) { _graphLines[line].Dirty = false; if (_graphLines [line].Content.Length == 0) { continue; } if (_graphLines [line].XMin.Length == 0) { continue; } if (_graphLines [line].XMax.Length == 0) { continue; } Equation equation = Equation.ParseEquation(EvalCell(_graphLines [line].Content)); Equation xmin = Equation.ParseEquation(EvalCell(_graphLines [line].XMin)); Equation xmax = Equation.ParseEquation(EvalCell(_graphLines [line].XMax)); double min = xmin.GetValue(); double max = xmax.GetValue(); double ymin = Double.NaN; double ymax = Double.NaN; if (max <= min || Double.IsNaN(max) || Double.IsNaN(min)) { continue; } double delta = (max - min) / 300.0d; int px = 0; for (double x = min; x <= max; x += delta) { double y = equation.GetValue(x); if (Double.IsNaN(y)) { continue; } if (Double.IsNaN(ymin) || y < ymin) { ymin = y; } if (Double.IsNaN(ymax) || y > ymax) { ymax = y; } if (px == (int)_slider) { _graphLines[line].x = x; _graphLines[line].y = y; } px++; } if (Double.IsNaN(_ymin) || ymin < _ymin) { _ymin = ymin; _refreshGraph = true; } if (Double.IsNaN(_ymax) || ymax > _ymax) { _ymax = ymax; _refreshGraph = true; } if (_lockY) { ymin = _ymin; ymax = _ymax; } _graph.drawLineOnGraph(x => equation.GetValue(min + x * delta), ymax, ymin, 300, _graphLines [line].LineColor); } if (_slider > 1e-2d) { _graph.drawVerticalLine((int)_slider, Color.grey); } _graph.Apply(); } } catch (Exception e) { Debug.Log(e.Message + "\n" + e.StackTrace); } if (_graph != null) { GUILayout.Label("Y max:" + _ymax.ToString("G10"), _labelStyle); GUILayout.Box(_graph.getImage()); GUILayout.Label("Y min:" + _ymin.ToString("G10"), _labelStyle); } GUILayout.EndVertical(); GUI.DragWindow(); }