I've just started to use your engine and have a slight problem with the mouse.
I've been working from the JMEDesktop demo and I would like to normally have the engine only use mouselook with the first person camera when the right mouse button is held down.
My problem is that I would like the mouse icon to disappear when I do this than come back when you release the mouse button. The mouse icon comes back but the application still seems to have a grab on the mouse. I can't click off of the jMonkey window (I'm running in windowed mode) and when I close the application I still get some strange mouse issues for a little bit.
The code I'm doing this with looks like:
MouseInput.get().setCursorVisible(true);
InputAction buttonAction = new InputAction() {
public void performAction(InputActionEvent evt) {
if(firstPersonHandler.getKeyboardLookHandler().isEnabled()) {
if(evt.getTriggerPressed()) {
//mouse was pressed - turn on mouselook
firstPersonHandler.getMouseLookHandler().setEnabled(true);
if(MouseInput.get().isCursorVisible()) {
//turn off the mouse cursor
MouseInput.get().setCursorVisible(false);
}
} else {
//mouse was released - turn off mouselook
firstPersonHandler.getMouseLookHandler().setEnabled(false);
if(!MouseInput.get().isCursorVisible()) {
//turn on the mouse cursor
MouseInput.get().setCursorVisible(true);
}
}
}
}
};
If I comment out the setCursorVisible(false) to keep the mouse icon always on I get no problems with the grabbing of the mouse.
It seems the underlying problem is the setGrabbed() function for the mouse. When this is called I get all kinds of trouble with windowed mode.
Just swapping out the cursor works with setNativeCursor() (thanks for that Nodwick).
Now the only problem I get is since nothing is grabbing the mouse when you're mouselooking it will go out of the canvas as you're looking around.
I tried tying in the mouse look to work with an absolute mouse and not a relative mouse but I get strange rotation problems.
Any code out there for an absolute mouse look handler?
Is there a lwjgl option for the awt robot method? Basically so you can move the mouse back to the middle of the screen without having that movement trigger an event. How I was used to doing it.
public void onClick(int button, int x, int y)
{
if(desktop.componentAt(x, Window.getInstance().getHeight() - y) != null) // The y coordinate is 'the other way around' in swing ;)
return;
if(button == 0) // left button
{
Object ob = Map.getInstance().getObject(Window.getInstance().getCameraRay(x, y), false);
if(ob != null)
setSelectedObject(ob);
}
}
public void onMove(int xDelta, int yDelta, int oldX, int oldY)
{
if(!isEnabled())
return;
Below is a simple app to show the problem. This is for windowed mode for a PC (haven't tested on mac - mental note).
If you hit the "m" key things work right - you can mouse look around and then switch and click off the jMe window.
However, when you use the right mouse button to look around and than try to click off of the jMe window it will not leave (or change focus or whatever) to something else on your desktop. Using a right click off the window seems to work if you give it a little bit.
Doesn't seem to have anything to do with JMEDesktop.
Every method I've tried to tie in this switching with a right mouse click has the problem. You can see below I tried doing an action as well as polling mouseInput in update (commented out atm). I also tried using a mouseEventListener as well as an awt listener through the JMEDesktop (which sees the awt events). All had the same result.
public static void main(String[] args) {
MouseProblem app = new MouseProblem();
app.setDialogBehaviour(BaseGame.ALWAYS_SHOW_PROPS_DIALOG);
app.start();
}
protected void simpleUpdate() {
//when you hit the "m" key will switch mouse look off and on along with the cursor
if(KeyBindingManager.getKeyBindingManager().isValidCommand("switch",false)) {
if(firstPerson.getMouseLookHandler().isEnabled()) {
//turn off mouselook
System.out.println("turning mouse look off with keyboard command");
firstPerson.getMouseLookHandler().setEnabled(false);
MouseInput.get().setCursorVisible(true);
} else {
//turn on mouselook
System.out.println("turning mouse look on with keyboard command");
firstPerson.getMouseLookHandler().setEnabled(true);
MouseInput.get().setCursorVisible(false);
}
}
/*
//other method I tried to get right clicking to work with mouse look
if(MouseInput.get().isButtonDown(1)) {
//right clicking - turn on mouse look
if(!firstPerson.getMouseLookHandler().isEnabled()) {
System.out.println("turning on mouselook");
firstPerson.getMouseLookHandler().setEnabled(true);
MouseInput.get().setCursorVisible(false);
}
} else {
//not right clicking - no mouse look
if(firstPerson.getMouseLookHandler().isEnabled()) {
System.out.println("turning off mouselook");
firstPerson.getMouseLookHandler().setEnabled(false);
MouseInput.get().setCursorVisible(true);
}
}*/
}
protected void simpleInitGame() {
//set up the input and firstperson handler - mouse look off by default
MouseInput.get().setCursorVisible(true);
input = new InputHandler();
firstPerson = new FirstPersonHandler(cam,50,1);
firstPerson.getMouseLookHandler().setEnabled(false);
input.addToAttachedHandlers(firstPerson);
//assign the m key to switch mouse look off and on
KeyBindingManager.getKeyBindingManager().add("switch",KeyInput.KEY_M);
//my attempt to get right clicking to tie with mouse look
InputAction buttonAction = new InputAction() {
public void performAction(InputActionEvent evt) {
if(firstPerson.getKeyboardLookHandler().isEnabled()) {
if(evt.getTriggerPressed()) {
//mouse was pressed - turn on mouselook
System.out.println("turning on mouselook");
firstPerson.getMouseLookHandler().setEnabled(true);
MouseInput.get().setCursorVisible(false);
} else {
//mouse was released - turn off mouselook
System.out.println("turning off mouselook");
firstPerson.getMouseLookHandler().setEnabled(false);
MouseInput.get().setCursorVisible(true);
kc5uyn said: However, when you use the right mouse button to look around and than try to click off of the jMe window it will not leave (or change focus or whatever) to something else on your desktop. Using a right click off the window seems to work if you give it a little bit.
I didn't quite understand this, maybe because I'm not a native english speaker. Maybe you could rephrase that?
Run the demo in windowed mode. Right click the mouse and hold the button down. The mouse pointer should go away and you are in mouse look.
Release the mouse and the mouse pointer comes back and mouse look stops. Now try and left click on your desktop, another folder, program, etc. Anywhere but the jMonkey window. You should see the problem.
Now try to right click twice on your desktop or anywhere other than the jMonkey window. Right clicking seems to break whatever grab is there.
Compare that to using the "m" key where it works correctly.
Alright, I saw your problem, but I'm puzzled why this happens in your code and not in mine. Look at the code of my InputHandler above, but keep in mind that's an inner class, so it won't compile straight away.
BTW, I managed to use something like NodeMouseLook in it, but I've re-coded everything myself, because it's easier to manage that way (for me, at least). If you want that code, just tell me, I can post it here.
I have the feeling that the 'mouse grabbing' is done by some part of JME that I haven't used.