Example usage for android.view InputDevice getMotionRange

List of usage examples for android.view InputDevice getMotionRange

Introduction

In this page you can find the example usage for android.view InputDevice getMotionRange.

Prototype

public MotionRange getMotionRange(int axis, int source) 

Source Link

Document

Gets information about the range of values for a particular MotionEvent axis used by a particular source on the device.

Usage

From source file:Main.java

public static float getCenteredAxis(MotionEvent event, InputDevice device, int axis, int historyPos) {
    if (device == null) {
        return 0;
    }//  w w  w. j a  va 2 s  .c o  m
    final InputDevice.MotionRange range = device.getMotionRange(axis, event.getSource());
    if (range != null) {
        final float flat = range.getFlat();
        final float value = historyPos < 0 ? event.getAxisValue(axis)
                : event.getHistoricalAxisValue(axis, historyPos);

        // Ignore axis values that are within the 'flat' region of the
        // joystick axis center.
        // A joystick at rest does not always report an absolute position of
        // (0,0).
        if (Math.abs(value) > flat) {
            return value;
        }
    }
    return 0;
}

From source file:tv.piratemedia.flightcontroller.ControlActivity.java

private static float getTriggerValue(MotionEvent event, InputDevice device, int axis, int historyPos) {
    final InputDevice.MotionRange range = device.getMotionRange(axis, event.getSource());
    if (range != null) {
        final float value = historyPos < 0 ? event.getAxisValue(axis)
                : event.getHistoricalAxisValue(axis, historyPos);
        return value / range.getRange();
    }//from www . j  av  a2s .c om
    return 0;
}

From source file:com.thalmic.android.sample.helloworld.HelloWorldActivity.java

private static float getCenteredAxis(MotionEvent event, InputDevice device, int axis, int historyPos) {
    final InputDevice.MotionRange range = device.getMotionRange(axis, event.getSource());

    // A joystick at rest does not always report an absolute position of
    // (0,0). Use the getFlat() method to determine the range of values
    // bounding the joystick axis center.
    if (range != null) {
        final float flat = range.getFlat();
        final float value = historyPos < 0 ? event.getAxisValue(axis)
                : event.getHistoricalAxisValue(axis, historyPos);

        // Ignore axis values that are within the 'flat' region of the
        // joystick axis center.
        if (Math.abs(value) > flat) {
            return value;
        }//from  w ww .  j  a  v  a 2  s  .  c  o  m
    }
    return 0;
}

From source file:com.example.android.visualgamecontroller.FullscreenActivity.java

/**
 * Get centered position for axis input by considering flat area.
 * /*from w  w  w .  j  av  a 2s .  c om*/
 * @param event
 * @param device
 * @param axis
 * @return
 */
private float getCenteredAxis(MotionEvent event, InputDevice device, int axis) {
    InputDevice.MotionRange range = device.getMotionRange(axis, event.getSource());

    // A joystick at rest does not always report an absolute position of
    // (0,0). Use the getFlat() method to determine the range of values
    // bounding the joystick axis center.
    if (range != null) {
        float flat = range.getFlat();
        float value = event.getAxisValue(axis);

        // Ignore axis values that are within the 'flat' region of the
        // joystick axis center.
        if (Math.abs(value) > flat) {
            return value;
        }
    }
    return 0;
}

From source file:com.amazon.appstream.fireclient.FireClientActivity.java

/**
 * A "generic motion event" includes joystick and mouse motion
 * when the mouse button isn't down. In our simple sample, we're
 * not handling the joystick, but this is where any such code
 * would live./*  w w w.  j a v  a2s  .  c o  m*/
 *
 * This will only ever be called in HONEYCOMB_MR1 (12) or later, so I'm marking the
 * function using \@TargetApi to allow it to call the super.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override
