Search in sources :

Example 1 with JoystickAxis

use of com.jme3.input.JoystickAxis in project jmonkeyengine by jMonkeyEngine.

the class TestAndroidSensors method simpleUpdate.

@Override
public void simpleUpdate(float tpf) {
    if (!initialCalibrationComplete) {
        // is a sensor joystick axis
        for (IntMap.Entry<Joystick> entry : joystickMap) {
            for (JoystickAxis axis : entry.getValue().getAxes()) {
                if (axis instanceof SensorJoystickAxis) {
                    logger.log(Level.INFO, "Calibrating Axis: {0}", axis.toString());
                    ((SensorJoystickAxis) axis).calibrateCenter();
                }
            }
        }
        initialCalibrationComplete = true;
    }
    if (enableGeometryRotation) {
        rotationQuat.fromAngles(anglesCurrent);
        rotationQuat.normalizeLocal();
        if (useAbsolute) {
            geomZero.setLocalRotation(rotationQuat);
        } else {
            geomZero.rotate(rotationQuat);
        }
        anglesCurrent[0] = anglesCurrent[1] = anglesCurrent[2] = 0f;
    }
}
Also used : Joystick(com.jme3.input.Joystick) SensorJoystickAxis(com.jme3.input.SensorJoystickAxis) IntMap(com.jme3.util.IntMap) SensorJoystickAxis(com.jme3.input.SensorJoystickAxis) JoystickAxis(com.jme3.input.JoystickAxis)

Example 2 with JoystickAxis

use of com.jme3.input.JoystickAxis in project jmonkeyengine by jMonkeyEngine.

the class AndroidJoystickJoyInput14 method onGenericMotion.

public boolean onGenericMotion(MotionEvent event) {
    boolean consumed = false;
    //        logger.log(Level.INFO, "onGenericMotion event: {0}", event);
    event.getDeviceId();
    event.getSource();
    //        logger.log(Level.INFO, "deviceId: {0}, source: {1}", new Object[]{event.getDeviceId(), event.getSource()});
    AndroidJoystick joystick = joystickIndex.get(event.getDeviceId());
    if (joystick != null) {
        for (int androidAxis : joystick.getAndroidAxes()) {
            String axisName = MotionEvent.axisToString(androidAxis);
            float value = event.getAxisValue(androidAxis);
            int action = event.getAction();
            if (action == MotionEvent.ACTION_MOVE) {
                //                    logger.log(Level.INFO, "MOVE axis num: {0}, axisName: {1}, value: {2}",
                //                            new Object[]{androidAxis, axisName, value});
                JoystickAxis axis = joystick.getAxis(androidAxis);
                if (axis != null) {
                    //                        logger.log(Level.INFO, "MOVE axis num: {0}, axisName: {1}, value: {2}, deadzone: {3}",
                    //                                new Object[]{androidAxis, axisName, value, axis.getDeadZone()});
                    JoyAxisEvent axisEvent = new JoyAxisEvent(axis, value);
                    joyInput.addEvent(axisEvent);
                    consumed = true;
                } else {
                //                        logger.log(Level.INFO, "axis was null for axisName: {0}", axisName);
                }
            } else {
            //                    logger.log(Level.INFO, "action: {0}", action);
            }
        }
    }
    return consumed;
}
Also used : JoyAxisEvent(com.jme3.input.event.JoyAxisEvent) JoystickAxis(com.jme3.input.JoystickAxis) DefaultJoystickAxis(com.jme3.input.DefaultJoystickAxis)

Example 3 with JoystickAxis

use of com.jme3.input.JoystickAxis 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 4 with JoystickAxis

use of com.jme3.input.JoystickAxis in project jmonkeyengine by jMonkeyEngine.

the class TestAndroidSensors method simpleInitApp.

