| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Runtime.CompilerServices; |
| | 8 | | using System.Text; |
| | 9 | | using GDX.Developer.ConsoleCommands; |
| | 10 | | using GDX.RuntimeContent; |
| | 11 | | using UnityEngine; |
| | 12 | | #if GDX_INPUT |
| | 13 | | using UnityEngine.InputSystem; |
| | 14 | | #endif |
| | 15 | | using UnityEngine.UIElements; |
| | 16 | | using Object = UnityEngine.Object; |
| | 17 | |
|
| | 18 | | namespace GDX.Developer |
| | 19 | | { |
| | 20 | | #if UNITY_2022_2_OR_NEWER |
| | 21 | | public class RuntimeConsoleController |
| | 22 | | { |
| | 23 | | const string k_GameObjectName = "GDX_RuntimeConsole"; |
| | 24 | |
|
| | 25 | | // Maybe expose these for people messing with fonts |
| | 26 | | const int k_SourceFontSize = 14; |
| | 27 | | const int k_FrameCountWidth = 100; |
| | 28 | | const int k_SourceLevelWidth = 22; |
| | 29 | |
|
| | 30 | | readonly ListView m_ConsoleListView; |
| | 31 | | readonly Label m_InputCaret; |
| | 32 | | readonly ScrollView m_ConsoleScrollView; |
| 0 | 33 | | readonly StringBuilder m_InputBuilder = new StringBuilder(1000); |
| | 34 | | readonly Label m_InputLabel; |
| | 35 | | readonly Label m_SuggestionLabel; |
| | 36 | | readonly VisualElement m_ConsoleBarElement; |
| | 37 | |
|
| 0 | 38 | | int m_CommandBufferOffset = -1; |
| | 39 | |
|
| | 40 | | uint m_CurrentVersion; |
| | 41 | | bool m_IsSubscribedToEvents; |
| | 42 | | readonly VisualElement m_RootElement; |
| | 43 | |
|
| 0 | 44 | | int m_FontSize = 14; |
| 0 | 45 | | float m_FontSizeMultiplier = 1f; |
| | 46 | |
|
| 0 | 47 | | StyleLength m_FrameWidth = new StyleLength(new Length(k_FrameCountWidth, LengthUnit.Pixel)); |
| 0 | 48 | | StyleLength m_LevelWidth = new StyleLength(new Length(k_SourceLevelWidth, LengthUnit.Pixel)); |
| | 49 | |
|
| 0 | 50 | | public UIDocument Document { get; private set; } |
| 0 | 51 | | public GameObject ConsoleGameObject { get; private set; } |
| | 52 | |
|
| 0 | 53 | | public RuntimeConsoleController(GameObject parentGameObject, int initialFontSize) |
| 0 | 54 | | { |
| | 55 | | #if UNITY_EDITOR |
| | 56 | |
|
| | 57 | | // Helps with domain reload, removes the old object, and recreates |
| 0 | 58 | | if (parentGameObject.transform.childCount != 0) |
| 0 | 59 | | { |
| 0 | 60 | | int childCount = parentGameObject.transform.childCount; |
| 0 | 61 | | for (int i = 0; i < childCount; i++) |
| 0 | 62 | | { |
| 0 | 63 | | if (parentGameObject.transform.GetChild(i).gameObject.name == k_GameObjectName) |
| 0 | 64 | | { |
| 0 | 65 | | Object.Destroy(parentGameObject.transform.GetChild(i).gameObject); |
| 0 | 66 | | break; |
| | 67 | | } |
| 0 | 68 | | } |
| 0 | 69 | | } |
| | 70 | | #endif |
| | 71 | |
|
| | 72 | | // UIDocuments do not allow multiple components per Game Object so we have to make a child object. |
| 0 | 73 | | ConsoleGameObject = new GameObject(k_GameObjectName); |
| 0 | 74 | | ConsoleGameObject.transform.SetParent(parentGameObject.transform, false); |
| | 75 | |
|
| | 76 | | // Create isolated UI document (thanks Damian, boy do I feel stupid.) |
| 0 | 77 | | Document = ConsoleGameObject.AddComponent<UIDocument>(); |
| 0 | 78 | | Document.sortingOrder = float.MaxValue; // Above all |
| 0 | 79 | | Document.visualTreeAsset = ResourceProvider.GetUIElements().RuntimeConsole; |
| | 80 | |
|
| | 81 | | // Build out the required references |
| 0 | 82 | | m_RootElement = Document.rootVisualElement.Q<VisualElement>("gdx-console"); |
| 0 | 83 | | m_ConsoleBarElement = m_RootElement.Q<VisualElement>("gdx-console-bar"); |
| 0 | 84 | | m_InputLabel = m_ConsoleBarElement.Q<Label>("gdx-console-input"); |
| 0 | 85 | | m_SuggestionLabel = m_ConsoleBarElement.Q<Label>("gdx-console-suggestion"); |
| 0 | 86 | | m_InputCaret = m_ConsoleBarElement.Q<Label>("gdx-console-caret"); |
| | 87 | |
|
| | 88 | | // We use a very slimmed down view of the consoles logs here as it takes time to process. |
| 0 | 89 | | m_ConsoleListView = m_RootElement.Q<ListView>("gdx-console-list"); |
| 0 | 90 | | m_ConsoleScrollView = m_RootElement.Q<ScrollView>(""); |
| 0 | 91 | | m_ConsoleListView.bindItem += BindItem; |
| 0 | 92 | | m_ConsoleListView.makeItem += MakeItem; |
| 0 | 93 | | m_ConsoleListView.itemsSource = Console.Log; |
| | 94 | |
|
| 0 | 95 | | UpdateFontSize(initialFontSize); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | public void UpdateFontSize(int fontSize) |
| 0 | 99 | | { |
| 0 | 100 | | m_FontSize = fontSize; |
| 0 | 101 | | m_FontSizeMultiplier = (float)m_FontSize / k_SourceFontSize; |
| | 102 | |
|
| | 103 | | // Calculate our Category with based on the managed log longest |
| 0 | 104 | | m_FrameWidth = new StyleLength(new Length(Mathf.RoundToInt(k_FrameCountWidth * m_FontSizeMultiplier))); |
| 0 | 105 | | m_LevelWidth = new StyleLength(new Length(Mathf.RoundToInt(k_SourceLevelWidth * m_FontSizeMultiplier))); |
| 0 | 106 | | m_ConsoleBarElement.style.height = fontSize + 10; |
| 0 | 107 | | m_InputCaret.style.fontSize = fontSize; |
| 0 | 108 | | m_InputLabel.style.fontSize = fontSize; |
| 0 | 109 | | m_SuggestionLabel.style.fontSize = fontSize; |
| | 110 | |
|
| 0 | 111 | | m_ConsoleListView.Rebuild(); |
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | public void OnInputVerticalScroll(float delta) |
| 0 | 115 | | { |
| 0 | 116 | | m_ConsoleScrollView.Focus(); |
| 0 | 117 | | m_ConsoleScrollView.scrollOffset = |
| | 118 | | new Vector2( |
| | 119 | | m_ConsoleScrollView.scrollOffset.x, |
| | 120 | | m_ConsoleScrollView.scrollOffset.y - delta * 10f); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | public void OnInputUp() |
| 0 | 124 | | { |
| 0 | 125 | | int bufferCount = Console.PreviousCommandCount; |
| 0 | 126 | | if (bufferCount <= 0) |
| 0 | 127 | | { |
| 0 | 128 | | return; |
| | 129 | | } |
| | 130 | |
|
| 0 | 131 | | m_CommandBufferOffset++; |
| 0 | 132 | | if (m_CommandBufferOffset >= bufferCount) |
| 0 | 133 | | { |
| 0 | 134 | | m_CommandBufferOffset = bufferCount - 1; |
| 0 | 135 | | } |
| | 136 | |
|
| 0 | 137 | | m_InputBuilder.Clear(); |
| 0 | 138 | | m_InputBuilder.Append(Console.GetPreviousCommand(m_CommandBufferOffset)); |
| | 139 | |
|
| | 140 | |
|
| 0 | 141 | | m_InputLabel.text = m_InputBuilder.ToString(); |
| | 142 | |
|
| 0 | 143 | | ClearSuggestion(); |
| 0 | 144 | | } |
| | 145 | |
|
| | 146 | | public void OnInputDown() |
| 0 | 147 | | { |
| | 148 | |
|
| 0 | 149 | | int bufferCount = Console.PreviousCommandCount; |
| 0 | 150 | | if (bufferCount <= 0) |
| 0 | 151 | | { |
| 0 | 152 | | return; |
| | 153 | | } |
| | 154 | |
|
| 0 | 155 | | m_CommandBufferOffset--; |
| 0 | 156 | | if (m_CommandBufferOffset < 0) |
| 0 | 157 | | { |
| 0 | 158 | | m_CommandBufferOffset = -1; |
| 0 | 159 | | m_InputBuilder.Clear(); |
| 0 | 160 | | } |
| | 161 | | else |
| 0 | 162 | | { |
| 0 | 163 | | m_InputBuilder.Clear(); |
| 0 | 164 | | m_InputBuilder.Append(Console.GetPreviousCommand(m_CommandBufferOffset)); |
| 0 | 165 | | } |
| | 166 | |
|
| 0 | 167 | | m_SuggestionLabel.text = string.Empty; |
| 0 | 168 | | m_InputLabel.text = m_InputBuilder.ToString(); |
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | |
|
| | 172 | | public void OnInputLeft() |
| 0 | 173 | | { |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | public void OnInputRight() |
| 0 | 177 | | { |
| | 178 | |
|
| 0 | 179 | | } |
| | 180 | |
|
| | 181 | | public void OnInputSubmit() |
| 0 | 182 | | { |
| 0 | 183 | | if (m_SuggestionLabel.text != string.Empty) |
| 0 | 184 | | { |
| 0 | 185 | | m_InputBuilder.Append(m_SuggestionLabel.text); |
| 0 | 186 | | m_InputLabel.text = m_InputBuilder.ToString(); |
| 0 | 187 | | } |
| | 188 | | else |
| 0 | 189 | | { |
| 0 | 190 | | Console.QueueCommand(m_InputLabel.text); |
| 0 | 191 | | m_CommandBufferOffset = -1; |
| 0 | 192 | | m_InputLabel.text = ""; |
| 0 | 193 | | m_InputBuilder.Clear(); |
| 0 | 194 | | m_InputLabel.text = m_InputBuilder.ToString(); |
| 0 | 195 | | } |
| 0 | 196 | | ClearSuggestion(); |
| 0 | 197 | | } |
| | 198 | |
|
| | 199 | | public void OnInputBackspace() |
| 0 | 200 | | { |
| 0 | 201 | | if (m_InputBuilder.Length >= 1) |
| 0 | 202 | | { |
| 0 | 203 | | m_InputBuilder.Remove(m_InputBuilder.Length - 1, 1); |
| | 204 | |
|
| 0 | 205 | | m_InputLabel.text = m_InputBuilder.ToString(); |
| | 206 | |
|
| 0 | 207 | | ResetSuggestion(); |
| 0 | 208 | | } |
| 0 | 209 | | } |
| | 210 | |
|
| | 211 | | public void OnInputAutocomplete() |
| 0 | 212 | | { |
| 0 | 213 | | m_SuggestionLabel.text = ConsoleAutoCompleteProvider.UpdateSuggestion(m_InputLabel.text) |
| | 214 | | ? ConsoleAutoCompleteProvider.GetCurrentSuggestion() |
| | 215 | | : string.Empty; |
| 0 | 216 | | } |
| | 217 | |
|
| | 218 | | void ClearSuggestion() |
| 0 | 219 | | { |
| 0 | 220 | | m_SuggestionLabel.text = string.Empty; |
| 0 | 221 | | ConsoleAutoCompleteProvider.Reset(); |
| 0 | 222 | | } |
| | 223 | | void ResetSuggestion() |
| 0 | 224 | | { |
| 0 | 225 | | if (m_SuggestionLabel.text != string.Empty) |
| 0 | 226 | | { |
| 0 | 227 | | ConsoleAutoCompleteProvider.Reset(); |
| 0 | 228 | | OnInputAutocomplete(); |
| 0 | 229 | | } |
| 0 | 230 | | } |
| | 231 | |
|
| | 232 | | public void OnInputPageUp() |
| 0 | 233 | | { |
| 0 | 234 | | m_ConsoleScrollView.Focus(); |
| 0 | 235 | | m_ConsoleScrollView.scrollOffset = |
| | 236 | | new Vector2( |
| | 237 | | m_ConsoleScrollView.scrollOffset.x, |
| | 238 | | m_ConsoleScrollView.scrollOffset.y + (m_ConsoleScrollView.horizontalPageSize * 100)); |
| 0 | 239 | | } |
| | 240 | |
|
| | 241 | |
|
| | 242 | | public void OnInputPageDown() |
| 0 | 243 | | { |
| 0 | 244 | | m_ConsoleScrollView.Focus(); |
| 0 | 245 | | m_ConsoleScrollView.scrollOffset = |
| | 246 | | new Vector2( |
| | 247 | | m_ConsoleScrollView.scrollOffset.x, |
| | 248 | | m_ConsoleScrollView.scrollOffset.y - (m_ConsoleScrollView.horizontalPageSize * 100)); |
| 0 | 249 | | } |
| | 250 | |
|
| | 251 | | VisualElement MakeItem() |
| 0 | 252 | | { |
| 0 | 253 | | VisualElement itemBaseElement = new VisualElement { name = "gdx-console-item" }; |
| | 254 | |
|
| 0 | 255 | | Label timestampLabel = new Label { name = "gdx-console-item-frame", style = { fontSize = m_FontSize, width = |
| 0 | 256 | | Label levelLabel = new Label { name = "gdx-console-item-level" , style = { fontSize = m_FontSize, width = m_ |
| 0 | 257 | | Label messageLabel = new Label { name = "gdx-console-item-message", style = { fontSize = m_FontSize}, enable |
| | 258 | |
|
| 0 | 259 | | itemBaseElement.Add(timestampLabel); |
| 0 | 260 | | itemBaseElement.Add(levelLabel); |
| 0 | 261 | | itemBaseElement.Add(messageLabel); |
| | 262 | |
|
| 0 | 263 | | return itemBaseElement; |
| 0 | 264 | | } |
| | 265 | |
|
| | 266 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 267 | | static char LogLevelToIcon(LogType level) |
| 0 | 268 | | { |
| 0 | 269 | | switch (level) |
| | 270 | | { |
| | 271 | | case LogType.Error: |
| 0 | 272 | | return '\uf06a'; |
| | 273 | | case LogType.Assert: |
| 0 | 274 | | return '\uf2d3'; |
| | 275 | | case LogType.Warning: |
| 0 | 276 | | return '\uf071'; |
| | 277 | | case LogType.Log: |
| 0 | 278 | | return '\uf27a'; |
| | 279 | | case LogType.Exception: |
| 0 | 280 | | return '\uf188'; |
| | 281 | | } |
| 0 | 282 | | return '\uf27a'; |
| 0 | 283 | | } |
| | 284 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 285 | | static string LogLevelToClass(LogType level) |
| 0 | 286 | | { |
| 0 | 287 | | switch (level) |
| | 288 | | { |
| | 289 | | case LogType.Error: |
| 0 | 290 | | return "error"; |
| | 291 | | case LogType.Assert: |
| 0 | 292 | | return "assert"; |
| | 293 | | case LogType.Warning: |
| 0 | 294 | | return "warning"; |
| | 295 | | case LogType.Log: |
| 0 | 296 | | return "log"; |
| | 297 | | case LogType.Exception: |
| 0 | 298 | | return "exception"; |
| | 299 | | } |
| 0 | 300 | | return "default"; |
| 0 | 301 | | } |
| | 302 | |
|
| | 303 | | static void BindItem(VisualElement element, int index) |
| 0 | 304 | | { |
| 0 | 305 | | ConsoleLogEntry entry = Console.Log.GetEntryAt(index); |
| 0 | 306 | | element.ClearClassList(); |
| 0 | 307 | | element.AddToClassList(LogLevelToClass(entry.Level)); |
| | 308 | |
|
| 0 | 309 | | ((Label)element[0]).text = entry.FrameCount; |
| | 310 | |
|
| 0 | 311 | | ((Label)element[1]).text = LogLevelToIcon(entry.Level).ToString(); |
| 0 | 312 | | ((Label)element[1]).tooltip = entry.StackTrace; |
| | 313 | |
|
| 0 | 314 | | ((Label)element[2]).text = entry.Message; |
| 0 | 315 | | } |
| | 316 | |
|
| | 317 | | public void ClearInput() |
| 0 | 318 | | { |
| 0 | 319 | | m_InputBuilder.Clear(); |
| 0 | 320 | | m_InputLabel.text = ""; |
| 0 | 321 | | ClearSuggestion(); |
| 0 | 322 | | } |
| | 323 | |
|
| | 324 | | public void Tick() |
| 0 | 325 | | { |
| 0 | 326 | | if (Console.Log.Version != m_CurrentVersion) |
| 0 | 327 | | { |
| 0 | 328 | | m_CurrentVersion = Console.Log.Version; |
| 0 | 329 | | m_ConsoleListView.RefreshItems(); |
| 0 | 330 | | m_ConsoleListView.ScrollToItem(-1); |
| 0 | 331 | | } |
| 0 | 332 | | } |
| | 333 | |
|
| | 334 | | public void Show(Action delayedSubscribe) |
| 0 | 335 | | { |
| 0 | 336 | | m_InputLabel.schedule.Execute(delayedSubscribe); |
| 0 | 337 | | Tick(); |
| | 338 | |
|
| | 339 | | // This is bad, we have to scroll to fix things showing |
| 0 | 340 | | m_InputLabel.schedule.Execute(() => |
| 0 | 341 | | { |
| 0 | 342 | | m_ConsoleListView.ScrollToItem(-1); |
| 0 | 343 | | }); |
| 0 | 344 | | } |
| | 345 | |
|
| | 346 | |
|
| | 347 | | public void OnTextInput(char inputCharacter) |
| 0 | 348 | | { |
| | 349 | | // There are a few commands we can process for the console as they have defined text characters (in Ascii) |
| 0 | 350 | | int asciiCode = inputCharacter; |
| | 351 | |
|
| | 352 | | // Outside the range of ASCII we want to support |
| 0 | 353 | | if (asciiCode < 32 || asciiCode > 125) |
| 0 | 354 | | { |
| 0 | 355 | | return; |
| | 356 | | } |
| | 357 | |
|
| 0 | 358 | | m_InputBuilder.Append(inputCharacter); |
| 0 | 359 | | m_InputLabel.text = m_InputBuilder.ToString(); |
| | 360 | |
|
| 0 | 361 | | ResetSuggestion(); |
| 0 | 362 | | } |
| | 363 | | } |
| | 364 | | #endif // UNITY_2022_2_OR_NEWER |
| | 365 | | } |