The mouse is one of the most accurate input devices supported by XNA Game Studio. It is used for a variety of tasks in games from selecting units and giving orders in a real-time strategy game to targeting and shooting weapons in first-person shooters and selecting moves in a chess game. Although the mouse APIs are available across all XNA Game Studio platforms, they don’t behave the same across all platforms. On Windows, the mouse APIs perform as you expect giving you the current state of the physical mouse plugged into your PC. On Xbox 360, there is never mouse input because the Xbox 360 does not support mouse input. In this case, the mouse state always contains default values with a position at 0, 0 and no buttons pressed. On Windows Phone, there is also no physical mouse device, but because all Windows Phone devices must support multitouch, the mouse state is returned as the location and state of the first finger detected on the multitouch screen.
Reading Mouse State
Reading the current mouse state is similar to reading keyboard state.The Mouse class provides a static GetState method that returns the current mouse state stored in a MouseState structure.Table 12.2 contains the properties exposed by the MouseState structure.
Table 12.2 Properties of MouseState
Property |
Type |
Description |
X |
int |
The horizontal position of the mouse |
Y |
int |
The vertical position of the mouse |
ScrollWheelValue |
int |
The scroll position of the mouse wheel |
LeftButton |
ButtonState |
State of the left mouse button |
RightButton |
ButtonState |
State of the right mouse button |
MiddleButton |
ButtonState |
State of the middle mouse button |
XButton1 |
ButtonState |
State of extra mouse button 1 |
XButton2 |
ButtonState |
State of extra mouse button 2 |
To read the current mouse state, add the following line of code to your game’s Update method:
Note
The GetState method supports reading only one mouse device, unlike the Keyboard.GetState method that we discussed previously.
Now that you have stored the current state of the mouse, you should also store the previous frame’s mouse state in the same manner as you did for the keyboard.You can use this previous state to determine whether a button is clicked in a specific frame or held down over multiple frames. Declare the following member variable in your game:
At the end of your game’s Update method, add the following line of code to store the previous frame’s mouse state:
The MouseState structure contains both analog data for the mouse and scroll wheel position and digital data for the mouse buttons.To retrieve the current position of the mouse, use the MouseState.X and MouseState.Y properties.These properties return the current position of the mouse on the screen. On Windows, if you are in windowed mode, the mouse can travel outside of the client bounds of the window. In this case, the mouse position is relative to the origin of the window’s client area in the top left corner. If your mouse moves outside the client area, you receive negative values or values outside of the size of the client area depending on the direction you move outside of the client bounds. To store the current mouse position, use the following lines of code:
Along with the mouse position, you can also read the current mouse wheel using the MouseState.ScrollWheelValue property.This is an integer value of the current mouse wheel position.This position is accumulated while your game runs. If you want to determine whether the wheel is scrolled on this frame, you need to check whether the value in the current frame is greater or less than the previous frame. Use the following code to determine whether the mouse scroll wheel is scrolled forwards or backwards in the current frame:
The MouseState structure contains five button properties to determine whether up to five different buttons on the mouse are pressed. Not all mice have five buttons. Similar to the keyboard’s KeyState, the ButtonState returned by these properties has two values. In the case of the button, these values are Pressed and Released.To determine whether the left mouse button is pressed, use the following lines of code:
Similar to the keyboard, if you want to determine whether a button is clicked in the current frame and has not been held down, you need to also check the previous frame’s mouse state and verify that the button is not pressed.
Note
By default on Windows, the mouse is hidden when it is over the client area of the window. To view the mouse position while it is over the client area of the window, set the IsMouseVisible property of your game class to true.
Moving Sprite Based on Mouse Input
Now that you know how to retrieve the current state of the mouse device, let’s use this state to move, rotate, and flip a sprite on the screen. First, you need to declare the following member variables to store the sprite texture, position, rotation, and if the sprite is flipped horizontally or vertically on the screen:
Next, load a texture to use as your sprite and set the sprite’s center based on the texture width and height. In the game’s LoadContent method, add the following lines of code:
Now, update the current position, rotation, and flip values based on the current input of the mouse. In your game’s Update method, add the following lines of code:
Now that you updated the sprite’s position, rotation, and flip values, you can draw the sprite on the screen. In your game’s Draw method, add the following lines of code:
The sprite now draws on the screen and updates its position, rotation, and flip, responding to mouse input (see Figure 12.3).
Figure 12.3 Using mouse input to move and rotate sprite
Setting the Mouse Position
Sometimes it is necessary to set the current position of the mouse on the screen.To set the position of the mouse, use the static Mouse.SetPosition method.This method takes the screen space integer X and Y values.To set the mouse to a location with the values of X: 400Y: 240, use the following line of code:
Setting the Mouse Window Handle
On Windows, the mouse state depends on the window it is attached to. By default, the mouse is set to be the game window that is created by your Game class. You might have to set the window manually if you are creating tools that use multiple windows. In this case, set the Mouse.WindowHandle property to the window you want the mouse attached to.