Drawing Shapes within gui

  • 2 Replies
  • 99 Views
*

Xdesign

  • *
  • 15
  • Graphic Design and Software development student
    • View Profile
Drawing Shapes within gui
« on: 28 December 2021, 21:10:15 »
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

  • *****
  • 1639
    • View Profile
    • Texus's Blog
Re: Drawing Shapes within gui
« Reply #1 on: 28 December 2021, 22:25:33 »
Quote
Can 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.

Quote
if 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...
}

Quote
Sorry for frequent questions but im relatively new :)
No problem, I don't mind.
« Last Edit: 28 December 2021, 22:32:35 by texus »

*

Xdesign

  • *
  • 15
  • Graphic Design and Software development student
    • View Profile
Re: Drawing Shapes within gui
« Reply #2 on: 28 December 2021, 22:45:01 »
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 :)