Remove all GUI Elements

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
rikyoh
Posts: 14
Joined: Fri Sep 08, 2006 1:02 pm

Remove all GUI Elements

Post by rikyoh »

Hi,

is there an easy way to remove all GUI Elements?

Bye,
Rikyoh
zeno60
Posts: 342
Joined: Sun May 21, 2006 2:48 am
Location: NC, USA
Contact:

Post by zeno60 »

I guess you could set a flag to stop calling environment->drawAll(); in your irrlicht loop.

Code: Select all

bool removegui = false;

while(device->run())
{
     driver->beginScene(true, true, 0);
     if(!removegui)
     {
          guienvironment->drawAll();
     }

     driver->endScene();
}

//
OnEvent(SEvent event)
{
     if(KEY_SPACE)
     {
          removegui = true;
     }
}
Although this prevents you from showing other gui menus later in your game. The way I do it is just to create seperate functions. Like showmenu() will set all the menu elements visible to true, hidemenu() sets them invisible. showpausemenu() sets the pause menu items visible, hidepausemenu, etc. etc.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

I also do what Zeno does as calling the remove method of GUI elements didn't work for me (using Jirr, don't know if that's just a Jirr problem or if it happens in Irrlicht too).
Image Image Image
AlexL
Posts: 184
Joined: Tue Mar 02, 2004 6:06 pm
Location: Washington State

Post by AlexL »

I forgot where abouts I found this on the forums, and who the code is by, but it has worked for me thus far. If anyone can tell who the credits goto that would be appriciated.

Code: Select all

bool CApplication::clearGUIElements()
{
	irr::core::list<irr::gui::IGUIElement*> hElementList = g_pGlobals->pGUI->getRootGUIElement()->getChildren();
	if(hElementList.empty())
	{
		return false;
	}
	while(!hElementList.empty())
	{
		(*(hElementList.getLast()))->remove();
	}
	return true;
}
Strong99
Admin
Posts: 687
Joined: Fri Mar 31, 2006 7:06 pm
Location: Netherlands
Contact:

Post by Strong99 »

if you set drawall off, you can still click the buttons i would say....
rikyoh
Posts: 14
Joined: Fri Sep 08, 2006 1:02 pm

Post by rikyoh »

Hi,

tanks for your replies. In my game I have several gamestates which all need different "setups" of scenenodes and guielements, so when I switch from one state to the other I want to first delete all elements of the old state and then create the elements needed for the new state. Thats no problem with the scene because there is a "clear()" function. Not to draw the gui-elements is no solution for me, because I need gui-elemtents in every gamestate. But the function AlexL posted seems to be what I need.

Update: it doesn't work! The "remove()" removes the gui-element, but the size of the list doesn't decrement and so the list never gets empty and I have endless loop. I think there must some code be added to delete the listmember after removing the gui element. Something with iterator... but I don't get it to work :-(

Bye,
Rikyoh
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

It doesn't work because you've made a copy of the list and you're iterating over the copy, but removing from the original. The code should look like this...

Code: Select all

// notice that this is a reference to the list maintained by the root element
const core::list<IGUIElement*>& children = Environment->getRootGUIElement()->getChildren();
while (!children.empty())
   (*children.getLast())->remove();
If you are going to maintain multiple sets of UI, it may make sense to do something like this...

Code: Select all

struct SGameState
{
  IGUIElement* UserInterface;
  ISceneNode* Scene;
};

enum E_GAME_STATE { EGS_INVENTORY, EGS_PLAYING, EGS_COUNT };
SGameState AvailableGameStates[EGS_COUNT];
SGameState* ActiveGameState = 0;

   // somewhere in your init code
   u32 s;
   for (s = 0; s < EGS_COUNT; ++s)
   {
      AvailableGameStates[s].UserInterface = new IGUIElement(EGUIET_ELEMENT, Environment, Environment->getRootSceneNode(), -1, FullWindowRect);
      AvailableGameStates[s].Scene = SceneManager->addEmptySceneNode();
   }
   AvailableGameStates[s].UserInterface = 0;
   AvailableGameStates[s].Scene            = 0;

   ActiveGameState = &AvailableGameStates[0];

   // add controls to each of the user interfaces...

