View Javadoc
1 2 package camera3d; 3 /* 4 * Title: Câmera Virtual - LIVES 5 * Description: Câmera Virtual para Controle via LabVIEW 6 * Company: Centro de Educação em Informática - SENAC - SP 7 * 8 * This code is in great part copied from the source of PickMouseBehavior in 9 * com.sun.j3d.utils.picking. It couldn't simply be extended because some of its 10 * implementation details prevented it from being used with a live scene graph. 11 * 12 * Sun's copyright notice and disclaimer are below: 13 */ 14 15 /* 16 * @(#)PickRotateBehavior.java 1.7 02/04/01 15:01:46 17 * 18 * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved. 19 * 20 * Redistribution and use in source and binary forms, with or without 21 * modification, are permitted provided that the following conditions 22 * are met: 23 * 24 * - Redistributions of source code must retain the above copyright 25 * notice, this list of conditions and the following disclaimer. 26 * 27 * - Redistribution in binary form must reproduce the above copyright 28 * notice, this list of conditions and the following disclaimer in 29 * the documentation and/or other materials provided with the 30 * distribution. 31 * 32 * Neither the name of Sun Microsystems, Inc. or the names of 33 * contributors may be used to endorse or promote products derived 34 * from this software without specific prior written permission. 35 * 36 * This software is provided "AS IS," without a warranty of any 37 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND 38 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 39 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY 40 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES 41 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR 42 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN 43 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR 44 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR 45 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF 46 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, 47 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 48 * 49 * You acknowledge that Software is not designed,licensed or intended 50 * for use in the design, construction, operation or maintenance of 51 * any nuclear facility. 52 */ 53 54 import com.sun.j3d.utils.picking.behaviors.*; 55 import com.sun.j3d.utils.picking.PickResult; 56 import com.sun.j3d.utils.picking.*; 57 import com.sun.j3d.utils.behaviors.mouse.*; 58 import javax.media.j3d.*; 59 import java.awt.event.*; 60 import java.awt.*; 61 import java.util.*; 62 63 import javax.vecmath.*; 64 import camera3d.gui.*; 65 import camera3d.PickListener; 66 67 /*** 68 * Behavior used to select scene graph objects with the mouse. 69 * 70 * Modified by Carlos Santos and Fábio Miranda from code originating from Sun Microsystems 71 * @version 1.0 72 */ 73 class VcPickMouseBehavior extends Behavior implements MouseBehaviorCallback{ 74 75 /*** Default number of mouse clicks that fire a pick operation. */ 76 public static final int DEFAULT_CLICK_COUNT = 2; 77 78 protected PickCanvas pickCanvas; 79 protected WakeupCriterion[] conditions; 80 protected WakeupOr wakeupCondition; 81 protected boolean buttonPress = false; 82 83 //protected TransformGroup currGrp; 84 //protected BranchGroup currBG; 85 86 // used to control printing of debug information 87 //protected static final boolean debug = false; 88 protected BranchGroup rootBG; 89 protected MouseEvent mevent; 90 protected Canvas3D canvas; 91 92 /*** Enables/disables picking. Typically, picking is disabled when transforming 93 * operations are performed on already selected objects. */ 94 boolean runEnable = true; 95 96 // use GEOMETRY for more precision or BOUNDS for better performance 97 int pickMode = PickTool.GEOMETRY; 98 //int pickMode = PickTool.BOUNDS; 99 100 private PickingCallback callback=null; 101 102 /*** Camera shown by the viewport where picking ocurred. We check the picking 103 * results against this camera to avoid self-picking. */ 104 VcView cameraVcView; 105 106 107 private VcObject nextObject; 108 109 private boolean debugflag = false; 110 111 /*** holds modifiers of mouse event which triggered picking, used when listeners 112 * are notified. */ 113 private int modifiers = 0; 114 115 /*** Number of mouse clicks that trigger a picking operation. */ 116 int clickCount = DEFAULT_CLICK_COUNT; 117 118 /*** List of objects interest in the results of picking operations. */ 119 private Vector pickListeners = new Vector(); 120 121 /*** Keeps list of objects which have been picked. */ 122 private ArrayList vcObjList; 123 124 125 /*** 126 * Creates a PickMouseBehavior given current canvas and the bounds. 127 */ 128 public VcPickMouseBehavior(Canvas3D canvas, Bounds bounds){ 129 super(); 130 //currBG = new BranchGroup(); 131 //currGrp = new TransformGroup(); 132 //currGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 133 //currGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 134 //currBG.setCapability(BranchGroup.ALLOW_DETACH); 135 //currBG.addChild(this); 136 //currBG.addChild(currGrp); 137 this.canvas = canvas; 138 this.setSchedulingBounds(bounds); 139 } 140 141 public VcPickMouseBehavior(Canvas3D canvas, Bounds bounds, int clickCount){ 142 this(canvas,bounds); 143 this.clickCount = clickCount; 144 } 145 146 /* 147 * Detaches this behavior from its parent 148 */ 149 /* 150 public void detach(){ 151 //currBG.detach(); 152 }*/ 153 154 /*** 155 * Sets the root BranchGroup used for picking. 156 */ 157 public void setRootBG(BranchGroup root){ 158 this.rootBG = root; 159 //currBG.detach(); // Detaches this bg from its parent in case it's attached 160 pickCanvas = new PickCanvas(canvas, root); 161 pickCanvas.setMode(pickMode); 162 //pickCanvas.setTolerance(0.0f); 163 pickCanvas.setTolerance(4.0f); 164 //rootBG.addChild(currBG); 165 } 166 167 public void setCanvas(Canvas3D canvas){ 168 pickCanvas = new PickCanvas(canvas, rootBG); 169 pickCanvas.setMode(pickMode); 170 pickCanvas.setTolerance(4.0f); 171 } 172 173 /*** 174 * Sets the pick mode 175 * @see PickTool#setMode 176 **/ 177 public void setMode(int pickMode) { 178 pickCanvas.setMode(pickMode); 179 } 180 181 /*** 182 * Returns the pickMode 183 * @see PickTool#getMode 184 */ 185 public int getMode() { 186 return pickCanvas.getMode(); 187 } 188 189 /*** 190 * Sets the pick tolerance 191 * @see PickCanvas#setTolerance 192 */ 193 public void setTolerance(float tolerance) { 194 pickCanvas.setTolerance(tolerance); 195 } 196 197 /*** 198 * Returns the pick tolerance 199 * @see PickCanvas#getTolerance 200 */ 201 public float getTolerance() { 202 return pickCanvas.getTolerance(); 203 } 204 205 public void initialize() { 206 //System.out.println("Behavior initializing"); 207 conditions = new WakeupCriterion[2]; 208 conditions[0] = new WakeupOnAWTEvent(Event.MOUSE_MOVE); 209 conditions[1] = new WakeupOnAWTEvent(Event.MOUSE_DOWN); 210 //conditions[1] = new WakeupOnElapsedFrames(2); 211 wakeupCondition = new WakeupOr(conditions); 212 wakeupOn(wakeupCondition); 213 } 214 215 private void processMouseEvent(MouseEvent evt) { 216 buttonPress = false; 217 /* 218 * the following function call was added because the mouse event is 219 * sent to all pick behaviors, causing the bug that made objects get 220 * selected by one behavior just to be unselected by another one in the 221 * same selection cycle; that virtually prevented selection of 222 * objects sometimes. 223 * Carlos 18/07/2002 224 */ 225 if(!isFromProperCanvas(evt)) return; 226 if (evt.getID()==MouseEvent.MOUSE_PRESSED | 227 evt.getID()==MouseEvent.MOUSE_CLICKED) { 228 // 229 if(evt.getClickCount()!=clickCount) return; 230 int mod = evt.getModifiers(); 231 // Contribuição de Fábio - 02/05/2002 232 if ((mod&MouseEvent.BUTTON1_MASK)==MouseEvent.BUTTON1_MASK){ 233 buttonPress = true; 234 } 235 return; 236 } 237 else if (evt.getID() == MouseEvent.MOUSE_MOVED) { 238 // Process mouse move event 239 } 240 241 } 242 243 public void processStimulus (Enumeration criteria) { 244 //System.out.println("Processing stimulus"); 245 if (runEnable){ 246 //debugln("running enabled"); 247 WakeupCriterion wakeup; 248 AWTEvent[] evt = null; 249 int xpos = 0, ypos = 0; 250 251 while(criteria.hasMoreElements()) { 252 wakeup = (WakeupCriterion)criteria.nextElement(); 253 if (wakeup instanceof WakeupOnAWTEvent){ 254 evt = ((WakeupOnAWTEvent)wakeup).getAWTEvent(); 255 } 256 } 257 258 if (evt==null) return; 259 if (evt[0] instanceof MouseEvent){ 260 mevent = (MouseEvent) evt[0]; 261 //debugln("got mouse event"); 262 processMouseEvent(mevent); 263 xpos = mevent.getPoint().x; 264 ypos = mevent.getPoint().y; 265 modifiers = mevent.getModifiers(); 266 } else modifiers = 0; 267 if (buttonPress){ 268 updateScene(xpos, ypos); 269 notifyPickListeners(); 270 } 271 } 272 273 wakeupOn (wakeupCondition); 274 } 275 276 /* 277 * Actual "action" 278 */ 279 public void updateScene(int xpos, int ypos){ 280 if (pickCanvas == null){ 281 debugln("PickCanvas is null, please set proper PickCanvas first"); 282 return; 283 } 284 if (rootBG == null){ 285 debugln("rootBG is null, please set proper root BranchGroup first"); 286 return; 287 } 288 289 BranchGroup bg = null; 290 291 pickCanvas.setShapeLocation(xpos, ypos); 292 293 PickResult[] prVec; 294 try{ 295 prVec = pickCanvas.pickAllSorted(); 296 //prVec = pickCanvas.pickAll(); 297 }catch(CapabilityNotSetException ex){ 298 ex.printStackTrace(); 299 return; 300 } 301 // debug only section; remove it in the future 302 303 debugln("PickResult[]:"); 304 for(int i=0;i<prVec.length;i++){ 305 debugln("____# "+i); 306 BranchGroup bGroup = (BranchGroup)(prVec[i]).getNode(PickResult.BRANCH_GROUP); 307 if(bGroup==null) debugln("BranchGroup == null"); 308 else debugln(bGroup.toString()); 309 Object obj = (Object)bGroup.getUserData(); 310 if(obj==null) debugln("User data == null"); 311 /* 312 if (obj instanceof VcObject){ 313 System.out.println("_"+i+":"+((VcObject)obj).getLabel()); 314 }*/ 315 } 316 //if(this.cameraVcView!=null) debugln("Current camera: "+ this.cameraVcView.getLabel()); 317 318 // end of debug section 319 320 vcObjList = new java.util.ArrayList(); 321 322 int vcObjCount = 0; 323 /* copies list of PickResults into a list of VcObjects */ 324 for(int i=0;i<prVec.length;i++){ 325 BranchGroup bGroup = (BranchGroup)(prVec[i]).getNode(PickResult.BRANCH_GROUP); 326 Object obj = (Object)bGroup.getUserData(); 327 if (obj instanceof VcObject){ 328 nextObject=(VcObject)obj; 329 if(nextObject!=this.cameraVcView){ 330 if(!(vcObjList.contains(nextObject) )){ 331 vcObjList.add(nextObject); 332 } 333 } 334 } 335 } 336 337 } 338 339 340 341 /* 342 * The picking occurs at a Viewport that shows the image of a camera. Sometimes it may happen 343 * that the camera is picked by mistake. To avoid that, we supply a camera (VcView) 344 * and avoid reporting it as picked 345 */ 346 public void setView(VcView view){ 347 this.cameraVcView = view; 348 } 349 350 /*** 351 * Used to display debug messages. 352 */ 353 public void debugln(String s){ 354 //if (GUIControl.debugflag) 355 if (debugflag) 356 System.out.println(s); 357 } 358 359 /*** 360 * Checks whether the object that generated the mouse event 361 * is the Canvas3D to which this behavior is attached 362 */ 363 public boolean isFromProperCanvas(MouseEvent event){ 364 Object src = event.getSource(); 365 366 if(!(src instanceof Canvas3D)) return false; 367 else { 368 Canvas3D tcanvas = (Canvas3D)src; 369 if(tcanvas!=this.canvas){ 370 //debugln("mouse event is not from proper canvas"); 371 return false; 372 } 373 else{ 374 //debugln("Mouse Event is from proper canvas"); 375 return true; 376 } 377 } 378 } 379 380 /*** 381 * Adds listener to the list of objects which receive picking notifications. 382 */ 383 public void addPickListener(PickListener listener){ 384 pickListeners.add(listener); 385 } 386 387 /*** 388 * Removes listener from list of objects which receive picking notifications. 389 */ 390 public void removePickListener(PickListener listener){ 391 pickListeners.remove(listener); 392 } 393 394 /*** 395 * Removes all listeners from list of objects which receive picking notifications. 396 */ 397 public void removeAllPickListeners(){ 398 pickListeners.removeAllElements(); 399 } 400 401 /*** 402 * Notifies objects registered as listeners that a picking operation has been 403 * performed. 404 */ 405 private void notifyPickListeners(){ 406 Iterator iter = pickListeners.iterator(); 407 while(iter.hasNext()){ 408 ((PickListener)iter.next()).pickingOcurred(modifiers,vcObjList); 409 } 410 } 411 412 413 public void transformChanged(int i, Transform3D t3D){ 414 } 415 416 /* 417 * Controls execution of picking operations. Usually, picking is disabled when 418 * we are moving or editing objects previously selected, so mouse events don't 419 * get misinterpreted. 420 * @param b enables picking if true, disables picking if false. 421 */ 422 /* 423 public void setEnable(boolean b){ 424 this.runEnable = b; 425 }*/ 426 427 } 428

This page was automatically generated by Maven