Browse code

add Gamepads API and java implementation

devnewton authored on 28/05/2014 08:23:03
Showing 12 changed files
... ...
@@ -22,6 +22,8 @@ import android.util.Log;
22 22
 
23 23
 import playn.core.AbstractPlatform;
24 24
 import playn.core.Game;
25
+import playn.core.Gamepads;
26
+import playn.core.GamepadsStub;
25 27
 import playn.core.Json;
26 28
 import playn.core.Mouse;
27 29
 import playn.core.MouseStub;
... ...
@@ -43,6 +45,7 @@ public class AndroidPlatform extends AbstractPlatform {
43 45
   private final AndroidNet net;
44 46
   private final AndroidPointer pointer;
45 47
   private final AndroidStorage storage;
48
+  private final Gamepads gamepads = new GamepadsStub();
46 49
   private final TouchImpl touch;
47 50
   private final Json json;
48 51
   private final long start = System.nanoTime();
... ...
@@ -210,4 +213,9 @@ public class AndroidPlatform extends AbstractPlatform {
210 213
       game.tick(tick());
211 214
     }
212 215
   }
216
+
217
+  @Override
218
+  public Gamepads gamepads() {
219
+    return gamepads;
220
+  }
213 221
 }
214 222
new file mode 100644
... ...
@@ -0,0 +1,45 @@
1
+package playn.core;
2
+
3
+/**
4
+ * Input-device interface for one gamepad
5
+ * 
6
+ * @author devnewton
7
+ */
8
+public interface Gamepad {
9
+
10
+    /**
11
+     * @return gamepad name
12
+     */
13
+    String name();
14
+
15
+    /**
16
+     * @return number of buttons on gamepad
17
+     */
18
+    int buttonCount();
19
+
20
+    /**
21
+     * @return button state value in range [0 .. 1] indicating how much a button is pressed
22
+     */
23
+    float buttonValue(int index);
24
+    
25
+    /**
26
+     * @return return true if button should be considered as not triggered
27
+     */
28
+    boolean isButtonInDeadZone(int index);
29
+
30
+    /**
31
+     * @return number of axis on gamepad
32
+     */
33
+    int axisCount();
34
+
35
+    /**
36
+     * @return axis state value in range [-1 .. 1] 
37
+     */
38
+    float axisValue(int index);
39
+    
40
+    /**
41
+     * @return return true if axis state should be considered as not triggered
42
+     */
43
+    boolean isAxisInDeadZone(int index);
44
+
45
+}
0 46
new file mode 100644
... ...
@@ -0,0 +1,21 @@
1
+package playn.core;
2
+
3
+import java.util.List;
4
+
5
+/**
6
+ * Input-device interface for gamepads
7
+ * 
8
+ * @author devnewton
9
+ */
10
+public interface Gamepads {
11
+    
12
+    /**
13
+     * @return true if current platform support gamepads
14
+     */
15
+    boolean hasGamepads();
16
+
17
+    /**
18
+     * @return list of plugged gamepads
19
+     */
20
+    List<Gamepad> plugged();
21
+}
0 22
new file mode 100644
... ...
@@ -0,0 +1,24 @@
1
+package playn.core;
2
+
3
+import java.util.Collections;
4
+import java.util.List;
5
+
6
+/**
7
+ * A NOOP gamepads service for use on platforms that don't support gamepad
8
+ * interaction.
9
+ *
10
+ * @author devnewton
11
+ */
12
+public class GamepadsStub implements Gamepads {
13
+
14
+    @Override
15
+    public boolean hasGamepads() {
16
+        return false;
17
+    }
18
+
19
+    @Override
20
+    public List<Gamepad> plugged() {
21
+        return Collections.emptyList();
22
+    }
23
+
24
+}
... ...
@@ -63,6 +63,8 @@ public interface Platform {
63 63
   Mouse mouse();
64 64
 
65 65
   Touch touch();
66
+  
67
+  Gamepads gamepads();
66 68
 
67 69
   Storage storage();
68 70
 }
