1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.mouse.*;
/**
* MouseTranslate is a Java3D behavior object that lets users control the
* translation (X, Y) of an object via a mouse drag motion with the third
* mouse button (alt-click on PC). See MouseRotate for similar usage info.
*/
public class CustomMouseTranslate extends MouseBehavior {
double x_factor = .02;
double y_factor = .02;
Vector3d translation = new Vector3d();
private MouseBehaviorCallback callback = null;
/**
* Creates a mouse translate behavior given the transform group.
* @param transformGroup The transformGroup to operate on.
*/
public CustomMouseTranslate(TransformGroup transformGroup) {
super(transformGroup);
}
/**
* Creates a default translate behavior.
*/
public CustomMouseTranslate(){
super(0);
}
/**
* Creates a translate behavior.
* Note that this behavior still needs a transform
* group to work on (use setTransformGroup(tg)) and
* the transform group must add this behavior.
* @param flags
*/
public CustomMouseTranslate(int flags)
{
// Cette méthode est appelée par 'CustomPickTranslateBehavior'.
super(flags);
}
/**
* Creates a translate behavior that uses AWT listeners and behavior
* posts rather than WakeupOnAWTEvent. The behavior is added to the
* specified Component. A null component can be passed to specify
* the behavior should use listeners. Components can then be added
* to the behavior with the addListener(Component c) method.
* @param c The Component to add the MouseListener
* and MouseMotionListener to.
* @since Java 3D 1.2.1
*/
public CustomMouseTranslate(Component c) {
super(c, 0);
}
/**
* Creates a translate behavior that uses AWT listeners and behavior
* posts rather than WakeupOnAWTEvent. The behaviors is added to
* the specified Component and works on the given TransformGroup.
* A null component can be passed to specify the behavior should use
* listeners. Components can then be added to the behavior with the
* addListener(Component c) method.
* @param c The Component to add the MouseListener and
* MouseMotionListener to.
* @param transformGroup The TransformGroup to operate on.
* @since Java 3D 1.2.1
*/
public CustomMouseTranslate(Component c, TransformGroup transformGroup) {
super(c, transformGroup);
}
/**
* Creates a translate behavior that uses AWT listeners and behavior
* posts rather than WakeupOnAWTEvent. The behavior is added to the
* specified Component. A null component can be passed to specify
* the behavior should use listeners. Components can then be added to
* the behavior with the addListener(Component c) method.
* Note that this behavior still needs a transform
* group to work on (use setTransformGroup(tg)) and the transform
* group must add this behavior.
* @param flags interesting flags (wakeup conditions).
* @since Java 3D 1.2.1
*/
public CustomMouseTranslate(Component c, int flags) {
super(c, flags);
}
public void initialize() {
super.initialize();
if ((flags & INVERT_INPUT) == INVERT_INPUT) {
invert = true;
x_factor *= -1;
y_factor *= -1;
}
}
/**
* Return the x-axis movement multipler.
**/
public double getXFactor() {
return x_factor;
}
/**
* Return the y-axis movement multipler.
**/
public double getYFactor() {
return y_factor;
}
/**
* Set the x-axis amd y-axis movement multipler with factor.
**/
public void setFactor( double factor) {
x_factor = y_factor = factor;
}
/**
* Set the x-axis amd y-axis movement multipler with xFactor and yFactor
* respectively.
**/
public void setFactor( double xFactor, double yFactor) {
x_factor = xFactor;
y_factor = yFactor;
}
public void processStimulus (Enumeration criteria) {
WakeupCriterion wakeup;
AWTEvent[] events;
MouseEvent evt;
// int id;
// int dx, dy;
while (criteria.hasMoreElements()) {
wakeup = (WakeupCriterion) criteria.nextElement();
if (wakeup instanceof WakeupOnAWTEvent) {
events = ((WakeupOnAWTEvent)wakeup).getAWTEvent();
if (events.length > 0) {
evt = (MouseEvent) events[events.length-1];
doProcess(evt);
}
}
else if (wakeup instanceof WakeupOnBehaviorPost) {
while (true) {
// access to the queue must be synchronized
synchronized (mouseq) {
if (mouseq.isEmpty()) break;
evt = (MouseEvent)mouseq.remove(0);
// consolodate MOUSE_DRAG events
while ((evt.getID() == MouseEvent.MOUSE_DRAGGED) &&
!mouseq.isEmpty() &&
(((MouseEvent)mouseq.get(0)).getID() ==
MouseEvent.MOUSE_DRAGGED)) {
evt = (MouseEvent)mouseq.remove(0);
}
}
doProcess(evt);
}
}
}
wakeupOn(mouseCriterion);
}
void doProcess(MouseEvent evt) {
int id;
int dx, dy;
processMouseEvent(evt);
if (((buttonPress)&&((flags & MANUAL_WAKEUP) == 0)) ||
((wakeUp)&&((flags & MANUAL_WAKEUP) != 0))){
id = evt.getID();
if ((id == MouseEvent.MOUSE_DRAGGED) &&
!evt.isAltDown() && !evt.isMetaDown()) {
x = evt.getX();
y = evt.getY();
dx = x - x_last;
dy = y - y_last;
if ((!reset) && ((Math.abs(dy) < 50) && (Math.abs(dx) < 50))) {
//System.out.println("dx " + dx + " dy " + dy);
transformGroup.getTransform(currXform);
translation.x = dx*x_factor;
translation.y = -dy*y_factor;
transformX.set(translation);
if (invert) {
currXform.mul(currXform, transformX);
} else {
currXform.mul(transformX, currXform);
}
// Ligne suivante désactivée: l'objet cliqué ne sera pas déplacé.
// Car on ne veut traiter que les clics de souris.
// transformGroup.setTransform(currXform);
transformChanged (currXform);
if (callback!=null) callback.transformChanged (MouseBehaviorCallback.TRANSLATE, currXform );
}
else {
reset = false;
}
x_last = x;
y_last = y;
}
else if (id == MouseEvent.MOUSE_PRESSED) {
x_last = evt.getX();
y_last = evt.getY();
}
}
}
/**
* Users can overload this method which is called every time
* the Behavior updates the transform
*
* Default implementation does nothing
*/
public void transformChanged (Transform3D transform)
{
// Ici on peut réaliser des actions chaque fois que la scène est modifiée.
// System.out.println("*");
}
/**
* The transformChanged method in the callback class will
* be called every time the transform is updated
*/
public void setupCallback( MouseBehaviorCallback callback ) {
this.callback = callback;
}
} |
Partager