Esempio n. 1
0
        /// <inheritdoc />
        public override bool OnKeyboardInputGiven(Event inputEvent, KeyConfigs keys)
        {
            InspectorUtility.ActiveInspector.OnNextLayout(TruncateAndSelectAllTextIfEditing);

            switch (inputEvent.keyCode)
            {
            case KeyCode.Escape:
            {
                if (DrawGUI.EditingTextField)
                {
                    GUI.changed = true;
                    KeyboardControlUtility.KeyboardControl = 0;
                    DrawGUI.EditingTextField = false;
                    DrawGUI.Use(inputEvent);
                    return(true);
                }
                return(false);
            }

            case KeyCode.Return:
            case KeyCode.KeypadEnter:
                if (inputEvent.modifiers == EventModifiers.None && !ReadOnly)
                {
                    GUI.changed = true;
                    DrawGUI.Use(inputEvent);

                    if (!DrawGUI.EditingTextField)
                    {
                        StartEditingField();
                    }
                    else
                    {
                        KeyboardControlUtility.KeyboardControl = 0;
                        DrawGUI.EditingTextField = false;
                    }
                    return(true);
                }
                return(false);

            case KeyCode.F2:
                if (!ReadOnly)
                {
                    GUI.changed = true;
                    DrawGUI.Use(inputEvent);
                    StartEditingField();
                    return(true);
                }
                return(false);

                                #if DEV_MODE
            case KeyCode.U:
                if (Event.current.modifiers == EventModifiers.Control && !ReadOnly)
                {
                    GUI.changed = true;
                    DrawGUI.Use(inputEvent);
                    Value = char.ToUpper(Value);
                    return(true);
                }
                return(false);

            case KeyCode.L:
                if (Event.current.modifiers == EventModifiers.Control && !ReadOnly)
                {
                    GUI.changed = true;
                    DrawGUI.UseEvent();
                    Value = char.ToLower(Value);
                    ApplyValueToField();
                    return(true);
                }
                return(false);

            case KeyCode.UpArrow:
                if (inputEvent.modifiers == EventModifiers.FunctionKey)
                {
                    DrawGUI.Use(inputEvent);
                    SelectNextFieldUp(GetSelectedRowIndex());
                    return(true);
                }
                return(false);

            case KeyCode.DownArrow:
                if (inputEvent.modifiers == EventModifiers.FunctionKey)
                {
                    DrawGUI.Use(inputEvent);
                    SelectNextFieldDown(GetSelectedRowIndex());
                    return(true);
                }
                return(false);

            case KeyCode.LeftArrow:
                if (inputEvent.modifiers == EventModifiers.FunctionKey)
                {
                    DrawGUI.Use(inputEvent);
                    SelectNextFieldLeft(false, false);
                    return(true);
                }
                return(false);

            case KeyCode.RightArrow:
                                        #if DEV_MODE
                Debug.Log(StringUtils.ToColorizedString(ToString(), " RightArrow with modifiers=", StringUtils.ToString(inputEvent.modifiers)));
                                        #endif
                if (inputEvent.modifiers == EventModifiers.FunctionKey)
                {
                    DrawGUI.Use(inputEvent);
                    SelectNextFieldRight(false, false);
                    return(true);
                }
                return(false);
                                #endif
            default:
                if (inputEvent.character != 0 && inputEvent.modifiers == EventModifiers.None && !ReadOnly)
                {
                    switch (inputEvent.character)
                    {
                    //TO DO: Don't insert tab, return etc.
                    case '\t':
                    case '\n':
                    case '\r':
                        //break;
                        //UPDATE: with only a break here,
                        //it was causing the field to get selected again
                        //immediately after being deselected
                        //via the return or enter keys
                        return(false);

                    default:
                        Value = inputEvent.character;
                        break;
                    }
                    GUI.changed = true;
                    DrawGUI.Use(inputEvent);
                    StartEditingField();
                    return(true);
                }
                break;
            }

            if (DrawGUI.EditingTextField && keys.DetectTextFieldReservedInput(inputEvent, TextFieldType.TextRow))
            {
                                #if DEV_MODE
                Debug.Log(GetType().Name + ".OnKeyboardInputGiven aborting because detected text field reserved input\nInput=" + StringUtils.ToString(inputEvent));
                                #endif

                return(false);
            }

            if (base.OnKeyboardInputGiven(inputEvent, keys))
            {
                return(true);
            }

            // Prevent keyboard events from bleeding elsewhere. E.g. when F was pressed, would cause Scene view to focus to selected Object.
            DrawGUI.Use(inputEvent);
            return(true);
        }
