diff --git a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/LwjglKeyInput.java b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwKeyInput.java similarity index 95% rename from jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/LwjglKeyInput.java rename to jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwKeyInput.java index a39010c95..4768290d7 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/LwjglKeyInput.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwKeyInput.java @@ -45,9 +45,9 @@ import java.util.logging.Logger; import static org.lwjgl.glfw.GLFW.*; -public class LwjglKeyInput implements KeyInput { +public class GlfwKeyInput implements KeyInput { - private static final Logger logger = Logger.getLogger(LwjglKeyInput.class.getName()); + private static final Logger logger = Logger.getLogger(GlfwKeyInput.class.getName()); private LwjglWindow context; private RawInputListener listener; @@ -55,7 +55,7 @@ public class LwjglKeyInput implements KeyInput { private GLFWKeyCallback keyCallback; private Queue keyInputEvents = new LinkedList(); - public LwjglKeyInput(LwjglWindow context) { + public GlfwKeyInput(LwjglWindow context) { this.context = context; } diff --git a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/LwjglMouseInput.java b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java similarity index 80% rename from jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/LwjglMouseInput.java rename to jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java index 45262ffbc..41e76a29d 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/LwjglMouseInput.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java @@ -50,9 +50,17 @@ import java.util.logging.Logger; import static org.lwjgl.glfw.GLFW.*; -public class LwjglMouseInput implements MouseInput { +/** + * Captures mouse input using GLFW callbacks. It then temporarily stores these in event queues which are processed in the + * {@link #update()} method. Due to some of the GLFW button id's there is a conversion method in this class which will + * convert the GLFW left, middle and right mouse button to JME3 left, middle and right button codes. + * + * @author Daniel Johansson (dannyjo) + * @since 3.1 + */ +public class GlfwMouseInput implements MouseInput { - private static final Logger logger = Logger.getLogger(LwjglMouseInput.class.getName()); + private static final Logger logger = Logger.getLogger(GlfwMouseInput.class.getName()); private LwjglWindow context; private RawInputListener listener; @@ -67,7 +75,7 @@ public class LwjglMouseInput implements MouseInput { private Queue mouseMotionEvents = new LinkedList(); private Queue mouseButtonEvents = new LinkedList(); - public LwjglMouseInput(LwjglWindow context) { + public GlfwMouseInput(final LwjglWindow context) { this.context = context; } @@ -78,7 +86,7 @@ public class LwjglMouseInput implements MouseInput { int xDelta; int yDelta; int x = (int) Math.round(xpos); - int y = (int) Math.round(ypos); + int y = context.getSettings().getHeight() - (int) Math.round(ypos); if (mouseX == 0) { mouseX = x; @@ -94,7 +102,7 @@ public class LwjglMouseInput implements MouseInput { mouseY = y; if (xDelta != 0 || yDelta != 0) { - final MouseMotionEvent mouseMotionEvent = new MouseMotionEvent(x, y * -1, xDelta, yDelta * -1, mouseWheel, 0); + final MouseMotionEvent mouseMotionEvent = new MouseMotionEvent(x, y, xDelta, yDelta, mouseWheel, 0); mouseMotionEvent.setTime(getInputTimeNanos()); mouseMotionEvents.add(mouseMotionEvent); } @@ -115,7 +123,7 @@ public class LwjglMouseInput implements MouseInput { glfwSetMouseButtonCallback(context.getWindowHandle(), mouseButtonCallback = new GLFWMouseButtonCallback() { @Override public void invoke(final long window, final int button, final int action, final int mods) { - final MouseButtonEvent mouseButtonEvent = new MouseButtonEvent(button, action == GLFW_PRESS, mouseX, mouseY); + final MouseButtonEvent mouseButtonEvent = new MouseButtonEvent(convertButton(button), action == GLFW_PRESS, mouseX, mouseY); mouseButtonEvent.setTime(getInputTimeNanos()); mouseButtonEvents.add(mouseButtonEvent); } @@ -131,7 +139,7 @@ public class LwjglMouseInput implements MouseInput { } public int getButtonCount() { - return 2; // TODO: How to determine this? + return GLFW_MOUSE_BUTTON_LAST + 1; } public void update() { @@ -186,4 +194,23 @@ public class LwjglMouseInput implements MouseInput { glfwSetCursor(context.getWindowHandle(), cursor); } } + + /** + * Simply converts the GLFW button code to a JME button code. If there is no match it just returns the GLFW button + * code. Bare in mind GLFW supports 8 different mouse buttons. + * + * @param glfwButton the raw GLFW button index. + * @return the mapped {@link MouseInput} button id. + */ + private int convertButton(final int glfwButton) { + if (glfwButton == GLFW_MOUSE_BUTTON_LEFT) { + return MouseInput.BUTTON_LEFT; + } else if(glfwButton == GLFW_MOUSE_BUTTON_MIDDLE) { + return MouseInput.BUTTON_MIDDLE; + } else if(glfwButton == GLFW_MOUSE_BUTTON_RIGHT) { + return MouseInput.BUTTON_RIGHT; + } + + return glfwButton; + } } diff --git a/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglContext.java b/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglContext.java index 3f6491e5f..86b6a294a 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglContext.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglContext.java @@ -33,8 +33,8 @@ package com.jme3.system.lwjgl; import com.jme3.input.lwjgl.GlfwJoystickInput; -import com.jme3.input.lwjgl.LwjglKeyInput; -import com.jme3.input.lwjgl.LwjglMouseInput; +import com.jme3.input.lwjgl.GlfwKeyInput; +import com.jme3.input.lwjgl.GlfwMouseInput; import com.jme3.renderer.Renderer; import com.jme3.renderer.RendererException; import com.jme3.renderer.lwjgl.LwjglGL; @@ -71,8 +71,8 @@ public abstract class LwjglContext implements JmeContext { protected AppSettings settings = new AppSettings(true); protected Renderer renderer; - protected LwjglKeyInput keyInput; - protected LwjglMouseInput mouseInput; + protected GlfwKeyInput keyInput; + protected GlfwMouseInput mouseInput; protected GlfwJoystickInput joyInput; protected Timer timer; protected SystemListener listener; @@ -123,16 +123,15 @@ public abstract class LwjglContext implements JmeContext { if (JmeSystem.isLowPermissions()) { return; } + if ("LWJGL".equals(settings.getAudioRenderer())) { NativeLibraryLoader.loadNativeLibrary("openal", true); } - if (settings.useJoysticks()) { - //NativeLibraryLoader.loadNativeLibrary("jinput", true); - //NativeLibraryLoader.loadNativeLibrary("jinput-dx8", true); - } + if (NativeLibraryLoader.isUsingNativeBullet()) { NativeLibraryLoader.loadNativeLibrary("bulletjme", true); } + NativeLibraryLoader.loadNativeLibrary("lwjgl", true); } @@ -236,11 +235,7 @@ public abstract class LwjglContext implements JmeContext { createdLock.notifyAll(); } - //if (renderable.get()) { - initContextFirstTime(); - //} else { -// assert getType() == Type.Canvas; -// } + initContextFirstTime(); } public void create() { diff --git a/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglWindow.java b/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglWindow.java index c089bf28d..4315618b1 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglWindow.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglWindow.java @@ -37,12 +37,11 @@ import com.jme3.input.KeyInput; import com.jme3.input.MouseInput; import com.jme3.input.TouchInput; import com.jme3.input.lwjgl.GlfwJoystickInput; -import com.jme3.input.lwjgl.LwjglKeyInput; -import com.jme3.input.lwjgl.LwjglMouseInput; +import com.jme3.input.lwjgl.GlfwKeyInput; +import com.jme3.input.lwjgl.GlfwMouseInput; import com.jme3.system.AppSettings; import com.jme3.system.JmeContext; import com.jme3.system.JmeSystem; -import org.lwjgl.PointerBuffer; import org.lwjgl.Sys; import org.lwjgl.glfw.*; @@ -256,7 +255,6 @@ public abstract class LwjglWindow extends LwjglContext implements Runnable { if (window != 0) { glfwDestroyWindow(window); } - //glfwTerminate(); } catch (Exception ex) { listener.handleError("Failed to destroy context", ex); } @@ -269,8 +267,10 @@ public abstract class LwjglWindow extends LwjglContext implements Runnable { } new Thread(this, THREAD_NAME).start(); - if (waitFor) + + if (waitFor) { waitFor(true); + } } /** @@ -417,14 +417,14 @@ public abstract class LwjglWindow extends LwjglContext implements Runnable { public MouseInput getMouseInput() { if (mouseInput == null) { - mouseInput = new LwjglMouseInput(this); + mouseInput = new GlfwMouseInput(this); } return mouseInput; } public KeyInput getKeyInput() { if (keyInput == null) { - keyInput = new LwjglKeyInput(this); + keyInput = new GlfwKeyInput(this); } return keyInput;