Ejemplo n.º 1
0
        public static bool BuildRadioButton(this ImGui gui, string option, bool selected, SchemeColor color = SchemeColor.None)
        {
            using (gui.EnterRow())
            {
                gui.BuildIcon(selected ? Icon.RadioCheck : Icon.RadioEmpty, 1.5f, color);
                gui.BuildText(option, Font.text, color: color, wrap: true);
            }

            return(!selected && gui.OnClick(gui.lastRect));
        }
Ejemplo n.º 2
0
        public static bool BuildCheckBox(this ImGui gui, string text, bool value, out bool newValue, SchemeColor color = SchemeColor.None)
        {
            using (gui.EnterRow())
            {
                gui.BuildIcon(value ? Icon.CheckBoxCheck : Icon.CheckBoxEmpty, 1.5f, color);
                gui.BuildText(text, Font.text, color: color);
            }

            if (gui.OnClick(gui.lastRect))
            {
                newValue = !value;
                return(true);
            }

            newValue = value;
            return(false);
        }
Ejemplo n.º 3
0
        public bool BuildTextInput(string text, out string newText, string placeholder, FontFile.FontSize fontSize, bool delayed, Icon icon, Padding padding, RectAlignment alignment, SchemeColor color)
        {
            newText = text;
            Rect textRect, realTextRect;

            using (gui.EnterGroup(padding, RectAllocator.LeftRow))
            {
                var lineSize = gui.PixelsToUnits(fontSize.lineSize);
                if (icon != Icon.None)
                {
                    gui.BuildIcon(icon, lineSize, color + 3);
                }
                textRect = gui.RemainingRow(0.3f).AllocateRect(0, lineSize, RectAlignment.MiddleFullRow);
            }
            var boundingRect = gui.lastRect;
            var focused      = rect == boundingRect;

            if (focused && this.text == null)
            {
                this.text = text ?? "";
                SetCaret(0, this.text.Length);
            }

            switch (gui.action)
            {
            case ImGuiAction.MouseDown:
                if (gui.actionParameter != SDL.SDL_BUTTON_LEFT)
                {
                    break;
                }
                if (gui.ConsumeMouseDown(boundingRect))
                {
                    SetFocus(boundingRect, text ?? "");
                    GetTextParameters(this.text, textRect, fontSize, alignment, out _, out _, out _, out realTextRect);
                    SetCaret(FindCaretIndex(text, gui.mousePosition.X - realTextRect.X, fontSize, textRect.Width));
                }
                break;

            case ImGuiAction.MouseMove:
                if (focused && gui.actionParameter == SDL.SDL_BUTTON_LEFT)
                {
                    GetTextParameters(this.text, textRect, fontSize, alignment, out _, out _, out _, out realTextRect);
                    SetCaret(caret, FindCaretIndex(this.text, gui.mousePosition.X - realTextRect.X, fontSize, textRect.Width));
                }
                gui.ConsumeMouseOver(boundingRect, RenderingUtils.cursorCaret, false);
                break;

            case ImGuiAction.Build:
                var    textColor = color + 2;
                string textToBuild;
                if (focused)
                {
                    textToBuild = this.text;
                }
                else if (string.IsNullOrEmpty(text))
                {
                    textToBuild = placeholder;
                    textColor   = color + 3;
                }
                else
                {
                    textToBuild = text;
                }

                GetTextParameters(textToBuild, textRect, fontSize, alignment, out var cachedText, out var scale, out var textWidth, out realTextRect);
                if (cachedText != null)
                {
                    gui.DrawRenderable(realTextRect, cachedText, textColor);
                }

                if (focused)
                {
                    if (selectionAnchor != caret)
                    {
                        var left  = GetCharacterPosition(Math.Min(selectionAnchor, caret), fontSize, textWidth) * scale;
                        var right = GetCharacterPosition(Math.Max(selectionAnchor, caret), fontSize, textWidth) * scale;
                        gui.DrawRectangle(new Rect(left + realTextRect.X, realTextRect.Y, right - left, realTextRect.Height), SchemeColor.TextSelection);
                    }
                    else
                    {
                        if (nextCaretTimer <= Ui.time)
                        {
                            nextCaretTimer = Ui.time + 500;
                            caretVisible   = !caretVisible;
                        }
                        gui.SetNextRebuild(nextCaretTimer);
                        if (caretVisible)
                        {
                            var caretPosition = GetCharacterPosition(caret, fontSize, textWidth) * scale;
                            gui.DrawRectangle(new Rect(caretPosition + realTextRect.X - 0.05f, realTextRect.Y, 0.1f, realTextRect.Height), color + 2);
                        }
                    }
                }
                gui.DrawRectangle(boundingRect, color);
                break;
            }

            if (boundingRect == prevRect)
            {
                var changed = text != prevText;
                if (changed)
                {
                    newText = prevText;
                }
                prevRect = default;
                prevText = null;
                return(changed);
            }

            if (focused && !delayed && this.text != text)
            {
                newText = this.text;
                return(true);
            }

            return(false);
        }
Ejemplo n.º 4
0
 public static bool BuildButton(this ImGui gui, Icon icon, SchemeColor normal = SchemeColor.None, SchemeColor over = SchemeColor.Grey, SchemeColor down = SchemeColor.None, float size = 1.5f)
 {
     using (gui.EnterGroup(new Padding(0.3f)))
         gui.BuildIcon(icon, size);
     return(gui.BuildButton(gui.lastRect, normal, over, down) == Event.Click);
 }