Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

My game has a background that I can move with the mouse. I want to add some semi-transparent buttons on top of background as DrawableGameComponent. But when I click the Button mouse click will propagate to underlying background and it will be moved. How to detect that click was handled and not to call Update methods of underlying game components?

Sorry for my bad english.

share|improve this question

1 Answer 1

As always, there are different ways to do this. One way would be to first check clicks on the buttons, return a value indicating whether the button was hit or not, and then only handle the click for the background if no button was hit.

I have used a similar method before (code simplified and from memory - not guaranteeing correct syntax, but the principle should be clear):

bool ClickHit(mouse.X, mouse.Y) //inside the button
{
    if (this.ScreenRectangle.Intersects(mouse.X, mouse.Y)
    {
        //do button stuff here or in another place, depending on where it's appropriate
        return true;
    }
    return false;
}

///////////////////////////////////////////////////
bool ButtonHit(mouse.X, mouse.Y) //inside the menu
{
    foreach (Button b in this.ButtonList)
    {
        if (b.ClickHit(mouse.X, mouse.Y))
        {
            //this might be a place to do button stuff as well
            return true;
        }
    }
    return false;
}

//////////////////////////////////////////
if (!Menu.ButtonHit(mouse.X, mouse.Y)) //where you do your other stuff
{
    //move your background and do other stuff that you only want to do if no Button was clicked
}

I'm sure there are more elegant solutions, but this one should be relatively simple to include into an existing codebase.

share|improve this answer
    
A more generic solution would be to encapsulate this into a class, abstract class, or interface, and simply iterate over all Clickable components, stopping at the first that handles the click. –  ashes999 Jan 16 '13 at 11:55
    
Thank you for your answer. But what should I do if all of my components are DrawableGameComponent or GameComponent and all clicks handles in their Update method? All GameComponents are dispatched by Game object which call Update method on every object and don't stop until call them all (of course, if Enabled flag is true). –  user25202 Jan 16 '13 at 12:15
    
@user25202 Maybe you could set a global bool clickHandled to falseat the beginning of the frame, to true when you do something with a click, and only handle your clicks if this is still false? That's the only way I can think of off the top of my mind other than restructuring your code to implement either my solution or @ashes999's cleaner one. –  Christian Jan 16 '13 at 15:35
    
I stopped on a variant with the creation of a static class with a flag and a reference to the object that currently has focus. Anyway thank you for idea. –  user25202 Feb 3 '13 at 8:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.