Java Code Examples for android.view.InputDevice#MotionRange

The following examples show how to use android.view.InputDevice#MotionRange . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AndroidDevices.java    From VCL-Android with Apache License 2.0 6 votes vote down vote up
@TargetApi(VERSION_CODES.HONEYCOMB_MR1)
public static float getCenteredAxis(MotionEvent event,
        InputDevice device, int axis) {
    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 = 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;
}
 
Example 2
Source File: AndroidView.java    From Alite with GNU General Public License v3.0 6 votes vote down vote up
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;
        }
    }
    return 0;
}
 
Example 3
Source File: FullscreenActivity.java    From androidtv-VisualGameController with Apache License 2.0 6 votes vote down vote up
/**
 * Get centered position for axis input by considering flat area.
 * 
 * @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;
}
 
Example 4
Source File: GameView.java    From bluetooth with Apache License 2.0 6 votes vote down vote up
private static float getCenteredAxis(MotionEvent event, InputDevice device,
        int axis, int historyPos) {
    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;
}
 
Example 5
Source File: GameView.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
private static float getCenteredAxis(MotionEvent event, InputDevice device,
        int axis, int historyPos) {
    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;
}
 
Example 6
Source File: MotionAlertDialog.java    From citra_android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Saves the provided motion input setting both to the INI file (so native code can use it) and as
 * an Android preference (so it persists correctly and is human-readable.)
 *
 * @param device      InputDevice from which the input event originated.
 * @param motionRange MotionRange of the movement
 * @param axisDir     Either '-' or '+'
 */
private void saveMotionInput(InputDevice device, InputDevice.MotionRange motionRange,
        char axisDir)
{
  String bindStr =
          "Device '" + device.getDescriptor() + "'-Axis " + motionRange.getAxis() + axisDir;
  String uiString = device.getName() + ": Axis " + motionRange.getAxis() + axisDir;

  saveInput(bindStr, uiString);
}
 
Example 7
Source File: EmulationActivity.java    From citra_android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean dispatchGenericMotionEvent(MotionEvent event)
{
  if (mMenuVisible)
  {
    return false;
  }

  if (((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) == 0))
  {
    return super.dispatchGenericMotionEvent(event);
  }

  // Don't attempt to do anything if we are disconnecting a device.
  if (event.getActionMasked() == MotionEvent.ACTION_CANCEL)
    return true;

  InputDevice input = event.getDevice();
  List<InputDevice.MotionRange> motions = input.getMotionRanges();

  float[] axisValues = {0.0f, 0.0f};
  for (InputDevice.MotionRange range : motions)
  {
    boolean consumed = false;
    int axis = range.getAxis();
    float origValue = event.getAxisValue(axis);
    float value = mControllerMappingHelper.scaleAxis(input, axis, origValue);

    if (axis == AXIS_X || axis == AXIS_Z)
    {
      axisValues[0] = value;
    }
    else if (axis == AXIS_Y || axis == AXIS_RZ)
    {
      axisValues[1] = value;
    }

    // If the input is still in the "flat" area, that means it's really zero.
    // This is used to compensate for imprecision in joysticks.
    if (Math.abs(axisValues[0]) > range.getFlat() || Math.abs(axisValues[1]) > range.getFlat())
    {
      consumed = NativeLibrary.onGamePadMoveEvent(input.getDescriptor(), axis, axisValues[0], axisValues[1]);
    }
    else
    {
      consumed = NativeLibrary.onGamePadMoveEvent(input.getDescriptor(), axis, 0.0f, 0.0f);
    }

    return NativeLibrary.onGamePadAxisEvent(input.getDescriptor(),axis, value) || consumed;
  }

  return false;
}
 
Example 8
Source File: MotionAlertDialog.java    From citra_android with GNU General Public License v3.0 4 votes vote down vote up
private boolean onMotionEvent(MotionEvent event)
{
  if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) == 0)
    return false;
  if (event.getAction() != MotionEvent.ACTION_MOVE)
    return false;

  InputDevice input = event.getDevice();

  List<InputDevice.MotionRange> motionRanges = input.getMotionRanges();

  int numMovedAxis = 0;
  float axisMoveValue = 0.0f;
  InputDevice.MotionRange lastMovedRange = null;
  char lastMovedDir = '?';
  if (mWaitingForEvent)
  {
    // Get only the axis that seem to have moved (more than .5)
    for (InputDevice.MotionRange range : motionRanges)
    {
      int axis = range.getAxis();
      float origValue = event.getAxisValue(axis);
      float value = mControllerMappingHelper.scaleAxis(input, axis, origValue);
      if (Math.abs(value) > 0.5f)
      {
        // It is common to have multiple axis with the same physical input. For example,
        // shoulder butters are provided as both AXIS_LTRIGGER and AXIS_BRAKE.
        // To handle this, we ignore an axis motion that's the exact same as a motion
        // we already saw. This way, we ignore axis with two names, but catch the case
        // where a joystick is moved in two directions.
        // ref: bottom of https://developer.android.com/training/game-controllers/controller-input.html
        if (value != axisMoveValue)
        {
          axisMoveValue = value;
          numMovedAxis++;
          lastMovedRange = range;
          lastMovedDir = value < 0.0f ? '-' : '+';
        }
      }
    }

    // If only one axis moved, that's the winner.
    if (numMovedAxis == 1)
    {
      mWaitingForEvent = false;
      saveMotionInput(input, lastMovedRange, lastMovedDir);
    }
  }

  return true;
}