Example usage for android.view InputDevice SOURCE_CLASS_JOYSTICK

List of usage examples for android.view InputDevice SOURCE_CLASS_JOYSTICK

Introduction

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

Prototype

int SOURCE_CLASS_JOYSTICK

To view the source code for android.view InputDevice SOURCE_CLASS_JOYSTICK.

Click Source Link

Document

The input source is a joystick.

Usage

From source file:Main.java

@SuppressLint("InlinedApi")
public static String getSourceClassesString(int sources) {
    List<String> names = new ArrayList<String>();
    addString(sources, InputDevice.SOURCE_CLASS_BUTTON, names);
    addString(sources, InputDevice.SOURCE_CLASS_POINTER, names);
    addString(sources, InputDevice.SOURCE_CLASS_TRACKBALL, names);
    addString(sources, InputDevice.SOURCE_CLASS_POSITION, names);
    addString(sources, InputDevice.SOURCE_CLASS_JOYSTICK, names);
    return TextUtils.join(", ", names);
}

From source file:Main.java

/**
 * Gets the name of the source performing an action.
 * /*w  ww  . jav  a2 s.co  m*/
 * @param source A number representing the source.
 * 
 * @return The name of the source.
 */
public static String getSourceName(int source) {
    switch (source) {
    case InputDevice.SOURCE_CLASS_BUTTON:
        return "BUTTON";
    case InputDevice.SOURCE_CLASS_POINTER:
        return "POINTER";
    case InputDevice.SOURCE_CLASS_TRACKBALL:
        return "TRACKBALL";
    case InputDevice.SOURCE_CLASS_POSITION:
        return "POSITION";
    case InputDevice.SOURCE_CLASS_JOYSTICK:
        return "JOYSTICK";
    case InputDevice.SOURCE_DPAD:
        return "dpad";
    case InputDevice.SOURCE_GAMEPAD:
        return "gamepad";
    case InputDevice.SOURCE_JOYSTICK:
        return "joystick";
    case InputDevice.SOURCE_KEYBOARD:
        return "keyboard";
    case InputDevice.SOURCE_MOUSE:
        return "mouse";
    case InputDevice.SOURCE_STYLUS:
        return "stylus";
    case InputDevice.SOURCE_TOUCHPAD:
        return "touchpad";
    case InputDevice.SOURCE_TOUCHSCREEN:
        return "touchscreen";
    case InputDevice.SOURCE_TRACKBALL:
        return "trackball";
    case InputDevice.SOURCE_UNKNOWN:
        return "unknown";
    default:
        return "source_" + source;
    }
}

From source file:foam.jellyfish.StarwispActivity.java

@Override
public boolean onGenericMotionEvent(final MotionEvent event) {
    //Get the player #
    int player = OuyaController.getPlayerNumByDeviceId(event.getDeviceId());

    Log.i("starwisp", "ogme");

    // Joystick/*from w w w.j av  a  2  s . c o m*/
    if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
        //Get all the axis for the event
        float LS_X = event.getAxisValue(OuyaController.AXIS_LS_X);
        float LS_Y = event.getAxisValue(OuyaController.AXIS_LS_Y);

        float RS_X = event.getAxisValue(OuyaController.AXIS_RS_X);
        float RS_Y = event.getAxisValue(OuyaController.AXIS_RS_Y);
        float L2 = event.getAxisValue(OuyaController.AXIS_L2);
        float R2 = event.getAxisValue(OuyaController.AXIS_R2);

        Log.i("starwisp", "controller " + LS_X + " " + LS_Y + RS_X + " " + RS_Y + L2 + " " + R2);

        Scheme.eval("(on-fling " + LS_X * -500 + " " + LS_Y * -500 + ")");
    }

    //Touchpad
    if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
        //Print the pixel coordinates of the cursor
        Log.i("starwisp", "Cursor X: " + event.getX() + "Cursor Y: " + event.getY());
    }

    return true;
}

From source file:com.vladstirbu.cordova.Gamepad.java

