Time for action – handling mouse input

  1. Add the HandleMouseInput() helper method to the Game1 class:
    private void HandleMouseInput(MouseState mouseState)
    {
    
        int x = ((mouseState.X -
            (int)gameBoardDisplayOrigin.X) / GamePiece.PieceWidth);
    
        int y = ((mouseState.Y -
            (int)gameBoardDisplayOrigin.Y) / GamePiece.PieceHeight);
    
        if ((x >= 0) && (x < GameBoard.GameBoardWidth) &&
          (y >= 0) && (y < GameBoard.GameBoardHeight))
        {
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                gameBoard.RotatePiece(x, y, false);
                timeSinceLastInput = 0.0f;
            }
    
            if (mouseState.RightButton == ButtonState.Pressed)
            {
                gameBoard.RotatePiece(x, y, true);
                timeSinceLastInput = 0.0f;
            }
        }
    }

What just happened?

The MouseState class reports the X and Y position of the mouse relative to the upper ...

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.