Search in sources :

Example 1 with JoystickButton

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;
}
Also used : DefaultJoystickButton(com.jme3.input.DefaultJoystickButton) JoystickButton(com.jme3.input.JoystickButton) InputDevice(android.view.InputDevice) AbstractJoystick(com.jme3.input.AbstractJoystick) Joystick(com.jme3.input.Joystick) ArrayList(java.util.ArrayList) JoystickAxis(com.jme3.input.JoystickAxis) DefaultJoystickAxis(com.jme3.input.DefaultJoystickAxis) MotionRange(android.view.InputDevice.MotionRange)

Example 2 with JoystickButton

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;
}
Also used : DefaultJoystickButton(com.jme3.input.DefaultJoystickButton) JoystickButton(com.jme3.input.JoystickButton) JoyButtonEvent(com.jme3.input.event.JoyButtonEvent)

Example 3 with JoystickButton

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);
        }
    }
}
Also used : JoystickButton(com.jme3.input.JoystickButton) Joystick(com.jme3.input.Joystick) JoystickAxis(com.jme3.input.JoystickAxis)

Example 4 with JoystickButton

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);
        }
    }
}
Also used : JoystickButton(com.jme3.input.JoystickButton) JoystickAxis(com.jme3.input.JoystickAxis)

Example 5 with JoystickButton

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));
        }
    }
}
Also used : JoyAxisEvent(com.jme3.input.event.JoyAxisEvent) JoyButtonEvent(com.jme3.input.event.JoyButtonEvent) FloatBuffer(java.nio.FloatBuffer) Map(java.util.Map) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer)

Aggregations

JoystickButton (com.jme3.input.JoystickButton)5 JoystickAxis (com.jme3.input.JoystickAxis)4 DefaultJoystickButton (com.jme3.input.DefaultJoystickButton)3 JoyButtonEvent (com.jme3.input.event.JoyButtonEvent)3 DefaultJoystickAxis (com.jme3.input.DefaultJoystickAxis)2 Joystick (com.jme3.input.Joystick)2 JoyAxisEvent (com.jme3.input.event.JoyAxisEvent)2 InputDevice (android.view.InputDevice)1 MotionRange (android.view.InputDevice.MotionRange)1 AbstractJoystick (com.jme3.input.AbstractJoystick)1 ByteBuffer (java.nio.ByteBuffer)1 FloatBuffer (java.nio.FloatBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Identifier (net.java.games.input.Component.Identifier)1 Axis (net.java.games.input.Component.Identifier.Axis)1 Button (net.java.games.input.Component.Identifier.Button)1