SFML community forums

Help => Window => Topic started by: Kelekom on April 30, 2019, 04:46:58 pm

Title: getLocalBounds() with text objects?
Post by: Kelekom on April 30, 2019, 04:46:58 pm
while(window.isOpen()){
        Event event;

            while(window.pollEvent(event)){
            window.clear(Color(210, 180, 140));
            window.draw(titleText);
            window.draw(equationsButtonText);
            window.draw(variablesButtonText);
            window.draw(onlineResourcesButtonText);
            window.draw(unitsButtonText);
            window.display();
                switch(event.type)
                {

                    case Event::Closed:
                        window.close();
                        break;

                    case Event::KeyPressed:
                        if(event.type == Event::MouseButtonPressed){
                            if(event.mouseButton.button == Mouse::Left){
                                if(equationsButtonText.getLocalBounds().contains(event.mouseButton.x, event.mouseButton.y)){
                                    cout << "This works!" << endl;
                                }
                            }
                        }

                    default:
                    break;

                    }
                }
            }

Not sure what part of this is wrong but clicking on the text doesn't work and im not entirely sure why. I'm thinking getLocalBounds doesn't work for text objects but really don't want to draw invisible sprites over each button unless theres a faster way to do that.
Title: Re: getLocalBounds() with text objects?
Post by: FRex on April 30, 2019, 05:29:13 pm
MouseButtonPressed is the event type you want, KeyPressed is for keyboard keys. Also, you should not have clearing, drawing and window display inside the event loop.
Title: Re: getLocalBounds() with text objects?
Post by: Kelekom on April 30, 2019, 05:41:23 pm
while(window.isOpen()){
        Event event;

            while(window.pollEvent(event)){

                switch(event.type)
                {

                    case Event::Closed:
                        window.close();
                        break;

                    case Event::MouseButtonPressed:
                        if(event.mouseButton.button == Mouse::Left){
                            cout << "  if(event.mouseButton.button == Mouse::Left){" << endl;
                            if(equationsButtonText.getLocalBounds().contains(event.mouseButton.x, event.mouseButton.y)){
                                cout << "This works!" << endl;
                                }
                            }

                    default:
                    break;
                    }
                }
            }

Okay yeah I just fixed the KeyPressed before I checked the post, it gets to here:
> cout << "  if(event.mouseButton.button == Mouse::Left){" << endl; (Testing to see how far it goes)
and im assuming the if statement just isnt valid at that point
Title: Re: getLocalBounds() with text objects?
Post by: Hapax on May 01, 2019, 12:23:02 am
Are you using a view? If so, you should probably convert the co-ordinates. Can be best to do so anyway, just to be safe:
https://www.sfml-dev.org/tutorials/2.5/graphics-view.php#coordinates-conversions

The local bounding rectangle will be an sf::FloatRect and the co-ordinates you are providing to "contains" are ints. I'm not 100% sure but I don't think these can be implicit. Try converting to floats or passing them to an sf::Vector2f.
Title: Re: getLocalBounds() with text objects?
Post by: Kelekom on May 01, 2019, 01:08:26 am
Forgot to send myself the .cpp file from school but no I'm not using a view. So ill check that tomorrow
And when I used getGlobalBounds I believe it gave me a FloatRect error and when i tried
cout << equationsButtonText.getLocalBounds(); to see what it was puting in for the coords it gave me a FloatRect error.
Title: Re: getLocalBounds() with text objects?
Post by: Hapax on May 01, 2019, 02:01:57 pm
You can't output a float rect to a steam. You can output their components individually, however e.g. top, left, width, height.

However, since the code you provided isn't working (does it fail to compile?), instead of:
if(equationsButtonText.getLocalBounds().contains(event.mouseButton.x, event.mouseButton.y))
try
if(equationsButtonText.getLocalBounds().contains(sf::Vector2f(event.mouseButton.x, event.mouseButton.y)))

It would be helpful if you provided the errors that you are getting.
Title: Re: getLocalBounds() with text objects?
Post by: Kelekom on May 02, 2019, 04:39:04 pm
mapPixelToCoords worked even though im not using a view. I don't know why it works but it does. Thanks!

if(equationsButtonText.getGlobalBounds().contains(window.mapPixelToCoords({event.mouseButton.x, event.mouseButton.y})
Title: Re: getLocalBounds() with text objects?
Post by: AlejandroCoria on May 02, 2019, 04:52:17 pm
The window comes with a default view.
Title: Re: getLocalBounds() with text objects?
Post by: Hapax on May 02, 2019, 08:12:36 pm
Just to clarify slightly, the window's default view matches the dimensions of the window in pixels. The view no longer aligns with the pixels if the window is resized, which I presume you must have done.

If you want your view to continue matching the window's size, you should use the part above the co-ordinates conversion (https://www.sfml-dev.org/tutorials/2.5/graphics-view.php#showing-more-when-the-window-is-resized).