... ...
@@ -220,6 +220,13 @@ public class PlayN {
220 220
   public static Touch touch() {
221 221
     return platform.touch();
222 222
   }
223
+  
224
+  /**
225
+   * Returns the {@link Gamepads} input service.
226
+   */
227
+  public static Gamepads gamepads() {
228
+    return platform.gamepads();
229
+  }
223 230
 
224 231
   /**
225 232
    * Returns the {@link Storage} storage service.
... ...
@@ -77,6 +77,7 @@ public class StubPlatform extends AbstractPlatform {
77 77
 
78 78
   private Touch touch = new TouchStub();
79 79
   private Mouse mouse = new MouseStub();
80
+  private Gamepads gamepads = new GamepadsStub();
80 81
   private Json json = new JsonImpl();
81 82
   private Pointer pointer = new PointerImpl() {};
82 83
   private final long start = System.currentTimeMillis();
... ...
@@ -188,4 +189,9 @@ public class StubPlatform extends AbstractPlatform {
188 189
   public Storage storage() {
189 190
     return storage;
190 191
   }
192
+
193
+  @Override
194
+  public Gamepads gamepads() {
195
+    return gamepads;
196
+  }
191 197
 }
... ...
@@ -25,6 +25,8 @@ import playn.core.Storage;
25 25
 import playn.core.Audio;
26 26
 import playn.core.PlayN;
27 27
 import playn.core.Game;
28
+import playn.core.Gamepads;
29
+import playn.core.GamepadsStub;
28 30
 import playn.core.Json;
29 31
 import playn.core.Keyboard;
30 32
 import playn.core.Net;
... ...
@@ -161,6 +163,7 @@ public class HtmlPlatform extends AbstractPlatform {
161 163
   private final HtmlPointer pointer;
162 164
   private final HtmlMouse mouse;
163 165
   private final HtmlTouch touch;
166
+  private final Gamepads gamepads = new GamepadsStub();
164 167
   private final HtmlStorage storage = new HtmlStorage(this);
165 168
 
166 169
   // installs backwards compat Date.now() if needed and calls it
... ...
@@ -395,4 +398,10 @@ public class HtmlPlatform extends AbstractPlatform {
395 398
   private static native double now () /*-{
396 399
     return Date.now();
397 400
   }-*/;
401
+
402
+  @Override
403
+  public Gamepads gamepads() {
404
+    return gamepads;
405
+  }
406
+  
398 407
 }
... ...
@@ -37,6 +37,8 @@ import cli.MonoTouch.UIKit.UIWindow;
37 37
 
38 38
 import playn.core.AbstractPlatform;
39 39
 import playn.core.Game;
40
+import playn.core.Gamepads;
41
+import playn.core.GamepadsStub;
40 42
 import playn.core.Json;
41 43
 import playn.core.Mouse;
42 44
 import playn.core.MouseStub;
... ...
@@ -166,6 +168,7 @@ public class IOSPlatform extends AbstractPlatform {
166 168
   private final IOSPointer pointer;
167 169
   private final IOSStorage storage;
168 170
   private final IOSTouch touch;
171
+  private final Gamepads gamepads = new GamepadsStub();
169 172
   private final IOSAssets assets;
170 173
 
171 174
   private Game game;
... ...
@@ -447,4 +450,10 @@ public class IOSPlatform extends AbstractPlatform {
447 450
     ORIENT_MAP.put(UIDeviceOrientation.wrap(UIDeviceOrientation.LandscapeRight),
448 451
                    UIInterfaceOrientation.wrap(UIInterfaceOrientation.LandscapeLeft));
449 452
   }
453
+
454
+  @Override
455
+  public Gamepads gamepads() {
456
+    return gamepads;
457
+  }
458
+  
450 459
 }
