I am having confusion when using sf::Transformable.
I have a class called Button that inherits sf::Transformable. My motivation to do this is I want to be able to set the position of the entire class with a single sf::Transformable::setPosition. This works.
Next, I create another class called ToggleButton that inherits from sf::Transformable and is composed of two Buttons. My motivation is the same. I want to be able to set the position of the entire class with a single single sf::Transformable::setPosition. This works.
However, I run into a problem with Button::handler(). The handler is comparing the position of the button with the position of the mouse. The problem is Button::handler() does not take into account the transform (setPosition) that occurs to ToggleButton.
When drawing the elements this is not an issue, because by inheriting from sf::Drawable I can easily apply the transform within sf::Drawable::draw with
states.transform *= getTransform();
One solution I see is for ToggleButton not to inherit from sf::Transformable. Then simply call sf::Transformable::setPosition on each Button member variable. However, I am curious to understand how I should be properly using sf::Tranformable in such situations. How do I "string" multiple transforms like this together?
I updated Button::handler's signature from
void handler(const sf::RenderWindow& window, const sf::View& view);
to
void handler(const sf::RenderWindow& window, const sf::View& view, sf::Transformable* transformable = nullptr);
and then I am able to apply the transform to my rect
sf::FloatRect buttonRect(getPosition().x, getPosition().y, mSize.x, mSize.y);
if (transformable)
buttonRect = transformable->getTransform().transformRect(buttonRect);
This works. Now I am wondering if my motivation to use sf::Transformable is intended. The ToggleButton class inherits sf::Transformable, because I like how it "creates" a coordinate system for that object. For instance, within the ToggleButton object I can place a Button button0 at (0,0) and then I can place Button button1 at (button0.width + buffer, 0). Then I can set the position of the entire instance of the ToggleButton object in the game world. Granted, I only care about the position (and not rotation, scaling, etc), but is this the intended usage?
By the way, are you only using the transforms for position? Do you test something like sf::FloatRect::contains()?
Yes and yes.