First screen not working with XML (Nifty GUI)

Hello all,



I followed the following documentation : https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:nifty_gui_xml_layout but the first screen never displays. I must be mistaken somewhere… I come directly on the 3D scene…



I do not use the SDK Jmonkey but Eclipse only. My tree in my Eclipse Project is like this:

+com.test.gui

->GameGui.java

->GameGui.xml



My java code :

[java]

import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.Vector3f;

import com.jme3.niftygui.NiftyJmeDisplay;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;

import de.lessvoid.nifty.Nifty;

import de.lessvoid.nifty.screen.Screen;

import de.lessvoid.nifty.screen.ScreenController;



public class GameGui extends SimpleApplication implements ScreenController {



private Nifty nifty;



public static void main(String[] args){

GameGui app = new GameGui();

app.setPauseOnLostFocus(false);

app.start();

}



public void simpleInitApp() {

NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,

inputManager,

audioRenderer,

guiViewPort);

nifty = niftyDisplay.getNifty();

nifty.fromXml(this.getClass().getResource("GameGui.xml").getPath(), "start");



guiViewPort.addProcessor(niftyDisplay);



flyCam.setEnabled(false);

flyCam.setDragToRotate(true);

inputManager.setCursorVisible(true);

}



public void bind(Nifty nifty, Screen screen) {

System.out.println("bind( " + screen.getScreenId() + ")");

}



public void onStartScreen() {

System.out.println("onStartScreen");

}



public void onEndScreen() {

System.out.println("onEndScreen");

}



public void quit(){

nifty.gotoScreen("end");

}



}

[/java]



My GameGui.xml :

[xml]

<?xml version="1.0" encoding="UTF-8"?>

<nifty xmlns="http://nifty-gui.sourceforge.net/nifty.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://nifty-gui.sourceforge.net/nifty.xsd http://nifty-gui.sourceforge.net/nifty.xsd">

<screen id="start">

<layer id="background" backgroundColor="#000f">

</layer>

<layer id="foreground" backgroundColor="#0000" childLayout="vertical">

<panel id="panel_top" height="25%" width="75%" align="center" childLayout="center"

backgroundColor="#f008">

</panel>

<panel id="panel_mid" height="50%" width="75%" align="center" childLayout="center"

backgroundColor="#0f08">

</panel>

<panel id="panel_bottom" height="25%" width="75%" align="center" childLayout="horizontal"

backgroundColor="#00f8">

<panel id="panel_bottom_left" height="50%" width="50%" valign="center" childLayout="center"

backgroundColor="#44f8">

</panel>

<panel id="panel_bottom_right" height="50%" width="50%" valign="center" childLayout="center"

backgroundColor="#88f8">

</panel>

</panel>

</layer>

</screen>



<screen id="hud">

</screen>

</nifty>

[/xml]





My questions :

  • Is there a plugin for eclipse JME3?
  • I dont understand how it tells JME3 that it must display the first screen. Explain to me plz
  • I haven’t tried it without the gui Nifty XML. What is the best practice (XML or Java )?



    Thanks my friends
- Is there a plugin for eclipse JME3?

Can't tell much about this one, but I believe officially no.

I dont understand how it tells JME3 that it must display the first screen. Explain to me plz

The screen with id=start always shows first unless you specify something else. Somewhere in the bible it was covered.

I haven’t tried it without the gui Nifty XML. What is the best practice (XML or Java )?

Well.. it's kind of that is better for you. I like better to use XML rather than write in java.

thank you for your quick response.



I specify id screen start :

[java]

nifty.fromXml(this.getClass().getResource(“GameGui.xml”).getPath(), “start”);

[/java]



and my XML :

[xml]

<screen id=“start”>



</screen>

[/xml]



But i don’t see the background colors even in debug mode. Am I that bad?

@thibaud-ecarot said:
I do not use the SDK Jmonkey but Eclipse only.
[...]
- Is there a plugin for eclipse JME3?

:facepalm:

Hello normen,



I need an explanation of the best working methods. I use eclipse where I put in Library path JME. I have a project where there are JAVA classes like :

  • Person
  • Skill

    … for example.



    Person and skill are business class. I have a class for describe the Person in 3D = Person3D class



    This is also why I ask you questions. I want to do things correctly. JME SDK should be used only to model objects? Or is it more?



    Do not laugh at me and explain me things.



    Thanks friends
