View Javadoc
1 /***************************************************************************** 2 * Virtual Mockup for Machine Vision 3 * Copyright (C) 2001-2003 Fabio R. de Miranda, João E. Kogler Jr., 4 * Carlos S. Santos. 5 * Virtual Mockup for Machine Vision Project funded by SENAC-SP 6 * 7 * Permission is granted to redistribute and/or modify this 8 * software under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This software is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License (http://www.gnu.org/copyleft/lesser.html) 16 * for more details. 17 * 18 *****************************************************************************/ 19 20 package camera3d.manipulation; 21 22 import com.sun.j3d.utils.picking.behaviors.*; 23 import com.sun.j3d.utils.picking.*; 24 import javax.media.j3d.*; 25 import camera3d.TransformMode; 26 import camera3d.TransformScope; 27 import java.awt.event.MouseEvent; 28 import java.util.Enumeration; 29 import java.awt.Event; 30 import java.awt.AWTEvent; 31 32 33 34 /*** 35 * <p>EXAMINE this class to decide if it will be include in the public release.</p> 36 * A behavior for picking and rotating scene objects. 37 * @author Fábio Roberto de Miranda, Carlos da Silva dos Santos 38 * @version 1.0 39 */ 40 41 public class PickRotateBehavior extends PickMouseBehavior { 42 /*** Axis of rotation. */ 43 private TransformScope axis; 44 /*** Mode of transformation - either local or absolute. */ 45 private TransformMode mode; 46 /*** Adapter for using with RotationManipulator.*/ 47 private RotTGAdapter adapter; 48 /*** Actual manipulator. */ 49 private RotationManipulator manip; 50 private boolean debug = false; 51 52 /*** 53 * Creates new rotate behavior given the root of tree to operate on, a Canvas3D and the Bounds. 54 * Default pick mode is PickTool.GEOMETRY. 55 */ 56 public PickRotateBehavior(Canvas3D canvas, BranchGroup root, Bounds bounds){ 57 this(canvas,root,bounds,PickTool.GEOMETRY); 58 } 59 60 /*** 61 * Creates new rotate behavior given the root of tree to operate on, Canvas3D, Bounds and pick 62 * mode. 63 */ 64 public PickRotateBehavior(Canvas3D canvas, BranchGroup root, Bounds bounds, int pickMode){ 65 super(canvas, root, bounds); 66 // needed cause superclass constructor fails to set bounds 67 this.setSchedulingBounds(bounds); 68 this.setMode(pickMode); 69 adapter = new RotTGAdapter(); 70 manip = new RotationManipulator(adapter); 71 manip.setCanvas3D(canvas); 72 } 73 74 public void initialize() { 75 76 conditions = new WakeupCriterion[3]; 77 conditions[0] = new WakeupOnAWTEvent(Event.MOUSE_DRAG); 78 conditions[1] = new WakeupOnAWTEvent(Event.MOUSE_DOWN); 79 conditions[2] = new WakeupOnAWTEvent(Event.MOUSE_UP); 80 wakeupCondition = new WakeupOr(conditions); 81 82 wakeupOn(wakeupCondition); 83 } 84 85 86 public void updateScene(int xpos, int ypos) { 87 if (debug) System.out.println("updating scene"); 88 pickCanvas.setShapeLocation(xpos,ypos); 89 PickResult result = pickCanvas.pickClosest(); 90 if(result==null) return; 91 TransformGroup tg = (TransformGroup) result.getNode(PickResult.TRANSFORM_GROUP); 92 if(tg == null|| 93 !tg.getCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ)|| 94 !tg.getCapability(TransformGroup.ALLOW_TRANSFORM_READ)|| 95 !tg.getCapability(TransformGroup.ALLOW_TRANSFORM_WRITE)){ 96 clearSelection(); 97 if (debug) System.out.println("__picking failed: cleaning selection"); 98 }else{ 99 // sets TransformGroup to be manipulated. 100 if (debug) System.out.println("__setting new transform group"); 101 adapter.setTransformGroup(tg); 102 manip.setBeginPoint(xpos,ypos); 103 } 104 reProcessMouseEvent(); 105 } 106 107 /*** 108 * Clears current selection and frees related resources. 109 */ 110 private void clearSelection(){ 111 adapter.setTransformGroup(null); 112 } 113 114 // copied from Sun's code 115 public void processStimulus (Enumeration criteria) { 116 WakeupCriterion wakeup; 117 AWTEvent[] evt = null; 118 int xpos = 0, ypos = 0; 119 120 while(criteria.hasMoreElements()) { 121 wakeup = (WakeupCriterion)criteria.nextElement(); 122 if (wakeup instanceof WakeupOnAWTEvent) 123 evt = ((WakeupOnAWTEvent)wakeup).getAWTEvent(); 124 } 125 126 if (evt[0] instanceof MouseEvent){ 127 mevent = (MouseEvent) evt[0]; 128 129 if (debug) 130 System.out.println("got mouse event"); 131 processMouseEvent((MouseEvent)evt[0]); 132 xpos = mevent.getPoint().x; 133 ypos = mevent.getPoint().y; 134 } 135 136 if (debug) 137 System.out.println("mouse position " + xpos + " " + ypos); 138 139 if (buttonPress){ 140 updateScene(xpos, ypos); 141 } 142 reProcessMouseEvent(); 143 wakeupOn (wakeupCondition); 144 } 145 146 // copied from Sun's code 147 private void processMouseEvent(MouseEvent evt) { 148 buttonPress = false; 149 150 if (evt.getID()==MouseEvent.MOUSE_PRESSED | 151 evt.getID()==MouseEvent.MOUSE_CLICKED) { 152 buttonPress = true; 153 return; 154 } 155 else if (evt.getID() == MouseEvent.MOUSE_MOVED) { 156 // Process mouse move event 157 } 158 } 159 160 161 private void reProcessMouseEvent () { 162 if (mevent == null) return; 163 if(mevent.getID() == MouseEvent.MOUSE_RELEASED){ 164 if (debug) System.out.println("___mouse released: cleaning selection"); 165 clearSelection(); 166 } 167 if(mevent.getID() == MouseEvent.MOUSE_DRAGGED){ 168 // must check whether selection is not clear 169 if (debug) System.out.println("___mouse dragged"); 170 int mod = mevent.getModifiers(); 171 if ((mod&MouseEvent.BUTTON1_MASK)==MouseEvent.BUTTON1_MASK){ 172 if (debug) System.out.println("_____setting current point in manipulator"); 173 manip.setCurrentPoint(mevent.getX(),mevent.getY()); 174 } 175 } 176 mevent = null; 177 } 178 179 public void setRotationAxis(TransformScope axis){ 180 manip.setTransformationScope(axis); 181 } 182 183 public void setTransformationMode(TransformMode mode){ 184 manip.setTransformationMode(mode); 185 } 186 187 }

This page was automatically generated by Maven