Nifty - Trouble calling method using

I have a Nifty GUI that is working up to the point of recieving input and invoking methods in the screen controller.



my xml looks like this…

[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" controller="com.nat.screencontrollers.MenuScreenController">

<layer id="layer1" backgroundColor="#303030ff" childLayout="center">

<panel height="25%" width="35%" align="center" valign="center" backgroundColor="#ff6030ff"

childLayout="center" visibleToMouse="true">

<text id="text" font="aurulent-sans-16.fnt" color="#000000ff"

text="Single Player Button" align="center" valign="center" />

<interact onClick="sayHello(hi)"/>

</panel>

</layer>

</screen>

</nifty>

[/xml]



And MenuScreenController.java looks like…

[java]

package com.nat.screencontrollers;



import de.lessvoid.nifty.Nifty;

import de.lessvoid.nifty.screen.Screen;

import de.lessvoid.nifty.screen.ScreenController;



/**

*

  • @author Neil Thomson

    */

    public class MenuScreenController implements ScreenController {



    public MenuScreenController(){



    }



    public void bind(Nifty nifty, Screen screen) {



    }



    public void onStartScreen() {



    }



    public void onEndScreen() {



    }



    public void sayHello(String myarg) {

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

    }

    }

    [/java]



    but I get nothing in the console when I click on the panel. No errors no println nothing :frowning:



    Can anyone see if I am making some silly mistakes? Or non silly ones lol?



    Thanks

Interestingly enough, I had the some problem some weeks ago and then it vanished. Do you have the latest nightly build?



Maybe you should expand you class like this

[java]



public class MenuScreenController implements ScreenController {



Nifty nifty;

Screen screen;

public MenuScreenController(){

}

public void bind(Nifty nifty, Screen screen) {

this.nifty = nifty;

this.screen = screen;



}

public void onStartScreen() {

}

public void onEndScreen() {

}

public void sayHello(String myarg) {

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

}

}



[/java]





Try if that works

Thanks for reply, unfortunately no joy, I have updated and tried what you said but to no avail :frowning:



The xml is being read as I can see that in the console, I call it using…

[java]

niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);

/** Create a new NiftyGUI object /

nifty = niftyDisplay.getNifty();

/
* Read your XML and initialize your custom ScreenController */

nifty.fromXml(“Interface/menuscreen.xml”, “start”, new MenuScreenController());

// attach the Nifty display to the gui view port as a processor

guiViewPort.addProcessor(niftyDisplay);

[/java]



and the rest of it is above, I cannot see why this wouldn’t be working, it is such an easy example, one of the simplest lol and I can’t get it to behave :frowning:

Hmmmmm, well I have been playing with this. I think that my Screen Controller is the problem, it is not getting… well… used.



I made these changes…



The niftyGui menuscreen.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" controller="com.nat.screencontrollers.MenuScreenController">

<layer id="layer1" backgroundColor="#303030ff" childLayout="center">

<panel id="panel1" height="25%" width="35%" align="center" valign="center" backgroundColor="#ff6030ff"

childLayout="center" visibleToMouse="true">

<interact onClick="buttonSinglePlayer()"/>

<effect>

<onStartScreen name="move" mode="in" direction="top" length="300" startDelay="0" inherit="true"/>

<onEndScreen name="move" mode="out" direction="bottom" length="300" startDelay="0" inherit="true"/>

<onHover name="pulsate" scaleFactor="0.008" startColor="#f600" endColor="#ffff" post="true"/>

</effect>

<text id="text" font="aurulent-sans-16.fnt" color="#000f" text="Single Player" align="center" valign="center" />

</panel>

</layer>

</screen>

<screen id="end">

</screen>

</nifty>

[/xml]



the bit that creates the gui and starts the screen controller

[java]

niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);

/** Create a new NiftyGUI object /

nifty = niftyDisplay.getNifty();

URL url = Thread.currentThread().getContextClassLoader().getResource("Interface/menuscreen.xml");

/
* Read your XML and initialize your custom ScreenController */

nifty.fromXml("Interface/menuscreen.xml", "start", new MenuScreenController());

// attach the Nifty display to the gui view port as a processor

guiViewPort.addProcessor(niftyDisplay);

[/java]



The screen controller MenuScreenController.java

[java]

package com.nat.screencontrollers;



import de.lessvoid.nifty.Nifty;

import de.lessvoid.nifty.screen.Screen;

import de.lessvoid.nifty.screen.ScreenController;



