2 * Copyright (c) 2003-2005 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
33 package jmetest.input;
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;
54 * <code>TestKeyInput</code>
56 * @version $Id: TestKeyInput.java,v 1.9 2005-10-11 10:41:57 irrisor Exp $
58 public class TestKeyInput extends BaseGame {
64 public static void main(String[] args) {
65 TestKeyInput app = new TestKeyInput();
66 app.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
70 protected void update(float interpolation) {
72 if(key.isKeyDown(KeyInput.KEY_0)) {
73 text.print("You pressed 0.");
76 if(key.isKeyDown(KeyInput.KEY_1)) {
77 text.print("You pressed 1.");
82 * draws the scene graph
83 * @see com.jme.app.SimpleGame#render(float)
85 protected void render(float interpolation) {
86 display.getRenderer().clearBuffers();
88 display.getRenderer().draw(scene);
93 * initializes the display and camera.
94 * @see com.jme.app.SimpleGame#initSystem()
96 protected void initSystem() {
98 display = DisplaySystem.getDisplaySystem(properties.getRenderer());
100 properties.getWidth(),
101 properties.getHeight(),
102 properties.getDepth(),
103 properties.getFreq(),
104 properties.getFullscreen());
106 cam = display.getRenderer().createCamera(properties.getWidth(), properties.getHeight());
107 } catch (JmeException e) {
111 ColorRGBA blueColor = new ColorRGBA();
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);
122 key = KeyInput.get();
123 display.getRenderer().setCamera(cam);
127 * initializes the scene
128 * @see com.jme.app.SimpleGame#initGame()
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();
136 TextureManager.loadTexture(
137 TestKeyInput.class.getClassLoader().getResource("jmetest/data/font/font.png"),
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);
152 scene.updateGeometricState(0.0f, true);
153 scene.updateRenderState();
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") );
164 * @see com.jme.app.SimpleGame#reinit()
166 protected void reinit() {
172 * @see com.jme.app.SimpleGame#cleanup()
174 protected void cleanup() {