Drawing Shapes within gui

Started by Xdesign, 28 December 2021, 21:10:15

Xdesign

Can i draw sf::RectangleShape or any other shape within custom widget?
Is there a way for me to draw shapes in order like elements of container are drawn in order?
Like, if i make custom widget that inherits from tgui::Panel i see that i cant override draw function.
basically what i want is to draw everything from tgui::Panel and then draw my shapes.

Sorry for frequent questions but im relatively new :)

texus

#1
QuoteCan i draw sf::RectangleShape or any other shape within custom widget?
Sure. Just look at some widgets (e.g. EditBox::draw) as an example on how they draw things.

The line you need is this:
Code (cpp) Select
target.drawFilledRect(states, size, color);

The position is based on the states (it starts at the widget position at the beginning of the draw() function and you can add to it with "states.transform.translate"). The size of the rectangle is specified as second parameter.

Quoteif i make custom widget that inherits from tgui::Panel i see that i cant override draw function
I'm not sure why you think you can't, because you can just override the draw function again.
Code (cpp) Select
void draw(BackendRenderTarget& target, RenderStates states) const override;
(Use BackendRenderTargetBase instead of BackendRenderTarget if using TGUI 0.9)

The implementation of your draw function will look like this:
Code (cpp) Select
void YourCustomWiget::draw(BackendRenderTarget& target, RenderStates states) const
{
    // Draw the background of the panel and all widgets inside it
    Panel::draw(target, states);

    // Draw your own shapes here
    //target.draw...
}


QuoteSorry for frequent questions but im relatively new :)
No problem, I don't mind.

Xdesign

I guess i got confuesed because tgui::Panel draw is without virtual but now i know what override is for :)
Alright cool i dont have to use sf::Shapes now
I did something similar but it didnt work for some reason. Now it does!
Thank you :)