OSDN Git Service

introduced event mode for mouse and key input, added JoystickInput (thus added jinput...
[mikumikustudio/MikuMikuStudio.git] / src / jmetest / input / TestKeyInput.java
1 /*
2  * Copyright (c) 2003-2005 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
33 package jmetest.input;
34
35 import com.jme.app.BaseGame;
36 import com.jme.image.Texture;
37 import com.jme.input.InputSystem;
38 import com.jme.input.KeyInput;
39 import com.jme.input.KeyInputListener;
40 import com.jme.math.Vector3f;
41 import com.jme.renderer.Camera;
42 import com.jme.renderer.ColorRGBA;
43 import com.jme.scene.Node;
44 import com.jme.scene.Text;
45 import com.jme.scene.state.AlphaState;
46 import com.jme.scene.state.TextureState;
47 import com.jme.system.DisplaySystem;
48 import com.jme.system.JmeException;
49 import com.jme.util.TextureManager;
50 import org.lwjgl.input.Keyboard;
51 import org.lwjgl.input.Mouse;
52
53 /**
54  * <code>TestKeyInput</code>
55  * @author Mark Powell
56  * @version $Id: TestKeyInput.java,v 1.9 2005-10-11 10:41:57 irrisor Exp $
57  */
58 public class TestKeyInput extends BaseGame {
59     private Text text;
60     private Camera cam;
61     private Node scene;
62     private KeyInput key;
63
64     public static void main(String[] args) {
65         TestKeyInput app = new TestKeyInput();
66         app.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
67         app.start();
68     }
69
70     protected void update(float interpolation) {
71         key.update();
72         if(key.isKeyDown(KeyInput.KEY_0)) {
73             text.print("You pressed 0.");
74         }
75
76         if(key.isKeyDown(KeyInput.KEY_1)) {
77             text.print("You pressed 1.");
78         }
79     }
80
81     /**
82      * draws the scene graph
83      * @see com.jme.app.SimpleGame#render(float)
84      */
85     protected void render(float interpolation) {
86         display.getRenderer().clearBuffers();
87
88         display.getRenderer().draw(scene);
89
90     }
91
92     /**
93      * initializes the display and camera.
94      * @see com.jme.app.SimpleGame#initSystem()
95      */
96     protected void initSystem() {
97         try {
98             display = DisplaySystem.getDisplaySystem(properties.getRenderer());
99             display.createWindow(
100                     properties.getWidth(),
101                     properties.getHeight(),
102                     properties.getDepth(),
103                     properties.getFreq(),
104                     properties.getFullscreen());
105
106             cam = display.getRenderer().createCamera(properties.getWidth(), properties.getHeight());
107         } catch (JmeException e) {
108             e.printStackTrace();
109             System.exit(1);
110         }
111         ColorRGBA blueColor = new ColorRGBA();
112         blueColor.r = 0;
113         blueColor.g = 0;
114         display.getRenderer().setBackgroundColor(blueColor);
115         cam.setFrustum(1.0f, 1000.0f, -0.55f, 0.55f, 0.4125f, -0.4125f);
116         Vector3f loc = new Vector3f(4.0f, 0.0f, 0.0f);
117         Vector3f left = new Vector3f(0.0f, -1.0f, 0.0f);
118         Vector3f up = new Vector3f(0.0f, 0.0f, 1.0f);
119         Vector3f dir = new Vector3f(-1.0f, 0f, 0.0f);
120         cam.setFrame(loc, left, up, dir);
121
122         key = KeyInput.get();
123         display.getRenderer().setCamera(cam);
124     }
125
126     /**
127      * initializes the scene
128      * @see com.jme.app.SimpleGame#initGame()
129      */
130     protected void initGame() {
131         text = new Text("Text Label", "Press 0 and/or 1");
132         text.setLocalTranslation(new Vector3f(1, 60, 0));
133         TextureState ts = display.getRenderer().createTextureState();
134         ts.setEnabled(true);
135         ts.setTexture(
136             TextureManager.loadTexture(
137                    TestKeyInput.class.getClassLoader().getResource("jmetest/data/font/font.png"),
138                 Texture.MM_LINEAR,
139                 Texture.FM_LINEAR));
140         text.setRenderState(ts);
141         AlphaState as1 = display.getRenderer().createAlphaState();
142         as1.setBlendEnabled(true);
143         as1.setSrcFunction(AlphaState.SB_SRC_ALPHA);
144         as1.setDstFunction(AlphaState.DB_ONE);
145         as1.setTestEnabled(true);
146         as1.setTestFunction(AlphaState.TF_GREATER);
147         text.setRenderState(as1);
148         scene = new Node("Scene graph node");
149         scene.attachChild(text);
150         cam.update();
151
152         scene.updateGeometricState(0.0f, true);
153         scene.updateRenderState();
154
155         KeyInput.get().addListener( new KeyInputListener() {
156             public void onKey( char character, int keyCode, boolean pressed ) {
157                 text.print( "key: '" + (character != 0 ? ""+character : "\\0") + "' (code "+keyCode+") " + (pressed?"pressed":"released") );
158             }
159         } );
160     }
161
162     /**
163      * not used.
164      * @see com.jme.app.SimpleGame#reinit()
165      */
166     protected void reinit() {
167
168     }
169
170     /**
171      * not used.
172      * @see com.jme.app.SimpleGame#cleanup()
173      */
174     protected void cleanup() {
175
176     }
177 }