[SOLVED] Cursor does not move

I have implemented a handler which uses an AbsoluteMouse but my cursor is stuck in the bottom left corner of the screen and won't move… The code is as follows:


public class Rebedelho extends SimpleGame{

    private RebedelhoHandler input;

    public static void main(String[] args) {
        Rebedelho app=new Rebedelho();
        // Signal to show properties dialog
        app.setConfigShowMode(ConfigShowMode.AlwaysShow);
        app.start();

    }
    protected void simpleInitGame() {
        UniverseBuilder bu = new UniverseBuilder(rootNode);

        //build the player input
        buildInput();
    }

...

    private void buildInput() {
        // Create the input handler
        input = new RebedelhoHandler(display.getRenderer(), rootNode);
    }

}



The Handler code is here:


public class RebedelhoHandler extends InputHandler {

    private AbsoluteMouse mouse;

    public RebedelhoHandler (Renderer renderer, Node rootNode) {
        mouse = new AbsoluteMouse("Mouse Input", DisplaySystem.getDisplaySystem().getWidth(), DisplaySystem.getDisplaySystem().getHeight());
        mouse.registerWithInputHandler( this );
                // Getting a picture for the cursor
        Texture cursorTexture = TextureManager.loadTexture(Rebedelho.class.getClassLoader().getResource(
            "Models/cursor_rebel.png"), Texture.MinificationFilter.Trilinear,
            Texture.MagnificationFilter.Bilinear);

        TextureState ts = renderer.createTextureState();
        ts.setEnabled(true);
        ts.setTexture(cursorTexture);

        BlendState alpha = renderer.createBlendState();
        alpha.setBlendEnabled(true);
        alpha.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
        alpha.setDestinationFunction(BlendState.DestinationFunction.One);
        alpha.setTestEnabled(true);
        alpha.setTestFunction(BlendState.TestFunction.GreaterThan);
        alpha.setEnabled(true);

        mouse.setRenderState(ts);
        mouse.setRenderState(alpha);

   rootNode.attachChild(mouse);

        // Get the position that the mouse is pointing to
        Vector2f screenPos = new Vector2f();
        // HotSpot: top left corner of the figure
        screenPos.set(mouse.getHotSpotPosition().x,
                      mouse.getHotSpotPosition().y);
    }

    public AbsoluteMouse getMouse() {
        return mouse;
    }

    public void MouseAction() {
       ...
    }

}



I would also like to disable the free look feature: how do I do this?

I'm no AbsoluteMouse guru, but there are a few things we've done in our own implementation that I don't see in your code.



For starters, I think you should updateRenderState() on the AbsoluteMouse since you changed render state data.



I'm not sure this is needed, but we also put it in the ortho render queue (we don't want it going into the scene, now do we?): setRenderQueueMode(Renderer.QUEUE_ORTHO);



One last thing I don't see in your code is setting the translation of the new mouse to be close to the screen (and in this example in the middle of the screen: setLocalTranslation(new Vector3f(display.getWidth() / 2, display.getHeight() / 2, 1));



EDIT: You might also want to hide the normal mouse: MouseInput.get().setCursorVisible(false);

I tried all of these but couldn't get any result except for the cursor being immobile in the center of the screen…



The example I followed was the TestAbsoluteMouse.java in the jmetest.input package



public class TestAbsoluteMouse extends SimpleGame {

    private Text text;
    private AbsoluteMouse mouse;

    public static void main(String[] args) {
        TestAbsoluteMouse app = new TestAbsoluteMouse();
        app.setConfigShowMode(ConfigShowMode.AlwaysShow);
        app.start();
    }

    protected void simpleUpdate() {
        text.print("Position: " + mouse.getLocalTranslation().x + " , "
                + mouse.getLocalTranslation().y);
    }

    protected void simpleInitGame() {
        lightState.setEnabled(false);
        display.getRenderer().setBackgroundColor(ColorRGBA.blue.clone());
        mouse = new AbsoluteMouse("Mouse Input", display.getWidth(), display
                .getHeight());
        TextureState cursor = display.getRenderer().createTextureState();
        cursor.setEnabled(true);
        cursor.setTexture(TextureManager.loadTexture(TestAbsoluteMouse.class
                .getClassLoader().getResource("jmetest/data/cursor/testcursor.png"),
                Texture.MinificationFilter.BilinearNearestMipMap,
                Texture.MagnificationFilter.Bilinear));
        mouse.setRenderState(cursor);
        mouse.registerWithInputHandler(input);

        text = Text.createDefaultTextLabel("Text Label", "Testing Mouse");
        text.setLocalTranslation(new Vector3f(1, 60, 0));
        BlendState as1 = display.getRenderer().createBlendState();
        as1.setBlendEnabled(true);
        as1.setSourceFunction(BlendState.SourceFunction.One);
        as1.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceColor);
        as1.setTestEnabled(true);
        as1.setTestFunction(BlendState.TestFunction.GreaterThan);
        mouse.setRenderState(as1);
        rootNode.attachChild(text);
        rootNode.attachChild(mouse);
    }
}



The strange part is that this code does not use anything like updateRenderState() or including the mouse in the RenderQueue...

I think the problem may be that I did not write all this code inside Rebedelho (which extends SimpleGame)... I'll try that now

I inserted the following code in the main class and it worked:


    @Override
    protected void simpleUpdate () {
        // Mouse actions
        //input.MouseAction();
        input.update(0.03f);

    }


I just found it strange that even when I did not override simpleUpdate or Update this input update was not done automatically as it was in the TestAbsoluteMouse example...

Ah, now I see.

Because you're using SimpleGame but using your own inputHandler that isn't updated with SimpleGame you get this problem.

In TestAbsoluteMouse it uses the inputHandler from BaseSimpleGame, which is updated in BaseSimpleGame.update (updateInput();). Since you're not using this inputHandler you have to update it yourself, like in the real setting.

Aah ok, that makes sense…! Thanks!



I still have one problem: when I move the mouse both the cursor and the camera move at the same time… How can I disable camera movement? When I use removeAllActions() in the input handler it stops the cursor movement but not camera movement…



I tried ((FirstPersonHandler) input).getMouseLookHandler().setEnabled(false) but I get an "inconvertible types" message

Have you checked out the HelloMousePick wiki yet?



Here is the wiki: http://www.jmonkeyengine.com/wiki/doku.php/starter:hello_mouse_pick_jme2:hello_mousepick



Nearly every Test class is written in SimpleGame, but the conversion to StandardGame and BaseGame isn’t too difficult.



Hope this helps!  :smiley:

Well, I found the problem… I declared my own inputhandler as input in the beginning of simplegame hoping it would override the existing attribute…

As it didn't, I had to rename this handler from input to input2 and then use

((FirstPersonHandler) input).getMouseLookHandler().setEnabled(false);