Disabling InputManager keys

Hello,

I have two different modes in my game and one inputManager with a lot of keyinputs registered. I was wondering what is the best way to disable some keys in one mode and re-enable them back in another mode.

So for example if I have the keyboard arrows that control the camera, I want to disable them with a single button, and be able to reenable them back with a different button.

I hope this makes sense. Although it sounds simple, I cannot figure it out.

thanks in advance

http://www.hub.jmonkeyengine.org/wiki/doku.php/jme3:faq

Specifically, this section of the FAQ, I think:
“How can I switch between screens or states?”

hey guys sorry I didn’t get any notifications about your replies. I am very familiar with AppStates and that would be definitely the cleanest desisn but I previously used appStates for things such as bakcground calculations, timer, etc I have never used it with inputManager. I am not asking to show me a pseudocode or anything, just can you elaborate more in a few lines how would I use AppStates in order to have two versions of inputManager mappings?

thanks a lot

App state registers inputs on initialize() removes them on cleanup(). Attach app state to get mappings. Detach app state to clear mappings.

out of curiosity (and I know it’s a bad design), if I cleared the mapings in InputManager and wrote something like:

[java]
//1 clear saved keys
ppa.getVisualizer().getInputManager().clearKeyMappings();

    //2 load old 3D inputManager keys
    ppa.getVisualizer().initializeCommands();
    
    //3 re-register inputManager keys
    ppa.getVisualizer().getInputManager().registerAnalogCommand(new IAnalogCommand() {
        @Override
        public void execute(float value, float tpf) {
            ppa.getVisualizer().getCurrentCamera().moveLeft(value);
        }}, 
        new KeyTrigger(KeyInput.KEY_A));

    ppa.getVisualizer().getInputManager().registerAnalogCommand(new IAnalogCommand() {
        @Override
        public void execute(float value, float tpf) {
            ppa.getVisualizer().getCurrentCamera().moveRight(value);
        }}, 
        new KeyTrigger(KeyInput.KEY_D));


    ppa.getVisualizer().getInputManager().registerAnalogCommand(new IAnalogCommand() {
        @Override
        public void execute(float value, float tpf) {
            ppa.getVisualizer().getCurrentCamera().zoomIn(value/10);
        }}, 
        new KeyTrigger(KeyInput.KEY_S));

    ppa.getVisualizer().getInputManager().registerAnalogCommand(new IAnalogCommand() {
        @Override
        public void execute(float value, float tpf) {
            ppa.getVisualizer().getCurrentCamera().zoomOut(value/10);
        }}, 
        new KeyTrigger(KeyInput.KEY_W));

    ppa.getVisualizer().getInputManager().registerAnalogCommand(new IAnalogCommand() {
        @Override
        public void execute(float value, float tpf) {
            ppa.getVisualizer().getCurrentCamera().moveBackwards(value);
        }}, 
        new KeyTrigger(KeyInput.KEY_Z));

    ppa.getVisualizer().getInputManager().registerAnalogCommand(new IAnalogCommand() {
        @Override
        public void execute(float value, float tpf) {
            ppa.getVisualizer().getCurrentCamera().moveForward(value);
        }}, 
        new KeyTrigger(KeyInput.KEY_Q));
    
    //4 re-register mouse controls
    ppa.getVisualizer().getCamera().registerWithInput(ppa.getVisualizer().getInputManager());
    ppa.getVisualizer().getCamera().setDragToRotate(true);
    ppa.getVisualizer().getInputManager().setCursorVisible(true);[/java]

why would the mouse not working. I can see it but I cannot drag to rotate

update: what are the mappings responsible of the mouse activation (dragToRotate) other than:

[java]
inputManager.addMapping(“FLYCAM_RotateDrag”, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.setCursorVisible(dragToRotate);
[/java]

I tried everything I could think of and I can’t get the mouse to work.

Well, having the mapping is not enough. You also need a listener added somewhere. Maybe the camera is not doing this right or they get cleared at some point because the mappings are removed or something. I cannot really say. Normally it works.

1 Like

but I also tried calling

[java]
ppa.getVisualizer().getCurrentCamera().registerWithInput(ppa.getVisualizer().getInputManager());
[/java]

which adds the mapping to the listener and checked that the mapping is in inputManager too but still the mouse doesn’t work :S

update: more debugging on the code onAction gets called and does exactly the same thing before the map clearing

[java]
public void onAction(String name, boolean value, float tpf) {
if (!enabled)
return;

    if (name.equals("FLYCAM_RotateDrag") && dragToRotate){
        canRotate = value;
        inputManager.setCursorVisible(!value);
    }
}

[/java]

So the only thing not happening is that the mouse is not dragging to rotate. I am really confused why the cam isn’t moving. I also notice the mouse scroll wheel isn’t working as well.

so in summary, if I clear the inputManager using clearMappings() , how can I get the mouse to work again???

Anyone could help on this ? any suggestions?

can someone at least explain to me how does it work? Looking at FlyByCamera.java there’s a method for every keyinput or mouse input (e.g. A, S, Left Button, ) to move, zoom, or do whatever with the camera - GOOD! But how about the DragToRotate() how is that handled?? I tried to debug it and got really confused. esepcially about this part:

[java]
public void onAction(String name, boolean value, float tpf) {
if (!enabled)
return;

    if (name.equals("FLYCAM_RotateDrag") && dragToRotate){
        canRotate = value;
        inputManager.setCursorVisible(!value);
    }
}

[/java]

as I am getting the same exact value before and after I clear and re-assign the mappings but no luck.

In summary, can someone please tell me if I cleared the mappings of the input manager how can I add back the mouse controls (mainly the drag to rotate)? I was able to add it for all the other keys but no luck with the mouse control… I have been stuck on that for more than 2 weeks of non stop trials…

Sorry if I’m not helping you a lot, but dragging is really hard because there’s not support for it.
If fact, it’s divided in several independent steps :

  • mouse-down
  • mouse-move
  • mouse-up

I would have to check the whole code to give you better insight. The fragment you gave is not sufficient.
No more time now, but I will this evening…

I don’t know why it’s not working and I don’t know what part confuses you.

The mapping gets added like:
inputManager.addMapping(“FLYCAM_RotateDrag”, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));

So the left mouse button will drag… but the code you pasted rightfully shows that dragging will only happen when dragToRotate is true.

I don’t know what this means: " I am getting the same exact value before and after I clear and re-assign the mappings but no luck."

What “exact same value”?

I solved it… I had “like u said” been clearing the mappings later on in a different class.

thanks !!!