001: /*
002: * $RCSfile: TimeSensor.java,v $
003: *
004: * @(#)TimeSensor.java 1.28 98/11/06 16:27:00
005: *
006: * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
007: *
008: * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
009: * modify and redistribute this software in source and binary code form,
010: * provided that i) this copyright notice and license appear on all copies of
011: * the software; and ii) Licensee does not utilize the software in a manner
012: * which is disparaging to Sun.
013: *
014: * This software is provided "AS IS," without a warranty of any kind. ALL
015: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
016: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
017: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
018: * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
019: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
020: * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
021: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
022: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
023: * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGES.
025: *
026: * This software is not designed or intended for use in on-line control of
027: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
028: * the design, construction, operation or maintenance of any nuclear
029: * facility. Licensee represents and warrants that it will not use or
030: * redistribute the Software for such purposes.
031: *
032: * $Revision: 1.2 $
033: * $Date: 2005/02/03 23:07:03 $
034: * $State: Exp $
035: */
036: /*
037: * @Author: Rick Goldberg
038: * @Author: Doug Gehringer
039: *
040: */
041: package org.jdesktop.j3d.loaders.vrml97.impl;
042:
043: /** Description of the Class */
044: public class TimeSensor extends Node {
045:
046: // exposedField
047:
048: SFTime cycleInterval;
049: SFBool enabled;
050: SFBool loop;
051: SFTime startTime;
052: SFTime stopTime;
053:
054: // eventOut
055:
056: SFTime cycleTime;
057: SFFloat fraction;
058: SFBool isActive;
059: SFTime time;
060:
061: double last = 0.0;
062: boolean simActive = false;
063:
064: // when the sensor is active (enabled) the corresponding events
065: // are ignored.
066: //
067:
068: double activeCycleInterval;
069: double activeStartTime;
070: double activeStopTime;
071:
072: /**
073: *Constructor for the TimeSensor object
074: *
075: *@param loader Description of the Parameter
076: */
077: public TimeSensor(Loader loader) {
078: super (loader);
079:
080: cycleInterval = new SFTime(1.0);
081: enabled = new SFBool(true);
082: loop = new SFBool(false);
083: startTime = new SFTime(0.0);
084: stopTime = new SFTime(0.0);
085: cycleTime = new SFTime(0.0);
086: fraction = new SFFloat(0.0f);
087: isActive = new SFBool(false);
088: time = new SFTime(0.0);
089:
090: loader.addTimeSensor(this );
091: initFields();
092: }
093:
094: /**
095: *Constructor for the TimeSensor object
096: *
097: *@param loader Description of the Parameter
098: *@param cycleInterval Description of the Parameter
099: *@param enabled Description of the Parameter
100: *@param loop Description of the Parameter
101: *@param startTime Description of the Parameter
102: *@param stopTime Description of the Parameter
103: */
104: public TimeSensor(Loader loader, SFTime cycleInterval,
105: SFBool enabled, SFBool loop, SFTime startTime,
106: SFTime stopTime) {
107:
108: super (loader);
109: this .cycleInterval = cycleInterval;
110: this .enabled = enabled;
111: this .loop = loop;
112: this .startTime = startTime;
113: this .stopTime = stopTime;
114: cycleTime = new SFTime(0.0);
115: fraction = new SFFloat(0.0f);
116: isActive = new SFBool(false);
117: time = new SFTime(0.0);
118:
119: loader.addTimeSensor(this );
120: initFields();
121: }
122:
123: /** Description of the Method */
124: void doneParse() {
125: if (enabled.value == true) {
126: if ((loop.value == true)
127: && (stopTime.time <= startTime.time)) {
128: isActive.setValue(true);
129: }
130: }
131: simActive = true;
132: implReady = true;
133: }
134:
135: /**
136: * Description of the Method
137: *
138: *@param eventInName Description of the Parameter
139: *@param time Description of the Parameter
140: */
141: public void notifyMethod(String eventInName, double time) {
142: if (eventInName.equals("enabled")) {
143: //simTick(Time.getNow());
144: simTick(time);
145: } else if (eventInName.equals("startTime")) {
146: //System.out.println("TimeSensor("+this+
147: //") startTime called time = "+ startTime.time);
148: if (isActive.value == true) {
149: // ignore by resetting the value back to the previous
150: startTime.time = activeStartTime;
151: } else {
152: isActive.setValue(true);
153: activeStartTime = startTime.time;
154: activeStopTime = stopTime.time;
155: }
156: } else if (eventInName.equals("stopTime")) {
157: if (isActive.value == true) {
158: // ignore by resetting the value back to the previous
159: // check
160: if (activeStopTime < activeStartTime) {
161: stopTime.time = activeStopTime;
162: }
163: if ((activeStartTime < stopTime.time)
164: && (stopTime.time <= time)) {
165: isActive.setValue(false);
166: }
167: }
168: } else if (eventInName.equals("cycleInterval")) {
169: if (isActive.value == true) {
170: cycleInterval.time = activeCycleInterval;
171: }
172: } else if (eventInName.equals("loop")
173: || eventInName.equals("route_enabled")
174: || eventInName.equals("route_startTime")
175: || eventInName.equals("route_stopTime")
176: || eventInName.equals("route_cycleInterval")
177: || eventInName.equals("route_loop")) {
178: ;
179: } else if (eventInName.equals("isActive")) {
180: if (isActive.value) {
181: activeStartTime = startTime.time;
182: activeStopTime = stopTime.time;
183: activeCycleInterval = cycleInterval.time;
184: }
185: } else {
186: System.err.println("TimeSensor: unknown eventIn "
187: + eventInName);
188: }
189:
190: }
191:
192: float lastF = 0.0f;
193:
194: /**
195: * Description of the Method
196: *
197: *@param updateTime Description of the Parameter
198: */
199: void updateFraction(double updateTime) {
200:
201: double delta;
202: double f;
203: float fractionValue = 0.0f;
204:
205: activeStopTime = stopTime.time;
206: activeCycleInterval = cycleInterval.time;
207: activeStartTime = startTime.time;
208:
209: delta = updateTime - startTime.time;
210:
211: f = Math.IEEEremainder(delta, cycleInterval.time);
212:
213: if (f < 0.0) {// IEEEremander values can be < 0
214: f += cycleInterval.time;
215: }
216: if (f <= 0.0) {
217: if (updateTime > startTime.time) {
218: fractionValue = 1.0f;
219: } else {
220: fractionValue = 0.0f;
221: }
222: } else {
223: fractionValue = (float) (f / cycleInterval.time);
224: }
225: if (fractionValue < lastF) {
226: cycleTime.setValue(updateTime);
227: }
228: lastF = fractionValue;
229: fraction.setValue(fractionValue);
230: }
231:
232: /**
233: * Description of the Method
234: *
235: *@param now Description of the Parameter
236: */
237: void simTick(double now) {
238:
239: time.setValue(now);
240: //System.out.println("TimeSensor("+this+") enabled"+enabled.value);
241: //System.out.println("isActive"+isActive.value);
242: //System.out.println("loop"+loop.value);
243: //System.out.println("sim tick"+now);
244: //System.out.println("startTime"+startTime.time);
245: //System.out.println("stopTime"+stopTime.time);
246: //System.out.println("cycleInterval"+cycleInterval.time);
247: //System.out.println("cycleTime"+cycleTime.time);
248: //System.out.println("time"+time.time);
249: //System.out.println();
250:
251: if (enabled.value == true) {
252:
253: if ((now >= stopTime.time)
254: && (stopTime.time > startTime.time)
255: && (!loop.value)) {
256: if (isActive.value) {
257: updateFraction(stopTime.time);
258: isActive.setValue(false);
259: }
260: } else if ((now >= (startTime.time + cycleInterval.time) && (!loop.value))) {
261: if (isActive.value) {
262: updateFraction(startTime.time + cycleInterval.time);
263: isActive.setValue(false);
264: }
265: } else if ((loop.value) && (stopTime.time > startTime.time)
266: && (now >= stopTime.time)) {
267: if (isActive.value) {
268: isActive.setValue(false);
269: updateFraction(stopTime.time);
270: }
271: } else if ((loop.value)
272: && (stopTime.time <= startTime.time)
273: && (now >= startTime.time)) {
274: if (!isActive.value) {
275: isActive.setValue(true);
276: }
277: updateFraction(now);
278: } else if ((now > startTime.time) && (!isActive.value)) {
279: isActive.setValue(true);
280: updateFraction(now);
281: } else if (isActive.value) {
282: updateFraction(now);
283: }
284: } else {// enabled.value == false
285: activeStopTime = -1.0;
286: activeStartTime = -1.0;
287: activeCycleInterval = -1.0;
288: }
289: }
290:
291: /** Description of the Method */
292: void initFields() {
293: cycleInterval.init(this , FieldSpec, Field.EXPOSED_FIELD,
294: "cycleInterval");
295: enabled.init(this , FieldSpec, Field.EXPOSED_FIELD, "enabled");
296: loop.init(this , FieldSpec, Field.EXPOSED_FIELD, "loop");
297: startTime.init(this , FieldSpec, Field.EXPOSED_FIELD,
298: "startTime");
299: stopTime.init(this , FieldSpec, Field.EXPOSED_FIELD, "stopTime");
300: cycleTime.init(this , FieldSpec, Field.EVENT_OUT, "cycleTime");
301: fraction.init(this , FieldSpec, Field.EVENT_OUT, "fraction");
302: isActive.init(this , FieldSpec, Field.EVENT_OUT, "isActive");
303: time.init(this , FieldSpec, Field.EVENT_OUT, "time");
304: }
305:
306: /**
307: * Description of the Method
308: *
309: *@return Description of the Return Value
310: */
311: public Object clone() {
312: return new TimeSensor(loader, (SFTime) cycleInterval.clone(),
313: (SFBool) enabled.clone(), (SFBool) loop.clone(),
314: (SFTime) startTime.clone(), (SFTime) stopTime.clone());
315: }
316:
317: /**
318: * Gets the type attribute of the TimeSensor object
319: *
320: *@return The type value
321: */
322: public String getType() {
323: return "TimeSensor";
324: }
325:
326: }
|