-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mouse pointer moves to centre of canvas on click in OpenGL3 with Swing #2327
Comments
I should have added that I am using FlyByCamera with dragToRotate set to true. This turns out to be relevant. A few things I have learnt so far: The reason for the difference in behaviour between jme3-lwjgl and jme3-lwjgl3 is that they use different instances of com.jme3.input.MouseInput jme3-lwjgl uses an instance of com.jme3.input.lwjgl.LwjglMouseInput (even if running in a Swing window) The unwanted recentering of the mouse cursor that I am seeing happens in com.jme3.input.awt.AwtMouseInput.setCursorVisible.
So every time the mouse cursor is set to not visible the cursor is moved to the centre of the canvas. com.jme3.input.lwjgl.LwjglMouseInput has different code in setCursorVisible: com.jme3.input.awt.AwtMouseInput.recenterMouse has this comment: According to git this Fix has been there since at least 2011. So in summary, the two instances of MouseInput have different ways of "grabbing" the mouse. Secondly...
The interesting thing is that FlyByCamera treats left mouse clicks as ROTATEDRAG events. This seems questionable. A mouse click is not a mouse drag. I can see at least two possible fixes for this issue:
|
Hi, here is a test class to replicate the issue. public class Test_SafeCanvas extends SimpleApplication {
public static void main(String[] args) {
AppSettings settings = new AppSettings(true);
settings.setGammaCorrection(false); /* for lwjgl3 */
settings.setResolution(640, 480);
final Test_SafeCanvas app = new Test_SafeCanvas();
app.setPauseOnLostFocus(false);
app.setSettings(settings);
app.createCanvas();
app.startCanvas(true);
JmeCanvasContext context = (JmeCanvasContext) app.getContext();
Canvas canvas = context.getCanvas();
canvas.setPreferredSize(new Dimension(settings.getWidth(), settings.getHeight()));
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
app.stop();
}
});
frame.getContentPane().add(canvas);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@Override
public void simpleInitApp() {
flyCam.setDragToRotate(true);
flyCam.setMoveSpeed(10f);
viewPort.setBackgroundColor(ColorRGBA.DarkGray);
Box b = new Box(1, 1, 1);
Geometry geom = new Geometry("Box", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
geom.setMaterial(mat);
rootNode.attachChild(geom);
}
} |
… click in OpenGL3 with Swing Modify FlyByCamera to only hide the mouse cursor when the user actually rotates the camera, rather than when the user first presses the left mouse button. When the mouse is clicked, instead of immediately hiding the cursor, a flag (hideCursorOnNextRotate) is set. If and when a rotate is detected, the flag is checked and if true the cursor is hidden and the flag is cleared.
… click in OpenGL3 with Swing Modify FlyByCamera to only hide the mouse cursor when the user actually rotates the camera, rather than when the user first presses the left mouse button. When the mouse is clicked, instead of immediately hiding the cursor, a flag (hideCursorOnNextRotate) is set. If and when a rotate is detected, the flag is checked and if true the cursor is hidden and the flag is cleared.
… click in OpenGL3 with Swing Modify FlyByCamera to only hide the mouse cursor when the user actually rotates the camera, rather than when the user first presses the left mouse button. When the mouse is clicked, instead of immediately hiding the cursor, a flag (hideCursorOnNextRotate) is set. If and when a rotate is detected, the flag is checked and if true the cursor is hidden and the flag is cleared.
I fixed the issue (for me) by modifying FlyByCamera to only hide the cursor when the view is actually rotated. I first tried to fix it by modifying com.jme3.input.awt.AwtMouseInput but couldn't make it work. Also the change required seems more intrusive and more difficult to reason about, |
Hello everyone. It seems that this problem goes beyond simple mouse cursor positioning; It seems that if the flying camera is active this will eventually break the analog inputs. By adding the following code block to the example provided by @capdevon, you can see that the mouse position is always the same (There is a small change in position, but it is minuscule - AnalogListener): inputManager.addListener((ActionListener) (String name, boolean isPressed, float tpf) -> {
Vector2f pos = inputManager.getCursorPosition();
if (isPressed) {
System.out.println("[DOWN] :" + pos);
} else {
System.out.println("[ UP ] :" + pos + "\n");
}
}, "Action");
inputManager.addMapping("Action", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener((AnalogListener) (String name, float value, float tpf) -> {
Vector2f pos = inputManager.getCursorPosition();
System.out.println("[ANALOG] :" + pos);
}, "Analog");
inputManager.addMapping("Analog", new MouseButtonTrigger(MouseInput.BUTTON_LEFT)); [ OUTPUT ]
[ NOTE ] Tests carried out in GNU/Linux, I don't know if this is the same on Windows... |
With JME3.7 we can now embed OpenGL3 canvas in a Swing window. I have noticed an issue with the mouse with this configuration.
Steps to reproduce:
Observed
The mouseDown (i.e. keyPressed=true) will have the correct mouse position.
By the time the mouseUp event is processed (i.e. keyPressed=false), the position of the mouse has been moved to the centre of the canvas.
Notes:
A workaround (more a hack) is to
The text was updated successfully, but these errors were encountered: