コード例 #1
0
        public static string Textbox(Rect rect, string label, string text)
        {
            Window window = GetCurrentWindow();

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

            var id = window.GetID(label);

            // style
            var style = GUIStyle.Basic;

            style.Save();
            style.ApplySkin(GUIControlName.TextBox);

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

            // interact
            InputTextContext context;

            text = GUIBehavior.TextBoxBehavior(id, rect, text, out bool hovered, out bool active, out context);

            // render
            var state = active ? GUIState.Active : hovered ? GUIState.Hover : GUIState.Normal;

            GUIAppearance.DrawTextBox(rect, id, text, context, state);

            style.Restore();

            return(text);
        }
コード例 #2
0
ファイル: Slider.cs プロジェクト: ibrahim-elsakka/ImGui-1
        /// <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
        public static double ProgressBar(string str_id, double percent, Size size, string overlayText = null)
        {
            Window window = GetCurrentWindow();

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

            GUIContext g  = GetCurrentContext();
            int        id = window.GetID(str_id);

            // style
            var style = GUIStyle.Basic;

            // rect
            var rect = window.GetRect(id, size);

            percent = MathEx.Clamp01(percent);

            // render
            DrawList d = window.DrawList;

            GUIAppearance.DrawProgressBar(rect, percent);
            if (overlayText != null)
            {
                style.PushTextAlignment(TextAlignment.Center);
                d.DrawBoxModel(rect, overlayText, style);
                style.PopStyle();
            }

            return(percent);
        }
コード例 #4
0
ファイル: Toggle.cs プロジェクト: qaz734913414/ImGui-1
        /// <summary>
        /// Create a toggle (check-box) with a label.
        /// </summary>
        /// <param name="rect">position and size of the control</param>
        /// <param name="label">label</param>
        /// <param name="value">Is this toggle checked or unchecked?</param>
        /// <returns>new value of the toggle</returns>
        public static bool Toggle(Rect rect, string label, bool value)
        {
            GUIContext g      = GetCurrentContext();
            Window     window = GetCurrentWindow();

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

            int id = window.GetID(label);

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

            // interact
            bool hovered;

            value = GUIBehavior.ToggleBehavior(rect, id, value, out hovered);

            // render
            var state = GUIState.Normal;

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

            return(value);
        }
コード例 #5
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);
        }
コード例 #6
0
ファイル: TextBox.cs プロジェクト: zwcloud/ImGui
        /// <summary>
        /// Create a single-line text box.
        /// </summary>
        /// <param name="label">label</param>
        /// <param name="width">width</param>
        /// <param name="text">text</param>
        /// <param name="flags">filter flags</param>
        /// <param name="checker">custom checker per char</param>
        /// <returns>(modified) text</returns>
        public static string TextBox(string label, double width, string text, InputTextFlags flags, Func <char, bool> checker, LayoutOptions?options)
        {
            Window window = GetCurrentWindow();

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

            var  id        = window.GetID(label);
            var  container = window.RenderTree.CurrentContainer;
            Node node      = container.GetNodeById(id);

            label = Utility.FindRenderedText(label);
            if (node == null)
            {
                //create node
                node             = new Node(id, $"{nameof(TextBox)}<{label}>");
                node.UseBoxModel = true;
                node.RuleSet.Replace(GUISkin.Current[GUIControlName.TextBox]);
                var size = node.RuleSet.CalcContentBoxSize(text, GUIState.Normal);
                node.AttachLayoutEntry(size);
            }
            container.AppendChild(node);
            node.RuleSet.ApplyOptions(options);
            node.ActiveSelf = true;

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

            // interact
            InputTextContext context;

            text = GUIBehavior.TextBoxBehavior(id, rect, text, out bool hovered, out bool active, out context, flags, checker);
            var state = active ? GUIState.Active : hovered ? GUIState.Hover : GUIState.Normal;

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

            // render
            if (flags.HaveFlag(InputTextFlags.Password))
            {
                var dotText = new string('*', text.Length);//FIXME bad performance
                GUIAppearance.DrawTextBox(node, id, dotText, context, state);
            }
            else
            {
                GUIAppearance.DrawTextBox(node, id, text, context, state);
            }

            return(text);
        }
