001: /*
002: * $RCSfile: PickRotateBehavior.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.4 $
041: * $Date: 2007/02/09 17:20:14 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.utils.behaviors.picking;
046:
047: import com.sun.j3d.utils.behaviors.mouse.*;
048: import java.awt.*;
049: import java.awt.event.*;
050: import java.util.*;
051: import javax.media.j3d.*;
052: import javax.vecmath.*;
053:
054: /*
055: * A mouse behavior that allows user to pick and drag scene graph objects.
056: * Common usage:
057: * <p>
058: * 1. Create your scene graph.
059: * <p>
060: * 2. Create this behavior with root and canvas.
061: * <p>
062: * <blockquote><pre>
063: * PickRotateBehavior behavior = new PickRotateBehavior(canvas, root, bounds);
064: * root.addChild(behavior);
065: * </pre></blockquote>
066: * <p>
067: * The above behavior will monitor for any picking events on
068: * the scene graph (below root node) and handle mouse drags on pick hits.
069: * Note the root node can also be a subgraph node of the scene graph (rather
070: * than the topmost).
071: */
072:
073: /**
074: * @deprecated As of Java 3D version 1.2, replaced by
075: * <code>com.sun.j3d.utils.picking.behaviors.PickRotateBehavior</code>
076: *
077: * @see com.sun.j3d.utils.picking.behaviors.PickRotateBehavior
078: */
079:
080: public class PickRotateBehavior extends PickMouseBehavior implements
081: MouseBehaviorCallback {
082: MouseRotate drag;
083: int pickMode = PickObject.USE_BOUNDS;
084: private PickingCallback callback = null;
085: private TransformGroup currentTG;
086:
087: /**
088: * Creates a pick/rotate behavior that waits for user mouse events for
089: * the scene graph. This method has its pickMode set to BOUNDS picking.
090: * @param root Root of your scene graph.
091: * @param canvas Java 3D drawing canvas.
092: * @param bounds Bounds of your scene.
093: **/
094:
095: public PickRotateBehavior(BranchGroup root, Canvas3D canvas,
096: Bounds bounds) {
097: super (canvas, root, bounds);
098: drag = new MouseRotate(MouseRotate.MANUAL_WAKEUP);
099: drag.setTransformGroup(currGrp);
100: currGrp.addChild(drag);
101: drag.setSchedulingBounds(bounds);
102: this .setSchedulingBounds(bounds);
103: }
104:
105: /**
106: * Creates a pick/rotate behavior that waits for user mouse events for
107: * the scene graph.
108: * @param root Root of your scene graph.
109: * @param canvas Java 3D drawing canvas.
110: * @param bounds Bounds of your scene.
111: * @param pickMode specifys PickObject.USE_BOUNDS or PickObject.USE_GEOMETRY.
112: * Note: If pickMode is set to PickObject.USE_GEOMETRY, all geometry object in
113: * the scene graph that allows pickable must have its ALLOW_INTERSECT bit set.
114: **/
115:
116: public PickRotateBehavior(BranchGroup root, Canvas3D canvas,
117: Bounds bounds, int pickMode) {
118: super (canvas, root, bounds);
119: drag = new MouseRotate(MouseRotate.MANUAL_WAKEUP);
120: drag.setTransformGroup(currGrp);
121: currGrp.addChild(drag);
122: drag.setSchedulingBounds(bounds);
123: this .setSchedulingBounds(bounds);
124: this .pickMode = pickMode;
125: }
126:
127: /**
128: * Sets the pickMode component of this PickRotateBehavior to the value of
129: * the passed pickMode.
130: * @param pickMode the pickMode to be copied.
131: **/
132:
133: public void setPickMode(int pickMode) {
134: this .pickMode = pickMode;
135: }
136:
137: /**
138: * Return the pickMode component of this PickRotateBehavior.
139: **/
140:
141: public int getPickMode() {
142: return pickMode;
143: }
144:
145: /**
146: * Update the scene to manipulate any nodes. This is not meant to be
147: * called by users. Behavior automatically calls this. You can call
148: * this only if you know what you are doing.
149: *
150: * @param xpos Current mouse X pos.
151: * @param ypos Current mouse Y pos.
152: **/
153: public void updateScene(int xpos, int ypos) {
154: TransformGroup tg = null;
155:
156: if (!mevent.isMetaDown() && !mevent.isAltDown()) {
157:
158: // tg = (TransformGroup) pickScene.pickNode(pickScene.pickClosest(xpos, ypos),
159: // PickObject.TRANSFORM_GROUP);
160:
161: tg = (TransformGroup) pickScene.pickNode(pickScene
162: .pickClosest(xpos, ypos, pickMode),
163: PickObject.TRANSFORM_GROUP);
164: // Make sure the selection exists and is movable.
165: if ((tg != null)
166: && (tg
167: .getCapability(TransformGroup.ALLOW_TRANSFORM_READ))
168: && (tg
169: .getCapability(TransformGroup.ALLOW_TRANSFORM_WRITE))) {
170: drag.setTransformGroup(tg);
171: drag.wakeup();
172: currentTG = tg;
173: } else if (callback != null)
174: callback
175: .transformChanged(PickingCallback.NO_PICK, null);
176: }
177: }
178:
179: /**
180: * Callback method from MouseRotate
181: * This is used when the Picking callback is enabled
182: */
183: public void transformChanged(int type, Transform3D transform) {
184: callback.transformChanged(PickingCallback.ROTATE, currentTG);
185: }
186:
187: /**
188: * Register the class @param callback to be called each
189: * time the picked object moves
190: */
191: public void setupCallback(PickingCallback callback) {
192: this.callback = callback;
193: if (callback == null)
194: drag.setupCallback(null);
195: else
196: drag.setupCallback(this);
197: }
198: }
|