Okay, so here's the situation:
I have a Button class, containing sf::Text and a sf::RectangleShape and a sf::FloatRect, telling what part of the window it must cover. I need to draw it on 2 different windows and I want it to scale with those windows automatically. The thing is that virtual void draw is const, so I can't change the size of the RectangleShape and the Text while drawing.
So, currently I have 2 choices:
Either copy the text and rectangleshape to new ones and draw those, which is waste of performance and typing or
create a specific update function for this case and call it every time the windows get resized which may make my code a lot more messy and I need to track that for every single button I create, which may cause bugs.
So, I was wondering if there is an easier way to accomplish this or resort to the second option?
My current code:
in UI\Button.hppnamespace UI
{
class Button : public sf::Drawable, public sf::Transformable
{
private:
sf::Text mText;
sf::FloatRect mButtonSize;
sf::RectangleShape mButtonRect;
...
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
...
}
}
in UI\Button.cppvoid UI::Button::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
sf::Vector2f tmp = sf::Vector2f(target.getView().getSize().x * mButtonSize.width, target.getView().getSize().y * mButtonSize.height);
mButtonRect.setSize(tmp); //changing size causes error due to const
mButtonRect.setPosition(target.getView().getSize().x * mButtonSize.left, target.getView().getSize().y * mButtonSize.top);
mText.setCharacterSize(mButtonRect.getSize().y * mTextRelativeSize);
target.draw(mButtonRect, states);
target.draw(mText, states);
}