コード例 #7
0
        /// <summary>
        /// Create an auto-layout toggle (check-box) with an label.
        /// </summary>
        /// <param name="label">text to display</param>
        /// <param name="value">Is this toggle checked or unchecked?</param>
        /// <returns>new value of the toggle</returns>
        public static bool Toggle(string label, bool value)
        {
            var g      = GetCurrentContext();
            var window = GetCurrentWindow();

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

            //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, $"Toggle<{label}>");
                node.UseBoxModel = true;
                node.RuleSet.Replace(GUISkin.Current[GUIControlName.Toggle]);
                var size = node.RuleSet.CalcContentBoxSize(text, GUIState.Normal);
                size.Width += size.Height + node.RuleSet.PaddingLeft;
                node.AttachLayoutEntry(size);
            }
            container.AppendChild(node);
            node.ActiveSelf = true;

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

            // interact
            value = GUIBehavior.ToggleBehavior(node.Rect, id, value, out var hovered);

            // render
            var state = GUIState.Normal;

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

            return(value);
        }
コード例 #8
0
        /// <summary>
        /// Create a toggle (check-box) with a label.
        /// </summary>
        /// <param name="rect">position and size of the control</param>
        /// <param name="label">label</param>
        /// <param name="value">Is this toggle checked or unchecked?</param>
        /// <returns>new value of the toggle</returns>
        public static bool Toggle(Rect rect, string label, bool value)
        {
            var g      = GetCurrentContext();
            var window = GetCurrentWindow();

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

            //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, $"Toggle<{label}>");
                container.Add(node);
                node.UseBoxModel = true;
                node.RuleSet.Replace(GUISkin.Current[GUIControlName.Toggle]);
            }

            node.ActiveSelf = true;

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

            // interact
            value = GUIBehavior.ToggleBehavior(node.Rect, id, value, out var hovered);

            // render
            var state = GUIState.Normal;

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

            return(value);
        }
コード例 #9
0
        /// <summary>
        /// Create a single-line text box.
        /// </summary>
        /// <param name="label">label</param>
        /// <param name="width">width</param>
        /// <param name="text">text</param>
        /// <param name="flags">filter flags</param>
        /// <param name="checker">custom checker per char</param>
        /// <returns>(modified) text</returns>
        public static string TextBox(string label, double width, string text, InputTextFlags flags, Func <char, bool> checker, LayoutOptions?options)
        {
            Window window = GetCurrentWindow();

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

            int id = window.GetID(label);

            // style apply
            var style = GUIStyle.Basic;

            style.Save();
            style.ApplySkin(GUIControlName.TextBox);
            style.ApplyOption(options);

            // rect
            var  height = style.GetLineHeight();
            var  size   = new Size(width, height);
            Rect rect   = window.GetRect(id, size);

            // interact
            InputTextContext context;

            text = GUIBehavior.TextBoxBehavior(id, rect, text, out bool hovered, out bool active, out context, flags, checker);

            // render
            var d     = window.DrawList;
            var state = active ? GUIState.Active : hovered ? GUIState.Hover : GUIState.Normal;

            if (flags.HaveFlag(InputTextFlags.Password))
            {
                var dotText = new string('*', text.Length);//FIXME bad performance
                GUIAppearance.DrawTextBox(rect, id, dotText, context, state);
            }
            else
            {
                GUIAppearance.DrawTextBox(rect, id, text, context, state);
            }

            style.Restore();

            return(text);
        }
コード例 #10
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);
        }
