|
| 1 | +/* |
| 2 | + * Copyright (c) 2009-2021 jMonkeyEngine |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | +package jme3test.input; |
| 33 | + |
| 34 | +import com.jme3.app.SimpleApplication; |
| 35 | +import com.jme3.font.BitmapText; |
| 36 | +import com.jme3.input.ChaseCamera; |
| 37 | +import com.jme3.input.KeyInput; |
| 38 | +import com.jme3.input.controls.ActionListener; |
| 39 | +import com.jme3.input.controls.KeyTrigger; |
| 40 | +import com.jme3.material.Material; |
| 41 | +import com.jme3.math.ColorRGBA; |
| 42 | +import com.jme3.math.FastMath; |
| 43 | +import com.jme3.math.Quaternion; |
| 44 | +import com.jme3.math.Vector3f; |
| 45 | +import com.jme3.scene.Geometry; |
| 46 | +import com.jme3.scene.shape.Quad; |
| 47 | + |
| 48 | +/** |
| 49 | + * A test for issue https://github.com/jMonkeyEngine/jmonkeyengine/pull/1692. |
| 50 | + * We are testing to see if disabling then re-enabling the chase camera keeps the correct flags |
| 51 | + * set so that we can still rotate without dragging |
| 52 | + */ |
| 53 | +public class TestIssue1692 extends SimpleApplication implements ActionListener { |
| 54 | + |
| 55 | + private ChaseCamera chaseCam; |
| 56 | + private BitmapText cameraStatus; |
| 57 | + |
| 58 | + public static void main(String[] args) { |
| 59 | + TestIssue1692 app = new TestIssue1692(); |
| 60 | + app.start(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void simpleInitApp() { |
| 65 | + // Load a teapot model, we will chase this with the camera |
| 66 | + Geometry teaGeom = (Geometry) assetManager.loadModel("Models/Teapot/Teapot.obj"); |
| 67 | + Material teapotMaterial = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md"); |
| 68 | + teaGeom.setMaterial(teapotMaterial); |
| 69 | + rootNode.attachChild(teaGeom); |
| 70 | + |
| 71 | + // Load a floor model |
| 72 | + Material floorMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); |
| 73 | + floorMaterial.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg")); |
| 74 | + Geometry ground = new Geometry("ground", new Quad(50, 50)); |
| 75 | + ground.setLocalRotation(new Quaternion().fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X)); |
| 76 | + ground.setLocalTranslation(-25, -1, 25); |
| 77 | + ground.setMaterial(floorMaterial); |
| 78 | + rootNode.attachChild(ground); |
| 79 | + |
| 80 | + // Disable the default first-person cam! |
| 81 | + flyCam.setEnabled(false); |
| 82 | + |
| 83 | + // Enable a chase cam |
| 84 | + chaseCam = new ChaseCamera(cam, teaGeom, inputManager); |
| 85 | + /* |
| 86 | + * Explicitly set drag to rotate to false. |
| 87 | + * We are testing to see if disabling then re-enabling the camera keeps the correct flags |
| 88 | + * set so that we can still rotate without dragging. |
| 89 | + */ |
| 90 | + chaseCam.setDragToRotate(false); |
| 91 | + |
| 92 | + // Show instructions |
| 93 | + int yTop = settings.getHeight(); |
| 94 | + int size = guiFont.getCharSet().getRenderedSize(); |
| 95 | + BitmapText hudText = new BitmapText(guiFont); |
| 96 | + hudText.setSize(size); |
| 97 | + hudText.setColor(ColorRGBA.Blue); |
| 98 | + hudText.setText("This test is for issue 1692.\n" |
| 99 | + + "We are testing to see if drag to rotate stays disabled" |
| 100 | + + "after disabling and re-enabling the chase camera.\n" |
| 101 | + + "For this test, use the SPACE key to disable and re-enable the camera."); |
| 102 | + hudText.setLocalTranslation(0, yTop - (hudText.getLineHeight() * 3), 0); |
| 103 | + guiNode.attachChild(hudText); |
| 104 | + |
| 105 | + // Show camera status |
| 106 | + cameraStatus = new BitmapText(guiFont); |
| 107 | + cameraStatus.setSize(size); |
| 108 | + cameraStatus.setColor(ColorRGBA.Blue); |
| 109 | + cameraStatus.setLocalTranslation(0, yTop - cameraStatus.getLineHeight(), 0); // position |
| 110 | + guiNode.attachChild(cameraStatus); |
| 111 | + |
| 112 | + // Register inputs |
| 113 | + registerInput(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void simpleUpdate(float tpf) { |
| 118 | + // Update chaseCam status |
| 119 | + cameraStatus.setText("chaseCam " + (chaseCam.isEnabled() ? "enabled" : "disabled")); |
| 120 | + } |
| 121 | + |
| 122 | + private void registerInput() { |
| 123 | + inputManager.addMapping("toggleCamera", new KeyTrigger(KeyInput.KEY_SPACE)); |
| 124 | + inputManager.addListener(this, "toggleCamera"); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void onAction(String name, boolean keyPressed, float tpf) { |
| 129 | + if (name.equals("toggleCamera") && keyPressed) { |
| 130 | + // Toggle chase camera |
| 131 | + chaseCam.setEnabled(!chaseCam.isEnabled()); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments