protected override void ValueChanged(Sandbox.Graphics.GUI.MyGuiControlBase sender)
        {
            MyRenderProxy.SetSettingsDirty();

            if (m_radioUpdate)
            {
                return;
            }

            m_radioUpdate = true;
            foreach (var item in m_cbs)
            {
                if (item != sender)
                {
                    item.IsChecked = false;
                }
            }
            m_radioUpdate = false;
        }
Exemple #2
0
        public override MyGuiControlBase HandleInput()
        {
            MyGuiControlBase baseResult = base.HandleInput();

            if (HasFocus && Selectable)
            {
                //  Move left
                switch (m_keyThrottler.GetKeyStatus(MyKeys.Left))
                {
                case ThrottledKeyStatus.PRESSED_AND_WAITING:
                    return(this);

                case ThrottledKeyStatus.PRESSED_AND_READY:
                    if (MyInput.Static.IsAnyCtrlKeyPressed())
                    {
                        CarriagePositionIndex = GetPreviousSpace();
                    }
                    else
                    {
                        CarriagePositionIndex--;
                    }

                    if (MyInput.Static.IsAnyShiftKeyPressed())
                    {
                        m_selection.SetEnd(this);
                    }
                    else
                    {
                        m_selection.Reset(this);
                    }
                    return(this);
                }

                //  Move right
                switch (m_keyThrottler.GetKeyStatus(MyKeys.Right))
                {
                case ThrottledKeyStatus.PRESSED_AND_WAITING:
                    return(this);

                case ThrottledKeyStatus.PRESSED_AND_READY:
                    if (MyInput.Static.IsAnyCtrlKeyPressed())
                    {
                        CarriagePositionIndex = GetNextSpace();
                    }
                    else
                    {
                        ++CarriagePositionIndex;
                    }
                    if (MyInput.Static.IsAnyShiftKeyPressed())
                    {
                        m_selection.SetEnd(this);
                    }
                    else
                    {
                        m_selection.Reset(this);
                    }
                    return(this);
                }

                //  Move Down
                switch (m_keyThrottler.GetKeyStatus(MyKeys.Down))
                {
                case ThrottledKeyStatus.PRESSED_AND_WAITING:
                    return(this);

                case ThrottledKeyStatus.PRESSED_AND_READY:
                    CarriagePositionIndex = GetIndexUnderCarriage(CarriagePositionIndex);
                    if (MyInput.Static.IsAnyShiftKeyPressed())
                    {
                        m_selection.SetEnd(this);
                    }
                    else
                    {
                        m_selection.Reset(this);
                    }
                    return(this);
                }

                //  Move Up
                switch (m_keyThrottler.GetKeyStatus(MyKeys.Up))
                {
                case ThrottledKeyStatus.PRESSED_AND_WAITING:
                    return(this);

                case ThrottledKeyStatus.PRESSED_AND_READY:
                    CarriagePositionIndex = GetIndexOverCarriage(CarriagePositionIndex);
                    if (MyInput.Static.IsAnyShiftKeyPressed())
                    {
                        m_selection.SetEnd(this);
                    }
                    else
                    {
                        m_selection.Reset(this);
                    }
                    return(this);
                }

                //Copy
                if (m_keyThrottler.IsNewPressAndThrottled(MyKeys.C) && MyInput.Static.IsAnyCtrlKeyPressed())
                {
                    m_selection.CopyText(this);
                }

                //Select All
                if (m_keyThrottler.IsNewPressAndThrottled(MyKeys.A) && MyInput.Static.IsAnyCtrlKeyPressed())
                {
                    m_selection.SelectAll(this);
                    return(this);
                }
            }

            //scroll
            bool captured   = false;
            var  deltaWheel = MyInput.Static.DeltaMouseScrollWheelValue();

            if (IsMouseOver && deltaWheel != 0)
            {
                m_scrollbar.ChangeValue(-0.0005f * deltaWheel);
                captured = true;
            }

            if (m_drawScrollbar)
            {
                bool capturedScrollbar = m_scrollbar.HandleInput();

                if (capturedScrollbar || captured)
                {
                    return(this);
                }
            }

            if (IsMouseOver && m_label.HandleInput(GetPositionAbsoluteTopLeft(), m_scrollbar.Value))
            {
                return(this);
            }

            if (Selectable)
            {
                if (MyInput.Static.IsNewLeftMousePressed())
                {
                    if (IsMouseOver)
                    {
                        m_selection.Dragging  = true;
                        CarriagePositionIndex = GetCarriagePositionFromMouseCursor();
                        if (MyInput.Static.IsAnyShiftKeyPressed())
                        {
                            m_selection.SetEnd(this);
                        }
                        else
                        {
                            m_selection.Reset(this);
                        }
                        return(this);
                    }
                    else
                    {
                        m_selection.Reset(this);
                    }
                }

                else if (MyInput.Static.IsNewLeftMouseReleased())
                {
                    m_selection.Dragging = false;
                }

                //user holding the mouse button
                else if (m_selection.Dragging)
                {
                    //If inside, we update selection and move the carriage (dragging what you want to select)
                    if (IsMouseOver)
                    {
                        CarriagePositionIndex = GetCarriagePositionFromMouseCursor();
                        m_selection.SetEnd(this);
                    }

                    //Otherwise, do the "scroll along with selection" effect
                    else if (HasFocus)
                    {
                        Vector2 mousePos        = MyGuiManager.MouseCursorPosition;
                        Vector2 positionTopLeft = GetPositionAbsoluteTopLeft();
                        if (mousePos.Y < positionTopLeft.Y)
                        {
                            m_scrollbar.ChangeValue(Position.Y - mousePos.Y);
                        }
                        else if (mousePos.Y > positionTopLeft.Y + Size.Y)
                        {
                            m_scrollbar.ChangeValue(mousePos.Y - positionTopLeft.Y - Size.Y);
                        }
                    }
                }
            }

            return(baseResult);
        }
