public void OnGUI() { //GUI.skin.font=f; // sorry for the if block if (ETGModGUI.CurrentMenu != ETGModGUI.MenuOpened.None) { if (!timeScale.HasValue && (ETGModGUI.CurrentMenu != ETGModGUI.MenuOpened.None)) { timeScale = Time.timeScale; Time.timeScale = 0; } if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape) { ETGModGUI.CurrentMenu = ETGModGUI.MenuOpened.None; ETGModGUI.UpdatePlayerState(); } } currentMenuScript.OnGUI(); //RandomSelector.OnGUI(); }
public void OnGUI() { //GUI.skin=skin; //THIS HAS TO BE CALLED TWICE, once on input, and once the frame after! //For some reason?.... if (needCorrectInput) { TextEditor txt = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl); if (txt != null) { txt.MoveTextEnd(); } needCorrectInput = false; } bool ranCommand = Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Return && CurrentCommand.Length > 0; if (ranCommand) { string[] splitCommand = CurrentCommand.Split(' '); if (!Commands.ContainsKey(splitCommand[0])) { //If there's no matching command for what the user has typed, we'll try to auto-correct it. List <string> validStrings = new List <string>(); validStrings.AddRange(displayedCorrectCommands); for (int i = 0; i < CurrentCommand.Length; i++) { List <string> toRemove = new List <string>(); for (int j = 0; j < validStrings.Count; j++) { if (validStrings[j][i] != CurrentCommand[i]) { toRemove.Add(validStrings[j]); } } foreach (string s in toRemove) { validStrings.Remove(s); } if (validStrings.Count == 1) { break; } else if (validStrings.Count == 0) { LoggedText.Add("There's no matching command for " + '"' + CurrentCommand + '"'); break; } } if (validStrings.Count > 0) { CurrentCommand = validStrings[0] + " "; //Move selection to end of text TextEditor txt = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl); if (txt != null) { txt.MoveTextEnd(); } needCorrectInput = true; } ranCommand = false; } else if (!Commands[splitCommand[0]].IsCommandCorrect(splitCommand)) { try { //If the parameters don't match, we auto-correct those too. int currCommandIndex = splitCommand.Length - 1; List <string> validStrings = new List <string>(); validStrings.AddRange(displayCorrectArguments); for (int i = 0; i < splitCommand[currCommandIndex].Length; i++) { List <string> toRemove = new List <string>(); for (int j = 0; j < validStrings.Count; j++) { if (validStrings[j][i] != splitCommand[currCommandIndex][i]) { toRemove.Add(validStrings[j]); } } foreach (string s in toRemove) { validStrings.Remove(s); } if (validStrings.Count == 1) { break; } else if (validStrings.Count == 0) { LoggedText.Add("There's no matching argument for " + '"' + CurrentCommand + '"'); break; } } if (validStrings.Count > 0) { splitCommand[splitCommand.Length - 1] = validStrings[0]; CurrentCommand = ""; for (int i = 0; i < splitCommand.Length; i++) { CurrentCommand += (i == 0 ? "" : " ") + splitCommand[i]; } CurrentCommand += " "; //Move selection to end of text TextEditor txt = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl); if (txt != null) { txt.MoveTextEnd(); } needCorrectInput = true; } } catch (System.Exception e) { LoggedText.Add(e.ToString()); } ranCommand = false; } else { //If we're all good, run the command! RunCommand(); } } mainBoxRect = new Rect(16, 16, Screen.width - 32, Screen.height - 32 - 29); inputBox = new Rect(16, Screen.height - 16 - 24, Screen.width - 32, 24); autoCorrectBox = new Rect(16, Screen.height - 16 - 144, Screen.width - 32, 120); GUI.Box(mainBoxRect, "Console"); //Input string changedCommand = GUI.TextField(inputBox, CurrentCommand); if (changedCommand != CurrentCommand) { CurrentCommand = changedCommand; OnTextChanged(); } //Logged text GUILayout.BeginArea(mainBoxRect); mainScrollPos = GUILayout.BeginScrollView(mainScrollPos); for (int i = 0; i < LoggedText.Count; i++) { GUILayout.Label(LoggedText[i]); } GUILayout.EndScrollView(); GUILayout.EndArea(); //Auto-correct box. if (CurrentCommand.Length > 0) { GUI.Box(autoCorrectBox, "Auto-Correct"); GUILayout.BeginArea(autoCorrectBox); correctScrollPos = GUILayout.BeginScrollView(correctScrollPos); for (int i = 0; i < displayedCorrectCommands.Length; i++) { GUILayout.Label(displayedCorrectCommands[i]); } for (int i = 0; i < displayCorrectArguments.Length; i++) { GUILayout.Label(displayCorrectArguments[i]); } GUILayout.EndScrollView(); GUILayout.EndArea(); } //Command handling if (ranCommand && Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.Return) { //If this command is valid // No new line when we ran a command. CurrentCommand = ""; if (cutInputFocusOnCommand) { GUI.FocusControl(""); } if (closeConsoleOnCommand) { ETGModGUI.CurrentMenu = ETGModGUI.MenuOpened.None; ETGModGUI.UpdatePlayerState(); } } }