Example usage for android.view MotionEvent getHistoricalAxisValue

List of usage examples for android.view MotionEvent getHistoricalAxisValue

Introduction

In this page you can find the example usage for android.view MotionEvent getHistoricalAxisValue.

Prototype

public final float getHistoricalAxisValue(int axis, int pos) 

Source Link

Document

#getHistoricalAxisValue(int,int,int) for the first pointer index (may be an arbitrary pointer identifier).

Usage

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   w ww  .  j  a  va2 s  .co m
    return 0;
}

From source file:Main.java

public static float getCenteredAxis(MotionEvent event, InputDevice device, int axis, int historyPos) {
    if (device == null) {
        return 0;
    }//from w  ww.j  av  a 2 s .c  om
    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: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  w w. ja  v  a  2 s .  co m*/
    }
    return 0;
}

From source file:com.fishstix.dosboxfree.DBGLSurfaceView.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private void processJoystickInput(MotionEvent event, int historyPos) {
    float hatX = 0.0f;
    InputDevice.MotionRange range = event.getDevice().getMotionRange(MotionEvent.AXIS_HAT_X, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            hatX = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_HAT_X, historyPos));
        } else {//from  w w  w  . j av  a2  s  .  com
            hatX = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_HAT_X));
        }
    }

    float hatY = 0.0f;
    range = event.getDevice().getMotionRange(MotionEvent.AXIS_HAT_Y, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            hatY = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_HAT_Y, historyPos));
        } else {
            hatY = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_HAT_Y));
        }
    }

    float joyX = 0.0f;
    range = event.getDevice().getMotionRange(MotionEvent.AXIS_X, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            joyX = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_X, historyPos));
        } else {
            joyX = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_X));
        }
    }

    float joyY = 0.0f;
    range = event.getDevice().getMotionRange(MotionEvent.AXIS_Y, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            joyY = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_Y, historyPos));
        } else {
            joyY = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_Y));
        }
    }

    float joy2X = 0.0f;
    range = event.getDevice().getMotionRange(MotionEvent.AXIS_Z, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            joy2X = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_Z, historyPos));
        } else {
            joy2X = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_Z));
        }
    }

    float joy2Y = 0.0f;
    range = event.getDevice().getMotionRange(MotionEvent.AXIS_RZ, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            joy2Y = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_RZ, historyPos));
        } else {
            joy2Y = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_RZ));
        }
    }

    if (mAnalogStickPref == 0) {
        mMouseThread.setCoord(
                (int) ((Math.abs(joyX * 32.0f) > DEADZONE) ? (-joyX * 32.0f * mMouseSensitivityX) : 0),
                (int) ((Math.abs(joyY * 32.0f) > DEADZONE) ? (-joyY * 32.0f * mMouseSensitivityY) : 0));
        DosBoxControl.nativeJoystick((int) ((joy2X * 256.0f) + mJoyCenterX),
                (int) ((joy2Y * 256.0f) + mJoyCenterY), ACTION_MOVE, -1);
    } else {
        mMouseThread.setCoord(
                (int) ((Math.abs(joy2X * 32.0f) > DEADZONE) ? (-joy2X * 32.0f * mMouseSensitivityX) : 0),
                (int) ((Math.abs(joy2Y * 32.0f) > DEADZONE) ? (-joy2Y * 32.0f * mMouseSensitivityY) : 0));
        DosBoxControl.nativeJoystick((int) ((joyX * 256.0f) + mJoyCenterX),
                (int) ((joyY * 256.0f) + mJoyCenterY), ACTION_MOVE, -1);
    }

    // Handle all other keyevents
    int value = 0;
    int tKeyCode = MAP_NONE;

    if (hatX < 0) {
        value = customMap.get(KeyEvent.KEYCODE_DPAD_LEFT);
        if (value > 0) {
            // found a valid mapping
            tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT));
            if (tKeyCode > MAP_NONE) {
                DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift);
            }
        }
        hatXlast = hatX;
    } else if (hatX > 0) {
        value = customMap.get(KeyEvent.KEYCODE_DPAD_RIGHT);
        if (value > 0) {
            // found a valid mapping
            tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT));
            if (tKeyCode > MAP_NONE) {
                DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift);
            }
        }
        hatXlast = hatX;
    } else {
        // released
        if (hatX != hatXlast) {
            if (hatXlast < 0) {
                value = customMap.get(KeyEvent.KEYCODE_DPAD_LEFT);
                if (value > 0) {
                    // found a valid mapping
                    tKeyCode = getMappedKeyCode(value,
                            new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_LEFT));
                    if (tKeyCode > MAP_NONE) {
                        DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt,
                                mModifierShift);
                    }
                }
            } else if (hatXlast > 0) {
                value = customMap.get(KeyEvent.KEYCODE_DPAD_RIGHT);
                if (value > 0) {
                    // found a valid mapping
                    tKeyCode = getMappedKeyCode(value,
                            new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_RIGHT));
                    if (tKeyCode > MAP_NONE) {
                        DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt,
                                mModifierShift);
                    }
                }
            }
        }
        hatXlast = hatX;
    }
    if (hatY < 0) {
        value = customMap.get(KeyEvent.KEYCODE_DPAD_UP);
        if (value > 0) {
            // found a valid mapping
            tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP));
            if (tKeyCode > MAP_NONE) {
                DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift);
            }
        }
        hatYlast = hatY;
    } else if (hatY > 0) {
        value = customMap.get(KeyEvent.KEYCODE_DPAD_DOWN);
        if (value > 0) {
            // found a valid mapping
            tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN));
            if (tKeyCode > MAP_NONE) {
                DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift);
            }
        }
        hatYlast = hatY;
    } else {
        // released
        if (hatY != hatYlast) {
            if (hatYlast < 0) {
                value = customMap.get(KeyEvent.KEYCODE_DPAD_UP);
                if (value > 0) {
                    // found a valid mapping
                    tKeyCode = getMappedKeyCode(value,
                            new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_UP));
                    if (tKeyCode > MAP_NONE) {
                        DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt,
                                mModifierShift);
                    }
                }
            } else if (hatYlast > 0) {
                value = customMap.get(KeyEvent.KEYCODE_DPAD_DOWN);
                if (value > 0) {
                    // found a valid mapping
                    tKeyCode = getMappedKeyCode(value,
                            new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_DOWN));
                    if (tKeyCode > MAP_NONE) {
                        DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt,
                                mModifierShift);
                    }
                }
            }
        }
        hatYlast = hatY;
    }
}