Skip to content

Commit

Permalink
Merge pull request #2332 from JNightRider/lwjglx_mouse
Browse files Browse the repository at this point in the history
Fix: Incorrect mouse position on canvas LWJGL3 (Canvas)
  • Loading branch information
yaRnMcDonuts authored Feb 16, 2025
2 parents 5089672 + e38c458 commit d563d9c
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions jme3-desktop/src/main/java/com/jme3/input/awt/AwtMouseInput.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2021 jMonkeyEngine
* Copyright (c) 2009-2024 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down 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 @@ -144,28 +153,21 @@ public void setInputListener(RawInputListener listener) {
public long getInputTimeNanos() {
return System.nanoTime();
}

@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 @@ -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 Down

0 comments on commit d563d9c

Please sign in to comment.