@thibaud-ecarot said:
This is also why I ask you questions. I want to do things correctly. JME SDK should be used only to model objects? Or is it more?


It is a complete development environment. You write your java-code in it and it helps you configure things, like loading assets such as GUI files. It also helps you compose your scene. The only thing it does NOT is model objects (for that you can use for example blender).

Have a look here: http://jmonkeyengine.com/sdk/

I am not here to help you, only to make comments and post hidden video links, sorry. The documentation contains answers to all your problems though, what you want to do is basic and has been done by many people (using the SDK or Eclipse as IDE).

I’m just asking if I have a problem in my code and you do not answer me. I do not think there is one in el JMonkey SDK Plugin Maven and SVN. But I will test.



It’s not smart to answer as you do to people and it is not as constructive exchange. I’ll manage alone. If you represent the community JME you’re doing it wrong.



Bye les bouseux.

No, I keep the amount of garbage posts down. If I’m nice for a few weeks theres an unsurmountable amount of posts like “I wanna do a MMO, can you give me some code plz?”



Are you sure the xml file is available in that package? Read the log of the application, if something goes wrong its written in the log. My guess is you are missing the package name for that file.



…wait, so you are using Eclipse with the maven repo? LOL We cannot give support for that, anything could happen and we wouldn’t know why. The maven repo is not updated by us and nobody uses Eclipse here.

Ok normen.



I do not pretend to an MMO I just want to test JME and Nifty GUI. Tonight I will test again and I would describe the solutions if I find one. I wanted to implement the Diamond-Square algorithm but there is already a function in JME. I use a SVN repository and Maven yes.



If you see no error in my code I do not know why this has occurred.



this.getClass().getResource(“GameGui.xml”).getPath() return /home/thibaud/workspace/testjme/src/com/test/gui/GameGui.xml



thank you for your attention anyway

Try using the SDK. Assuming you get it working painlessly in that (which 99% of us did) then you can compare what that is doing with what your eclipse is doing.



We don’t know how you have set up your eclipse project, what libraries you have, environment, etc…so it’s virtually impossible for us to help with eclipse project setup problems. That’s why the default answer is going to be “use the SDK” - which is a fully featured IDE based on Netbeans.

Ok chief. I will test tonight and I will return on my experience.



Thanks.

http://www.youtube.com/watch?feature=player_embedded&v=XglN7yZ-Njo



Here's to you! If you want to use CryEngine, you go with it's SDK. If you want to use Unreal, you go with UDK. Why you want to go with jME, and don't want to use it's SDK?

try “com/test/gui/GameGui.xml” and don’t try to get the path, you try to load stuff from the classpath here.

If you’re experienced with Eclipse, there’s no problem using it with jME, but if you’re having problems due to using Eclipse or if you have no problems running the SDK (which is based on the IDE NetBeans), then it would probably make your work easier if you use the SDK instead.

I use primarily Eclipse for personal reasons and I know it works without problems with jME (I’ve been using it with both jME2, jME3 and lots of other libraries), but I suggest that you try using the SDK first.



As for your actual problem with Nifty, there are a few items in your code above that might be problematic:

  • You SimpleApplication class implements ScreenController, suggesting that you would want this class to be a screen controller to some screen in your xml, but there is no controller defined for any screen.
  • If you were to define that class as controller for a screen in the xml, nifty would instantiate another SimpleApplication, which would not be advisable.
  • The following path:

    [java]this.getClass().getResource(“GameGui.xml”).getPath() return /home/thibaud/workspace/testjme/src/com/test/gui/GameGui.xml[/java]

    is probably not in your class path. Try putting “com/test/gui/GameGui.xml” there instead.

    EDIT: normen was 9 seconds faster!



    I’d suggest using a separate ScreenController (if you want similar functionality to the SimpleApplication, you could make it an AppState as well) and passing your instance in to the fromXml method as last argument. Then Nifty won’t instantiate a new instance.

Hello all,



After testing with Eclipse everything works perfectly. This is my source code so you can tell me if it’s perfect or not.



Main.java :

[java]

package com.socialempire.client.main;



import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.niftygui.NiftyJmeDisplay;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;

import com.jme3.system.AppSettings;

