Browse code

fix html gamepads implementation

devnewton authored on 29/05/2014 18:02:52
Showing 4 changed files
... ...
@@ -1,47 +1,52 @@
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
-}
1
+package playn.html;
2
+
3
+import playn.core.Gamepad;
4
+
5
+/**
6
+ *
7
+ * @author devnewton <devnewton@bci.im>
8
+ */
9
+class HtmlGamepad implements Gamepad {
10
+
11
+    private final HtmlGamepadJavascriptObject js;
12
+
13
+    HtmlGamepad(HtmlGamepadJavascriptObject js) {
14
+        this.js = js;
15
+    }
16
+
17
+    @Override
18
+    public String name() {
19
+        return js.name();
20
+    }
21
+
22
+    @Override
23
+    public int buttonCount() {
24
+        return js.buttonCount();
25
+    }
26
+
27
+    @Override
28
+    public float buttonValue(int index) {
29
+        return js.buttonValue(index);
30
+    }
31
+
32
+    @Override
33
+    public boolean isButtonInDeadZone(int index) {
34
+        return js.isButtonInDeadZone(index);
35
+    }
36
+
37
+    @Override
38
+    public int axisCount() {
39
+        return js.axisCount();
40
+    }
41
+
42
+    @Override
43
+    public float axisValue(int index) {
44
+        return js.axisValue(index);
45
+    }
46
+
47
+    @Override
48
+    public boolean isAxisInDeadZone(int index) {
49
+        return js.isAxisInDeadZone(index);
50
+    }
51
+
52
+}
48 53
new file mode 100644
... ...
@@ -0,0 +1,44 @@
1
+package playn.html;
2
+
3
+import com.google.gwt.core.client.JavaScriptObject;
4
+
5
+/**
6
+ *
7
+ * @author devnewton
8
+ */
9
+public final class HtmlGamepadJavascriptObject extends JavaScriptObject {
10
+
11
+    protected HtmlGamepadJavascriptObject() {
12
+
13
+    }
14
+
15
+    public native String name() /*-{
16
+     return this.id;
17
+     }-*/;
18
+
19
+    public native int buttonCount() /*-{
20
+     return this.buttons.length;
21
+     }-*/;
22
+
23
+    public native float buttonValue(int index) /*-{
24
+     return this.buttons[index].value;
25
+     }-*/;
26
+
27
+    public native boolean isButtonInDeadZone(int index) /*-{
28
+     return !this.buttons[index].pressed;
29
+     }-*/;
30
+
31
+    public native int axisCount() /*-{
32
+     return this.axes.length;
33
+     }-*/;
34
+
35
+    public native float axisValue(int index) /*-{
36
+     return this.axes[index];
37
+     }-*/;
38
+
39
+    public boolean isAxisInDeadZone(int index) {
40
+        float value = axisValue(index);
41
+        return value < 0.1f && value > -0.1f;
42
+    }
43
+
44
+}
... ...
@@ -18,7 +18,7 @@ public class HtmlGamepads implements Gamepads {
18 18
     public native boolean hasGamepads() /*-{
19 19
      return ('getGamepads' in navigator);
20 20
      }-*/;
21
-    
21
+
22 22
     @Override
23 23
     public List<Gamepad> plugged() {
24 24
         if (hasGamepads()) {
... ...
@@ -28,16 +28,20 @@ public class HtmlGamepads implements Gamepads {
28 28
     }
29 29
 
30 30
     private void updateGamepads() {
31
-        JsArray<HtmlGamepad> gamepadArray = nativePlugged();
31
+        JsArray<HtmlGamepadJavascriptObject> gamepadArray = nativePlugged();
32 32
         if (gamepadArray.length() != gamepads.size()) {
33 33
             gamepads.clear();
34 34
             for (int i = 0, n = gamepadArray.length(); i < n; ++i) {
35
-                gamepads.add(gamepadArray.get(i));
35
+                HtmlGamepadJavascriptObject js = gamepadArray.get(i);
36
+                if(null != js) {
37
+                    final HtmlGamepad gamepad = new HtmlGamepad(js);
38
+                    gamepads.add(gamepad);
39
+                }
36 40
             }
37 41
         }
38 42
     }
39 43
 
40
-    private static native JsArray<HtmlGamepad> nativePlugged() /*-{
44
+    private static native JsArray<HtmlGamepadJavascriptObject> nativePlugged() /*-{
41 45
      return navigator.getGamepads();
42 46
      }-*/;
43 47
 
... ...
@@ -28,7 +28,7 @@ class GamepadTest extends Test {
28 28
     @Override
29 29
     public void init() {
30 30
         CanvasImage image = graphics().createImage(32, 32);
31
-        image.canvas().setFillColor(Color.rgb(100,100, 100));
31
+        image.canvas().setFillColor(Color.rgb(100, 100, 100));
32 32
         image.canvas().fillRect(0, 0, image.width(), image.height());
33 33
         movable = graphics().createImageLayer(image);
34 34
         movable.setTranslation(graphics().width() / 2.0f, graphics().height() / 2.0f);
... ...
@@ -39,22 +39,24 @@ class GamepadTest extends Test {
39 39
     public void update(int delta) {
40 40
         if (gamepads().hasGamepads()) {
41 41
             for (Gamepad gamepad : gamepads().plugged()) {
42
-                if (gamepad.axisCount() > 0) {
43
-                    if (!gamepad.isAxisInDeadZone(0)) {
44
-                        float value = gamepad.axisValue(0);
45
-                        movable.transform().translateX(value);
42
+                if (null != gamepad) {
43
+                    if (gamepad.axisCount() > 0) {
44
+                        if (!gamepad.isAxisInDeadZone(0)) {
45
+                            float value = gamepad.axisValue(0);
46
+                            movable.transform().translateX(value);
47
+                        }
46 48
                     }
47
-                }
48
-                if (gamepad.axisCount() > 1) {
49
-                    if (!gamepad.isAxisInDeadZone(1)) {
50
-                        movable.transform().translateY(gamepad.axisValue(1));
49
+                    if (gamepad.axisCount() > 1) {
50
+                        if (!gamepad.isAxisInDeadZone(1)) {
51
+                            movable.transform().translateY(gamepad.axisValue(1));
52
+                        }
51 53
                     }
52
-                }
53
-                if(gamepad.buttonCount() > 0) {
54
-                    if(!gamepad.isButtonInDeadZone(0)) {
55
-                        movable.setTint(Color.rgb(255, 0, 0));
56
-                    } else {
57
-                        movable.setTint(Color.rgb(255, 255, 255));
54
+                    if (gamepad.buttonCount() > 0) {
55
+                        if (!gamepad.isButtonInDeadZone(0)) {
56
+                            movable.setTint(Color.rgb(255, 0, 0));
57
+                        } else {
58
+                            movable.setTint(Color.rgb(255, 255, 255));
59
+                        }
58 60
                     }
59 61
                 }
60 62
             }