/**

*

  • @author Neil Thomson

    */

    public class MenuScreenController implements ScreenController {



    Nifty nifty;

    Screen screen;



    public MenuScreenController(){



    }



    public void bind(Nifty nifty, Screen screen) {

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

    this.nifty = nifty;

    this.screen = screen;

    }



    public void onStartScreen() {

    System.out.println(“onStartScreen”);

    }



    public void onEndScreen() {

    System.out.println(“onEndScreen”);

    }



    public void buttonSinglePlayer() {

    System.out.println("SinglePlayer ");

    nifty.gotoScreen(“end”);

    }

    }

    [/java]



    Everything works except the screen controller, I get no printlns occur for any of the methods bind(), onStartScreen(), onEndScreen(), buttonSinglePlayer(). My conclusion can only be that I am not initialising the class MenuScreenController correctly but I do not know why.



    I can see the gui and it pulsates when I mouse over it but the onClick doesn’t work because I believe it cannot see the buttonSinglePlayer() method or anything in fact in the MenuScreenController class. Hmmmz Why might this be?

Below is part of the data in the console, you can see in bold where it initialises the screen controllers, the screen=“end” doesn’t have a controller so this is what I would expect to see if the screen controller was not found. But it seems to be able to find MenuScreenController, I don’t know what @19bfb30 means though 8O



19-Sep-2011 20:43:21 de.lessvoid.nifty.loaderv2.types.ScreenType create

INFO: internal prepare screen (start) [1]

19-Sep-2011 20:43:21 de.lessvoid.nifty.NiftyMethodInvoker

INFO: target objects for [buttonSinglePlayer()]

19-Sep-2011 20:43:21 de.lessvoid.nifty.NiftyMethodInvoker

INFO: com.nat.screencontrollers.MenuScreenController@19bfb30

19-Sep-2011 20:43:21 com.jme3.scene.Node attachChild

INFO: Child (BitmapFont) attached to this node (null)

19-Sep-2011 20:43:21 de.lessvoid.nifty.NiftyStopwatch stop

INFO: [0006] RenderDevice.createFont(aurulent-sans-16.fnt)

19-Sep-2011 20:43:21 de.lessvoid.nifty.loaderv2.types.ScreenType create

INFO: internal create screen (start) [13]

19-Sep-2011 20:43:21 de.lessvoid.nifty.screen.Screen

INFO: Missing ScreenController for screen [end] using DefaultScreenController() instead but this might not be what you want.

19-Sep-2011 20:43:21 de.lessvoid.nifty.loaderv2.types.ScreenType create

INFO: internal prepare screen (end) [0]

19-Sep-2011 20:43:21 de.lessvoid.nifty.loaderv2.types.ScreenType create

INFO: internal create screen (end) [0]

19-Sep-2011 20:43:21 de.lessvoid.nifty.loaderv2.types.NiftyType create

INFO: create Screens [15]

19-Sep-2011 20:43:21 de.lessvoid.nifty.Nifty loadFromFile

I’ve used your XML and ScreenController and it works here - I can see the System.out.println() and the onClick() works as well ><



I’ve tried this with jME3 nightly builds jME3_2011_07_01 and jME3_2011_08_29!



Your example looks good. I can’t find any error at all >
<



Can you send me the whole project? I’m curious what that might be :slight_smile:

I can run the TestNiftyGui test which has a onClick event and it works propperly so think that it is definately something that I am doing wrong :frowning: lol but I cannot see what, there must be more to it, I’ll stick at it I suppose lol

I made this and it works…



[java]

package com.nat.clientapp;



import com.jme3.app.SimpleApplication;

import com.jme3.niftygui.NiftyJmeDisplay;

import com.nat.screencontrollers.MenuScreenController;

import de.lessvoid.nifty.Nifty;

import de.lessvoid.nifty.screen.Screen;



/**

*

  • @author Neil Thomson

    */

    public class NewClass extends SimpleApplication {



    Nifty nifty;

    Screen screen;

    NiftyJmeDisplay niftyDisplay;



    public static void main(String[] args) {

    NewClass app = new NewClass();

    app.start();

    }



    @Override

    public void simpleInitApp() {

    niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);

    nifty = niftyDisplay.getNifty();

    nifty.fromXml("Interface/menuscreen.xml", "start", new MenuScreenController());

    guiViewPort.addProcessor(niftyDisplay);

    flyCam.setEnabled(false);

    }



    }

    [/java]



    My original code uses AppStates though and I still cannot get it to work with that :frowning:

Ok so I have been playing around with this still and basically I am still confused but I have made progress.



Basically my project is set up so that I have AppStates that each create a GUI and then destroy it and themselves before initialising a new AppState. Here is an example of one of my AppStates…



