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 static org.apache.commons.math3.util.FastMath.abs;
41  
42  import java.awt.AWTEvent;
43  import java.awt.event.MouseEvent;
44  import java.util.Iterator;
45  import org.jogamp.java3d.Behavior;
46  import org.jogamp.java3d.Transform3D;
47  import org.jogamp.java3d.TransformGroup;
48  import org.jogamp.java3d.WakeupCriterion;
49  import org.jogamp.java3d.WakeupOnAWTEvent;
50  import org.jogamp.vecmath.Vector3d;
51  
52  /**
53   * The MouseTranslate class implements a mouse translate behavior.
54   *
55   * @author Michael J. Schnieders
56   */
57  public class MouseTranslate extends MouseBehavior {
58  
59    private static Vector3d zero3d = new Vector3d(0.0, 0.0, 0.0);
60    private double xFactor = 0.05; // .01;
61    private double yFactor = 0.05; // .01;
62    private Vector3d translation = new Vector3d();
63    private MouseBehaviorCallback callback = null;
64    private int mouseButton = MouseEvent.BUTTON3_DOWN_MASK;
65    private int doneID = 0;
66  
67    /**
68     * Constructor for MouseTranslate.
69     *
70     * @param flags a int.
71     * @param VPTG a {@link org.jogamp.java3d.TransformGroup} object.
72     */
73    public MouseTranslate(int flags, TransformGroup VPTG) {
74      super(flags, VPTG);
75    }
76  
77    /**
78     * Constructor for MouseTranslate.
79     *
80     * @param flags a int.
81     * @param VPTG a {@link org.jogamp.java3d.TransformGroup} object.
82     * @param behavior a {@link org.jogamp.java3d.Behavior} object.
83     * @param postID a int.
84     * @param dID a int.
85     */
86    public MouseTranslate(int flags, TransformGroup VPTG, Behavior behavior, int postID, int dID) {
87      super(flags, VPTG, behavior, postID);
88      doneID = dID;
89    }
90  
91    /**
92     * Return the x-axis movement multipler.
93     *
94     * <p>getXFactor
95     *
96     * @return a double.
97     */
98    public double getXFactor() {
99      return xFactor;
100   }
101 
102   /**
103    * Return the y-axis movement multipler.
104    *
105    * <p>getYFactor
106    *
107    * @return a double.
108    */
109   public double getYFactor() {
110     return yFactor;
111   }
112 
113   /** initialize */
114   public void initialize() {
115     super.initialize();
116     if ((flags & INVERT_INPUT) == INVERT_INPUT) {
117       invert = true;
118       xFactor *= -1;
119       yFactor *= -1;
120     }
121   }
122 
123   /** {@inheritDoc} */
124   public void processStimulus(Iterator<WakeupCriterion> criteria) {
125     while (criteria.hasNext()) {
126       WakeupCriterion wakeup = criteria.next();
127       if (wakeup instanceof WakeupOnAWTEvent) {
128         AWTEvent[] event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
129         for (AWTEvent awtEvent : event) {
130           MouseEvent mevent = (MouseEvent) awtEvent;
131           processMouseEvent(mevent);
132           int id = awtEvent.getID();
133           int mod = mevent.getModifiersEx();
134           boolean rightButton = ((mod & mouseButton) == mouseButton);
135           if (!rightButton) {
136             rightButton = ((mod & MouseEvent.SHIFT_DOWN_MASK) == MouseEvent.SHIFT_DOWN_MASK);
137           }
138           if ((id == MouseEvent.MOUSE_DRAGGED) && rightButton && 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) && ((abs(dy) < 50) && (abs(dx) < 50))) {
144               transformGroup.getTransform(currXform);
145               Transform3D VPTG_T3D = new Transform3D();
146               ViewerTG.getTransform(VPTG_T3D);
147               VPTG_T3D.setTranslation(zero3d);
148               VPTG_T3D.invert();
149               currXform.mul(VPTG_T3D, currXform);
150               translation.x = dx * xFactor;
151               translation.y = -dy * yFactor;
152               transformX.set(translation);
153               if (invert) {
154                 currXform.mul(currXform, transformX);
155               } else {
156                 currXform.mul(transformX, currXform);
157               }
158               VPTG_T3D.invert();
159               currXform.mul(VPTG_T3D, currXform);
160               transformGroup.setTransform(currXform);
161               transformChanged(currXform);
162               if (callback != null) {
163                 callback.transformChanged(MouseBehaviorCallback.TRANSLATE, currXform);
164               }
165             } else {
166               reset = false;
167             }
168             xLast = x;
169             yLast = y;
170           }
171           if (id == MouseEvent.MOUSE_PRESSED) {
172             xLast = ((MouseEvent) awtEvent).getX();
173             yLast = ((MouseEvent) awtEvent).getY();
174           } else if (id == MouseEvent.MOUSE_RELEASED) {
175             setTransformGroup(null);
176           }
177         }
178       }
179     }
180     if (transformGroup != null) {
181       wakeupOn(mouseCriterion);
182     } else {
183       mouseButton = MouseEvent.BUTTON3_DOWN_MASK;
184       postId(doneID);
185       wakeupOn(postCriterion);
186     }
187   }
188 
189   /**
190    * Set the x-axis amd y-axis movement multipler with factor.
191    *
192    * <p>setFactor
193    *
194    * @param factor a double.
195    */
196   public void setFactor(double factor) {
197     xFactor = yFactor = factor;
198   }
199 
200   /**
201    * Set the x-axis amd y-axis movement multipler with xFactor and yFactor respectively.
202    *
203    * <p>setFactor
204    *
205    * @param xFactor a double.
206    * @param yFactor a double.
207    */
208   public void setFactor(double xFactor, double yFactor) {
209     this.xFactor = xFactor;
210     this.yFactor = yFactor;
211   }
212 
213   /**
214    * Setter for the field <code>mouseButton</code>.
215    *
216    * @param button a int.
217    */
218   public void setMouseButton(int button) {
219     mouseButton = button;
220   }
221 
222   /**
223    * The transformChanged method in the callback class will be called every time the transform is
224    * updated.
225    *
226    * <p>setupCallback
227    *
228    * @param c a {@link ffx.ui.behaviors.MouseBehaviorCallback} object.
229    */
230   public void setupCallback(MouseBehaviorCallback c) {
231     callback = c;
232   }
233 
234   /**
235    * transformChanged
236    *
237    * @param transform a {@link org.jogamp.java3d.Transform3D} object.
238    */
239   public void transformChanged(Transform3D transform) {}
240 }