Added a JInput plugin to interface with the LWJGL mouse and keyboard from jinput (untested)

This commit is contained in:
Elias Naur 2006-04-05 11:27:46 +00:00
parent 461a9a2ba4
commit f16f0e26cc
4 changed files with 616 additions and 0 deletions

View File

@ -0,0 +1,285 @@
/**
* 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.
* The name of the author may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 org.lwjgl.util.jinput;
import org.lwjgl.input.Keyboard;
import net.java.games.input.Component;
/**
* @author elias
*/
final class KeyMap {
public final static Component.Identifier.Key map(int lwjgl_key_code) {
switch (lwjgl_key_code) {
case Keyboard.KEY_ESCAPE:
return Component.Identifier.Key.ESCAPE;
case Keyboard.KEY_1:
return Component.Identifier.Key._1;
case Keyboard.KEY_2:
return Component.Identifier.Key._2;
case Keyboard.KEY_3:
return Component.Identifier.Key._3;
case Keyboard.KEY_4:
return Component.Identifier.Key._4;
case Keyboard.KEY_5:
return Component.Identifier.Key._5;
case Keyboard.KEY_6:
return Component.Identifier.Key._6;
case Keyboard.KEY_7:
return Component.Identifier.Key._7;
case Keyboard.KEY_8:
return Component.Identifier.Key._8;
case Keyboard.KEY_9:
return Component.Identifier.Key._9;
case Keyboard.KEY_0:
return Component.Identifier.Key._0;
case Keyboard.KEY_MINUS:
return Component.Identifier.Key.MINUS;
case Keyboard.KEY_EQUALS:
return Component.Identifier.Key.EQUALS;
case Keyboard.KEY_BACK:
return Component.Identifier.Key.BACK;
case Keyboard.KEY_TAB:
return Component.Identifier.Key.TAB;
case Keyboard.KEY_Q:
return Component.Identifier.Key.Q;
case Keyboard.KEY_W:
return Component.Identifier.Key.W;
case Keyboard.KEY_E:
return Component.Identifier.Key.E;
case Keyboard.KEY_R:
return Component.Identifier.Key.R;
case Keyboard.KEY_T:
return Component.Identifier.Key.T;
case Keyboard.KEY_Y:
return Component.Identifier.Key.Y;
case Keyboard.KEY_U:
return Component.Identifier.Key.U;
case Keyboard.KEY_I:
return Component.Identifier.Key.I;
case Keyboard.KEY_O:
return Component.Identifier.Key.O;
case Keyboard.KEY_P:
return Component.Identifier.Key.P;
case Keyboard.KEY_LBRACKET:
return Component.Identifier.Key.LBRACKET;
case Keyboard.KEY_RBRACKET:
return Component.Identifier.Key.RBRACKET;
case Keyboard.KEY_RETURN:
return Component.Identifier.Key.RETURN;
case Keyboard.KEY_LCONTROL:
return Component.Identifier.Key.LCONTROL;
case Keyboard.KEY_A:
return Component.Identifier.Key.A;
case Keyboard.KEY_S:
return Component.Identifier.Key.S;
case Keyboard.KEY_D:
return Component.Identifier.Key.D;
case Keyboard.KEY_F:
return Component.Identifier.Key.F;
case Keyboard.KEY_G:
return Component.Identifier.Key.G;
case Keyboard.KEY_H:
return Component.Identifier.Key.H;
case Keyboard.KEY_J:
return Component.Identifier.Key.J;
case Keyboard.KEY_K:
return Component.Identifier.Key.K;
case Keyboard.KEY_L:
return Component.Identifier.Key.L;
case Keyboard.KEY_SEMICOLON:
return Component.Identifier.Key.SEMICOLON;
case Keyboard.KEY_APOSTROPHE:
return Component.Identifier.Key.APOSTROPHE;
case Keyboard.KEY_GRAVE:
return Component.Identifier.Key.GRAVE;
case Keyboard.KEY_LSHIFT:
return Component.Identifier.Key.LSHIFT;
case Keyboard.KEY_BACKSLASH:
return Component.Identifier.Key.BACKSLASH;
case Keyboard.KEY_Z:
return Component.Identifier.Key.Z;
case Keyboard.KEY_X:
return Component.Identifier.Key.X;
case Keyboard.KEY_C:
return Component.Identifier.Key.C;
case Keyboard.KEY_V:
return Component.Identifier.Key.V;
case Keyboard.KEY_B:
return Component.Identifier.Key.B;
case Keyboard.KEY_N:
return Component.Identifier.Key.N;
case Keyboard.KEY_M:
return Component.Identifier.Key.M;
case Keyboard.KEY_COMMA:
return Component.Identifier.Key.COMMA;
case Keyboard.KEY_PERIOD:
return Component.Identifier.Key.PERIOD;
case Keyboard.KEY_SLASH:
return Component.Identifier.Key.SLASH;
case Keyboard.KEY_RSHIFT:
return Component.Identifier.Key.RSHIFT;
case Keyboard.KEY_MULTIPLY:
return Component.Identifier.Key.MULTIPLY;
case Keyboard.KEY_LMENU:
return Component.Identifier.Key.LALT;
case Keyboard.KEY_SPACE:
return Component.Identifier.Key.SPACE;
case Keyboard.KEY_CAPITAL:
return Component.Identifier.Key.CAPITAL;
case Keyboard.KEY_F1:
return Component.Identifier.Key.F1;
case Keyboard.KEY_F2:
return Component.Identifier.Key.F2;
case Keyboard.KEY_F3:
return Component.Identifier.Key.F3;
case Keyboard.KEY_F4:
return Component.Identifier.Key.F4;
case Keyboard.KEY_F5:
return Component.Identifier.Key.F5;
case Keyboard.KEY_F6:
return Component.Identifier.Key.F6;
case Keyboard.KEY_F7:
return Component.Identifier.Key.F7;
case Keyboard.KEY_F8:
return Component.Identifier.Key.F8;
case Keyboard.KEY_F9:
return Component.Identifier.Key.F9;
case Keyboard.KEY_F10:
return Component.Identifier.Key.F10;
case Keyboard.KEY_NUMLOCK:
return Component.Identifier.Key.NUMLOCK;
case Keyboard.KEY_SCROLL:
return Component.Identifier.Key.SCROLL;
case Keyboard.KEY_NUMPAD7:
return Component.Identifier.Key.NUMPAD7;
case Keyboard.KEY_NUMPAD8:
return Component.Identifier.Key.NUMPAD8;
case Keyboard.KEY_NUMPAD9:
return Component.Identifier.Key.NUMPAD9;
case Keyboard.KEY_SUBTRACT:
return Component.Identifier.Key.SUBTRACT;
case Keyboard.KEY_NUMPAD4:
return Component.Identifier.Key.NUMPAD4;
case Keyboard.KEY_NUMPAD5:
return Component.Identifier.Key.NUMPAD5;
case Keyboard.KEY_NUMPAD6:
return Component.Identifier.Key.NUMPAD6;
case Keyboard.KEY_ADD:
return Component.Identifier.Key.ADD;
case Keyboard.KEY_NUMPAD1:
return Component.Identifier.Key.NUMPAD1;
case Keyboard.KEY_NUMPAD2:
return Component.Identifier.Key.NUMPAD2;
case Keyboard.KEY_NUMPAD3:
return Component.Identifier.Key.NUMPAD3;
case Keyboard.KEY_NUMPAD0:
return Component.Identifier.Key.NUMPAD0;
case Keyboard.KEY_DECIMAL:
return Component.Identifier.Key.DECIMAL;
case Keyboard.KEY_F11:
return Component.Identifier.Key.F11;
case Keyboard.KEY_F12:
return Component.Identifier.Key.F12;
case Keyboard.KEY_F13:
return Component.Identifier.Key.F13;
case Keyboard.KEY_F14:
return Component.Identifier.Key.F14;
case Keyboard.KEY_F15:
return Component.Identifier.Key.F15;
case Keyboard.KEY_KANA:
return Component.Identifier.Key.KANA;
case Keyboard.KEY_CONVERT:
return Component.Identifier.Key.CONVERT;
case Keyboard.KEY_NOCONVERT:
return Component.Identifier.Key.NOCONVERT;
case Keyboard.KEY_YEN:
return Component.Identifier.Key.YEN;
case Keyboard.KEY_NUMPADEQUALS:
return Component.Identifier.Key.NUMPADEQUAL;
case Keyboard.KEY_CIRCUMFLEX:
return Component.Identifier.Key.CIRCUMFLEX;
case Keyboard.KEY_AT:
return Component.Identifier.Key.AT;
case Keyboard.KEY_COLON:
return Component.Identifier.Key.COLON;
case Keyboard.KEY_UNDERLINE:
return Component.Identifier.Key.UNDERLINE;
case Keyboard.KEY_KANJI:
return Component.Identifier.Key.KANJI;
case Keyboard.KEY_STOP:
return Component.Identifier.Key.STOP;
case Keyboard.KEY_AX:
return Component.Identifier.Key.AX;
case Keyboard.KEY_UNLABELED:
return Component.Identifier.Key.UNLABELED;
case Keyboard.KEY_NUMPADENTER:
return Component.Identifier.Key.NUMPADENTER;
case Keyboard.KEY_RCONTROL:
return Component.Identifier.Key.RCONTROL;
case Keyboard.KEY_NUMPADCOMMA:
return Component.Identifier.Key.NUMPADCOMMA;
case Keyboard.KEY_DIVIDE:
return Component.Identifier.Key.DIVIDE;
case Keyboard.KEY_SYSRQ:
return Component.Identifier.Key.SYSRQ;
case Keyboard.KEY_RMENU:
return Component.Identifier.Key.RALT;
case Keyboard.KEY_PAUSE:
return Component.Identifier.Key.PAUSE;
case Keyboard.KEY_HOME:
return Component.Identifier.Key.HOME;
case Keyboard.KEY_UP:
return Component.Identifier.Key.UP;
case Keyboard.KEY_PRIOR:
return Component.Identifier.Key.PAGEUP;
case Keyboard.KEY_LEFT:
return Component.Identifier.Key.LEFT;
case Keyboard.KEY_RIGHT:
return Component.Identifier.Key.RIGHT;
case Keyboard.KEY_END:
return Component.Identifier.Key.END;
case Keyboard.KEY_DOWN:
return Component.Identifier.Key.DOWN;
case Keyboard.KEY_NEXT:
return Component.Identifier.Key.PAGEDOWN;
case Keyboard.KEY_INSERT:
return Component.Identifier.Key.INSERT;
case Keyboard.KEY_DELETE:
return Component.Identifier.Key.DELETE;
case Keyboard.KEY_LWIN:
return Component.Identifier.Key.LWIN;
case Keyboard.KEY_RWIN:
return Component.Identifier.Key.RWIN;
case Keyboard.KEY_APPS:
return Component.Identifier.Key.APPS;
case Keyboard.KEY_POWER:
return Component.Identifier.Key.POWER;
case Keyboard.KEY_SLEEP:
return Component.Identifier.Key.SLEEP;
default:
return Component.Identifier.Key.UNKNOWN;
}
}
}