[java]

package com.nat.clientappstates;

import com.jme3.app.Application;

import com.jme3.app.state.AbstractAppState;

import com.jme3.app.state.AppStateManager;

import com.jme3.asset.AssetManager;

import com.jme3.audio.AudioRenderer;

import com.jme3.input.InputManager;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.AnalogListener;

import com.jme3.niftygui.NiftyJmeDisplay;

import com.jme3.renderer.ViewPort;

import com.nat.clientapp.ClientMain;

import com.nat.screencontrollers.SplashScreenController;

import de.lessvoid.nifty.Nifty;

/**

*

  • @author Neil Thomson

    */

    public class SplashScreen extends AbstractAppState implements ActionListener, AnalogListener {

    Application application;

    AssetManager assetManager;

    InputManager inputManager;

    AudioRenderer audioRenderer;

    ViewPort guiViewPort;

    AppStateManager appStateManager;

    ClientMain clientMain;

    NiftyJmeDisplay niftyDisplay;

    Nifty nifty;

    String[] mappings;

    public SplashScreen(ClientMain app) {

    clientMain = app;

    }

    @Override

    public void initialize(AppStateManager stateManager, Application app) {

    application = app;

    appStateManager = stateManager;

    assetManager = application.getAssetManager();

    inputManager = application.getInputManager();

    audioRenderer = application.getAudioRenderer();

    guiViewPort = application.getGuiViewPort();

    initialized = true;

    niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);

    nifty = niftyDisplay.getNifty();

    nifty.fromXml("Interface/splashscreen.xml", "start", new SplashScreenController());

    guiViewPort.addProcessor(niftyDisplay);

    if (inputManager != null) {

    registerWithInput(inputManager);

    }

    }

    public void registerWithInput(InputManager inputManager) {

    mappings = new String[]{

    "MOUSE_BUTTON_LEFT",

    "ESCAPE"

    };

    inputManager.addListener(this, mappings);

    }

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

    if (name.equals("MOUSE_BUTTON_LEFT") && isPressed){

    clientMain.deactivateSplashScreen();

    clientMain.activateLoginScreen();

    } else if (name.equals("ESCAPE") && isPressed) {

    clientMain.deactivateSplashScreen();

    clientMain.activateLoginScreen();

    }

    }

    public void onAnalog(String name, float value, float tpf) {

    }

    @Override

    public void update(float tpf) {

    }

    @Override

    public void cleanup() {

    // cleanup

    initialized = false;

    inputManager.removeListener(this);

    guiViewPort.removeProcessor(niftyDisplay);

    // destroy variables

    application = null;

    assetManager = null;

    inputManager = null;

    audioRenderer = null;

    guiViewPort = null;

    niftyDisplay = null;

    nifty = null;

    mappings = null;

    }

    }

    [/java]



    I have three App States in total at the moment, the a SplashScreen a LoginScreen and a MenuScreen each with their respective ScreenControllers and nifty .xml file.



    I changed the above SplashScreen class so that it used the MenuScreenController() and menuscreen.xml and low and behold it worked on the splash screen, I was able to see clicks outputting strings to the console!!! I changed to the LoginScreen which is just a box with no interact tag and it looked ok… but I clicked the box… and it outputted strings to the console!!! It is not supposed to, so I think that the initial ScreenController still lived!!! OH NO!!! So I think that this may be the problem!



    I then went to the last screen and it worked but I think that it was still the first ScreenController that was in action. I thought that I was killing the NiftyGui in the cleanup() method but maybe not?!?!?!



    What does anyone think?

Urgh, I’m an idiot lol I have just fixed it.


Neilos said:
I thought that I was killing the NiftyGui in the cleanup() method but maybe not?!?!?!


No I wasn't. It took me several millions of picoseconds to fix (after several hours of head-butting walls!)... dun dun dun...

[java]
nifty.exit();
[/java]

needed to be called in the cleanup() method.

#@#£$%%$ and $*){£@%%"*&!!!!!
Neilos said:
Urgh, I'm an idiot lol I have just fixed it.



No I wasn't. It took me several millions of picoseconds to fix (after several hours of head-butting walls!)... dun dun dun...

[java]
nifty.exit();
[/java]

needed to be called in the cleanup() method.

#@#£$%%$ and $*){£@%%"*&!!!!!


@Neilos:

In your earlier problem, aside from the event clicking and the colored boxes, could the label/texts/images be displayed finely at that time? Because I'm having this issue now after testing about and searching around but no answers to my problems yet.