1

I have SFML.NET working fine, and wrote my own little scene manager. In the scene interface I have a method called "Update". I'm trying to see if the user has clicked on the play button or quit button, but I get nothing.

    public void Update(RenderWindow window)
    {
        if (Mouse.IsButtonPressed(Mouse.Button.Left))
        {
            Program.WriteDebug("Mouse was clicked.");
            Vector2i mousePosition = Mouse.GetPosition(window);
            if (play.TextureRect.Contains(mousePosition.X, mousePosition.Y))
            {
                // Not displaying text.
                Program.WriteDebug("Play button pressed.");
            }
            else if (quit.TextureRect.Contains(mousePosition.X, mousePosition.Y))
            {
                // Not displaying text.
                Program.WriteDebug("Quit button pressed");
            }
        }
    }

EDIT: I have the project set to Console Application, so I can see the console and the application.

melotic
  • 51
  • 8
  • I found out that when I change the location of the buttons, the texture rectangle wasn't updating. When I tried to fix it, the button disappears. – melotic Jun 01 '14 at 18:03

1 Answers1

0

You are using the texturerect as the global clicking area:

if (play.TextureRect.Contains(mousePosition.X, mousePosition.Y))
{
    // Not displaying text, because the texture rect can be something like:
    // width: 100 height: 30 top: 0 left: 0
    // or you don't use it at all and it's all zero (impossible to click)
    Program.WriteDebug("Play button pressed.");
}

This is because the texturerect are local coordinates from the texture which are say which part of the texture is displaying. You need to use the global bounds of the button:

if (play.getGlobalBounds.Contains(mousePosition.X, mousePosition.Y))
{
    // should displaying text.
    Program.WriteDebug("Play button pressed.");
}

The global bounds are the size and position of the entity including all transformations like rotation and scaling. That's the right one for you.

Bruno Zell
  • 7,761
  • 5
  • 38
  • 46