Exemple #3
0
        private void HandleNewMousePress(ref MyGuiControlBase captureInput)
        {
            var  mousePos      = MyGuiManager.MouseCursorPosition - GetPositionAbsoluteTopLeft();
            bool cursorInItems = m_itemsRectangle.Contains(mousePos);

            if (MyInput.Static.IsAnyNewMouseOrJoystickPressed() && cursorInItems)
            {
                int row = ComputeIndexFromPosition(mousePos);
                if (IsValidIndex(row) && Items[row].Visible)
                {
                    if (MultiSelect && MyInput.Static.IsAnyCtrlKeyPressed())
                    {
                        if (SelectedItems.Contains(Items[row]))
                        {
                            SelectedItems.Remove(Items[row]);
                        }
                        else
                        {
                            SelectedItems.Add(Items[row]);
                        }
                    }
                    else if (MultiSelect && MyInput.Static.IsAnyShiftKeyPressed())
                    {
                        int index = 0;
                        if (SelectedItems.Count > 0)
                        {
                            index = Items.IndexOf(SelectedItems[SelectedItems.Count - 1]);
                        }

                        do
                        {
                            index += index > row ? -1 : 1;
                            if (!IsValidIndex(index))
                            {
                                break;
                            }
                            if (!Items[index].Visible)
                            {
                                continue;
                            }
                            if (SelectedItems.Contains(Items[index]))
                            {
                                SelectedItems.Remove(Items[index]);
                            }
                            else
                            {
                                SelectedItems.Add(Items[index]);
                            }
                        } while (index != row);
                    }
                    else
                    {
                        SelectedItems.Clear();
                        SelectedItems.Add(Items[row]);
                    }
                    if (ItemsSelected != null)
                    {
                        ItemsSelected(this);
                    }

                    captureInput = this;
                    if (ItemClicked != null)
                    {
                        ItemClicked(this);
                        MyGuiSoundManager.PlaySound(GuiSounds.MouseClick);
                    }
                }
            }

            if (MyInput.Static.IsNewPrimaryButtonPressed() && cursorInItems)
            {
                if (!m_doubleClickStarted.HasValue)
                {
                    int row = ComputeIndexFromPosition(mousePos);
                    if (IsValidIndex(row) && Items[row].Visible)
                    {
                        m_doubleClickStarted       = MyGuiManager.TotalTimeInMilliseconds;
                        m_doubleClickFirstPosition = MyGuiManager.MouseCursorPosition;
                    }
                }
                else if ((MyGuiManager.TotalTimeInMilliseconds - m_doubleClickStarted.Value) <= MyGuiConstants.DOUBLE_CLICK_DELAY &&
                         (m_doubleClickFirstPosition - MyGuiManager.MouseCursorPosition).Length() <= 0.005f)
                {
                    if (ItemDoubleClicked != null)
                    {
                        ItemDoubleClicked(this);
                    }

                    m_doubleClickStarted = null;
                    captureInput         = this;
                }
            }
        }
