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

Author Topic: my getGlobalBounds()  (Read 548 times)

0 Members and 1 Guest are viewing this topic.

djarkan

  • Newbie
  • *
  • Posts: 14
    • View Profile
my getGlobalBounds()
« 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
« Last Edit: June 11, 2023, 02:52:03 pm by djarkan »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: my getGlobalBounds()
« Reply #1 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.

  • Place rectangle at (0, 0).
  • Keep given rectangle size (150x100 here).
  • Set the Button's position to where you want the rectangle to start (using setPosition, which is part of sf::Transformable).
  • Rotate the button using setRotate (or just rotate) as you have done (this is also a part of sf::Transformable).
  • Get global bounds by transforming local bounds by the transform: this is the rectangle that is at (0, 0) and then transformed by Button's transform (as you have done already). This uses the sf::Transform position to put it in its position.

Hope this helps.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*