Simple Nifty Usage Question

Hi All,



I feel like I must be daft or something, because I am reading through Nifty and it is quite neat, except that I can’t seem to figure out how to do a few what I feel should be menial tasks.



What I have now is an xml document with a panel that just slides across that says “Hello World” and you click it and it prints to the console “Hello”.



I want to have it so that when you click the Hello World, it switches to a different screen, or the panels defined in that screen disapear and the panels defined in a second screen appear.



It would also be nice to have my controller classes inherit from whatever the main controller class is that nifty uses so that I can utilize all the effects and buttons and other neat things that are provided and then extend upon it for my calculations that I need to do.



Thanks for any help/hints/tips…Im just finding this information hard to locate. It would be nice if there were a tutorial called “Changing screens” or “How to Change Screens” most of the tutorials seem to focus on one screen and I haven’t seen any that talk about switching between them. (the java doc on the methods is also empty).



~David



Here is my nifty xml

[xml]

<nifty>

<screen id=“start” controller=“mygame.StartScene”>

<layer id=“baseLayer” backgroundcolor="#003f" childLayout=“center”>



<panel height=“25%” width=“35%” align=“center” valign=“center” backgroundColor="#f60f"

childLayout=“center” visibleToMouse=“true”>

<text id=“text” font=“aurulent-sans-17.fnt” color="#000f" text=“Menu Selection!”

align=“center” valign=“center” />

<interact onClick=“sayHello(hi)”/>

</panel>



</layer>

</screen>



<screen id=“dataCollectionScene” controller=“mygame.DataCollectionScene”>

<layer id=“baseLayer” backgroundcolor="#005f" childLayout=“center”>



<panel height=“25%” width=“35%” align=“center” valign=“center” backgroundColor="#f60f"

childLayout=“center” visibleToMouse=“true”>

<text id=“text” font=“aurulent-sans-17.fnt” color="#000f" text=“Data Collection!”

align=“center” valign=“center” />

<interact onClick=“sayHelloToo(hi)”/>

</panel>



</layer>

</screen>





</nifty>

[/xml]



Here are the two controller classes

[java]/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.

    */



    package mygame;



    import com.jme3.asset.AssetManager;

    import com.jme3.font.BitmapFont;

    import de.lessvoid.nifty.Nifty;

    import de.lessvoid.nifty.screen.Screen;

    import de.lessvoid.nifty.screen.ScreenController;



    /**

    *
  • @author David

    /

    public class StartScene implements ScreenController

    {





    public void bind(Nifty nifty, Screen screen)

    {



    }



    public void onStartScreen()

    {



    }



    public void onEndScreen()

    {

    }



    public void sayHello(String myarg)

    {

    System.out.println("Nifty says: "+myarg);

    //Change to other screen now for test.

    }





    }

    [/java]



    Other Java Class

    [java]/

  • To change this template, choose Tools | Templates
  • and open the template in the editor.

    */



    package mygame;



    import de.lessvoid.nifty.Nifty;

    import de.lessvoid.nifty.screen.Screen;

    import de.lessvoid.nifty.screen.ScreenController;



    /**

    *
  • @author David

    */

    public class DataCollectionScene implements ScreenController

    {

    public void bind(Nifty nifty, Screen screen)

    {



    }



    public void onStartScreen()

    {



    }



    public void onEndScreen()

    {

    }



    public void sayHelloToo(String myarg)

    {

    System.out.println("Nifty says: "+myarg);

    //Change to other screen now for test.

    }



    }[/java]

nifty.gotoScreen() ?

Thanks to the both of ya. I have a few defined in one xml, so gotoscreen was exactly what I was looking for.

well, that was about what I thought it should be like, I just could not find the method for the life of me…



Thanks, that was just frustrating.

You just do exactly the same thing you did when loading up the first screen:

[java]

nifty.fromXml("screenconfig.xml", "screenID");

[/java]



This loads up the new screen. Unless off course you have several screens defined in the same xml file.

Then you probably need to use:

[java]

nifty.gotoScreen("screenID");

[/java]

Does anybody have an example on how to use buttons (in the xml and the java class) or how to connect it. I looked at this page on the wiki, and it doesnt seem to help much: http://sourceforge.net/apps/mediawiki/nifty-gui/index.php?title=Standard_Controls_Button



This is what I tried to do, but it just doesn’t work…

java.util.NoSuchElementException

at java.util.AbstractList$Itr.next(AbstractList.java:350)

