use of com.jme3.input.JoystickButton in project jmonkeyengine by jMonkeyEngine.
the class AndroidJoystickJoyInput14 method loadJoysticks.
public List<Joystick> loadJoysticks(int joyId, InputManager inputManager) {
logger.log(Level.INFO, "loading Joystick devices");
ArrayList<Joystick> joysticks = new ArrayList<Joystick>();
joysticks.clear();
joystickIndex.clear();
ArrayList gameControllerDeviceIds = new ArrayList();
int[] deviceIds = InputDevice.getDeviceIds();
for (int deviceId : deviceIds) {
InputDevice dev = InputDevice.getDevice(deviceId);
int sources = dev.getSources();
logger.log(Level.FINE, "deviceId[{0}] sources: {1}", new Object[] { deviceId, sources });
// Verify that the device has gamepad buttons, control sticks, or both.
if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) || ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)) {
// This device is a game controller. Store its device ID.
if (!gameControllerDeviceIds.contains(deviceId)) {
gameControllerDeviceIds.add(deviceId);
logger.log(Level.FINE, "Attempting to create joystick for device: {0}", dev);
// Create an AndroidJoystick and store the InputDevice so we
// can later correspond the input from the InputDevice to the
// appropriate jME Joystick event
AndroidJoystick joystick = new AndroidJoystick(inputManager, joyInput, dev, joyId + joysticks.size(), dev.getName());
joystickIndex.put(deviceId, joystick);
joysticks.add(joystick);
// Each analog input is reported as a MotionRange
// The axis number corresponds to the type of axis
// The AndroidJoystick.addAxis(MotionRange) converts the axis
// type reported by Android into the jME Joystick axis
List<MotionRange> motionRanges = dev.getMotionRanges();
for (MotionRange motionRange : motionRanges) {
logger.log(Level.INFO, "motion range: {0}", motionRange.toString());
logger.log(Level.INFO, "axis: {0}", motionRange.getAxis());
JoystickAxis axis = joystick.addAxis(motionRange);
logger.log(Level.INFO, "added axis: {0}", axis);
}
// device, but I haven't found a better way yet.
for (int keyCode : AndroidGamepadButtons) {
logger.log(Level.INFO, "button[{0}]: {1}", new Object[] { keyCode, KeyCharacterMap.deviceHasKey(keyCode) });
if (KeyCharacterMap.deviceHasKey(keyCode)) {
// add button even though we aren't sure if the button
// actually exists on this InputDevice
logger.log(Level.INFO, "button[{0}] exists somewhere", keyCode);
JoystickButton button = joystick.addButton(keyCode);
logger.log(Level.INFO, "added button: {0}", button);
}
}
}
}
}
loaded = true;
return joysticks;
}
use of com.jme3.input.JoystickButton in project jmonkeyengine by jMonkeyEngine.
the class AndroidJoystickJoyInput14 method onKey.
public boolean onKey(KeyEvent event) {
boolean consumed = false;
// logger.log(Level.INFO, "onKey event: {0}", event);
event.getDeviceId();
event.getSource();
AndroidJoystick joystick = joystickIndex.get(event.getDeviceId());
if (joystick != null) {
JoystickButton button = joystick.getButton(event.getKeyCode());
boolean pressed = event.getAction() == KeyEvent.ACTION_DOWN;
if (button != null) {
JoyButtonEvent buttonEvent = new JoyButtonEvent(button, pressed);
joyInput.addEvent(buttonEvent);
consumed = true;
} else {
JoystickButton newButton = joystick.addButton(event.getKeyCode());
JoyButtonEvent buttonEvent = new JoyButtonEvent(newButton, pressed);
joyInput.addEvent(buttonEvent);
consumed = true;
}
}
return consumed;
}
use of com.jme3.input.JoystickButton in project jmonkeyengine by jMonkeyEngine.
the class TestJoystick method dumpJoysticks.
protected void dumpJoysticks(Joystick[] joysticks, PrintWriter out) {
for (Joystick j : joysticks) {
out.println("Joystick[" + j.getJoyId() + "]:" + j.getName());
out.println(" buttons:" + j.getButtonCount());
for (JoystickButton b : j.getButtons()) {
out.println(" " + b);
}
out.println(" axes:" + j.getAxisCount());
for (JoystickAxis axis : j.getAxes()) {
out.println(" " + axis);
}
}
}
use of com.jme3.input.JoystickButton in project jmonkeyengine by jMonkeyEngine.
the class TestJoystick method setViewedJoystick.
protected void setViewedJoystick(Joystick stick) {
if (this.viewedJoystick == stick)
return;
if (this.viewedJoystick != null) {
joystickInfo.detachAllChildren();
}
this.viewedJoystick = stick;
if (this.viewedJoystick != null) {
// Draw the hud
yInfo = 0;
addInfo("Joystick:\"" + stick.getName() + "\" id:" + stick.getJoyId(), 0);
yInfo -= 5;
float ySave = yInfo;
// Column one for the buttons
addInfo("Buttons:", 0);
for (JoystickButton b : stick.getButtons()) {
addInfo(" '" + b.getName() + "' id:'" + b.getLogicalId() + "'", 0);
}
yInfo = ySave;
// Column two for the axes
addInfo("Axes:", 1);
for (JoystickAxis a : stick.getAxes()) {
addInfo(" '" + a.getName() + "' id:'" + a.getLogicalId() + "' analog:" + a.isAnalog(), 1);
}
}
}
use of com.jme3.input.JoystickButton in project jmonkeyengine by jMonkeyEngine.
the class GlfwJoystickInput method update.
public void update() {
for (final Map.Entry<Integer, GlfwJoystick> entry : joysticks.entrySet()) {
// Axes
final FloatBuffer axisValues = glfwGetJoystickAxes(entry.getKey());
for (final JoystickAxis axis : entry.getValue().getAxes()) {
final float value = axisValues.get(axis.getAxisId());
listener.onJoyAxisEvent(new JoyAxisEvent(axis, value));
}
// Buttons
final ByteBuffer byteBuffer = glfwGetJoystickButtons(entry.getKey());
for (final JoystickButton button : entry.getValue().getButtons()) {
final boolean pressed = byteBuffer.get(button.getButtonId()) == GLFW_PRESS;
listener.onJoyButtonEvent(new JoyButtonEvent(button, pressed));
}
}
}
Aggregations