001 package tutorial;
002 import java.awt.event.KeyEvent;
003
004 import javax.media.opengl.GL;
005 import javax.media.opengl.GLDrawable;
006
007 import de.rico.engine.camera.Camera;
008 import de.rico.engine.game.AbstractBaseGame;
009 import de.rico.engine.input.InputAction;
010 import de.rico.engine.input.InputManager;
011
012 /**
013 * A class that shows how to create and process user defined input actions.
014 * @author Frank Bruns
015 *
016 */
017 public class CreateAndProcessInputActions extends AbstractBaseGame
018 {
019 private InputManager inputManager = null; // input manager
020
021 private InputAction keyESC = null;
022 private InputAction keyP = null;
023 private InputAction button1 = null;
024 private InputAction mouseLeft = null;
025 private InputAction mouseRight = null;
026
027 /**
028 * Constructor for the new game.
029 * @param title title for the window bar
030 * @param maxFps maximum possible framerate
031 */
032 public CreateAndProcessInputActions(String title, int maxFps)
033 {
034 super(title, maxFps);
035 }
036
037 /* (non-Javadoc)
038 * @see de.rico.engine.game.AbstractBaseGame#initResources(javax.media.opengl.GLDrawable, javax.media.opengl.GL, int, int)
039 */
040 @Override
041 public void initResources(GLDrawable gld, GL gl, int width, int height)
042 {
043 // we have to create an instance of the InputManager class and assign it
044 // to the OpenGL canvas
045 this.inputManager = new InputManager(this.getOGLCanvas());
046
047 // create the input actions. The specified string is a
048 // describing reference name for an input action.
049
050 // DETECT_INITAL_PRESS_ONLY means that only one key press or mouse click
051 // gets detected, even if the key or button is held down.
052 // The key or mouse button needs to be released before
053 // another click or press can be registered
054 this.keyESC = new InputAction("Quit program",InputAction.DETECT_INITAL_PRESS_ONLY);
055
056 // If DETECT_INITAL_PRESS_ONLY is not specified, key presses or mouse click
057 // get detected as long as the key or mouse is held down
058 this.keyP = new InputAction("Print command");
059 this.button1 = new InputAction("Mouse Button 1",InputAction.DETECT_INITAL_PRESS_ONLY);
060 this.mouseLeft = new InputAction("Mouse moves left");
061 this.mouseRight = new InputAction("Mouse move right");
062
063 // assign the input actions with their according key event or mouse code
064 // to the input manager
065 this.inputManager.assignKey(this.keyESC, KeyEvent.VK_ESCAPE);
066 this.inputManager.assignKey(this.keyP, KeyEvent.VK_P);
067 this.inputManager.assignMouse(this.button1, InputManager.MOUSE_BUTTON_1);
068 this.inputManager.assignMouse(this.mouseLeft, InputManager.MOUSE_MOVE_LEFT);
069 this.inputManager.assignMouse(this.mouseRight, InputManager.MOUSE_MOVE_RIGHT);
070 }
071
072 /* (non-Javadoc)
073 * @see de.rico.engine.game.AbstractBaseGame#update(long)
074 */
075 @Override
076 public void update(long elapsedTime)
077 {
078 // poll and process process the input actions if they
079 // have been triggered
080
081 // If the specified key or mouse button is pressed
082 // perform a certain command. In these cases quit the
083 // programme or simply print a text
084 if(this.keyESC.isPressed()) System.exit(0);
085 if(this.keyP.isPressed()) System.out.println("Key P was pressed");
086 if(this.button1.isPressed()) System.out.println("Left mouse button was pressed");
087
088 // Access the camera by its singleton instance and rotate
089 // it according to the amount of mouse movement.
090 Camera.getInstance().rotateLeft(this.mouseLeft.getAmount());
091 Camera.getInstance().rotateRight(this.mouseRight.getAmount());
092 }
093
094 /* (non-Javadoc)
095 * @see de.rico.engine.game.AbstractBaseGame#draw(javax.media.opengl.GL)
096 */
097 @Override
098 public void draw(GL gl)
099 {
100 // nothing to draw!
101 }
102
103 /**
104 * The usual main method. It's the entry point to the application.
105 * @param args argument string
106 */
107 public static void main(String args[])
108 {
109 // create a new game
110 CreateAndProcessInputActions game = new CreateAndProcessInputActions("Test Game", 100);
111
112 // initialise the camera --> needs to be done!
113 game.initCamera(0f, 0f, 0f, 0f, 0f, 0.1f, 200f);
114
115 // show framerate --> this is optional
116 game.setDisplayFPS(true);
117
118 // finally start the game
119 game.start(game);
120 }
121 }
|