451 460
new file mode 100644
... ...
@@ -0,0 +1,55 @@
1
+package playn.java;
2
+
3
+import org.lwjgl.input.Controller;
4
+import playn.core.Gamepad;
5
+
6
+/**
7
+ *
8
+ * @author devnewton
9
+ */
10
+class JavaGamepad implements Gamepad {
11
+
12
+    private final Controller controller;
13
+
14
+    JavaGamepad(Controller controller) {
15
+        this.controller = controller;
16
+    }
17
+
18
+    @Override
19
+    public String name() {
20
+        return controller.getName();
21
+    }
22
+
23
+    @Override
24
+    public int buttonCount() {
25
+        return controller.getButtonCount();
26
+    }
27
+
28
+    @Override
29
+    public float buttonValue(int index) {
30
+        return controller.isButtonPressed(index) ? 1.0f : 0.0f;
31
+    }
32
+
33
+    @Override
34
+    public boolean isButtonInDeadZone(int index) {
35
+        return buttonValue(index) < 0.1f;
36
+    }
37
+
38
+    @Override
39
+    public int axisCount() {
40
+        return controller.getAxisCount();
41
+    }
42
+
43
+    @Override
44
+    public float axisValue(int index) {
45
+        return controller.getAxisValue(index);
46
+    }
47
+
48
+    @Override
49
+    public boolean isAxisInDeadZone(int index) {
50
+        float deadZone = controller.getDeadZone(index);
51
+        float value = axisValue(index);
52
+        return value > deadZone || value < -deadZone;
53
+    }
54
+
55
+}
0 56
new file mode 100644
... ...
@@ -0,0 +1,43 @@
1
+package playn.java;
2
+
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+import java.util.logging.Level;
6
+import java.util.logging.Logger;
7
+import org.lwjgl.LWJGLException;
8
+import org.lwjgl.input.Controllers;
9
+import playn.core.Gamepad;
10
+import playn.core.Gamepads;
11
+
12
+/**
13
+ *
14
+ * @author devnewton
15
+ */
16
+class JavaGamepads implements Gamepads {
17
+
18
+    private final List<Gamepad> gamepads = new ArrayList<Gamepad>();
19
+
20
+    public void init() {
21
+        gamepads.clear();
22
+        try {
23
+            Controllers.create();
24
+            for (int i = 0, n = Controllers.getControllerCount(); i < n; ++i) {
25
+                gamepads.add(new JavaGamepad(Controllers.getController(i)));
26
+            }
27
+        } catch (LWJGLException e) {
28
+            Logger.getLogger(JavaGamepads.class.getName()).log(Level.WARNING, "Cannot init gamepads service", e);
29
+        }
30
+
31
+    }
32
+
33
+    @Override
34
+    public boolean hasGamepads() {
35
+        return true;
36
+    }
37
+
38
+    @Override
39
+    public List<Gamepad> plugged() {
40
+        return gamepads;
41
+    }
42
+
43
+}
... ...
@@ -28,6 +28,7 @@ import org.lwjgl.opengl.Display;
28 28
 
29 29
 import playn.core.AbstractPlatform;
30 30
 import playn.core.Game;
31
+import playn.core.Gamepads;
31 32
 import playn.core.Json;
32 33
 import playn.core.Key;
33 34
 import playn.core.Keyboard;
... ...
@@ -132,6 +133,7 @@ public class JavaPlatform extends AbstractPlatform {
132 133
   private final JavaKeyboard keyboard;
133 134
   private final JavaPointer pointer = new JavaPointer();
134 135
   private final TouchImpl touch;
136
+  private final JavaGamepads gamepads;
135 137
   private final JavaGraphics graphics;
136 138
   private final JavaMouse mouse;
137 139
   private final JavaAssets assets = new JavaAssets(this);
... ...
@@ -151,6 +153,7 @@ public class JavaPlatform extends AbstractPlatform {
151 153
     keyboard = createKeyboard();
152 154
     storage = new JavaStorage(this, config);
153 155
     touch = createTouch(config);
156
+    gamepads = new JavaGamepads();
154 157
     if (touch instanceof JavaEmulatedTouch) {
155 158
       mouse = ((JavaEmulatedTouch)touch).createMouse(this);
156 159
     } else {
... ...
@@ -236,6 +239,11 @@ public class JavaPlatform extends AbstractPlatform {
236 239
   public Touch touch() {
237 240
     return touch;
238 241
   }
242
+  
243
+  @Override
244
+  public Gamepads gamepads() {
245
+    return gamepads;
246
+  }
239 247
 
240 248
   @Override
241 249
   public Storage storage() {
... ...
@@ -330,6 +338,7 @@ public class JavaPlatform extends AbstractPlatform {
330 338
     graphics.init();
331 339
     mouse.init();
332 340
     keyboard.init(keyListener);
341
+    gamepads.init();
333 342
     game.init();
334 343
   }
335 344