// method for changing ui
void setActiveGameState(E_GAME_STATE egs)
{
   ActiveGameState->UserInterface->setVisible(false);
   ActiveGameState->Scene        ->setVisible(false);

   ActiveGameState = &AvailableGameStates[egs];

   ActiveGameState->UserInterface->setVisible(true);
   ActiveGameState->Scene        ->setVisible(true);
}
I would wrap all of that up into a game management class. It allows you to enable/disable an entire game state without needing to rebuild the scene/gui every time the user switches.
rikyoh
Posts: 14
Joined: Fri Sep 08, 2006 1:02 pm

Post by rikyoh »

Hello,

thanks, the removing of GUI Elements seems to work know. Your idea for maintaining multiple sets of UI and Scenenodes looks very interesting and would fit easy into my actual design, but wouldn't that have an impact on performance? If I have two very complex scenes and only deactivate the visibility of one of them for example?

Bye
Riky
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Things that aren't visible don't get rendered so the frame rate should be ok, but if you have loads of stuff in memory then you could run out maybe or run into problems there.
Image Image Image
shivanraptor
Posts: 40
Joined: Tue Aug 15, 2006 6:01 am
Location: Hong Kong
Contact:

Post by shivanraptor »

how to remove all GUI elements in C# ?? please help .

i opened a thread here :
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=16935
Irrlicht.NET user
* * * * * * * * * * * * *
Currently working on:
RPG Turbulence 2
Game Engine Turbulence Engine
shivanraptor
Posts: 40
Joined: Tue Aug 15, 2006 6:01 am
Location: Hong Kong
Contact:

Post by shivanraptor »

vitek wrote:It doesn't work because you've made a copy of the list and you're iterating over the copy, but removing from the original. The code should look like this...

Code: Select all

// notice that this is a reference to the list maintained by the root element
const core::list<IGUIElement*>& children = Environment->getRootGUIElement()->getChildren();
while (!children.empty())
   (*children.getLast())->remove();
If you are going to maintain multiple sets of UI, it may make sense to do something like this...

Code: Select all

struct SGameState
{
  IGUIElement* UserInterface;
  ISceneNode* Scene;
};

enum E_GAME_STATE { EGS_INVENTORY, EGS_PLAYING, EGS_COUNT };
SGameState AvailableGameStates[EGS_COUNT];
SGameState* ActiveGameState = 0;

   // somewhere in your init code
   u32 s;
   for (s = 0; s < EGS_COUNT; ++s)
   {
      AvailableGameStates[s].UserInterface = new IGUIElement(EGUIET_ELEMENT, Environment, Environment->getRootSceneNode(), -1, FullWindowRect);
      AvailableGameStates[s].Scene = SceneManager->addEmptySceneNode();
   }
   AvailableGameStates[s].UserInterface = 0;
   AvailableGameStates[s].Scene            = 0;

   ActiveGameState = &AvailableGameStates[0];

   // add controls to each of the user interfaces...

// method for changing ui
void setActiveGameState(E_GAME_STATE egs)
{
   ActiveGameState->UserInterface->setVisible(false);
   ActiveGameState->Scene        ->setVisible(false);

   ActiveGameState = &AvailableGameStates[egs];

   ActiveGameState->UserInterface->setVisible(true);
   ActiveGameState->Scene        ->setVisible(true);
}
I would wrap all of that up into a game management class. It allows you to enable/disable an entire game state without needing to rebuild the scene/gui every time the user switches.
can u translate the code to C# ? i think some functions in C++ does not exist in Irrlicht .NET . true ?
Irrlicht.NET user
* * * * * * * * * * * * *
Currently working on:
RPG Turbulence 2
Game Engine Turbulence Engine
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

can u translate the code to C# ? i think some functions in C++ does not exist in Irrlicht .NET . true ?
I don't know, can you? Try it and find out, it's only a few lines of code.
shivanraptor
Posts: 40
Joined: Tue Aug 15, 2006 6:01 am
Location: Hong Kong
Contact:

Post by shivanraptor »

vitek wrote:
can u translate the code to C# ? i think some functions in C++ does not exist in Irrlicht .NET . true ?
I don't know, can you? Try it and find out, it's only a few lines of code.
the constructor of IGUIElement is VERY different from C++ and it cannot be implemented in C# . it cannot follow the code as two libraries are totally differrent

public IGUIElement(
IGUIElement* element
);
Irrlicht.NET user
* * * * * * * * * * * * *
Currently working on:
RPG Turbulence 2
Game Engine Turbulence Engine
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Sure seems like that is enough to do it.
Post Reply