at de.lessvoid.nifty.loaderv2.types.ControlType.makeFlatControlsInternal(ControlType.java:67)

at de.lessvoid.nifty.loaderv2.types.ElementType.makeFlatControls(ElementType.java:330)

at de.lessvoid.nifty.loaderv2.types.ElementType.makeFlatControls(ElementType.java:328)

at de.lessvoid.nifty.loaderv2.types.ElementType.makeFlatControls(ElementType.java:328)

at de.lessvoid.nifty.loaderv2.types.ElementType.prepare(ElementType.java:288)

at de.lessvoid.nifty.loaderv2.types.ScreenType.create(ScreenType.java:64)

at de.lessvoid.nifty.loaderv2.types.NiftyType.create(NiftyType.java:137)

at de.lessvoid.nifty.Nifty.loadFromFile(Nifty.java:435)

at de.lessvoid.nifty.Nifty.fromXml(Nifty.java:336)



[xml] <screen id=“dataCollectionScene” controller=“mygame.DataCollectionScene”>

<layer id=“baseLayer” backgroundcolor="#005f" childLayout=“center”>



<panel style=“nifty-panel” height=“50%” width=“50%” backgroundColor="#f60f"

childLayout=“center” visibleToMouse=“true”>

<control name=“appendButton” label=“Go To Menu”>

<interact onClick=“returnToStartScreen()”/>

</control>

</panel>

<panel height=“15%” width=“25%” align=“center” valign=“top” backgroundColor="#f60f"

childLayout=“center” visibleToMouse=“true”>

<text id=“serverStart” font=“aurulent-sans-17.fnt” color="#000f" text=“Start Server”

align=“center” valign=“center” />

<interact onClick=“startServer()”/>

</panel>



</layer>

</screen>[/xml]



and the methods listed in each interact work as they should (tested sepeartely). The idea is to have the first panel (the nifty-style panel) contain two buttons one to start the server and one to return to the other screen.



Thanks for any help,

~David

ups, the example in the wiki was broken. I’ve corrected it now. sorry about that :slight_smile:



This probably works better:



[xml]<control id=“appendButton” name=“button” label=“Append” />[/xml]



for the control tag the attributes are:



name → select the control, in this case (name=“button”) the button control

label → the label text the button should display

id → the id of the button so that you can access it from java

Hrm, I tried changing that, and Im not getting it to work properly…do I need to somehow connect my Data Collection Scene

differently, or have it inherit from a class? Right now it just implements ScreenController as in the basic example.



Thanks for any help,

~David

[xml][/xml]



[xml][/xml]



[java]package mygame;



import ServerStuff.TCPServer;

import de.lessvoid.nifty.Nifty;

import de.lessvoid.nifty.screen.Screen;

import de.lessvoid.nifty.screen.ScreenController;



/**

*

  • @author David

    */

    public class DataCollectionScene implements ScreenController

    {



    TCPServer myServer = new TCPServer();

    [/java]

after some digging around and some more attempts I cant get it to work, it would appear that the element “button” does not exist.



WARNING: controlDefinition [button] missing.

java.util.NoSuchElementException

at java.util.AbstractList$Itr.next(AbstractList.java:350)

at de.lessvoid.nifty.loaderv2.types.ControlType.makeFlatControlsInternal(ControlType.java:67)

at de.lessvoid.nifty.loaderv2.types.ElementType.makeFlatControls(ElementType.java:330)

at de.lessvoid.nifty.loaderv2.types.ElementType.makeFlatControls(ElementType.java:328)

at de.lessvoid.nifty.loaderv2.types.ElementType.makeFlatControls(ElementType.java:328)

at de.lessvoid.nifty.loaderv2.types.ElementType.prepare(ElementType.java:288)

at de.lessvoid.nifty.loaderv2.types.ScreenType.create(ScreenType.java:64)

at de.lessvoid.nifty.loaderv2.types.NiftyType.create(NiftyType.java:137)

at de.lessvoid.nifty.Nifty.loadFromFile(Nifty.java:435)

at de.lessvoid.nifty.Nifty.fromXml(Nifty.java:336)

at mygame.Main.simpleInitApp(Main.java:53)

at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:218)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:138)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:206)

at java.lang.Thread.run(Thread.java:662)



defined just as in the example.

Maybe adding this:

[xml]

<nifty>

<useStyles filename="nifty-default-styles.xml" />

<useControls filename="nifty-default-controls.xml" />

…[/xml]



will help.

1 Like

WOOHOO! works now! Awesome, thanks!