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

Author Topic: Get true bounds of a sprite in transformed container  (Read 1236 times)

0 Members and 1 Guest are viewing this topic.

aleksailic

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Get true bounds of a sprite in transformed container
« on: January 18, 2020, 02:15:50 am »
I have created 2 wrapper classes for easier UI managments. That are given below
struct Button: public sf::Drawable, public sf::Transformable{
  sf::Sprite sprite;
  void handle(const sf::Event& event){
     if(event.type == sf::Event::EventType::mouseButtonReleased)
        if(sprite.getGlobalBounds().contains({ event.mouseButton.x, event.mouseButton.y })
          std::cout << "Clicked!";
  }

  void draw(sf::RenderTarget& target, sf::RenderStates states) const override {
    states.transform *= getTransform();
     target.draw(sprite,states);
  }
}
struct ButtonGroup: public sf::Drawable, public sf::Transformable{
  std::list<Button> buttons;

   void handle(const sf::Event& event){
      for(auto& button : buttons)
           button.handle(event);
   }

  void draw(sf::RenderTarget& target, sf::RenderStates states) const override {
    states.transform *= getTransform();
    for (auto& button : buttons)
       button.draw(target,states);
  }
}
 

Basic idea is that I can position buttons relative to buttongroup, and then be free to move buttongroup as I wish. This idea is properly executed when rendering is considered, renderstates takes care of transform propagation, however bounding box checking in the event does not work as intenteded as button event handler is unaware of transformations given to buttongroup. What is the concensus on this issue? Should I somehow put and propagate renderState into handling function and then somehow apply that transform to globalBounds()' rect ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Get true bounds of a sprite in transformed container
« Reply #1 on: January 18, 2020, 09:28:17 am »
Quote
What is the concensus on this issue?
A getWorldTransform() function that gets the global transform of a node by recursively combining parent transforms until you've reached the root.
Laurent Gomila - SFML developer