コード例 #11
0
ファイル: Label.cs プロジェクト: zwcloud/ImGui
        /// <summary>
        /// Create a label with a little bullet.
        /// </summary>
        /// <param name="text">The text to display.</param>
        public static void BulletText(string text)
        {
            Window window = GetCurrentWindow();

            if (window.SkipItems)
            {
                return;
            }

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

            text = Utility.FindRenderedText(text);
            if (node == null)
            {
                node             = new Node(id, $"BulletText<{text}>");
                node.UseBoxModel = true;
                node.RuleSet.Replace(GUISkin.Current[GUIControlName.Label]);
                var size       = node.RuleSet.CalcContentBoxSize(text, GUIState.Normal);
                var lineHeight = node.RuleSet.GetLineHeight();
                size += new Vector(lineHeight, 0);
                node.AttachLayoutEntry(size);
            }
            container.AppendChild(node);

            node.ActiveSelf = true;

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

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

            using (var dc = node.RenderOpen())
            {
                var lineHeight     = node.RuleSet.GetLineHeight();
                var bulletPosition = node.Rect.TopLeft + new Vector(lineHeight * 0.5f, lineHeight * 0.5f);
                GUIAppearance.RenderBullet(dc, bulletPosition, lineHeight, node.RuleSet.FontColor);
                var rect = node.Rect;
                rect.Offset(lineHeight, 0);
                dc.DrawGlyphRun(node.RuleSet, text, rect.TopLeft);
            }
        }
コード例 #12
0
ファイル: TextBox.cs プロジェクト: zwcloud/ImGui
        //TODO multiple/single line TextBox
        public static string TextBox(Rect rect, string label, string text, LayoutOptions?options)
        {
            Window window = GetCurrentWindow();

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

            var  id        = window.GetID(label);
            var  container = window.AbsoluteVisualList;
            Node node      = (Node)container.Find(visual => visual.Id == id);

            label = Utility.FindRenderedText(label);
            if (node == null)
            {
                //create node
                node             = new Node(id, $"{nameof(TextBox)}<{label}>");
                node.UseBoxModel = true;
                node.RuleSet.Replace(GUISkin.Current[GUIControlName.TextBox]);
                container.Add(node);
            }
            node.RuleSet.ApplyOptions(options);
            node.ActiveSelf = true;

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

            // interact
            InputTextContext context;

            text = GUIBehavior.TextBoxBehavior(id, rect, text, out bool hovered, out bool active, out context);
            var state = active ? GUIState.Active : hovered ? GUIState.Hover : GUIState.Normal;

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

            // render
            GUIAppearance.DrawTextBox(node, id, text, context, state);

            return(text);
        }
コード例 #13
0
ファイル: Label.cs プロジェクト: zwcloud/ImGui
        /// <summary>
        /// Create an auto-layout bullet mark.
        /// </summary>
        public static void Bullet(string str_id)
        {
            Window window = GetCurrentWindow();

            if (window.SkipItems)
            {
                return;
            }

            //get or create the root node
            int  id        = window.GetID(str_id);
            var  container = window.RenderTree.CurrentContainer;
            Node node      = container.GetNodeById(id);

            if (node == null)
            {
                node             = new Node(id, $"Bullet<{str_id}>");
                node.UseBoxModel = true;
                node.RuleSet.Replace(GUISkin.Current[GUIControlName.Label]);
                var lineheight = node.RuleSet.GetLineHeight();
                var size       = new Size(lineheight, lineheight);
                node.AttachLayoutEntry(size);
            }
            container.AppendChild(node);

            node.ActiveSelf = true;

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

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

            using (var dc = node.RenderOpen())
            {
                var bulletPosition = node.Rect.TopLeft + new Vector(node.Rect.Height * 0.5f, node.Rect.Height * 0.5f);
                var lineheight     = node.RuleSet.GetLineHeight();
                GUIAppearance.RenderBullet(dc, bulletPosition, lineheight, node.RuleSet.FontColor);
            }
        }
