001: /*
002: * $RCSfile: ButtonPositionControls.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.2 $
041: * $Date: 2007/02/09 17:21:55 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.virtual_input_device;
046:
047: import java.awt.*;
048: import java.awt.event.*;
049: import javax.vecmath.*;
050: import javax.media.j3d.*;
051:
052: public class ButtonPositionControls extends Panel implements
053: PositionControls, MouseListener {
054: private final static int STILL = 0;
055: private final static int MOVING_UP = 1;
056: private final static int MOVING_DOWN = 2;
057: private final static int MOVING_LEFT = 3;
058: private final static int MOVING_RIGHT = 4;
059: private final static int MOVING_FORWARD = 5;
060: private final static int MOVING_BACK = 6;
061:
062: // initial mode
063: private int mode = STILL;
064:
065: Vector3f position = new Vector3f();
066: Vector3f orig_position = new Vector3f();
067:
068: private Button leftB = new Button("Move Left");
069: private Button rightB = new Button("Move Right");
070: private Button upB = new Button("Move Up");
071: private Button downB = new Button("Move Down");
072:
073: private Button forwardB = new Button("Move Forward");
074: private Button backwardB = new Button("Move Back");
075:
076: private Button reset = new Button("Reset");
077: private InputDevice device;
078:
079: private float step_rate = 0.0023f; // movement rate per millisecond
080: private long time_last_state_change = System.currentTimeMillis();
081:
082: // the constructor arguments are the intitial X, Y, and Z positions
083: public ButtonPositionControls(float x, float y, float z) {
084:
085: // up, down, right, and left movement buttons
086: Panel panPanel = new Panel();
087: panPanel.setLayout(new BorderLayout());
088: panPanel.add("North", upB);
089: panPanel.add("East", rightB);
090: panPanel.add("South", downB);
091: panPanel.add("West", leftB);
092:
093: // forward, backward, and reset buttons
094: Panel p = new Panel();
095: p.setLayout(new GridLayout(0, 1, 0, 0));
096: p.add(forwardB);
097: p.add(backwardB);
098: p.add(reset);
099:
100: // set the initial position
101: position.x = x;
102: position.y = y;
103: position.z = z;
104: orig_position.set(position);
105:
106: // add a mouse listener to each button
107: upB.addMouseListener(this );
108: downB.addMouseListener(this );
109: leftB.addMouseListener(this );
110: rightB.addMouseListener(this );
111: forwardB.addMouseListener(this );
112: backwardB.addMouseListener(this );
113: reset.addMouseListener(this );
114:
115: this .setLayout(new BorderLayout());
116: add("East", p);
117: add("West", panPanel);
118: }
119:
120: public void setDevice(InputDevice device) {
121: this .device = device;
122: }
123:
124: public void getPosition(Vector3f pos) {
125: calculateMotion();
126: pos.set(position);
127: }
128:
129: public void setPosition(Vector3f pos) {
130: position.set(pos);
131: }
132:
133: public void setStepRate(float stepRate) {
134: step_rate = stepRate;
135: }
136:
137: private void calculateMotion() {
138:
139: long current_time = System.currentTimeMillis();
140: long elapsed_time = current_time - time_last_state_change;
141:
142: switch (mode) {
143: case STILL:
144: break;
145: case MOVING_LEFT:
146: position.x = orig_position.x - step_rate * elapsed_time;
147: break;
148: case MOVING_RIGHT:
149: position.x = orig_position.x + step_rate * elapsed_time;
150: break;
151: case MOVING_UP:
152: position.y = orig_position.y + step_rate * elapsed_time;
153: break;
154: case MOVING_DOWN:
155: position.y = orig_position.y - step_rate * elapsed_time;
156: break;
157: case MOVING_FORWARD:
158: position.z = orig_position.z - step_rate * elapsed_time;
159: break;
160: case MOVING_BACK:
161: position.z = orig_position.z + step_rate * elapsed_time;
162: break;
163: default:
164: throw (new RuntimeException("Unknown motion"));
165: }
166: }
167:
168: public void mouseClicked(MouseEvent e) {
169: }
170:
171: public void mouseEntered(MouseEvent e) {
172: }
173:
174: public void mouseExited(MouseEvent e) {
175: }
176:
177: public void mousePressed(MouseEvent e) {
178: if (e.getSource() == leftB && mode != MOVING_LEFT) {
179: time_last_state_change = System.currentTimeMillis();
180: mode = MOVING_LEFT;
181: orig_position.set(position);
182: } else if (e.getSource() == rightB && mode != MOVING_RIGHT) {
183: time_last_state_change = System.currentTimeMillis();
184: mode = MOVING_RIGHT;
185: orig_position.set(position);
186: } else if (e.getSource() == upB && mode != MOVING_UP) {
187: time_last_state_change = System.currentTimeMillis();
188: mode = MOVING_UP;
189: orig_position.set(position);
190: } else if (e.getSource() == downB && mode != MOVING_DOWN) {
191: time_last_state_change = System.currentTimeMillis();
192: mode = MOVING_DOWN;
193: orig_position.set(position);
194: } else if (e.getSource() == forwardB && mode != MOVING_FORWARD) {
195: time_last_state_change = System.currentTimeMillis();
196: mode = MOVING_FORWARD;
197: orig_position.set(position);
198: } else if (e.getSource() == backwardB && mode != MOVING_BACK) {
199: time_last_state_change = System.currentTimeMillis();
200: mode = MOVING_BACK;
201: orig_position.set(position);
202: } else if (e.getSource() == reset) {
203: device.setNominalPositionAndOrientation();
204: }
205: }
206:
207: public void mouseReleased(MouseEvent e) {
208: mode = STILL;
209: }
210: }
|