static void HandleValidateCommands(TextEditor editor, Event ev) { string commandName = ev.commandName; switch (commandName) { case "Cut": case "Copy": if (editor.hasSelection) { ev.Use(); } break; case "Paste": if (editor.CanPaste()) { ev.Use(); } break; case "SelectAll": case "UndoRedoPerformed": ev.Use(); break; } }
private static void ShowTextEditorPopupMenu(TextEditor editor) { GenericMenu pm = new GenericMenu(); var enabled = GUI.enabled; var receiver = EditorWindow.focusedWindow; // Cut if (editor.hasSelection && !editor.isPasswordField && enabled) { pm.AddItem(EditorGUIUtility.TrTextContent("Cut"), false, () => receiver.SendEvent(EditorGUIUtility.CommandEvent(CommandName.Cut))); } else { pm.AddDisabledItem(EditorGUIUtility.TrTextContent("Cut")); } // Copy -- when GUI is disabled, allow Copy even with no selection (will copy everything) if ((editor.hasSelection || !enabled) && !editor.isPasswordField) { pm.AddItem(EditorGUIUtility.TrTextContent("Copy"), false, () => receiver.SendEvent(EditorGUIUtility.CommandEvent(CommandName.Copy))); } else { pm.AddDisabledItem(EditorGUIUtility.TrTextContent("Copy")); } // Paste if (editor.CanPaste() && enabled) { pm.AddItem(EditorGUIUtility.TrTextContent("Paste"), false, () => receiver.SendEvent(EditorGUIUtility.CommandEvent(CommandName.Paste))); } pm.ShowAsContext(); }
private void OnValidateCommand(Event current) { if (GUIUtility.keyboardControl == controlID) { string commandName = current.commandName; if (commandName != null) { if (!(commandName == "Cut") && !(commandName == "Copy")) { if (!(commandName == "Paste")) { if (!(commandName == "SelectAll") && !(commandName == "Delete")) { if (commandName == "UndoRedoPerformed") { _textEdtior.text = _tempContent.text; current.Use(); } } else { current.Use(); } } else if (_textEdtior.CanPaste()) { current.Use(); } } else if (_textEdtior.hasSelection) { current.Use(); } } } }
public override void OnGUI(Rect position, UnityEditor.SerializedProperty property, GUIContent label) { UnityEditor.SerializedProperty entityKind = property.FindPropertyRelative("entityKind"), domain = property.FindPropertyRelative("domain"), country = property.FindPropertyRelative("country"), category = property.FindPropertyRelative("category"), subCategory = property.FindPropertyRelative("subCategory"), specific = property.FindPropertyRelative("specific"), extra = property.FindPropertyRelative("extra"); float ShiftLeft = 8 + 7 * UnityEditor.EditorGUI.indentLevel; UnityEditor.EditorGUI.BeginProperty(position, label, property); float titleWidth = UnityEditor.EditorGUIUtility.labelWidth; Rect labelR = new Rect(position); labelR.width = titleWidth; UnityEditor.EditorGUI.LabelField(labelR, label); float indentationFix = 15 * UnityEditor.EditorGUI.indentLevel; float totalTextWidth = 4 * TextWidth - 6 * ShiftLeft; float partWidth = 4 + (position.width - titleWidth - totalTextWidth + indentationFix) / 4; Rect kindR = new Rect(labelR.xMax - indentationFix, position.y, partWidth, 16), colon1R = new Rect(kindR.x + partWidth - ShiftLeft, position.y, TextWidth, 16), domainR = new Rect(colon1R.x + colon1R.width - ShiftLeft, position.y, partWidth, 16), colon2R = new Rect(domainR.x + partWidth - ShiftLeft, position.y, TextWidth, 16), countryR = new Rect(colon2R.x + colon2R.width - ShiftLeft, position.y, partWidth, 16), colon3R = new Rect(countryR.x + partWidth - ShiftLeft, position.y, TextWidth, 16), categoryR = new Rect(colon3R.x + colon3R.width - ShiftLeft, position.y, partWidth, 16), colon4R = new Rect(categoryR.x + partWidth - ShiftLeft, position.y, TextWidth, 16), subCategoryR = new Rect(kindR.x, position.yMax - 16, partWidth, 16), colon5R = new Rect(colon1R.x, subCategoryR.y, TextWidth, 16), specificR = new Rect(domainR.x, colon5R.y, partWidth, 16), colon6R = new Rect(colon2R.x, specificR.y, TextWidth, 16), extraR = new Rect(countryR.x, colon6R.y, partWidth, 16); GUIStyle s = new GUIStyle(); s.alignment = TextAnchor.MiddleCenter; UnityEditor.EditorGUI.PropertyField(kindR, entityKind, GUIContent.none); UnityEditor.EditorGUI.LabelField(colon1R, ":", s); UnityEditor.EditorGUI.PropertyField(domainR, domain, GUIContent.none); UnityEditor.EditorGUI.LabelField(colon2R, ":", s); UnityEditor.EditorGUI.PropertyField(countryR, country, GUIContent.none); UnityEditor.EditorGUI.LabelField(colon3R, ":", s); UnityEditor.EditorGUI.PropertyField(categoryR, category, GUIContent.none); UnityEditor.EditorGUI.LabelField(colon4R, ":", s); UnityEditor.EditorGUI.PropertyField(subCategoryR, subCategory, GUIContent.none); UnityEditor.EditorGUI.LabelField(colon5R, ":", s); UnityEditor.EditorGUI.PropertyField(specificR, specific, GUIContent.none); UnityEditor.EditorGUI.LabelField(colon6R, ":", s); UnityEditor.EditorGUI.PropertyField(extraR, extra, GUIContent.none); UnityEditor.EditorGUI.EndProperty(); Rect propertyArea = Rect.MinMaxRect(kindR.xMin, kindR.yMin, categoryR.xMax + TextWidth, extraR.yMax); Event e = Event.current; if (e.type == EventType.MouseDown && e.button == 1 && propertyArea.Contains(e.mousePosition)) { UnityEditor.GenericMenu menu = new UnityEditor.GenericMenu(); TextEditor te = new TextEditor(); string value = new System.Text.StringBuilder() .Append(entityKind.intValue).Append(":") .Append(domain.intValue).Append(":") .Append(country.intValue).Append(":") .Append(category.intValue).Append(":") .Append(subCategory.intValue).Append(":") .Append(specific.intValue).Append(":") .Append(extra.intValue).ToString(); menu.AddItem(new GUIContent("Copy " + value, value), false, () => { te.text = value; te.SelectAll(); te.Copy(); }); te.text = string.Empty; if (te.CanPaste()) { te.Paste(); EntityType type; if (EntityType.TryFromString(te.text, out type, false)) { menu.AddItem(new GUIContent("Paste " + te.text, te.text), false, () => { entityKind.intValue = type.entityKind; domain.intValue = type.domain; country.intValue = type.country; category.intValue = type.category; subCategory.intValue = type.subCategory; specific.intValue = type.specific; extra.intValue = type.extra; property.serializedObject.ApplyModifiedProperties(); }); } else { menu.AddDisabledItem(new GUIContent("Paste as string", te.text)); } } else { menu.AddDisabledItem(new GUIContent("Paste as string")); } menu.ShowAsContext(); } }
public override void OnGUI(Rect position, UnityEditor.SerializedProperty property, GUIContent label) { UnityEditor.SerializedProperty site = property.FindPropertyRelative("site"), host = property.FindPropertyRelative("host"), entity = property.FindPropertyRelative("entity"); float ShiftLeft = 6 + 8 * UnityEditor.EditorGUI.indentLevel; UnityEditor.EditorGUI.BeginProperty(position, label, property); float titleWidth = UnityEditor.EditorGUIUtility.labelWidth; Rect labelR = new Rect(position); labelR.width = titleWidth; UnityEditor.EditorGUI.LabelField(labelR, label); float indentationFix = 15 * UnityEditor.EditorGUI.indentLevel; float totalTextWidth = 2 * TextWidth - 4 * ShiftLeft; float partWidth = (position.width - titleWidth - totalTextWidth + indentationFix) / 3; Rect siteR = new Rect(labelR.xMax - indentationFix, position.y, partWidth, position.height), colon1R = new Rect(siteR.xMax - ShiftLeft, position.y, TextWidth, position.height), hostR = new Rect(colon1R.xMax - ShiftLeft, position.y, partWidth, position.height), colon2R = new Rect(hostR.xMax - ShiftLeft, position.y, TextWidth, position.height), entityR = new Rect(colon2R.xMax - ShiftLeft, position.y, partWidth, position.height); UnityEditor.EditorGUI.PropertyField(siteR, site, GUIContent.none); UnityEditor.EditorGUI.PropertyField(hostR, host, GUIContent.none); UnityEditor.EditorGUI.PropertyField(entityR, entity, GUIContent.none); GUIStyle s = new GUIStyle(); s.alignment = TextAnchor.MiddleCenter; UnityEditor.EditorGUI.LabelField(colon1R, ":", s); UnityEditor.EditorGUI.LabelField(colon2R, ":", s); Rect propertyArea = Rect.MinMaxRect(siteR.xMin, siteR.yMin, entityR.xMax, entityR.yMax); Event e = Event.current; if (e.type == EventType.MouseDown && e.button == 1 && propertyArea.Contains(e.mousePosition)) { TextEditor te = new TextEditor(); UnityEditor.GenericMenu menu = new UnityEditor.GenericMenu(); string value = new System.Text.StringBuilder() .Append(site.intValue).Append(":") .Append(host.intValue).Append(":") .Append(entity.intValue).ToString(); GUIContent content = new GUIContent("Copy " + value, value); menu.AddItem(content, false, () => { te.text = value; te.SelectAll(); te.Copy(); }); te.text = string.Empty; if (te.CanPaste()) { te.Paste(); EntityId id; if (EntityId.TryFromString(te.text, out id, false)) { menu.AddItem(new GUIContent("Paste " + te.text, te.text), false, () => { site.intValue = id.site; host.intValue = id.host; entity.intValue = id.entity; property.serializedObject.ApplyModifiedProperties(); }); } else { menu.AddDisabledItem(new GUIContent("Paste as string")); } } else { menu.AddDisabledItem(new GUIContent("Paste as string")); } menu.ShowAsContext(); } UnityEditor.EditorGUI.EndProperty(); }
public string DrawFilterField(IInspector inspector, int id, Rect position, string text, bool setIsSelected, out bool textChanged) { var e = Event.current; var eventType = e.GetTypeForControl(id); editor.style = style; bool wasSelected = isSelected; if (setIsSelected != isSelected) { #if DEV_MODE && DEBUG_ENABLED Debug.Log(StringUtils.ToColorizedString("SearchBox.isSelected = ", setIsSelected)); #endif isSelected = setIsSelected; if (isSelected) { KeyboardControlUtility.KeyboardControl = id; BeginEditing(id, position, style); if (eventType == EventType.MouseDown || eventType == EventType.MouseUp) { editor.MoveCursorToPosition(e.mousePosition); if (GUI.skin.settings.cursorColor.a > 0f) { #if DEV_MODE && DEBUG_ENABLED Debug.Log("SearchBox.SelectAllOnMouseUp"); #endif selectAllOnMouseUp = true; } } else { #if DEV_MODE && DEBUG_ENABLED Debug.Log("SearchBox.SelectAll"); #endif editor.SelectAll(); } } } string textInput = text; if (text == null) { text = ""; } #if DEV_MODE && DEBUG_ENABLED if (e.rawType == EventType.MouseDown || e.rawType == EventType.MouseUp || GUIUtility.hotControl == id) { Debug.Log(StringUtils.ToColorizedString("SearchBox Event=", e, ", typeForControl=", eventType, ", button=", e.button, ", pos=", position, ", mousePos=", e.mousePosition, ", pos.Contains(mousePos)=", position.Contains(e.mousePosition), ", HasKeyboardFocus=", HasKeyboardFocus(), ", isSelected=", isSelected, ", GUIUtility.hotControl=", GUIUtility.hotControl, ", id=", id)); } #endif if (isSelected && inspector.InspectorDrawer.HasFocus) { KeyboardControlUtility.KeyboardControl = id; } if (HasKeyboardFocus() && eventType != EventType.Layout) { if (IsEditingControl()) { editor.position = position; editor.controlID = id; editor.DetectFocusChange(); } else if (DrawGUI.EditingTextField || eventType == EventType.ExecuteCommand && string.Equals(e.commandName, "NewKeyboardFocus")) { BeginEditing(id, position, style); if (GUI.skin.settings.cursorColor.a > 0f) { #if DEV_MODE && DEBUG_ENABLED Debug.Log("SearchBox.SelectAll"); #endif editor.SelectAll(); } if (e.GetTypeForControl(id) == EventType.ExecuteCommand) { DrawGUI.Use(e); } } } if (editor.controlID == id && !isSelected) { EndEditing(); } bool flag1 = false; string text1 = editor.text; switch (eventType) { case EventType.MouseDown: if (position.Contains(e.mousePosition) && e.button == 0) { var dropdownButtonRect = position; dropdownButtonRect.width = 15f; if (onDropdownClicked != null && dropdownButtonRect.Contains(e.mousePosition)) { onDropdownClicked(position); if (IsEditingControl()) { Clear(); EndEditing(); flag1 = true; } } else if (wasSelected) { if (e.clickCount == 2 && GUI.skin.settings.doubleClickSelectsWord) { editor.MoveCursorToPosition(Event.current.mousePosition); editor.SelectCurrentWord(); editor.MouseDragSelectsWholeWords(true); editor.DblClickSnap(TextEditor.DblClickSnapping.WORDS); dragToPosition = false; } else if (e.clickCount == 3 && GUI.skin.settings.tripleClickSelectsLine) { editor.MoveCursorToPosition(e.mousePosition); editor.SelectCurrentParagraph(); editor.MouseDragSelectsWholeWords(true); editor.DblClickSnap(TextEditor.DblClickSnapping.PARAGRAPHS); dragToPosition = false; } else { editor.MoveCursorToPosition(e.mousePosition); selectAllOnMouseUp = false; } } else { KeyboardControlUtility.KeyboardControl = id; BeginEditing(id, position, style); editor.MoveCursorToPosition(e.mousePosition); if (GUI.skin.settings.cursorColor.a > 0f) { #if DEV_MODE && DEBUG_ENABLED Debug.Log("SearchBox.SelectAllOnMouseUp"); #endif selectAllOnMouseUp = true; } } KeyboardControlUtility.JustClickedControl = id; DrawGUI.Use(e); } break; case EventType.MouseUp: if (GUIUtility.hotControl == id) { if (dragged && dragToPosition) { #if DEV_MODE && DEBUG_ENABLED Debug.Log("SearchBox.MoveSelectionToAltCursor"); #endif editor.MoveSelectionToAltCursor(); flag1 = true; } else if (postPoneMove) { editor.MoveCursorToPosition(e.mousePosition); } else if (selectAllOnMouseUp) { if (GUI.skin.settings.cursorColor.a > 0f) { #if DEV_MODE && DEBUG_ENABLED Debug.Log("SearchBox.SelectAll"); #endif editor.SelectAll(); } selectAllOnMouseUp = false; } editor.MouseDragSelectsWholeWords(false); dragToPosition = true; dragged = false; postPoneMove = false; if (e.button == 0) { KeyboardControlUtility.JustClickedControl = 0; DrawGUI.Use(e); } } break; case EventType.MouseDrag: if (GUIUtility.hotControl == id) { if (!e.shift && editor.hasSelection && dragToPosition) { #if DEV_MODE && DEBUG_ENABLED Debug.Log("SearchBox.MoveAltCursorToPosition"); #endif editor.MoveAltCursorToPosition(e.mousePosition); } else { if (e.shift) { #if DEV_MODE && DEBUG_ENABLED Debug.Log("SearchBox.MoveCursorToPosition"); #endif editor.MoveCursorToPosition(e.mousePosition); } else { #if DEV_MODE && DEBUG_ENABLED Debug.Log("SearchBox.SelectToPosition"); #endif editor.SelectToPosition(e.mousePosition); } dragToPosition = false; selectAllOnMouseUp = !editor.hasSelection; #if DEV_MODE && DEBUG_ENABLED Debug.Log("SearchBox.SelectAllOnMouseUp = " + selectAllOnMouseUp); #endif } dragged = true; DrawGUI.Use(e); } break; case EventType.KeyDown: if (!isSelected) { break; } bool flag2 = false; char character = e.character; if (IsEditingControl() && editor.HandleKeyEvent(e)) { DrawGUI.Use(e); flag1 = true; break; } if (e.keyCode == KeyCode.Escape) { if (IsEditingControl()) { Clear(); EndEditing(); flag1 = true; } } else if (character == 10 || character == 3) { if (!IsEditingControl()) { BeginEditing(id, position, style); editor.SelectAll(); } else { EndEditing(); } DrawGUI.Use(e); } else if (character == 9 || e.keyCode == KeyCode.Tab) { flag2 = true; } else if (character != 25 && character != 27 && IsEditingControl()) { if (character != 0) { editor.Insert(character); flag1 = true; } else if (Input.compositionString.Length > 0) { editor.ReplaceSelection(""); flag1 = true; } } if (IsEditingControl() && MightBePrintableKey(e) && !flag2) { DrawGUI.Use(e); } break; case EventType.Repaint: #if DEV_MODE && PI_ASSERTATIONS Debug.Assert(string.Equals(text, editor.text), "text (" + text + ") != editor.text (" + editor.text + ")"); #endif if (GUIUtility.hotControl == 0) { // new test if (inspector.MouseoveredPart != InspectorPart.None) { DrawGUI.Active.AddCursorRect(position, MouseCursor.Text); } } string renderedText = !IsEditingControl() ? text : editor.text; if (!IsEditingControl()) { style.Draw(position, GUIContentPool.Temp(renderedText), id, false); break; } editor.DrawCursor(renderedText); break; default: switch (eventType - 13) { case EventType.MouseDown: if (KeyboardControlUtility.KeyboardControl == id) { switch (e.commandName) { case "Cut": case "Copy": if (editor.hasSelection) { DrawGUI.Use(e); } break; case "Paste": if (editor.CanPaste()) { DrawGUI.Use(e); } break; case "SelectAll": case "Delete": DrawGUI.Use(e); break; case "UndoRedoPerformed": Text = text; DrawGUI.Use(e); break; } } break; case EventType.MouseUp: if (KeyboardControlUtility.KeyboardControl == id) { switch (e.commandName) { case "OnLostFocus": #if DEV_MODE && DEBUG_ENABLED Debug.Log("SearchBox - MouseUp / OnLostFocus: end editing"); #endif if (nowEditingAFilterField) { EndEditing(); } DrawGUI.Use(e); break; case "Cut": BeginEditing(id, position, style); editor.Cut(); flag1 = true; break; case "Copy": editor.Copy(); DrawGUI.Use(e); break; case "Paste": BeginEditing(id, position, style); editor.Paste(); flag1 = true; break; case "SelectAll": #if DEV_MODE && DEBUG_ENABLED Debug.Log("SearchBox.SelectAll"); #endif editor.SelectAll(); DrawGUI.Use(e); break; case "Delete": BeginEditing(id, position, style); if (SystemInfo.operatingSystemFamily == OperatingSystemFamily.MacOSX) { editor.Delete(); } else { editor.Cut(); } flag1 = true; DrawGUI.Use(e); break; } } break; case EventType.MouseDrag: if (position.Contains(e.mousePosition)) { if (!IsEditingControl()) { KeyboardControlUtility.KeyboardControl = id; BeginEditing(id, position, style); editor.MoveCursorToPosition(e.mousePosition); } ShowTextEditorPopupMenu(inspector); DrawGUI.Use(e); } break; } break; } textChanged = false; if (flag1) { textChanged = !string.Equals(text1, editor.text); if (Event.current.type != EventType.Used) { DrawGUI.Use(e); } } if (textChanged) { SaveCursorPositions(); GUI.changed = true; return(editor.text); } return(textInput); }