예제 #1
0
        public static bool BuildLink(this ImGui gui, string text)
        {
            gui.BuildText(text, color: SchemeColor.Link);
            var rect = gui.lastRect;

            switch (gui.action)
            {
            case ImGuiAction.MouseMove:
                gui.ConsumeMouseOver(rect, RenderingUtils.cursorHand);
                break;

            case ImGuiAction.MouseDown:
                if (gui.actionParameter == SDL.SDL_BUTTON_LEFT)
                {
                    gui.ConsumeMouseDown(rect);
                }
                break;

            case ImGuiAction.MouseUp:
                if (gui.ConsumeMouseUp(rect))
                {
                    return(true);
                }
                break;

            case ImGuiAction.Build:
                if (gui.IsMouseOver(rect))
                {
                    gui.DrawRectangle(new Rect(rect.X, rect.Bottom - 0.2f, rect.Width, 0.1f), SchemeColor.Link);
                }
                break;
            }

            return(false);
        }
예제 #2
0
        public static Event BuildButton(this ImGui gui, Rect rect, SchemeColor normal, SchemeColor over, SchemeColor down = SchemeColor.None, uint button = SDL.SDL_BUTTON_LEFT)
        {
            if (button == 0)
            {
                button = (uint)InputSystem.Instance.mouseDownButton;
            }
            switch (gui.action)
            {
            case ImGuiAction.MouseMove:
                var wasOver = gui.IsMouseOver(rect);
                return(gui.ConsumeMouseOver(rect, RenderingUtils.cursorHand) && !wasOver ? Event.MouseOver : Event.None);

            case ImGuiAction.MouseDown:
                return(gui.actionParameter == button && gui.ConsumeMouseDown(rect, button) ? Event.MouseDown : Event.None);

            case ImGuiAction.MouseUp:
                return(gui.actionParameter == button && gui.ConsumeMouseUp(rect, true, button) ? Event.Click : Event.None);

            case ImGuiAction.Build:
                var color = gui.IsMouseOver(rect) ? (down != SchemeColor.None && gui.IsMouseDown(rect, button)) ? down : over : normal;
                gui.DrawRectangle(rect, color);
                return(Event.None);

            default:
                return(Event.None);
            }
        }
예제 #3
0
 public static bool OnClick(this ImGui gui, Rect rect)
 {
     if (gui.action == ImGuiAction.MouseUp)
     {
         return(gui.ConsumeMouseUp(rect));
     }
     if (gui.action == ImGuiAction.MouseDown && gui.actionParameter == SDL.SDL_BUTTON_LEFT)
     {
         gui.ConsumeMouseDown(rect);
     }
     return(false);
 }
예제 #4
0
 public static bool InitiateDrag <T>(this ImGui gui, Rect moveHandle, Rect contents, T index, SchemeColor backgroundColor = SchemeColor.PureBackground)
 {
     if (gui.action == ImGuiAction.MouseDown)
     {
         gui.ConsumeMouseDown(moveHandle);
     }
     if (gui.ShouldEnterDrag(moveHandle) || (gui.action == ImGuiAction.Build && gui.IsDragging(index)))
     {
         gui.SetDraggingArea(contents, index, backgroundColor);
         return(true);
     }
     return(false);
 }
예제 #5
0
        public static bool BuildSlider(this ImGui gui, float value, out float newValue, float width = 10f)
        {
            var sliderRect  = gui.AllocateRect(width, 2f, RectAlignment.Full);
            var handleStart = (sliderRect.Width - 1f) * value;
            var handleRect  = new Rect(sliderRect.X + handleStart, sliderRect.Y, 1f, sliderRect.Height);
            var update      = false;

            newValue = value;

            switch (gui.action)
            {
            case ImGuiAction.Build:
                gui.DrawRectangle(handleRect, gui.IsMouseOverOrDown(sliderRect) ? SchemeColor.Background : SchemeColor.PureBackground, RectangleBorder.Thin);
                sliderRect.Y     += (sliderRect.Height - 0.3f) / 2f;
                sliderRect.Height = 0.3f;
                gui.DrawRectangle(sliderRect, SchemeColor.Grey);
                break;

            case ImGuiAction.MouseMove:
                if (gui.IsMouseDown(sliderRect))
                {
                    update = true;
                }
                else
                {
                    gui.ConsumeMouseOver(sliderRect, RenderingUtils.cursorHand);
                }
                break;

            case ImGuiAction.MouseDown:
                if (gui.IsMouseOver(sliderRect))
                {
                    gui.ConsumeMouseDown(sliderRect);
                    update = true;
                }
                break;
            }

            if (!update)
            {
                return(false);
            }
            var positionX = (gui.mousePosition.X - sliderRect.X - 0.5f) / (sliderRect.Width - 1f);

            newValue = MathUtils.Clamp(positionX, 0f, 1f);
            gui.Rebuild();
            return(true);
        }
예제 #6
0
        public static bool BuildButtonClick(this ImGui gui, Rect rect, uint button = SDL.SDL_BUTTON_LEFT)
        {
            if (gui.actionParameter == button)
            {
                if (gui.action == ImGuiAction.MouseDown)
                {
                    gui.ConsumeMouseDown(rect);
                }
                else if (gui.action == ImGuiAction.MouseUp)
                {
                    return(gui.ConsumeMouseUp(rect));
                }
            }

            return(false);
        }
예제 #7
0
        private void BuildHeaderResizer(ImGui gui, DataColumn <TData> column, Rect rect)
        {
            switch (gui.action)
            {
            case ImGuiAction.Build:
                var center = rect.X + rect.Width * 0.5f;
                if (gui.IsMouseDown(rect, SDL.SDL_BUTTON_LEFT))
                {
                    var unclampedWidth = gui.mousePosition.X - rect.Center.X + column.width;
                    var clampedWidth   = MathUtils.Clamp(unclampedWidth, column.minWidth, column.maxWidth);
                    center = center - column.width + clampedWidth;
                }
                var viewRect = new Rect(center - 0.1f, rect.Y, 0.2f, rect.Height);
                gui.DrawRectangle(viewRect, SchemeColor.GreyAlt);
                break;

            case ImGuiAction.MouseMove:
                gui.ConsumeMouseOver(rect, RenderingUtils.cursorHorizontalResize);
                if (gui.IsMouseDown(rect, SDL.SDL_BUTTON_LEFT))
                {
                    gui.Rebuild();
                }
                break;

            case ImGuiAction.MouseDown:
                gui.ConsumeMouseDown(rect, cursor: RenderingUtils.cursorHorizontalResize);
                break;

            case ImGuiAction.MouseUp:
                if (gui.ConsumeMouseUp(rect, false))
                {
                    var unclampedWidth = gui.mousePosition.X - rect.Center.X + column.width;
                    column.width = MathUtils.Clamp(unclampedWidth, column.minWidth, column.maxWidth);
                    contentGui?.Rebuild();
                }
                break;
            }
        }
예제 #8
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);
        }