SFML community forums

Help => Graphics => Topic started by: ScArL3T on August 09, 2015, 03:55:42 pm

Title: getLocalBounds and getGlobalBounds
Post by: ScArL3T on August 09, 2015, 03:55:42 pm
So I'm trying to make a button but I can't resize and center the text because both getLocalBounds and getGlobalBounds are returning 0. Here is the code:

       
void Button::setString(sf::String string)
        {
                m_string = string;
                m_text.setString(m_string);
                std::cout << m_text.getGlobalBounds().width; //This returns  0
    }
 

m_text is a private sf::Text variable.
The text is drawn on the screen, the setChar size method works as well. The only thing that isn't right here is that getGlobalBounds().width/height and getLocalBounds().width/height always return 0.
Title: Re: getLocalBounds and getGlobalBounds
Post by: FRex on August 09, 2015, 04:15:31 pm
Are you sure font is set and string is not empty?
Title: Re: getLocalBounds and getGlobalBounds
Post by: ScArL3T on August 09, 2015, 04:19:18 pm
Here is an image with the result:
(http://i.imgur.com/gvEl7lp.png)
Title: Re: getLocalBounds and getGlobalBounds
Post by: Hapax on August 09, 2015, 07:42:05 pm
Are you checking the bounds' width before setting the font?
(hint: you are)
Title: Re: getLocalBounds and getGlobalBounds
Post by: ScArL3T on August 09, 2015, 07:44:07 pm
I think I found the problem. If I load the font inside the button constructor like this it works:
m_font.loadFromFile("data/font3.ttf");
m_text.setFont(m_font);

But I can't find a good solution to use the gui.setGlobalFont(font) properly. If you have any ideas please let me know. Thank you!
Title: Re: getLocalBounds and getGlobalBounds
Post by: Hapax on August 09, 2015, 07:46:57 pm
As I wrote in my previous post, you are checking the bounds before setting the font. You can access the bounds of the text after it has a font otherwise how does it know how large it is?
Try accessing its bounds after adding the "button" to "gui".
Title: Re: getLocalBounds and getGlobalBounds
Post by: ScArL3T on August 09, 2015, 08:11:32 pm
Ok I fixed it. Thank you!   ;D