private void ShowEditor() { GUILayout.BeginHorizontal(); GUILayout.Label(useContinuationPrompt ? Shell.CONTINUATION_PROMPT : Shell.MAIN_PROMPT, EditorStyles.wordWrappedLabel, GUILayout.Width(37)); lnEditorState.text = codeToProcess; lnEditorState = UnityREPLHelper.NumberedTextArea(editorControlName, lnEditorState); codeToProcess = lnEditorState.text; GUILayout.EndHorizontal(); }
public static NumberedEditorState NumberedTextArea(string controlName, NumberedEditorState editorState) { // This is a WAG about Unity's box model. Seems to work though, so... yeah. float effectiveWidgetHeight = 7 * GUI.skin.label.lineHeight + GUI.skin.label.padding.top + GUI.skin.label.padding.bottom; Rect r = EditorGUILayout.BeginVertical(); if(r.width > 0) { editorState.scrollViewWidth = r.width; editorState.scrollViewHeight = r.height; } editorState.scrollPos = GUILayout.BeginScrollView(editorState.scrollPos, false, false, CachedStyle("HorizontalScrollbar"), CachedStyle("VerticalScrollbar"), CachedStyle("TextField"), GUILayout.Height(effectiveWidgetHeight)); GUILayout.BeginHorizontal(); GUILayout.Label(editorState.lineNumberingContent, NumberedEditorStyles.LineNumbering); GUIContent txt = new GUIContent(editorState.text); GUIContent dTxt = new GUIContent(editorState.dummyText); float minW, maxW; NumberedEditorStyles.NumberedEditor.CalcMinMaxWidth(dTxt, out minW, out maxW); GUI.SetNextControlName(controlName); Rect editorRect = GUILayoutUtility.GetRect(txt, NumberedEditorStyles.NumberedEditor, GUILayout.Width(maxW)); editorRect.width = maxW; bool wasMouseDrag = Event.current.type == EventType.MouseDrag; bool wasRelevantEvent = wasMouseDrag || Event.current.type == EventType.KeyDown; editorState.text = GUI.TextField(editorRect, editorState.text, NumberedEditorStyles.NumberedEditor); if((GUI.GetNameOfFocusedControl() == controlName) && wasRelevantEvent) { int editorId = GUIUtility.keyboardControl; TextEditor te = GUIUtility.QueryStateObject(typeof(System.Object), editorId) as TextEditor; int pos = te.cursorIndex; // TODO: How does this play with keyboard selection? We want the actual cursor pos, not necessarily the right-end. if(pos != editorState.lastPos) { Vector2 cursorPixelPos = NumberedEditorStyles.NumberedEditor.GetCursorPixelPosition(editorRect, txt, pos); cursorPixelPos.y -= 1; // 0-align... float yBuffer = NumberedEditorStyles.NumberedEditor.lineHeight * 2; float xBuffer = 40f; // TODO: Make this a little less arbitrary? if(wasMouseDrag) { yBuffer = 0; xBuffer = 0; } if(editorState.scrollViewWidth > 0) { if(cursorPixelPos.y + yBuffer > editorState.scrollPos.y + editorState.scrollViewHeight - NumberedEditorStyles.NumberedEditor.lineHeight) editorState.scrollPos.y = cursorPixelPos.y + yBuffer + NumberedEditorStyles.NumberedEditor.lineHeight - editorState.scrollViewHeight; if(cursorPixelPos.y - yBuffer < editorState.scrollPos.y) editorState.scrollPos.y = cursorPixelPos.y - yBuffer; if(cursorPixelPos.x + xBuffer > editorState.scrollPos.x + editorState.scrollViewWidth) editorState.scrollPos.x = cursorPixelPos.x + xBuffer - editorState.scrollViewWidth; if(cursorPixelPos.x - xBuffer < editorState.scrollPos.x) editorState.scrollPos.x = cursorPixelPos.x - xBuffer; } } editorState.lastPos = pos; } GUILayout.EndHorizontal(); GUILayout.EndScrollView(); EditorGUILayout.EndVertical(); return editorState; }
public static NumberedEditorState NumberedTextArea(string controlName, NumberedEditorState editorState) { // This is a WAG about Unity's box model. Seems to work though, so... yeah. float effectiveWidgetHeight = 7 * GUI.skin.label.lineHeight + GUI.skin.label.padding.top + GUI.skin.label.padding.bottom ; Rect r = EditorGUILayout.BeginVertical(); if (r.width > 0) { editorState.scrollViewWidth = r.width; editorState.scrollViewHeight = r.height; } editorState.scrollPos = GUILayout.BeginScrollView(editorState.scrollPos, false, false, CachedStyle("HorizontalScrollbar"), CachedStyle("VerticalScrollbar"), CachedStyle("TextField"), GUILayout.Height(effectiveWidgetHeight)); GUILayout.BeginHorizontal(); GUILayout.Label(editorState.lineNumberingContent, NumberedEditorStyles.LineNumbering); GUIContent txt = new GUIContent(editorState.text); GUIContent dTxt = new GUIContent(editorState.dummyText); float minW, maxW; NumberedEditorStyles.NumberedEditor.CalcMinMaxWidth(dTxt, out minW, out maxW); GUI.SetNextControlName(controlName); Rect editorRect = GUILayoutUtility.GetRect(txt, NumberedEditorStyles.NumberedEditor, GUILayout.Width(maxW)); editorRect.width = maxW; bool wasMouseDrag = Event.current.type == EventType.MouseDrag; bool wasRelevantEvent = wasMouseDrag || Event.current.type == EventType.KeyDown; editorState.text = GUI.TextField(editorRect, editorState.text, NumberedEditorStyles.NumberedEditor); if ((GUI.GetNameOfFocusedControl() == controlName) && wasRelevantEvent) { int editorId = GUIUtility.keyboardControl; TextEditor te = GUIUtility.QueryStateObject(typeof(System.Object), editorId) as TextEditor; int pos = te.pos; // TODO: How does this play with keyboard selection? We want the actual cursor pos, not necessarily the right-end. if (pos != editorState.lastPos) { Vector2 cursorPixelPos = NumberedEditorStyles.NumberedEditor.GetCursorPixelPosition(editorRect, txt, pos); cursorPixelPos.y -= 1; // 0-align... float yBuffer = NumberedEditorStyles.NumberedEditor.lineHeight * 2; float xBuffer = 40f; // TODO: Make this a little less arbitrary? if (wasMouseDrag) { yBuffer = 0; xBuffer = 0; } if (editorState.scrollViewWidth > 0) { if (cursorPixelPos.y + yBuffer > editorState.scrollPos.y + editorState.scrollViewHeight - NumberedEditorStyles.NumberedEditor.lineHeight) { editorState.scrollPos.y = cursorPixelPos.y + yBuffer + NumberedEditorStyles.NumberedEditor.lineHeight - editorState.scrollViewHeight; } if (cursorPixelPos.y - yBuffer < editorState.scrollPos.y) { editorState.scrollPos.y = cursorPixelPos.y - yBuffer; } if (cursorPixelPos.x + xBuffer > editorState.scrollPos.x + editorState.scrollViewWidth) { editorState.scrollPos.x = cursorPixelPos.x + xBuffer - editorState.scrollViewWidth; } if (cursorPixelPos.x - xBuffer < editorState.scrollPos.x) { editorState.scrollPos.x = cursorPixelPos.x - xBuffer; } } } editorState.lastPos = pos; } GUILayout.EndHorizontal(); GUILayout.EndScrollView(); EditorGUILayout.EndVertical(); return(editorState); }