SFML community forums

Help => Graphics => Topic started by: djarkan on June 10, 2023, 05:02:51 pm

Title: my getGlobalBounds()
Post by: djarkan on June 10, 2023, 05:02:51 pm
hi

i made a bitmap and sf::text button 
class Button : public sf::Drawable, public sf::Transformable

with position and size being

sf::FloatRect                   m_shape;

initialized like this and origin is set in the constructor to be le middle

mylib::Button button(sf::FloatRect(320, 200 ,150, 100), texture, sf::Vector2f(230, 1600));
    button.rotate(90);
    button.setButtonType(mylib::Button::ButtonType::release);
    button.setScale(sf::Vector2f(2,2));
    button.setFont("OneSlot.ttf");
    button.setTextSize(30);
    button.setTextColor(sf::Color::Red);
    button.setText("the button");
 

my pb is i don't have a getGlobalBounds() available

so i tried this

sf::FloatRect Button::getGlobalBounds() const
{
    return getTransform().transformRect(m_shape);
}

width and height are good but left = -180 and top = 690

how do i have the good values ??

thx
Title: Re: my getGlobalBounds()
Post by: Hapax on June 11, 2023, 04:37:26 pm
It's worth noting that you are transforming a rectangle that is not at the origin (it's at the position of m_shape, I believe). Then, you rotate around the origin so it will not rotate around the rectangle's corner as you may expect.

If you always place your rectangle at (0, 0) position (this is called local co-ordinates and would be the rectangle what getLocalBounds would return), you can then translate (move) it into position using the transformable's position instead. getTransform uses the transformable's transform so you can use its position there.


Hope this helps.