KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > input > command > navigation > RotateCommand


1 /*
2  * Copyright 2004 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.buchuki.ensmer.input.command.navigation;
18
19 import com.buchuki.ensmer.input.Accelerator;
20 import com.buchuki.ensmer.input.event.EnsmerInputEvent;
21 import com.buchuki.ensmer.input.event.Measurable;
22 import javax.media.j3d.Transform3D;
23 import javax.vecmath.*;
24
25 /**
26  *
27  * @author Dusty Phillips [dusty@buchuki.com]
28  */

29 public class RotateCommand extends TransformCommand {
30     
31     /**
32      * Creates a new MoveCommand with a reference to the transformable,
33      * accelerator, and modifier.
34      *
35      * @param transformable the Transformable to notify whenever the position
36      * is updated
37      * @param accel the Accelerator used to dynamically modify the magnitude
38      * @param accelMod a value to statically modify the magnitude (for hardware
39      * correction)
40      * @param axis a member of the Axis enumeration that specifies which axis
41      * to move in
42      */

43     public RotateCommand(Transformable3D transformable, Accelerator accel,
44     float accelMod, Axis axis) {
45         super(transformable, accel, accelMod);
46         this.axis = axis;
47     }
48     
49     /**
50      * Execute the command; in this case, create a movement vector on the
51      * axis the movement was constructed with, and move the object
52      */

53     public boolean execute(EnsmerInputEvent event) {
54         float rotateAmount = getAcceleratedMagnitude((Measurable) event);
55         performRotation(rotateAmount);
56         return true;
57     }
58     
59     /**
60      * Undo the rotation about that particular axis to return it to the default
61      * orientation.
62      */

63     public void undoRotation() {
64         performRotation(-1.0f * totalRotation);
65     }
66     
67     /**
68      * Method to transform the rotation matrix. Subclasses should override
69      this if they
70      * need to apply extra transformations (specifically, aligning it with the
71      * view) to the matrix. The default implementation is empty.
72      *
73      * @param matrix the Matrix to be transformed
74      */

75     protected void transformRotation(Matrix4f matrix) {
76     }
77     
78     /**
79      * Utility method to rotate the view by a particular magnitude
80      *
81      * @param magnitude the amount to rotate the view
82      */

83     private void performRotation(float magnitude) {
84         totalRotation += magnitude;
85         Transform3D transform = new Transform3D();
86         axis.rotate(transform, magnitude);
87         Matrix4f matrix = new Matrix4f();
88         transform.get(matrix);
89         transformRotation(matrix);
90         updatePosition(matrix);
91     }
92     
93     /**
94      * The axis associated with this MoveCommand
95      */

96     private Axis axis;
97     
98     /**
99      * The total amount of rotation that has been done on this access. This is
100      * used to undo the rotation.
101      */

102     private float totalRotation;
103     
104     /**
105      * Enumeration of three Axes in three dimensions. Used to simplify
106      * implementation details.
107      */

108     public enum Axis {
109         X,
110         Y,
111         Z;
112         
113         /**
114          * Rotate the given transform about this axis by the given
115          * amount
116          * @param transform the Transform to rotate about
117          * @param magnitude the amount to rotate
118          */

119         void rotate(Transform3D transform, float magnitude) {
120             switch(this) {
121                 case X:
122                     transform.rotX(magnitude);
123                     break;
124                 case Y:
125                     transform.rotY(magnitude);
126                     break;
127                 case Z:
128                     transform.rotZ(magnitude);
129             }
130         }
131     }
132 }
133
Popular Tags