[nifty] element.setVisible(false) not working

I didn’t specify the height of menuPanel. It is calculated by Nifty after the screen is read from the XML file. I’d call that layouting. And I’d expect Nifty to do the same layouting again if needed or at least if ask for it.



I finally found a solution. Not beautiful, but working.



[java]

void showMenu(boolean hasStarted)

{

Element menuLayer = this.screen.findElementByName(“menuLayer”);

Element menuPanel = this.screen.findElementByName(“menuPanel”);



menuLayer.setVisible(true);



if (hasStarted)

{

if (this.backButton != null)

{

menuPanel.getElements().add(this.backButtonIndex, this.backButton);

menuLayer.layoutElements();

this.backButton = null;

}



this.screen.findElementByName(“backButton”).setFocus();

}

else

{

if (this.backButton == null)

{

this.backButton = this.screen.findElementByName(“backButton”);

this.backButtonIndex = menuPanel.getElements().indexOf(this.backButton);

menuPanel.getElements().remove(this.backButton);

menuLayer.layoutElements();

}



this.screen.findElementByName(“startNewGameButton”).setFocus();

}

}

[/java]



The more time I spend into Nifty, the more things I discover that are not done as one would expect or the most complicated way possible.

1 Like

Ok, let me put in my two cents…



Using the hide method just makes the element invisible. It does not reclaim the space used by the element. This is what’s causing the behaviour you see in the image above. You have successfully hidden the element, but the space used by the element is still being reserved for the element.



To remedy this, you need to completely REMOVE the element. You can do this with the combination element.markForRemoval() and parentElement.layoutElements().

Later, when you want to show the element again you need to add it in the right location of the element list. This can be done with (of the top of my head, no code availlable right now) parentElement.insertChild(). Or something like that, there are several child methods, one fo just adding it to the end of the child element list (addChild) and one for inserting it at a certain index.



While this way of doing things may seem a bit more work, it does work quite well. It als ocmpletely removes the element from the element stack, which means it is not taken into consideration in the update loop. A hidden element is still traversed in the update loop, just not displayed. So in the end, doing it this way benefits performance as well.

It’s just so frustrating to spend hours and days debugging to achieve the simplest things. I really hope this thread can be of help for someone else.



I came across another problem. When first changing the resolution, then starting the game (which implies adding the “Back to game” button) and then opening the menu again, it looked like this:





To make Nifty re-calculate the size of “menuPanel”, I have to reset its height constraint to null. So here is the latest and hopefully final version of “showMenu()”:

[java]

void showMenu(boolean hasStarted)

{

Element menuLayer = this.screen.findElementByName(“menuLayer”);

Element menuPanel = this.screen.findElementByName(“menuPanel”);



menuLayer.setVisible(true);



if (hasStarted)

{

if (this.backButton != null)

{

menuPanel.getElements().add(this.backButtonIndex, this.backButton);

menuPanel.setConstraintHeight(null);

menuLayer.layoutElements();

this.backButton = null;

}



this.screen.findElementByName(“backButton”).setFocus();

}

else

{

if (this.backButton == null)

{

this.backButton = this.screen.findElementByName(“backButton”);

this.backButtonIndex = menuPanel.getElements().indexOf(this.backButton);

menuPanel.getElements().remove(this.backButton);

menuPanel.setConstraintHeight(null);

menuLayer.layoutElements();

}



this.screen.findElementByName(“startNewGameButton”).setFocus();

}

}

[/java]

I’ve had problems with resolution changing in runtime too, when I set the resolution and restart the application, it works, and then when I go to another screen, the size is not updated, I have to re-restart the application for all screens. I give it up.

Maybe @void256 should be informed of these resolution change issues… Since he’s the builder of the nifty core. I do know that there’s new functionality for changing the resolution in the latest nifty 1.3.1 nightly. Have you tried changing to that latest nightly?



Just checked but I can’t find a wiki page on the screen resizing. Wil talk to @void256 about it next time I see him.

The problem that I have is when I change the application resolution via app settings.

As I said, there’s now a method you can cal on nifty which you can feed the resolution.



nifty.enableAutoScaling(baseResolutionX, baseResolutionY)



I think this is the method you are looking for. This should rescale your entire nifty to the set resolution. I’m almost positive this is only availlable in the nightly build though

1 Like

hmmm! ok!

One can switch the display resolution in my little game here. The scaling feature sounds very promising. My GUI has a fixed size which can become a bit small on high resolutions.

like I said, if you pull the latest nifty nightly build, it should be in there. This will also get you the nifty treebox control, and a soon to be comlpetely done MessageBox.