Esempio n. 2
0
        /// <inheritdoc cref="IDrawer.OnKeyboardInputGiven" />
        public override bool OnKeyboardInputGiven(Event inputEvent, KeyConfigs keys)
        {
                        #if DEV_MODE && DEBUG_KEYBOARD_INPUT
            Debug.Log(ToString() + ".OnKeyboardInputGiven(" + StringUtils.ToString(inputEvent) + ")");
                        #endif

            if (DrawGUI.EditingTextField)
            {
                switch (inputEvent.keyCode)
                {
                case KeyCode.Escape:
                {
                    GUI.changed = true;
                    StopEditingField();
                    DrawGUI.Use(inputEvent);
                    return(true);
                }

                case KeyCode.Return:
                case KeyCode.KeypadEnter:
                {
                    if (inputEvent.modifiers == EventModifiers.None)
                    {
                        GUI.changed = true;
                        StopEditingField();
                        DrawGUI.Use(inputEvent);
                        return(true);
                    }
                    return(false);
                }
                }

                if (keys.DetectTextFieldReservedInput(inputEvent, TextFieldType.Numeric))
                {
                    return(false);
                }
                return(base.OnKeyboardInputGiven(inputEvent, keys));
            }

            switch (inputEvent.keyCode)
            {
            case KeyCode.Alpha0:
            case KeyCode.Alpha1:
            case KeyCode.Alpha2:
            case KeyCode.Alpha3:
            case KeyCode.Alpha4:
            case KeyCode.Alpha5:
            case KeyCode.Alpha6:
            case KeyCode.Alpha7:
            case KeyCode.Alpha8:
            case KeyCode.Alpha9:
            case KeyCode.Keypad0:
            case KeyCode.Keypad1:
            case KeyCode.Keypad2:
            case KeyCode.Keypad3:
            case KeyCode.Keypad4:
            case KeyCode.Keypad5:
            case KeyCode.Keypad6:
            case KeyCode.Keypad7:
            case KeyCode.Keypad8:
            case KeyCode.Keypad9:
                if (inputEvent.modifiers == EventModifiers.None)
                {
                    GUI.changed = true;
                    string valueString = inputEvent.ToString();

                    char valueChar    = valueString[valueString.Length - 1];
                    var  valueNumeric = char.GetNumericValue(valueChar);
                    var  setValue     = (TValue)Convert.ChangeType(valueNumeric, typeof(TValue));

                                                #if DEV_MODE
                    Debug.Log("valueString=" + valueString + ", valueChar=" + valueChar + ", valueNumeric=" + valueNumeric + ", setValue=" + setValue);
                                                #endif

                    Value = setValue;
                    DrawGUI.Use(inputEvent);
                    StartEditingField();

                                                #if UNITY_EDITOR
                    TextEditorUtility.MoveCursorToTextEditorEnd();
                                                #endif
                    return(true);
                }
                break;

            case KeyCode.Return:
            case KeyCode.KeypadEnter:
            {
                if (inputEvent.modifiers == EventModifiers.None && !ReadOnly)
                {
                    GUI.changed = true;
                    DrawGUI.Use(inputEvent);
                    StartEditingField();
                    return(true);
                }
                return(false);
            }

            case KeyCode.Comma:
            case KeyCode.Period:
            case KeyCode.KeypadPeriod:
                if (!ReadOnly)
                {
                                                #if UNITY_EDITOR
                    InspectorUtility.ActiveInspector.OnNextLayout(() => TextEditorUtility.Insert('.'));
                                                #endif
                    return(true);
                }
                return(false);

            case KeyCode.Plus:
            case KeyCode.KeypadPlus:
                if (!ReadOnly)
                {
                    GUI.changed = true;
                    DrawGUI.Use(inputEvent);
                    double valueNumeric = (double)Convert.ChangeType(Value, Types.Double) + 1d;
                    Value = (TValue)Convert.ChangeType(valueNumeric, typeof(TValue));
                    return(true);
                }
                return(false);

            case KeyCode.Minus:
            case KeyCode.KeypadMinus:
                if (!ReadOnly)
                {
                    GUI.changed = true;
                    DrawGUI.Use(inputEvent);
                    double valueNumeric = (double)Convert.ChangeType(Value, Types.Double) - 1d;
                    Value = (TValue)Convert.ChangeType(valueNumeric, typeof(TValue));
                    return(true);
                }
                return(false);

            case KeyCode.F2:
                if (!ReadOnly)
                {
                    GUI.changed = true;
                    DrawGUI.Use(inputEvent);
                    StartEditingField();
                    return(true);
                }
                return(false);
            }

            return(base.OnKeyboardInputGiven(inputEvent, keys));
        }
