private void CheckButtonReleased(Func <MouseState, ButtonState> getButtonState, MouseButton button)
        {
            if (getButtonState(_currentState) == ButtonState.Released &&
                getButtonState(_previousState) == ButtonState.Pressed)
            {
                var args = new MouseEventArgs(_gameTime.TotalGameTime, _previousState, _currentState, button);

                if (_mouseDownArgs.Button == args.Button)
                {
                    var clickMovement = DistanceBetween(args.Position, _mouseDownArgs.Position);

                    // If the mouse hasn't moved much between mouse down and mouse up
                    if (clickMovement < DragThreshold)
                    {
                        if (!_hasDoubleClicked)
                        {
                            MouseClicked.Raise(this, args);
                        }
                    }
                    else // If the mouse has moved between mouse down and mouse up
                    {
                        MouseDragged.Raise(this, args);
                    }
                }

                MouseUp.Raise(this, args);

                _hasDoubleClicked  = false;
                _previousClickArgs = args;
            }
        }
Exemplo n.º 2
0
Arquivo: Input.cs Projeto: ndech/Alpha
        public void Update(double delta)
        {
            // Truncate mouse position to screen dimensions
            Point position = _form.PointToClient(Cursor.Position);

            _mousePosition = new Vector2I(position.X, position.Y);
            MouseMoved.Raise(_mousePosition);

            MouseButtons buttons = Control.MouseButtons;
            bool         leftMouseButtonPressed = (buttons == MouseButtons.Left);

            if (leftMouseButtonPressed && !_previousMouseButtons[0])
            {
                MouseClicked.Raise(_mousePosition, 0);
            }
            else if (_previousMouseButtons[0] && !leftMouseButtonPressed)
            {
                MouseReleased.Raise(_mousePosition, 0);
            }
            _previousMouseButtons[0] = leftMouseButtonPressed;

            bool rightMouseButtonPressed = (buttons == MouseButtons.Right);

            if (rightMouseButtonPressed && !_previousMouseButtons[1])
            {
                MouseClicked.Raise(_mousePosition, 1);
            }
            else if (_previousMouseButtons[1] && !rightMouseButtonPressed)
            {
                MouseReleased.Raise(_mousePosition, 1);
            }
            _previousMouseButtons[1] = rightMouseButtonPressed;


            foreach (Key key in _keys.Cast <Key>().Where(key => key != Key.None))
            {
                if (Keyboard.IsKeyDown(key))
                {
                    if (_pressedKeys.ContainsKey(key))
                    {
                        _pressedKeys[key] += delta;
                        if (_pressedKeys[key] > 0.1)
                        {
                            _pressedKeys[key] = 0.0;
                            KeyPressed.Raise(key, InputInterop.GetCharFromKey(key), true);
                        }
                    }
                    else
                    {
                        _pressedKeys.Add(key, -0.9);
                        KeyPressed.Raise(key, InputInterop.GetCharFromKey(key), false);
                    }
                }
                else
                {
                    if (_pressedKeys.ContainsKey(key))
                    {
                        _pressedKeys.Remove(key);
                        KeyReleased.Raise(key);
                    }
                }
            }
        }