import com.socialempire.client.gui.Etat;

import com.socialempire.client.gui.GameGui;

import com.socialempire.client.gui.LoadingScreen;



import de.lessvoid.nifty.Nifty;



public class Main extends SimpleApplication {

private GameGui startScreen;

public Etat etat;

public LoadingScreen test;



public Main() {

//Initialisation des etats

this.etat = Etat.STARTMENU;



this.test = new LoadingScreen();

}



/**

*

  • @param args

    */

    public static void main (String[] args){

    AppSettings settings = new AppSettings(true);

    settings.setResolution(1000, 600);



    Main app = new Main();

    app.setShowSettings(false);

    app.setSettings(settings);

    app.start();

    }



    @Override

    public void simpleInitApp() {



    Box b = new Box(Vector3f.ZERO, 1, 1, 1);

    Geometry geom = new Geometry("Box", b);

    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    mat.setColor("Color", ColorRGBA.Blue);

    geom.setMaterial(mat);

    rootNode.attachChild(geom);



    startScreen = new GameGui(this);

    stateManager.attach(startScreen);



    /**
  • Activate the Nifty-JME integration:

    /

    NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(

    assetManager, inputManager, audioRenderer, guiViewPort);

    Nifty nifty = niftyDisplay.getNifty();

    guiViewPort.addProcessor(niftyDisplay);

    nifty.fromXml("Interface/GameGui.xml", "start", startScreen);

    //nifty.setDebugOptionPanelColors(true);



    flyCam.setDragToRotate(true); // you need the mouse for clicking now

    }



    }



    [/java]



    GameGui.java

    [java]

    package com.socialempire.client.gui;



    import com.jme3.app.Application;

    import com.jme3.app.state.AbstractAppState;

    import com.jme3.app.state.AppStateManager;

    import com.socialempire.client.main.Main;



    import de.lessvoid.nifty.Nifty;

    import de.lessvoid.nifty.screen.Screen;

    import de.lessvoid.nifty.screen.ScreenController;



    public class GameGui extends AbstractAppState implements ScreenController {

    private Nifty nifty;

    private Application app;

    private Screen screen;

    private Main main;



    public GameGui(Main main) {

    this.main = main;

    }



    @Override

    public void bind(Nifty nifty, Screen screen) {

    this.nifty = nifty;

    this.screen = screen;

    }



    public void changeScreen(String nextScreen) {

    if(nextScreen.equals("loadGame")) {

    this.main.etat = Etat.INLOADGAME;

    }



    this.nifty.gotoScreen(nextScreen);

    }



    public void quitGame() {

    app.stop();

    System.exit(0);

    }



    /
    * jME3 AppState methods */

    @Override

    public void initialize(AppStateManager stateManager, Application app) {

    this.app = app;

    }





    @Override

    public void onEndScreen() {

    // TODO Auto-generated method stub



    }



    @Override

    public void onStartScreen() {

    // TODO Auto-generated method stub



    }



    @Override

    public void update(float tpf) {

    // TODO Auto-generated method stub

    super.update(tpf);



    if(this.main.etat == Etat.INLOADGAME) {

    this.main.test.update();

    }

    }



    }



    [/java]



    LoadingScreen.java

    [java]

    package com.socialempire.client.gui;



    import java.util.Properties;



    import de.lessvoid.nifty.Nifty;

    import de.lessvoid.nifty.controls.Controller;

    import de.lessvoid.nifty.elements.Element;

    import de.lessvoid.nifty.elements.render.TextRenderer;

    import de.lessvoid.nifty.input.NiftyInputEvent;

    import de.lessvoid.nifty.screen.Screen;

    import de.lessvoid.nifty.screen.ScreenController;

    import de.lessvoid.nifty.tools.SizeValue;

    import de.lessvoid.xml.xpp3.Attributes;



    public class LoadingScreen implements ScreenController, Controller {

    private Element progressBarElement;

    private Element progressTextElement;



    @Override

    public void bind(Nifty nifty, Screen screenParam, Element element, Properties parameter, Attributes controlDefinitionAttributes) {

    progressBarElement = element.findElementByName("progress");

    progressTextElement = element.findElementByName("progress-text");

    }