コード例 #14
0
ファイル: Toggle.cs プロジェクト: qaz734913414/ImGui-1
        /// <summary>
        /// Create an auto-layout toggle (check-box) with an label.
        /// </summary>
        /// <param name="label">text to display</param>
        /// <param name="value">Is this toggle checked or unchecked?</param>
        /// <returns>new value of the toggle</returns>
        public static bool Toggle(string label, bool value)
        {
            GUIContext g      = GetCurrentContext();
            Window     window = GetCurrentWindow();

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

            int id = window.GetID(label);

            // rect
            var style    = GUIStyle.Basic;
            var textSize = style.CalcSize(label, GUIState.Normal);
            var size     = new Size(16 + textSize.Width, 16 > textSize.Height ? 16 : textSize.Height);
            var rect     = window.GetRect(id);

            // interact
            bool hovered;

            value = GUIBehavior.ToggleBehavior(rect, id, value, out hovered);

            // render
            var state = GUIState.Normal;

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

            return(value);
        }
コード例 #15
0
ファイル: ColorField.cs プロジェクト: masums/ImGui-1
        public static Color ColorField(string label, Color value)
        {
            Window window = GetCurrentWindow();

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

            var style = GUIStyle.Basic;

            var id = window.GetID(label);

            // rect

            int rId;
            int gId;
            int bId;
            int aId;
            int colorId;

            Rect rectR, rectG, rectB, rectA, rectColor;

            var labelHeight = style.CalcSize(label, GUIState.Normal).Height;

            BeginHorizontal("FieldGroup~" + id);
            {
                {
                    using (HScope("#RGBA&Color", GUILayout.ExpandWidth(true)))
                    {
                        using (VScope(label + "#RGBA"))
                        {
                            using (HScope("#RGB"))
                            {
                                PushHStretchFactor(1);
                                rId   = window.GetID("#R");
                                gId   = window.GetID("#G");
                                bId   = window.GetID("#B");
                                rectR = window.GetRect(rId, (0, labelHeight));
                                rectG = window.GetRect(gId, (0, labelHeight));
                                rectB = window.GetRect(bId, (0, labelHeight));
                                PopStyleVar(1);
                            }

                            aId   = window.GetID("#A");
                            rectA = window.GetRect(aId, (0, labelHeight * 0.25), GUILayout.ExpandWidth(true));
                        }
                        colorId   = window.GetID("#Color");
                        rectColor = window.GetRect(colorId, (labelHeight * 1.25f, labelHeight * 1.25f));
                    }

                    // interact
                    value.R = GUIBehavior.SliderBehavior(rectR, rId, true, value.R, 0, 1.0, out bool R_hovered, out bool R_held);
                    value.G = GUIBehavior.SliderBehavior(rectG, gId, true, value.G, 0, 1.0, out bool G_hovered, out bool G_held);
                    value.B = GUIBehavior.SliderBehavior(rectB, bId, true, value.B, 0, 1.0, out bool B_hovered, out bool B_held);
                    value.A = GUIBehavior.SliderBehavior(rectA, aId, true, value.A, 0, 1.0, out bool A_hovered, out bool A_held);

                    // render
                    var d = window.DrawList;
                    DrawColorDragButton(d, rectR, rId, 'R', value.R, (R_hovered && R_held) ? GUIState.Active : R_hovered?GUIState.Hover: GUIState.Normal);
                    DrawColorDragButton(d, rectG, gId, 'G', value.G, (G_hovered && G_held) ? GUIState.Active : G_hovered?GUIState.Hover: GUIState.Normal);
                    DrawColorDragButton(d, rectB, bId, 'B', value.B, (B_hovered && B_held) ? GUIState.Active : B_hovered?GUIState.Hover: GUIState.Normal);

                    GUIAppearance.DrawProgressBar(rectA, value.A);

                    d.AddRectFilled(rectColor, value);
                }

                Space("FieldSpacing", GUISkin.Current.FieldSpacing);
                Label(label, GUILayout.Width((int)GUISkin.Current.LabelWidth));
            }
            EndHorizontal();

            return(value);
        }
