check if a widget is already

Started by BruceBoc, 10 February 2014, 15:49:34

BruceBoc

Hello,
I have a button that will open a childwindow, but i want only one instance of chilwindow even if i click several time on the button.
Here the code :

void cVanne::afficherWidget(tgui::Gui &gui)
{
widget.fenêtre = tgui::ChildWindow::Ptr(gui, "fenetre");
widget.fenêtre->load(THEME_CONFIG_FILE);
widget.fenêtre->setSize(100, 100);
widget.fenêtre->setBackgroundColor(sf::Color(80, 80, 80, 125));
widget.fenêtre->setPosition(sprite.posX, sprite.posY+sprite.tailleY);
widget.fenêtre->setTransparency(125);
widget.fenêtre->setBorders(2,2,2,2);
widget.fenêtre->setCallbackId(1);

}


What should i do if i want to check if childwindows is already open?

texus

If there is a window then the Gui::get function will return a pointer to it, otherwise it will return a nullptr.

So you only need to add the following check:
if (gui.get("fenetre") == nullptr) {
    // The window doesn't exist yet, create it now
}

BruceBoc

Thank you very much, its working.

Other problem, in this fonction i have :

void cVanne::afficherWidget(tgui::Gui &gui)
{
//tgui::ChildWindow::Ptr child(gui);
if (gui.get("fenetre") == nullptr) {
widget.fenêtre = tgui::ChildWindow::Ptr(gui, "fenetre");
widget.fenêtre->load(THEME_CONFIG_FILE);
widget.fenêtre->setSize(100, 100);
widget.fenêtre->setBackgroundColor(sf::Color(80, 80, 80, 125));
widget.fenêtre->setPosition(sprite.posX, sprite.posY+sprite.tailleY);
widget.fenêtre->setTransparency(125);
widget.fenêtre->setBorders(2,2,2,2);
widget.fenêtre->setCallbackId(1);
widget.fenêtre->bindCallbackEx(cVanne::fermerWidget, tgui::ChildWindow::Closed);
}

}

void cVanne::fermerWidget(const tgui::Callback& callback)
{



}


but the compiler don't accepte the bindCallbackEx.

texus

#3
Here is a tutorial about the callbacks: https://tgui.eu/tutorials/v06/callback-system/.

To go a little more into detail:
A function pointer doesn't accept pointers to member functions, because these function actually have another parameter behind the scenes (a pointer to the object). This is the reason I went to c++11 to use std::function. But even here you have to do something extra, you have to bind that extra parameter.
editBox->bindCallback(std::bind(&MyClass::function, &myObj), tgui::EditBox::ReturnKeyPressed);

And to make that line a little shorter I added another bindCallback function that you can just call like this:
editBox->bindCallback(&MyClass::function, &myObj, tgui::EditBox::ReturnKeyPressed);

So your line should become:
widget.fenêtre->bindCallbackEx(&cVanne::fermerWidget, this, tgui::ChildWindow::Closed);

BruceBoc

Thank, it's working.
I think i will to revise a little c++ therorie...