OSDN Git Service

Set optimal mime types and executable settings.
[mikumikustudio/MikuMikuStudio.git] / src / jmetest / input / controls / TestControls.java
1 /*
2  * Copyright (c) 2003-2009 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 jmetest.input.controls;
33
34 import com.jme.image.Texture;
35 import com.jme.input.KeyInput;
36 import com.jme.input.controls.GameControl;
37 import com.jme.input.controls.GameControlManager;
38 import com.jme.input.controls.binding.KeyboardBinding;
39 import com.jme.input.controls.controller.ActionChangeController;
40 import com.jme.input.controls.controller.Axis;
41 import com.jme.input.controls.controller.ControlChangeListener;
42 import com.jme.input.controls.controller.RotationController;
43 import com.jme.input.controls.controller.ThrottleController;
44 import com.jme.math.Vector3f;
45 import com.jme.scene.shape.Box;
46 import com.jme.scene.state.TextureState;
47 import com.jme.util.TextureManager;
48 import com.jmex.game.StandardGame;
49 import com.jmex.game.state.DebugGameState;
50 import com.jmex.game.state.GameStateManager;
51
52 /**
53  * @author Matthew D. Hicks
54  */
55 public class TestControls {
56         public static void main(String[] args) throws Exception {
57                 // Create StandardGame
58                 final StandardGame game = new StandardGame("Test Controls");
59                 game.start();
60                 
61                 // Create our GameState
62                 DebugGameState state = new DebugGameState(false);
63                 GameStateManager.getInstance().attachChild(state);
64                 state.setActive(true);
65                 
66                 // Create Box
67                 Box box = new Box("Test Node", new Vector3f(), 5.0f, 5.0f, 5.0f);
68                 state.getRootNode().attachChild(box);
69                 TextureState ts = game.getDisplay().getRenderer().createTextureState();
70             Texture t = TextureManager.loadTexture(TestSwingControlEditor.class.getClassLoader().getResource("jmetest/data/images/Monkey.jpg"), Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear);
71             t.setWrap(Texture.WrapMode.Repeat);
72             ts.setTexture(t);
73             box.setRenderState(ts); 
74             box.updateRenderState();
75             state.getRootNode().attachChild(box);
76             
77             // Create our Controls
78             GameControlManager manager = new GameControlManager();
79             GameControl forward = manager.addControl("Forward");
80             forward.addBinding(new KeyboardBinding(KeyInput.KEY_W));
81             GameControl backward = manager.addControl("Backward");
82             backward.addBinding(new KeyboardBinding(KeyInput.KEY_S));
83             GameControl rotateLeft = manager.addControl("Rotate Left");
84             rotateLeft.addBinding(new KeyboardBinding(KeyInput.KEY_A));
85             GameControl rotateRight = manager.addControl("Rotate Right");
86             rotateRight.addBinding(new KeyboardBinding(KeyInput.KEY_D));
87             GameControl exit = manager.addControl("Exit");
88             exit.addBinding(new KeyboardBinding(KeyInput.KEY_ESCAPE));
89             
90             // Configure controls to "make it go"
91             ThrottleController throttle = new ThrottleController(box, forward, 1.0f, backward, -1.0f, 0.05f, 0.5f, 1.0f, false, Axis.Z);
92                 state.getRootNode().addController(throttle);
93             RotationController rotation = new RotationController(box, rotateLeft, rotateRight, 0.2f, Axis.Y);
94             state.getRootNode().addController(rotation);
95             ActionChangeController quit = new ActionChangeController(exit, new ControlChangeListener() {
96                         public void changed(GameControl control, float oldValue, float newValue, float time) {
97                                 if (newValue == 1.0f) {
98                                         game.shutdown();
99                                 }
100                         }
101             });
102             state.getRootNode().addController(quit);
103         }
104 }