Пример #1
0
        /// <inheritdoc />
        /// <summary>
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            // Check if the mouse is in the click area, as well as if the game window is actually the active window.
            if (GameBase.Game.IsActive && Visible && IsMouseInClickArea())
            {
                // Set this to be hovered without the draw order check.
                IsHoveredWithoutDrawOrder = true;

                // Get the button that is on the top layer.
                var topLayerButton = ButtonManager.Buttons.FindAll(x => x.IsHoveredWithoutDrawOrder && x.IsClickable && IsGloballyClickable)
                                     .OrderBy(x => x.Depth).ThenByDescending(x => x.DrawOrder).DefaultIfEmpty(null).First();

                if (topLayerButton == null)
                {
                    base.Update(gameTime);
                    return;
                }

                // Set this to be truly hovered and follow up with click actions for this button.
                if (topLayerButton == this)
                {
                    if (!IsHovered)
                    {
                        Hovered?.Invoke(this, EventArgs.Empty);
                    }

                    IsHovered = true;
                    OnHover(gameTime);

                    // If we're not waiting for a click release and the mouse button is currently held down,
                    // then we'll set this to true.
                    if (!WaitingForClickRelease && MouseManager.IsUniquePress(MouseButton.Left) || MouseManager.IsUniquePress(MouseButton.Right) ||
                        MouseManager.IsUniquePress(MouseButton.Middle))
                    {
                        WaitingForClickRelease = true;
                        IsHeld = true;

                        if (MouseManager.IsUniquePress(MouseButton.Left))
                        {
                            MouseButtonClicked = MouseButton.Left;
                        }
                        else if (MouseManager.IsUniquePress(MouseButton.Right))
                        {
                            MouseButtonClicked = MouseButton.Right;
                        }
                        else if (MouseManager.IsUniquePress(MouseButton.Middle))
                        {
                            MouseButtonClicked = MouseButton.Middle;
                        }
                    }
                    // In the event that we are waiting for a click release, and the user doesn, then we can call
                    // the click action.
                    else if (WaitingForClickRelease)
                    {
                        // Check to see if the clicked button was released.
                        var released = false;
                        switch (MouseButtonClicked)
                        {
                        case MouseButton.Left:
                            released = MouseManager.CurrentState.LeftButton == ButtonState.Released;
                            break;

                        case MouseButton.Right:
                            released = MouseManager.CurrentState.RightButton == ButtonState.Released;
                            break;

                        case MouseButton.Middle:
                            released = MouseManager.CurrentState.MiddleButton == ButtonState.Released;
                            break;
                        }

                        // If the button was released, reset the waiting property.
                        if (released)
                        {
                            WaitingForClickRelease = false;

                            if (IsClickable)
                            {
                                switch (MouseButtonClicked)
                                {
                                case MouseButton.Left:
                                    Clicked?.Invoke(this, new EventArgs());
                                    break;

                                case MouseButton.Right:
                                    RightClicked?.Invoke(this, new EventArgs());
                                    break;

                                case MouseButton.Middle:
                                    MiddleMouseClicked?.Invoke(this, new EventArgs());
                                    break;

                                default:
                                    throw new ArgumentOutOfRangeException();
                                }
                            }

                            MouseButtonClicked = null;
                        }
                    }
                }
                // If the button isn't the top layered button, then we'll want to consider it not hovered.
                else
                {
                    var wasHovered = IsHovered;
                    IsHovered = false;

                    WaitingForClickRelease = false;
                    MouseButtonClicked     = null;

                    if (wasHovered)
                    {
                        LeftHover?.Invoke(this, EventArgs.Empty);
                    }

                    OnNotHovered(gameTime);
                }
            }
            // The button isn't actually hovered over so we can safely consider it not hovered.
            // However,
            else
            {
                IsHoveredWithoutDrawOrder = false;
                WaitingForClickRelease    = false;

                var wasHovered = IsHovered;
                IsHovered = false;

                if (wasHovered)
                {
                    LeftHover?.Invoke(this, EventArgs.Empty);
                }

                OnNotHovered(gameTime);
            }

            if (MouseManager.CurrentState.LeftButton == ButtonState.Released)
            {
                IsHeld = false;
            }

            if (IsHeld)
            {
                OnHeld(gameTime);
            }

            // Fire an event if the user clicks outside of the button.
            if (MouseManager.IsUniqueClick(MouseButton.Left) && !IsMouseInClickArea())
            {
                ClickedOutside?.Invoke(this, EventArgs.Empty);
            }

            base.Update(gameTime);
        }
Пример #2
0
        /// <inheritdoc />
        /// <summary>
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            // Check if the mouse is in the click area, as well as if the game window is actually the active window.
            if (GameBase.Game.IsActive && Visible && IsMouseInClickArea())
            {
                // Set this to be hovered without the draw order check.
                IsHoveredWithoutDrawOrder = true;

                // Get the button that is on the top layer.
                Button topLayerButton;

                try
                {
                    topLayerButton = ButtonManager.Buttons.FindAll(x => x.IsHoveredWithoutDrawOrder && x.IsClickable && IsGloballyClickable)
                                     .OrderByDescending(x => x.DrawOrder).First();
                }
                catch (Exception e)
                {
                    base.Update(gameTime);
                    return;
                }

                // Set this to be truly hovered and follow up with click actions for this button.
                if (topLayerButton == this)
                {
                    if (!IsHovered)
                    {
                        Hovered?.Invoke(this, EventArgs.Empty);
                    }

                    IsHovered = true;
                    OnHover(gameTime);

                    // If we're not waiting for a click reelase and the mouse button is currently held down,
                    // then we'll set this to true.
                    if (!WaitingForClickRelease && MouseManager.CurrentState.LeftButton == ButtonState.Pressed &&
                        MouseManager.PreviousState.LeftButton == ButtonState.Released)
                    {
                        WaitingForClickRelease = true;
                        IsHeld = true;
                    }
                    // In the event that we are waiting for a click release, and the user doesn, then we can call
                    // the click action.
                    else if (WaitingForClickRelease && MouseManager.CurrentState.LeftButton == ButtonState.Released)
                    {
                        // Now that the button is clicked, reset the waiting property.
                        WaitingForClickRelease = false;

                        if (IsClickable)
                        {
                            Clicked?.Invoke(this, new EventArgs());
                        }
                    }
                }
                // If the button isn't the top layered button, then we'll want to consider it not hovered.
                else
                {
                    if (IsHovered)
                    {
                        LeftHover?.Invoke(this, EventArgs.Empty);
                    }

                    IsHovered = false;
                    WaitingForClickRelease = false;

                    OnNotHovered(gameTime);
                }
            }
            // The button isn't actually hovered over so we can safely consider it not hovered.
            // However,
            else
            {
                if (IsHovered)
                {
                    LeftHover?.Invoke(this, EventArgs.Empty);
                }

                IsHoveredWithoutDrawOrder = false;
                IsHovered = false;
                WaitingForClickRelease = false;

                OnNotHovered(gameTime);
            }

            if (MouseManager.CurrentState.LeftButton == ButtonState.Released)
            {
                IsHeld = false;
            }

            if (IsHeld)
            {
                OnHeld(gameTime);
            }

            // Fire an event if the user clicks outside of the button.
            if (MouseManager.IsUniqueClick(MouseButton.Left) && !IsMouseInClickArea())
            {
                ClickedOutside?.Invoke(this, EventArgs.Empty);
            }

            base.Update(gameTime);
        }