Hello monkeys! I’ve already got lots of help from this forum as a passive reader, thanks for that. Now I finally made an account here because i can’t find anything useful on this topic.
I’m working on a RTS and I use ChaseCamera for zooming and controlling the viewing angle.
Zooming is bound to the mouse wheel. The ChaseCamera receives events as AnalogListener from InputManager.
I’m using Nifty and its Window controls for the higher-level parts of my GUI. Inside each window, there’s a ScollPanel control which also reacts on mouse wheel input.
My issue basically is: When I scroll a ScrollPanel it also zooms the ChaseCamera.
But I think it’s not particularly related to ScrollPanel and ChaseCamera. I guess the problem boils down to the MouseWheel events not being consumed by Nifty so they are forwarded to the InputManager.
Here’s a simple test case. It listens on mouse click and wheel events via InputManager. Nifty should catch them when the cursor is inside the red square. Clicks are consumed by Nifty, as expected.
package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.MouseAxisTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.niftygui.NiftyJmeDisplay;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
public class NiftyMouseWheelTest extends SimpleApplication implements AnalogListener {
public static class TestScreenController implements ScreenController {
public void bind(Nifty nifty, Screen screen) {}
public void onStartScreen() {}
public void onEndScreen() {}
public boolean onMouseWheel() {
System.out.println("Nifty onMouseWheel()");
return true; // Set consumed? Doesn't work no matter what i return
}
public void onClick() {
System.out.println("Nifty onClick()");
// This one gets consumed
}
}
@Override
public void simpleInitApp() {
flyCam.setEnabled(false);
// Input
inputManager.addMapping("MouseWheelUp", new MouseAxisTrigger(MouseInput.AXIS_WHEEL, false));
inputManager.addMapping("MouseWheelDown", new MouseAxisTrigger(MouseInput.AXIS_WHEEL, true));
inputManager.addMapping("Click", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(this, "MouseWheelUp", "MouseWheelDown", "Click");
// Nifty
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);
niftyDisplay.getNifty().fromXml("Interface/MouseWheelTest.xml", "start");
guiViewPort.addProcessor(niftyDisplay);
}
public void onAnalog(String name, float value, float tpf) {
System.out.println("Analog: " + name);
}
public static void main(String[] args) {
NiftyMouseWheelTest app = new NiftyMouseWheelTest();
app.setShowSettings(false);
app.start();
}
}
I couldn’t find a way to set interact/onMouseWheel through an ElementBuilder.
So it has to use this XML (Assets/Interface/MouseWheelTest.xml):
<?xml version="1.0" encoding="UTF-8"?>
<nifty>
<screen id="start" controller="mygame.NiftyMouseWheelTest$TestScreenController">
<layer id="GLayer0" childLayout="center">
<panel width="100px" height="100px" backgroundColor="#f00f" visibleToMouse="true">
<interact onMouseWheel="onMouseWheel()" onClick="onClick()"/>
</panel>
</layer>
</screen>
</nifty>
sgold suggested a workaround here: Filtering out AXIS_WHEEL events handled by a Nifty HUD
But that’s very cumbersome and I’d rather use a more elegant solution if there is one.
How can i consume MouseWheel events in Nifty?
Version:
jMonkeyEngine 3.1-alpha1 (c5c893f, built on 2015-08-17),
Nifty 1.4.1 (2015-01-25 13:48:55)