SFML community forums

Help => Graphics => Topic started by: pkthunderr on June 30, 2014, 06:32:08 pm

Title: sf::Text.getGlobalBounds.width returns 0 incorrectly
Post by: pkthunderr on June 30, 2014, 06:32:08 pm
I am having some trouble with getting a bounding box around a sf::Text object. I am trying to make a Button Class that holds the coordinates of buttons. I made a vector of this button class and when a Mouse Click Event is triggered, each button in the vector is check against the coordinates of the mouse to see if that specific button was clicked.

class ButtonTracker
{
private:
        sf::Shape* obj;
        float xMin;
        float xMax;
        float yMin;
        float yMax;

public:
        ButtonTracker();
        ButtonTracker(sf::Shape* obj);
        ButtonTracker* wasClicked(const sf::Event* click);
        int clickHandler();
};

ButtonTracker::ButtonTracker(sf::Shape* obj)
{
        this->obj = obj;
        sf::Vector2f pos = obj->getPosition();
    sf::FloatRect size = obj->getGlobalBounds();
        xMin = pos.x;
        xMax = pos.x + obj->size.width;
        yMin = pos.y;
        yMax = pos.y + obj->size.height;
}

ButtonTracker* ButtonTracker::wasClicked(const sf::Event* click)
{
        cout << "Mouse Coords: " << click->mouseButton.x << ", " << click->mouseButton.y <<
                "\nButton Coords: " << xMin << "-" << xMax << ", " << yMin << "-" << yMax << endl;

        if (click->mouseButton.x >= xMin && click->mouseButton.x <= xMax &&
                click->mouseButton.y >= yMin && click->mouseButton.y <= yMax )
        {
                return &(*this);
        }

        return NULL; //no buttons were clicked
}
 

In my ButtonTracker constructor I called getGlobalBounds() on the sf::Shape*, which in my case is a sf::Text object. However getGlobalBounds().width always returns 0 and in some cases getGlobalBounds().height returns 0 as well. Which is incorrect because the sf::Text objects have been created already, so there width shouldn't be 0.

I printed some variables in the terminal, here's a sample output (the button coords are returned as a range):
Mouse Click Detected
Processing Left Mouse Click

Checking Button Number 0
Mouse Coords: 337, 352
Button Coords: 30-30, 529.25-529.25

Checking Button Number 1
Mouse Coords: 337, 352
Button Coords: 30-30, 571.25-601.25

Checking Button Number 2
Mouse Coords: 337, 352
Button Coords: 30-30, 613.25-643.25

Checking Button Number 3
Mouse Coords: 337, 352
Button Coords: 30-30, 655.25-685.25

Non-button click determined
 

As you can see the GlobalBounds returned are wrong, but sometimes the correct Y value is returned.

I also tried it with getLocalBounds() and the same error occured. I never change the origin or character size of the text.

Note: I added +30.0f to my xMax to test it and my code works. So I can't figure out why getGlobalBounds().width/height return the wrong values.

Am I doing something wrong?



Title: Re: sf::Text.getGlobalBounds.width returns 0 incorrectly
Post by: G. on June 30, 2014, 06:56:54 pm
In my ButtonTracker constructor I called getGlobalBounds() on the sf::Shape*, which in my case is a sf::Text object.
sf::Text doesn't inherit from sf::Shape.
Title: Re: sf::Text.getGlobalBounds.width returns 0 incorrectly
Post by: pkthunderr on June 30, 2014, 08:02:00 pm
Thank you!

I knew it was going to be something I overlooked. I just assumed getGlobalBounds() is inherited. Nevertheless, Thank you!