|
| 1 | +using UnityEngine; |
| 2 | +using static UnityEngine.GUI; |
| 3 | + |
| 4 | +namespace ModLoader |
| 5 | +{ |
| 6 | + |
| 7 | + public class ConsoleGUI : MonoBehaviour |
| 8 | + { |
| 9 | + private const string WINDOW_NAME = "Mod loader console"; |
| 10 | + private const float MENU_HEIGHT = 0.6f; |
| 11 | + private const float MENU_WIDTH = 0.4f; |
| 12 | + |
| 13 | + // show console initial position and size |
| 14 | + private Rect _consoleRect; |
| 15 | + |
| 16 | + //draw windows Gui items |
| 17 | + private WindowFunction _windosDraw; |
| 18 | + |
| 19 | + /// show in what position is the scroll of console |
| 20 | + private Vector2 _scrollConsole = Vector2.zero; |
| 21 | + private Vector2 _scrollScene = Vector2.zero; |
| 22 | + private Vector2 _scrollMods = Vector2.zero; |
| 23 | + |
| 24 | + private bool _isVisible; |
| 25 | + // show if the console is visible for user |
| 26 | + public bool IsVisible |
| 27 | + { |
| 28 | + get { return this._isVisible; } |
| 29 | + set { this._isVisible = value; } |
| 30 | + } |
| 31 | + |
| 32 | + // identify what tab render |
| 33 | + private int _activeTab; |
| 34 | + |
| 35 | + // tab list |
| 36 | + public string[] tabs = new string[] { "Console", "Scene", "Mods" }; |
| 37 | + |
| 38 | + // store all logs in game |
| 39 | + private GUIContent _logs; |
| 40 | + public string Logs |
| 41 | + { |
| 42 | + set |
| 43 | + { |
| 44 | + this._logs.text += value; |
| 45 | + if (this._activeTab == 0 && this._scrollConsole.y > this._consoleheight * 0.2) |
| 46 | + { |
| 47 | + this._scrollConsole.y = this._consoleheight; |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private GUIContent _sceneGameObjects; |
| 55 | + public GameObject[] SceneGameObjects |
| 56 | + { |
| 57 | + set |
| 58 | + { |
| 59 | + this._sceneGameObjects = new GUIContent(); |
| 60 | + Component[] components; |
| 61 | + foreach (GameObject sceneObject in value) |
| 62 | + { |
| 63 | + this._sceneGameObjects.text += sceneObject.name + "\n"; |
| 64 | + components = sceneObject.GetComponents(typeof(Component)); |
| 65 | + foreach (Component component in components) |
| 66 | + { |
| 67 | + this._sceneGameObjects.text += "\t" + component.ToString() + "\n"; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private GUIContent _mods; |
| 74 | + public SFSMod[] Mods |
| 75 | + { |
| 76 | + set |
| 77 | + { |
| 78 | + this._mods = new GUIContent(); |
| 79 | + foreach (SFSMod mod in value) |
| 80 | + { |
| 81 | + this._mods.text += "Name:" + mod.Name + "\n"; |
| 82 | + this._mods.text += "Id:" + mod.ModId + "\n"; |
| 83 | + this._mods.text += "Author:" + mod.Author + "\n"; |
| 84 | + this._mods.text += "Description:\n"; |
| 85 | + this._mods.text += mod.Description + "\n\n"; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // console styles like background and text color |
| 91 | + private GUIStyle _consoleStyle; |
| 92 | + |
| 93 | + // this make resize console |
| 94 | + private float _consoleheight; |
| 95 | + |
| 96 | + void Awake() |
| 97 | + { |
| 98 | + this._isVisible = true; |
| 99 | + this._consoleStyle = new GUIStyle(); |
| 100 | + this._logs = new GUIContent(); |
| 101 | + this._sceneGameObjects = new GUIContent(); |
| 102 | + this._mods = new GUIContent(); |
| 103 | + this._consoleRect = new Rect(Screen.width * 0.1f, Screen.height * 0.1f, Screen.width * MENU_WIDTH, Screen.height * MENU_HEIGHT); |
| 104 | + this._windosDraw = new GUI.WindowFunction(this.windowFunction); |
| 105 | + } |
| 106 | + |
| 107 | + void Start() |
| 108 | + { |
| 109 | + this._consoleStyle.normal.textColor = Color.white; |
| 110 | + this._consoleStyle.normal.background = this.makeTexture(Color.black); |
| 111 | + } |
| 112 | + |
| 113 | + /// <sumary> |
| 114 | + /// Create solid color texture |
| 115 | + ///</sumary> |
| 116 | + private Texture2D makeTexture(Color col) |
| 117 | + { |
| 118 | + Color[] pix = new Color[1]; |
| 119 | + for (int i = 0; i < pix.Length; ++i) |
| 120 | + { |
| 121 | + pix[i] = col; |
| 122 | + } |
| 123 | + Texture2D result = new Texture2D(1, 1); |
| 124 | + result.SetPixels(pix); |
| 125 | + result.Apply(); |
| 126 | + return result; |
| 127 | + } |
| 128 | + |
| 129 | + private void Update() |
| 130 | + { |
| 131 | + if (Input.GetKeyDown(KeyCode.F1)){ |
| 132 | + this._isVisible = !this._isVisible; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + async void OnGUI() |
| 137 | + { |
| 138 | + |
| 139 | + if (this._isVisible) |
| 140 | + { |
| 141 | + this._consoleRect = GUI.Window(GUIUtility.GetControlID(FocusType.Passive), this._consoleRect, this._windosDraw, WINDOW_NAME); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private void windowFunction(int windowID) |
| 146 | + { |
| 147 | + this._activeTab = GUI.Toolbar(new Rect(10, 20, this._consoleRect.width - 20f, 30), this._activeTab, this.tabs); |
| 148 | + switch (this._activeTab) |
| 149 | + { |
| 150 | + case 0: |
| 151 | + this.consoleTab(); |
| 152 | + break; |
| 153 | + case 1: |
| 154 | + this.sceneTab(); |
| 155 | + break; |
| 156 | + case 2: |
| 157 | + this.modsTab(); |
| 158 | + break; |
| 159 | + } |
| 160 | + GUI.DragWindow(); |
| 161 | + } |
| 162 | + |
| 163 | + private void consoleTab() |
| 164 | + { |
| 165 | + this._consoleheight = this._consoleStyle.CalcHeight(this._logs, this._consoleRect.width - 20f); |
| 166 | + if (this._consoleheight < this._consoleRect.height - 60f) |
| 167 | + { |
| 168 | + this._scrollConsole.y = this._consoleheight = this._consoleRect.height - 60f; |
| 169 | + } |
| 170 | + |
| 171 | + this._scrollConsole = GUI.BeginScrollView(new Rect(0, 55, this._consoleRect.width, this._consoleRect.height - 60f), this._scrollConsole, new Rect(0, 0, this._consoleRect.width - 20f, this._consoleheight)); |
| 172 | + |
| 173 | + GUI.Box(new Rect(10, 0, this._consoleRect.width - 30f, this._consoleheight), this._logs, this._consoleStyle); |
| 174 | + |
| 175 | + GUI.EndScrollView(); |
| 176 | + } |
| 177 | + |
| 178 | + private void modsTab() |
| 179 | + { |
| 180 | + this._consoleheight = this._consoleStyle.CalcHeight(this._mods, this._consoleRect.width - 20f); |
| 181 | + if (this._consoleheight < this._consoleRect.height - 60f) |
| 182 | + { |
| 183 | + this._scrollMods.y = this._consoleheight = this._consoleRect.height - 60f; |
| 184 | + } |
| 185 | + |
| 186 | + this._scrollMods = GUI.BeginScrollView(new Rect(0, 55, this._consoleRect.width, this._consoleRect.height - 60f), this._scrollMods, new Rect(0, 0, this._consoleRect.width - 20f, this._consoleheight)); |
| 187 | + |
| 188 | + GUI.Box(new Rect(10, 0, this._consoleRect.width - 30f, this._consoleheight), this._mods, this._consoleStyle); |
| 189 | + |
| 190 | + GUI.EndScrollView(); |
| 191 | + } |
| 192 | + |
| 193 | + private void sceneTab() |
| 194 | + { |
| 195 | + this._consoleheight = this._consoleStyle.CalcHeight(this._sceneGameObjects, this._consoleRect.width - 20f); |
| 196 | + if (this._consoleheight < this._consoleRect.height - 60f) |
| 197 | + { |
| 198 | + this._scrollScene.y = this._consoleheight = this._consoleRect.height - 60f; |
| 199 | + } |
| 200 | + |
| 201 | + this._scrollScene = GUI.BeginScrollView(new Rect(0, 55, this._consoleRect.width, this._consoleRect.height - 60f), this._scrollScene, new Rect(0, 0, this._consoleRect.width - 20f, this._consoleheight)); |
| 202 | + |
| 203 | + GUI.Box(new Rect(10, 0, this._consoleRect.width - 30f, this._consoleheight), this._sceneGameObjects, this._consoleStyle); |
| 204 | + |
| 205 | + GUI.EndScrollView(); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | +} |
0 commit comments