Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: getLocalBounds() with text objects?  (Read 984 times)

0 Members and 1 Guest are viewing this topic.

Kelekom

  • Newbie
  • *
  • Posts: 4
    • View Profile
getLocalBounds() with text objects?
« 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.

FRex

  • Hero Member
  • *****
  • Posts: 1843
    • View Profile
    • My GitHub Page
    • Email
Re: getLocalBounds() with text objects?
« Reply #1 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.
« Last Edit: April 30, 2019, 05:31:25 pm by FRex »
ZipSavings, script to count rar/7z/zip savings: https://goo.gl/vvBj5M
LuaConsole: https://goo.gl/X4kRUk
FoxRaycaster: https://goo.gl/27nVS8
Small Games - Heart, Routing and Snek: https://goo.gl/15ZGWE https://goo.gl/k5gwWD https://goo.gl/4nKPnT
Botes - a notes app in Pascal: https://goo.gl/bzTqsi

Kelekom

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: getLocalBounds() with text objects?
« Reply #2 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

Hapax

  • Hero Member
  • *****
  • Posts: 3040
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: getLocalBounds() with text objects?
« Reply #3 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.
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

Kelekom

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: getLocalBounds() with text objects?
« Reply #4 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3040
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: getLocalBounds() with text objects?
« Reply #5 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.
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

Kelekom

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: getLocalBounds() with text objects?
« Reply #6 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})

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: getLocalBounds() with text objects?
« Reply #7 on: May 02, 2019, 04:52:17 pm »
The window comes with a default view.

Hapax

  • Hero Member
  • *****
  • Posts: 3040
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: getLocalBounds() with text objects?
« Reply #8 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.
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter