Sign in to follow this  

XNA Noob question Mouse input and buttons

This topic is 3289 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to learn XNA and working on building a set of menus for a game at the same time. I'm trying to get to where I can click on a button and have it load another menu/do something/etc. Just simple click on button and do something stuff. Can someone point me in the direction of some good resources for this?

Share this post


Link to post
Share on other sites
Quote:
Original post by murdock
I am trying to learn XNA and working on building a set of menus for a game at the same time.

I'm trying to get to where I can click on a button and have it load another menu/do something/etc.

Just simple click on button and do something stuff.
Can someone point me in the direction of some good resources for this?


Okay, so you have a menu structure of some type, that has all the data about where the buttons are, and you are using that to draw the buttons I'm assuming. So you know where the x and y locations of the buttons are, and how tall and wide they are, giving you information about how much space they take up.

You have a mouse pointer, and you know where it is on screen.

When the user clicks the left mouse button, which you check for in your update function, you scan through the menu structure, and each button checks if the mouse pointer is within it's space:

Simple Pseudo Code:
if (Mouse.X > Button[i].X && Mouse.X < (Button[i].X + Button[i].Width) &&
Mouse.Y > Button[i].Y && Mouse.Y < (Button[i].Y + Button[i].Height) DoSomethingAndReturn();

What I usually do is have the menu structure itself simply return a value for what button was clicked. 0 is none, and anything else is a button, and I do a simple switch block to react to the values.

ClickID = MenuSystem.GetClickId();
if (ClickID > 0)
{
switch(ClickID)
{
..
}
}


I suggest you work out a class called MenuSystem that can hold a 'List' of MenuButton class objects, and has at least a function to scan through the list and return the ID of the button that was clicked.

The MenuButton class should have an X and Y position, Width, Height, and a function to Test if the mouse cursor is inside the area it takes up. Also, give it an ID value. You can give non clickable buttons a value of 0. Meaning the MenuSystem class will just act like nothing was clicked anyways.

You can further specialize your buttons later on, by subclassing the MenuButton class, and giving each type of button it's own behaviour. You can have static buttons that return false right away on the click check, but update some displayable information in their update() function.

Share this post


Link to post
Share on other sites
I found it was pretty easy to make some nice and functional buttons in XNA. I made a simple button class with the following:

- Button(): has parameters posX and posY (the center of the button on screen), scale (on hover the button gets slightly larger), LeftClickSound, RightClickSound, HoverSound, Delegate LeftClickAction, Delegate RightClickAction (delegates that fire when you click the button), and finally, a Texture.

- Make some custom constructors such as Button(int x, int y, Delegate onLeftClick, Delegate onRightClick), which would give you a button at position X,Y that fires different functions depending on left or right click.

You also need the following functions:

- MouseIsOver(): check if mouse X,Y is within the button texture

- IsRightClicked(): mouse is over and right button down

- IsLeftClicked(): mouse is over and left button down

- BeginHover(): this is true the first frame the mouse hovers over the button, to play the HoverSound

- Update(): checks for the above items every frame

- Draw(): render the button by position and texture. I use a simple Sprite Batch.

I can post my functions if you like. Its mostly just mouse position comparison cases.

Share this post


Link to post
Share on other sites
Quote:
Original post by FippyDarkpaw
I found it was pretty easy to make some nice and functional buttons in XNA. I made a simple button class with the following:

- Button(): has parameters posX and posY (the center of the button on screen), scale (on hover the button gets slightly larger), LeftClickSound, RightClickSound, HoverSound, Delegate LeftClickAction, Delegate RightClickAction (delegates that fire when you click the button), and finally, a Texture.

- Make some custom constructors such as Button(int x, int y, Delegate onLeftClick, Delegate onRightClick), which would give you a button at position X,Y that fires different functions depending on left or right click.

You also need the following functions:

- MouseIsOver(): check if mouse X,Y is within the button texture

- IsRightClicked(): mouse is over and right button down

- IsLeftClicked(): mouse is over and left button down

- BeginHover(): this is true the first frame the mouse hovers over the button, to play the HoverSound

- Update(): checks for the above items every frame

- Draw(): render the button by position and texture. I use a simple Sprite Batch.

I can post my functions if you like. Its mostly just mouse position comparison cases.


When doing an XBox menu however, we need the 'menusystem' class to have functions to allow toggling through the buttons directionally, and the concept of an 'active button'.

Share this post


Link to post
Share on other sites

This topic is 3289 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this