/**
 * @param cordova The context of the main Activity.
 * @param webView The associated CordovaWebView.
 *//*from w  w w.ja  va2s.  c o  m*/
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);

    this.map.put("KEYCODE_BUTTON_A", 0);
    this.map.put("KEYCODE_BUTTON_B", 1);
    this.map.put("KEYCODE_BUTTON_Y", 3);
    this.map.put("KEYCODE_BUTTON_X", 2);
    this.map.put("KEYCODE_BUTTON_L1", 4);
    this.map.put("KEYCODE_BUTTON_R1", 5);
    this.map.put("KEYCODE_BUTTON_L2", 6);
    this.map.put("KEYCODE_BUTTON_R2", 7);
    this.map.put("KEYCODE_SPACE", 8);
    this.map.put("KEYCODE_SELECT", 8);
    this.map.put("KEYCODE_ENTER", 9);
    this.map.put("KEYCODE_START", 9);
    this.map.put("KEYCODE_BUTTON_THUMBL", 10);
    this.map.put("KEYCODE_BUTTON_THUMBR", 11);
    this.map.put("KEYCODE_DPAD_UP", 12);
    this.map.put("KEYCODE_DPAD_DOWN", 13);
    this.map.put("KEYCODE_DPAD_LEFT", 14);
    this.map.put("KEYCODE_DPAD_RIGHT", 15);
    this.map.put("KEYCODE_BACK", 16);
    this.map.put("KEYCODE_BUTTON_MODE", 16);

    this.webView.setFocusable(true);
    this.webView.setFocusableInTouchMode(true);
    this.webView.requestFocus();

    this.webView.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            //Log.v("Keyboards", String.valueOf(InputDevice.getDeviceIds().length));
            //Log.v("Input", InputDevice.getDevice(1).getName());
            //Log.v("Input", String.valueOf(InputDevice.getDevice(1).getSources()));

            //Log.v("Device id", String.valueOf(event.getDeviceId()));
            //Log.v("Source id", String.valueOf(event.getSource()));
            //Log.v("Input device", String.valueOf(InputDevice.getDevice(event.getDeviceId()).getName()));
            Log.v("KEY", String.valueOf(event.getScanCode()));
            Log.v("KEY", KeyEvent.keyCodeToString(keyCode));
            //Log.v("GamePad", String.valueOf(KeyEvent.isGamepadButton(keyCode)));

            String jsStr = jsString(keyCode, event);
            if (!jsStr.isEmpty()) {
                self.webView.sendJavascript(jsStr);
            }
            return true;
        }
    });
    this.webView.setOnGenericMotionListener(new OnGenericMotionListener() {
        public boolean onGenericMotion(View v, MotionEvent event) {
            if (event.isFromSource(InputDevice.SOURCE_CLASS_JOYSTICK)) {
                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    // process the joystick movement...
                    JSONObject data = new JSONObject();
                    JSONArray axes = new JSONArray();
                    try {
                        axes.put(event.getAxisValue(MotionEvent.AXIS_X));
                        axes.put(event.getAxisValue(MotionEvent.AXIS_Y));
                        axes.put(event.getAxisValue(MotionEvent.AXIS_Z));
                        axes.put(event.getAxisValue(MotionEvent.AXIS_RZ));
                        data.put("deviceId", event.getDeviceId());
                        data.put("axes", axes);
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    self.webView.sendJavascript(
                            "cordova.fireWindowEvent('GamepadMotion', " + data.toString() + ");");

                    data = new JSONObject();
                    try {
                        data.put("deviceId", event.getDeviceId());
                        data.put("button", 6);
                        data.put("value", event.getAxisValue(MotionEvent.AXIS_LTRIGGER));
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    self.webView.sendJavascript(
                            "cordova.fireWindowEvent('GamepadMotion', " + data.toString() + ");");

                    data = new JSONObject();
                    try {
                        data.put("deviceId", event.getDeviceId());
                        data.put("button", 7);
                        data.put("value", event.getAxisValue(MotionEvent.AXIS_RTRIGGER));
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    self.webView.sendJavascript(
                            "cordova.fireWindowEvent('GamepadMotion', " + data.toString() + ");");

                    float hatX = event.getAxisValue(MotionEvent.AXIS_HAT_X);
                    float hatY = event.getAxisValue(MotionEvent.AXIS_HAT_Y);
                    try {
                        data = new JSONObject();
                        data.put("deviceId", event.getDeviceId());
                        data.put("button", 14);
                        data.put("value", hatX < 0.0f);
                        self.webView.sendJavascript(
                                "cordova.fireWindowEvent('GamepadMotion', " + data.toString() + ");");
                        data = new JSONObject();
                        data.put("deviceId", event.getDeviceId());
                        data.put("button", 15);
                        data.put("value", hatX > 0.0f);
                        self.webView.sendJavascript(
                                "cordova.fireWindowEvent('GamepadMotion', " + data.toString() + ");");
                    } catch (JSONException e) {
                    }
                    try {
                        data = new JSONObject();
                        data.put("deviceId", event.getDeviceId());
                        data.put("button", 12);
                        data.put("value", hatY < 0.0f);
                        self.webView.sendJavascript(
                                "cordova.fireWindowEvent('GamepadMotion', " + data.toString() + ");");
                        data = new JSONObject();
                        data.put("deviceId", event.getDeviceId());
                        data.put("button", 13);
                        data.put("value", hatY > 0.0f);
                        self.webView.sendJavascript(
                                "cordova.fireWindowEvent('GamepadMotion', " + data.toString() + ");");
                    } catch (JSONException e) {
                    }
                }
                Log.v("MOTION", event.toString());
                return true;
            }
            return false;
        }
    });

    Log.v("GamepadButtons", "initialized");
}

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

/**
 * Utility method to determine if input device is a gamepad.
 * // w  w w  .  j a v a 2s  .  c o  m
 * @param device
 * @return
 */
private boolean isGamepad(InputDevice device) {
    if ((device.getSources() & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD
            || (device.getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) {
        return true;
    }
    return false;
}