コード例 #16
0
ファイル: Slider.cs プロジェクト: zwcloud/ImGui
        /// <summary>
        /// Create an auto-layout vertical 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 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 VSlider(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);
                var minSilderHeight = 200;
                size.Width   = 20;
                size.Height += minSilderHeight;
                node.AttachLayoutEntry(size);
            }
            container.AppendChild(node);
            node.ActiveSelf = true;

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

            // interact
            var spacing      = node.RuleSet.Get <double>("ControlLabelSpacing");
            var labelHeight  = node.RuleSet.Get <double>("LabelHeight");
            var sliderHeight = node.Rect.Height - spacing - labelHeight;

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

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

            var state = GUI.Normal;

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

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

            // render
            GUIAppearance.DrawVSlider(node, label, value, minValue, maxValue, state, sliderRect, labelHeight);

            return(value);
        }
コード例 #17
0
ファイル: TextBox.cs プロジェクト: ibrahim-elsakka/ImGui-1
        /// <summary>
        /// Create a multi-line text box of fixed size.
        /// </summary>
        /// <param name="str_id">id</param>
        /// <param name="size">fixed size</param>
        /// <param name="text">text</param>
        /// <returns>(modified) text</returns>
        public static string TextBox(string str_id, Size size, string text, LayoutOptions?options)
        {
            Window window = GetCurrentWindow();

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

            var container = window.RenderTree.CurrentContainer;

            var  id   = window.GetID(str_id);
            Node node = container.GetNodeById(id);

            if (node == null)
            {
                //create node
                node             = new Node(id, $"{nameof(TextBox)}<{str_id}>");
                node.UseBoxModel = true;
                node.RuleSet.Replace(GUISkin.Current[GUIControlName.TextBox]);
                node.AttachLayoutGroup(true);
                node.RuleSet.ApplyOptions(GUILayout.Width(size.Width).Height(size.Height));
            }

            var textNodeId = window.GetID($"{nameof(TextBox)}<{str_id}>_Text");
            var textNode   = node.GetDirectNodeById(textNodeId);

            if (textNode == null)
            {
                textNode = new Node(textNodeId);
            }
            var textSize = node.RuleSet.CalcContentBoxSize(text, node.State);

            textNode.RuleSet.Replace(GUISkin.Current[GUIControlName.TextBox]);
            textNode.ContentSize = textSize;
            textNode.ActiveSelf  = true;
            node.AppendChild(textNode);

            container.AppendChild(node);
            node.RuleSet.ApplyOptions(options);
            node.ActiveSelf = true;

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

            // interact
            text = GUIBehavior.TextBoxBehavior(textNode.Id, textNode.Rect, text,
                                               out bool hovered, out bool active, out var context);

            // render
            var state = active ? GUIState.Active : hovered ? GUIState.Hover : GUIState.Normal;

            GUIAppearance.DrawTextBox(textNode, textNode.Id, text, context, state);

            // draw the box
            var dc = node.RenderOpen();

            dc.DrawBoxModel(node.RuleSet, node.Rect);
            dc.Close();

            // do GUI logic for possible scroll-bars
            node.OnGUI();

            return(text);
        }