Esempio n. 3
0
        /// <inheritdoc cref="IDrawer.OnKeyboardInputGiven" />
        public override bool OnKeyboardInputGiven(Event inputEvent, KeyConfigs keys)
        {
                        #if DEV_MODE && DEBUG_KEYBOARD_INPUT
            Debug.Log(StringUtils.ToColorizedString(ToString(), ".OnKeyboardInputGiven(", inputEvent, ") with DrawGUI.EditingTextField=", DrawGUI.EditingTextField, ")"));
                        #endif

            if (DrawGUI.EditingTextField)
            {
                switch (inputEvent.keyCode)
                {
                case KeyCode.Escape:
                    if (!ReadOnly)
                    {
                        DrawGUI.Use(inputEvent);
                        StopEditingField();
                        return(true);
                    }
                    return(false);

                case KeyCode.Return:
                case KeyCode.KeypadEnter:
                {
                    if (inputEvent.modifiers == EventModifiers.None)
                    {
                        if (!textArea)
                        {
                            DrawGUI.Use(inputEvent);
                            StopEditingField();
                            return(true);
                        }
                        return(false);
                    }

                    var textEditor  = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
                    int cursorIndex = textEditor.cursorIndex;
                    textEditor.text         = string.Concat(textEditor.text.Substring(0, cursorIndex), Environment.NewLine, textEditor.text.Substring(cursorIndex));
                    textEditor.cursorIndex += Environment.NewLine.Length;
                    return(true);
                }
                }

                if (keys.DetectTextFieldReservedInput(inputEvent, lastDrawPosition.height > DrawGUI.SingleLineHeight ? TextFieldType.TextArea : TextFieldType.TextRow))
                {
                                        #if DEV_MODE
                    Debug.Log(GetType().Name + ".OnKeyboardInputGiven aborting because detected text field reserved input\ntextArea=" + textArea + "\nInput=" + StringUtils.ToString(inputEvent));
                                        #endif

                    return(false);
                }
                return(base.OnKeyboardInputGiven(inputEvent, keys));
            }

            switch (inputEvent.keyCode)
            {
            case KeyCode.Return:
            case KeyCode.KeypadEnter:
                if (inputEvent.modifiers == EventModifiers.None && !ReadOnly)
                {
                    DrawGUI.Use(inputEvent);
                    //this needs to be delayed or the text field's content can get replaced by a new line character
                    InspectorUtility.ActiveInspector.OnNextLayout(() => InspectorUtility.ActiveInspector.OnNextLayout(StartEditingField));
                    DrawGUI.Use(inputEvent);
                    return(true);
                }
                return(false);

            case KeyCode.F2:
                if (!ReadOnly)
                {
                    GUI.changed = true;
                    DrawGUI.Use(inputEvent);
                    StartEditingField();
                    return(true);
                }
                return(false);

                                #if DEV_MODE
            case KeyCode.U:
                if (Event.current.modifiers == EventModifiers.Control && !ReadOnly)
                {
                    if (MixedContent)
                    {
                        Debug.LogWarning("Converting mixed content to upper case not yet supported...");
                        return(true);
                    }

                    if (string.IsNullOrEmpty(Value))
                    {
                        return(true);
                    }

                    GUI.changed = true;
                    DrawGUI.Use(inputEvent);
                    Value = Value.ToUpper();
                    return(true);
                }
                return(false);

            case KeyCode.L:
                if (Event.current.modifiers == EventModifiers.Control && !ReadOnly)
                {
                    if (MixedContent)
                    {
                        Debug.LogWarning("Converting mixed content to lower case not yet supported...");
                        return(true);
                    }

                    if (string.IsNullOrEmpty(Value))
                    {
                        return(true);
                    }

                    GUI.changed = true;
                    DrawGUI.Use(inputEvent);
                    Value = Value.ToLower();
                    ApplyValueToField();
                    return(true);
                }
                return(false);
                                #endif
            default:
                if (inputEvent.character != 0 && inputEvent.modifiers == EventModifiers.None && !ReadOnly)
                {
                    switch (inputEvent.character)
                    {
                    //TO DO: Don't append tab, return etc. at the end of a field
                    case '\t':
                    case '\n':
                    case '\r':
                        //break;
                        //UPDATE: with only a break here,
                        //it was causing the field to get selected again
                        //immediately after being deselected
                        //via the return or enter keys
                        return(false);

                    default:
                        Value = string.Concat(Value, inputEvent.character.ToString());
                        break;
                    }
                    GUI.changed = true;
                    DrawGUI.Use(inputEvent);
                    StartEditingField();

                                                #if UNITY_EDITOR
                    TextEditorUtility.MoveCursorToTextEditorEnd();
                                                #endif

                    return(true);
                }
                break;
            }

            return(base.OnKeyboardInputGiven(inputEvent, keys));
        }