VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_BasicDevConsoleUI.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 
5 namespace AVR.Core {
6  /// <summary>
7  /// A rudimentary UI version of the AVR_DevConsole. If you wish to implement a better one, take a look at AVR_DevConsoleMirror.
8  /// </summary>
9  [AVR.Core.Attributes.DocumentationUrl("class_a_v_r_1_1_core_1_1_a_v_r___basic_dev_console_u_i.html")]
11  {
12  public GUIStyle style;
13  public Color backgroundColor = new Color(0.1f, 0.1f, 0.1f, 1.0f);
14  public Color textBackgroundColor = new Color(0.07f, 0.07f, 0.07f, 1.0f);
15  public Vector2 position = new Vector2(0, 0);
16  public Vector2 size = new Vector2(820, 420);
17 
18  private string cmd;
19  private Vector2 scrollPosition;
20  private Texture2D background, textBackground;
21  private KeyCode toggleKey;
22  private KeyCode offKey;
23  private KeyCode submitKey;
24  private bool active = false;
25  private int history = -1;
26 
27  void Awake() {
28  background = new Texture2D(1, 1, TextureFormat.RGBAFloat, false);
29  background.SetPixel(0, 0, backgroundColor);
30  background.Apply();
31 
32  textBackground = new Texture2D(1, 1, TextureFormat.RGBAFloat, false);
33  textBackground.SetPixel(0, 0, textBackgroundColor);
34  textBackground.Apply();
35 
36  toggleKey = AVR.Core.AVR_Settings.get_key("/settings/core/devConsole/toggleConsoleKey");
37  offKey = AVR.Core.AVR_Settings.get_key("/settings/core/devConsole/disableConsoleKey");
38  submitKey = AVR.Core.AVR_Settings.get_key("/settings/core/devConsole/submitConsoleKey");
39  }
40 
41  private void runCommand() {
42  AVR.Core.AVR_DevConsole.command(cmd);
43  cmd = "";
44  scrollPosition = new Vector2(0, float.PositiveInfinity);
45  history = -1;
46  }
47 
48  void OnGUI()
49  {
50  if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == toggleKey || Event.current.keyCode == offKey))
51  {
52  active = !active && Event.current.keyCode != offKey;
53  history = -1;
54  }
55  if(active) {
56  // We need check for submitKey *before* running FocusControl, or it wont work.
57  if (Event.current.type == EventType.KeyDown && Event.current.keyCode == submitKey) {
58  runCommand();
59  }
60  if(Event.current.type==EventType.KeyDown && Event.current.keyCode == KeyCode.UpArrow) {
61  history += 1;
62  cmd = AVR.Core.AVR_DevConsole.history(history);
63  }
64 
65  GUI.FocusControl("BasicDevConsoleUI_input");
66 
67  // Background box
68  style.normal.background = background;
69  GUI.Box(new Rect(position.x, position.y, size.x, size.y), "", style);
70 
71  // Content area
72  GUILayout.BeginArea(new Rect(position.x+10, position.y+10, size.x-20, size.y-20), style);
73 
74  // Console Output
75  style.normal.background = textBackground;
76  scrollPosition = GUILayout.BeginScrollView(scrollPosition, style);
77  GUILayout.TextArea(AVR.Core.AVR_DevConsole.get_text(), style);
78  style.normal.background = background;
79  GUILayout.EndScrollView();
80 
81  // Command input and run button
82  GUILayout.BeginHorizontal(style);
83  GUI.SetNextControlName("BasicDevConsoleUI_input");
84  cmd = GUILayout.TextField(cmd, style);
85  if(GUILayout.Button("Run", style, GUILayout.Width(50))) {
86  runCommand();
87  }
88  GUILayout.EndHorizontal();
89 
90  GUILayout.EndArea();
91  }
92  }
93  }
94 }
Monobehaviour but with an added URL button to a documentation page.
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
A rudimentary UI version of the AVR_DevConsole. If you wish to implement a better one...