public static string DrawText(string text, float realHeight, params GUILayoutOption[] options) { textContent.text = text; Rect bounds = EditorGUILayout.GetControlRect(options); float guiHeight = bounds.height; /// BUG: GetControlRect (when EventType is Layout) is returning value incorrectly.... /// This issue have BIG impact on scrolling system if (bounds.size == Vector2.one) { bounds = lastWorkingRect; } else { lastWorkingRect = bounds; } var ev = Event.current; if (realHeight > guiHeight) { bounds.width -= GUI.skin.verticalScrollbar.fixedWidth; } text = GUI.TextArea(bounds, text, EditorStyles.textArea); var id = GUIUtility.keyboardControl; TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), id); #if UNITY_5_2 if (editor.content == null || editor.content.text != text) #else if (editor.text == null || editor.text != text) #endif { /* if (message2focus >= 0) { * GUIUtility.keyboardControl = id - 1; * editor = (TextEditor)GUIUtility.GetStateObject (typeof(TextEditor), id - 1); * editor.cursorIndex = message2focus; * message2focus = -1; * } else*/ return(text); //Maybe we catch the wrong one? } if (realHeight > guiHeight) { HandleScrolling(bounds, editor, ev, GUI.skin.textArea); } //GUI can't handle cut/copy operations. so DIY. var typ = ev.GetTypeForControl(id); if (typ == EventType.ExecuteCommand) { var scroll = editor.scrollOffset; if (HandleExecuteCommands(editor, ev)) { editor.scrollOffset = scroll; #if UNITY_5_2 text = editor.content.text; #else text = editor.text; #endif GUI.changed = true; } } else if (typ == EventType.ValidateCommand) { HandleValidateCommands(editor, ev); } else if (typ == EventType.KeyDown) { if (ev.character == '\t') { editor.ReplaceSelection("\t"); ev.Use(); GUI.changed = true; #if UNITY_5_2 text = editor.content.text; #else text = editor.text; #endif } } //Handle tab (autosuggestions) (not yet ready) /*if (typ == EventType.KeyDown && (ev.keyCode == KeyCode.Tab || ev.keyCode == KeyCode.Space) && ev.modifiers == EventModifiers.None) { * var l = text.Length; * if (HandleAutoComplete (editor, ref text)) { * editor.cursorIndex += text.Length - l; * message2focus = editor.cursorIndex; * ev.Use (); * } * }*/ int pos = editor.cursorIndex; //Find & Highlight Left Group int first = FindBeforeGroup(text, pos); bool redrawCursor = false; if (first >= 0) { DrawHighlight(style, editor, text, first, first + 1, Color.yellow); redrawCursor = true; } //Find & Highlight Right Group int last = FindAfterGroup(text, pos); if (last < text.Length) { DrawHighlight(style, editor, text, last, last + 1, Color.yellow); redrawCursor = true; } //Do we need to redraw cursor? if (editor.cursorIndex != editor.selectIndex) { redrawCursor = false; } else { //Find & Highlight Escapes int res = FindPossibleEscape(text, pos, out first, out last); if (res > 0) { Color scheme; switch (res) { case 1: scheme = Color.green; break; case 2: scheme = Color.cyan; break; case 3: scheme = new Color(0.8f, 1f, 0); break; default: scheme = new Color(0.7f, 0.7f, 0.7f); break; } DrawHighlight(style, editor, text, first, last + 1, scheme); redrawCursor = true; } } if (redrawCursor) { bounds.position -= editor.scrollOffset; style.DrawCursor(bounds, textContent, id, editor.cursorIndex); } return(text); }
private void OnGUI() { if (!m_showConsole) { return; } float y = 0; GUI.Box(new Rect(0, y, Screen.width, VIEW_HEIGHT), ""); Rect viewport = new Rect(0, 0, Screen.width - 30, LINE_SIZE * BUFFER_SIZE); m_scroll = GUI.BeginScrollView(new Rect(0, y + VIEW_BORDER_SIZE, Screen.width, LINE_SIZE * VIEW_LENGTH - VIEW_BORDER_SIZE), m_scroll, viewport); int i = 0; foreach (ConsoleText line in m_buffer) { if (string.IsNullOrEmpty(line.text)) { continue; } Rect labelRect = new Rect(VIEW_BORDER_SIZE, LINE_SIZE * i, viewport.width - 100, LINE_SIZE); GUI.Label(labelRect, line.text, LogTypeToStyle(line.type)); i++; } GUI.EndScrollView(); y += VIEW_HEIGHT; GUI.Box(new Rect(0, y, Screen.width, LINE_SIZE * 2), ""); GUI.Label(new Rect(VIEW_BORDER_SIZE, y + 9, Screen.width - LINE_SIZE, LINE_SIZE + 8), m_input, m_textStyle); m_textStyle.DrawCursor(new Rect(VIEW_BORDER_SIZE, y + 8, Screen.width - LINE_SIZE, LINE_SIZE), new GUIContent(m_input), 0, Mathf.Min(m_input.Length, m_cursorIndex)); //m_input = GUI.TextField(r, m_input, m_textStyle); y += LINE_SIZE + LINE_SIZE / 2; if (!string.IsNullOrEmpty(m_input)) { m_hintScroll = GUI.BeginScrollView( new Rect(0, y + VIEW_BORDER_SIZE, Screen.width, LINE_SIZE * 8 - VIEW_BORDER_SIZE), m_hintScroll, new Rect(0, 0, Screen.width - 30, LINE_SIZE * 8)); m_hints = MatchStringToCommand(m_input); for (int j = 0; j < m_hints.Count; j++) { if (m_hints[j] == null || string.IsNullOrEmpty(m_hints[j])) { continue; } GUI.Box(new Rect(0, (LINE_SIZE + 10) * j, Screen.width - 30, LINE_SIZE + 10), ""); Rect labelRect = new Rect(VIEW_BORDER_SIZE, (LINE_SIZE + 10) * j + 5, viewport.width - 100, LINE_SIZE); if (j == m_index - 1) { GUI.Label(labelRect, m_hints[j], m_hintSelectStyle); } else { GUI.Label(labelRect, m_hints[j], m_hintStyle); } } GUI.EndScrollView(); } GUI.backgroundColor = new Color(0, 0, 0, 0); }