|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +public class DebugConsole : MonoBehaviour |
| 7 | +{ |
| 8 | + enum DisplayType |
| 9 | + { |
| 10 | + None, |
| 11 | + Help, |
| 12 | + Autocomplete, |
| 13 | + Output |
| 14 | + } |
| 15 | + |
| 16 | + private static GUIStyle _logStyle; |
| 17 | + |
| 18 | + private bool _showConsole = false; |
| 19 | + private string _consoleInput; |
| 20 | + |
| 21 | + private DisplayType _displayType; |
| 22 | + |
| 23 | + private List<string> _commandOutput; |
| 24 | + |
| 25 | + private void Awake() |
| 26 | + { |
| 27 | + new DebugCommand("?", "Lists all available debug commands.", "?", () => |
| 28 | + { |
| 29 | + _displayType = DisplayType.Help; |
| 30 | + }); |
| 31 | + new DebugCommand("toggle_fov", "Toggles the FOV parameter on/off.", "toggle_fov", () => |
| 32 | + { |
| 33 | + bool fov = !GameManager.instance.gameGlobalParameters.enableFOV; |
| 34 | + GameManager.instance.gameGlobalParameters.enableFOV = fov; |
| 35 | + EventManager.TriggerEvent("UpdateGameParameter:enableFOV", fov); |
| 36 | + }); |
| 37 | + new DebugCommand<int>("add_gold", "Adds a given amount of gold to the current player.", "add_gold <amount>", (x) => |
| 38 | + { |
| 39 | + Globals.GAME_RESOURCES[GameManager.instance.gamePlayersParameters.myPlayerId][InGameResource.Gold].AddAmount(x); |
| 40 | + EventManager.TriggerEvent("UpdateResourceTexts"); |
| 41 | + }); |
| 42 | + new DebugCommand<int>("add_wood", "Adds a given amount of wood to the current player.", "add_wood <amount>", (x) => |
| 43 | + { |
| 44 | + Globals.GAME_RESOURCES[GameManager.instance.gamePlayersParameters.myPlayerId][InGameResource.Wood].AddAmount(x); |
| 45 | + EventManager.TriggerEvent("UpdateResourceTexts"); |
| 46 | + }); |
| 47 | + new DebugCommand<int>("add_stone", "Adds a given amount of stone to the current player.", "add_stone <amount>", (x) => |
| 48 | + { |
| 49 | + Globals.GAME_RESOURCES[GameManager.instance.gamePlayersParameters.myPlayerId][InGameResource.Stone].AddAmount(x); |
| 50 | + EventManager.TriggerEvent("UpdateResourceTexts"); |
| 51 | + }); |
| 52 | + new DebugCommand("list_players", "Lists all current players (with their IDs).", "list_players", () => |
| 53 | + { |
| 54 | + if (_commandOutput == null) |
| 55 | + _commandOutput = new List<string>(); |
| 56 | + else |
| 57 | + _commandOutput.Clear(); |
| 58 | + int i = 0; |
| 59 | + foreach (PlayerData p in GameManager.instance.gamePlayersParameters.players) |
| 60 | + _commandOutput.Add($"Player #{i++} - {p.name}"); |
| 61 | + _displayType = DisplayType.Output; |
| 62 | + }); |
| 63 | + new DebugCommand<int>("set_player_id", "Sets the current player (by ID).", "set_player_id <id>", (x) => |
| 64 | + { |
| 65 | + GameManager.instance.gamePlayersParameters.myPlayerId = x; |
| 66 | + EventManager.TriggerEvent("SetPlayer", x); |
| 67 | + }); |
| 68 | + |
| 69 | + _displayType = DisplayType.None; |
| 70 | + } |
| 71 | + |
| 72 | + private void OnEnable() |
| 73 | + { |
| 74 | + EventManager.AddListener("<Input>ShowDebugConsole", _OnShowDebugConsole); |
| 75 | + } |
| 76 | + |
| 77 | + private void OnDisable() |
| 78 | + { |
| 79 | + EventManager.RemoveListener("<Input>ShowDebugConsole", _OnShowDebugConsole); |
| 80 | + } |
| 81 | + |
| 82 | + private void _OnShowDebugConsole() |
| 83 | + { |
| 84 | + _showConsole = true; |
| 85 | + EventManager.TriggerEvent("PauseGame"); |
| 86 | + } |
| 87 | + |
| 88 | + private void OnGUI() |
| 89 | + { |
| 90 | + if (_logStyle == null) |
| 91 | + { |
| 92 | + _logStyle = new GUIStyle(GUI.skin.label); |
| 93 | + _logStyle.fontSize = 12; |
| 94 | + } |
| 95 | + |
| 96 | + if (_showConsole) |
| 97 | + { |
| 98 | + // add fake boxes in the background to increase the opacity |
| 99 | + GUI.Box(new Rect(0, 0, Screen.width, Screen.height), ""); |
| 100 | + GUI.Box(new Rect(0, 0, Screen.width, Screen.height), ""); |
| 101 | + |
| 102 | + // show main input field |
| 103 | + string newInput = GUI.TextField(new Rect(0, 0, Screen.width, 24), _consoleInput); |
| 104 | + |
| 105 | + // show log area |
| 106 | + float y = 24; |
| 107 | + GUI.Box(new Rect(0, y, Screen.width, Screen.height - 24), ""); |
| 108 | + if (_displayType == DisplayType.Help) |
| 109 | + _ShowHelp(y); |
| 110 | + else if (_displayType == DisplayType.Autocomplete) |
| 111 | + _ShowAutocomplete(y, newInput); |
| 112 | + else if (_displayType == DisplayType.Output) |
| 113 | + _ShowOutput(y); |
| 114 | + |
| 115 | + // reset display state to "none" if input changes |
| 116 | + if (_displayType != DisplayType.None && _consoleInput.Length != newInput.Length) |
| 117 | + _displayType = DisplayType.None; |
| 118 | + |
| 119 | + // update input variable |
| 120 | + _consoleInput = newInput; |
| 121 | + |
| 122 | + // check for special keys |
| 123 | + Event e = Event.current; |
| 124 | + if (e.isKey) |
| 125 | + { |
| 126 | + if (e.keyCode == KeyCode.Tab) |
| 127 | + _displayType = DisplayType.Autocomplete; |
| 128 | + else if (e.keyCode == KeyCode.Return && _consoleInput.Length > 0) |
| 129 | + _OnReturn(); |
| 130 | + else if (e.keyCode == KeyCode.Escape) |
| 131 | + { |
| 132 | + _showConsole = false; |
| 133 | + EventManager.TriggerEvent("ResumeGame"); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private void _ShowHelp(float y) |
| 140 | + { |
| 141 | + foreach (DebugCommandBase command in DebugCommandBase.DebugCommands.Values) |
| 142 | + { |
| 143 | + GUI.Label( |
| 144 | + new Rect(2, y, Screen.width, 20), |
| 145 | + $"{command.Format} - {command.Description}", |
| 146 | + _logStyle |
| 147 | + ); |
| 148 | + y += 16; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private void _ShowAutocomplete(float y, string newInput) |
| 153 | + { |
| 154 | + IEnumerable<string> autocompleteCommands = |
| 155 | + DebugCommandBase.DebugCommands.Keys |
| 156 | + .Where(k => k.StartsWith(newInput.ToLower())); |
| 157 | + foreach (string k in autocompleteCommands) |
| 158 | + { |
| 159 | + DebugCommandBase c = DebugCommandBase.DebugCommands[k]; |
| 160 | + GUI.Label( |
| 161 | + new Rect(2, y, Screen.width, 20), |
| 162 | + $"{c.Format} - {c.Description}", |
| 163 | + _logStyle |
| 164 | + ); |
| 165 | + y += 16; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private void _ShowOutput(float y) |
| 170 | + { |
| 171 | + foreach (string line in _commandOutput) |
| 172 | + { |
| 173 | + GUI.Label(new Rect(2, y, Screen.width, 20), line, _logStyle); |
| 174 | + y += 16; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private void _OnReturn() |
| 179 | + { |
| 180 | + _HandleConsoleInput(); |
| 181 | + _consoleInput = ""; |
| 182 | + } |
| 183 | + |
| 184 | + private void _HandleConsoleInput() |
| 185 | + { |
| 186 | + // parse input |
| 187 | + string[] inputParts = _consoleInput.Split(' '); |
| 188 | + string mainKeyword = inputParts[0]; |
| 189 | + // check against available commands |
| 190 | + DebugCommandBase command; |
| 191 | + if (DebugCommandBase.DebugCommands.TryGetValue(mainKeyword.ToLower(), out command)) |
| 192 | + { |
| 193 | + // try to invoke command if it exists |
| 194 | + if (command is DebugCommand dc) |
| 195 | + dc.Invoke(); |
| 196 | + else |
| 197 | + { |
| 198 | + if (inputParts.Length < 2) |
| 199 | + { |
| 200 | + Debug.LogError("Missing parameter!"); |
| 201 | + return; |
| 202 | + } |
| 203 | + if (command is DebugCommand<int> dcInt) |
| 204 | + { |
| 205 | + int i; |
| 206 | + if (int.TryParse(inputParts[1], out i)) |
| 207 | + dcInt.Invoke(i); |
| 208 | + else |
| 209 | + { |
| 210 | + Debug.LogError($"'{command.Id}' requires an int parameter!"); |
| 211 | + return; |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments