fix a mouse bug

This commit is contained in:
Tungstend 2023-07-25 01:16:04 +08:00
parent 903557dc6a
commit f39f69f7c4
4 changed files with 42 additions and 46 deletions

View File

@ -1 +1 @@
1690212623003 1690218918755

View File

@ -17,10 +17,10 @@ public class GLFWInputImplementation implements InputImplementation {
private final ByteBuffer keyboardEvent = ByteBuffer.allocate(Keyboard.EVENT_SIZE); private final ByteBuffer keyboardEvent = ByteBuffer.allocate(Keyboard.EVENT_SIZE);
public final byte[] keyDownBuffer = new byte[Keyboard.KEYBOARD_SIZE]; public final byte[] keyDownBuffer = new byte[Keyboard.KEYBOARD_SIZE];
public final byte[] mouseBuffer = new byte[3]; public final byte[] mouseBuffer = new byte[3];
private int last_x; public int mouseX = 0;
private int last_y; public int mouseY = 0;
public int accum_dx; public int mouseLastX = 0;
public int accum_dy; public int mouseLastY = 0;
public boolean grab; public boolean grab;
public boolean correctCursor; public boolean correctCursor;
@ -46,11 +46,12 @@ public class GLFWInputImplementation implements InputImplementation {
@Override @Override
public void pollMouse(IntBuffer coord_buffer, ByteBuffer buttons) { public void pollMouse(IntBuffer coord_buffer, ByteBuffer buttons) {
coord_buffer.put(0, grab ? accum_dx : last_x); coord_buffer.put(0, grab ? mouseX - mouseLastX : mouseX);
coord_buffer.put(1, grab ? accum_dy : last_y); coord_buffer.put(1, grab ? mouseY - mouseLastY : mouseY);
buttons.rewind(); buttons.rewind();
buttons.put(mouseBuffer); buttons.put(mouseBuffer);
accum_dx = accum_dy = 0; mouseLastX = mouseX;
mouseLastY = mouseY;
} }
@Override @Override
@ -61,7 +62,7 @@ public class GLFWInputImplementation implements InputImplementation {
@Override @Override
public void grabMouse(boolean newGrab) { public void grabMouse(boolean newGrab) {
grab = newGrab; grab = newGrab;
correctCursor = grab; correctCursor = newGrab;
GLFW.glfwSetInputMode(Display.getWindow(), GLFW.GLFW_CURSOR, grab ? GLFW.GLFW_CURSOR_DISABLED : GLFW.GLFW_CURSOR_NORMAL); GLFW.glfwSetInputMode(Display.getWindow(), GLFW.GLFW_CURSOR, grab ? GLFW.GLFW_CURSOR_DISABLED : GLFW.GLFW_CURSOR_NORMAL);
} }
@ -136,45 +137,39 @@ public class GLFWInputImplementation implements InputImplementation {
return true; return true;
} }
private int transformY(int y) { public void putMouseEventWithCoords(byte button, byte state, int x, int y, int dz, long nanos) {
return Display.getHeight() - 1 - y; int rebaseX;
} int rebaseY;
if (x == -1 && y == -1) {
public void setCursorPos(int x, int y, long nanos) { rebaseX = mouseX;
y = transformY(y); rebaseY = mouseY;
if (correctCursor) { } else {
last_x = x; rebaseX = x;
last_y = y; rebaseY = y;
correctCursor = false; if (correctCursor) {
return; mouseX = x;
} mouseY = y;
int dx = x - last_x; mouseLastX = mouseX;
int dy = y - last_y; mouseLastY = mouseY;
if (dx != 0 || dy != 0) { correctCursor = false;
accum_dx += dx; return;
accum_dy += dy;
last_x = x;
last_y = y;
if (grab) {
putMouseEventWithCoords((byte)-1, (byte)0, dx, dy, 0, nanos * 1000000);
} else {
putMouseEventWithCoords((byte)-1, (byte)0, x, y, 0, nanos * 1000000);
} }
} }
}
private void putMouseEventWithCoords(byte button, byte state, int x, int y, int z, long nanos) {
eventBuffer.clear(); eventBuffer.clear();
eventBuffer.put(button).put(state).putInt(x).putInt(y).putInt(z).putLong(nanos); eventBuffer.put(button).put(state);
if (grab) {
eventBuffer.putInt(rebaseX - mouseX).putInt(rebaseY - mouseY);
} else {
eventBuffer.putInt(rebaseX).putInt(rebaseY);
}
if (button != -1) {
mouseBuffer[button] = state;
}
eventBuffer.putInt(dz).putLong(nanos);
eventBuffer.flip(); eventBuffer.flip();
eventQueue.putEvent(eventBuffer); eventQueue.putEvent(eventBuffer);
} mouseX = rebaseX;
mouseY = rebaseY;
public void putMouseEvent(byte button, byte state, int dz, long nanos) {
if (grab)
putMouseEventWithCoords(button, state, 0, 0, dz, nanos);
else
putMouseEventWithCoords(button, state, last_x, last_y, dz, nanos);
} }
public void putKeyboardEvent(int keycode, byte state, int ch, long nanos, boolean repeat) { public void putKeyboardEvent(int keycode, byte state, int ch, long nanos, boolean repeat) {
@ -185,3 +180,4 @@ public class GLFWInputImplementation implements InputImplementation {
keyboardEventQueue.putEvent(keyboardEvent); keyboardEventQueue.putEvent(keyboardEvent);
} }
} }

View File

@ -332,14 +332,14 @@ public class Display {
Window.cursorPosCallback = new GLFWCursorPosCallback() { Window.cursorPosCallback = new GLFWCursorPosCallback() {
@Override @Override
public void invoke(long window, double xpos, double ypos) { public void invoke(long window, double xpos, double ypos) {
GLFWInputImplementation.singleton.setCursorPos((int) xpos, (int) ypos, Sys.getNanoTime()); GLFWInputImplementation.singleton.putMouseEventWithCoords((byte) -1, (byte) 0, (int) xpos, (int) ((ypos - Display.getHeight()) * -1), 0, Sys.getNanoTime());
} }
}; };
Window.mouseButtonCallback = new GLFWMouseButtonCallback() { Window.mouseButtonCallback = new GLFWMouseButtonCallback() {
@Override @Override
public void invoke(long window, int button, int action, int mods) { public void invoke(long window, int button, int action, int mods) {
GLFWInputImplementation.singleton.putMouseEvent((byte) button, (byte) action, 0, Sys.getNanoTime()); GLFWInputImplementation.singleton.putMouseEventWithCoords((byte) button, (byte) action, -1, -1, 0, Sys.getNanoTime());
} }
}; };
@ -395,7 +395,7 @@ public class Display {
Window.scrollCallback = new GLFWScrollCallback() { Window.scrollCallback = new GLFWScrollCallback() {
@Override @Override
public void invoke(long window, double xoffset, double yoffset) { public void invoke(long window, double xoffset, double yoffset) {
GLFWInputImplementation.singleton.putMouseEvent((byte) -1, (byte) 0, (int) (yoffset * 120), Sys.getNanoTime()); GLFWInputImplementation.singleton.putMouseEventWithCoords((byte) -1, (byte) 0, -1, -1, (int) (yoffset * 120), Sys.getNanoTime());
} }
}; };