Skip to content

Commit d563d9c

Browse files
authored
Merge pull request #2332 from JNightRider/lwjglx_mouse
Fix: Incorrect mouse position on canvas LWJGL3 (Canvas)
2 parents 5089672 + e38c458 commit d563d9c

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

jme3-desktop/src/main/java/com/jme3/input/awt/AwtMouseInput.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2021 jMonkeyEngine
2+
* Copyright (c) 2009-2024 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -36,10 +36,16 @@
3636
import com.jme3.input.RawInputListener;
3737
import com.jme3.input.event.MouseButtonEvent;
3838
import com.jme3.input.event.MouseMotionEvent;
39-
import java.awt.*;
39+
40+
import java.awt.Component;
41+
import java.awt.Cursor;
42+
import java.awt.Point;
43+
import java.awt.Robot;
44+
import java.awt.Toolkit;
4045
import java.awt.event.*;
4146
import java.awt.image.BufferedImage;
4247
import java.util.ArrayList;
48+
import java.util.List;
4349
import java.util.logging.Level;
4450
import java.util.logging.Logger;
4551
import javax.swing.SwingUtilities;
@@ -64,8 +70,8 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
6470

6571
private Component component;
6672

67-
private final java.util.List<MouseButtonEvent> eventQueue = new ArrayList<>();
68-
private final java.util.List<MouseButtonEvent> eventQueueCopy = new ArrayList<>();
73+
private final List<MouseButtonEvent> eventQueue = new ArrayList<>();
74+
private final List<MouseButtonEvent> eventQueueCopy = new ArrayList<>();
6975

7076
private int lastEventX;
7177
private int lastEventY;
@@ -79,6 +85,7 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
7985
private Point centerLocation;
8086
private Point centerLocationOnScreen;
8187
private Point lastKnownLocation;
88+
private Point grabLocation;
8289
private boolean isRecentering;
8390
private boolean cursorMoved;
8491
private int eventsSinceRecenter;
@@ -88,6 +95,7 @@ public AwtMouseInput() {
8895
centerLocation = new Point();
8996
centerLocationOnScreen = new Point();
9097
lastKnownLocation = new Point();
98+
grabLocation = new Point();
9199

92100
try {
93101
robot = new Robot();
@@ -111,6 +119,7 @@ public void setInputSource(Component comp) {
111119
lastEventY = 0;
112120
lastEventWheel = 0;
113121
location = new Point();
122+
grabLocation = new Point();
114123
centerLocation = new Point();
115124
centerLocationOnScreen = new Point();
116125
lastKnownLocation = new Point();
@@ -144,28 +153,21 @@ public void setInputListener(RawInputListener listener) {
144153
public long getInputTimeNanos() {
145154
return System.nanoTime();
146155
}
147-
156+
148157
@Override
149158
public void setCursorVisible(boolean visible) {
150-
// if(JmeSystem.getPlatform() != Platform.MacOSX32 &&
151-
// JmeSystem.getPlatform() != Platform.MacOSX64 &&
152-
// JmeSystem.getPlatform() != Platform.MacOSX_PPC32 &&
153-
// JmeSystem.getPlatform() != Platform.MacOSX_PPC64){
154159
if (this.visible != visible) {
155-
lastKnownLocation.x = lastKnownLocation.y = 0;
160+
grabLocation.x = lastKnownLocation.x;
161+
grabLocation.y = lastKnownLocation.y;
156162

157163
this.visible = visible;
158164
final boolean newVisible = visible;
159-
SwingUtilities.invokeLater(new Runnable() {
160-
@Override
161-
public void run() {
162-
component.setCursor(newVisible ? null : getTransparentCursor());
163-
if (!newVisible) {
165+
SwingUtilities.invokeLater(() -> {
166+
component.setCursor(newVisible ? null : getTransparentCursor());
167+
if (!newVisible) {
164168
recenterMouse(component);
165-
}
166169
}
167170
});
168-
// }
169171
}
170172
}
171173

@@ -314,7 +316,11 @@ private void recenterMouse(final Component component) {
314316
if (robot != null) {
315317
eventsSinceRecenter = 0;
316318
isRecentering = true;
317-
centerLocation.setLocation(component.getWidth() / 2, component.getHeight() / 2);
319+
if (grabLocation.x == 0 && grabLocation.y == 0) {
320+
centerLocation.setLocation(component.getWidth() / 2, component.getHeight() / 2);
321+
} else {
322+
centerLocation.setLocation(grabLocation.x, grabLocation.y);
323+
}
318324
centerLocationOnScreen.setLocation(centerLocation);
319325
SwingUtilities.convertPointToScreen(centerLocationOnScreen, component);
320326
robot.mouseMove(centerLocationOnScreen.x, centerLocationOnScreen.y);

0 commit comments

Comments
 (0)