Example #1
0
        /// <summary>
        /// Occurs when a mouse button is depressed.
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnMouseDown(MouseControlEventArgs e)
        {
            e.SourceControl = this;

            if (IsResizable && e.Left)
            {
                if (this.TopDragArea.Intersects(e.SourceArea))
                {
                    this.IsDraggingTop = true;
                }

                if (this.BottomDragArea.Intersects(e.SourceArea))
                {
                    this.IsDraggingBottom = true;
                }

                if (this.RightDragArea.Intersects(e.SourceArea))
                {
                    this.IsDraggingRight = true;
                }

                if (this.LeftDragArea.Intersects(e.SourceArea))
                {
                    this.IsDraggingLeft = true;
                }
            }

            if (e.SourceArea.Intersects(this.Area))
            {
                this.IsDepressed = true;
                this.IsDirty = true;

                EventHandler<MouseControlEventArgs> handler = MouseDown;
                if (handler != null)
                {
                    handler(this, e);
                }
            }

            foreach (var control in this.Children)
            {
                control.OnMouseDown(e);
            }
        }
Example #2
0
        /// <summary>
        /// Occurs when a mouse button is released.
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnMouseUp(MouseControlEventArgs e)
        {
            e.SourceControl = this;

            if (this.GuiManager != null)
            {
                if (IsDraggingLeft)
                {
                    this.Resize(new Rectangle(this.GuiManager.MouseArea.X, this.Area.Y, this.Area.Width + (this.Area.X - this.GuiManager.MouseArea.X), this.Area.Height));
                }

                if (IsDraggingTop)
                {
                    this.Resize(new Rectangle(this.Area.X, this.GuiManager.MouseArea.Y, this.Area.Width, this.Area.Height + (this.Area.Y - this.GuiManager.MouseArea.Y)));
                }

                if (IsDraggingRight)
                {
                    this.Resize(new Rectangle(this.Area.X, this.Area.Y, this.GuiManager.MouseArea.X - this.Area.X, this.Area.Height));
                }

                if (IsDraggingBottom)
                {
                    this.Resize(new Rectangle(this.Area.X, this.Area.Y, this.Area.Width, this.GuiManager.MouseArea.Y - this.Area.Y));
                }
            }

            this.IsDraggingBottom = false;
            this.IsDraggingLeft = false;
            this.IsDraggingRight = false;
            this.IsDraggingTop = false;

            if (this.IsDepressed)
            {
                this.IsDepressed = false;
                this.IsDirty = true;
            }

            if (e.SourceArea.Intersects(this.Area))
            {
                EventHandler<MouseControlEventArgs> handler = MouseUp;
                if (handler != null)
                {
                    handler(this, e);
                }

                //Handle clicks (each child control needs to check this themselves because of the area check.
                var clickArguments = new MouseControlEventArgs(this);
                if (e.Left)
                {
                    if (e.LeftArea.Intersects(this.Area))
                    {
                        clickArguments.Left = true;
                        this.HasFocus = true;
                    }
                    else
                    {
                        this.HasFocus = false;
                    }
                }

                if (e.Middle)
                {
                    if (e.MiddleArea.Intersects(this.Area))
                    {
                        clickArguments.Middle = true;
                    }
                }

                if (e.Right)
                {
                    if (e.RightArea.Intersects(this.Area))
                    {
                        clickArguments.Right = true;
                    }
                }

                if (clickArguments.Left || clickArguments.Middle || clickArguments.Right)
                {
                    this.OnClick(clickArguments);
                }
            }

            foreach (var control in this.Children)
            {
                control.OnMouseUp(e);
            }
        }
