This project is read-only.

Detecting "Button" pressed

Apr 6, 2009 at 4:30 PM
Edited Apr 6, 2009 at 4:31 PM
Hi I'm new to both XNA and XEN and have what I hope is a simple question...

I have a SolidColourElement I'm using to represent a "button" - I want to detect left button mouse clicks on this element and then act on them.

I've created a Button class, which contains a SolidColourElement (representing the button) and implements IUpdate (to pick-up UpdateState.MouseState.LeftButton.OnPressed).  From this I can establish the Mouse X and Y at the point the click took place, but this is relative to the entire application rather than the Button instance.  I'm having trouble working out how to transform the position of the Mouse into local space relative to the Button so I can bounds test it - any pointers?

I've created a very simple representation of my code (with a TODO highlighted) to illustrate what I'm trying to achieve:

Any help would be great - thanks in advance.
Apr 7, 2009 at 1:41 AM
Hi. I'll be putting out an update soonish which will have 'TryGetLayout' in the Element class, which will be what you are after. Sorry it's not in there at the moment.
Apr 7, 2009 at 8:37 AM
That sounds great - is there a direction you could suggest I look into in the mean time to work something out for myself?

Apr 15, 2009 at 6:50 AM
Sorry I didn't get back to you, or get the update out. I was moving out of town all last week, so you caught me at a bad time.
The update it out now, including TryGetLayout, which hopefully does the trick.
Apr 15, 2009 at 9:39 AM
No problem - I found a work around that worked for the time being (essentially using a bounding box to store a "buttons" position - bit hacky, but I'm only doing a Proof of Concept at the moment, so it's fine).

Hope the move went well!