Nifty GUI Button touch listener

Hello, I’m making a game on android and have a little problem. Nifty GUI Button doesn’t react to any touch input (like taping with two fingers in one time and so on) and doesn’t invoke method it should. Maybe there are any other commands instead of onClick? Here are some code related with Nifty GUI:

Main:

    //...

 private void initNiftyGui(){
    niftyDisplay = new NiftyJmeDisplay(StaticAssetManager.getAssetManager(),
            inputManager,
            audioRenderer,
            guiViewPort);
    guiViewPort.addProcessor(niftyDisplay);
}

Lifecycle:

public class TestLifecycle extends Lifecycle implements ScreenController {

private CameraControl cameraControl;
private GUI gui;
//...

public TestLifecycle(SimpleApplication simpleApplication) {
    super(simpleApplication);
    loadModels();
    cameraControl = new CameraControl(simpleApplication.getCamera(), simpleApplication.getInputManager());
    Level level = LevelFactory.getLevel(0);
    gameLogicProcessor = new GameLogicProcessor();
    worldMap = new WorldMap(level.getWorldObjects());
    mapRenderer = new MapRenderer(simpleApplication.getRootNode(), 1f, simpleApplication.getCamera());
    gui = new GUI(simpleApplication.getGuiNode());
    worldLight = new WorldLight(simpleApplication.getRootNode(), new Vector3f(-1f, -2f, 0.1f)/*simpleApplication.getCamera().getDirection()*/);
    mapRenderer.addTerrain(new Terrain(5));
    selectToolManager = new SelectToolManager(worldMap, mapRenderer, simpleApplication.getRootNode(), cameraControl);
    worldMap.addObserver(mapRenderer);
    worldMap.addObserver(gameLogicProcessor);
    worldMap.addObserver(selectToolManager);

    //....

@Override
public void update() {
    mapRenderer.onUpdate();
    worldLight.onUpdate();
    worldMap.onUpdate();
    gui.onUpdate();
    gameLogicProcessor.onUpdate();
    cameraControl.onUpdate();
    selectToolManager.onUpdate();
}

//...

private void niftyTest(){
    Element niftyElement = gui.nifty.getCurrentScreen().findElementByName("text");
    niftyElement.getRenderer(TextRenderer.class).setText("124");
}

public void addTree(){
    Tree tree = new Tree(getCenterPoint(1));
    worldMap.put(tree);
}

Gui:

    public class GUI {
private Node guiNode;
public static final int CLICK_OFFSET = 5;
public Nifty nifty;

public GUI(Node guiNode){
    this.guiNode = guiNode;
    nifty = Main.niftyDisplay.getNifty();
    nifty.fromXml("Interface/HelloJme.xml", "start");
}

public void onUpdate(){
}
}

And XML file:

<?xml version="1.0" encoding="UTF-8"?>
<nifty xmlns="http://nifty-gui.sourceforge.net/nifty-1.3.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd">
<useStyles filename="nifty-default-styles.xml" />
<useControls filename="nifty-default-controls.xml" />

<screen id="start" controller="tk.ubublik.redivansion.gamelogic.lifecycle.TestLifecycle">
    <layer id="background" childLayout="center">
    </layer>
    <layer id="foreground" childLayout="vertical">
        <panel id="buttontest" color="#FF0F" height="25%" width="25%" align="left" valign="top" childLayout="center">
            <control name="button" label="Add tree" id="addTree" height="100%" width="100%" align="center" valign="center" visibleToMouse="true" >
            <interact onClick="niftyTest()"/></control>
        </panel>
        <panel id="text" backgroundColor="#5728" height="25%" width="25%" align="left" valign="top" childLayout="center">
            <text name="text" text="My Cool Game" font="Interface/Fonts/Default.fnt" width="100%" height="100%" />
        </panel>
    </layer>
</screen>

The button and text appears on android device screen, but pressing on the button (with onClick=“niftyTest()” or “addTree()”) gives nothing.

Also, one another question — when I place finger on a panel (with text or button) and move it, the camera is moving too. Is it possible to make camera stop while user is interacting with GUI?

Thanks!

If youre using the default fly cam then you need to call flyCam.setDragToRotate(false); or just disable the flyCam while in the menus, and that should fix this part of your issue.

I’ve never worked with touch input myself, but maybe the info in this other thread could be of use until someone more knowledgeable in this area responds.

Edit: I also just noticed that you’re registering your events by using < interact onClick=“niftyTest()”/ > in the xml file, which I think is the old way to do it - when I was learning to use nifty, registering events that way never seemed to work, and then after googling around I saw that nifty handles interact events by referencing the button’s IDs in your Screen Controller class.

Try changing your method to add this tag above it and maybe that will fix your problem

 @NiftyEventSubscriber(id="addTree")
 public void addTree(){
     Tree tree = new Tree(getCenterPoint(1));
     worldMap.put(tree);
}