Browse code

Move the emulated touch listener up into the platform.

This is better in case we want to use the assign other keys to different things.

Also renamed the member to pivotKey.

Jamie Doornbos authored on 28/04/2014 23:58:12
Showing 7 changed files
... ...
@@ -16,8 +16,6 @@
16 16
 package playn.java;
17 17
 
18 18
 import playn.core.Events;
19
-import playn.core.Key;
20
-import playn.core.Keyboard;
21 19
 import playn.core.Mouse;
22 20
 import playn.core.Mouse.ButtonEvent.Impl;
23 21
 import playn.core.TouchImpl;
... ...
@@ -32,23 +30,15 @@ public class JavaEmulatedTouch extends TouchImpl
32 30
   private Point pivot;
33 31
   private float x, y;
34 32
   private int currentId;
35
-  private final Key multiTouchKey;
36
-
37
-  Keyboard.Listener keyListener = new Keyboard.Adapter() {
38
-    @Override public void onKeyUp (playn.core.Keyboard.Event event) {
39
-      if (event.key() == multiTouchKey)
40
-        pivot = new Point(x, y);
41
-    }
42
-  };
43
-
44
-  public JavaEmulatedTouch (Key multiTouchKey) {
45
-    this.multiTouchKey = multiTouchKey;
46
-  }
47 33
 
48 34
   @Override public boolean hasTouch() {
49 35
     return true;
50 36
   }
51 37
 
