Browse Source

Moved the debug key handling to its own app state.
Also fixed a bug in some SimpleApplication subclasses
that expect access to the guiFont and fpsText from
simpleInit. This is why some believe protected fields
are the work of the devil. ;)


git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9166 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

PSp..om 13 years ago
parent
commit
dac2de12c6

+ 117 - 0
engine/src/core/com/jme3/app/DebugKeysAppState.java

@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2009-2012 jMonkeyEngine
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *   may be used to endorse or promote products derived from this software
+ *   without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.jme3.app;
+
+import com.jme3.app.state.AbstractAppState;
+import com.jme3.app.state.AppStateManager;
+import com.jme3.input.InputManager;
+import com.jme3.input.KeyInput;
+import com.jme3.input.controls.ActionListener;
+import com.jme3.input.controls.KeyTrigger;
+import com.jme3.math.Quaternion;
+import com.jme3.math.Vector3f;
+import com.jme3.renderer.Camera;
+import com.jme3.util.BufferUtils;
+
+
+/**
+ *  Registers a few keys that will dump debug information
+ *  to the console.
+ *
+ *  @author    Paul Speed
+ */
+public class DebugKeysAppState extends AbstractAppState {
+
+    public static final String INPUT_MAPPING_CAMERA_POS = "SIMPLEAPP_CameraPos";
+    public static final String INPUT_MAPPING_MEMORY = "SIMPLEAPP_Memory";
+    
+    private Application app;
+    private DebugKeyListener keyListener = new DebugKeyListener();
+    private InputManager inputManager;
+
+    public DebugKeysAppState() {
+    }    
+
+    @Override
+    public void initialize(AppStateManager stateManager, Application app) {
+        super.initialize(stateManager, app);
+        
+        this.app = app;
+        this.inputManager = app.getInputManager();
+        
+        if (app.getInputManager() != null) {
+ 
+            inputManager.addMapping(INPUT_MAPPING_CAMERA_POS, new KeyTrigger(KeyInput.KEY_C));
+            inputManager.addMapping(INPUT_MAPPING_MEMORY, new KeyTrigger(KeyInput.KEY_M));
+            
+            inputManager.addListener(keyListener, 
+                                     INPUT_MAPPING_CAMERA_POS, 
+                                     INPUT_MAPPING_MEMORY);                   
+        }               
+    }
+            
+    @Override
+    public void cleanup() {
+        super.cleanup();
+
+        if (inputManager.hasMapping(INPUT_MAPPING_CAMERA_POS))
+            inputManager.deleteMapping(INPUT_MAPPING_CAMERA_POS);
+        if (inputManager.hasMapping(INPUT_MAPPING_MEMORY))
+            inputManager.deleteMapping(INPUT_MAPPING_MEMORY);
+        
+        inputManager.removeListener(keyListener);
+    }
+
+    
+    private class DebugKeyListener implements ActionListener {
+
+        public void onAction(String name, boolean value, float tpf) {
+            if (!value) {
+                return;
+            }
+
+            if (name.equals(INPUT_MAPPING_CAMERA_POS)) {
+                Camera cam = app.getCamera();
+                if (cam != null) {
+                    Vector3f loc = cam.getLocation();
+                    Quaternion rot = cam.getRotation();
+                    System.out.println("Camera Position: ("
+                            + loc.x + ", " + loc.y + ", " + loc.z + ")");
+                    System.out.println("Camera Rotation: " + rot);
+                    System.out.println("Camera Direction: " + cam.getDirection());
+                }
+            } else if (name.equals(INPUT_MAPPING_MEMORY)) {
+                BufferUtils.printCurrentDirectMemory(null);
+            }
+        }
+    }
+}

+ 9 - 18
engine/src/core/com/jme3/app/SimpleApplication.java

