001: /*
002: * $RCSfile: MouseRotateY.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:46 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.oriented_shape3d;
046:
047: import java.awt.*;
048: import java.awt.event.*;
049: import java.util.*;
050: import javax.media.j3d.*;
051: import javax.vecmath.*;
052: import com.sun.j3d.utils.behaviors.mouse.*;
053:
054: /**
055: * MouseRotateY is a Java3D behavior object that lets users control the
056: * rotation of an object via a mouse.
057: * <p>
058: * To use this utility, first create a transform group that this
059: * rotate behavior will operate on. Then,
060: *<blockquote><pre>
061: *
062: * MouseRotateY behavior = new MouseRotateY();
063: * behavior.setTransformGroup(objTrans);
064: * objTrans.addChild(behavior);
065: * behavior.setSchedulingBounds(bounds);
066: *
067: *</pre></blockquote>
068: * The above code will add the rotate behavior to the transform
069: * group. The user can rotate any object attached to the objTrans.
070: */
071:
072: public class MouseRotateY extends MouseBehavior {
073: double y_angle;
074: double y_factor;
075:
076: /**
077: * Creates a rotate behavior given the transform group.
078: * @param transformGroup The transformGroup to operate on.
079: */
080: public MouseRotateY(TransformGroup transformGroup) {
081: super (transformGroup);
082: }
083:
084: /**
085: * Creates a default mouse rotate behavior.
086: **/
087: public MouseRotateY() {
088: super (0);
089: }
090:
091: /**
092: * Creates a rotate behavior.
093: * Note that this behavior still needs a transform
094: * group to work on (use setTransformGroup(tg)) and
095: * the transform group must add this behavior.
096: * @param flags interesting flags (wakeup conditions).
097: */
098: public MouseRotateY(int flags) {
099: super (flags);
100: }
101:
102: public void initialize() {
103: super .initialize();
104: y_angle = 0;
105: y_factor = .03;
106: if ((flags & INVERT_INPUT) == INVERT_INPUT) {
107: invert = true;
108: y_factor *= -1;
109: }
110: }
111:
112: public double getYFactor() {
113: return y_factor;
114: }
115:
116: public void setFactor(double factor) {
117: y_factor = factor;
118:
119: }
120:
121: public void processStimulus(Enumeration criteria) {
122: WakeupCriterion wakeup;
123: AWTEvent[] event;
124: int id;
125: int dx;
126:
127: while (criteria.hasMoreElements()) {
128: wakeup = (WakeupCriterion) criteria.nextElement();
129: if (wakeup instanceof WakeupOnAWTEvent) {
130: event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
131: for (int i = 0; i < event.length; i++) {
132: processMouseEvent((MouseEvent) event[i]);
133:
134: if (((buttonPress) && ((flags & MANUAL_WAKEUP) == 0))
135: || ((wakeUp) && ((flags & MANUAL_WAKEUP) != 0))) {
136:
137: id = event[i].getID();
138: if ((id == MouseEvent.MOUSE_DRAGGED)
139: && !((MouseEvent) event[i])
140: .isMetaDown()
141: && !((MouseEvent) event[i]).isAltDown()) {
142:
143: x = ((MouseEvent) event[i]).getX();
144:
145: dx = x - x_last;
146:
147: if (!reset) {
148: y_angle = dx * y_factor;
149:
150: transformY.rotY(y_angle);
151:
152: transformGroup.getTransform(currXform);
153:
154: //Vector3d translation = new Vector3d();
155: //Matrix3f rotation = new Matrix3f();
156: Matrix4d mat = new Matrix4d();
157:
158: // Remember old matrix
159: currXform.get(mat);
160:
161: // Translate to origin
162: currXform.setTranslation(new Vector3d(
163: 0.0, 0.0, 0.0));
164: if (invert) {
165: currXform
166: .mul(currXform, transformX);
167: currXform
168: .mul(currXform, transformY);
169: } else {
170: currXform
171: .mul(transformX, currXform);
172: currXform
173: .mul(transformY, currXform);
174: }
175:
176: // Set old translation back
177: Vector3d translation = new Vector3d(
178: mat.m03, mat.m13, mat.m23);
179: currXform.setTranslation(translation);
180:
181: // Update xform
182: transformGroup.setTransform(currXform);
183: } else {
184: reset = false;
185: }
186:
187: x_last = x;
188: } else if (id == MouseEvent.MOUSE_PRESSED) {
189: x_last = ((MouseEvent) event[i]).getX();
190: }
191: }
192: }
193: }
194: }
195:
196: wakeupOn(mouseCriterion);
197:
198: }
199: }
|