Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/GamepadMappings.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/GamepadMappings.java b/content/public/android/java/src/org/chromium/content/browser/GamepadMappings.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..09f797043e490448e20e4717b73da2494ac03a0f |
| --- /dev/null |
| +++ b/content/public/android/java/src/org/chromium/content/browser/GamepadMappings.java |
| @@ -0,0 +1,140 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.content.browser; |
| + |
| +import android.view.KeyEvent; |
| + |
| +import org.chromium.content.browser.input.CanonicalAxisIndex; |
| +import org.chromium.content.browser.input.CanonicalButtonIndex; |
| + |
| +/* |
| +* Class to manage mapping information related to each supported gamepad controller device. |
| +*/ |
| +class GamepadMappings { |
| + public static boolean mapToStandardGamepad(float[] mappedAxis, float[] mappedButtons, |
| + float[] rawAxes, float[] rawButtons, |
|
Ted C
2014/04/04 18:52:18
use java indenting
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 18:52:18, Ted C wrote:
Done.
|
| + String deviceName) { |
| + boolean isMapped = true; |
| + if (deviceName.contains("NVIDIA Corporation NVIDIA Controller")) { |
| + mapShieldGamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); |
| + } |
| + else if (deviceName.contains("Microsoft X-Box 360 pad")) { |
| + mapXBox360Gamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); |
| + } |
| + else { |
|
Ted C
2014/04/04 18:52:18
needs to go on the line above with the }
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 18:52:18, Ted C wrote:
Removed else
|
| + mapUnkownGamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); |
| + isMapped = false; |
| + } |
| + return isMapped; |
| + } |
| + |
| + private static void mapCommonButtons(float[] mappedButtons, float[] rawButtons) { |
| + mappedButtons[CanonicalButtonIndex.BUTTON_PRIMARY] = rawButtons[KeyEvent.KEYCODE_BUTTON_A]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_SECONDARY] = |
| + rawButtons[KeyEvent.KEYCODE_BUTTON_B]; |
|
Ted C
2014/04/04 18:52:18
should be indented 8 from the start of the previou
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 18:52:18, Ted C wrote:
Done.
|
| + mappedButtons[CanonicalButtonIndex.BUTTON_TERTIARY] = |
| + rawButtons[KeyEvent.KEYCODE_BUTTON_X]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_QUATERNARY] = |
| + rawButtons[KeyEvent.KEYCODE_BUTTON_Y]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_SHOULDER] = |
| + rawButtons[KeyEvent.KEYCODE_BUTTON_L1]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_SHOULDER] = |
| + rawButtons[KeyEvent.KEYCODE_BUTTON_R1]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_BACK_SELECT] = |
| + rawButtons[KeyEvent.KEYCODE_BUTTON_SELECT]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_START] = |
| + rawButtons[KeyEvent.KEYCODE_BUTTON_START]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_THUMBSTICK] = |
| + rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBL]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_THUMBSTICK] = |
| + rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBR]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_META] = rawButtons[KeyEvent.KEYCODE_BUTTON_MODE]; |
| + } |
| + |
| + private static void mapDpadButtonsToAxes(float[] mappedButtons, float[] rawAxes) { |
| + // Negative value indicates dpad up. |
| + mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = negativeAxisValueAsButton(rawAxes[9]); |
| + // Positive axis value indicates dpad down. |
| + mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] = |
| + positiveAxisValueAsButton(rawAxes[9]); |
| + // Positive axis value indicates dpad right. |
| + mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] = |
| + positiveAxisValueAsButton(rawAxes[8]); |
| + // Negative value indicates dpad left. |
| + mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] = |
| + negativeAxisValueAsButton(rawAxes[8]); |
| + } |
| + |
| + private static void mapAxes(float[] mappedAxis, float[] rawAxes) { |
| + // Standard gamepad can have only four axes. |
| + mappedAxis[CanonicalAxisIndex.AXIS_LEFT_STICK_X] = rawAxes[0]; |
| + mappedAxis[CanonicalAxisIndex.AXIS_LEFT_STICK_Y] = rawAxes[1]; |
| + mappedAxis[CanonicalAxisIndex.AXIS_RIGHT_STICK_X] = rawAxes[4]; |
| + mappedAxis[CanonicalAxisIndex.AXIS_RIGHT_STICK_Y] = rawAxes[5]; |
| + } |
| + |
| + private static float negativeAxisValueAsButton(float input) { |
| + return (input < -0.5f) ? 1.f : 0.f; |
| + } |
| + |
| + private static float positiveAxisValueAsButton(float input) { |
| + return (input > 0.5f) ? 1.f : 0.f; |
| + } |
| + |
| + /** |
| + * Method for mapping Nvidia gamepad axis and button values |
| + * to standard gamepad button and axes values. |
| + */ |
| + private static void mapShieldGamepad(float[] mappedButtons, float[] rawButtons, |
| + float[] mappedAxis, float[] rawAxes) { |
| + mapCommonButtons(mappedButtons, rawButtons); |
| + |
| + mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = rawAxes[2]; // or rawAxes[3]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = rawAxes[6]; // or rawAxes[7]; |
| + |
| + mapDpadButtonsToAxes(mappedButtons, rawAxes); |
| + mapAxes(mappedAxis, rawAxes); |
| + } |
| + |
| + /** |
| + * Method for mapping Microsoft XBox 360 gamepad axis and button values |
| + * to standard gamepad button and axes values. |
| + */ |
| + private static void mapXBox360Gamepad(float[] mappedButtons, float[] rawButtons, |
| + float[] mappedAxis, float[] rawAxes) { |
| + mapCommonButtons(mappedButtons, rawButtons); |
| + |
| + mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = rawAxes[2]; // or rawAxes[3]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = rawAxes[6]; // or rawAxes[7]; |
| + |
| + mapDpadButtonsToAxes(mappedButtons, rawAxes); |
| + mapAxes(mappedAxis, rawAxes); |
| + } |
| + |
| + /** |
| + * Method for mapping Unkown gamepad axis and button values |
| + * to standard gamepad button and axes values. |
| + */ |
| + private static void mapUnkownGamepad(float[] mappedButtons, float[] rawButtons, |
| + float[] mappedAxis, float[] rawAxes) { |
| + mapCommonButtons(mappedButtons, rawButtons); |
| + |
| + mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = |
| + rawButtons[KeyEvent.KEYCODE_BUTTON_L2]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = |
| + rawButtons[KeyEvent.KEYCODE_BUTTON_R2]; |
| + |
| + mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = rawButtons[KeyEvent.KEYCODE_DPAD_UP]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] = |
| + rawButtons[KeyEvent.KEYCODE_DPAD_DOWN]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] = |
| + rawButtons[KeyEvent.KEYCODE_DPAD_RIGHT]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] = |
| + rawButtons[KeyEvent.KEYCODE_DPAD_LEFT]; |
| + |
| + mapAxes(mappedAxis, rawAxes); |
| + } |
| + |
| +} |