internal static string DoDoubleClickTextField(Rect rect, Editor editor, EditorWindow editorWindow, string id, string text, int dragId, IList draggableList, int draggedItemIndex, GUIStyle defaultStyle = null, GUIStyle editingStyle = null) { if (defaultStyle == null) { defaultStyle = EditorStyles.label; } if (editingStyle == null) { editingStyle = EditorStyles.textField; } GUI.SetNextControlName(id); bool isMouseDown = Event.current.rawType == EventType.MouseDown; bool isMouseDownInside = isMouseDown && rect.Contains(Event.current.mousePosition); bool forceDeselect = isMouseDown && (_doubleClickTextFieldId == id && !isMouseDownInside || _doubleClickTextFieldId != id && isMouseDownInside); if (isMouseDownInside && _doubleClickTextFieldId != id) { if (Event.current.clickCount == 2) { forceDeselect = false; // Activate edit mode _doubleClickTextFieldId = id; EditorGUI.FocusTextInControl(id); // DeGUI.ExitCurrentEvent(); if (editor != null) { editor.Repaint(); } else { editorWindow.Repaint(); } } else if (draggableList != null) { // Start drag if (editor != null) { DeGUIDrag.StartDrag(dragId, editor, draggableList, draggedItemIndex); } else { DeGUIDrag.StartDrag(dragId, editorWindow, draggableList, draggedItemIndex); } } } // bool selected = _doubleClickTextFieldId == id && GUI.GetNameOfFocusedControl() == id; bool selected = _doubleClickTextFieldId == id && (Event.current.type != EventType.Layout || GUI.GetNameOfFocusedControl() == id); if (!selected) { EditorGUIUtility.AddCursorRect(rect, MouseCursor.Arrow); if (GUI.GetNameOfFocusedControl() == id) { GUIUtility.keyboardControl = 0; } } text = EditorGUI.TextField(rect, text, selected ? editingStyle : defaultStyle); // End editing if (forceDeselect) { goto Deselect; } if (!selected) { return(text); } if ( Event.current.isKey && Event.current.keyCode == KeyCode.Return || Event.current.type == EventType.MouseDown && !rect.Contains(Event.current.mousePosition) ) { goto Deselect; } else { goto End; } Deselect: GUIUtility.keyboardControl = 0; _doubleClickTextFieldId = null; if (editor != null) { editor.Repaint(); } else { editorWindow.Repaint(); } End: return(text); }
/// <summary> /// Exits the current event correctly, also taking care of eventual drag operations /// </summary> public static void ExitCurrentEvent() { DeGUIDrag.EndDrag(false); // Event.current.Use(); Event.current.type = EventType.Used; }