17

I have a 2D game in which I use only the mouse as input. How can I make it so that my when the mouse hovers over a Texture2D object, the Texture2D and the mouse cursor change, and when the texture is clicked it moves to another place.

Simply put, I want to know how to do something when I hover over or click on a Texture2D.

Beska
  • 12,445
  • 14
  • 77
  • 112
Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64

2 Answers2

39

In XNA you can use the Mouse class to query user input.

The easiest way of doing it is to check the mouse state for each frame and react accordingly. Is the mouse position inside a certain area? Display a different cursor. Is the right button pressed during this frame? Show a menu. etc.

var mouseState = Mouse.GetState();

Get the mouse position in screen coordinates (relative to the top left corner):

var mousePosition = new Point(mouseState.X, mouseState.Y);

Change a texture when the mouse is inside a certain area:

Rectangle area = someRectangle;

// Check if the mouse position is inside the rectangle
if (area.Contains(mousePosition))
{
    backgroundTexture = hoverTexture;
}
else
{
    backgroundTexture = defaultTexture;
}

Do something while the left mouse button is clicked:

if (mouseState.LeftButton == ButtonState.Pressed)
{
    // Do cool stuff here
}

Remember though that you will always have information of the current frame. So while something cool may happen during the time the button is clicked, it will stop as soon as released.

To check for a single click you would have to store the mouse state of the last frame and compare what has changed:

// The active state from the last frame is now old
lastMouseState = currentMouseState;

// Get the mouse state relevant for this frame
currentMouseState = Mouse.GetState();

// Recognize a single click of the left mouse button
if (lastMouseState.LeftButton == ButtonState.Released && currentMouseState.LeftButton == ButtonState.Pressed)
{
    // React to the click
    // ...
    clickOccurred = true;
}

You could make it even more advanced and work with events. So you would still use the snippets from above, but instead of directly including the code for the action you would fire events: MouseIn, MouseOver, MouseOut. ButtonPush, ButtonPressed, ButtonRelease, etc.

Lucius
  • 3,705
  • 2
  • 22
  • 41
  • 1
    Rectangle.Contains takes a point so I'm not sure why you converted the Point returned by Mouse.GetState() to a Vector. – ClassicThunder Mar 15 '12 at 15:32
  • but how can I associate the click with the texture in any position. when i left clicked it in any position not a specific rectangle! – Ahmed Hegazy Mar 16 '12 at 22:01
  • The Texture2D class has no built in functionality for recieving input and that is good. So you will always have to define an active area and associate that with a texture or other objects. If you got more rectangles simple loop through them and check each with the methods above. – Lucius Mar 17 '12 at 09:37
-1

I would just like to add that the Mouse Click code could be simplified so that you don't have to make a variable for it:

if (Mouse.GetState().LeftButton == ButtonState.Pressed)
    {
        //Write code here
    }
Vilhelm
  • 11