View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2024.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.ui.behaviors;
39  
40  import java.awt.AWTEvent;
41  import java.awt.event.MouseEvent;
42  import java.util.Iterator;
43  import org.jogamp.java3d.Behavior;
44  import org.jogamp.java3d.Transform3D;
45  import org.jogamp.java3d.TransformGroup;
46  import org.jogamp.java3d.WakeupCriterion;
47  import org.jogamp.java3d.WakeupOnAWTEvent;
48  import org.jogamp.vecmath.Matrix4d;
49  import org.jogamp.vecmath.Vector3d;
50  
51  // import ffe.panels.*;
52  
53  /**
54   * The MouseRotate class implements a mouse rotation behavior.
55   *
56   * @author Michael J. Schnieders
57   */
58  public class MouseRotate extends MouseBehavior {
59  
60    private static final Vector3d zero3d = new Vector3d(0.0, 0.0, 0.0);
61    private static Transform3D VPTG_T3D = new Transform3D();
62    private static Vector3d translation = new Vector3d();
63    private static Matrix4d mat = new Matrix4d();
64    double xAngle, yAngle;
65    double xFactor = 0.001;
66    double yFactor = 0.001;
67    int doneID = 0;
68    private MouseBehaviorCallback callback = null;
69  
70    /**
71     * Constructor for MouseRotate.
72     *
73     * @param flags a int.
74     * @param VPTG a {@link org.jogamp.java3d.TransformGroup} object.
75     */
76    public MouseRotate(int flags, TransformGroup VPTG) {
77      super(flags, VPTG);
78    }
79  
80    /**
81     * Constructor for MouseRotate.
82     *
83     * @param flags a int.
84     * @param VPTG a {@link org.jogamp.java3d.TransformGroup} object.
85     * @param behavior a {@link org.jogamp.java3d.Behavior} object.
86     * @param postID a int.
87     * @param dID a int.
88     */
89    public MouseRotate(int flags, TransformGroup VPTG, Behavior behavior, int postID, int dID) {
90      super(flags, VPTG, behavior, postID);
91      doneID = dID;
92    }
93  
94    /**
95     * Return the x-axis movement multipler.
96     *
97     * @return a double.
98     */
99    public double getXFactor() {
100     return xFactor;
101   }
102 
103   /**
104    * Return the y-axis movement multipler.
105    *
106    * @return a double.
107    */
108   public double getYFactor() {
109     return yFactor;
110   }
111 
112   /** initialize */
113   public void initialize() {
114     super.initialize();
115     xAngle = 0;
116     yAngle = 0;
117     if ((flags & INVERT_INPUT) == INVERT_INPUT) {
118       invert = true;
119       xFactor *= -1;
120       yFactor *= -1;
121     }
122   }
123 
124   /** {@inheritDoc} */
125   public void processStimulus(Iterator<WakeupCriterion> criteria) {
126     while (criteria.hasNext()) {
127       WakeupCriterion wakeup = criteria.next();
128       if (wakeup instanceof WakeupOnAWTEvent) {
129         AWTEvent[] event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
130         for (AWTEvent awtEvent : event) {
131           MouseEvent mevent = (MouseEvent) awtEvent;
132           processMouseEvent(mevent);
133           int id = awtEvent.getID();
134           // Drag and Button 1 down
135           if ((id == MouseEvent.MOUSE_DRAGGED)
136               && ((mevent.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK)
137                   == MouseEvent.BUTTON1_DOWN_MASK)
138               && transformGroup != null) {
139             x = ((MouseEvent) awtEvent).getX();
140             y = ((MouseEvent) awtEvent).getY();
141             int dx = x - xLast;
142             int dy = y - yLast;
143             if (!reset) {
144               xAngle = dy * yFactor;
145               yAngle = dx * xFactor;
146               transformX.rotX(xAngle);
147               transformY.rotY(yAngle);
148               transformGroup.getTransform(currXform);
149               currXform.get(mat);
150               currXform.setTranslation(zero3d);
151               if (ViewerTG != null) {
152                 ViewerTG.getTransform(VPTG_T3D);
153                 VPTG_T3D.setTranslation(zero3d);
154                 VPTG_T3D.invert();
155                 currXform.mul(VPTG_T3D, currXform);
156               }
157               if (invert) {
158                 currXform.mul(currXform, transformX);
159                 currXform.mul(currXform, transformY);
160               } else {
161                 currXform.mul(transformX, currXform);
162                 currXform.mul(transformY, currXform);
163               }
164               if (ViewerTG != null) {
165                 VPTG_T3D.invert();
166                 currXform.mul(VPTG_T3D, currXform);
167               }
168               translation.set(mat.m03, mat.m13, mat.m23);
169               currXform.setTranslation(translation);
170               transformGroup.setTransform(currXform);
171               transformChanged(currXform);
172               if (callback != null) {
173                 callback.transformChanged(MouseBehaviorCallback.TRANSLATE, currXform);
174               }
175             } else {
176               reset = false;
177             }
178             xLast = x;
179             yLast = y;
180           } else if (id == MouseEvent.MOUSE_PRESSED) {
181             xLast = ((MouseEvent) awtEvent).getX();
182             yLast = ((MouseEvent) awtEvent).getY();
183           } else if (id == MouseEvent.MOUSE_RELEASED) {
184             setTransformGroup(null);
185           }
186         }
187       }
188     }
189     if (transformGroup != null || postCriterion == null) {
190       wakeupOn(mouseCriterion);
191     } else {
192       postId(doneID);
193       wakeupOn(postCriterion);
194     }
195   }
196 
197   /**
198    * Set the x-axis amd y-axis movement multipler with factor.
199    *
200    * @param factor a double.
201    */
202   public void setFactor(double factor) {
203     xFactor = yFactor = factor;
204   }
205 
206   /**
207    * Set the x-axis amd y-axis movement multipler with xFactor and yFactor respectively.
208    *
209    * @param xFactor a double.
210    * @param yFactor a double.
211    */
212   public void setFactor(double xFactor, double yFactor) {
213     this.xFactor = xFactor;
214     this.yFactor = yFactor;
215   }
216 
217   /**
218    * setupCallback
219    *
220    * @param c a {@link ffx.ui.behaviors.MouseBehaviorCallback} object.
221    */
222   public void setupCallback(MouseBehaviorCallback c) {
223     callback = c;
224   }
225 
226   /**
227    * transformChanged
228    *
229    * @param transform a {@link org.jogamp.java3d.Transform3D} object.
230    */
231   public void transformChanged(Transform3D transform) {}
232 }