1 /* 2 * $RCSfile: PickTranslateBehavior.java,v $ 3 * 4 * Copyright (c) 2007 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.4 $ 41 * $Date: 2007/02/09 17:20:14 $ 42 * $State: Exp $ 43 */ 44 45 package com.sun.j3d.utils.behaviors.picking; 46 47 import com.sun.j3d.utils.behaviors.mouse.*; 48 import java.awt.*; 49 import java.awt.event.*; 50 import java.util.*; 51 import javax.media.j3d.*; 52 import javax.vecmath.*; 53 54 // A mouse behavior that allows user to pick and translate scene graph objects. 55 // Common usage: 1. Create your scene graph. 2. Create this behavior with 56 // the root and canvas. See PickRotateBehavior for more details. 57 58 /** 59 * @deprecated As of Java 3D version 1.2, replaced by 60 * <code>com.sun.j3d.utils.picking.behaviors.PickTranslateBehavior</code> 61 * 62 * @see com.sun.j3d.utils.picking.behaviors.PickTranslateBehavior 63 */ 64 65 public class PickTranslateBehavior extends PickMouseBehavior implements MouseBehaviorCallback { 66 MouseTranslate translate; 67 int pickMode = PickObject.USE_BOUNDS; 68 private PickingCallback callback = null; 69 private TransformGroup currentTG; 70 71 /** 72 * Creates a pick/translate behavior that waits for user mouse events for 73 * the scene graph. This method has its pickMode set to BOUNDS picking. 74 * @param root Root of your scene graph. 75 * @param canvas Java 3D drawing canvas. 76 * @param bounds Bounds of your scene. 77 **/ 78 PickTranslateBehavior(BranchGroup root, Canvas3D canvas, Bounds bounds)79 public PickTranslateBehavior(BranchGroup root, Canvas3D canvas, Bounds bounds){ 80 super(canvas, root, bounds); 81 translate = new MouseTranslate(MouseBehavior.MANUAL_WAKEUP); 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 PickObject.USE_BOUNDS or PickObject.USE_GEOMETRY. 95 * Note: If pickMode is set to PickObject.USE_GEOMETRY, all geometry object in 96 * the scene graph that allows pickable must have its ALLOW_INTERSECT bit set. 97 **/ 98 PickTranslateBehavior(BranchGroup root, Canvas3D canvas, Bounds bounds, int pickMode)99 public PickTranslateBehavior(BranchGroup root, Canvas3D canvas, Bounds bounds, 100 int pickMode){ 101 super(canvas, root, bounds); 102 translate = new MouseTranslate(MouseBehavior.MANUAL_WAKEUP); 103 translate.setTransformGroup(currGrp); 104 currGrp.addChild(translate); 105 translate.setSchedulingBounds(bounds); 106 this.setSchedulingBounds(bounds); 107 this.pickMode = pickMode; 108 } 109 110 /** 111 * Sets the pickMode component of this PickTranslateBehavior to the value of 112 * the passed pickMode. 113 * @param pickMode the pickMode to be copied. 114 **/ 115 setPickMode(int pickMode)116 public void setPickMode(int pickMode) { 117 this.pickMode = pickMode; 118 } 119 120 /** 121 * Return the pickMode component of this PickTranslaeBehavior. 122 **/ 123 getPickMode()124 public int getPickMode() { 125 return pickMode; 126 } 127 128 /** 129 * Update the scene to manipulate any nodes. This is not meant to be 130 * called by users. Behavior automatically calls this. You can call 131 * this only if you know what you are doing. 132 * 133 * @param xpos Current mouse X pos. 134 * @param ypos Current mouse Y pos. 135 **/ updateScene(int xpos, int ypos)136 public void updateScene(int xpos, int ypos){ 137 TransformGroup tg = null; 138 139 if (!mevent.isAltDown() && mevent.isMetaDown()){ 140 141 tg =(TransformGroup)pickScene.pickNode(pickScene.pickClosest(xpos, ypos, pickMode), 142 PickObject.TRANSFORM_GROUP); 143 //Check for valid selection. 144 if ((tg != null) && 145 (tg.getCapability(TransformGroup.ALLOW_TRANSFORM_READ)) && 146 (tg.getCapability(TransformGroup.ALLOW_TRANSFORM_WRITE))){ 147 148 translate.setTransformGroup(tg); 149 translate.wakeup(); 150 currentTG = tg; 151 } else if (callback!=null) 152 callback.transformChanged( PickingCallback.NO_PICK, null ); 153 } 154 155 } 156 157 /** 158 * Callback method from MouseTranslate 159 * This is used when the Picking callback is enabled 160 */ transformChanged( int type, Transform3D transform )161 public void transformChanged( int type, Transform3D transform ) { 162 callback.transformChanged( PickingCallback.TRANSLATE, currentTG ); 163 } 164 165 /** 166 * Register the class @param callback to be called each 167 * time the picked object moves 168 */ setupCallback( PickingCallback callback )169 public void setupCallback( PickingCallback callback ) { 170 this.callback = callback; 171 if (callback==null) 172 translate.setupCallback( null ); 173 else 174 translate.setupCallback( this ); 175 } 176 177 } 178 179