View Javadoc

1   /*
2    * $RCSfile: PickMouseBehavior.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 java.awt.AWTEvent;
48  import java.awt.Event;
49  import java.awt.event.MouseEvent;
50  import java.util.Enumeration;
51  
52  import javax.media.j3d.Behavior;
53  import javax.media.j3d.Bounds;
54  import javax.media.j3d.BranchGroup;
55  import javax.media.j3d.Canvas3D;
56  import javax.media.j3d.TransformGroup;
57  import javax.media.j3d.WakeupCriterion;
58  import javax.media.j3d.WakeupOnAWTEvent;
59  import javax.media.j3d.WakeupOr;
60  
61  import com.sun.j3d.utils.picking.PickCanvas;
62  import com.sun.j3d.utils.picking.PickTool;
63  
64  
65  /**
66   * Base class that allows users to adding picking and mouse manipulation to
67   * the scene graph (see PickDragBehavior for an example of how to extend
68   * this base class). This class is useful for interactive apps.
69   */
70  
71  public abstract class PickMouseBehavior extends Behavior {
72    
73    protected PickCanvas pickCanvas;
74  
75    protected WakeupCriterion[] conditions;
76    protected WakeupOr wakeupCondition;
77    protected boolean buttonPress = false;
78  
79    protected TransformGroup currGrp;
80    protected static final boolean debug = false;
81    protected MouseEvent mevent;
82    
83    /** 
84     * Creates a PickMouseBehavior given current canvas, root of the tree to
85     * operate on, and the bounds.
86     */
87    public PickMouseBehavior(Canvas3D canvas, BranchGroup root, Bounds bounds){
88      super();
89      currGrp = new TransformGroup();
90      currGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
91      currGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
92      root.addChild(currGrp);
93      pickCanvas = new PickCanvas(canvas, root);
94    }
95  
96    /**
97     * Sets the pick mode 
98     * @see PickTool#setMode
99     **/  
100   public void setMode(int pickMode) {
101     pickCanvas.setMode(pickMode);
102   }
103 
104  /**
105    * Returns the pickMode
106    * @see PickTool#getMode
107    */ 
108 
109   public int getMode() {
110     return pickCanvas.getMode();
111   }
112 
113   /**
114    * Sets the pick tolerance 
115    * @see PickCanvas#setTolerance
116    */  
117   public void setTolerance(float tolerance) {
118     pickCanvas.setTolerance(tolerance);
119   }
120 
121   /**
122     * Returns the pick tolerance
123     * @see PickCanvas#getTolerance
124     */ 
125   public float getTolerance() {
126     return pickCanvas.getTolerance();
127   }
128 
129   public void initialize() {
130 
131     conditions = new WakeupCriterion[2];
132     conditions[0] = new WakeupOnAWTEvent(Event.MOUSE_MOVE);
133     conditions[1] = new WakeupOnAWTEvent(Event.MOUSE_DOWN);
134     wakeupCondition = new WakeupOr(conditions);
135 
136     wakeupOn(wakeupCondition);
137   }
138   
139   private void processMouseEvent(MouseEvent evt) {
140     buttonPress = false;
141 
142     if (evt.getID()==MouseEvent.MOUSE_PRESSED |
143 	evt.getID()==MouseEvent.MOUSE_CLICKED) {
144       buttonPress = true;
145       return;
146     }
147     else if (evt.getID() == MouseEvent.MOUSE_MOVED) {
148       // Process mouse move event
149     }
150   }
151   
152   public void processStimulus (Enumeration criteria) {
153     WakeupCriterion wakeup;
154     AWTEvent[] evt = null;
155     int xpos = 0, ypos = 0;
156 
157     while(criteria.hasMoreElements()) {
158       wakeup = (WakeupCriterion)criteria.nextElement();
159       if (wakeup instanceof WakeupOnAWTEvent)
160 	evt = ((WakeupOnAWTEvent)wakeup).getAWTEvent();
161     }
162     
163     if (evt[0] instanceof MouseEvent){
164       mevent = (MouseEvent) evt[0];
165 
166       if (debug)
167 	System.out.println("got mouse event");
168       processMouseEvent((MouseEvent)evt[0]);
169       xpos = mevent.getPoint().x;
170       ypos = mevent.getPoint().y;
171     }
172     
173     if (debug)
174       System.out.println("mouse position " + xpos + " " + ypos);
175     
176     if (buttonPress){
177       updateScene(xpos, ypos);
178     }
179     wakeupOn (wakeupCondition);
180   }
181 
182   /** Subclasses shall implement this update function 
183    */
184   public abstract void updateScene(int xpos, int ypos);
185 
186 }
187