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

Author Topic: sf::Transformable  (Read 1627 times)

0 Members and 1 Guest are viewing this topic.

Kanefa

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
sf::Transformable
« on: October 25, 2014, 11:52:57 am »
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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Transformable
« Reply #1 on: October 25, 2014, 12:03:11 pm »
In case the handler() method should be called directly on one of the two child buttons, the Button class must be aware if it is a child. This can be done by storing a parent pointer ToggleButton* (or in general, sf::Transformable or an intermediate interface class). Button::handler() would then check whether the parent is no null pointer, and multiply its transform with the current one.

Otherwise, if you don't want a Button to know about its parent ToggleButton, you could pass a transform argument to handler().

By the way, are you only using the transforms for position? Do you test something like sf::FloatRect::contains()?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Kanefa

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: sf::Transformable
« Reply #2 on: October 25, 2014, 02:47:29 pm »
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.