check if a widget is already
« on: 10 February 2014, 15:49:34 »
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 :
Code: [Select]
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

  • *****
  • 1161
    • View Profile
    • Texus's Blog
Re: check if a widget is already
« Reply #1 on: 10 February 2014, 17:08:51 »
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:
Code: [Select]
if (gui.get("fenetre") == nullptr) {
    // The window doesn't exist yet, create it now
}

Re: check if a widget is already
« Reply #2 on: 10 February 2014, 17:48:58 »
Thank you very much, its working.

Other problem, in this fonction i have :
Code: [Select]
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

  • *****
  • 1161
    • View Profile
    • Texus's Blog
Re: check if a widget is already
« Reply #3 on: 10 February 2014, 18:05:24 »
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.
Code: [Select]
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:
Code: [Select]
editBox->bindCallback(&MyClass::function, &myObj, tgui::EditBox::ReturnKeyPressed);
So your line should become:
Code: [Select]
widget.fenÍtre->bindCallbackEx(&cVanne::fermerWidget, this, tgui::ChildWindow::Closed);
« Last Edit: 09 March 2014, 00:19:29 by texus »

Re: check if a widget is already
« Reply #4 on: 10 February 2014, 18:18:36 »
Thank, it's working.
I think i will to revise a little c++ therorie...