コード例 #18
0
ファイル: PolygonButton.cs プロジェクト: zwcloud/ImGui
        /// <summary>
        /// Create a polyon-button.
        /// </summary>
        /// <param name="rect">position and size of the control</param>
        /// <param name="points"><see cref="Point"/> list of the polygon.</param>
        /// <param name="textRect">the rect that occupied by the text</param>
        /// <param name="text">text to display on the button</param>
        /// <returns>true when the users clicks the button.</returns>
        public static bool PolygonButton(Rect rect, IReadOnlyList <Point> points, Rect textRect, string text)
        {
            var window = GetCurrentWindow();

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

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

            text = Utility.FindRenderedText(text);
            if (node == null)
            {
                //create button node
                node = new Node(id, $"PolygonButton<{text}>");
                container.Add(node);
                node.UseBoxModel = true;
                node.RuleSet.Replace(GUISkin.Current[GUIControlName.Button]);
            }

            node.ActiveSelf = true;

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

            // interact
            var mousePos = Mouse.Instance.Position;
            var g        = GetCurrentContext();
            var clicked  = false;
            var hovered  = MathEx.IsPointInPolygon(mousePos, points, (Vector)node.Rect.Location);

            g.KeepAliveID(id);
            var state = GUIState.Normal;

            if (hovered)
            {
                g.SetHoverID(id);

                if (Mouse.Instance.LeftButtonPressed)//start track
                {
                    g.SetActiveID(id);
                }

                if (Mouse.Instance.LeftButtonReleased)//end track
                {
                    clicked = true;
                    g.SetActiveID(0);
                }
            }
            if (hovered)
            {
                state = GUIState.Hover;
                if (g.ActiveId == id && Mouse.Instance.LeftButtonState == KeyState.Down)
                {
                    state = GUIState.Active;
                }
            }
            node.State = state;

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

            // draw
            GUIAppearance.DrawPolygonButton(points, textRect, text, node);

            return(clicked);
        }
コード例 #19
0
ファイル: PolygonButton.cs プロジェクト: zwcloud/ImGui
        /// <summary>
        /// Create an auto-layout polyon-button.
        /// </summary>
        /// <param name="points"><see cref="Point"/> list of the polygon.</param>
        /// <param name="textRect">the rect that occupied by the text</param>
        /// <param name="text">text to display on the button</param>
        /// <param name="options">layout options that specify layouting properties. See also <see cref="GUILayout.Width"/>, <see cref="GUILayout.Height"/>, <see cref="GUILayout.ExpandWidth"/>, <see cref="GUILayout.ExpandHeight"/>, <see cref="GUILayout.StretchWidth"/>, <see cref="GUILayout.StretchHeight"/></param>
        /// <returns>true when the users clicks the button.</returns>
        internal static bool PolygonButton(IReadOnlyList <Point> points, Rect textRect, string text, LayoutOptions?options)
        {
            if (points.Count < 3)
            {
                return(false);
            }

            var window = GetCurrentWindow();

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

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

            text = Utility.FindRenderedText(text);
            if (node == null)
            {
                //create node
                node             = new Node(id, $"PolygonButton<{text}>");
                node.UseBoxModel = true;
                node.RuleSet.Replace(GUISkin.Current[GUIControlName.Button]);
                var boundingRect = new Rect(points[0], points[1]);
                foreach (var point in points)
                {
                    boundingRect = Rect.Union(boundingRect, point);
                }
                node.AttachLayoutEntry(boundingRect.Size);
            }
            container.AppendChild(node);
            node.RuleSet.ApplyOptions(options);
            node.ActiveSelf = true;

            // rect
            node.Rect = window.GetRect(id);
            textRect.Offset((Vector)node.Rect.Location);

            // interact
            var mousePos = Mouse.Instance.Position;
            var g        = GetCurrentContext();
            var clicked  = false;
            var hovered  = MathEx.IsPointInPolygon(mousePos, points, (Vector)node.Rect.Location);

            g.KeepAliveID(id);
            var state = GUIState.Normal;

            if (hovered)
            {
                g.SetHoverID(id);

                if (Mouse.Instance.LeftButtonPressed)//start track
                {
                    g.SetActiveID(id);
                }

                if (Mouse.Instance.LeftButtonReleased)//end track
                {
                    clicked = true;
                    g.SetActiveID(0);
                }
            }
            if (hovered)
            {
                state = GUIState.Hover;
                if (g.ActiveId == id && Mouse.Instance.LeftButtonState == KeyState.Down)
                {
                    state = GUIState.Active;
                }
            }
            node.State = state;

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

            // draw
            GUIAppearance.DrawPolygonButton(points, textRect, text, node);

            return(clicked);
        }