1 /*
2 * $RCSfile: PickTranslateBehavior.java,v $
3 *
4 * Copyright (c) 2006 Sun Microsystems, Inc. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistribution of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * - Redistribution in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Sun Microsystems, Inc. or the names of
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * This software is provided "AS IS," without a warranty of any
23 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
24 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
26 * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
27 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
28 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
29 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
30 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
31 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
32 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
33 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGES.
35 *
36 * You acknowledge that this software is not designed, licensed or
37 * intended for use in the design, construction, operation or
38 * maintenance of any nuclear facility.
39 *
40 * $Revision: 1.1 $
41 * $Date: 2009/04/08 06:31:15 $
42 * $State: Exp $
43 */
44
45 package edu.uci.ics.jung.visualization3d.control;
46
47 import javax.media.j3d.Bounds;
48 import javax.media.j3d.BranchGroup;
49 import javax.media.j3d.Canvas3D;
50 import javax.media.j3d.Transform3D;
51 import javax.media.j3d.TransformGroup;
52
53 import com.sun.j3d.utils.behaviors.mouse.MouseBehavior;
54 import com.sun.j3d.utils.behaviors.mouse.MouseBehaviorCallback;
55 import com.sun.j3d.utils.picking.PickResult;
56 import com.sun.j3d.utils.picking.PickTool;
57 import com.sun.j3d.utils.picking.behaviors.PickingCallback;
58
59 /**
60 * A mouse behavior that allows user to pick and translate scene graph objects.
61 * Common usage: 1. Create your scene graph. 2. Create this behavior with
62 * the root and canvas. See PickRotateBehavior for more details.
63 */
64
65 public class PickTranslateBehavior extends PickMouseBehavior implements MouseBehaviorCallback {
66 MouseTranslate translate;
67 private PickingCallback callback = null;
68 private TransformGroup currentTG;
69
70 /**
71 * Creates a pick/translate behavior that waits for user mouse events for
72 * the scene graph.
73 * @param root Root of your scene graph.
74 * @param canvas Java 3D drawing canvas.
75 * @param bounds Bounds of your scene.
76 **/
77
78 public PickTranslateBehavior(BranchGroup root, Canvas3D canvas, Bounds bounds){
79 super(canvas, root, bounds);
80 translate = new MouseTranslate(MouseBehavior.MANUAL_WAKEUP);
81 translate.setFactor(1);
82 translate.setTransformGroup(currGrp);
83 currGrp.addChild(translate);
84 translate.setSchedulingBounds(bounds);
85 this.setSchedulingBounds(bounds);
86 }
87
88 /**
89 * Creates a pick/translate behavior that waits for user mouse events for
90 * the scene graph.
91 * @param root Root of your scene graph.
92 * @param canvas Java 3D drawing canvas.
93 * @param bounds Bounds of your scene.
94 * @param pickMode specifys PickTool.BOUNDS, PickTool.GEOMETRY or
95 * PickTool.GEOMETRY_INTERSECT_INFO.
96 * @see PickTool#setMode
97 **/
98 public PickTranslateBehavior(BranchGroup root, Canvas3D canvas,
99 Bounds bounds, int pickMode) {
100 super(canvas, root, bounds);
101 translate = new MouseTranslate(MouseBehavior.MANUAL_WAKEUP);
102 translate.setFactor(1);
103 translate.setTransformGroup(currGrp);
104 currGrp.addChild(translate);
105 translate.setSchedulingBounds(bounds);
106 this.setSchedulingBounds(bounds);
107 this.setMode(pickMode);
108 }
109
110
111 /**
112 * Update the scene to manipulate any nodes. This is not meant to be
113 * called by users. Behavior automatically calls this. You can call
114 * this only if you know what you are doing.
115 *
116 * @param xpos Current mouse X pos.
117 * @param ypos Current mouse Y pos.
118 **/
119 public void updateScene(int xpos, int ypos){
120 // System.err.println("update scene pick translate");
121 TransformGroup tg = null;
122
123 if (!mevent.isAltDown() && !mevent.isMetaDown()){
124
125 pickCanvas.setShapeLocation(xpos, ypos);
126 PickResult pr = pickCanvas.pickClosest();
127 if ((pr != null) &&
128 ((tg = (TransformGroup)pr.getNode(PickResult.TRANSFORM_GROUP))
129 != null) &&
130 (tg.getCapability(TransformGroup.ALLOW_TRANSFORM_READ)) &&
131 (tg.getCapability(TransformGroup.ALLOW_TRANSFORM_WRITE))){
132
133 translate.setTransformGroup(tg);
134 translate.wakeup();
135 currentTG = tg;
136 // Need to clean up Issue 123 --- Chien
137 // freePickResult(pr);
138 } else if (callback!=null)
139 callback.transformChanged( PickingCallback.NO_PICK, null );
140 }
141
142 }
143
144 /**
145 * Callback method from MouseTranslate
146 * This is used when the Picking callback is enabled
147 */
148 public void transformChanged( int type, Transform3D transform ) {
149 callback.transformChanged( PickingCallback.TRANSLATE, currentTG );
150 }
151
152 /**
153 * Register the class @param callback to be called each
154 * time the picked object moves
155 */
156 public void setupCallback( PickingCallback callback ) {
157 this.callback = callback;
158 if (callback==null)
159 translate.setupCallback( null );
160 else
161 translate.setupCallback( this );
162 }
163
164 }
165