Browse code

resizable window on playn java version

devnewton authored on 27/05/2014 at 18:38:25
Showing 2 changed files
... ...
@@ -56,10 +56,10 @@ public class VirtualPad {
56 56
     private static final Logger LOGGER = Logger.getLogger(VirtualPad.class.getName());
57 57
     private final RealWatchedAssets assets;
58 58
     private ImageLayer virtualPadUp;
59
-    private Layer virtualPadRight;
60
-    private Layer virtualPadLeft;
61
-    private Layer virtualPadRotateClockwise;
62
-    private Layer virtualPadRotateCounterClockwise;
59
+    private ImageLayer virtualPadRight;
60
+    private ImageLayer virtualPadLeft;
61
+    private ImageLayer virtualPadRotateClockwise;
62
+    private ImageLayer virtualPadRotateCounterClockwise;
63 63
     private final VirtualControl virtualControlRight = new VirtualControl("Virtual right");
64 64
     private final VirtualControl virtualControlLeft = new VirtualControl("Virtual left");
65 65
     private final VirtualControl virtualControlUp = new VirtualControl("Virtual up");
... ...
@@ -117,7 +117,7 @@ public class VirtualPad {
117 117
             @Override
118 118
             public Layer hitTest(Layer layer, Point p) {
119 119
                 Layer defaultResult = layer.hitTestDefault(p);
120
-                if(null != defaultResult) {
120
+                if (null != defaultResult) {
121 121
                     return defaultResult;
122 122
                 } else {
123 123
                     return layer;
... ...
@@ -138,8 +138,6 @@ public class VirtualPad {
138 138
                 virtualPadUp = PlayN.graphics().createImageLayer(upImage);
139 139
                 virtualPadUp.setVisible(false);
140 140
                 virtualPadUp.setAlpha(0.4f);
141
-                virtualPadUp.setTranslation(0.0f, PlayN.graphics().height() * 1.5f / 4.0f);
142
-                virtualPadUp.setScale(PlayN.graphics().width() / 4.0f / upImage.width(), PlayN.graphics().height() / 4.0f / upImage.height());
143 141
                 virtualPadUp.addListener(new Pointer.Adapter() {
144 142
 
145 143
                     @Override
... ...
@@ -170,8 +168,6 @@ public class VirtualPad {
170 170
                 virtualPadRotateCounterClockwise = PlayN.graphics().createImageLayer(rotateCounterClockwiseImage);
171 171
                 virtualPadRotateCounterClockwise.setVisible(false);
172 172
                 virtualPadRotateCounterClockwise.setAlpha(0.4f);
173
-                virtualPadRotateCounterClockwise.setTranslation(0.0f, 0.0f);
174
-                virtualPadRotateCounterClockwise.setScale(PlayN.graphics().width() / 4.0f / rotateCounterClockwiseImage.width(), PlayN.graphics().height() / 4.0f / rotateCounterClockwiseImage.height());
175 173
                 virtualPadRotateCounterClockwise.addListener(new Pointer.Adapter() {
176 174
 
177 175
                     @Override
... ...
@@ -202,8 +198,6 @@ public class VirtualPad {
202 202
                 virtualPadLeft = PlayN.graphics().createImageLayer(leftImage);
203 203
                 virtualPadLeft.setVisible(false);
204 204
                 virtualPadLeft.setAlpha(0.4f);
205
-                virtualPadLeft.setTranslation(0.0f, PlayN.graphics().height() * 3.0f / 4.0f);
206
-                virtualPadLeft.setScale(PlayN.graphics().width() / 4.0f / leftImage.width(), PlayN.graphics().height() / 4.0f / leftImage.height());
207 205
                 virtualPadLeft.addListener(new Pointer.Adapter() {
208 206
 
209 207
                     @Override
... ...
@@ -233,8 +227,6 @@ public class VirtualPad {
233 233
                 virtualPadRight = PlayN.graphics().createImageLayer(rightImage);
234 234
                 virtualPadRight.setVisible(false);
235 235
                 virtualPadRight.setAlpha(0.4f);
236
-                virtualPadRight.setTranslation(PlayN.graphics().width() * 3.0f / 4.0f, PlayN.graphics().height() * 3.0f / 4.0f);
237
-                virtualPadRight.setScale(PlayN.graphics().width() / 4.0f / rightImage.width(), PlayN.graphics().height() / 4.0f / rightImage.height());
238 236
                 virtualPadRight.addListener(new Pointer.Adapter() {
239 237
 
240 238
                     @Override
... ...
@@ -264,8 +256,6 @@ public class VirtualPad {
264 264
                 virtualPadRotateClockwise = PlayN.graphics().createImageLayer(rotateClockwiseImage);
265 265
                 virtualPadRotateClockwise.setVisible(false);
266 266
                 virtualPadRotateClockwise.setAlpha(0.4f);
267
-                virtualPadRotateClockwise.setTranslation(PlayN.graphics().width() * 3.0f / 4.0f, 0.0f);
268
-                virtualPadRotateClockwise.setScale(PlayN.graphics().width() / 4.0f / rotateClockwiseImage.width(), PlayN.graphics().height() / 4.0f / rotateClockwiseImage.height());
269 267
                 virtualPadRotateClockwise.addListener(new Pointer.Adapter() {
270 268
 
271 269
                     @Override
... ...
@@ -315,20 +305,40 @@ public class VirtualPad {
315 315
         final boolean isLevel = game.getCurrentSequence() instanceof LevelSequence;
316 316
         virtualPadLayer.setInteractive(isLevel);
317 317
         if (null != virtualPadUp) {
318
-            virtualPadUp.setVisible(isLevel);
318
+            virtualPadUp.setTranslation(0.0f, PlayN.graphics().height() * 1.5f / 4.0f);
319
+            if (virtualPadUp.image().isReady()) {
320
+                virtualPadUp.setScale(PlayN.graphics().width() / 4.0f / virtualPadUp.image().width(), PlayN.graphics().height() / 4.0f / virtualPadUp.image().height());
321
+                virtualPadUp.setVisible(isLevel);
322
+            }
319 323
         }
320 324
         if (null != virtualPadLeft) {
321 325
             virtualPadLeft.setVisible(isLevel);
326
+            if (virtualPadLeft.image().isReady()) {
327
+                virtualPadLeft.setTranslation(0.0f, PlayN.graphics().height() * 3.0f / 4.0f);
328
+                virtualPadLeft.setScale(PlayN.graphics().width() / 4.0f / virtualPadLeft.image().width(), PlayN.graphics().height() / 4.0f / virtualPadLeft.image().height());
329
+            }
322 330
         }
323 331
         if (null != virtualPadRight) {
324 332
             virtualPadRight.setVisible(isLevel);
333
+            if (virtualPadRight.image().isReady()) {
334
+                virtualPadRight.setTranslation(PlayN.graphics().width() * 3.0f / 4.0f, PlayN.graphics().height() * 3.0f / 4.0f);
335
+                virtualPadRight.setScale(PlayN.graphics().width() / 4.0f / virtualPadRight.image().width(), PlayN.graphics().height() / 4.0f / virtualPadRight.image().height());
336
+            }
325 337
         }
326 338
         final boolean isBonusLevel = game.getCurrentSequence() instanceof BonusSequence;
327 339
         if (null != virtualPadRotateClockwise) {
328 340
             virtualPadRotateClockwise.setVisible(isLevel && !isBonusLevel);
341
+            if (virtualPadRotateClockwise.image().isReady()) {
342
+                virtualPadRotateClockwise.setTranslation(PlayN.graphics().width() * 3.0f / 4.0f, 0.0f);
343
+                virtualPadRotateClockwise.setScale(PlayN.graphics().width() / 4.0f / virtualPadRotateClockwise.image().width(), PlayN.graphics().height() / 4.0f / virtualPadRotateClockwise.image().height());
344
+            }
329 345
         }
330 346
         if (null != virtualPadRotateCounterClockwise) {
331 347
             virtualPadRotateCounterClockwise.setVisible(isLevel && !isBonusLevel);
348
+            if (virtualPadRotateCounterClockwise.image().isReady()) {
349
+                virtualPadRotateCounterClockwise.setTranslation(0.0f, 0.0f);
350
+                virtualPadRotateCounterClockwise.setScale(PlayN.graphics().width() / 4.0f / virtualPadRotateCounterClockwise.image().width(), PlayN.graphics().height() / 4.0f / virtualPadRotateCounterClockwise.image().height());
351
+            }
332 352
         }
333 353
         fadingAction.update(game.getFrameTimeInfos().elapsedTime / 1000000000f);
334 354
         virtualPadLayer.setAlpha(1.0f - fadingAction.getProgress());
... ...
@@ -1,12 +1,12 @@
1 1
 package im.bci.newtonadv.platform.playn.java;
2 2
 
3
-import im.bci.jnuit.playn.java.PlaynNuitJavaDisplay;
4 3
 import im.bci.jnuit.NuitDisplay;
4
+import im.bci.jnuit.playn.java.PlaynNuitJavaDisplay;
5
+import im.bci.newtonadv.platform.playn.core.PlaynNewtonAdventureGame;
6
+import org.lwjgl.opengl.Display;
5 7
 import playn.core.PlayN;
6 8
 import playn.java.JavaPlatform;
7 9
 
8
-import im.bci.newtonadv.platform.playn.core.PlaynNewtonAdventureGame;
9
-
10 10
 public class PlaynNewtonAdventureGameJava {
11 11
 
12 12
     public static void main(String[] args) {
... ...
@@ -15,12 +15,30 @@ public class PlaynNewtonAdventureGameJava {
15 15
         JavaPlatform.register(config);
16 16
         PlayN.run(new PlaynNewtonAdventureGame() {
17 17
 
18
+            int width, height;
19
+
18 20
             @Override
19 21
             protected NuitDisplay createNuitDisplay() {
20 22
                 return new PlaynNuitJavaDisplay();
21 23
             }
22 24
 
23 25
             @Override
26
+            public void init() {
27
+                Display.setResizable(true);
28
+                super.init();
29
+            }
30
+
31
+            @Override
32
+            public void paint(float alpha) {
33
+                if (width != Display.getWidth() || height != Display.getHeight()) {
34
+                    width = Display.getWidth();
35
+                    height = Display.getHeight();
36
+                    PlayN.graphics().ctx().setSize(Display.getWidth(), Display.getHeight());
37
+                }
38
+                super.paint(alpha);
39
+            }
40
+
41
+            @Override
24 42
             protected void finish() {
25 43
                 System.exit(0);
26 44
             }