Browse code

add Gamepads html implementation

devnewton authored on 28/05/2014 11:44:13
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,47 @@
1
+package playn.html;
2
+
3
+import com.google.gwt.core.client.JavaScriptObject;
4
+import playn.core.Gamepad;
5
+
6
+/**
7
+ *
8
+ * @author devnewton
9
+ */
10
+public class HtmlGamepad extends JavaScriptObject implements Gamepad {
11
+
12
+    @Override
13
+    public native String name() /*-{
14
+     return this.id;
15
+     }-*/;
16
+
17
+    @Override
18
+    public native int buttonCount() /*-{
19
+     return this.buttons.length;
20
+     }-*/;
21
+
22
+    @Override
23
+    public native float buttonValue(int index) /*-{
24
+        return this.buttons[index].value;
25
+     }-*/;
26
+
27
+    @Override
28
+    public native boolean isButtonInDeadZone(int index) /*-{
29
+        return !this.buttons[index].pressed;
30
+     }-*/;
31
+
32
+    @Override
33
+    public native int axisCount() /*-{
34
+        return this.axes.length;
35
+     }-*/;
36
+
37
+    @Override
38
+    public native float axisValue(int index) /*-{
39
+        return this.axes[index];
40
+     }-*/;
41
+
42
+    @Override
43
+    public native boolean isAxisInDeadZone(int index) /*-{
44
+        return this.axes[index] < 0.1f;
45
+     }-*/;
46
+
47
+}
0 48
new file mode 100644
... ...
@@ -0,0 +1,44 @@
1
+package playn.html;
2
+
3
+import com.google.gwt.core.client.JsArray;
4
+import java.util.ArrayList;
5
+import java.util.List;
6
+import playn.core.Gamepad;
7
+import playn.core.Gamepads;
8
+
9
+/**
10
+ *
11
+ * @author devnewton
12
+ */
13
+public class HtmlGamepads implements Gamepads {
14
+
15
+    private final List<Gamepad> gamepads = new ArrayList<Gamepad>();
16
+
17
+    @Override
18
+    public native boolean hasGamepads() /*-{
19
+     return ('getGamepads' in navigator);
20
+     }-*/;
21
+    
22
+    @Override
23
+    public List<Gamepad> plugged() {
24
+        if (hasGamepads()) {
25
+            updateGamepads();
26
+        }
27
+        return gamepads;
28
+    }
29
+
30
+    private void updateGamepads() {
31
+        JsArray<HtmlGamepad> gamepadArray = nativePlugged();
32
+        if (gamepadArray.length() != gamepads.size()) {
33
+            gamepads.clear();
34
+            for (int i = 0, n = gamepadArray.length(); i < n; ++i) {
35
+                gamepads.add(gamepadArray.get(i));
36
+            }
37
+        }
38
+    }
39
+
40
+    private static native JsArray<HtmlGamepad> nativePlugged() /*-{
41
+     return navigator.getGamepads();
42
+     }-*/;
43
+
44
+}
... ...
@@ -163,7 +163,7 @@ public class HtmlPlatform extends AbstractPlatform {
163 163
   private final HtmlPointer pointer;
164 164
   private final HtmlMouse mouse;
165 165
   private final HtmlTouch touch;
166
-  private final Gamepads gamepads = new GamepadsStub();
166
+  private final Gamepads gamepads = new HtmlGamepads();
167 167
   private final HtmlStorage storage = new HtmlStorage(this);
168 168
 
169 169
   // installs backwards compat Date.now() if needed and calls it