View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class MouseManipulatorBehavior
3    * 
4    * Copyright (c), 2003, Masahiro Takatsuka.
5    * All Rights Researved.
6    * 
7    * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8    * $Author: takatsukam $
9    * 
10   * $Date: 2004/03/03 11:53:07 $
11   * 
12   * $Id: MouseManipulatorBehavior.java,v 1.3 2004/03/03 11:53:07 takatsukam Exp $
13   * 
14   * Reference:		Document no:
15   * ___				___
16   * 
17   * To Do:
18   * ___
19   * 
20  ------------------------------------------------------------------- */
21  
22  /* --------------------------- Package ---------------------------- */
23  package net.jbeans.j3d.util.behavior.mouse;
24  
25  /* ------------------ Import classes (packages) ------------------- *//package-summary/html">class="comment"> ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment"> ------------------ Import classes (packages) ------------------- */
26  import java.awt.*;
27  import java.awt.event.*;
28  import java.util.*;
29  import javax.media.j3d.*;
30  
31  import net.jbeans.j3d.util.manipulator.*;
32  
33  /*====================================================================
34             Implementation of class MouseManipulatorBehavior           
35  ====================================================================*/
36  /***
37   * MtMouseManipulatorBehavior is a mouse event manager.  When this object
38   * receives a mouse event, it creates a MtStimulus object, which contains
39   * all possible information needed to manipulate 3D objects, and send it
40   * to registered objects, which process the stimulus.
41   * 
42   * @version $Revision: 1.3 $
43   * @author Masahiro Takatsuka (masa@jbeans.net)
44   * @see Behavior
45   */
46  
47  public class MouseManipulatorBehavior extends Behavior {
48      public static final int MANUAL_WAKEUP = 1;
49      public static final int INVERT_INPUT = 2;
50  	
51      private WakeupCriterion[] mouseEvents;
52      private WakeupOr mouseCriterion;
53      private int x;
54      private int y;
55      private int x_last;
56      private int y_last;
57      private int x_center;
58      private int y_center;
59      private boolean buttonPress;
60      private boolean reset;
61      private boolean invert;
62      private boolean wakeUp;
63      private int flags;
64  	private Map dragManipulators;
65  	private Map pressManipulators;	
66  	private Stimulus stimulus;
67  	private TransformGroup topTG;
68  	
69      public MouseManipulatorBehavior(int i) {
70          this(new TransformGroup());
71          this.flags = i;
72      }
73  
74      public MouseManipulatorBehavior(TransformGroup topTG) {
75          this.buttonPress = false;
76          this.invert = false;
77          this.wakeUp = false;
78          this.flags = 0;
79          this.reset = true;
80  		this.dragManipulators = Collections.synchronizedMap(new HashMap());
81  		this.pressManipulators = Collections.synchronizedMap(new HashMap());
82  		this.stimulus = new Stimulus();
83  		this.topTG = topTG;
84      }
85  	
86      public void initialize() {
87          this.mouseEvents = new WakeupCriterion[2];
88          this.mouseEvents[0] = new WakeupOnAWTEvent(MouseEvent.MOUSE_DRAGGED);
89          this.mouseEvents[1] = new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);
90  //          mouseEvents[2] = new WakeupOnAWTEvent(MouseEvent.MOUSE_RELEASED);
91  //          mouseEvents[3] = new WakeupOnAWTEvent(MouseEvent.MOUSE_CLICKED);
92          this.mouseCriterion = new WakeupOr(this.mouseEvents);
93          wakeupOn(this.mouseCriterion);
94          this.x = 0;
95          this.y = 0;
96          this.x_last = 0;
97          this.y_last = 0;
98          this.x_center = 0;
99          this.y_center = 0;
100     }
101 
102     public void processMouseEvent(MouseEvent mouseevent) {
103 		// compute the center of Canvas3D.
104 		Component comp = mouseevent.getComponent();
105 		Dimension dim = comp.getSize();
106 		this.x_center = dim.width / 2;
107 		this.y_center = dim.height / 2;
108 
109 		// find out mouse pressed/released
110         if (mouseevent.getID() == MouseEvent.MOUSE_PRESSED) {
111             this.buttonPress = true;
112             return;
113         }
114         if (mouseevent.getID() == MouseEvent.MOUSE_RELEASED) {
115             this.buttonPress = false;
116             this.wakeUp = false;
117         }
118         else {
119             mouseevent.getID();
120         }
121     }
122 
123 	
124 	public void addDragManipulator(int modifiers, Manipulator manipulator) {
125 		this.dragManipulators.put(new Integer(modifiers), manipulator);
126 	}
127 
128 	public void removeDragManipulator(int modifiers) {
129 		this.dragManipulators.remove(new Integer(modifiers));
130 	}
131 
132 	public Manipulator getDragManipulator(int modifiers) {
133 		return (Manipulator) this.dragManipulators.get(new Integer(modifiers));
134 	}
135 
136 	public void addPressManipulator(int modifiers, Manipulator manipulator) {
137 		this.pressManipulators.put(new Integer(modifiers), manipulator);
138 	}
139 
140 	public void removePressManipulator(int modifiers) {
141 		this.pressManipulators.remove(new Integer(modifiers));
142 	}
143 
144 	public Manipulator getPressManipulator(int modifiers) {
145 		return (Manipulator) this.pressManipulators.get(new Integer(modifiers));
146 	}
147 	
148     public void processStimulus(Enumeration enumeration) {
149         while (enumeration.hasMoreElements()) {
150             WakeupCriterion wakeupcriterion = (WakeupCriterion)enumeration.nextElement();
151             if (wakeupcriterion instanceof WakeupOnAWTEvent) {
152                 AWTEvent aawtevent[] = ((WakeupOnAWTEvent)wakeupcriterion).getAWTEvent();
153                 for (int l = 0; l < aawtevent.length; l++) {
154                     processMouseEvent((MouseEvent)aawtevent[l]);
155                     if (this.buttonPress && (this.flags & 0x1) == 0 ||
156 						this.wakeUp && (this.flags & 0x1) != 0) {
157                         int i = aawtevent[l].getID();
158 						this.x = ((MouseEvent)aawtevent[l]).getPoint().x;
159 						this.y = ((MouseEvent)aawtevent[l]).getPoint().y;
160 						this.reset = false;
161 						if (!this.reset) {
162 							this.stimulus.setX(this.x);
163 							this.stimulus.setY(this.y);
164 							this.stimulus.setLastX(this.x_last);
165 							this.stimulus.setLastY(this.y_last);
166 							this.stimulus.setCenterX(this.x_center);
167 							this.stimulus.setCenterY(this.y_center);
168 							this.stimulus.setTopTransformGroup(this.topTG);
169 						} else {
170 							this.reset = false;
171 						}
172 						Integer modifier = new Integer(((MouseEvent)aawtevent[l]).getModifiers());
173 						Manipulator man = null;
174                         if (i == MouseEvent.MOUSE_DRAGGED) {
175 							man = (Manipulator) this.dragManipulators.get(modifier);
176                         } else if (i == MouseEvent.MOUSE_PRESSED) {
177 							man = (Manipulator) this.pressManipulators.get(modifier);
178                         }
179 						if (man != null) {
180 							man.processStimulus(this.stimulus);
181 						}
182 						this.x_last = this.x;
183 						this.y_last = this.y;
184                     }
185                 }
186             }
187         }
188 
189         wakeupOn(this.mouseCriterion);
190 	}
191 	
192     public void wakeup() {
193         this.wakeUp = true;
194     }
195 }