@Override
public void simpleInitApp() {
    if (enableFlyByCameraRotation) {
        flyCam.setEnabled(true);
    } else {
        flyCam.setEnabled(false);
    }
    Mesh lineX = new Line(Vector3f.ZERO, Vector3f.ZERO.add(Vector3f.UNIT_X.mult(3)));
    Mesh lineY = new Line(Vector3f.ZERO, Vector3f.ZERO.add(Vector3f.UNIT_Y.mult(3)));
    Mesh lineZ = new Line(Vector3f.ZERO, Vector3f.ZERO.add(Vector3f.UNIT_Z.mult(3)));
    Geometry geoX = new Geometry("X", lineX);
    Material matX = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matX.setColor("Color", ColorRGBA.Red);
    matX.getAdditionalRenderState().setLineWidth(30);
    geoX.setMaterial(matX);
    rootNode.attachChild(geoX);
    Geometry geoY = new Geometry("Y", lineY);
    Material matY = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matY.setColor("Color", ColorRGBA.Green);
    matY.getAdditionalRenderState().setLineWidth(30);
    geoY.setMaterial(matY);
    rootNode.attachChild(geoY);
    Geometry geoZ = new Geometry("Z", lineZ);
    Material matZ = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matZ.setColor("Color", ColorRGBA.Blue);
    matZ.getAdditionalRenderState().setLineWidth(30);
    geoZ.setMaterial(matZ);
    rootNode.attachChild(geoZ);
    Box b = new Box(1, 1, 1);
    geomZero = new Geometry("Box", b);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setColor("Color", ColorRGBA.Yellow);
    Texture tex_ml = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
    mat.setTexture("ColorMap", tex_ml);
    geomZero.setMaterial(mat);
    geomZero.setLocalTranslation(Vector3f.ZERO);
    geomZero.setLocalRotation(Quaternion.IDENTITY);
    rootNode.attachChild(geomZero);
    // Touch (aka MouseInput.BUTTON_LEFT) is used to record the starting
    // orientation when using absolute rotations
    inputManager.addMapping("MouseClick", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    inputManager.addListener(this, "MouseClick");
    Joystick[] joysticks = inputManager.getJoysticks();
    if (joysticks == null || joysticks.length < 1) {
        logger.log(Level.INFO, "Cannot find any joysticks!");
    } else {
        // Joysticks return a value of 0 to 1 based on how far the stick is
        // push on the axis. This value is then scaled based on how long
        // during the frame the joystick axis has been in that position.
        // If the joystick is push all the way for the whole frame,
        // then the value in onAnalog is equal to tpf.
        // If the joystick is push 1/2 way for the entire frame, then the
        // onAnalog value is 1/2 tpf.
        // Similarly, if the joystick is pushed to the maximum during a frame
        // the value in onAnalog will also be scaled.
        // For Android sensors, rotating the device 90deg is the same as
        // pushing an actual joystick axis to the maximum.
        logger.log(Level.INFO, "Number of joysticks: {0}", joysticks.length);
        JoystickAxis axis;
        for (Joystick joystick : joysticks) {
            // Get and display all axes in joystick.
            List<JoystickAxis> axes = joystick.getAxes();
            for (JoystickAxis joystickAxis : axes) {
                logger.log(Level.INFO, "{0} axis scan Name: {1}, LogicalId: {2}, AxisId: {3}", new Object[] { joystick.getName(), joystickAxis.getName(), joystickAxis.getLogicalId(), joystickAxis.getAxisId() });
            }
            // Get specific axis based on LogicalId of the JoystickAxis
            // If found, map axis
            axis = joystick.getAxis(SensorJoystickAxis.ORIENTATION_X);
            if (axis != null) {
                axis.assignAxis(ORIENTATION_X_PLUS, ORIENTATION_X_MINUS);
                inputManager.addListener(this, ORIENTATION_X_PLUS, ORIENTATION_X_MINUS);
                logger.log(Level.INFO, "Found {0} Joystick, assigning mapping for X axis: {1}, with max value: {2}", new Object[] { joystick.toString(), axis.toString(), ((SensorJoystickAxis) axis).getMaxRawValue() });
            }
            axis = joystick.getAxis(SensorJoystickAxis.ORIENTATION_Y);
            if (axis != null) {
                axis.assignAxis(ORIENTATION_Y_PLUS, ORIENTATION_Y_MINUS);
                inputManager.addListener(this, ORIENTATION_Y_PLUS, ORIENTATION_Y_MINUS);
                logger.log(Level.INFO, "Found {0} Joystick, assigning mapping for Y axis: {1}, with max value: {2}", new Object[] { joystick.toString(), axis.toString(), ((SensorJoystickAxis) axis).getMaxRawValue() });
            }
            axis = joystick.getAxis(SensorJoystickAxis.ORIENTATION_Z);
            if (axis != null) {
                axis.assignAxis(ORIENTATION_Z_PLUS, ORIENTATION_Z_MINUS);
                inputManager.addListener(this, ORIENTATION_Z_PLUS, ORIENTATION_Z_MINUS);
                logger.log(Level.INFO, "Found {0} Joystick, assigning mapping for Z axis: {1}, with max value: {2}", new Object[] { joystick.toString(), axis.toString(), ((SensorJoystickAxis) axis).getMaxRawValue() });
            }
            joystickMap.put(joystick.getJoyId(), joystick);
        }
    }
}
Also used : Line(com.jme3.scene.shape.Line) Geometry(com.jme3.scene.Geometry) Joystick(com.jme3.input.Joystick) Mesh(com.jme3.scene.Mesh) Material(com.jme3.material.Material) Box(com.jme3.scene.shape.Box) MouseButtonTrigger(com.jme3.input.controls.MouseButtonTrigger) SensorJoystickAxis(com.jme3.input.SensorJoystickAxis) JoystickAxis(com.jme3.input.JoystickAxis) Texture(com.jme3.texture.Texture)

Example 5 with JoystickAxis

use of com.jme3.input.JoystickAxis in project jmonkeyengine by jMonkeyEngine.

the class TestAndroidSensors method onAction.

public void onAction(String string, boolean pressed, float tpf) {
    if (string.equalsIgnoreCase("MouseClick") && pressed) {
        // is a sensor joystick axis
        for (IntMap.Entry<Joystick> entry : joystickMap) {
            for (JoystickAxis axis : entry.getValue().getAxes()) {
                if (axis instanceof SensorJoystickAxis) {
                    logger.log(Level.INFO, "Calibrating Axis: {0}", axis.toString());
                    ((SensorJoystickAxis) axis).calibrateCenter();
                }
            }
        }
        if (enableRumble) {
            // manipulate joystick rumble
            for (IntMap.Entry<Joystick> entry : joystickMap) {
                rumbleAmount += 0.1f;
                if (rumbleAmount > 1f + FastMath.ZERO_TOLERANCE) {
                    rumbleAmount = 0f;
                }
                logger.log(Level.INFO, "rumbling with amount: {0}", rumbleAmount);
                entry.getValue().rumble(rumbleAmount);
            }
        }
    }
}
Also used : Joystick(com.jme3.input.Joystick) SensorJoystickAxis(com.jme3.input.SensorJoystickAxis) IntMap(com.jme3.util.IntMap) SensorJoystickAxis(com.jme3.input.SensorJoystickAxis) JoystickAxis(com.jme3.input.JoystickAxis)

Aggregations

JoystickAxis (com.jme3.input.JoystickAxis)8 Joystick (com.jme3.input.Joystick)5 JoystickButton (com.jme3.input.JoystickButton)4 DefaultJoystickAxis (com.jme3.input.DefaultJoystickAxis)3 SensorJoystickAxis (com.jme3.input.SensorJoystickAxis)3 JoyAxisEvent (com.jme3.input.event.JoyAxisEvent)3 DefaultJoystickButton (com.jme3.input.DefaultJoystickButton)2 JoyButtonEvent (com.jme3.input.event.JoyButtonEvent)2 IntMap (com.jme3.util.IntMap)2 InputDevice (android.view.InputDevice)1 MotionRange (android.view.InputDevice.MotionRange)1 AbstractJoystick (com.jme3.input.AbstractJoystick)1 MouseButtonTrigger (com.jme3.input.controls.MouseButtonTrigger)1 Material (com.jme3.material.Material)1 Geometry (com.jme3.scene.Geometry)1 Mesh (com.jme3.scene.Mesh)1 Box (com.jme3.scene.shape.Box)1 Line (com.jme3.scene.shape.Line)1 Texture (com.jme3.texture.Texture)1 ByteBuffer (java.nio.ByteBuffer)1