Nifty console focus

Hi Guys,



I am having a problem with the nifty console and appstates. I have setup my game so that each “scene” is an app state ( extends AppState ) and a GUI app state ( extends AppState and implements ScreenController ). So a switch of scene is done like this:

[java]

public void goToGame(){

System.out.println(“State change: To Game Screen”);

this.stateManager.detach( this.startmenustate );

this.stateManager.detach( this.startmenugui );



this.stateManager.attach( this.gamescreen );

this.stateManager.attach( this.gamegui );

}



public void goToStartScreen(){

System.out.println(“State change: To Start Screen”);

this.stateManager.detach( this.gamescreen );

this.stateManager.detach( this.gamegui );



this.stateManager.attach( this.startmenustate );

this.stateManager.attach( this.startmenugui );

}

[/java]



Now when I go directly to the Game ( skipping the start screen ) my console works perfectly. The textfield is given focus and all keyboard events are consumed by the console. When I start the game through the start screen the textfield does not get focus ( the text cursor is in the top left cornor and keyboard events are not consumed… )



I doubt the error is in the bit of code that calls the console popup and gives focus, but I really have no idea… Here is that bit of code:

[java]

// Find the focus element:

Element focus = popup.findElementByName( “console#textInput” );

if( focus == null )

console.output(“Error: Unable to focus textfield… Console broken!”);



// Show the popup:

nifty.showPopup( screen, popup.getId(), focus );



// Force an update of the layers.

screen.processAddAndRemoveLayerElements();

[/java]



Any ideas what might be wrong? Any suggestions would be appreciated, I seem to have been going in circles for days on end now…



Cheers,



NiHal

Just a short addition to the above. I have checked that the second block of code actually finds the textInput element, and it is never null. I have a vague feeling that the error may be related to how I am initializing a new GUI State.



StartScreenGUI is very simple:

[java]

nifty.fromXml( GameProperties.getProperty("menusystem"), "start", this );

[/java]



GameScreenGUI is only slightly more involved ( two controllers ):

[java]

nifty.fromXml( "Interface/hud.xml", "gamehud", new ScreenController[]{ this, new ConsoleSameScreenStartScreen() } );



// Create the console popup…

if ( popup == null ) {

popup = nifty.createPopupWithId("consolePopup", "consolePopup");

}



// Get a reference to the console control.

if ( console == null ) {

console = popup.findNiftyControl( "console", Console.class );

}





this.setupCommands(); // Setup all the console commands

[/java]



Edit: I believe that .fromXML is supposed to clear any previous contents of the nifty object, can someone confirm that?

A hint anyone?

I am really not sure if it helps.



But that’s how I handle my console + focusing the textbox of it.

My console is on its own layer.



[java]

switch (mode) {

case TOGGLE:

if (consolelayer.isVisible()) {

consolelayer.hide();

} else {

consolelayer.show();

console.getTextField().setFocus();

}

break;

case SHOW:

consolelayer.show();

console.getTextField().setFocus();

break;

case HIDE:

consolelayer.hide();

break;

}

[/java]

1 Like

Cheers imax,



I will look into that approach. Your method of focusing is slightly different, I might start there.