38
+  void updatePivot () {
39
+    pivot = new Point(x, y);
40
+  }
41
+
52 42
   JavaMouse createMouse (JavaPlatform platform) {
53 43
     return new JavaLWJGLMouse(platform) {
54 44
 
... ...
@@ -21,7 +21,6 @@ import java.util.List;
21 21
 
22 22
 import playn.core.Events;
23 23
 import playn.core.Key;
24
-import playn.core.Touch;
25 24
 
26 25
 public abstract class JavaKeyboard implements playn.core.Keyboard {
27 26
 
... ...
@@ -72,10 +71,10 @@ public abstract class JavaKeyboard implements playn.core.Keyboard {
72 71
     return true;
73 72
   }
74 73
 
75
-  void init(Touch touch) {
74
+  void init(Listener platformListener) {
76 75
     // let our friend the touch emulator have key messages too
77
-    if (touch instanceof JavaEmulatedTouch)
78
-      listeners = new Listener[] { listeners[0], ((JavaEmulatedTouch)touch).keyListener };
76
+    if (platformListener !=null)
77
+      listeners = new Listener[] { listeners[0], platformListener };
79 78
   }
80 79
 
81 80
   void update() {
... ...
@@ -23,7 +23,6 @@ import org.lwjgl.input.Keyboard;
23 23
 
24 24
 import playn.core.Events;
25 25
 import playn.core.Key;
26
-import playn.core.Touch;
27 26
 import playn.core.util.Callback;
28 27
 
29 28
 public class JavaLWJGLKeyboard extends JavaKeyboard {
... ...
@@ -39,13 +38,13 @@ public class JavaLWJGLKeyboard extends JavaKeyboard {
39 38
   }
40 39
 
41 40
   @Override
42
-  void init(Touch touch) {
41
+  void init(Listener platformListener) {
43 42
     try {
44 43
       Keyboard.create();
45 44
     } catch (LWJGLException e) {
46 45
       throw new RuntimeException(e);
47 46
     }
48
-    super.init(touch);
47
+    super.init(platformListener);
49 48
   }
50 49
 
51 50
   @Override
... ...
@@ -68,7 +68,7 @@ public class JavaPlatform extends AbstractPlatform {
68 68
     public boolean emulateTouch;
69 69
 
70 70
     /** If {link #emulateTouch} is set, sets the pivot for a two-finger touch when pressed. */
71
-    public Key multiTouchKey = Key.F11;
71
+    public Key pivotKey = Key.F11;
72 72
 
73 73
     /** If set, converts images into a format for fast GPU uploads when initially loaded versus
74 74
      * doing it on demand when displayed. Assuming asynchronous image loads, this keeps that effort
... ...
@@ -131,6 +131,7 @@ public class JavaPlatform extends AbstractPlatform {
131 131
   private final JavaGraphics graphics;
132 132
   private final JavaMouse mouse;
133 133
   private final JavaAssets assets = new JavaAssets(this);
134
+  private final Keyboard.Listener keyListener;
134 135
 
135 136
   private final ExecutorService _exec = Executors.newFixedThreadPool(4);
136 137
   private final long start = System.nanoTime();
... ...
@@ -151,6 +152,18 @@ public class JavaPlatform extends AbstractPlatform {
151 152
       mouse = createMouse();
152 153
     }
153 154
 
155
+    if (touch instanceof JavaEmulatedTouch) {
156
+      final Key pivotKey = (touch instanceof JavaEmulatedTouch) ? config.pivotKey : null;
157
+      keyListener = new Keyboard.Adapter() {
158
+        @Override public void onKeyUp (playn.core.Keyboard.Event event) {
159
+          if (event.key() == pivotKey)
160
+            ((JavaEmulatedTouch)touch).updatePivot();
161
+        }
162
+      };
163
+    } else {
164
+      keyListener = null;
165
+    }
166
+
154 167
     if (!config.headless) {
155 168
       setTitle(config.appName);
156 169
     }
... ...
@@ -293,7 +306,7 @@ public class JavaPlatform extends AbstractPlatform {
293 306
   }
294 307
   protected TouchImpl createTouch(Config config) {
295 308
     if (config.emulateTouch) {
296
-      return new JavaEmulatedTouch(config.multiTouchKey);
309
+      return new JavaEmulatedTouch();
297 310
     } else {
298 311
       return new TouchStub();
299 312
     }
... ...
@@ -308,7 +321,7 @@ public class JavaPlatform extends AbstractPlatform {
308 321
   protected void init(Game game) {
309 322
     graphics.init();
310 323
     mouse.init();
311
-    keyboard.init(touch);
324
+    keyboard.init(keyListener);
312 325
     game.init();
313 326
   }
314 327
 
... ...
@@ -15,16 +15,11 @@
15 15
  */
16 16
 package playn.java;
17 17
 
18
-import playn.core.Key;
19 18
 import playn.core.Mouse;
20 19
 import playn.core.Mouse.ButtonEvent.Impl;
21 20
 
22 21
 public class SWTEmulatedTouch extends JavaEmulatedTouch
23 22
 {
24
-  public SWTEmulatedTouch (Key multiTouchKey) {
25
-    super(multiTouchKey);
26
-  }
27
-
28 23
   @Override JavaMouse createMouse (JavaPlatform platform) {
29 24
     final JavaEmulatedTouch self = this;
30 25
     return new SWTMouse((SWTPlatform)platform) {
... ...
@@ -19,7 +19,6 @@ import org.eclipse.swt.SWT;
19 19
 
20 20
 import playn.core.Events;
21 21
 import playn.core.Key;
22
-import playn.core.Touch;
23 22
 import playn.core.util.Callback;
24 23
 
25 24
 public class SWTKeyboard extends JavaKeyboard {
... ...
@@ -37,8 +36,8 @@ public class SWTKeyboard extends JavaKeyboard {
37 36
     callback.onFailure(new Exception("TODO"));
38 37
   }
39 38
 
40
-  void init(Touch touch) {
41
-    super.init(touch);
39
+  void init(Listener platformListener) {
40
+    super.init(platformListener);
42 41
 
43 42
     platform.display.addFilter(SWT.KeyDown, new org.eclipse.swt.widgets.Listener() {
44 43
       public void handleEvent (org.eclipse.swt.widgets.Event event) {
... ...
@@ -110,7 +110,7 @@ public class SWTPlatform extends JavaPlatform {
110 110
 
111 111
   @Override protected TouchImpl createTouch (Config config) {
112 112
     if (config.emulateTouch) {
113
-      return new SWTEmulatedTouch(config.multiTouchKey);
113
+      return new SWTEmulatedTouch();
114 114
     } else {
115 115
       return super.createTouch(config);
116 116
     }