View File

@ -0,0 +1,43 @@
/**
* 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.
* The name of the author may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 org.lwjgl.util.jinput;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;
import net.java.games.util.plugins.Plugin;
/**
* @author elias
*/
public class LWJGLEnvironmentPlugin extends ControllerEnvironment implements Plugin {
private final Controller[] controllers;
public LWJGLEnvironmentPlugin() {
this.controllers = new Controller[]{new LWJGLKeyboard(), new LWJGLMouse()};
}
public Controller[] getControllers() {
return controllers;
}
}

View File

@ -0,0 +1,115 @@
/**
* 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.
* The name of the author may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 org.lwjgl.util.jinput;
import net.java.games.input.Keyboard;
import net.java.games.input.Component;
import net.java.games.input.Controller;
import net.java.games.input.Rumbler;
import net.java.games.input.Event;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/**
* @author elias
*/
final class LWJGLKeyboard extends Keyboard {
protected LWJGLKeyboard() {
super("LWJGLKeyboard", createComponents(), new Controller[]{}, new Rumbler[]{});
}
private final static Component[] createComponents() {
List components = new ArrayList();
Field[] vkey_fields = org.lwjgl.input.Keyboard.class.getFields();
for (int i = 0; i < vkey_fields.length; i++) {
Field vkey_field = vkey_fields[i];
try {
if (Modifier.isStatic(vkey_field.getModifiers()) && vkey_field.getType() == int.class &&
vkey_field.getName().startsWith("KEY_")) {
int vkey_code = vkey_field.getInt(null);
Component.Identifier.Key key_id = KeyMap.map(vkey_code);
if (key_id != Component.Identifier.Key.UNKNOWN)
components.add(new Key(key_id, vkey_code));
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
return (Component[])components.toArray(new Component[]{});
}
public final synchronized void pollDevice() throws IOException {
if (!org.lwjgl.input.Keyboard.isCreated())
return;
org.lwjgl.input.Keyboard.poll();
Component[] components = getComponents();
for (int i = 0; i < components.length; i++) {
Key key = (Key)components[i];
key.update();
}
}
protected final synchronized boolean getNextDeviceEvent(Event event) throws IOException {
if (!org.lwjgl.input.Keyboard.isCreated())
return false;
if (!org.lwjgl.input.Keyboard.next())
return false;
int lwjgl_key = org.lwjgl.input.Keyboard.getEventKey();
if (lwjgl_key == org.lwjgl.input.Keyboard.KEY_NONE)
return false;
Component.Identifier.Key key_id = KeyMap.map(lwjgl_key);
if (key_id == null)
return false;
Component key = getComponent(key_id);
if (key == null)
return false;
float value = org.lwjgl.input.Keyboard.getEventKeyState() ? 1 : 0;
event.set(key, value);
return true;
}
private final static class Key extends Keyboard.Key {
private final int lwjgl_key;
private float value;
public Key(Component.Identifier.Key key_id, int lwjgl_key) {
super(key_id.getName(), key_id);
this.lwjgl_key = lwjgl_key;
}
public final void update() {
this.value = org.lwjgl.input.Keyboard.isKeyDown(lwjgl_key) ? 1 : 0;
}
protected final float poll() {
return value;
}
}
}

View File

@ -0,0 +1,173 @@
/**
*
* 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.
* The name of the author may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 org.lwjgl.util.jinput;
import net.java.games.input.Mouse;
import net.java.games.input.Component;
import net.java.games.input.Controller;
import net.java.games.input.Rumbler;
import net.java.games.input.Event;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
/**
* @author elias
*/
final class LWJGLMouse extends Mouse {
private final static int EVENT_X = 1;
private final static int EVENT_Y = 2;
private final static int EVENT_WHEEL = 3;
private final static int EVENT_BUTTON = 4;
private final static int EVENT_DONE = 5;
private int event_state = EVENT_DONE;
protected LWJGLMouse() {
super("LWJGLMouse", createComponents(), new Controller[]{}, new Rumbler[]{});
}
private final static Component[] createComponents() {
return new Component[]{new Axis(Component.Identifier.Axis.X),
new Axis(Component.Identifier.Axis.Y),
new Axis(Component.Identifier.Axis.Z),
new Button(Component.Identifier.Button.LEFT),
new Button(Component.Identifier.Button.MIDDLE),
new Button(Component.Identifier.Button.RIGHT)};
}
public final synchronized void pollDevice() throws IOException {
if (!org.lwjgl.input.Mouse.isCreated())
return;
org.lwjgl.input.Mouse.poll();
for (int i = 0; i < 3; i++)
setButtonState(i);
}
private final Button map(int lwjgl_button) {
switch (lwjgl_button) {
case 0:
return (Button)getLeft();
case 1:
return (Button)getRight();
case 2:
return (Button)getMiddle();
default:
return null;
}
}
private final void setButtonState(int lwjgl_button) {
Button button = map(lwjgl_button);
if (button != null)
button.setValue(org.lwjgl.input.Mouse.isButtonDown(lwjgl_button) ? 1 : 0);
}
protected final synchronized boolean getNextDeviceEvent(Event event) throws IOException {
if (!org.lwjgl.input.Mouse.isCreated())
return false;
while (true) {
switch (event_state) {
case EVENT_X:
event_state = EVENT_Y;
int dx = org.lwjgl.input.Mouse.getEventDX();
if (dx != 0) {
event.set(getX(), dx);
return true;
}
break;
case EVENT_Y:
event_state = EVENT_WHEEL;
/* We must negate the y coord since lwjgl uses the
* OpenGL coordinate system
*/
int dy = -org.lwjgl.input.Mouse.getEventDY();
if (dy != 0) {
event.set(getY(), dy);
return true;
}
break;
case EVENT_WHEEL:
event_state = EVENT_BUTTON;
int dwheel = org.lwjgl.input.Mouse.getEventDWheel();
if (dwheel != 0) {
event.set(getWheel(), dwheel);
return true;
}
break;
case EVENT_BUTTON:
event_state = EVENT_DONE;
int lwjgl_button = org.lwjgl.input.Mouse.getEventButton();
if (lwjgl_button != -1) {
Button button = map(lwjgl_button);
if (button != null) {
event.set(button, org.lwjgl.input.Mouse.getEventButtonState() ? 1f : 0f);
return true;
}
}
break;
case EVENT_DONE:
if (!org.lwjgl.input.Mouse.next())
return false;
event_state = EVENT_X;
break;
default:
break;
}
}
}
final static class Axis extends Mouse.Axis {
public Axis(Component.Identifier.Axis axis_id) {
super(axis_id.getName(), axis_id);
}
public final boolean isRelative() {
return true;
}
protected final float poll() throws IOException {
return 0;
}
}
final static class Button extends Mouse.Button {
private float value;
public Button(Component.Identifier.Button button_id) {
super(button_id.getName(), button_id);
}
protected final void setValue(float value) {
this.value = value;
}
protected final float poll() throws IOException {
return value;
}
}
}