Example #3
0
        /// <summary>
        /// Raises events for controls under the Gui manager.
        /// This should be called from the Game Update method.
        /// </summary>
        public void Update()
        {
            var mouseState = Mouse.GetState();
            var keyboardState = Keyboard.GetState();
            var gamepadState = GamePad.GetState(PlayerIndex.One); //TODO: design gamepad support.

            this.MouseArea = new Rectangle(mouseState.X,mouseState.Y,1,1); //used for intersect.

            //Mouse Down
            var mouseDownArguments = new MouseControlEventArgs(this);
            mouseDownArguments.SourceArea = this.MouseArea;
            if (mouseState.LeftButton == ButtonState.Pressed && mOldMouseState.LeftButton != ButtonState.Pressed)
            {
                mouseDownArguments.Left = true;
                mLeftMouseDownArea = mouseDownArguments.SourceArea;
            }
            if (mouseState.MiddleButton == ButtonState.Pressed && mOldMouseState.MiddleButton != ButtonState.Pressed)
            {
                mouseDownArguments.Middle = true;
                mMiddleMouseDownArea = mouseDownArguments.SourceArea;
            }
            if (mouseState.RightButton == ButtonState.Pressed && mOldMouseState.RightButton != ButtonState.Pressed)
            {
                mouseDownArguments.Right = true;
                mRightMouseDownArea = mouseDownArguments.SourceArea;
            }
            if (mouseDownArguments.Left || mouseDownArguments.Middle || mouseDownArguments.Right)
            {
                base.OnMouseDown(mouseDownArguments);
            }

            //Mouse Up
            var mouseUpArguments = new MouseControlEventArgs(this);
            mouseUpArguments.SourceArea = this.MouseArea;
            if (mouseState.LeftButton == ButtonState.Released && mOldMouseState.LeftButton != ButtonState.Released)
            {
                mouseUpArguments.Left = true;
                mouseUpArguments.LeftArea = mLeftMouseDownArea;
            }
            if (mouseState.MiddleButton == ButtonState.Released && mOldMouseState.MiddleButton != ButtonState.Released)
            {
                mouseUpArguments.Middle = true;
                mouseUpArguments.MiddleArea = mMiddleMouseDownArea;
            }
            if (mouseState.RightButton == ButtonState.Released && mOldMouseState.RightButton != ButtonState.Released)
            {
                mouseUpArguments.Right = true;
                mouseUpArguments.RightArea = mRightMouseDownArea;
            }
            if (mouseUpArguments.Left || mouseUpArguments.Middle || mouseUpArguments.Right)
            {
                base.OnMouseUp(mouseUpArguments);
            }

            //Key Down
            bool isShiftDepressed = keyboardState.IsKeyDown(Keys.LeftShift) || keyboardState.IsKeyDown(Keys.RightShift);
            bool isAltDepressed = keyboardState.IsKeyDown(Keys.LeftAlt) || keyboardState.IsKeyDown(Keys.RightAlt);
            bool isControlDepressed = keyboardState.IsKeyDown(Keys.LeftControl) || keyboardState.IsKeyDown(Keys.RightControl);
            foreach (var pressedKey in keyboardState.GetPressedKeys())
            {
                if (this.mOldKeyboardState.IsKeyUp(pressedKey))
                {
                    var keyDownArguments = new KeyControlEventArgs(this);
                    keyDownArguments.IsShiftDepressed = isShiftDepressed;
                    keyDownArguments.IsAltDepressed = isAltDepressed;
                    keyDownArguments.IsControlDepressed = isControlDepressed;

                    keyDownArguments.SourceKey = pressedKey;
                    base.OnKeyDown(keyDownArguments);
                }
            }

            //Key Up
            foreach (var pressedKey in this.mOldKeyboardState.GetPressedKeys())
            {
                if (keyboardState.IsKeyUp(pressedKey))
                {
                    var keyUpArguments = new KeyControlEventArgs(this);
                    keyUpArguments.SourceKey = pressedKey;
                    base.OnKeyUp(keyUpArguments);
                }
            }

            //change old state so events don't repeat.
            mOldMouseState = mouseState;
            mOldKeyboardState = keyboardState;
        }