diff options
-rw-r--r-- | src/classes/share/com/sun/j3d/utils/behaviors/mouse/MouseBehavior.java | 38 | ||||
-rw-r--r-- | src/classes/share/com/sun/j3d/utils/behaviors/mouse/MouseWheelZoom.java | 254 |
2 files changed, 287 insertions, 5 deletions
diff --git a/src/classes/share/com/sun/j3d/utils/behaviors/mouse/MouseBehavior.java b/src/classes/share/com/sun/j3d/utils/behaviors/mouse/MouseBehavior.java index 20e8f08..58b3e83 100644 --- a/src/classes/share/com/sun/j3d/utils/behaviors/mouse/MouseBehavior.java +++ b/src/classes/share/com/sun/j3d/utils/behaviors/mouse/MouseBehavior.java @@ -59,7 +59,7 @@ import com.sun.j3d.internal.J3dUtilsI18N; */ public abstract class MouseBehavior extends Behavior - implements MouseListener, MouseMotionListener { + implements MouseListener, MouseMotionListener, MouseWheelListener { private boolean listener = false; @@ -141,6 +141,7 @@ public abstract class MouseBehavior extends Behavior if (c != null) { c.addMouseListener(this); c.addMouseMotionListener(this); + c.addMouseWheelListener(this); } listener = true; } @@ -162,6 +163,7 @@ public abstract class MouseBehavior extends Behavior if (c != null) { c.addMouseListener(this); c.addMouseMotionListener(this); + c.addMouseWheelListener(this); } listener = true; } @@ -192,11 +194,13 @@ public abstract class MouseBehavior extends Behavior */ public void initialize() { - mouseEvents = new WakeupCriterion[3]; + mouseEvents = new WakeupCriterion[4]; + if (!listener) { mouseEvents[0] = new WakeupOnAWTEvent(MouseEvent.MOUSE_DRAGGED); mouseEvents[1] = new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED); mouseEvents[2] = new WakeupOnAWTEvent(MouseEvent.MOUSE_RELEASED); + mouseEvents[3] = new WakeupOnAWTEvent(MouseEvent.MOUSE_WHEEL); } else { mouseEvents[0] = new WakeupOnBehaviorPost(this, @@ -205,6 +209,8 @@ public abstract class MouseBehavior extends Behavior MouseEvent.MOUSE_PRESSED); mouseEvents[2] = new WakeupOnBehaviorPost(this, MouseEvent.MOUSE_RELEASED); + mouseEvents[3] = new WakeupOnBehaviorPost(this, + MouseEvent.MOUSE_WHEEL); mouseq = new LinkedList(); } mouseCriterion = new WakeupOr(mouseEvents); @@ -240,6 +246,12 @@ public abstract class MouseBehavior extends Behavior else if (evt.getID() == MouseEvent.MOUSE_MOVED) { // Process mouse move event } + else if (evt.getID() == MouseEvent.MOUSE_WHEEL) { + // Process mouse wheel event + } + else {// no default code path authorized. + assert false; + } } /** @@ -248,11 +260,11 @@ public abstract class MouseBehavior extends Behavior public abstract void processStimulus (Enumeration criteria); /** - * Adds this behavior as a MouseListener and MouseMotionListener to + * Adds this behavior as a MouseListener, mouseWheelListener and MouseMotionListener to * the specified component. This method can only be called if * the behavior was created with one of the constructors that takes * a Component as a parameter. - * @param c The component to add the MouseListener and + * @param c The component to add the MouseListener, MouseWheelListener and * MouseMotionListener to. * @exception IllegalStateException if the behavior was not created * as a listener @@ -260,10 +272,11 @@ public abstract class MouseBehavior extends Behavior */ public void addListener(Component c) { if (!listener) { - throw new IllegalStateException(J3dUtilsI18N.getString("Behavior0")); + throw new IllegalStateException(J3dUtilsI18N.getString("Behavior0")); } c.addMouseListener(this); c.addMouseMotionListener(this); + c.addMouseWheelListener(this); } public void mouseClicked(MouseEvent e) {} @@ -324,6 +337,21 @@ public abstract class MouseBehavior extends Behavior mouseq.clear(); } } + + public void mouseWheelMoved(MouseWheelEvent e){ + System.out.println("MouseBehavior : mouseWheel enable = " + enable ); + + // add new event to the to the queue + // must be MT safe. + if (enable) { + synchronized (mouseq) { + mouseq.add(e); + // only need to post if this is the only event in the queue + if (mouseq.size() == 1) + postId(MouseEvent.MOUSE_WHEEL); + } + } + } } diff --git a/src/classes/share/com/sun/j3d/utils/behaviors/mouse/MouseWheelZoom.java b/src/classes/share/com/sun/j3d/utils/behaviors/mouse/MouseWheelZoom.java new file mode 100644 index 0000000..0676a92 --- /dev/null +++ b/src/classes/share/com/sun/j3d/utils/behaviors/mouse/MouseWheelZoom.java @@ -0,0 +1,254 @@ +/* + * $RCSfile$ + * + * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any + * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND + * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY + * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL + * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF + * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS + * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR + * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, + * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND + * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR + * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed, licensed or + * intended for use in the design, construction, operation or + * maintenance of any nuclear facility. + * + * $Revision$ + * $Date$ + * $State$ + */ + +package com.sun.j3d.utils.behaviors.mouse; + +import java.awt.*; +import java.awt.event.*; +import java.util.*; +import javax.media.j3d.*; +import javax.vecmath.*; + + +/** + * MouseWheelZoom is a Java3D behavior object that lets users control the + * Z axis translation of an object via mouse wheel. + * @since Java 3D 1.3.2 + */ + +public class MouseWheelZoom extends MouseBehavior { + + double z_factor = .1; + Vector3d translation = new Vector3d(); + + private MouseBehaviorCallback callback = null; + + /** + * Creates a zoom behavior given the transform group. + * @param transformGroup The transformGroup to operate on. + */ + public MouseWheelZoom(TransformGroup transformGroup) { + super(transformGroup); + } + + /** + * Creates a default mouse zoom behavior. + **/ + public MouseWheelZoom() { + super(0); + } + + /** + * Creates a zoom 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 MouseWheelZoom(int flags) { + super(flags); + } + + /** + * Creates a zoom 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.3.2 + */ + public MouseWheelZoom(Component c) { + super(c, 0); + } + + /** + * Creates a zoom 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. + * @param c The Component to add the MouseListener and + * MouseMotionListener to. 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 transformGroup The TransformGroup to operate on. + * @since Java 3D 1.3.2 + */ + public MouseWheelZoom(Component c, TransformGroup transformGroup) { + super(c, transformGroup); + } + + /** + * Creates a zoom 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.3.2 + */ + public MouseWheelZoom(Component c, int flags) { + super(c, flags); + } + + public void initialize() { + super.initialize(); + if ((flags & INVERT_INPUT) == INVERT_INPUT) { + z_factor *= -1; + invert = true; + } + } + + /** + * Return the y-axis movement multipler. + **/ + public double getFactor() { + return z_factor; + } + + /** + * Set the wheel units movement multipler with factor. + **/ + public void setFactor( double factor) { + z_factor = factor; + } + + + public void processStimulus(Enumeration criteria) { + WakeupCriterion wakeup; + AWTEvent[] events; + MouseEvent evt; + + 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) { + synchronized (mouseq) { + if (mouseq.isEmpty()) break; + evt = (MouseEvent)mouseq.remove(0); + // consolidate MOUSE_WHEEL events + while((evt.getID() == MouseEvent.MOUSE_WHEEL) && + !mouseq.isEmpty() && + (((MouseEvent)mouseq.get(0)).getID() == + MouseEvent.MOUSE_WHEEL)) { + evt = (MouseEvent)mouseq.remove(0); + } + } + doProcess(evt); + } + } + + } + wakeupOn(mouseCriterion); + } + + void doProcess(MouseEvent evt) { + int units = 0; + + processMouseEvent(evt); + + if ((evt.getID() == MouseEvent.MOUSE_WHEEL)) { + MouseWheelEvent wheelEvent = (MouseWheelEvent)evt; + if (wheelEvent.getScrollType() == wheelEvent.WHEEL_UNIT_SCROLL ) { + units = wheelEvent.getUnitsToScroll(); + } + + if (!reset) { + transformGroup.getTransform(currXform); + + translation.z = units*z_factor; + + transformX.set(translation); + + if (invert) { + currXform.mul(currXform, transformX); + } else { + currXform.mul(transformX, currXform); + } + + transformGroup.setTransform(currXform); + + transformChanged( currXform ); + + if (callback!=null) + callback.transformChanged( MouseBehaviorCallback.ZOOM, + currXform ); + + } + else { + reset = false; + } + } + } + + + /** + * Users can overload this method which is called every time + * the Behavior updates the transform + * + * Default implementation does nothing + */ + public void transformChanged( Transform3D transform ) { + } + + /** + * The transformChanged method in the callback class will + * be called every time the transform is updated + */ + public void setupCallback( MouseBehaviorCallback callback ){ + this.callback = callback; + } +} + |