    public void setProgress(final float progressValue) {

    float progress = progressValue;

    if (progress < 0.0f) {

    progress = 0.0f;

    } else if (progress > 1.0f) {

    progress = 1.0f;

    }

    final int MIN_WIDTH = 32;

    int pixelWidth = (int)(MIN_WIDTH + (progressBarElement.getParent().getWidth() - MIN_WIDTH) * progress);

    progressBarElement.setConstraintWidth(new SizeValue(pixelWidth + "px"));

    progressBarElement.getParent().layoutElements();



    String progressText = String.format("%3.0f%%", progress * 100);

    progressTextElement.getRenderer(TextRenderer.class).setText(progressText);

    }





    @Override

    public void init(Properties arg0, Attributes arg1) {

    // TODO Auto-generated method stub



    }



    @Override

    public boolean inputEvent(NiftyInputEvent inputEvent) {

    return false;

    }



    @Override

    public void onFocus(boolean arg0) {

    // TODO Auto-generated method stub



    }



    @Override

    public void onStartScreen() {

    // TODO Auto-generated method stub



    }





    public void update(){

    setProgress(1f);

    }



    @Override

    public void bind(Nifty nifty, Screen screen) {

    progressBarElement = nifty.getScreen("loadGame").findElementByName("progressbar");

    progressTextElement = nifty.getScreen("loadGame").findElementByName("progress-text");

    }



    @Override

    public void onEndScreen() {

    // TODO Auto-generated method stub



    }







    }



    [/java]



    Gamegui.xml :

    [xml]

    <?xml version="1.0" encoding="UTF-8"?>

    <nifty xmlns="http://nifty-gui.sourceforge.net/nifty.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://nifty-gui.sourceforge.net/nifty.xsd http://nifty-gui.sourceforge.net/nifty.xsd">



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

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





    <controlDefinition name="loadingbar" controller="com.socialempire.client.gui.LoadingScreen">

    <image filename="Interface/border.png" childLayout="absolute" imageMode="resize:15,2,15,15,15,2,15,2,15,2,15,15">

    <image id="progressbar" x="0" y="0" filename="Interface/inner.png" width="32px" height="100%" imageMode="resize:15,2,15,15,15,2,15,2,15,2,15,15" />

    </image>

    </controlDefinition>



    <screen id="start" controller="com.socialempire.client.gui.GameGui">

    <layer id="background" childLayout="center">

    <image filename="Interface/backgroundv2.jpg"></image>

    </layer>

    <layer id="foreground" backgroundColor="#0000" childLayout="vertical">

    <panel id="panel_top" height="30%" width="75%" size="32px" align="center" childLayout="center">

    <text text="Social Empire Pre-Alpha 0.1" font="Interface/Fonts/Default.fnt" width="100%" height="100%" />

    </panel>

    <panel id="panel_middle" height="10%" width="75%" align="center" childLayout="center">

    <control name="button" label="Commencer" id="StartButton" align="center" valign="center">

    <interact onClick="changeScreen(loadGame)" />

    </control>

    </panel>

    <panel id="panel_mid" height="10%" width="75%" align="center" childLayout="center">

    <effect>



    </effect>

    <control name="button" label="Quitter" id="QuitButton" align="center" valign="center">

    <interact onClick="quitGame()"/>

    </control>

    </panel>

    <panel id="panel_bottom" height="50%" width="75%" align="center" childLayout="horizontal">

    <text text="" font="Interface/Fonts/Default.fnt" width="100%" height="100%" wrap="true" />

    </panel>

    </layer>

    </screen>



    <screen id="loadGame" controller="com.socialempire.client.gui.GameGui">

    <layer id="loadinglayer" childLayout="center" backgroundColor="#ffffff">

    <panel id = "loadingpanel" childLayout="vertical" align="center" valign="center" height="32px" width="70%">

    <control name="loadingbar" align="center" valign="center" width="100%" height="100%" />

    <control id="loadingtext" name="label" align="center" text=" "/>

    </panel>

    </layer>

    </screen>



    <screen id="game">





    </screen>



    <screen id="credit">





    </screen>



    <screen id="end">





    </screen>

    </nifty>

    [/xml]



    Do you I do things correctly ? Methods binds in LoadingScreen does not work. Is this normal ? I searched the documentation but I can not find. How to size a text element ?



    Thank you for your advice.