Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(385)

Side by Side Diff: content/public/android/java/src/org/chromium/content/browser/GamepadDevice.java

Issue 133943002: Gamepad API support for chrome on android (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Uploading forgotten things in previous patch Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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;
Ted C 2014/04/04 18:52:18 for all these gamepage files, can you move them in
SaurabhK 2014/04/11 14:41:42 On 2014/04/04 18:52:18, Ted C wrote: Done.
6
7 import android.os.SystemClock;
8 import android.view.InputDevice;
9 import android.view.InputDevice.MotionRange;
10 import android.view.KeyEvent;
11 import android.view.MotionEvent;
12
13 import org.chromium.content.browser.input.CanonicalAxisIndex;
14 import org.chromium.content.browser.input.CanonicalButtonIndex;
15
16 import java.util.Arrays;
17
18 /*
Ted C 2014/04/04 18:52:18 for javadocs, the starting token is /**
SaurabhK 2014/04/11 14:41:42 On 2014/04/04 18:52:18, Ted C wrote: Done.
19 * Class to manage information related to each connected gamepad device.
20 */
21
Ted C 2014/04/04 18:52:18 remove blank line
SaurabhK 2014/04/11 14:41:42 On 2014/04/04 18:52:18, Ted C wrote: Done.
22 class GamepadDevice {
23 // An id for the gamepad.
24 private int mDeviceId;
25 // The index of the gamepad in the Navigator.
26 private int mDeviceIndex;
27 // Last time the data for this gamepad was updated.
28 private long mTimestamp;
29 // If this gamepad is mapped to standard gamepad?
30 private String mMapping;
31
32 // Array of values for all axes of the gamepad.
33 // All axis values must be linearly normalized to the range [-1.0 .. 1.0].
34 // As appropriate, -1.0 should correspond to "up" or "left", and 1.0
35 // should correspond to "down" or "right".
36 private float[] mAxisValues;
37
38 private float[] mButtonsValues;
39
40 // When the user agent recognizes the attached inputDevice, it is recommende d
41 // that it be remapped to a canonical ordering when possible. Devices that a re
42 // not recognized should still be exposed in their raw form. Therefore we mu st
43 // pass the raw Button and raw Axis values.
44 private float[] mRawButtons;
45 private float[] mRawAxes;
46
47 // An identification string for the gamepad.
48 private String mDeviceName;
49
50 // Array of axes ids.
51 private int[] mAxes;
52
53 GamepadDevice(int index, InputDevice inputDevice) {
54 mDeviceIndex = index;
55 mDeviceId = inputDevice.getId();
56 mDeviceName = inputDevice.getName();
57 mTimestamp = SystemClock.uptimeMillis();
58 mMapping = " ";
Ted C 2014/04/04 18:52:18 can you make " " and "standard" constants.
SaurabhK 2014/04/11 14:41:42 On 2014/04/04 18:52:18, Ted C wrote: Now i am kee
59 mButtonsValues = new float[CanonicalButtonIndex.NUM_CANONICAL_BUTTONS];
60 mAxisValues = new float[CanonicalAxisIndex.NUM_CANONICAL_AXES];
61 mRawButtons = new float[256];
62 // Get the total number of axes supported by gamepad.
63 int totalAxes = 0;
64 for (MotionRange range : inputDevice.getMotionRanges()) {
65 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
66 totalAxes++;
67 }
68 }
69 // Get axis ids and initialize axes values.
70 mAxes = new int[totalAxes];
71 mRawAxes = new float[totalAxes];
72 int i = 0;
73 for (MotionRange range : inputDevice.getMotionRanges()) {
74 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
75 mAxes[i] = range.getAxis();
76 mRawAxes[i] = 0.0f;
77 i++;
78 }
79 }
80 }
81
82 GamepadDevice() {}
Ted C 2014/04/04 18:52:18 why do you need the empty constructor?
SaurabhK 2014/04/11 14:41:42 On 2014/04/04 18:52:18, Ted C wrote: Removed it.
83
84 /**
85 * Map the axes and buttons of a gamepad device to a standard gamepad format.
Ted C 2014/04/04 18:52:18 for these javadoc comments and below, the seond an
SaurabhK 2014/04/11 14:41:42 On 2014/04/04 18:52:18, Ted C wrote: Done.
86 */
87 public void mapButtonsAndAxes() {
88 mMapping = GamepadMappings.mapToStandardGamepad(mAxisValues, mButtonsVal ues, mRawAxes,
Ted C 2014/04/04 18:52:18 style nit in java, the indenting is different tha
SaurabhK 2014/04/11 14:41:42 On 2014/04/04 18:52:18, Ted C wrote: Keeping all
89 mRawButtons,
90 mDeviceName) ? "standard " : " ";
91 }
92
93 /**
94 * @return Device Id of the gamepad device.
95 */
96 public int getId() { return mDeviceId; }
97
98 /**
99 * @return Mapping status of the gamepad device.
100 */
101 public String getMapping() { return mMapping; }
102
103 /**
104 * @return Device name of the gamepad device.
105 */
106 public String getName() { return mDeviceName; }
107
108 /**
109 * @return Device index of the gamepad device.
110 */
111 public int getIndex() { return mDeviceIndex; }
112
113 /**
114 * @return The timestamp when the gamepad device was last interacted.
115 */
116 public long getTimestamp() { return mTimestamp; }
117
118 /**
119 * @return The axes state of the gamepad device.
120 */
121 public float[] getAxes() {
Ted C 2014/04/04 18:52:18 for consistency with the ones above, these two sho
SaurabhK 2014/04/11 14:41:42 On 2014/04/04 18:52:18, Ted C wrote: Done.
122 return mAxisValues;
123 }
124
125 /**
126 * @return The buttons state of the gamepad device.
127 */
128 public float[] getButtons() {
129 return mButtonsValues;
130
Ted C 2014/04/04 18:52:18 remove extra blank line
SaurabhK 2014/04/11 14:41:42 On 2014/04/04 18:52:18, Ted C wrote: Done.
131 }
132
133 /**
134 * Reset the axes and buttons data of the gamepad device everytime gamepad da ta access is
135 * paused.
136 */
137 public void clearData() {
138 Arrays.fill(mAxisValues, 0);
139 Arrays.fill(mRawAxes, 0);
140 Arrays.fill(mButtonsValues, 0);
141 Arrays.fill(mRawButtons, 0);
142 }
143
144 /**
145 * Handles key event from the gamepad device.
146 * @return True if the key event from the gamepad device has been consumed.
147 */
148 public boolean updateForEvent(KeyEvent event) {
Ted C 2014/04/04 18:52:18 for consistency with android, I would call this ha
SaurabhK 2014/04/11 14:41:42 On 2014/04/04 18:52:18, Ted C wrote: Done.
149 // Ignore event if it is not for standard gamepad key.
150 if (!GamepadList.isGamepadEvent(event)) return false;
151 int keyCode = event.getKeyCode();
152 assert keyCode < 256;
153 // Button value 0.0 must mean fully unpressed, and 1.0 must mean fully p ressed.
154 if (event.getAction() == KeyEvent.ACTION_DOWN) {
155 mRawButtons[keyCode] = 1.0f;
156 } else if (event.getAction() == KeyEvent.ACTION_UP) {
157 mRawButtons[keyCode] = 0.0f;
158 }
159 mTimestamp = event.getEventTime();
160
161 return true;
162 }
163
164 /**
165 * Handles motion event from the gamepad device.
166 * @return True if the motion event from the gamepad device has been consumed .
167 */
168 public boolean updateForEvent(MotionEvent event) {
169 // Update axes values.
Ted C 2014/04/04 18:52:18 does this need a corresponding isGamepadEvent like
SaurabhK 2014/04/11 14:41:42 On 2014/04/04 18:52:18, Ted C wrote: Added a chec
170 for (int i = 0; i < mAxes.length; ++i) {
Ted C 2014/04/04 18:52:18 i++ in java
SaurabhK 2014/04/11 14:41:42 On 2014/04/04 18:52:18, Ted C wrote: Done.
171 mRawAxes[i] = event.getAxisValue(mAxes[i]);
172 }
173 mTimestamp = event.getEventTime();
174 return true;
175 }
176
177 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698