Example #1
0
        public int GetID <T>(T obj) where T : class
        {
            int seed = this.IDStack.Peek();
            var id   = this.Hash(seed, obj.GetHashCode());

            GUIContext g = Application.ImGuiContext;

            g.KeepAliveID(id);
            return(id);
        }
Example #2
0
        public int GetID(int int_id)
        {
            int seed = this.IDStack.Peek();
            var id   = this.Hash(seed, int_id);

            GUIContext g = Form.current.uiContext;

            g.KeepAliveID(id);
            return(id);
        }
Example #3
0
        public int GetID(int int_id)
        {
            int seed = this.IDStack.Peek();
            var id   = this.Hash(seed, int_id);

            GUIContext g = Application.ImGuiContext;

            g.KeepAliveID(id);
            return(id);
        }
Example #4
0
        public static double SliderBehavior(Rect sliderRect, int id, bool horizontal, double value, double minValue, double maxValue, out bool hovered, out bool held)
        {
            GUIContext g = Form.current.uiContext;

            hovered = false;
            held    = false;

            hovered = g.IsHovered(sliderRect, id);
            g.KeepAliveID(id);
            if (hovered)
            {
                g.SetHoverID(id);

                if (Mouse.Instance.LeftButtonPressed) //start track
                {
                    g.SetActiveID(id);
                }
            }
            if (g.ActiveId == id)
            {
                if (Mouse.Instance.LeftButtonState == KeyState.Down)
                {
                    var mousePos = Mouse.Instance.Position;
                    if (horizontal)
                    {
                        var leftPoint     = new Point(sliderRect.X + 10, sliderRect.Y + sliderRect.Height / 2);
                        var rightPoint    = new Point(sliderRect.Right - 10, sliderRect.Y + sliderRect.Height / 2);
                        var minX          = leftPoint.X;
                        var maxX          = rightPoint.X;
                        var currentPointX = MathEx.Clamp(mousePos.X, minX, maxX);
                        value = minValue + (currentPointX - minX) / (maxX - minX) * (maxValue - minValue);
                    }
                    else
                    {
                        var upPoint       = new Point(sliderRect.X + sliderRect.Width / 2, sliderRect.Y + 10);
                        var bottomPoint   = new Point(sliderRect.X + sliderRect.Width / 2, sliderRect.Bottom - 10);
                        var minY          = upPoint.Y;
                        var maxY          = bottomPoint.Y;
                        var currentPointY = MathEx.Clamp(mousePos.Y, minY, maxY);
                        value = (float)(minValue + (currentPointY - minY) / (maxY - minY) * (maxValue - minValue));
                    }
                }
                else //end track
                {
                    g.SetActiveID(0);
                }
            }

            if (g.ActiveId == id)
            {
                held = true;
            }

            return(value);
        }
Example #5
0
        public int GetID(ITexture texture)
        {
            int seed   = IDStack.Peek();
            int int_id = texture.GetHashCode();
            var id     = Hash(seed, int_id);

            GUIContext g = Form.current.uiContext;

            g.KeepAliveID(id);
            return(id);
        }
Example #6
0
        public int GetID(string str_id)
        {
            var    seed          = this.IDStack.Peek();
            var    hashCharIndex = str_id.IndexOf("##", StringComparison.Ordinal);
            string customId      = null;

            if (hashCharIndex > 0)
            {
                customId = str_id.Substring(hashCharIndex + 1);
            }

            var subId = string.IsNullOrWhiteSpace(customId) ? str_id.GetHashCode() : customId.GetHashCode();
            var id    = this.Hash(seed, subId);

            GUIContext g = Form.current.uiContext;

            g.KeepAliveID(id);

            return(id);
        }
Example #7
0
        public static bool ToggleBehavior(Rect rect, int id, bool value, out bool hovered)
        {
            GUIContext g = Form.current.uiContext;

            hovered = g.IsHovered(rect, id);
            g.KeepAliveID(id);
            if (hovered)
            {
                g.SetHoverID(id);

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

                if (g.ActiveId == id && Mouse.Instance.LeftButtonReleased)
                {
                    value = !value;
                    g.SetActiveID(0);
                }
            }
            return(value);
        }
