Nifty button stops working

The camera in my game uses drag to rotate functionality, now the problem is when I rotate my camera the buttons on my HUD stop receiving input from the mouse.



Start game, click buttons - all works. Rotate camera with drag to rotate, attempt to click buttons, no detection.



Was wondering if anyone knew of a reason/solution for this happening? I did a small search through the forums and on google but couldn’t find anything.



If it’s needed I’ll write a quick test case, just let me know.



Thanks,

Nyfinscyf

@nyfinscyf said:
The camera in my game uses drag to rotate functionality, now the problem is when I rotate my camera the buttons on my HUD stop receiving input from the mouse.

Start game, click buttons - all works. Rotate camera with drag to rotate, attempt to click buttons, no detection.

Was wondering if anyone knew of a reason/solution for this happening? I did a small search through the forums and on google but couldn't find anything.

If it's needed I'll write a quick test case, just let me know.

Thanks,
Nyfinscyf


I ran into this exact issue, and I believe I fixed it by dynamically disabling the flyCam (but leaving it's drag enabled) when I interacted with Nifty and enabling it when not. Not sure if your project's architecture would support doing the same thing, though. If you provide the test case code, I'll see if I can come up with a suggestion.

@knucklesandwich

I disable the flyCam for my menu because I noticed that was happening. It fixed the issue for the menu. But, a menu is a different scenario. In-game both need to be active, I could detect the position of the mouse and disable it like that, but that’s kind of crappy.

I’m heading off to bed right now, so I’ll write up a test case tomorrow morning.

Okay, so I wrote a small test. Click the button, text appears on screen using BitmapText. flyCam set to drag to rotate. As soon as you rotate the camera with the mouse the button will stop working. I know flyCam.setEnabled(false) will allow the button to work again but then I can’t control the camera and it doesn’t update it’s positions.



Here is the main class:

[java]

package mygame;



import com.jme3.app.SimpleApplication;

import com.jme3.font.BitmapText;

import com.jme3.font.Rectangle;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.input.controls.MouseButtonTrigger;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.niftygui.NiftyJmeDisplay;

import com.jme3.renderer.RenderManager;

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;

import java.util.logging.Level;

import java.util.logging.Logger;



/**

  • test
  • @author normenhansen

    */

    public class Main extends SimpleApplication implements ScreenController, ActionListener {



    public Nifty m_rNifty;

    private BitmapText m_rText;

    private int m_iClicked;



    public static void main(String[] args) {

    Main app = new Main();

    app.setShowSettings(false);

    app.start();

    }



    @Override

    public void simpleInitApp() {

    // stuff to look at

    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);



    // logging to remove nifty mouse movement spam

    Logger.getLogger("").setLevel(Level.WARNING);



    // drag to rotate camera

    flyCam.setDragToRotate(true);



    // I know I can do this, but then I cant control the camera.

    //flyCam.setEnabled(false);



    // text display to see button clicks working

    m_rText = new BitmapText(guiFont);

    guiNode.attachChild(m_rText);

    m_rText.setLocalTranslation(10.0f, settings.getHeight() - 10.0f, 0);

    m_rText.setBox(new Rectangle(0, 0, settings.getWidth(), settings.getHeight()));

    m_rText.setText("");

    m_iClicked = 0;



    // init nifty

    initNifty();



    inputManager.addMapping("toggleCam", new KeyTrigger(KeyInput.KEY_SPACE));

    inputManager.addListener(this, "toggleCam");

    }



    @Override

    public void simpleUpdate(float tpf) {

    }



    @Override

    public void simpleRender(RenderManager rm) {

    }



    private void initNifty() {

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

    m_rNifty = rNiftyDisplay.getNifty();

    m_rNifty.fromXml("Interface/buttonXML.xml", "start", this);

    guiViewPort.addProcessor(rNiftyDisplay);

    }



    public void bind(Nifty nifty, Screen screen) {

    }



    public void onStartScreen() {

    }



    public void onEndScreen() {

    }



    /**
  • Function called when nifty button is clicked

    */

    public void clicked()

    {

    m_iClicked++;

    if(m_iClicked >= 10) {

    m_iClicked = 0;

    m_rText.setText("");

    }

    else {

    m_rText.setText(m_rText.getText() + “Clicked!n”);

    }

    }



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

    if(name.equals(“toggleCam”) && isPressed)

    {

    flyCam.setEnabled(false == flyCam.isEnabled());

    }

    }

    }

    [/java]



    And here is the xml

    [xml]

    <?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=“mygame.Main”>

    <layer childLayout=“vertical”>

    <panel width=“100%” height=“30%” />

    <panel width=“100%” height=“70%” childLayout=“horizontal”>

    <panel width=“30%” height=“100%” />

    <control id=“clickButton” name=“button” label=“Click Me” focusable=“false”>

    <interact onClick=“clicked()” />

    </control>

    </panel>

    </layer>

    </screen>

    <screen id=“end”>

    </screen>

    </nifty>

    [/xml]



    I’m just not sure how to handle it. I could maybe put a onHover event to the buttons and when that is triggered disable drag to rotate or something and then when it’s not hovering re-enable it. I can’t call setEnabled(false) because then the camera won’t move when it’s updated.
1 Like

Thanks, that will help @void256 or @Momoko_Fan