zooming behavior with mouse wheel
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   John_Sonsini
Posted On:   Monday, February 17, 2003 10:05 AM

Does anyone know the correct combination of events/modifiers needed to "capture" the rotation of the mouse wheel as part of a WakeupOr list of mouse events (either MouseEvent or its extension, MouseWheelEvent) to drive a zooming functionality? The class below currently provides two axis rotation by dragging with the left button and zooming by verticaly dragging the wheel button. Thanks in advance. package test3d; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.media.j3d.*; import javax.vecmath.*; public class MouseViewPlatformBehavior extends Behavior { private static final float PI = (float) Math.PI; private Transform   More>>

Does anyone know the correct combination of events/modifiers needed to "capture" the rotation of the mouse wheel as part of a WakeupOr list of mouse events (either MouseEvent or its extension, MouseWheelEvent) to drive a zooming functionality? The class below currently provides two axis rotation by dragging with the left button and zooming by verticaly dragging the wheel button. Thanks in advance.

			
package test3d;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class MouseViewPlatformBehavior extends Behavior {

private static final float PI = (float) Math.PI;

private TransformGroup transformGroup;

private Transform3D transform = new Transform3D ();

private Point3f viewPoint = new Point3f ();

private boolean latitudeRotation, proportionalZooming;

private float theta, phi, radius;

private int x, y, xLast, yLast;

private WakeupCriterion[] mouseEvents;

private WakeupOr mouseCriterion;

//ViewPlatform must be direct child of this TransformGroup
public MouseViewPlatformBehavior (TransformGroup tg, float theta,
float phi, float radius,
boolean latitudeRotation,
boolean proportionalZooming) {

super ();

transformGroup = tg;

transformGroup.getTransform (transform);

transformGroup.setCapability (TransformGroup.ALLOW_TRANSFORM_READ);

transformGroup.setCapability (TransformGroup.ALLOW_TRANSFORM_WRITE);

setLatitudeRotation (latitudeRotation);

setProportionalZooming (proportionalZooming);

initView (theta, phi, radius);

}

public void initialize () {

mouseEvents = new WakeupCriterion[3];

mouseEvents[0] = new WakeupOnAWTEvent (MouseEvent.MOUSE_DRAGGED);

mouseEvents[1] = new WakeupOnAWTEvent (MouseEvent.MOUSE_PRESSED);

mouseEvents[2] = new WakeupOnAWTEvent (MouseWheelEvent.MOUSE_WHEEL);

mouseCriterion = new WakeupOr (mouseEvents);

wakeupOn (mouseCriterion);

}

public void processStimulus (Enumeration criteria) {

WakeupCriterion wakeup;

AWTEvent[] event;

MouseEvent mouseEvent;

int eventId = 0, dx, dy;

while (criteria.hasMoreElements ()) {

wakeup = (WakeupCriterion) criteria.nextElement ();

if (wakeup instanceof WakeupOnAWTEvent) {

event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();

for (int i = 0; i < event.length; i++) {

if (event[i] instanceof MouseEvent) {

mouseEvent = (MouseEvent) event[i];

eventId = mouseEvent.getID ();

if (eventId == MouseEvent.MOUSE_DRAGGED) {

x = mouseEvent.getX ();

y = mouseEvent.getY ();

dx = x - xLast;

dy = y - yLast;

xLast = x;

yLast = y;

if (mouseEvent.getModifiers () ==
MouseEvent.BUTTON1_MASK) {

setRotation (PI * dx / 1000.0f,
PI * dy / 1000.0f);

}

//don't want to press wheel button to activate
//see next comments
else if (mouseEvent.getModifiers () ==
MouseEvent.BUTTON2_MASK) {

setScale (dy / 10.0f);

}

}

else if (eventId == MouseEvent.MOUSE_PRESSED) {

xLast = mouseEvent.getX ();

yLast = mouseEvent.getY ();

}

//first failed attempt
else if (mouseEvent.getModifiers () ==
MouseWheelEvent.MOUSE_WHEEL) {

System.out.println ("wheel event occured");

}

//second failed attempt
else if (mouseEvent.getModifiers () ==
MouseWheelEvent.WHEEL_UNIT_SCROLL) {

System.out.println ("wheel event occured");

}

}

//not recognizing event from WakeupOr
//should call setScale (MouseWheelEvent.getWheelRotation ())
// here
//third failed attempt
else if (event[i] instanceof MouseWheelEvent) {

System.out.println ("wheel event occured");

}

}

}

}

wakeupOn (mouseCriterion);

}

public boolean getLatitudeRotation () {

return latitudeRotation;

}

public void setLatitudeRotation (boolean latitudeRotation) {

this.latitudeRotation = latitudeRotation;

}

public boolean getProportionalZooming () {

return proportionalZooming;

}

public void setProportionalZooming (boolean proportionalZooming) {

this.proportionalZooming = proportionalZooming;

}

private void initView (float theta, float phi, float radius) {

setScale (radius);

setRotation (theta, -phi);

transform.setTranslation (new Vector3f (polarToCartesian (theta, phi,
radius)));

transformGroup.setTransform (transform);

}

private void setRotation (float dTheta, float dPhi) {

if (latitudeRotation && (phi - dPhi < 0 || PI / 2.0f < phi - dPhi)) {

return;

}

theta -= dTheta;

phi -= dPhi;

Vector3f translation = new Vector3f (0.0f, 0.0f, -radius);

Transform3D toRotationPoint = new Transform3D ();

toRotationPoint.setTranslation (translation);

Transform3D rotateX = new Transform3D ();

rotateX.setRotation (new AxisAngle4f (1.0f, 0.0f, 0.0f, dPhi));

Transform3D rotateY = new Transform3D ();

if (latitudeRotation) {

rotateY.setRotation (new AxisAngle4f (0.0f, (float) Math.cos (phi),
(float) Math.sin (phi),
dTheta));

}

else {

rotateY.setRotation (new AxisAngle4f (0.0f, 1.0f, 0.0f, dTheta));

}

translation.negate ();

Transform3D fromRotationPoint = new Transform3D ();

fromRotationPoint.setTranslation (translation);

transform.mul (toRotationPoint);

transform.mul (rotateX);

transform.mul (rotateY);

transform.mul (fromRotationPoint);

transformGroup.setTransform (transform);

updateViewPoint ();

}

private void setScale (float dr) {

if (radius + dr < Float.MIN_VALUE) {

return;

}

Vector3f translation = new Vector3f (0.0f, 0.0f, -radius);

Transform3D toRotationPoint = new Transform3D ();

toRotationPoint.setTranslation (translation);

if (proportionalZooming) {

translation.scale (-1.0f - dr / 10.0f);

radius *= 1.0f + dr / 10.0f;

}

else {

translation.scale (-(radius + dr) / radius);

radius += dr;

}

Transform3D fromRotationPoint = new Transform3D ();

fromRotationPoint.setTranslation (translation);

transform.mul (toRotationPoint);

transform.mul (fromRotationPoint);

transformGroup.setTransform (transform);

updateViewPoint ();

}

private void updateViewPoint () {

viewPoint.set (polarToCartesian (theta, phi, radius));

}

private Point3f polarToCartesian (float theta, float phi, float radius) {

float x0, y0, z0;

x0 = radius * (float) (Math.sin (theta) * Math.cos (phi));

y0 = radius * (float) Math.sin (phi);

z0 = radius * (float) (Math.cos (theta) * Math.cos (phi));

return new Point3f (x0, y0, z0);

}

}
   <<Less
About | Sitemap | Contact