Skip to content

Commit 797bfd8

Browse files
committed
GlfwMouseInput: scale mouse coords only if Retina AppSetting is true (#1746)
* GlfwMouseInput: scale mouse coords only if Retina AppSetting is true * GlfwMouseInput: some minor code cleanup * GlfwMouseInput: check for scaled content in initCurrentMousePosition()
1 parent c21d836 commit 797bfd8

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java

+26-16
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,31 @@ private static ByteBuffer transformCursorImage(final IntBuffer imageData, final
134134
private boolean cursorVisible;
135135
private boolean initialized;
136136

137+
/**
138+
* temporary storage for GLFW queries
139+
*/
140+
private final float[] xScale = new float[1];
141+
private final float[] yScale = new float[1];
142+
137143
public GlfwMouseInput(final LwjglWindow context) {
138144
this.context = context;
139145
this.cursorVisible = true;
140146
}
141147

142148
private void onCursorPos(final long window, final double xpos, final double ypos) {
143-
float[] xScale = new float[1];
144-
float[] yScale = new float[1];
145-
glfwGetWindowContentScale(window, xScale, yScale);
146-
147-
int xDelta;
148-
int yDelta;
149-
int x = (int) Math.round(xpos * xScale[0]);
150-
int y = (int) Math.round((currentHeight - ypos) * yScale[0]);
149+
int x;
150+
int y;
151+
if (context.isScaledContent()) {
152+
glfwGetWindowContentScale(window, xScale, yScale);
153+
x = (int) Math.round(xpos * xScale[0]);
154+
y = (int) Math.round((currentHeight - ypos) * yScale[0]);
155+
} else {
156+
x = (int) Math.round(xpos);
157+
y = (int) Math.round(currentHeight - ypos);
158+
}
151159

152-
xDelta = x - mouseX;
153-
yDelta = y - mouseY;
160+
int xDelta = x - mouseX;
161+
int yDelta = y - mouseY;
154162
mouseX = x;
155163
mouseY = y;
156164

@@ -249,12 +257,14 @@ private void initCurrentMousePosition(long window) {
249257
double[] y = new double[1];
250258
glfwGetCursorPos(window, x, y);
251259

252-
float[] xScale = new float[1];
253-
float[] yScale = new float[1];
254-
glfwGetWindowContentScale(window, xScale, yScale);
255-
256-
mouseX = (int) Math.round(x[0] * xScale[0]);
257-
mouseY = (int) Math.round((currentHeight - y[0]) * yScale[0]);
260+
if (context.isScaledContent()) {
261+
glfwGetWindowContentScale(window, xScale, yScale);
262+
mouseX = (int) Math.round(x[0] * xScale[0]);
263+
mouseY = (int) Math.round((currentHeight - y[0]) * yScale[0]);
264+
} else {
265+
mouseX = (int) Math.round(x[0]);
266+
mouseY = (int) Math.round(currentHeight - y[0]);
267+
}
258268
}
259269

260270
/**

jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglWindow.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ public abstract class LwjglWindow extends LwjglContext implements Runnable {
140140
protected boolean wasActive = false;
141141
protected boolean autoFlush = true;
142142
protected boolean allowSwapBuffers = false;
143+
/**
144+
* Set true if Retina/HiDPI frame buffer was enabled via AppSettings.
145+
*/
146+
private boolean isScaledContent = false;
143147

144148
public LwjglWindow(final JmeContext.Type type) {
145149

@@ -229,7 +233,9 @@ public void invoke(int error, long description) {
229233
glfwWindowHint(GLFW_SAMPLES, settings.getSamples());
230234
glfwWindowHint(GLFW_STEREO, settings.useStereo3D() ? GLFW_TRUE : GLFW_FALSE);
231235
glfwWindowHint(GLFW_REFRESH_RATE, settings.getFrequency()<=0?GLFW_DONT_CARE:settings.getFrequency());
232-
glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, settings.isUseRetinaFrameBuffer() ? GLFW_TRUE : GLFW_FALSE);
236+
237+
isScaledContent = settings.isUseRetinaFrameBuffer();
238+
glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, isScaledContent ? GLFW_TRUE : GLFW_FALSE);
233239

234240
if (settings.getBitsPerPixel() == 24) {
235241
glfwWindowHint(GLFW_RED_BITS, 8);
@@ -707,6 +713,15 @@ public TouchInput getTouchInput() {
707713
return null;
708714
}
709715

716+
/**
717+
* Test whether Retina/HiDPI frame buffer was enabled via AppSettings.
718+
*
719+
* @return true if enabled, otherwise false
720+
*/
721+
public boolean isScaledContent() {
722+
return isScaledContent;
723+
}
724+
710725
@Override
711726
public void setAutoFlushFrames(boolean enabled) {
712727
this.autoFlush = enabled;

0 commit comments

Comments
 (0)