Access to GUI Inputs

Hello,

I want to work with some user inputs, that means user inputs should be visible in my main java class.



I have the following classes:


  • Main.java (includes main(), simpleInitApp() and simpleUpdate())
  • InputController.java
  • controls.xml (Nifty GUI)



    I need the user inputs in main.java, so how can I access to the inputs? I already used the NiftyEventSubscriber, but I can access to user inputs only in InputController.java, but I need the inputs in main.java, where I would like to work with.



    I need access to the subscriber from the main.java, but I don’t find any solution to do that.



    How can I realise that?

Is your InputController a screenController on the nifty-Screen?



If so you could add a field to your InputController called “Main main”, load this controller from the screen with screen.getScreenController() and set the main-object to the new field.



After that you can direct the input-calls from your controller to the main.

1 Like

Thank you very much for your answer.

Yes, the InputController is a screenController on the nifty screen. But I don’t understand what you mean with “set the main-object to the new field”



Here is my small test example. In this case I want to recognize if the user has clicked on the logo, but I don’t understand what I should do now.



[xml]

<screen id=“start” controller=“package.InputController”>

<layer id=“layer” childLayout=“center”>

<image id=“imageId” filename=“Interface/nifty-logo-150x150.png”

visibleToMouse=“true” />

</layer>

</screen>

</nifty>

[/xml]



[java]

public class InputController extends AbstractAppState

implements ScreenController{

Main main;

public void bind(Nifty nifty, Screen screen) {

this.nifty = nifty;

this.screen = screen;

}



public void onStartScreen() {

}



public void onEndScreen() {

}



@NiftyEventSubscriber(id=“imageId”)

public void onClick(String id, NiftyMousePrimaryClickedEvent event){

}

}

[/java]



[java]

public class Main extends SimpleApplication{



private NiftyJmeDisplay niftyDisplay;

private Nifty nifty;



public static void main(String[] args){



Main app = new Main();

app.start();

}



public void simpleInitApp(){



niftyDisplay = new NiftyJmeDisplay(

assetManager,inputManager,audioRenderer,guiViewPort);

nifty = niftyDisplay.getNifty();

nifty.fromXml(“Interface/controls.xml”, “start”);

guiViewPort.addProcessor(niftyDisplay);

flyCam.setDragToRotate(true);

}



public void simpleUpdate(float tpf){



}



public void onEvent(String string, Object t) {

throw new UnsupportedOperationException(“Not supported yet.”);

}

}

[/java]

After you’ve loaded the xml-File with “.fromXml” you can fetch the screenController you’ve implemented above with “inputController = (InputController)nifty.getScreen(“screenId”).getScreenController();”



Than you can set the field main on the controller with your Application-Object (something like: “inputController.setMain(this)”)



Now your InputController is able to call the methods in your Application-Object.



Also it would be a good idea to fetch every input from your screen in your inputController. It’s not a good idea to try to delegate the input-events to other classes than a controller 'cause you will end up confused very fast.



If you follow this pattern you will always know where your input will be handled.

Or better yet register the screen controller with Nifty (either first using the register call or by supplying it to the from/add xml calls) and then you know what the screen controller is already.