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

Author Topic: [SOLVED] setPosition of composite transformable class  (Read 3002 times)

0 Members and 1 Guest are viewing this topic.

Anrock

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
[SOLVED] setPosition of composite transformable class
« on: March 28, 2014, 02:14:02 pm »
I'm trying to make TextField widget, here's header:
class TextField: public sf::Drawable, public sf::Transformable
{
        public:
                TextField(sf::Font& font, unsigned int charSize, const std::string& content, const sf::Vector2f& size);
                void setFocused(bool state);
                std::string getText() const;
                void setBackgroundColor(sf::Color);
                void processInput(sf::Event event);
                void draw(sf::RenderTarget& target, sf::RenderStates states) const;
                void clear();
                sf::Vector2f getSize() const;
        private:
                sf::RectangleShape background;
                sf::Text text;
                std::string string;

                sf::Color focusedColor;
                sf::Color unfocusedColor;
                };
 
As you can see it's just wrapper with rectangle inside and text on top of it and bunch of helper functions.

I've figured out how to draw passing it to the window, by inheriting from drawable and overloading draw method.

But i can't figure out how to make setPosition method for this class. Documentation suggests just inheriting from Transformable and using inherited setPosition method and all will be fine.
However it's doesn't work - position remains 0,0 whatever is passed to setPosition.

Transformable::setPosition isn't virtual so only option is make my own setPosition, right?
But what's the point in inheriting Transformable then? It doesn't work because i'm doing something wrong?
« Last Edit: April 02, 2014, 09:42:48 am by Anrock »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: setPosition of composite transformable class
« Reply #1 on: March 28, 2014, 04:17:04 pm »
Your current approach of inheriting sf::Transformable is correct, if not only getPosition(), but all the other inherited methods are meaningful for your class (Liskov Substitution Principle).

You have to take the transform of the base class into account when you draw:
void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    states.transform *= getTransform(); // getTransform is in sf::Transformable

    target.draw(object1, states);
    target.draw(object2, states);
    ....
}

By the way, I suggest to use the virtual (or even override) keyword if you override a virtual function, so that you immediately see that it is used polymorphically.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Anrock

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: setPosition of composite transformable class
« Reply #2 on: April 02, 2014, 09:42:21 am »
Thanks, that worked. I was multiplying transform, but didn't pass states.

 

anything