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  
49  /**
50   * The MouseZoom class implements a Mouse Zoom behavior.
51   *
52   * @author Michael J. Schnieders
53   */
54  public class MouseZoom extends MouseBehavior {
55  
56    private double zFactor = 0.0002;
57    private MouseBehaviorCallback callback = null;
58    private int mouseButton = MouseEvent.BUTTON2_DOWN_MASK;
59    private int doneID = 0;
60  
61    /**
62     * Constructor for MouseZoom.
63     *
64     * @param flags a int.
65     * @param VPTG a {@link org.jogamp.java3d.TransformGroup} object.
66     */
67    public MouseZoom(int flags, TransformGroup VPTG) {
68      super(flags, VPTG);
69    }
70  
71    /**
72     * Constructor for MouseZoom.
73     *
74     * @param flags a int.
75     * @param VPTG a {@link org.jogamp.java3d.TransformGroup} object.
76     * @param behavior a {@link org.jogamp.java3d.Behavior} object.
77     * @param postID a int.
78     * @param dID a int.
79     */
80    public MouseZoom(int flags, TransformGroup VPTG, Behavior behavior, int postID, int dID) {
81      super(flags, VPTG, behavior, postID);
82      doneID = dID;
83    }
84  
85    /**
86     * Return the y-axis movement multipler.
87     *
88     * <p>getFactor
89     *
90     * @return a double.
91     */
92    public double getFactor() {
93      return zFactor;
94    }
95  
96    /**
97     * Set the y-axis movement multipler with factor.
98     *
99     * <p>setFactor
100    *
101    * @param factor a double.
102    */
103   public void setFactor(double factor) {
104     zFactor = factor;
105   }
106 
107   /** initialize */
108   public void initialize() {
109     super.initialize();
110     if ((flags & INVERT_INPUT) == INVERT_INPUT) {
111       zFactor *= -1;
112       invert = true;
113     }
114   }
115 
116   /** {@inheritDoc} */
117   public void processStimulus(Iterator<WakeupCriterion> criteria) {
118     boolean done = false;
119     while (criteria.hasNext()) {
120       WakeupCriterion wakeup = criteria.next();
121       if (wakeup instanceof WakeupOnAWTEvent) {
122         AWTEvent[] event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
123         for (AWTEvent awtEvent : event) {
124           processMouseEvent((MouseEvent) awtEvent);
125           int id = awtEvent.getID();
126           MouseEvent mevent = (MouseEvent) awtEvent;
127           int mod = mevent.getModifiersEx();
128           boolean middleButton = ((mod & mouseButton) == mouseButton);
129           if (!middleButton) {
130             middleButton = ((mod & MouseEvent.ALT_DOWN_MASK) == MouseEvent.ALT_DOWN_MASK);
131           }
132           if ((id == MouseEvent.MOUSE_DRAGGED) && middleButton) {
133             y = ((MouseEvent) awtEvent).getY();
134             int dy = y - yLast;
135             if (!reset) {
136               transformGroup.getTransform(currXform);
137               double z = (-1.0) * dy * zFactor;
138               double scale = currXform.getScale() + z;
139               if (scale > 0) {
140                 currXform.setScale(scale);
141                 transformGroup.setTransform(currXform);
142                 transformChanged(currXform);
143               }
144               if (callback != null) {
145                 callback.transformChanged(MouseBehaviorCallback.ZOOM, currXform);
146               }
147             } else {
148               reset = false;
149             }
150             xLast = x;
151             yLast = y;
152           }
153           if (id == MouseEvent.MOUSE_PRESSED) {
154             xLast = ((MouseEvent) awtEvent).getX();
155             yLast = ((MouseEvent) awtEvent).getY();
156           } else if (id == MouseEvent.MOUSE_RELEASED) {
157             done = true;
158           }
159         }
160       }
161     }
162     if (!done) {
163       wakeupOn(mouseCriterion);
164     } else {
165       reset = true;
166       mouseButton = MouseEvent.BUTTON2_DOWN_MASK;
167       postId(doneID);
168       wakeupOn(postCriterion);
169     }
170   }
171 
172   /**
173    * Setter for the field <code>mouseButton</code>.
174    *
175    * @param button a int.
176    */
177   public void setMouseButton(int button) {
178     mouseButton = button;
179   }
180 
181   /**
182    * The transformChanged method in the callback class will be called every time the transform is
183    * updated.
184    *
185    * <p>setupCallback
186    *
187    * @param c a {@link ffx.ui.behaviors.MouseBehaviorCallback} object.
188    */
189   public void setupCallback(MouseBehaviorCallback c) {
190     callback = c;
191   }
192 
193   /**
194    * transformChanged
195    *
196    * @param transform a {@link org.jogamp.java3d.Transform3D} object.
197    */
198   public void transformChanged(Transform3D transform) {}
199 }