示例#1
0
        /// <summary>
        /// Create an auto-layout horizontal slider that user can drag to select a value.
        /// </summary>
        /// <param name="label">label of the slider</param>
        /// <param name="value">The value the slider shows.</param>
        /// <param name="minValue">The value at the left end of the slider.</param>
        /// <param name="maxValue">The value at the right end of the slider.</param>
        /// <returns>The value set by the user.</returns>
        /// <remarks>minValue &lt;= value &lt;= maxValue</remarks>
        public static double Slider(string label, double value, double minValue, double maxValue)
        {
            GUIContext g      = GetCurrentContext();
            Window     window = GetCurrentWindow();

            if (window.SkipItems)
            {
                return(value);
            }

            var id = window.GetID(label);

            // style apply
            var style = GUIStyle.Basic;

            // rect
            Size size = style.CalcSize(label, GUIState.Normal);

            style.PushStretchFactor(false, 1);//+1
            {
                var minSilderWidth = 200;
                size.Width += minSilderWidth;
                size.Height = 20;
            }
            var rect = window.GetRect(id);

            // interact
            var spacing     = GUISkin.Instance.InternalStyle.Get <double>(GUIStyleName._ControlLabelSpacing);
            var labelWidth  = GUISkin.Instance.InternalStyle.Get <double>(GUIStyleName._LabelWidth);
            var sliderWidth = rect.Width - spacing - labelWidth;

            if (sliderWidth <= 0)
            {
                sliderWidth = 1;
            }
            var sliderRect = new Rect(rect.X, rect.Y,
                                      sliderWidth,
                                      rect.Height);
            bool hovered, held;

            value = GUIBehavior.SliderBehavior(sliderRect, id, true, value, minValue, maxValue, out hovered, out held);

            // render
            var state = GUIState.Normal;

            if (hovered)
            {
                state = GUIState.Hover;
            }
            if (g.ActiveId == id && Mouse.Instance.LeftButtonState == KeyState.Down)
            {
                state = GUIState.Active;
            }
            GUIAppearance.DrawSlider(rect, label, value, minValue, maxValue, state, sliderRect, labelWidth);

            // style restore
            style.PopStyle();//-1

            return(value);
        }
示例#2
0
        /// <summary>
        /// Create a horizontal slider that user can drag to select a value.
        /// </summary>
        /// <param name="rect">position and size of the control</param>
        /// <param name="label">label</param>
        /// <param name="value">The value the slider shows.</param>
        /// <param name="minValue">The value at the top end of the slider.</param>
        /// <param name="maxValue">The value at the bottom end of the slider.</param>
        /// <returns>The value set by the user.</returns>
        /// <remarks>minValue &lt;= value &lt;= maxValue</remarks>
        public static double Slider(Rect rect, string label, double value, double minValue, double maxValue)
        {
            var g      = GetCurrentContext();
            var window = GetCurrentWindow();

            if (window.SkipItems)
            {
                return(0);
            }

            //get or create the root node
            var id        = window.GetID(label);
            var container = window.AbsoluteVisualList;
            var node      = (Node)container.Find(visual => visual.Id == id);
            var text      = Utility.FindRenderedText(label);

            if (node == null)
            {
                node = new Node(id, $"Slider<{label}>");
                container.Add(node);
                node.UseBoxModel = true;
                node.RuleSet.Replace(GUISkin.Current[GUIControlName.Slider]);
            }

            node.ActiveSelf = true;

            // rect
            node.Rect = window.GetRect(rect);

            // interact
            var spacing     = node.RuleSet.Get <double>("ControlLabelSpacing");
            var labelWidth  = node.RuleSet.Get <double>("LabelWidth");
            var sliderWidth = rect.Width - spacing - labelWidth;

            if (sliderWidth <= 0)
            {
                sliderWidth = 1;
            }
            var sliderRect = new Rect(node.Rect.X, node.Rect.Y,
                                      sliderWidth,
                                      node.Rect.Height);
            bool hovered, held;

            value = GUIBehavior.SliderBehavior(sliderRect, id, true, value, minValue, maxValue, out hovered, out held);

            // render
            var state = GUIState.Normal;

            if (hovered)
            {
                state = GUIState.Hover;
            }
            if (g.ActiveId == id && Mouse.Instance.LeftButtonState == KeyState.Down)
            {
                state = GUIState.Active;
            }
            GUIAppearance.DrawSlider(node, text, value, minValue, maxValue, state, sliderRect, labelWidth);

            return(value);
        }