Exemple #4
0
 internal static bool CanHaveFocusRightNow(MyGuiControlBase control)
 {
     return(control.Enabled && control.Visible && control.CanHaveFocus);
 }
Exemple #5
0
        private bool HandleControlsInput(bool receivedFocusInThisUpdate)
        {
            MyGuiControlBase inputHandledBySomeControl = null;

            if (m_lastHandlingControl != null && m_lastHandlingControl.Visible)
            {
                if (m_lastHandlingControl.HandleInput() != null)
                {
                    inputHandledBySomeControl = m_lastHandlingControl;
                }
            }

            if (inputHandledBySomeControl == null && m_listboxDragAndDropHandlingNow != null)
            {
                if (m_listboxDragAndDropHandlingNow.HandleInput() != null)
                {
                    inputHandledBySomeControl = m_listboxDragAndDropHandlingNow;
                }
            }

            if (inputHandledBySomeControl == null && m_comboboxHandlingNow != null)
            {
                if (m_comboboxHandlingNow.HandleInput() != null)
                {
                    inputHandledBySomeControl = m_comboboxHandlingNow;
                }
            }

            // Check focused screen first
            MyGuiControlBase mouseOverControl = null;

            if (inputHandledBySomeControl == null)
            {
                var visibleControls = Controls.GetVisibleControls();
                for (int i = 0; i < visibleControls.Count; i++)
                {
                    MyGuiControlBase control = visibleControls[i];
                    if (control != m_comboboxHandlingNow && control != m_listboxDragAndDropHandlingNow && control.CheckMouseOver())
                    {
                        mouseOverControl          = control;
                        inputHandledBySomeControl = control.HandleInput();
                        break;
                    }
                }
            }

            //  If opened combobox didn't capture the input, we will try to handle it in remaining controls
            if (inputHandledBySomeControl == null)
            {
                var visibleControls = Controls.GetVisibleControls();
                for (int i = visibleControls.Count - 1; i >= 0; --i)
                {
                    MyGuiControlBase control = visibleControls[i];
                    if (control != m_comboboxHandlingNow && control != m_listboxDragAndDropHandlingNow && control != mouseOverControl)
                    {
                        inputHandledBySomeControl = control.HandleInput();
                        if (inputHandledBySomeControl != null)
                        {
                            break;
                        }
                    }
                }
            }

            if (inputHandledBySomeControl == null)
            {
                foreach (var element in Elements)
                {
                    if (!element.Visible || !element.CanHaveFocus)
                    {
                        continue;
                    }

                    inputHandledBySomeControl = element.HandleInput();
                    if (inputHandledBySomeControl != null)
                    {
                        break;
                    }
                }
            }

            if (inputHandledBySomeControl != null)
            {
                FocusedControl = inputHandledBySomeControl;
            }

            m_lastHandlingControl = inputHandledBySomeControl;

            return(inputHandledBySomeControl != null);
        }