public boolean dispatchGenericMotionEvent(MotionEvent event) {
    if (event.getSource() == InputDevice.SOURCE_MOUSE) {
        event.getPointerCoords(0, mCoordHolder);
        switch (event.getAction()) {
        case MotionEvent.ACTION_HOVER_MOVE:
            AppStreamInterface.mouseEvent((int) mCoordHolder.x, (int) mCoordHolder.y, 0);
            break;

        default:
            return super.dispatchGenericMotionEvent(event);
        }
        return true;
    } else if (event.getSource() == InputDevice.SOURCE_JOYSTICK) {
        Log.v(TAG, "Joystick event:" + event.toString());

        if (!mInitializedJoystick) {
            mInitializedJoystick = true;

            InputDevice joystick = InputDevice.getDevice(event.getDeviceId());
            InputDevice.MotionRange lThumbX = joystick.getMotionRange(MotionEvent.AXIS_X,
                    InputDevice.SOURCE_JOYSTICK);
            InputDevice.MotionRange lThumbY = joystick.getMotionRange(MotionEvent.AXIS_Y,
                    InputDevice.SOURCE_JOYSTICK);
            InputDevice.MotionRange rThumbX = joystick.getMotionRange(MotionEvent.AXIS_Z);
            InputDevice.MotionRange rThumbY = joystick.getMotionRange(MotionEvent.AXIS_RZ);
            InputDevice.MotionRange rTrigger = joystick.getMotionRange(MotionEvent.AXIS_GAS);
            if (rTrigger == null) {
                rTrigger = joystick.getMotionRange(MotionEvent.AXIS_RTRIGGER);
                mRTrigger = MotionEvent.AXIS_RTRIGGER;
            }

            InputDevice.MotionRange lTrigger = joystick.getMotionRange(MotionEvent.AXIS_BRAKE);
            if (lTrigger == null) {
                lTrigger = joystick.getMotionRange(MotionEvent.AXIS_LTRIGGER);
                mLTrigger = MotionEvent.AXIS_LTRIGGER;
            }

            List<InputDevice.MotionRange> ranges = joystick.getMotionRanges();

            InputDevice.MotionRange dPad = null;
            String name = joystick.getName();

            /*
            The Amazon Fire Game Controller follows the NVidia standard of sending
            AXIS_HAT_X/AXIS_HAT_Y results when the user hits the D-Pad. Only if we
            return false from dispatchGenericMotionEvent() will it then send
            DPAD keycodes.
                    
            But the most popular Android joystick on the market at this time, the
            Nyko Playpad Pro, returns AXIS_HAT_X/AXIS_HAT_Y results when the left
            analog stick hits its extremes, meaning that the analog stick will
            generate DPAD keys if we return false. The Nyko generates DPAD keys
            directly for the DPAD controller.
                    
            So we have two incompatible standards fighting with each other. Probably
            the safest thing to do would be to ask the user to press on their DPAD
            and see what messages we get, but that is beyond the scope of this
            example code.
            */
            if (name.equals("Amazon Fire Game Controler")) {
                for (int i = 0; i < ranges.size(); ++i) {
                    InputDevice.MotionRange range = ranges.get(i);
                    int axis = range.getAxis();

                    if (axis == MotionEvent.AXIS_HAT_X || axis == MotionEvent.AXIS_HAT_Y) {
                        dPad = ranges.get(i);
                        break;
                    }
                }
            }

            JoystickHelper.joystickDeadZones(lTrigger, rTrigger, lThumbX, lThumbY, rThumbX, rThumbY, dPad);
        }

        float lThumbX = event.getAxisValue(MotionEvent.AXIS_X);
        float lThumbY = -event.getAxisValue(MotionEvent.AXIS_Y);
        float rThumbX = event.getAxisValue(MotionEvent.AXIS_Z);
        float rThumbY = -event.getAxisValue(MotionEvent.AXIS_RZ);
        float lTrigger = event.getAxisValue(mLTrigger);
        float rTrigger = event.getAxisValue(mRTrigger);
        float hatX = event.getAxisValue(MotionEvent.AXIS_HAT_X);
        float hatY = event.getAxisValue(MotionEvent.AXIS_HAT_Y);

        JoystickHelper.setJoystickState(lTrigger, rTrigger, lThumbX, lThumbY, rThumbX, rThumbY, hatX, hatY);
        return true;
    }

    return super.dispatchGenericMotionEvent(event);
}