Example #1
0
        /// <summary>Submits the form this element is in.</summary>
        public void submit()
        {
            FormTag elementForm = form;

            if (elementForm != null)
            {
                elementForm.submit();
            }
        }
Example #2
0
        public override bool OnClick(UIEvent clickEvent)
        {
            // Did the mouse go up, and was the element clicked down on too?
            if (!clickEvent.heldDown && Element.MouseWasDown())
            {
                // Focus it:
                Element.Focus();

                if (Type == InputType.Submit)
                {
                    // Find the form and then attempt to submit it.
                    FormTag form = Element.form;
                    if (form != null)
                    {
                        form.submit();
                    }
                }
                else if (IsScrollInput())
                {
                    // Clicked somewhere on a scrollbar:
                    // Figure out where the click was, and scroll there.
                }
                else if (Type == InputType.Radio)
                {
                    Select();
                }
                else if (Type == InputType.Checkbox)
                {
                    if (Checked)
                    {
                        Unselect();
                    }
                    else
                    {
                        Select();
                    }
                }
                else if (IsTextInput())
                {
                    // Move the cursor to the click point:
                    int localClick = clickEvent.clientX - Element.Style.Computed.OffsetLeft + Element.Style.Computed.ScrollLeft;
                    int index      = 0;

                    if (Element.childNodes.Count > 1)
                    {
                        // Note: If it's equal to 1, ele[0] is the cursor.
                        TextElement text = (TextElement)(Element.childNodes[0]);
                        if (text != null)
                        {
                            index = text.LetterIndex(localClick);
                        }
                    }

                    MoveCursor(index, true);
                }
            }

            base.OnClick(clickEvent);
            clickEvent.stopPropagation();

            return(true);
        }
Example #3
0
        public override void OnKeyPress(UIEvent pressEvent)
        {
            // We want to fire the nitro event first:
            base.OnKeyPress(pressEvent);

            // How long is the current value?
            int length = 0;

            if (Value != null)
            {
                length = Value.Length;
            }

            // Is the cursor too far over?
            if (CursorIndex > length)
            {
                MoveCursor(0);
            }

            if (pressEvent.cancelBubble)
            {
                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 cursorIndex
                        if (value == null)
                        {
                            value = "" + pressEvent.character;
                        }
                        else
                        {
                            value = value.Substring(0, CursorIndex) + pressEvent.character + value.Substring(CursorIndex, value.Length - CursorIndex);
                        }

                        SetValue(value);
                        MoveCursor(CursorIndex + 1);
                        return;
                    }

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

                    if (key == KeyCode.LeftArrow)
                    {
                        MoveCursor(CursorIndex - 1, true);
                    }
                    else if (key == KeyCode.RightArrow)
                    {
                        MoveCursor(CursorIndex + 1, true);
                    }
                    else if (key == KeyCode.Backspace)
                    {
                        // Delete the character before the cursor.
                        if (string.IsNullOrEmpty(value) || CursorIndex == 0)
                        {
                            return;
                        }
                        value = value.Substring(0, CursorIndex - 1) + value.Substring(CursorIndex, value.Length - CursorIndex);
                        int index = CursorIndex;
                        SetValue(value);
                        MoveCursor(index - 1);
                    }
                    else if (key == KeyCode.Delete)
                    {
                        // Delete the character after the cursor.
                        if (string.IsNullOrEmpty(value) || CursorIndex == value.Length)
                        {
                            return;
                        }

                        value = value.Substring(0, CursorIndex) + value.Substring(CursorIndex + 1, value.Length - CursorIndex - 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.

                        FormTag form = Element.form;
                        if (form != null && form.HasSubmitButton)
                        {
                            form.submit();
                        }

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

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

                        int maxCursor = 0;

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

                        MoveCursor(maxCursor, true);
                    }
                    else if (pressEvent.ctrlKey)
                    {
                        if (key == KeyCode.V)
                        {
                            // Run the onpaste function.
                            if (Element.RunBlocked("onpaste", pressEvent))
                            {
                                // It blocked it.
                                return;
                            }

                            // Paste the text, stripping any newlines:
                            string textToPaste = Clipboard.Paste().Replace("\r", "").Replace("\n", "");

                            if (!string.IsNullOrEmpty(textToPaste))
                            {
                                // Drop the character in the string at cursorIndex
                                if (value == null)
                                {
                                    value = "" + textToPaste;
                                }
                                else
                                {
                                    value = value.Substring(0, CursorIndex) + textToPaste + value.Substring(CursorIndex, value.Length - CursorIndex);
                                }

                                SetValue(value);
                                MoveCursor(CursorIndex + textToPaste.Length);
                            }
                        }
                        else if (key == KeyCode.C)
                        {
                            // Run the oncopy function.
                            if (Element.RunBlocked("oncopy", pressEvent))
                            {
                                // It blocked it.
                                return;
                            }

                            Clipboard.Copy(value);
                        }
                    }
                }
            }
        }