package PCG;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.picking.*;
import com.sun.j3d.utils.geometry.*;

public class PickMagnifyBehavior extends com.sun.j3d.utils.picking.behaviors.PickMouseBehavior {

        WakeupCriterion m_oWakeupCriterion;

        /**
         * The instance of <code>TransformGroup</code>, which has the
         * focus right now.
         */
        private TransformGroup m_oFocusedTransformGroup = null;

        /**
         * The "old" scaling-factor of the currently focused instanced of
         * <code>TransformGroup</code>, <emp>before</emp> it has been
         * modified by this class!
         */
        private double m_dOldScaling = 0;

        public PickMagnifyBehavior( BranchGroup oBranchGroup, Canvas3D oCanvas3D,
                BoundingSphere oBoundingSphere ) {

                super( oCanvas3D, oBranchGroup, oBoundingSphere );

                setSchedulingBounds( oBoundingSphere );
                setEnable( true );

                int i = 1;
        }

        public void updateScene( int iXPos, int iYPos ) {
                Shape3D oShape;

                pickCanvas.setShapeLocation( iXPos, iYPos );
                PickResult result = pickCanvas.pickClosest();
                if (result != null) {
                        TransformGroup oTransformGroup =
                                (TransformGroup) result.getNode(
                                PickResult.TRANSFORM_GROUP );

                        Transform3D oTransform3D = null;

                        /* If there is a TransformGroup focused right now, we
                        will "unfocus" it, meaning: set it's scaling to the
                        original value: */
                        if (m_oFocusedTransformGroup != null) {
                                oTransform3D = new Transform3D();
                                m_oFocusedTransformGroup.getTransform( oTransform3D );
                                oTransform3D.setScale( m_dOldScaling );
                                m_oFocusedTransformGroup.setTransform( oTransform3D );
                                m_oFocusedTransformGroup = null;
                        }

                        /* If the mouse has hitten another TransformGroup, we'll
                        "focus" this one. */
                        if (oTransformGroup != null) {

                                oTransform3D = new Transform3D();
                                oTransformGroup.getTransform( oTransform3D );

                                m_oFocusedTransformGroup = oTransformGroup;
                                m_dOldScaling = oTransform3D.getScale();

                                oTransform3D.setScale( m_dOldScaling * 1.3 );
                                oTransformGroup.setTransform( oTransform3D );
                        }
                }
        }
}
