190 lines
6.9 KiB
Java
190 lines
6.9 KiB
Java
|
/*
|
||
|
* Copyright (c) 2009-2012 jMonkeyEngine
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* Redistribution and use in source and binary forms, with or without
|
||
|
* modification, are permitted provided that the following conditions are
|
||
|
* met:
|
||
|
*
|
||
|
* * Redistributions of source code must retain the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer.
|
||
|
*
|
||
|
* * Redistributions in binary form must reproduce the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer in the
|
||
|
* documentation and/or other materials provided with the distribution.
|
||
|
*
|
||
|
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
|
||
|
* may be used to endorse or promote products derived from this software
|
||
|
* without specific prior written permission.
|
||
|
*
|
||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
*/
|
||
|
|
||
|
package com.jme3.input.lwjgl;
|
||
|
|
||
|
import com.jme3.cursors.plugins.JmeCursor;
|
||
|
import com.jme3.input.MouseInput;
|
||
|
import com.jme3.input.RawInputListener;
|
||
|
import com.jme3.input.event.MouseButtonEvent;
|
||
|
import com.jme3.input.event.MouseMotionEvent;
|
||
|
import com.jme3.system.lwjgl.LwjglTimer;
|
||
|
import com.jme3.system.lwjgl.LwjglWindow;
|
||
|
import org.lwjgl.glfw.GLFWCursorPosCallback;
|
||
|
import org.lwjgl.glfw.GLFWMouseButtonCallback;
|
||
|
import org.lwjgl.glfw.GLFWScrollCallback;
|
||
|
|
||
|
import java.nio.ByteBuffer;
|
||
|
import java.util.LinkedList;
|
||
|
import java.util.Queue;
|
||
|
import java.util.logging.Logger;
|
||
|
|
||
|
import static org.lwjgl.glfw.GLFW.*;
|
||
|
|
||
|
public class LwjglMouseInput implements MouseInput {
|
||
|
|
||
|
private static final Logger logger = Logger.getLogger(LwjglMouseInput.class.getName());
|
||
|
|
||
|
private LwjglWindow context;
|
||
|
private RawInputListener listener;
|
||
|
private boolean cursorVisible = true;
|
||
|
private int mouseX;
|
||
|
private int mouseY;
|
||
|
private int mouseWheel;
|
||
|
private boolean initialized;
|
||
|
private GLFWCursorPosCallback cursorPosCallback;
|
||
|
private GLFWScrollCallback scrollCallback;
|
||
|
private GLFWMouseButtonCallback mouseButtonCallback;
|
||
|
private Queue<MouseMotionEvent> mouseMotionEvents = new LinkedList<MouseMotionEvent>();
|
||
|
private Queue<MouseButtonEvent> mouseButtonEvents = new LinkedList<MouseButtonEvent>();
|
||
|
|
||
|
public LwjglMouseInput(LwjglWindow context) {
|
||
|
this.context = context;
|
||
|
}
|
||
|
|
||
|
public void initialize() {
|
||
|
glfwSetCursorPosCallback(context.getWindowHandle(), cursorPosCallback = new GLFWCursorPosCallback() {
|
||
|
@Override
|
||
|
public void invoke(long window, double xpos, double ypos) {
|
||
|
int xDelta;
|
||
|
int yDelta;
|
||
|
int x = (int) Math.round(xpos);
|
||
|
int y = (int) Math.round(ypos);
|
||
|
|
||
|
if (mouseX == 0) {
|
||
|
mouseX = x;
|
||
|
}
|
||
|
|
||
|
if (mouseY == 0) {
|
||
|
mouseY = y;
|
||
|
}
|
||
|
|
||
|
xDelta = x - mouseX;
|
||
|
yDelta = y - mouseY;
|
||
|
mouseX = x;
|
||
|
mouseY = y;
|
||
|
|
||
|
if (xDelta != 0 || yDelta != 0) {
|
||
|
final MouseMotionEvent mouseMotionEvent = new MouseMotionEvent(x, y * -1, xDelta, yDelta * -1, mouseWheel, 0);
|
||
|
mouseMotionEvent.setTime(getInputTimeNanos());
|
||
|
mouseMotionEvents.add(mouseMotionEvent);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
glfwSetScrollCallback(context.getWindowHandle(), scrollCallback = new GLFWScrollCallback() {
|
||
|
@Override
|
||
|
public void invoke(final long window, final double xOffset, final double yOffset) {
|
||
|
mouseWheel += yOffset;
|
||
|
|
||
|
final MouseMotionEvent mouseMotionEvent = new MouseMotionEvent(mouseX, mouseY, 0, 0, mouseWheel, (int) Math.round(yOffset));
|
||
|
mouseMotionEvent.setTime(getInputTimeNanos());
|
||
|
mouseMotionEvents.add(mouseMotionEvent);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
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);
|
||
|
mouseButtonEvent.setTime(getInputTimeNanos());
|
||
|
mouseButtonEvents.add(mouseButtonEvent);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
setCursorVisible(cursorVisible);
|
||
|
logger.fine("Mouse created.");
|
||
|
initialized = true;
|
||
|
}
|
||
|
|
||
|
public boolean isInitialized() {
|
||
|
return initialized;
|
||
|
}
|
||
|
|
||
|
public int getButtonCount() {
|
||
|
return 2; // TODO: How to determine this?
|
||
|
}
|
||
|
|
||
|
public void update() {
|
||
|
while (!mouseMotionEvents.isEmpty()) {
|
||
|
listener.onMouseMotionEvent(mouseMotionEvents.poll());
|
||
|
}
|
||
|
|
||
|
while (!mouseButtonEvents.isEmpty()) {
|
||
|
listener.onMouseButtonEvent(mouseButtonEvents.poll());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void destroy() {
|
||
|
if (!context.isRenderable()) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
cursorPosCallback.release();
|
||
|
scrollCallback.release();
|
||
|
mouseButtonCallback.release();
|
||
|
|
||
|
logger.fine("Mouse destroyed.");
|
||
|
}
|
||
|
|
||
|
public void setCursorVisible(boolean visible) {
|
||
|
cursorVisible = visible;
|
||
|
|
||
|
if (!context.isRenderable()) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (cursorVisible) {
|
||
|
glfwSetInputMode(context.getWindowHandle(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||
|
} else {
|
||
|
glfwSetInputMode(context.getWindowHandle(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void setInputListener(RawInputListener listener) {
|
||
|
this.listener = listener;
|
||
|
}
|
||
|
|
||
|
public long getInputTimeNanos() {
|
||
|
return (long) (glfwGetTime() * LwjglTimer.LWJGL_TIME_TO_NANOS);
|
||
|
}
|
||
|
|
||
|
public void setNativeCursor(final JmeCursor jmeCursor) {
|
||
|
if (jmeCursor != null) {
|
||
|
final ByteBuffer byteBuffer = org.lwjgl.BufferUtils.createByteBuffer(jmeCursor.getImagesData().capacity());
|
||
|
byteBuffer.asIntBuffer().put(jmeCursor.getImagesData().array());
|
||
|
final long cursor = glfwCreateCursor(byteBuffer, jmeCursor.getXHotSpot(), jmeCursor.getYHotSpot());
|
||
|
glfwSetCursor(context.getWindowHandle(), cursor);
|
||
|
}
|
||
|
}
|
||
|
}
|