OSDN Git Service

Set optimal mime types and executable settings.
[mikumikustudio/MikuMikuStudio.git] / src / jmetest / TutorialGuide / HelloKeyInput.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
33 package jmetest.TutorialGuide;
34
35 import java.net.URL;
36 import java.nio.FloatBuffer;
37
38 import com.jme.app.SimpleGame;
39 import com.jme.image.Texture;
40 import com.jme.input.KeyBindingManager;
41 import com.jme.input.KeyInput;
42 import com.jme.math.Vector2f;
43 import com.jme.math.Vector3f;
44 import com.jme.scene.TexCoords;
45 import com.jme.scene.TriMesh;
46 import com.jme.scene.state.TextureState;
47 import com.jme.util.TextureManager;
48 import com.jme.util.geom.BufferUtils;
49
50 /**
51  * Started Date: Jul 21, 2004<br><br>
52  *
53  * This program demonstrates using key inputs to change things.
54  * 
55  * @author Jack Lindamood
56  */
57 public class HelloKeyInput extends SimpleGame {
58     // The TriMesh that I will change
59     TriMesh square;
60     // A scale of my current texture values
61     float coordDelta;
62     public static void main(String[] args) {
63         HelloKeyInput app = new HelloKeyInput();
64         app.setConfigShowMode(ConfigShowMode.AlwaysShow);
65         app.start();
66     }
67
68     protected void simpleInitGame() {
69         // Vertex positions for the mesh
70         Vector3f[] vertexes={
71             new Vector3f(0,0,0),
72             new Vector3f(1,0,0),
73             new Vector3f(0,1,0),
74             new Vector3f(1,1,0)
75         };
76
77         // Texture Coordinates for each position
78         coordDelta=1;
79         Vector2f[] texCoords={
80             new Vector2f(0,0),
81             new Vector2f(coordDelta,0),
82             new Vector2f(0,coordDelta),
83             new Vector2f(coordDelta,coordDelta)
84         };
85
86         // The indexes of Vertex/Normal/Color/TexCoord sets.  Every 3 makes a triangle.
87         int[] indexes={
88             0,1,2,1,2,3
89         };
90         // Create the square
91         square=new TriMesh("My Mesh",BufferUtils.createFloatBuffer(vertexes),null, null, TexCoords.makeNew(texCoords), BufferUtils.createIntBuffer(indexes));
92         // Point to the monkey image
93         URL monkeyLoc=HelloKeyInput.class.getClassLoader().getResource("jmetest/data/images/Monkey.jpg");
94         // Get my TextureState
95         TextureState ts=display.getRenderer().createTextureState();
96         // Get my Texture
97         Texture t=TextureManager.loadTexture(monkeyLoc,Texture.MinificationFilter.BilinearNearestMipMap, Texture.MagnificationFilter.Bilinear);
98         // Set a wrap for my texture so it repeats
99         t.setWrap(Texture.WrapMode.Repeat);
100         // Set the texture to the TextureState
101         ts.setTexture(t);
102
103         // Assign the TextureState to the square
104         square.setRenderState(ts);
105         // Scale my square 10x larger
106         square.setLocalScale(10);
107         // Attach my square to my rootNode
108         rootNode.attachChild(square);
109
110         // Assign the "+" key on the keypad to the command "coordsUp"
111         KeyBindingManager.getKeyBindingManager().set(
112             "coordsUp",
113             KeyInput.KEY_ADD);
114
115         // Adds the "u" key to the command "coordsUp"
116         KeyBindingManager.getKeyBindingManager().add(
117             "coordsUp",
118             KeyInput.KEY_U);
119
120         // Assign the "-" key on the keypad to the command "coordsDown"
121         KeyBindingManager.getKeyBindingManager().set(
122             "coordsDown",
123             KeyInput.KEY_SUBTRACT);
124     }
125
126     // Called every frame update
127     protected void simpleUpdate(){
128
129         boolean updateTex = false;
130         // If the coordsDown command was activated
131         if (KeyBindingManager.getKeyBindingManager().isValidCommand("coordsDown",true)){
132             // Scale my texture down
133             coordDelta-=.01f;
134             updateTex = true;
135         }
136         // if the coordsUp command was activated
137         if (KeyBindingManager.getKeyBindingManager().isValidCommand("coordsUp",true)){
138             // Scale my texture up
139             coordDelta+=.01f;
140             updateTex = true;
141         }
142         
143         if (updateTex) {
144             // Get my square's texture array
145             FloatBuffer texBuf = square.getTextureCoords(0).coords;
146             texBuf.rewind().position(2); // start after the 1st texcoord (2 floats wide)
147             // Change the values of the texture coords in the buffer
148             texBuf.put(coordDelta).put(0);
149             texBuf.put(0).put(coordDelta);
150             texBuf.put(coordDelta).put(coordDelta);            
151         }
152     }
153 }