示例#3
0
        /// <summary>
        /// Create a horizontal slider that user can drag to select a value.
        /// </summary>
        /// <param name="rect">position and size of the control</param>
        /// <param name="label">label</param>
        /// <param name="value">The value the slider shows.</param>
        /// <param name="minValue">The value at the top end of the slider.</param>
        /// <param name="maxValue">The value at the bottom end of the slider.</param>
        /// <returns>The value set by the user.</returns>
        /// <remarks>minValue &lt;= value &lt;= maxValue</remarks>
        public static double Slider(Rect rect, string label, double value, double minValue, double maxValue)
        {
            GUIContext g      = GetCurrentContext();
            Window     window = GetCurrentWindow();

            if (window.SkipItems)
            {
                return(value);
            }

            int id = window.GetID(label);

            // rect
            rect = window.GetRect(rect);

            // interact
            var spacing     = GUISkin.Instance.InternalStyle.Get <double>(GUIStyleName._ControlLabelSpacing);
            var labelWidth  = GUISkin.Instance.InternalStyle.Get <double>(GUIStyleName._LabelWidth);
            var sliderWidth = rect.Width - spacing - labelWidth;

            if (sliderWidth <= 0)
            {
                sliderWidth = 1;
            }
            var sliderRect = new Rect(rect.X, rect.Y,
                                      sliderWidth,
                                      rect.Height);
            bool hovered, held;

            value = GUIBehavior.SliderBehavior(sliderRect, id, true, value, minValue, maxValue, out hovered, out held);

            // render
            var state = GUIState.Normal;

            if (hovered)
            {
                state = GUIState.Hover;
            }
            if (g.ActiveId == id && Mouse.Instance.LeftButtonState == KeyState.Down)
            {
                state = GUIState.Active;
            }
            GUIAppearance.DrawSlider(rect, label, value, minValue, maxValue, state, sliderRect, labelWidth);

            return(value);
        }
示例#4
0
文件: Slider.cs 项目: zwcloud/ImGui
        /// <summary>
        /// Create an auto-layout horizontal slider that user can drag to select a value.
        /// </summary>
        /// <param name="label">label of the slider</param>
        /// <param name="value">The value the slider shows.</param>
        /// <param name="minValue">The value at the left end of the slider.</param>
        /// <param name="maxValue">The value at the right end of the slider.</param>
        /// <returns>The value set by the user.</returns>
        /// <remarks>minValue &lt;= value &lt;= maxValue</remarks>
        public static double Slider(string label, double value, double minValue, double maxValue)
        {
            var g      = GetCurrentContext();
            var window = GetCurrentWindow();

            if (window.SkipItems)
            {
                return(0);
            }

            //get or create the root node
            var id        = window.GetID(label);
            var container = window.RenderTree.CurrentContainer;
            var node      = container.GetNodeById(id);
            var text      = Utility.FindRenderedText(label);

            if (node == null)
            {
                node             = new Node(id, $"Slider<{label}>");
                node.UseBoxModel = true;
                node.RuleSet.Replace(GUISkin.Current[GUIControlName.Slider]);
                var size = node.RuleSet.CalcContentBoxSize(text, GUIState.Normal);
                size.Width += size.Height + node.RuleSet.PaddingLeft;
                var minSilderWidth = 200;
                size.Width += minSilderWidth;
                size.Height = 20;
                node.AttachLayoutEntry(size);
            }
            container.AppendChild(node);
            node.ActiveSelf = true;

            // rect
            node.Rect = window.GetRect(id);

            // interact
            var spacing     = node.RuleSet.Get <double>("ControlLabelSpacing");
            var labelWidth  = node.RuleSet.Get <double>("LabelWidth");
            var sliderWidth = node.Rect.Width - spacing - labelWidth;

            if (sliderWidth <= 0)
            {
                sliderWidth = 1;
            }
            var sliderRect = new Rect(node.Rect.Location, sliderWidth, node.Rect.Height);

            value = GUIBehavior.SliderBehavior(sliderRect, id, true, value, minValue, maxValue, out var hovered, out var held);

            var state = GUIState.Normal;

            if (hovered)
            {
                state = GUIState.Hover;
            }
            if (g.ActiveId == id && Mouse.Instance.LeftButtonState == KeyState.Down)
            {
                state = GUIState.Active;
            }

            // last item state
            window.TempData.LastItemState = node.State;

            // render
            GUIAppearance.DrawSlider(node, label, value, minValue, maxValue, state, sliderRect, labelWidth);

            return(value);
        }