The Mouse in XNA

Before the player can use the mouse in a game, he has to be able to see it. By default, the mouse pointer is invisible in a Windows XNA game. To make the mouse visible, set the global IsMouseVisible variable to true:

IsMouseVisible = true;

To hide the mouse:

IsMouseVisible = false;

Just as keyboard events are captured using a KeyboardState object, mouse events are captured using a MouseState object. To capture the mouse state, declare a MouseState variable:

MouseStatemouse;

and use the built-in Mouse object to retrieve its current state:

mouse=Mouse.GetState();

Notethatourvariableis"mouse"withalower-case"m", andweareusing"Mouse"withanupper-case"M"toretrievethemousestate."Mouse"withanupper-case"M"isabuilt-inobjectinXNA.TheMouseclasshasastaticmethodcalledGetStatewhichreturnsaMouseStatestructure.Thisstructurewilltellyouifamousebuttonispressedorreleasedandthe(x,y)locationofthemousepointer.

ThefollowingtableliststheimportantpropertiesoftheMouseStatestruct:

Property / Type / Description
LeftButton / ButtonState / Returnsthestateoftheleftmousebutton. Either ButtonState.Pressed or ButtonState.Released.
MiddleButton / ButtonState / Returnsthestateofthemiddlemousebutton.
RightButton / ButtonState / Returnsthestateoftherightmousebutton.
ScrollWheelValue / int / Returnsthetotalaccumulatedmovementofthescrollwheelsincethegamestarted.
X / int / Returnsthehorizontalpositionofthemouseinrelationtotheupper-leftcornerofthewindow.
Y / int / Returnstheverticalpositionofthemouseinrelationtotheupper-leftcornerofthewindow.

To access these values, first,createavariabletoholdthemousestate:

MouseStatemouse;

Read the mouse:

mouse=Mouse.GetState()

Now you can refer to the fields of the mouse like this:

mouse.LeftButton

mouse.RightButton

mouse.MiddleButton

mouse.ScrollWheelValue

mouse.X

mouse.Y

Determining whether a mouse button is pressed

The LeftButton and RightButton properties are of type ButtonState. The ButtonState type is an enumerated type that has only two values: Pressed and Released. To determine if the left mouse button is pressed:

if (mouse.Leftbutton == ButtonState.Pressed)

// Stuff to do if the left button is pressed.

Determining if the mouse is inside of a rectangle

The Rectangle class has a Contains method that accepts a point on the screen. The point may be in several different formats, including a pair of integer arguments. So, to see if a point is inside of a rectangle:

if (myRectangle.Contains (X, Y))

// Stuff to do if the location (X, Y) is in the rectangle

So, to see if a mouse is inside of a given myRectangle:

if (myRectangle.Contains(mouse.X, mouse.Y))

// Stuff to do if the mouse is in the rectangle

The above example assumes that the variable mouse has been declared as in the prior examples and that the variable myRectangle is a Rectangle.

Converting X and Y into a row and a column

In our Tic-Tac-Toe program, we need to determine which square the mouse is over on the screen.

What we need to do is take a set of absolute coordinates on the screen (the location of the mouse) and convert them into a row and a column in a grid that is drawn on the screen. We will solve the problem in a single dimension first.

Assumptions

The mouse's X input can be anything from 0 to 799. But let's give them names:

Rectangle screen = new Rectangle(0, 0, 800, 480);

Now the mouse's input can be anything from screen.Left to screen.Right.

Let's assume that we have a grid of vertical lines on the screen. The vertical lines all fall within the following rectangle:

Rectangle grid = new Rectangle(100, 0, 400, 480);

And let's further assume that the grid rectangle demarks a texture that has vertical lines at regular intervals: 100, 150, 200, 250, 300, 350, 400, 450, 500.

We will call the area between X=100 and X=149 column 0. In all we will have:

X range
/
Column Number
100-149 / 0
150-199 / 1
200-249 / 2
250-299 / 3
300-349 / 4
350-399 / 5
400-449 / 6
450-499 / 7

We need to figure out a way to convert an X value on the screen into its corresponding number. Note two things: (1) the X range starts at 100, and (2) it increases in increments of 50. We need to look for a formula that will convert an X value into the corresponding column number. Since the X range starts at 100, we need to subtract 100 from each X value (so we can have a starting value of 0). If we do this, we get the following table:

X range
/
Column Number
0-49 / 0
50-99 / 1
100-149 / 2
150-199 / 3
200-249 / 4
250-299 / 5
300-349 / 6
350-399 / 7

Now, every 50 pixels, the column number increases by 1. This tells us that we need to divide the X-range number by 50. If we do so (using integer division), we get the following table:

X range
/
Column Number
0-0 / 0
1-1 / 1
2-2 / 2
3-3 / 3
4-4 / 4
5-5 / 5
6-6 / 6
7-7 / 7

We've done it! We have converted an X value into a column number.

Let's generalize it using variables rather than numbers.

The grid:

Rectangle grid;

The width of each column on the grid:

intcolWidth;

The position of the mouse:

mouse.X

mouse.Y

The variable to hold the column number:

int col;

The formula to compute the column number:

col = (mouse.X – grid.Left) / colWidth;

That's it!

We can do the same thing if the lines are horizontal and we want to convert a Y value into a corresponding row number.

Y:\CSCI201-XNA\Notes\21-The Mouse in XNA.docx1