001: /*
002: * $RCSfile: SensorRead.java,v $
003: *
004: * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.6 $
028: * $Date: 2008/02/28 20:17:29 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import javax.vecmath.*;
035:
036: /**
037: * A SensorRead encapsulates all the information associated with a single
038: * reading of a sensor, including a timestamp, a transform, and,
039: * optionally, button values.
040: */
041:
042: public class SensorRead {
043:
044: /**
045: * The maximum number of sensor-attached buttons tracked on a per
046: * sensor basis.
047: */
048: public static final int MAXIMUM_SENSOR_BUTTON_COUNT = 12;
049:
050: /**
051: * This reading's time stamp
052: */
053: long time;
054:
055: /**
056: * The six-degree-of-freedom reading
057: */
058: Transform3D read;
059:
060: /**
061: * The state of the sensor's buttons
062: */
063: int[] buttonValues;
064:
065: /**
066: * The number of buttons associated with this SensorRead
067: */
068: int numButtons;
069:
070: /**
071: * Constructs a SensorRead object with default parameters.
072: * The default values are as follows:
073: * <ul>
074: * number of buttons : 0<br>
075: * button values : 0 (for all array elements)<br>
076: * transform : identity<br>
077: * time : current time<br>
078: * </ul>
079: */
080: public SensorRead() {
081: this (0);
082: }
083:
084: /**
085: * Constructs a SensorRead object with the specified number
086: * of buttons.
087: * @param numButtons the number of buttons for this SensorRead
088: */
089: public SensorRead(int numButtons) {
090: this .read = new Transform3D();
091: this .numButtons = numButtons;
092: this .buttonValues = new int[numButtons];
093:
094: // Do this last
095: this .time = J3dClock.currentTimeMillis();
096: }
097:
098: final void set(SensorRead sensorRead) {
099: this .time = sensorRead.time;
100: this .numButtons = sensorRead.numButtons;
101: this .read.set(sensorRead.read);
102: if (numButtons > 0)
103: System.arraycopy(sensorRead.buttonValues, 0,
104: this .buttonValues, 0, sensorRead.numButtons);
105: }
106:
107: /**
108: * Set the SensorRead's transform to the value specified
109: * @param t1 this sensor's reading
110: */
111: public void set(Transform3D t1) {
112: read.set(t1);
113: }
114:
115: /**
116: * Retrieve the SensorRead's transform and place it in result
117: * @param result the recipient of the this sensor's reading
118: */
119: public void get(Transform3D result) {
120: result.set(read);
121: }
122:
123: /**
124: * Sets this SensorRead's time stamp to the specified argument
125: * @param time the time to associate with this reading
126: */
127: public void setTime(long time) {
128: this .time = time;
129: }
130:
131: /**
132: * Retrieve this SensorRead's associated time stamp
133: * @return the SensorRead's time as a long
134: */
135: public long getTime() {
136: return this .time;
137: }
138:
139: /**
140: * Sets the values of all buttons for this SensorRead object.
141: * @param values array contining the new buttons for this SensorRead
142: * @exception ArrayIndexOutOfBoundsException if this object
143: * has 0 buttons or if values.length is less than the number of
144: * buttons in this object.
145: */
146: public void setButtons(int[] values) {
147: if (numButtons == 0)
148:
149: throw new ArrayIndexOutOfBoundsException(J3dI18N
150: .getString("SensorRead1"));
151:
152: else if (values.length < numButtons)
153:
154: throw new ArrayIndexOutOfBoundsException(J3dI18N
155: .getString("SensorRead0"));
156: System.arraycopy(values, 0, buttonValues, 0, numButtons);
157: }
158:
159: /**
160: * Copies the array of button values for this SensorRead object into
161: * the specified array.
162: * This method has no effect
163: * if this SensorRead object has 0 buttons. The array must be
164: * large enough to hold all of the buttons.
165: * @param values array that will receive the values of all buttons
166: * for this SensorRead
167: */
168: public void getButtons(int[] values) {
169: if (numButtons > 0)
170: System.arraycopy(buttonValues, 0, values, 0, numButtons);
171: }
172:
173: /**
174: * Returns the number of buttons associated with this SensorRead
175: * object.
176: *
177: * @return the number of buttons associated with this SensorRead
178: * object
179: *
180: * @since Java 3D 1.2
181: */
182: public int getNumButtons() {
183: return numButtons;
184: }
185:
186: }
|