Exemple #1
0
        public static bool BuildButton(this ImGui gui, string text, SchemeColor color = SchemeColor.Primary, Padding?padding = null, bool active = true)
        {
            if (!active)
            {
                color = SchemeColor.Grey;
            }
            using (gui.EnterGroup(padding ?? DefaultButtonPadding, active ? color + 2 : color + 3))
                gui.BuildText(text, Font.text, align: RectAlignment.Middle);

            return(gui.BuildButton(gui.lastRect, color, color + 1) == Event.Click && active);
        }
Exemple #2
0
 public static bool BuildContextMenuButton(this ImGui gui, string text, string rightText = null)
 {
     using (gui.EnterGroup(DefaultButtonPadding, RectAllocator.LeftRow, SchemeColor.BackgroundText))
     {
         gui.BuildText(text, Font.text, wrap: true);
         if (rightText != null)
         {
             gui.allocator = RectAllocator.RightRow;
             gui.BuildText(rightText, align: RectAlignment.MiddleRight);
         }
     }
     return(gui.BuildButton(gui.lastRect, SchemeColor.None, SchemeColor.Grey) == Event.Click);
 }
Exemple #3
0
        public static Event BuildRedButton(this ImGui gui, Icon icon, float size = 1.5f)
        {
            Rect iconRect;

            using (gui.EnterGroup(new Padding(0.3f)))
                iconRect = gui.AllocateRect(size, size, RectAlignment.Middle);
            var evt = gui.BuildButton(gui.lastRect, SchemeColor.None, SchemeColor.Error);

            if (gui.isBuilding)
            {
                gui.DrawIcon(iconRect, icon, gui.IsMouseOver(gui.lastRect) ? SchemeColor.ErrorText : SchemeColor.Error);
            }
            return(evt);
        }
Exemple #4
0
        public static Event BuildRedButton(this ImGui gui, string text)
        {
            Rect      textRect;
            TextCache cache;

            using (gui.EnterGroup(DefaultButtonPadding))
                textRect = gui.AllocateTextRect(out cache, text, align: RectAlignment.Middle);
            var evt = gui.BuildButton(gui.lastRect, SchemeColor.None, SchemeColor.Error);

            if (gui.isBuilding)
            {
                gui.DrawRenderable(textRect, cache, gui.IsMouseOver(gui.lastRect) ? SchemeColor.ErrorText : SchemeColor.Error);
            }
            return(evt);
        }
        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);
        }
Exemple #6
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);
 }