Example #8
0
        public void NewFrame(GUIContext g)
        {
            // Handle user moving window (at the beginning of the frame to avoid input lag or sheering). Only valid for root windows.
            if (this.MovedWindowMoveId != 0 && this.MovedWindowMoveId == g.ActiveId)
            {
                g.KeepAliveID(this.MovedWindowMoveId);
                Debug.Assert(this.MovedWindow != null && this.MovedWindow.RootWindow != null);
                Debug.Assert(this.MovedWindow.RootWindow.MoveID == this.MovedWindowMoveId);
                if (Mouse.Instance.LeftButtonState == KeyState.Down)
                {
                    if (!this.MovedWindow.Flags.HaveFlag(WindowFlags.NoMove))
                    {
                        this.MovedWindow.Position += Mouse.Instance.MouseDelta;
                    }
                    this.FocusWindow(this.MovedWindow);
                }
                else
                {
                    g.SetActiveID(0);
                    this.MovedWindow       = null;
                    this.MovedWindowMoveId = 0;
                }
            }
            else
            {
                this.MovedWindow       = null;
                this.MovedWindowMoveId = 0;
            }

            // Find the window we are hovering. Child windows can extend beyond the limit of their parent so we need to derive HoveredRootWindow from HoveredWindow
            this.HoveredWindow = this.MovedWindow ?? this.FindHoveredWindow(Mouse.Instance.Position, false);
            if (this.HoveredWindow != null && (this.HoveredWindow.Flags.HaveFlag(WindowFlags.ChildWindow)))
            {
                this.HoveredRootWindow = this.HoveredWindow.RootWindow;
            }
            else
            {
                this.HoveredRootWindow = (this.MovedWindow != null) ? this.MovedWindow.RootWindow : this.FindHoveredWindow(Mouse.Instance.Position, true);
            }


            // Scale & Scrolling
            if (this.HoveredWindow != null && Mouse.Instance.MouseWheel != 0.0 && !this.HoveredWindow.Collapsed)
            {
                Window window = this.HoveredWindow;
                if (Keyboard.Instance.KeyDown(Key.LeftControl))
                {
                    //Scale
                    //TODO
                }
                else
                {
                    // Scroll
                    if (!(window.Flags.HaveFlag(WindowFlags.NoScrollWithMouse)))
                    {
                        var    newScrollY         = window.Scroll.Y - Math.Sign(Mouse.Instance.MouseWheel) * 20 /*scroll step*/;
                        float  window_rounding    = (float)window.WindowContainer.RuleSet.Get <double>(GUIStyleName.WindowRounding);
                        double resize_corner_size = Math.Max(window.WindowContainer.RuleSet.FontSize * 1.35, window_rounding + 1.0 + window.WindowContainer.RuleSet.FontSize * 0.2);
                        var    contentSize        = window.ContentRect.Size;
                        var    vH = window.Rect.Height - window.TitleBarHeight - window.WindowContainer.RuleSet.BorderVertical - window.WindowContainer.RuleSet.PaddingVertical;
                        var    cH = contentSize.Height;
                        if (cH > vH)
                        {
                            newScrollY = MathEx.Clamp(newScrollY, 0, cH - vH);
                            window.SetWindowScrollY(newScrollY);
                        }
                    }
                }
            }

            // Mark all windows as not visible
            for (int i = 0; i != this.Windows.Count; i++)
            {
                Window window = this.Windows[i];
                window.WasActive = window.Active;
                window.Active    = false;
                window.Accessed  = false;
            }

            // No window should be open at the beginning of the frame.
            // But in order to allow the user to call NewFrame() multiple times without calling Render(), we are doing an explicit clear.
            this.WindowStack.Clear();
        }
Example #9
0
        internal static bool DoPolygonButton(Rect rect, IReadOnlyList <Point> points, Rect textRect, string text)
        {
            Form       form     = Form.current;
            GUIContext g        = form.uiContext;
            Window     window   = g.WindowManager.CurrentWindow;
            DrawList   d        = window.DrawList;
            int        id       = window.GetID(text);
            var        mousePos = Mouse.Instance.Position;

            var clicked = false;
            var hovered = MathEx.IsPointInPolygon(mousePos, points, new Vector(rect.X, rect.Y));

            textRect.Offset(rect.X, rect.Y);

            //control logic
            g.KeepAliveID(id);
            if (hovered)
            {
                g.SetHoverID(id);

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

                if (Mouse.Instance.LeftButtonReleased)//end track
                {
                    clicked = true;
                    g.SetActiveID(GUIContext.None);
                }
            }

            // ui representation
            var state = GUI.Normal;

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

            // ui painting
            {
                var style = GUISkin.Instance[GUIControlName.PolygonButton];

                d.PathClear();
                foreach (var point in points)
                {
                    d.PathMoveTo(point + new Vector(rect.X, rect.Y));
                }
                d.PathFill(style.FillColor);

                d.PathClear();
                foreach (var point in points)
                {
                    d.PathMoveTo(point + new Vector(rect.X, rect.Y));
                }
                d.PathStroke(style.LineColor, true, 2);

                d.DrawBoxModel(textRect, text, style, state);
            }

            return(clicked);
        }