@@ -67,8 +67,8 @@ import com.jme3.util.BufferUtils;
 public abstract class SimpleApplication extends Application {
 
     public static final String INPUT_MAPPING_EXIT = "SIMPLEAPP_Exit";
-    public static final String INPUT_MAPPING_CAMERA_POS = "SIMPLEAPP_CameraPos";
-    public static final String INPUT_MAPPING_MEMORY = "SIMPLEAPP_Memory";
+    public static final String INPUT_MAPPING_CAMERA_POS = DebugKeysAppState.INPUT_MAPPING_CAMERA_POS;
+    public static final String INPUT_MAPPING_MEMORY = DebugKeysAppState.INPUT_MAPPING_MEMORY;
     public static final String INPUT_MAPPING_HIDE_STATS = "SIMPLEAPP_HideStats";
                                                                          
     protected Node rootNode = new Node("Root Node");
@@ -88,17 +88,6 @@ public abstract class SimpleApplication extends Application {
 
             if (name.equals(INPUT_MAPPING_EXIT)) {
                 stop();
-            } else if (name.equals(INPUT_MAPPING_CAMERA_POS)) {
-                if (cam != null) {
-                    Vector3f loc = cam.getLocation();
-                    Quaternion rot = cam.getRotation();
-                    System.out.println("Camera Position: ("
-                            + loc.x + ", " + loc.y + ", " + loc.z + ")");
-                    System.out.println("Camera Rotation: " + rot);
-                    System.out.println("Camera Direction: " + cam.getDirection());
-                }
-            } else if (name.equals(INPUT_MAPPING_MEMORY)) {
-                BufferUtils.printCurrentDirectMemory(null);
             }else if (name.equals(INPUT_MAPPING_HIDE_STATS)){
                 if (stateManager.getState(StatsAppState.class) != null) {
                     stateManager.getState(StatsAppState.class).toggleStats();
@@ -108,7 +97,7 @@ public abstract class SimpleApplication extends Application {
     }
 
     public SimpleApplication() {
-        this( new StatsAppState(), new FlyCamAppState() );
+        this( new StatsAppState(), new FlyCamAppState(), new DebugKeysAppState() );
     }
 
     public SimpleApplication( AppState... initialStates ) {
@@ -183,6 +172,9 @@ public abstract class SimpleApplication extends Application {
     public void initialize() {
         super.initialize();
 
+        // Several things rely on having this
+        guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
+
         guiNode.setQueueBucket(Bucket.Gui);
         guiNode.setCullHint(CullHint.Never);
         viewPort.attachScene(rootNode);
@@ -204,19 +196,18 @@ public abstract class SimpleApplication extends Application {
                 inputManager.addMapping(INPUT_MAPPING_EXIT, new KeyTrigger(KeyInput.KEY_ESCAPE));
             }
 
-            inputManager.addMapping(INPUT_MAPPING_CAMERA_POS, new KeyTrigger(KeyInput.KEY_C));
-            inputManager.addMapping(INPUT_MAPPING_MEMORY, new KeyTrigger(KeyInput.KEY_M));
             if (stateManager.getState(StatsAppState.class) != null) {
                 inputManager.addMapping(INPUT_MAPPING_HIDE_STATS, new KeyTrigger(KeyInput.KEY_F5));
+                inputManager.addListener(actionListener, INPUT_MAPPING_HIDE_STATS);            
             }
-            inputManager.addListener(actionListener, INPUT_MAPPING_EXIT,
-                    INPUT_MAPPING_CAMERA_POS, INPUT_MAPPING_MEMORY, INPUT_MAPPING_HIDE_STATS);
             
+            inputManager.addListener(actionListener, INPUT_MAPPING_EXIT);            
         }
 
         if (stateManager.getState(StatsAppState.class) != null) {
             // Some of the tests rely on having access to fpsText
             // for quick display.  Maybe a different way would be better.
+            stateManager.getState(StatsAppState.class).setFont(guiFont);
             fpsText = stateManager.getState(StatsAppState.class).getFpsText();
         }
 

+ 15 - 1
engine/src/core/com/jme3/app/StatsAppState.java

@@ -68,6 +68,17 @@ public class StatsAppState extends AbstractAppState {
         this.guiFont = guiFont;
     }
 
+    /**
+     *  Called by SimpleApplication to provide an early font
+     *  so that the fpsText can be created before init.  This
+     *  is because several applications expect to directly access
+     *  fpsText... unfortunately.
+     */
+    void setFont( BitmapFont guiFont ) {
+        this.guiFont = guiFont;
+        this.fpsText = new BitmapText(guiFont, false);
+    }
+
     public BitmapText getFpsText() {
         return fpsText;
     }
@@ -126,7 +137,10 @@ public class StatsAppState extends AbstractAppState {
      *
      */
     public void loadFpsText() {
-        fpsText = new BitmapText(guiFont, false);
+        if (fpsText == null) {
+            fpsText = new BitmapText(guiFont, false);
+        }
+        
         fpsText.setLocalTranslation(0, fpsText.getLineHeight(), 0);
         fpsText.setText("Frames per second");
         fpsText.setCullHint(showFps ? CullHint.Never : CullHint.Always);