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; | |
6 | |
7 /* | |
8 * Class to manage mapping information related to each supported gamepad control ler device. | |
9 */ | |
10 | |
11 class GamepadMappings { | |
12 | |
13 // Standard gamepad buttons. | |
14 private static class StandardButtons { | |
15 public static final int BUTTON_A = 0; | |
16 public static final int BUTTON_B = 1; | |
17 public static final int BUTTON_X = 2; | |
18 public static final int BUTTON_Y = 3; | |
19 public static final int LEFT_TRIGGER = 4; | |
20 public static final int RIGHT_TRIGGER = 5; | |
21 public static final int BOTTOM_LEFT_TRIGGER = 6; | |
22 public static final int BOTTOM_RIGHT_TRIGGER = 7; | |
23 public static final int SELECT = 8; | |
24 public static final int START = 9; | |
25 public static final int LEFT_THUMB = 10; | |
26 public static final int RIGHT_THUMB = 11; | |
27 public static final int DPAD_UP = 12; | |
28 public static final int DPAD_DOWN = 13; | |
29 public static final int DPAD_LEFT = 14; | |
30 public static final int DPAD_RIGHT = 15; | |
31 public static final int MODE = 16; | |
32 public static final int MAX_BUTTONS = 17; | |
33 } | |
34 | |
35 // Standard gamepad axes. | |
36 private static class StandardAxes { | |
37 public static final int AXIS_0 = 0; | |
38 public static final int AXIS_1 = 1; | |
39 public static final int AXIS_2 = 2; | |
40 public static final int AXIS_3 = 3; | |
41 public static final int MAX_AXES = 4; | |
42 } | |
43 | |
44 // We guess the gamepad inputDevice type from its name and then map it to th e | |
45 // standard gamepad if it is one of the known gamepads. | |
46 protected static enum GamepadType { | |
47 Shield, | |
48 XBox360, | |
49 Unknown | |
50 } | |
51 | |
52 // Following values should be changed if the gamepad layout changes | |
53 // or frameworks exposes different axes / buttons for a particular gamepad. | |
54 // mRawShieldAxes is the total raw axes on known shield gamepad. | |
55 protected static final int mRawShieldAxes = 10; | |
56 // mRawShieldButtons is the total raw buttons on known shield gamepad. | |
57 protected static final int mRawShieldButtons = 10; | |
58 // mRawXBoxAxes is the total raw axes on known XBox gamepad. | |
59 protected static final int mRawXBoxAxes = 10; | |
60 // mRawXBoxButtons is the total raw buttons on known XBox gamepad. | |
61 protected static final int mRawXBoxButtons = 9; | |
62 | |
63 public static float[] mapToStandardGamepadAxes(float[] rawAxes, GamepadType gamepadType) { | |
bajones
2014/02/28 23:56:11
The way this is set up now feels like it will be a
SaurabhK
2014/03/11 17:39:27
On 2014/02/28 23:56:11, bajones wrote:
The new pa
| |
64 switch (gamepadType) { | |
65 case Shield: | |
66 return mapShieldAxes(rawAxes); | |
67 case XBox360: | |
68 return mapXBox360Axes(rawAxes); | |
69 } | |
70 return rawAxes; | |
71 } | |
72 | |
73 public static float[] mapToStandardGamepadButtons(float[] rawButtons, float[ ] rawAxes, | |
74 GamepadType gamepadType) { | |
75 switch (gamepadType) { | |
76 case Shield: | |
77 return mapShieldButtons(rawButtons, rawAxes); | |
78 case XBox360: | |
79 return mapXBox360Buttons(rawButtons, rawAxes); | |
80 } | |
81 return rawAxes; | |
82 } | |
83 | |
84 private static float negativeAxisValueAsButton(float input) { | |
85 return (input < -0.5f) ? 1.f : 0.f; | |
86 } | |
87 | |
88 private static float positiveAxisValueAsButton(float input) { | |
89 return (input > 0.5f) ? 1.f : 0.f; | |
90 } | |
91 | |
92 /** | |
93 * Method for mapping Nvidia gamepad axis values to | |
94 * standard gamepad axis values. | |
95 */ | |
96 private static float[] mapShieldAxes(float[] rawAxes) { | |
97 float[] values = new float[StandardAxes.MAX_AXES]; | |
98 // Standard gamepad can have only four axes. | |
99 values[StandardAxes.AXIS_0] = rawAxes[0]; | |
100 values[StandardAxes.AXIS_1] = rawAxes[1]; | |
101 values[StandardAxes.AXIS_2] = rawAxes[4]; | |
102 values[StandardAxes.AXIS_3] = rawAxes[5]; | |
103 return values; | |
104 } | |
105 | |
106 /** | |
107 * Method for mapping Nvidia gamepad axis and button values | |
108 * to standard gamepad button values. | |
109 */ | |
110 private static float[] mapShieldButtons(float[] rawButtons, float[] rawAxes) { | |
111 float[] values = new float[StandardButtons.MAX_BUTTONS]; | |
112 // First four buttons on Nvidia gamepad appear in correct order so no ma pping is required. | |
113 values[StandardButtons.BUTTON_A] = rawButtons[0]; | |
114 values[StandardButtons.BUTTON_B] = rawButtons[1]; | |
115 values[StandardButtons.BUTTON_X] = rawButtons[2]; | |
116 values[StandardButtons.BUTTON_Y] = rawButtons[3]; | |
117 // Third axis on Nvidia gamepad acts as a bottom left trigger button. | |
118 values[StandardButtons.BOTTOM_LEFT_TRIGGER] = rawAxes[2]; | |
119 values[StandardButtons.LEFT_TRIGGER] = rawButtons[4]; | |
120 values[StandardButtons.RIGHT_TRIGGER] = rawButtons[5]; | |
121 values[StandardButtons.SELECT] = rawButtons[6]; | |
122 values[StandardButtons.START] = rawButtons[7]; | |
123 values[StandardButtons.LEFT_THUMB] = rawButtons[8]; | |
124 values[StandardButtons.RIGHT_THUMB] = rawButtons[9]; | |
125 // Seventh axis on Nvidia gamepad acts as a bottom right trigger button. | |
126 values[StandardButtons.BOTTOM_RIGHT_TRIGGER] = rawAxes[6]; | |
127 // Ninth axis on Nvidia gamepad acts as directional pad right and left b utton. | |
128 // Positive axis value indicates dpad right. | |
129 // Negative value indicates dpad left. | |
130 values[StandardButtons.DPAD_RIGHT] = positiveAxisValueAsButton(rawAxes[8 ]); | |
131 values[StandardButtons.DPAD_LEFT] = negativeAxisValueAsButton(rawAxes[8] ); | |
132 // Tenth axis on Nvidia gamepad acts as directional pad up and down butt on. | |
133 // Positive axis value indicates dpad down. | |
134 // Negative value indicates dpad up. | |
135 values[StandardButtons.DPAD_DOWN] = positiveAxisValueAsButton(rawAxes[9] ); | |
136 values[StandardButtons.DPAD_UP] = negativeAxisValueAsButton(rawAxes[9]); | |
137 // Other standard buttons are either not present on the gamepad or not e xposed to an | |
138 // application. | |
139 values[StandardButtons.MODE] = 0.0f; | |
140 return values; | |
141 } | |
142 | |
143 /** | |
144 * Method for mapping Microsoft XBox 360 gamepad axis values to | |
145 * standard gamepad axis values. | |
146 */ | |
147 private static float[] mapXBox360Axes(float[] rawAxes) { | |
148 float[] values = new float[StandardAxes.MAX_AXES]; | |
149 // Standard gamepad can have only four axes. | |
150 values[StandardAxes.AXIS_0] = rawAxes[0]; | |
151 values[StandardAxes.AXIS_1] = rawAxes[1]; | |
152 values[StandardAxes.AXIS_2] = rawAxes[4]; | |
153 values[StandardAxes.AXIS_3] = rawAxes[5]; | |
154 return values; | |
155 } | |
156 | |
157 /** | |
158 * Method for mapping Microsoft XBox 360 gamepad axis and button values | |
159 * to standard gamepad button values. | |
160 */ | |
161 private static float[] mapXBox360Buttons(float[] rawButtons, float[] rawAxes ) { | |
162 float[] values = new float[StandardButtons.MAX_BUTTONS]; | |
163 // First four buttons on Microsoft XBox 360 gamepad appear in correct or der so no mapping | |
164 // is required. | |
165 values[StandardButtons.BUTTON_A] = rawButtons[0]; | |
166 values[StandardButtons.BUTTON_B] = rawButtons[1]; | |
167 values[StandardButtons.BUTTON_X] = rawButtons[2]; | |
168 values[StandardButtons.BUTTON_Y] = rawButtons[3]; | |
169 // Third axis on Microsoft XBox 360 gamepad acts as a left bottom should er button. | |
170 values[StandardButtons.BOTTOM_LEFT_TRIGGER] = rawAxes[2]; | |
171 values[StandardButtons.LEFT_TRIGGER] = rawButtons[4]; | |
172 values[StandardButtons.RIGHT_TRIGGER] = rawButtons[5]; | |
173 values[StandardButtons.START] = rawButtons[6]; | |
174 values[StandardButtons.LEFT_THUMB] = rawButtons[7]; | |
175 values[StandardButtons.RIGHT_THUMB] = rawButtons[8]; | |
176 // Seventh axis on Microsoft XBox 360 gamepad acts as a right bottom sho ulder button. | |
177 values[StandardButtons.BOTTOM_RIGHT_TRIGGER] = rawAxes[6]; | |
178 // Ninth axis on Microsoft XBox 360 gamepad acts as directional pad righ t and left button. | |
179 // Positive axis value indicates dpad right. | |
180 // Negative value indicates dpad left. | |
181 values[StandardButtons.DPAD_RIGHT] = positiveAxisValueAsButton(rawAxes[8 ]); | |
182 values[StandardButtons.DPAD_LEFT] = negativeAxisValueAsButton(rawAxes[8] ); | |
183 // Tenth axis on Microsoft XBox 360 gamepad acts as directional pad up a nd down button. | |
184 // Positive axis value indicates dpad down. | |
185 // Negative value indicates dpad up. | |
186 values[StandardButtons.DPAD_DOWN] = positiveAxisValueAsButton(rawAxes[9] ); | |
187 values[StandardButtons.DPAD_UP] = negativeAxisValueAsButton(rawAxes[9]); | |
188 // Other standard buttons are either not present on the gamepad or not e xposed to an | |
189 // application. | |
190 values[StandardButtons.SELECT] = 0.0f; | |
191 values[StandardButtons.MODE] = 0.0f; | |
192 return values; | |
193 } | |
194 | |
195 } | |
OLD | NEW |