# Interacting with the GUI from Java

1. Nifty GUI Concepts
2. Nifty GUI Best Practices
3. Nifty GUI XML Layout or Nifty GUI Java Layout
4. Nifty GUI Overlay or Nifty GUI Projection
5. Nifty GUI Java Interaction

In the previous parts of the tutorial, you created a two-screen user interface. But it is still static, and when you click the buttons, nothing happens yet. The purpose of the GUI is to communicate with your Java classes: Your game needs to know what the users clicked, which settings they chose, which values they entered into a field, etc. Similarly, the user needs to know what the currently game state is (score, health, etc).

## Connect GUI to Java Controller

To let a Nifty screen communicate with the Java application, you register a `ScreenController` to every NiftyGUI screen. You create a ScreenController by creating a Java class that implements the `de.lessvoid.nifty.screen.ScreenController` interface and its abtract methods.

Pro Tip: Since you are writing a jME3 application, you can additionally make the ScreenController class extend the AbstractAppState class! This gives the ScreenController access to the application object and to the update loop!

    package tutorial;

    import com.jme3.app.Application;
    import com.jme3.app.SimpleApplication;
    import com.jme3.app.state.AbstractAppState;
    import com.jme3.app.state.AppStateManager;
    import de.lessvoid.nifty.Nifty;
    import de.lessvoid.nifty.screen.Screen;
    import de.lessvoid.nifty.screen.ScreenController;

    public class MyStartScreen extends AbstractAppState implements ScreenController {

      private Nifty nifty;
      private Screen screen;
      private SimpleApplication app;

      /** custom methods */

      public MyStartScreen(String data) {
        /** Your custom constructor, can accept arguments */
      }

      /** Nifty GUI ScreenControl methods */

      public void bind(Nifty nifty, Screen screen) {
        this.nifty = nifty;
        this.screen = screen;
      }

      public void onStartScreen() { }

      public void onEndScreen() { }

      /** jME3 AppState methods */

      @Override
      public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
        this.app=(SimpleApplication)app;
      }

      @Override
      public void update(float tpf) {
        /** jME update loop! */
      }

    }

The name and package of your custom ScreenController class (here `tutorial.MyStartScreen`) goes into the controller parameter of the respective XML screen it belongs to. For example:

    <nifty>
      <screen id="start" controller="tutorial.MyStartScreen">
          <!-- layer and panel code ... -->
      </screen>
    </nifty>

Or the same in a Java syntax, respectively:

      nifty.addScreen("start", new ScreenBuilder("start") {{
          controller(new tutorial.MyStartScreen())}});

Now the Java class `MyStartScreen` and this GUI screen (`start`) are connected. For this example you can also connect the `hud` screen to MyStartScreen.

## Make GUI and Java Interact

In most cases, you will want to pass game data in and out of the ScreenController. Note that you can pass any custom arguments from your Java class into your ScreenController constructor (`public MyStartScreen(GameData data) {}`).

Use any combination of the three following approaches to make Java classes interact with the GUI.

### GUI Calls a Void Java Method

This is how you respond to an GUI interaction such as clicks in XML GUIs:

1. Add `visibleToMouse="true"` to the parent element!
2. Embed the `<interact />` element into the parent element.
3. Specify the Java methods that you want to call when the users performs certain actions, such as clicking.
   Example: `<interact onClick="startGame(hud)" />`

Or this is how you respond to an GUI interaction such as clicks in Java GUIs:

1. Add `visibleToMouse(true);` to the parent element!
2. Embed one of the `interact???()` elements into the parent element
3. Specify the Java method that you want to call after the interaction.
   Example: `interactOnClick("startGame(hud)");`

In the following example, we call the `startGame()` method when the player clicks the Start button, and `quitGame()` when the player clicks the Quit button.

            <panel id="panel_bottom_left" height="50%" width="50%" valign="center" childLayout="center">
              <control name="button" label="Start" id="StartButton" align="center" valign="center"
              visibleToMouse="true" >
                <interact onClick="startGame(hud)"/>
              </control>
            </panel>

            <panel id="panel_bottom_right" height="50%" width="50%" valign="center" childLayout="center">
              <control name="button" label="Quit" id="QuitButton" align="center" valign="center"
              visibleToMouse="true" >
                <interact onClick="quitGame()"/>
              </control>
            </panel>

