1 package edu
.mit
.ezyang
.gravity
.j3d
;
3 import com
.sun
.j3d
.utils
.picking
.behaviors
.*;
4 import com
.sun
.j3d
.utils
.picking
.*;
5 import com
.sun
.j3d
.utils
.behaviors
.mouse
.*;
6 import javax
.media
.j3d
.*;
9 * Reimplemented PickTranslateBehavior, but uses SmartMouseTranslate, which
10 * has been customized to take into account rotations relative to the surface
11 * display. Modifications are solely in constructor and new setRotateTransformGroup
13 * @warning Convenience constructor with pick mode parameter not implemented.
14 * @author Edward Z. Yang <ezyang@mit.edu>
16 public class SmartPickTranslateBehavior
extends PickMouseBehavior
implements MouseBehaviorCallback
{
18 private SmartMouseTranslate translate
;
19 private PickingCallback callback
= null;
20 private TransformGroup currentTG
;
23 * Propagates rotation transform group to SmartMouseTranslate in order
24 * to provide appropriate information to transform.
25 * @note We only support one rotation transform group; this should be
26 * enough for people with shallow object graphs.
27 * @param group TransformGroup with rotation we need to adjust to
29 public void setRotateTransformGroup(TransformGroup group
) {
30 translate
.setRotateTransformGroup(group
);
34 * Creates a pick/translate behavior that waits for user mouse events for
36 * @param root Root of your scene graph.
37 * @param canvas Java 3D drawing canvas.
38 * @param bounds Bounds of your scene.
40 public SmartPickTranslateBehavior(BranchGroup root
, Canvas3D canvas
, Bounds bounds
) {
41 super(canvas
, root
, bounds
);
42 translate
= new SmartMouseTranslate(MouseBehavior
.MANUAL_WAKEUP
);
43 translate
.setTransformGroup(currGrp
);
44 currGrp
.addChild(translate
);
45 translate
.setSchedulingBounds(bounds
);
46 this.setSchedulingBounds(bounds
);
49 // below code is copied verbatim from com.sun.j3d.utils.picking.behaviors
52 * Update the scene to manipulate any nodes. This is not meant to be
53 * called by users. Behavior automatically calls this. You can call
54 * this only if you know what you are doing.
56 * @param xpos Current mouse X pos.
57 * @param ypos Current mouse Y pos.
59 public void updateScene(int xpos
, int ypos
) {
60 TransformGroup tg
= null;
62 if (!mevent
.isAltDown() && mevent
.isMetaDown()) {
64 pickCanvas
.setShapeLocation(xpos
, ypos
);
65 PickResult pr
= pickCanvas
.pickClosest();
67 ((tg
= (TransformGroup
) pr
.getNode(PickResult
.TRANSFORM_GROUP
)) != null) &&
68 (tg
.getCapability(TransformGroup
.ALLOW_TRANSFORM_READ
)) &&
69 (tg
.getCapability(TransformGroup
.ALLOW_TRANSFORM_WRITE
))) {
71 translate
.setTransformGroup(tg
);
74 // Need to clean up Issue 123 --- Chien
75 // freePickResult(pr);
76 } else if (callback
!= null) {
77 callback
.transformChanged(PickingCallback
.NO_PICK
, null);
84 * Callback method from MouseTranslate
85 * This is used when the Picking callback is enabled
87 public void transformChanged(int type
, Transform3D transform
) {
88 callback
.transformChanged(PickingCallback
.TRANSLATE
, currentTG
);
92 * Register the class @param callback to be called each
93 * time the picked object moves
95 public void setupCallback(PickingCallback callback
) {
96 this.callback
= callback
;
97 if (callback
== null) {
98 translate
.setupCallback(null);
100 translate
.setupCallback(this);