001         package com.croftsoft.apps.tag3d;
002    
003         import java.awt.event.KeyEvent;
004         import java.awt.event.KeyListener;
005    
006         import javax.media.j3d.Transform3D;
007         import javax.media.j3d.TransformGroup;
008    
009         import com.croftsoft.core.media.j3d.Transform3DLib;
010         import com.croftsoft.core.media.j3d.Transform3DState;
011         import com.croftsoft.core.util.state.StateMulticaster;
012    
013         /*********************************************************************
014         * Handles keyboard events from the Canvas3D.
015         *
016         * @author
017         *   <A HREF="http://www.alumni.caltech.edu/~croft/">David W. Croft</A>
018         * @version
019         *   1999-02-07
020         *********************************************************************/
021    
022         public class  Tag3DKeyListener implements KeyListener
023         //////////////////////////////////////////////////////////////////////
024         //////////////////////////////////////////////////////////////////////
025         {
026    
027         protected double            deltaRotation;
028         protected double            deltaTranslation;
029         protected String            id;
030         protected StateMulticaster  stateMulticaster;
031         protected TransformGroup    viewTransformGroup;
032    
033         //////////////////////////////////////////////////////////////////////
034         //////////////////////////////////////////////////////////////////////
035    
036         /*********************************************************************
037         * How keyboard input view effects and where the updates are broadcast.
038         *
039         * @param  deltaRotation
040         *   The number of radians to rotate the view upon a keyboard input.
041         * @param  deltaTranslation
042         *   The distance to translate the view upon a keyboard input.
043         * @param  id
044         *   The unique identifier for the object controlled by the keyboard.
045         * @param  stateMulticaster
046         *   Destination for Transform3DState updates for this view.
047         * @param  viewTransformGroup
048         *   Contains the current view transform for the object controlled.
049         *********************************************************************/
050         public  Tag3DKeyListener (
051           double            deltaRotation,
052           double            deltaTranslation,
053           String            id,
054           StateMulticaster  stateMulticaster,
055           TransformGroup    viewTransformGroup )
056         //////////////////////////////////////////////////////////////////////
057         {
058           this.deltaRotation      = deltaRotation;
059           this.deltaTranslation   = deltaTranslation;
060           this.id                 = id;
061           this.stateMulticaster   = stateMulticaster;
062           this.viewTransformGroup = viewTransformGroup;
063         }
064    
065         //////////////////////////////////////////////////////////////////////
066         // KeyListener interface methods
067         //////////////////////////////////////////////////////////////////////
068    
069         /*********************************************************************
070         * Interprets view transform inputs and broadcasts the updates.
071         *
072         * @param  keyEvent
073         *   Arrow keys in combination with no other key, the Shift key, or
074         *   the Alt key will rotate or translate the transform about the
075         *   relative X, Y, and Z axes respectively for 6 degrees of freedom.
076         * @return
077         *   Sends a Transform3DState update to the StateMulticaster if needed.
078         *********************************************************************/
079         public synchronized void  keyPressed ( KeyEvent  keyEvent )
080         //////////////////////////////////////////////////////////////////////
081         {
082           Transform3D  transform3D = new Transform3D ( );
083           viewTransformGroup.getTransform ( transform3D );
084    
085           if ( Transform3DLib.viewKeyPressed (
086             transform3D, keyEvent, deltaRotation, deltaTranslation ) )
087           {
088             stateMulticaster.update (
089               new Transform3DState ( id, transform3D ) );
090           }
091         }
092    
093         public void  keyReleased ( KeyEvent  keyEvent ) { }
094         public void  keyTyped    ( KeyEvent  keyEvent ) { }
095    
096         //////////////////////////////////////////////////////////////////////
097         //////////////////////////////////////////////////////////////////////
098         }