Or the same in a Java syntax, respectively:

    control(new ButtonBuilder("StartButton", "Start") {{
      alignCenter();
      valignCenter();
      height("50%");
      width("50%");
      visibleToMouse(true);
      interactOnClick("startGame(hud)");
    }});
    ...

    control(new ButtonBuilder("QuitButton", "Quit") {{
      alignCenter();
      valignCenter();
      height("50%");
      width("50%");
      visibleToMouse(true);
      interactOnClick("quitGame()");
    }});

Back in the MyStartScreen class, you specify what the `startGame()` and `quitGame()` methods do. As you see, you can pass String arguments (here `hud`) in the method call. You also see that you have access to the app object.

    public class MyStartScreen implements ScreenController {
      ...

      /** custom methods */
      public void startGame(String nextScreen) {
        nifty.gotoScreen(nextScreen);  // switch to another screen
        // start the game and do some more stuff...
      }

      public void quitGame() {
        app.stop();
      }

      ...
    }

The startGame() example simply switches the GUI to the `hud` screen when the user clicks Start. Of course, in a real game, you would perform more steps here: Load the game level, switch to in-game input and navigation handling, set a custom `running` boolean to true, attach custom in-game AppStates ??? and lots more.

The quitGame() example shows that you have access to the application `app` object because you made the ScreenController extend AbstractAppState. (If you're creating code from this example, note that you'll need to make sure `app` is initialized before you can successfully call its methods.)

### GUI Gets Return Value from Java Method

When the Nifty GUI is initialized, you can get data from Java. In this example, the Java class `getPlayerName()` in `MyStartScreen` defines the Text that is displayed in the textfield before the words `'s Cool Game`.

First define a Java method in the screen controller, in this example, `getPlayerName()`.

    public class MySettingsScreen implements ScreenController {
      ...
      public String getPlayerName(){
        return System.getProperty("user.name");
      }
    }

Nifty uses `${CALL.getPlayerName()}` to get the return value of the getPlayerName() method from your ScreenController Java class.

    <text text="${CALL.getPlayerName()}'s Cool Game" font="Interface/Fonts/Default.fnt" width="100%" height="100%" />

Or the same in a Java syntax, respectively:

    text(new TextBuilder() {{
      text("${CALL.getPlayerName()}'s Cool Game");
      font("Interface/Fonts/Default.fnt");
      height("100%");
      width("100%");
    }});

You can use this for Strings and numeric values (e.g. when you read settings from a file, you display the results in the GUI) and also for methods with side effects.

### Java Modifies Nifty Elements and Events

You can also alter the appearance and functions of your nifty elements from Java. Make certain that the element that you want to alter has its `id="name"` attribute set, so you can identy and address it.

Here's an example of how to change an image called `playerhealth`:

    // load or create new image
    NiftyImage img = nifty.getRenderEngine().createImage("Interface/Images/face2.png", false);
    // find old image
    Element niftyElement = nifty.getCurrentScreen().findElementByName("playerhealth");
    // swap old with new image
    niftyElement.getRenderer(ImageRenderer.class).setImage(img);

The same is valid for other elements, for example a text label "score":

    // find old text
    Element niftyElement = nifty.getCurrentScreen().findElementByName("score");
    // swap old with new text
    niftyElement.getRenderer(TextRenderer.class).setText("124");

Similarly, to change the onClick() event of an element, create an `ElementInteraction` object:

    Element niftyElement = nifty.getCurrentScreen().findElementByName("myElement");
    niftyElement.getElementInteraction().getPrimary().setOnMouseOver(new NiftyMethodInvoker(nifty, "myCustomMethod()", this));

For this to work, there already needs to be a (possibly inactive) `<interact />` tag inside your xml element:

    <interact onClick="doNothing()"/>

## Next Steps

You're done with the basic Nifty GUI for jME3 tutorial. You can proceed to advanced topics and learn how add controls and effects:

- Nifty GUI Scenarios

gui, documentation, input, control, hud, nifty

view online version