Nifty GUI – toggle Screen [solved]

Hi all,

In my current project I am trying to use Nifty to “toggle” between a game hub and a specific gui element. For example every time I press the key “M” I want to open an overview about my stats. If I press the key “M” again, the overview goes away and my game hub is on the screen again. No problems so far. The problem starts if I hit a close button on the overview which is also a way to open the hub again. If I am pressing the key “M” now, nothing happens. Only after another press on the key opens the overview. I am still new to Nifty, so I have absolut no clues about what I am doing wrong. :frowning:

Here a part from my code:

ScreenController:

[java]public void switchScreen(String id){
if(activeScreen == true){
nifty.gotoScreen(“hud”);
this.activeScreen = false;
}

    else if(activeScreen == false) {
        nifty.gotoScreen(id);
        this.activeScreen = true;
    }
}[/java] 

Key Input:

[java]public void onAction(String name, boolean isPressed, float tpf) {

   if(name.equalsIgnoreCase("M") && !isPressed){
        screenController.switchScreen("start");
   }
}[/java] 

I hope someone can solve my problem. :slight_smile:

Could it be that you mess up something in the handler of close button ? I don’t know maybe forgot to update the activeScreen variable? By the way for this user cases popups also play well :slight_smile: but you can do also as you made :).

Thanks for your reply. :slight_smile: As far as I know the click action ignores to update the activeScreen variable somehow. It seems like the click interaction opens a seperate object of my screencontroller. I used the debug modus and it shows that the activeScreen is still “true” after clicking the start button.

That´s my XML file:

[java]<?xml version=“1.0” encoding=“UTF-8”?>
<nifty xmlns=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd”>
<useStyles filename=“nifty-default-styles.xml” />
<useControls filename=“nifty-default-controls.xml” />
<screen id=“start” controller=“GUI.TestScreenController”>
<layer id=“background” childLayout=“center”>
<image filename=“Interface/splash.png”></image>
</layer>
<layer id=“foreground” childLayout=“vertical”>
<panel id=“panel_top” height=“25%” width=“75%” align=“center” childLayout=“center”>
<text text="" font=“Interface/Fonts/Default.fnt” width=“100%” height=“100%” />
</panel>
<panel id=“panel_mid” height=“50%” width=“75%” align=“center” childLayout=“center”>
<text text="" font=“Interface/Fonts/Default.fnt” width=“100%” height=“100%” wrap=“true” />
</panel>
<panel id=“panel_bottom” height=“25%” width=“75%” align=“center” childLayout=“horizontal” >
<panel id=“panel_bottom_left” height=“25%” width=“50%” valign=“center” childLayout=“center”>
<control name=“button” label=“Start” id=“StartButton” align=“center” valign=“center”
visibleToMouse=“true” >
<interact onClick=“switchScreen(hud)”/>
</control>
</panel>
<panel id=“panel_bottom_right” height=“50%” width=“50%” valign=“center” childLayout=“center”>
<control name=“button” label=“Quit” id=“QuitButton” align=“center” valign=“center”
visibleToMouse=“true” >
<interact onClick=“quitGame()”/>
</control>
</panel>
</panel>
</layer>
</screen>
<screen id=“hud” controller=“GUI.TestScreenController”></screen>
</nifty>[/java]

Update: Found the solution by myself. Thanks anyway! :slight_smile:

[java]public void switchScreen(String id){
screen = nifty.getCurrentScreen();
String ID1 = screen.getScreenId();

    if(!id.equalsIgnoreCase(ID1)){
        nifty.gotoScreen(id);
    }
    else {
        nifty.gotoScreen("hud");
    }
}[/java]