/// <summary> /// Creates a new instance of a MyGuiControlRanged slider with given parameters /// </summary> /// <param name="position">The position of the slider in screenspace</param> /// <param name="width">The width of the slider in screenspace</param> /// <param name="minValue">The minimum value of the slider</param> /// <param name="maxValue">The maximum value of the slider</param> /// <param name="showLabel">Whether to show a label with the sliders values</param> /// <param name="labelSpaceWidth">The space between label and slider</param> /// <param name="labelScale">The size of the labels text</param> /// <param name="labelFont">The font of the label</param> /// <param name="toolTip">The tooltip for the slider</param> /// <param name="originAlign">The alignment of the slider</param> /// <param name="useLogScale">Whether to use logarithmic scaling of values on the slider to allow for wider ranges</param> /// <param name="defaultMax">Default max value</param> /// <param name="defaultMin">Default min value</param> /// <param name="intMode">If the slider should work in int mode</param> public MyGuiControlRangedSlider(double minValue, double maxValue, double defaultMin, double defaultMax, bool intMode = false, Vector2?position = null, float width = 0.29f, bool showLabel = true, float labelSpaceWidth = 0f, float labelScale = 0.8f, string labelFont = "White", string toolTip = null, MyGuiDrawAlignEnum originAlign = MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_CENTER, bool useLogScale = false) : base(position, null, null, toolTip, null, true, true, MyGuiControlHighlightType.WHEN_CURSOR_OVER_OR_FOCUS, originAlign) { m_showLabel = showLabel; m_minValue = Math.Min(minValue, maxValue); m_maxValue = Math.Max(maxValue, minValue); m_labelSpaceWidth = labelSpaceWidth; m_railTexture = MyGuiConstants.TEXTURE_SLIDER_RAIL; m_minThumb = new MyGuiSliderThumb(defaultMin); m_maxThumb = new MyGuiSliderThumb(defaultMax); m_intMode = intMode; m_logScale = useLogScale; m_minLabel = new MyGuiControlLabel(null, null, "", null, labelScale, labelFont, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_BOTTOM); m_maxLabel = new MyGuiControlLabel(null, null, "", null, labelScale, labelFont, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP); if (m_showLabel) { Elements.Add(m_minLabel); Elements.Add(m_maxLabel); } Size = new Vector2(width, Size.Y); UpdateLabels(); RefreshInternals(); }
/// <summary> /// Handles a ctrl click on one of the thumbs to open a direct input window. /// </summary> private bool HandleClickOnThumb() { if (!MyInput.Static.IsAnyCtrlKeyPressed() || !MyInput.Static.IsNewPrimaryButtonPressed()) { return(false); } double min = 0; double max = 0; double current = 0; MyGuiSliderThumb clickedThumb = null; if (m_minThumb.IsHighlighted() || m_minThumb.IsFocused()) { min = m_minValue; max = m_maxThumb.CurrentValue; current = m_minThumb.CurrentValue; clickedThumb = m_minThumb; } if (m_maxThumb.IsHighlighted() || m_maxThumb.IsFocused()) { min = m_minThumb.CurrentValue; max = m_maxValue; current = m_maxThumb.CurrentValue; clickedThumb = m_maxThumb; } if (clickedThumb == null) { return(false); } MyGuiScreenDialogAmount dialog = new MyGuiScreenDialogAmount((float)min, (float)max, MyCommonTexts.DialogAmount_SetValueCaption, parseAsInteger: m_intMode, defaultAmount: (float)current, backgroundTransition: MySandboxGame.Config.UIBkOpacity, guiTransition: MySandboxGame.Config.UIOpacity); dialog.OnConfirmed += delegate(float value) { clickedThumb.CurrentValue = value; RefreshInternals(); }; MyGuiSandbox.AddScreen(dialog); return(true); }
public override MyGuiControlBase HandleInput() { var ret = base.HandleInput(); if (ret != null) { return(ret); } if (!Enabled) { return(null); } bool controlCaptured = false; bool mouseDown = MyInput.Static.IsNewPrimaryButtonPressed(); if (IsMouseOver && mouseDown && !OnSliderClicked()) { controlCaptured = true; if (HandleClickOnThumb()) { return(ret); } } if (m_currentFocusThumb != null) { controlCaptured = true; } if (MyInput.Static.IsNewPrimaryButtonReleased()) { controlCaptured = false; } if (IsMouseOver || true) { if (controlCaptured) { var mousePos = MyGuiManager.MouseCursorPosition; float startX = MyGuiConstants.SLIDER_INSIDE_OFFSET_X; float endX = Size.X - MyGuiConstants.SLIDER_INSIDE_OFFSET_X; float centerY = Size.Y / 2; if (m_currentFocusThumb == null && m_maxThumb.GetBounds(GetPositionAbsoluteTopLeft()).Contains(mousePos) != ContainmentType.Disjoint && !(m_minThumb.CurrentValue == m_maxValue)) { m_currentFocusThumb = m_maxThumb; m_maxThumb.ForcusThumb(); m_minThumb.ResetThumbAppearance(); } else if (m_currentFocusThumb == null && m_minThumb.GetBounds(GetPositionAbsoluteTopLeft()).Contains(mousePos) != ContainmentType.Disjoint) { m_currentFocusThumb = m_minThumb; m_minThumb.ForcusThumb(); m_maxThumb.ResetThumbAppearance(); } if (m_currentFocusThumb != null) { float mouseRatio = (mousePos.X - GetPositionAbsoluteTopLeft().X) / (GetPositionAbsoluteTopRight().X - GetPositionAbsoluteTopLeft().X); if (m_logScale) { mouseRatio = (float)Math.Pow(mouseRatio, 3); } double mouseVal = (m_maxValue - m_minValue) * mouseRatio + m_minValue; if (m_currentFocusThumb == m_minThumb) { m_currentFocusThumb.CurrentValue = MathHelper.Clamp(mouseVal, m_minValue, m_maxThumb.CurrentValue); if (m_intMode) { m_currentFocusThumb.CurrentValue = (int)m_currentFocusThumb.CurrentValue; } } else { m_currentFocusThumb.CurrentValue = MathHelper.Clamp(mouseVal, m_minThumb.CurrentValue, m_maxValue); if (m_intMode) { m_currentFocusThumb.CurrentValue = (int)m_currentFocusThumb.CurrentValue; } } float currentRatio = (float)((m_currentFocusThumb.CurrentValue - m_minValue) / (m_maxValue - m_minValue)); if (m_logScale) { currentRatio = (float)Math.Pow(currentRatio, 1f / 3f); } m_currentFocusThumb.CurrentPosition = new Vector2(startX + (endX - startX) * currentRatio, centerY); } } else { m_currentFocusThumb = null; RefreshInternals(); } } return(ret); }