update the progressbars
« on: 28 July 2016, 12:28:20 »
Hi,
Sorry for my bad english and thinks for your libray.
i want update the progressbars for my entite but i don t know how do that.

i have a class how initializes progressBars.
Code: [Select]
for (unsigned int i=0 ;i<m_eRPG->m_lavie.size();i++){
        std::cout<<"nb de progresse bar "<<i<<std::endl;
        tgui::ProgressBar::Ptr progressBar ;
        progressBar = theme->load("ProgressBar");
        progressBar->setSize(30, 5);
        progressBar->setValue(m_eRPG->m_lavie[i]->getM_VieRestante());
        m_gui.add(progressBar);
    }

and i have a update for the positions with widgets and it s ok

Code: [Select]
void guiWorld::update(sf::View v){

   m_gui.setView(v);
    std::cout<<"widgets ="<<m_gui.getWidgets().size()<<std::endl;

    for(int i=0;i<m_gui.getWidgets().size();i++){
         m_gui.getWidgets()[i]->setPosition(m_eRPG->m_lavie[i]->getPosition().x*32+5,  m_eRPG->m_lavie[i]->getPosition().y*32-30);
   
    }

}

but for update progressBar->setValue i don't know how do to have the progressBars ?

*

texus

  • *****
  • 1161
    • View Profile
    • Texus's Blog
Re: update the progressbars
« Reply #1 on: 28 July 2016, 12:58:07 »
With your current code the m_gui.getWidgets()[ i ] is of type Widget::Ptr so you will need to cast it to a ProgressBar::Ptr to call functions of the progress bar. Since these types are just a typedefs for std::shared_ptr, you can use std::dynamic_pointer_cast to cast them:
std::dynamic_pointer_cast<tgui::ProgressBar>(m_gui.getWidgets()[i])->setValue(...));

The code that you show might give some problems later on though. The code only functions as long as m_gui.getWidgets() and m_eRPG->m_lavie.size() have the same size which means that if you want to use anything from the gui other than health bars for the characters then you get into trouble.

I would suggest that you store the progress bars yourself. The first way would be to have a ProgressBar::Ptr member in m_eRPG->m_lavie. That way you can loop over the the m_eRPG->m_lavie every frame and just do m_eRPG->m_lavie[ i ]->progressBar->setPosition(...). This may however not be good design if you want to keep the gui seperate from your entities. So a second way would simply to hold a std::vector<tgui::ProgressBar::Ptr> somewhere. In the first code snippet you would add progressBarVector.push_back(progressBar) while in the second code snippet you would loop over this progressBarVector instead of over all widgets which allows you to even call setValue directly (as you don't have to cast).

Edit: I just noticed that you are already using the gui for other things than progress bars. Are you using multiple Gui objects perhaps?
« Last Edit: 28 July 2016, 13:00:32 by texus »

Re: update the progressbars
« Reply #2 on: 28 July 2016, 13:57:04 »
Thank you very much for the quick response

Quote
Are you using multiple Gui objects perhaps?
yes, currently i use for one for the defaut view for the interface. And another for the progresseBar who is the view in the game.
Quote
The code that you show might give some problems later on though.
lol i understand if a entite is deleted it's a big probleme

Re: update the progressbars
« Reply #3 on: 28 July 2016, 17:54:27 »
if i use std::vector<tgui::ProgressBar::Ptr> how i draw the elements ?

i can do
std::vector<tgui::ProgressBar::Ptr> myProgressBar;
...

void game::draw(sf::RenderTarget& target, sf::RenderStates states) const{

for (int i=0;i<myProgressBar.size();i++)
target.draw(myProgressbar, states);

}

*

texus

  • *****
  • 1161
    • View Profile
    • Texus's Blog
Re: update the progressbars
« Reply #4 on: 28 July 2016, 18:01:50 »
You shouldn't have to draw them manually.
If you add them to the gui with gui.add then they will be drawn automatically when calling gui.draw().