Ejemplo n.º 1
0
        public virtual void Draw(SpriteBatch spriteBatch)
        {
            if (!IsShow)
            {
                return;
            }

            if (IsClicked && ClickedTexture != null)
            {
                ClickedTexture.Draw(spriteBatch, ScreenPosition);
            }
            else if (IsMouveOver && MouseOverTexture != null)
            {
                MouseOverTexture.Draw(spriteBatch, ScreenPosition);
            }
            else if (BaseTexture != null)
            {
                BaseTexture.Draw(spriteBatch, ScreenPosition);
            }
        }
Ejemplo n.º 2
0
        public virtual void Update(GameTime gameTime)
        {
            if (!IsShow)
            {
                return;
            }
            var mouseState = Mouse.GetState();

            if (Globals.IsInputDisabled)
            {
                mouseState = Utils.GetMouseStateJustPosition(mouseState);
            }
            var screenPosition = new Vector2(mouseState.X, mouseState.Y);
            var position       = screenPosition - ScreenPosition;
            var lastPosition   = new Vector2(_lastMouseState.X, _lastMouseState.Y) - ScreenPosition;

            if (OnUpdate != null)
            {
                OnUpdate(this, gameTime);
            }

            if (RegionInScreen.Contains(mouseState.X, mouseState.Y))
            {
                if (InRange == false)
                {
                    if (EnteredSound != null)
                    {
                        EnteredSound.Play();
                    }
                    if (MouseEnter != null)
                    {
                        MouseEnter(this, new MouseEvent(position, screenPosition));
                    }
                }

                if (lastPosition == position && !_isStayOver)
                {
                    _stayOverMilliSecond += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                    if (_stayOverMilliSecond > 500)
                    {
                        _isStayOver = true;
                        if (MouseStayOver != null)
                        {
                            MouseStayOver(this, new MouseEvent(position, screenPosition));
                        }
                    }
                }

                if (mouseState.LeftButton == ButtonState.Pressed &&
                    _lastMouseState.LeftButton == ButtonState.Released)
                {
                    IsClicked = true;
                    if (ClickedTexture != null)
                    {
                        ClickedTexture.CurrentFrameIndex = 0;
                    }

                    if (MouseLeftDown != null)
                    {
                        MouseLeftDown(this, new MouseLeftDownEvent(position, screenPosition));
                    }

                    if (ClickedSound != null)
                    {
                        ClickedSound.Play();
                    }
                }

                if (mouseState.LeftButton == ButtonState.Released &&
                    _lastMouseState.LeftButton == ButtonState.Pressed &&
                    IsClicked)
                {
                    if (Click != null)
                    {
                        Click(this, new MouseLeftClickEvent(position, screenPosition));
                    }
                }

                if (mouseState.RightButton == ButtonState.Pressed &&
                    _lastMouseState.RightButton == ButtonState.Released)
                {
                    IsRightClicked = true;
                    if (MouseRightDown != null)
                    {
                        MouseRightDown(this, new MouseRightDownEvent(position, screenPosition));
                    }

                    if (RightClick != null)
                    {
                        RightClick(this, new MouseRightClickEvent(position, screenPosition));
                    }
                }

                if (MouseScrollHandler.IsScrollUp &&
                    MouseScrollUp != null)
                {
                    MouseScrollUp(this, new MouseEvent(position, screenPosition));
                }
                if (MouseScrollHandler.IsScrollDown &&
                    MouseScrollDown != null)
                {
                    MouseScrollDown(this, new MouseEvent(position, screenPosition));
                }

                InRange     = true;
                IsMouveOver = true;
            }
            else
            {
                if (InRange)
                {
                    if (MouseLeave != null)
                    {
                        MouseLeave(this, new MouseEvent(position, ScreenPosition));
                    }
                }
                InRange              = false;
                IsMouveOver          = false;
                _isStayOver          = false;
                _stayOverMilliSecond = 0;
            }

            if (mouseState.LeftButton == ButtonState.Released &&
                _lastMouseState.LeftButton == ButtonState.Pressed)
            {
                if (MouseLeftUp != null)
                {
                    MouseLeftUp(this, new MouseLeftUpEvent(position, screenPosition));
                }

                IsClicked = false;
            }

            if (mouseState.RightButton == ButtonState.Released &&
                _lastMouseState.RightButton == ButtonState.Pressed)
            {
                if (MouseRightUp != null)
                {
                    MouseRightUp(this, new MouseRightUpEvent(position, screenPosition));
                }

                IsRightClicked = false;
            }

            if (lastPosition != position)
            {
                if (MouseMove != null)
                {
                    MouseMove(this,
                              new MouseMoveEvent(position,
                                                 screenPosition,
                                                 mouseState.LeftButton == ButtonState.Pressed,
                                                 mouseState.RightButton == ButtonState.Pressed));
                }
            }

            if (IsClicked)
            {
                if (MouseLeftClicking != null)
                {
                    MouseLeftClicking(this, new MouseLeftClickingEvent(position, screenPosition));
                }
            }

            if (IsClicked || IsRightClicked)
            {
                GuiManager.IsMouseStateEated = true;
            }

            if (IsClicked && ClickedTexture != null)
            {
                ClickedTexture.Update(gameTime);
            }
            else if (IsMouveOver && MouseOverTexture != null)
            {
                MouseOverTexture.Update(gameTime);
            }
            else if (BaseTexture != null)
            {
                BaseTexture.Update(gameTime);
            }

            _lastMouseState = mouseState;
        }