OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.content.browser.input; |
| 6 |
| 7 import android.view.KeyEvent; |
| 8 |
| 9 /** |
| 10 * Class to manage mapping information related to each supported gamepad control
ler device. |
| 11 */ |
| 12 class GamepadMappings { |
| 13 public static boolean mapToStandardGamepad(float[] mappedAxis, float[] mappe
dButtons, |
| 14 float[] rawAxes, float[] rawButtons, String deviceName) { |
| 15 if (deviceName.contains("NVIDIA Corporation NVIDIA Controller")) { |
| 16 mapShieldGamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); |
| 17 return true; |
| 18 } else if (deviceName.contains("Microsoft X-Box 360 pad")) { |
| 19 mapXBox360Gamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); |
| 20 return true; |
| 21 } |
| 22 |
| 23 mapUnknownGamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); |
| 24 return false; |
| 25 } |
| 26 |
| 27 private static void mapCommonButtons(float[] mappedButtons, float[] rawButto
ns) { |
| 28 mappedButtons[CanonicalButtonIndex.BUTTON_PRIMARY] = rawButtons[KeyEvent
.KEYCODE_BUTTON_A]; |
| 29 mappedButtons[CanonicalButtonIndex.BUTTON_SECONDARY] = |
| 30 rawButtons[KeyEvent.KEYCODE_BUTTON_B]; |
| 31 mappedButtons[CanonicalButtonIndex.BUTTON_TERTIARY] = |
| 32 rawButtons[KeyEvent.KEYCODE_BUTTON_X]; |
| 33 mappedButtons[CanonicalButtonIndex.BUTTON_QUATERNARY] = |
| 34 rawButtons[KeyEvent.KEYCODE_BUTTON_Y]; |
| 35 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_SHOULDER] = |
| 36 rawButtons[KeyEvent.KEYCODE_BUTTON_L1]; |
| 37 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_SHOULDER] = |
| 38 rawButtons[KeyEvent.KEYCODE_BUTTON_R1]; |
| 39 mappedButtons[CanonicalButtonIndex.BUTTON_BACK_SELECT] = |
| 40 rawButtons[KeyEvent.KEYCODE_BUTTON_SELECT]; |
| 41 mappedButtons[CanonicalButtonIndex.BUTTON_START] = |
| 42 rawButtons[KeyEvent.KEYCODE_BUTTON_START]; |
| 43 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_THUMBSTICK] = |
| 44 rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBL]; |
| 45 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_THUMBSTICK] = |
| 46 rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBR]; |
| 47 mappedButtons[CanonicalButtonIndex.BUTTON_META] = rawButtons[KeyEvent.KE
YCODE_BUTTON_MODE]; |
| 48 } |
| 49 |
| 50 private static void mapDpadButtonsToAxes(float[] mappedButtons, float[] rawA
xes) { |
| 51 // Negative value indicates dpad up. |
| 52 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = negativeAxisValueAs
Button(rawAxes[9]); |
| 53 // Positive axis value indicates dpad down. |
| 54 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] = |
| 55 positiveAxisValueAsButton(rawAxes[9]); |
| 56 // Positive axis value indicates dpad right. |
| 57 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] = |
| 58 positiveAxisValueAsButton(rawAxes[8]); |
| 59 // Negative value indicates dpad left. |
| 60 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] = |
| 61 negativeAxisValueAsButton(rawAxes[8]); |
| 62 } |
| 63 |
| 64 private static void mapAxes(float[] mappedAxis, float[] rawAxes) { |
| 65 // Standard gamepad can have only four axes. |
| 66 mappedAxis[CanonicalAxisIndex.AXIS_LEFT_STICK_X] = rawAxes[0]; |
| 67 mappedAxis[CanonicalAxisIndex.AXIS_LEFT_STICK_Y] = rawAxes[1]; |
| 68 mappedAxis[CanonicalAxisIndex.AXIS_RIGHT_STICK_X] = rawAxes[4]; |
| 69 mappedAxis[CanonicalAxisIndex.AXIS_RIGHT_STICK_Y] = rawAxes[5]; |
| 70 } |
| 71 |
| 72 private static float negativeAxisValueAsButton(float input) { |
| 73 return (input < -0.5f) ? 1.f : 0.f; |
| 74 } |
| 75 |
| 76 private static float positiveAxisValueAsButton(float input) { |
| 77 return (input > 0.5f) ? 1.f : 0.f; |
| 78 } |
| 79 |
| 80 /** |
| 81 * Method for mapping Nvidia gamepad axis and button values |
| 82 * to standard gamepad button and axes values. |
| 83 */ |
| 84 private static void mapShieldGamepad(float[] mappedButtons, float[] rawButto
ns, |
| 85 float[] mappedAxis, float[] rawAxes) { |
| 86 mapCommonButtons(mappedButtons, rawButtons); |
| 87 |
| 88 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = rawAxes[2]; |
| 89 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = rawAxes[6]; |
| 90 |
| 91 mapDpadButtonsToAxes(mappedButtons, rawAxes); |
| 92 mapAxes(mappedAxis, rawAxes); |
| 93 } |
| 94 |
| 95 /** |
| 96 * Method for mapping Microsoft XBox 360 gamepad axis and button values |
| 97 * to standard gamepad button and axes values. |
| 98 */ |
| 99 private static void mapXBox360Gamepad(float[] mappedButtons, float[] rawButt
ons, |
| 100 float[] mappedAxis, float[] rawAxes) { |
| 101 mapCommonButtons(mappedButtons, rawButtons); |
| 102 |
| 103 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = rawAxes[2]; |
| 104 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = rawAxes[6]; |
| 105 |
| 106 mapDpadButtonsToAxes(mappedButtons, rawAxes); |
| 107 mapAxes(mappedAxis, rawAxes); |
| 108 } |
| 109 |
| 110 /** |
| 111 * Method for mapping Unkown gamepad axis and button values |
| 112 * to standard gamepad button and axes values. |
| 113 */ |
| 114 private static void mapUnknownGamepad(float[] mappedButtons, float[] rawButt
ons, |
| 115 float[] mappedAxis, float[] rawAxes) { |
| 116 mapCommonButtons(mappedButtons, rawButtons); |
| 117 |
| 118 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = |
| 119 rawButtons[KeyEvent.KEYCODE_BUTTON_L2]; |
| 120 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = |
| 121 rawButtons[KeyEvent.KEYCODE_BUTTON_R2]; |
| 122 |
| 123 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = rawButtons[KeyEvent
.KEYCODE_DPAD_UP]; |
| 124 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] = |
| 125 rawButtons[KeyEvent.KEYCODE_DPAD_DOWN]; |
| 126 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] = |
| 127 rawButtons[KeyEvent.KEYCODE_DPAD_RIGHT]; |
| 128 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] = |
| 129 rawButtons[KeyEvent.KEYCODE_DPAD_LEFT]; |
| 130 |
| 131 mapAxes(mappedAxis, rawAxes); |
| 132 } |
| 133 |
| 134 } |
OLD | NEW |