/*
 *      @(#)PickZoomBehavior.java 1.1 99/04/13 15:39:26
 *
 * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.*/
import com.sun.j3d.utils.behaviors.mouse.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.media.j3d.*;
import javax.vecmath.*;


/**
 * A mouse behavior that allows user to pick and zoom scene graph objects.
 * Common usage: 1. Create your scene graph. 2. Create this behavior with
 * the root and canvas.
 */

public class PickZoomBehavior extends PickMouseBehavior implements com.sun.j3d.utils.behaviors.mouse.MouseBehaviorCallback {
  MouseZoom zoom;
  private PickingCallback callback = null;
  private TransformGroup currentTG;

  /**
   * Creates a pick/zoom behavior that waits for user mouse events for
   * the scene graph. This constructor initializes the  pickMode to 
   * BOUNDS picking. 
   * @param root   Root of your scene graph.
   * @param canvas Java 3D drawing canvas.
   * @param bounds Bounds of your scene.
   **/

  public PickZoomBehavior(BranchGroup root, Canvas3D canvas, Bounds bounds){
    super(canvas, root, bounds);
    zoom = new MouseZoom(MouseBehavior.MANUAL_WAKEUP);
    zoom.setTransformGroup(currGrp);
    currGrp.addChild(zoom);
    zoom.setSchedulingBounds(bounds);
    this.setSchedulingBounds(bounds);
  }

  /**
   * Creates a pick/zoom behavior that waits for user mouse events for
   * the scene graph. This constructor sets the pick mode.
   * @param root   Root of your scene graph.
   * @param canvas Java 3D drawing canvas.
   * @param bounds Bounds of your scene.
   * @param pickMode specifies PickObject.USE_BOUNDS or PickObject.USE_GEOMETRY.
   * Note: If pickMode is set to PickObject.USE_GEOMETRY, all the geometry 
   * objects in the scene graph that allow picking must have the 
   * ALLOW_INTERSECT bit set. 
   */
  public PickZoomBehavior(BranchGroup root, Canvas3D canvas, Bounds bounds,
                          int pickMode){
    super(canvas, root, bounds);
    zoom = new MouseZoom(MouseBehavior.MANUAL_WAKEUP);
    zoom.setTransformGroup(currGrp);
    currGrp.addChild(zoom);
    zoom.setSchedulingBounds(bounds);
    this.setSchedulingBounds(bounds);
    pickScene.setPickMode(pickMode);  
  }
  
  /**
   * Update the scene to manipulate any nodes. This is not meant to be 
   * called by users. Behavior automatically calls this. You can call 
   * this only if you know what you are doing.
   * 
   * @param xpos Current mouse X pos.
   * @param ypos Current mouse Y pos.
   **/

  public void updateScene(int xpos, int ypos){
    TransformGroup tg = null;
      
    if (mevent.isAltDown() && !mevent.isMetaDown()){
           
      tg =(TransformGroup)pickScene.pickNode(pickScene.pickClosest(xpos, ypos),
                                             PickObject.TRANSFORM_GROUP);
      
      // Check for valid selection
      if ((tg != null) && 
          (tg.getCapability(TransformGroup.ALLOW_TRANSFORM_READ)) && 
          (tg.getCapability(TransformGroup.ALLOW_TRANSFORM_WRITE))){
        zoom.setTransformGroup(tg);
        zoom.wakeup();
        currentTG = tg;
      } else if (callback!=null)
          callback.transformChanged( PickingCallback.NO_PICK, null );
    }
  }

  /**
    * Callback method from MouseZoom
    * This is used when the Picking callback is enabled
    */
  public void transformChanged( int type, Transform3D transform ) {
      callback.transformChanged( PickingCallback.ZOOM, currentTG );
  }
 
  /**
    * Register the class @param callback to be called each
    * time the picked object moves
    */
  public void setupCallback( PickingCallback callback ) {
      this.callback = callback;
      if (callback==null)
          zoom.setupCallback( null );
      else
          zoom.setupCallback( this );
  }
}

