Skip to content
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

Fix: Incorrect mouse position on canvas LWJGL3 (Canvas) #2332

Merged
merged 4 commits into from
Feb 16, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions jme3-desktop/src/main/java/com/jme3/input/awt/AwtMouseInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@
import com.jme3.input.RawInputListener;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent;
import java.awt.*;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
Expand All @@ -64,8 +70,8 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe

private Component component;

private final java.util.List<MouseButtonEvent> eventQueue = new ArrayList<>();
private final java.util.List<MouseButtonEvent> eventQueueCopy = new ArrayList<>();
private final List<MouseButtonEvent> eventQueue = new ArrayList<>();
private final List<MouseButtonEvent> eventQueueCopy = new ArrayList<>();

private int lastEventX;
private int lastEventY;
Expand All @@ -79,6 +85,7 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
private Point centerLocation;
private Point centerLocationOnScreen;
private Point lastKnownLocation;
private Point grabLocation;
private boolean isRecentering;
private boolean cursorMoved;
private int eventsSinceRecenter;
Expand All @@ -88,6 +95,7 @@ public AwtMouseInput() {
centerLocation = new Point();
centerLocationOnScreen = new Point();
lastKnownLocation = new Point();
grabLocation = new Point();

try {
robot = new Robot();
Expand All @@ -111,6 +119,7 @@ public void setInputSource(Component comp) {
lastEventY = 0;
lastEventWheel = 0;
location = new Point();
grabLocation = new Point();
centerLocation = new Point();
centerLocationOnScreen = new Point();
lastKnownLocation = new Point();
Expand Down Expand Up @@ -147,25 +156,18 @@ public long getInputTimeNanos() {

@Override
public void setCursorVisible(boolean visible) {
// if(JmeSystem.getPlatform() != Platform.MacOSX32 &&
// JmeSystem.getPlatform() != Platform.MacOSX64 &&
// JmeSystem.getPlatform() != Platform.MacOSX_PPC32 &&
// JmeSystem.getPlatform() != Platform.MacOSX_PPC64){
if (this.visible != visible) {
lastKnownLocation.x = lastKnownLocation.y = 0;

grabLocation.x = lastKnownLocation.x;
grabLocation.y = lastKnownLocation.y;

this.visible = visible;
final boolean newVisible = visible;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
component.setCursor(newVisible ? null : getTransparentCursor());
if (!newVisible) {
SwingUtilities.invokeLater(() -> {
component.setCursor(newVisible ? null : getTransparentCursor());
if (!newVisible) {
recenterMouse(component);
}
}
});
// }
}
}

Expand Down Expand Up @@ -283,7 +285,7 @@ public void mouseDragged(MouseEvent awtEvt) {

@Override
public void mouseMoved(MouseEvent awtEvt) {
if (isRecentering) {
if (isRecentering) {
// MHenze (cylab) Fix Issue 35:
// As long as the MouseInput is in recentering mode, nothing is done until the mouse is entered in the component
// by the events generated by the robot. If this happens, the last known location is reset.
Expand All @@ -304,7 +306,7 @@ public void mouseMoved(MouseEvent awtEvt) {
}
lastKnownLocation.x = awtEvt.getX();
lastKnownLocation.y = awtEvt.getY();

cursorMoved = true;
}
}
Expand All @@ -314,7 +316,11 @@ private void recenterMouse(final Component component) {
if (robot != null) {
eventsSinceRecenter = 0;
isRecentering = true;
centerLocation.setLocation(component.getWidth() / 2, component.getHeight() / 2);
if (grabLocation.x == 0 && grabLocation.y == 0) {
centerLocation.setLocation(component.getWidth() / 2, component.getHeight() / 2);
} else {
centerLocation.setLocation(grabLocation.x, grabLocation.y);
}
centerLocationOnScreen.setLocation(centerLocation);
SwingUtilities.convertPointToScreen(centerLocationOnScreen, component);
robot.mouseMove(centerLocationOnScreen.x, centerLocationOnScreen.y);
Expand All @@ -341,4 +347,4 @@ private int getJMEButtonIndex(MouseEvent awtEvt) {
@Override
public void setNativeCursor(JmeCursor cursor) {
}
}
}