Exemple #6
0
        private void HandleNewMousePress(ref MyGuiControlBase captureInput)
        {
            bool cursorInItems = m_itemsRectangle.Contains(MyGuiManager.MouseCursorPosition);

            if (MyInput.Static.IsNewPrimaryButtonReleased() || MyInput.Static.IsNewSecondaryButtonReleased())
            {
                if (cursorInItems)
                {
                    int?mouseOverIndex = ComputeIndex(MyGuiManager.MouseCursorPosition);
                    if (!IsValidIndex(mouseOverIndex.Value))
                    {
                        mouseOverIndex = null;
                    }

                    SelectMouseOverItem(mouseOverIndex);

                    if (SelectedIndex.HasValue && m_itemClicked.HasValue && m_lastClick.HasValue && mouseOverIndex.HasValue)
                    {
                        if (MyGuiManager.TotalTimeInMilliseconds - m_lastClick.Value < MyGuiConstants.CLICK_RELEASE_DELAY && m_itemClicked.Value.ItemIndex == mouseOverIndex.Value)
                        {
                            captureInput = this;
                            MySharedButtonsEnum button = MySharedButtonsEnum.None;
                            if (MyInput.Static.IsNewPrimaryButtonReleased())
                            {
                                button = MySharedButtonsEnum.Primary;
                            }
                            else if (MyInput.Static.IsNewSecondaryButtonReleased())
                            {
                                button = MySharedButtonsEnum.Secondary;
                            }

                            EventArgs args;
                            MakeEventArgs(out args, SelectedIndex.Value, button);

                            var handler = ItemReleased;
                            if (handler != null)
                            {
                                handler(this, args);
                            }
                        }
                    }
                }
                m_itemClicked = null;
                m_lastClick   = null;
            }

            if (MyInput.Static.IsAnyNewMouseOrJoystickPressed() && cursorInItems)
            {
                m_lastClick = MyGuiManager.TotalTimeInMilliseconds;

                int?mouseOverIndex = ComputeIndex(MyGuiManager.MouseCursorPosition);
                if (!IsValidIndex(mouseOverIndex.Value))
                {
                    mouseOverIndex = null;
                }
                SelectMouseOverItem(mouseOverIndex);

                captureInput = this;
                if (SelectedIndex.HasValue && (ItemClicked != null || ItemClickedWithoutDoubleClick != null))
                {
                    MySharedButtonsEnum button = MySharedButtonsEnum.None;
                    if (MyInput.Static.IsNewPrimaryButtonPressed())
                    {
                        button = MySharedButtonsEnum.Primary;
                    }
                    else if (MyInput.Static.IsNewSecondaryButtonPressed())
                    {
                        button = MySharedButtonsEnum.Secondary;
                    }

                    EventArgs args;
                    MakeEventArgs(out args, SelectedIndex.Value, button);
                    var handler = ItemClicked;
                    if (handler != null)
                    {
                        handler(this, args);
                    }

                    m_singleClickEvents = args;
                    m_itemClicked       = args;

                    if (MyInput.Static.IsAnyCtrlKeyPressed() || MyInput.Static.IsAnyShiftKeyPressed())
                    {
                        MyGuiSoundManager.PlaySound(GuiSounds.Item);
                    }
                }
            }

            if (MyInput.Static.IsNewPrimaryButtonPressed() && cursorInItems)
            {
                if (!m_doubleClickStarted.HasValue)
                {
                    m_doubleClickStarted       = MyGuiManager.TotalTimeInMilliseconds;
                    m_doubleClickFirstPosition = MyGuiManager.MouseCursorPosition;
                }
                else if ((MyGuiManager.TotalTimeInMilliseconds - m_doubleClickStarted.Value) <= MyGuiConstants.DOUBLE_CLICK_DELAY &&
                         (m_doubleClickFirstPosition - MyGuiManager.MouseCursorPosition).Length() <= 0.005f)
                {
                    if (SelectedIndex.HasValue && TryGetItemAt(SelectedIndex.Value) != null && ItemDoubleClicked != null)
                    {
                        //Cancel click event when we double click
                        m_singleClickEvents = null;

                        EventArgs args;
                        MakeEventArgs(out args, SelectedIndex.Value, MySharedButtonsEnum.Primary);
                        Debug.Assert(GetItemAt(args.ItemIndex) != null, "Double click should not be reported when clicking on empty position.");
                        ItemDoubleClicked(this, args);
                        MyGuiSoundManager.PlaySound(GuiSounds.Item);
                    }

                    m_doubleClickStarted = null;
                    captureInput         = this;
                }
            }
        }
        private void HandleTextInputBuffered(ref MyGuiControlBase ret)
        {
            const char BACKSPACE   = '\b';
            bool       textChanged = false;

            foreach (var character in MyInput.Static.TextInput)
            {
                bool skipCharacter = false;
                if (SkipCombinations != null)
                {
                    foreach (var skipCombination in SkipCombinations)
                    {
                        if (skipCombination.Alt == MyInput.Static.IsAnyAltKeyPressed() &&
                            skipCombination.Ctrl == MyInput.Static.IsAnyCtrlKeyPressed() &&
                            skipCombination.Shift == MyInput.Static.IsAnyShiftKeyPressed() &&
                            (skipCombination.Keys == null ||
                             skipCombination.Keys.Contains((MyKeys)character)))
                        {
                            skipCharacter = true;
                            break; //forbiden character combination is being pressed
                        }
                    }
                }

                if (skipCharacter)
                {
                    continue;
                }

                if (Char.IsControl(character))
                {
                    if (character == BACKSPACE)
                    {
                        if (m_selection.Length == 0)
                        {
                            ApplyBackspace();
                        }
                        else
                        {
                            m_selection.EraseText(this);
                        }

                        textChanged = true;
                    }
                }
                else
                {
                    if (m_selection.Length > 0)
                    {
                        m_selection.EraseText(this);
                    }

                    InsertChar(character);
                    textChanged = true;
                }
            }

            // Unbuffered Delete because it's not delivered as a message through Win32 message loop.
            if (MyInput.Static.IsKeyPress(MyKeys.Delete) &&
                IsEnoughDelay(MyKeys.Delete, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY))
            {
                if (m_selection.Length == 0)
                {
                    ApplyDelete();
                }
                else
                {
                    m_selection.EraseText(this);
                }

                textChanged = true;
                UpdateLastKeyPressTimes(MyKeys.Delete);
            }

            if (textChanged)
            {
                OnTextChanged();
                ret = this;
            }
        }
        /// <summary>
        /// Method returns true if input was captured by control, so no other controls, nor screen can use input in this update
        /// </summary>
        public override MyGuiControlBase HandleInput()
        {
            MyGuiControlBase ret = base.HandleInput();

            try
            {
                if (ret == null && Enabled)
                {
                    if (MyInput.Static.IsNewLeftMousePressed())
                    {
                        if (IsMouseOver)
                        {
                            m_selection.Dragging  = true;
                            CarriagePositionIndex = GetCarriagePositionFromMouseCursor();
                            if (MyInput.Static.IsAnyShiftKeyPressed())
                            {
                                m_selection.SetEnd(this);
                            }
                            else
                            {
                                m_selection.Reset(this);
                            }
                            ret = this;
                        }
                        else
                        {
                            m_selection.Reset(this);
                        }
                    }
                    else if (MyInput.Static.IsNewLeftMouseReleased())
                    {
                        m_selection.Dragging = false;
                    }
                    else if (m_selection.Dragging)
                    {
                        if (IsMouseOver)
                        {
                            CarriagePositionIndex = GetCarriagePositionFromMouseCursor();
                            m_selection.SetEnd(this);
                            ret = this;
                        }
                    }

                    if (HasFocus && !m_hadFocusLastTime)
                    {
                        UpdateLastKeyPressTimes(null);
                    }

                    if (HasFocus)
                    {
                        if (!MyInput.Static.IsAnyCtrlKeyPressed())
                        {
                            HandleTextInputBuffered(ref ret);
                        }

                        //  Move left
                        if ((MyInput.Static.IsKeyPress(MyKeys.Left)) && (IsEnoughDelay(MyKeys.Left, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                        {
                            if (MyInput.Static.IsAnyCtrlKeyPressed())
                            {
                                CarriagePositionIndex = GetPreviousSpace();
                            }
                            else
                            {
                                CarriagePositionIndex--;
                            }

                            UpdateLastKeyPressTimes(MyKeys.Left);

                            if (MyInput.Static.IsAnyShiftKeyPressed())
                            {
                                m_selection.SetEnd(this);
                            }
                            else
                            {
                                m_selection.Reset(this);
                            }

                            ret = this;
                        }

                        //  Move right
                        if ((MyInput.Static.IsKeyPress(MyKeys.Right)) && (IsEnoughDelay(MyKeys.Right, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                        {
                            if (MyInput.Static.IsAnyCtrlKeyPressed())
                            {
                                CarriagePositionIndex = GetNextSpace();
                            }
                            else
                            {
                                CarriagePositionIndex++;
                            }

                            UpdateLastKeyPressTimes(MyKeys.Right);

                            if (MyInput.Static.IsAnyShiftKeyPressed())
                            {
                                m_selection.SetEnd(this);
                            }
                            else
                            {
                                m_selection.Reset(this);
                            }

                            ret = this;
                        }

                        //  Move home
                        if ((MyInput.Static.IsNewKeyPressed(MyKeys.Home)) && (IsEnoughDelay(MyKeys.Home, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                        {
                            CarriagePositionIndex = 0;
                            UpdateLastKeyPressTimes(MyKeys.Home);
                            ret = this;
                        }

                        //  Move end
                        if ((MyInput.Static.IsNewKeyPressed(MyKeys.End)) && (IsEnoughDelay(MyKeys.End, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                        {
                            CarriagePositionIndex = m_text.Length;
                            UpdateLastKeyPressTimes(MyKeys.End);
                            ret = this;
                        }

                        //Cut selected text
                        if (MyInput.Static.IsNewKeyPressed(MyKeys.X) && IsEnoughDelay(MyKeys.X, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY) && MyInput.Static.IsAnyCtrlKeyPressed())
                        {
                            UpdateLastKeyPressTimes(MyKeys.X);
                            m_selection.CutText(this);
                        }

                        //Copy
                        if (MyInput.Static.IsNewKeyPressed(MyKeys.C) && IsEnoughDelay(MyKeys.C, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY) && MyInput.Static.IsAnyCtrlKeyPressed())
                        {
                            UpdateLastKeyPressTimes(MyKeys.C);
                            m_selection.CopyText(this);
                        }

                        //Paste
                        if (MyInput.Static.IsNewKeyPressed(MyKeys.V) && IsEnoughDelay(MyKeys.V, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY) && MyInput.Static.IsAnyCtrlKeyPressed())
                        {
                            UpdateLastKeyPressTimes(MyKeys.V);
                            m_selection.PasteText(this);
                        }

                        //Select All
                        if (MyInput.Static.IsNewKeyPressed(MyKeys.A) && IsEnoughDelay(MyKeys.A, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY) && MyInput.Static.IsAnyCtrlKeyPressed())
                        {
                            UpdateLastKeyPressTimes(MyKeys.A);
                            m_selection.SelectAll(this);
                        }

                        if (MyInput.Static.IsNewKeyPressed(MyKeys.Enter))
                        {
                            if (EnterPressed != null)
                            {
                                EnterPressed(this);
                            }
                        }

                        ResetLastKeyPressTimes();
                        m_formattedAlready = false;
                    }
                    else
                    {
                        if (Type == MyGuiControlTextboxType.DigitsOnly && m_formattedAlready == false && m_text.Length != 0)
                        {
                            var number        = MyValueFormatter.GetDecimalFromString(Text, 1);
                            int decimalDigits = (number - (int)number > 0) ? 1 : 0;
                            m_text.Clear().Append(MyValueFormatter.GetFormatedFloat((float)number, decimalDigits, ""));
                            CarriagePositionIndex = m_text.Length;
                            m_formattedAlready    = true;
                        }
                    }
                }
            }
            catch (IndexOutOfRangeException) // mkTODO: Why? Handle correctly
            {
            }

            m_hadFocusLastTime = HasFocus;
            return(ret);
        }