Ejemplo n.º 1
0
        //  Method returns true if input was captured by control, so no other controls, nor screen can use input in this update
        public override bool HandleInput(MyGuiInput input, bool hasKeyboardActiveControl, bool hasKeyboardActiveControlPrevious, bool isThisFirstHandleInput)
        {
            if (!Visible)
            {
                return(false);
            }

            bool ret = base.HandleInput(input, hasKeyboardActiveControl, hasKeyboardActiveControlPrevious, isThisFirstHandleInput);

            if (ret == false)
            {
                if ((IsMouseOver() == true) && (input.IsNewLeftMousePressed() == true))
                {
                    m_carriagePositionIndex = GetCarriagePositionFromMouseCursor();
                    ret = true;
                }

                if ((m_hasKeyboardActiveControl == true) && (hasKeyboardActiveControlPrevious == false))
                {
                    UpdateLastKeyPressTimes(null);
                }

                if (hasKeyboardActiveControl == true)
                {
                    bool isShiftPressed = input.IsKeyPress(Keys.LeftShift) || input.IsKeyPress(Keys.RightShift);
                    bool isCapsLockOn   = (ushort)MyGuiInput.GetKeyState(0x14) == 1;

                    //(deltaFromLastKeypress > MyGuiConstants.TEXTBOX_KEYPRESS_DELAY)
                    //int deltaFromLastKeypress = MyMinerGame.TotalGameTime_Miliseconds - m_lastKeyPressTime;

                    input.GetPressedKeys(m_pressedKeys);

                    for (int i = 0; i < m_pressedKeys.Count; i++)
                    {
                        System.Diagnostics.Debug.Assert(input.IsKeyPress((Keys)m_pressedKeys[i]));
                    }

                    //  Transform pressed letters, characters, numbers, etc to real character/string
                    for (int i = 0; i < m_pressedKeys.Count; i++)
                    {
                        bool tempShift     = isShiftPressed;
                        bool transformText = true;
                        MyGuiControlTextboxKeyToString pressedKey = m_keyToString[(int)m_pressedKeys[i]];

                        //Allow to enter only digits in case such textbox type is set
                        if (m_type == MyGuiControlTextboxType.DIGITS_ONLY)
                        {
                            if ((pressedKey != null) && !input.IsKeyDigit(pressedKey.Key) && pressedKey.Key != Keys.OemPeriod && pressedKey.Key != Keys.Decimal)
                            {
                                transformText = false;
                            }
                        }

                        if (transformText)
                        {
                            //  If we have mapping from this key to string (that means it's allowed character)
                            if (pressedKey != null && m_text.Length < m_maxLength && pressedKey.Character != null && pressedKey.CharacterWhenShift != null)
                            {
                                ret = true;
                                if (IsEnoughDelay((Keys)m_pressedKeys[i], MyGuiConstants.TEXTBOX_CHANGE_DELAY))
                                {
                                    if (Char.IsLetter(pressedKey.Character, 0))
                                    {
                                        if (isCapsLockOn)
                                        {
                                            tempShift = !tempShift;//carefull here variable is not used anymore so we can invert it
                                        }
                                    }
                                    m_text = m_text.Insert(m_carriagePositionIndex, (tempShift == true) ? pressedKey.CharacterWhenShift : pressedKey.Character);

                                    m_carriagePositionIndex++;
                                    UpdateLastKeyPressTimes((Keys)m_pressedKeys[i]);

                                    OnTextChanged();
                                }
                            }
                        }
                    }

                    //  Move left
                    if ((input.IsKeyPress(Keys.Left)) && (IsEnoughDelay(Keys.Left, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        m_carriagePositionIndex--;
                        if (m_carriagePositionIndex < 0)
                        {
                            m_carriagePositionIndex = 0;
                        }
                        UpdateLastKeyPressTimes(Keys.Left);
                    }

                    //  Move right
                    if ((input.IsKeyPress(Keys.Right)) && (IsEnoughDelay(Keys.Right, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        m_carriagePositionIndex++;
                        if (m_carriagePositionIndex > m_text.Length)
                        {
                            m_carriagePositionIndex = m_text.Length;
                        }
                        UpdateLastKeyPressTimes(Keys.Right);
                    }

                    //  Move home
                    if ((input.IsNewKeyPress(Keys.Home)) && (IsEnoughDelay(Keys.Home, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        m_carriagePositionIndex = 0;
                        UpdateLastKeyPressTimes(Keys.Home);
                    }

                    //  Move end
                    if ((input.IsNewKeyPress(Keys.End)) && (IsEnoughDelay(Keys.End, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        m_carriagePositionIndex = m_text.Length;
                        UpdateLastKeyPressTimes(Keys.End);
                    }

                    //  Delete
                    if ((input.IsKeyPress(Keys.Delete)) && (IsEnoughDelay(Keys.Delete, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        if (m_carriagePositionIndex < m_text.Length)
                        {
                            m_text = m_text.Remove(m_carriagePositionIndex, 1);
                            UpdateLastKeyPressTimes(Keys.Delete);

                            OnTextChanged();
                        }
                    }

                    //  Backspace
                    if ((input.IsKeyPress(Keys.Back)) && (IsEnoughDelay(Keys.Back, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        if (m_carriagePositionIndex > 0)
                        {
                            // Handle text scrolling, basicaly try hold carriage on same position in textBox window (avoid textBox with hidden characters)
                            var carriagePositionChange = GetCarriagePosition(m_carriagePositionIndex) - GetCarriagePosition(m_carriagePositionIndex - 1);;
                            var textAreaPosition       = GetTextAreaPosition();
                            if (m_slidingWindowPosition.X - carriagePositionChange.X >= textAreaPosition.X)
                            {
                                m_slidingWindowPosition.X -= carriagePositionChange.X;
                            }
                            else
                            {
                                m_slidingWindowPosition.X = textAreaPosition.X;
                            }

                            m_text = m_text.Remove(m_carriagePositionIndex - 1, 1);
                            m_carriagePositionIndex--;
                            UpdateLastKeyPressTimes(Keys.Back);

                            OnTextChanged();
                        }
                    }

                    ResetLastKeyPressTimes(input);
                    m_formattedAlready = false;
                }
                else
                {
                    if (m_type == MyGuiControlTextboxType.DIGITS_ONLY && m_formattedAlready == false && !string.IsNullOrEmpty(m_text))
                    {
                        var number        = MyValueFormatter.GetDecimalFromString(m_text, 1);
                        int decimalDigits = (number - (int)number > 0) ? 1 : 0;
                        m_text = MyValueFormatter.GetFormatedFloat((float)number, decimalDigits, "");
                        m_carriagePositionIndex = m_text.Length;
                        m_formattedAlready      = true;
                    }
                }
            }

            return(ret);
        }
Ejemplo n.º 2
0
        //  Method returns true if input was captured by control, so no other controls, nor screen can use input in this update
        public override bool HandleInput(MyGuiInput input, bool hasKeyboardActiveControl, bool hasKeyboardActiveControlPrevious, bool isThisFirstHandleInput)
        {
            if (!Visible)
            {
                return false;
            }

            bool ret = base.HandleInput(input, hasKeyboardActiveControl, hasKeyboardActiveControlPrevious, isThisFirstHandleInput);
            
            if (ret == false)
            {
                if ((IsMouseOver() == true) && (input.IsNewLeftMousePressed() == true))                    
                {
                    m_carriagePositionIndex = GetCarriagePositionFromMouseCursor();
                    ret = true;
                }

                if ((m_hasKeyboardActiveControl == true) && (hasKeyboardActiveControlPrevious == false))
                {
                    UpdateLastKeyPressTimes(null);
                }

                if (hasKeyboardActiveControl == true)
                {
                    bool isShiftPressed = input.IsKeyPress(Keys.LeftShift) || input.IsKeyPress(Keys.RightShift);
                    bool isCapsLockOn = (ushort)MyGuiInput.GetKeyState(0x14) == 1;

                    //(deltaFromLastKeypress > MyGuiConstants.TEXTBOX_KEYPRESS_DELAY)
                    //int deltaFromLastKeypress = MyMinerGame.TotalGameTime_Miliseconds - m_lastKeyPressTime;

                    input.GetPressedKeys(m_pressedKeys);

                    for (int i = 0; i < m_pressedKeys.Count; i++)
                    {
                        System.Diagnostics.Debug.Assert(input.IsKeyPress((Keys)m_pressedKeys[i]));
                    }

                    //  Transform pressed letters, characters, numbers, etc to real character/string
                    for (int i = 0; i < m_pressedKeys.Count; i++)
                    {
                        bool tempShift = isShiftPressed;
                        bool transformText = true;
                        MyGuiControlTextboxKeyToString pressedKey = m_keyToString[(int)m_pressedKeys[i]];

                        //Allow to enter only digits in case such textbox type is set
                        if (m_type == MyGuiControlTextboxType.DIGITS_ONLY)
                        {
                            if ((pressedKey != null) && !input.IsKeyDigit(pressedKey.Key) && pressedKey.Key != Keys.OemPeriod && pressedKey.Key != Keys.Decimal)
                            {
                                transformText = false;
                            }
                        }

                        if (transformText)  
                        {
                            //  If we have mapping from this key to string (that means it's allowed character)
                            if (pressedKey != null && m_text.Length < m_maxLength && pressedKey.Character != null && pressedKey.CharacterWhenShift != null)
                            {
                                ret = true;
                                if (IsEnoughDelay((Keys)m_pressedKeys[i], MyGuiConstants.TEXTBOX_CHANGE_DELAY))
                                {
                                    if (Char.IsLetter(pressedKey.Character, 0))
                                    {
                                        if (isCapsLockOn)
                                            tempShift = !tempShift;//carefull here variable is not used anymore so we can invert it
                                        
                                    }
                                    m_text = m_text.Insert(m_carriagePositionIndex, (tempShift == true) ? pressedKey.CharacterWhenShift : pressedKey.Character);
                                
                                    m_carriagePositionIndex++;
                                    UpdateLastKeyPressTimes((Keys)m_pressedKeys[i]);

                                    OnTextChanged();
                                }
                            }
                        }
                    }

                    //  Move left
                    if ((input.IsKeyPress(Keys.Left)) && (IsEnoughDelay(Keys.Left, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        m_carriagePositionIndex--;
                        if (m_carriagePositionIndex < 0) m_carriagePositionIndex = 0;
                        UpdateLastKeyPressTimes(Keys.Left);
                    }

                    //  Move right
                    if ((input.IsKeyPress(Keys.Right)) && (IsEnoughDelay(Keys.Right, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        m_carriagePositionIndex++;
                        if (m_carriagePositionIndex > m_text.Length) m_carriagePositionIndex = m_text.Length;
                        UpdateLastKeyPressTimes(Keys.Right);
                    }

                    //  Move home
                    if ((input.IsNewKeyPress(Keys.Home)) && (IsEnoughDelay(Keys.Home, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        m_carriagePositionIndex = 0;
                        UpdateLastKeyPressTimes(Keys.Home);
                    }

                    //  Move end
                    if ((input.IsNewKeyPress(Keys.End)) && (IsEnoughDelay(Keys.End, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        m_carriagePositionIndex = m_text.Length;
                        UpdateLastKeyPressTimes(Keys.End);
                    }

                    //  Delete
                    if ((input.IsKeyPress(Keys.Delete)) && (IsEnoughDelay(Keys.Delete, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        if (m_carriagePositionIndex < m_text.Length)
                        {
                            m_text = m_text.Remove(m_carriagePositionIndex, 1);
                            UpdateLastKeyPressTimes(Keys.Delete);

                            OnTextChanged();
                        }
                    }

                    //  Backspace
                    if ((input.IsKeyPress(Keys.Back)) && (IsEnoughDelay(Keys.Back, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        if (m_carriagePositionIndex > 0)
                        {
                            // Handle text scrolling, basicaly try hold carriage on same position in textBox window (avoid textBox with hidden characters)
                            var carriagePositionChange = GetCarriagePosition(m_carriagePositionIndex) - GetCarriagePosition(m_carriagePositionIndex-1); ;
                            var textAreaPosition = GetTextAreaPosition();
                            if (m_slidingWindowPosition.X - carriagePositionChange.X >= textAreaPosition.X)
                            {
                                m_slidingWindowPosition.X -= carriagePositionChange.X;
                            }
                            else
                            {
                                m_slidingWindowPosition.X = textAreaPosition.X;
                            }

                            m_text = m_text.Remove(m_carriagePositionIndex - 1, 1);
                            m_carriagePositionIndex--;
                            UpdateLastKeyPressTimes(Keys.Back);

                            OnTextChanged();
                        }
                    }

                    ResetLastKeyPressTimes(input);
                    m_formattedAlready = false;
                }
                else
                {
                    if (m_type == MyGuiControlTextboxType.DIGITS_ONLY && m_formattedAlready == false && !string.IsNullOrEmpty(m_text))
                    {
                        var number = MyValueFormatter.GetDecimalFromString(m_text, 1);
                        int decimalDigits = (number - (int)number > 0) ? 1 : 0;
                        m_text = MyValueFormatter.GetFormatedFloat((float)number, decimalDigits, "");
                        m_carriagePositionIndex = m_text.Length;
                        m_formattedAlready = true;
                    }
                }
            }

            return ret;
        }