001: /*
002: * $RCSfile: MoverBehavior.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:53 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.text2d;
046:
047: import java.awt.event.*;
048: import java.awt.AWTEvent;
049: import javax.media.j3d.*;
050: import java.util.Enumeration;
051: import javax.vecmath.*;
052:
053: // Mover behavior class - used to allow viewer to move using arrow keys
054: class MoverBehavior extends Behavior {
055: WakeupOnAWTEvent w1 = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
056: WakeupCriterion[] w2 = { w1 };
057: WakeupCondition w = new WakeupOr(w2);
058: TransformGroup viewTransformGroup;
059: double rotation = 0.0; // holds current rotation radians
060:
061: public void initialize() {
062: // Establish initial wakeup criteria
063: wakeupOn(w);
064: }
065:
066: /**
067: * Override Behavior's stimulus method to handle the event.
068: */
069: public void processStimulus(Enumeration criteria) {
070: WakeupOnAWTEvent ev;
071: WakeupCriterion genericEvt;
072: AWTEvent[] events;
073:
074: while (criteria.hasMoreElements()) {
075: genericEvt = (WakeupCriterion) criteria.nextElement();
076: if (genericEvt instanceof WakeupOnAWTEvent) {
077: ev = (WakeupOnAWTEvent) genericEvt;
078: events = ev.getAWTEvent();
079: processManualEvent(events);
080: }
081: }
082: // Set wakeup criteria for next time
083: wakeupOn(w);
084: }
085:
086: /**
087: * Process a keyboard event to move or rotate the viewer.
088: */
089: void processManualEvent(AWTEvent[] events) {
090:
091: for (int i = 0; i < events.length; ++i) {
092: if (events[i] instanceof KeyEvent) {
093: KeyEvent event = (KeyEvent) events[i];
094: if (event.getKeyCode() == KeyEvent.VK_EQUALS) {
095: continue;
096: }
097: Transform3D t = new Transform3D();
098: viewTransformGroup.getTransform(t);
099: Vector3f viewDir = new Vector3f(0f, 0f, -1f);
100: Vector3f translation = new Vector3f();
101: t.get(translation);
102: t.transform(viewDir);
103: if (event.getKeyCode() == KeyEvent.VK_UP) {
104: translation.x += viewDir.x;
105: translation.y += viewDir.y;
106: translation.z += viewDir.z;
107: } else if (event.getKeyCode() == KeyEvent.VK_DOWN) {
108: translation.x -= viewDir.x;
109: translation.y -= viewDir.y;
110: translation.z -= viewDir.z;
111: } else if (event.getKeyCode() == KeyEvent.VK_RIGHT) {
112: rotation += -.1;
113: } else if (event.getKeyCode() == KeyEvent.VK_LEFT) {
114: rotation += .1;
115: }
116: t.rotY(rotation);
117: t.setTranslation(translation);
118: viewTransformGroup.setTransform(t);
119: }
120: }
121: }
122:
123: /**
124: * Constructor
125: */
126: public MoverBehavior(TransformGroup trans) {
127: viewTransformGroup = trans;
128: Bounds bound = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
129: 10000.0);
130: this.setSchedulingBounds(bound);
131: }
132: }
|