Example #1
0
        public override void OnClickEvent(MouseEvent clickEvent)
        {
            // Focus it:
            focus();

            if (Type == InputType.Submit)
            {
                // Find the form and then attempt to submit it.
                HtmlFormElement f = form;
                if (f != null)
                {
                    f.submit(this);
                }
            }
            else if (Type == InputType.Radio)
            {
                Select();
            }
            else if (Type == InputType.Checkbox)
            {
                if (Checked_)
                {
                    Unselect();
                }
                else
                {
                    Select();
                }
            }
            else if (IsTextInput())
            {
                // Move the caret to the clicked point.

                // Get the text content:
                RenderableTextNode htn = TextHolder;

                if (htn == null)
                {
                    // Index is just 0.
                    return;
                }

                // Get the letter index:
                int index = htn.LetterIndex(clickEvent.clientX, clickEvent.clientY);

                // Move the caret there (requesting a redraw):
                MoveCaret(index, true);
            }
        }
Example #2
0
        public override void OnKeyPress(KeyboardEvent pressEvent)
        {
            if (this["readonly"] != null)
            {
                return;
            }

            if (Type == InputType.Number)
            {
                if (!char.IsNumber(pressEvent.character) && pressEvent.character != '.' && !char.IsControl(pressEvent.character))
                {
                    // Not a number, point or control character. Block it:
                    return;
                }
            }

            if (pressEvent.heldDown)
            {
                if (IsTextInput())
                {
                    // Add to value if pwd/text, unless it's backspace:
                    string value = Value;

                    if (!char.IsControl(pressEvent.character) && pressEvent.character != '\0')
                    {
                        // Drop the character in the string at caretIndex
                        if (value == null)
                        {
                            value = "" + pressEvent.character;
                        }
                        else
                        {
                            value = value.Substring(0, CaretIndex) + pressEvent.character + value.Substring(CaretIndex, value.Length - CaretIndex);
                        }

                        SetValue(value);
                        MoveCaret(CaretIndex + 1);
                        return;
                    }

                    // Grab the keycode:
                    KeyCode key = pressEvent.unityKeyCode;

                    if (key == KeyCode.LeftArrow)
                    {
                        MoveCaret(CaretIndex - 1, true);
                    }
                    else if (key == KeyCode.RightArrow)
                    {
                        MoveCaret(CaretIndex + 1, true);
                    }
                    else if (key == KeyCode.Backspace)
                    {
                        // Delete the character before the caret.

                        // Got a selection?
                        if (Caret != null && Caret.TryDeleteSelection())
                        {
                            return;
                        }

                        if (string.IsNullOrEmpty(value) || CaretIndex == 0)
                        {
                            return;
                        }
                        value = value.Substring(0, CaretIndex - 1) + value.Substring(CaretIndex, value.Length - CaretIndex);
                        int index = CaretIndex;
                        SetValue(value);
                        MoveCaret(index - 1);
                    }
                    else if (key == KeyCode.Delete)
                    {
                        // Delete the character after the caret.

                        // Got a selection?
                        if (Caret != null && Caret.TryDeleteSelection())
                        {
                            return;
                        }

                        if (string.IsNullOrEmpty(value) || CaretIndex == value.Length)
                        {
                            return;
                        }

                        value = value.Substring(0, CaretIndex) + value.Substring(CaretIndex + 1, value.Length - CaretIndex - 1);
                        SetValue(value);
                    }
                    else if (key == KeyCode.Return || key == KeyCode.KeypadEnter)
                    {
                        // Does the form have a submit button? If so, submit now.
                        // Also call a convenience (non-standard) "onenter" method.

                        HtmlFormElement f = form;
                        if (f != null)
                        {
                            // Get the first submit button:
                            HtmlElement submitButton = f.GetSubmitButton();

                            if (submitButton != null)
                            {
                                // Submit it now:
                                f.submit(submitButton);
                            }
                        }

                        return;
                    }
                    else if (key == KeyCode.Home)
                    {
                        // Hop to the start:

                        MoveCaret(0, true);
                    }
                    else if (key == KeyCode.End)
                    {
                        // Hop to the end:

                        int maxCaret = 0;

                        if (value != null)
                        {
                            maxCaret = value.Length;
                        }

                        MoveCaret(maxCaret, true);
                    }
                }
                else if (Type == InputType.Submit)
                {
                    // Submit button.
                    if (!char.IsControl(pressEvent.character) && pressEvent.character != '\0')
                    {
                        return;
                    }

                    // Grab the keycode:
                    KeyCode key = pressEvent.unityKeyCode;

                    if (key == KeyCode.Return || key == KeyCode.KeypadEnter)
                    {
                        // Find the form and then attempt to submit it.
                        HtmlFormElement f = form;

                        if (f != null)
                        {
                            f.submit(this);
                        }
                    }
                }
            }

            base.OnKeyPress(pressEvent);
        }