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.Transform3D;
44  import org.jogamp.java3d.TransformGroup;
45  import org.jogamp.java3d.WakeupCriterion;
46  import org.jogamp.java3d.WakeupOnAWTEvent;
47  
48  /**
49   * The MouseSelection class implements a mouse selection behavior.
50   *
51   * @author Michael J. Schnieders
52   */
53  public class MouseSelection extends MouseBehavior {
54  
55    private double xAngle, yAngle;
56    private double xFactor = .03;
57    private double yFactor = .03;
58    private MouseBehaviorCallback callback = null;
59  
60    /**
61     * Constructor for MouseSelection.
62     *
63     * @param flags a int.
64     * @param VPTG a {@link org.jogamp.java3d.TransformGroup} object.
65     */
66    public MouseSelection(int flags, TransformGroup VPTG) {
67      super(flags, VPTG);
68    }
69  
70    /**
71     * Return the x-axis movement multipler.
72     *
73     * <p>getXFactor
74     *
75     * @return a double.
76     */
77    public double getXFactor() {
78      return xFactor;
79    }
80  
81    /**
82     * Return the y-axis movement multipler.
83     *
84     * <p>getYFactor
85     *
86     * @return a double.
87     */
88    public double getYFactor() {
89      return yFactor;
90    }
91  
92    /** initialize */
93    public void initialize() {
94      super.initialize();
95      xAngle = 0;
96      yAngle = 0;
97      if ((flags & INVERT_INPUT) == INVERT_INPUT) {
98        invert = true;
99        xFactor *= -1;
100       yFactor *= -1;
101     }
102   }
103 
104   /** {@inheritDoc} */
105   public void processStimulus(Iterator<WakeupCriterion> criteria) {
106     while (criteria.hasNext()) {
107       WakeupCriterion wakeup = criteria.next();
108       if (wakeup instanceof WakeupOnAWTEvent) {
109         AWTEvent[] event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
110         for (AWTEvent awtEvent : event) {
111           processMouseEvent((MouseEvent) awtEvent);
112           if (((buttonPress) && ((flags & MANUAL_WAKEUP) == 0))
113               || ((wakeUp) && ((flags & MANUAL_WAKEUP) != 0))) {
114             int id = awtEvent.getID();
115             if ((id == MouseEvent.MOUSE_DRAGGED)) {
116               if (!reset) {
117                 transformChanged(currXform);
118                 if (callback != null) {
119                   callback.transformChanged(MouseBehaviorCallback.SELECTION, currXform);
120                 }
121               } else {
122                 reset = false;
123               }
124               xLast = ((MouseEvent) awtEvent).getX();
125               yLast = ((MouseEvent) awtEvent).getY();
126             }
127             if (id == MouseEvent.MOUSE_PRESSED) {
128               xLast = ((MouseEvent) awtEvent).getX();
129               yLast = ((MouseEvent) awtEvent).getY();
130             }
131           }
132         }
133       }
134     }
135     wakeupOn(mouseCriterion);
136   }
137 
138   /**
139    * Set the x-axis amd y-axis movement multipler with factor.
140    *
141    * <p>setFactor
142    *
143    * @param factor a double.
144    */
145   public void setFactor(double factor) {
146     xFactor = yFactor = factor;
147   }
148 
149   /**
150    * Set the x-axis amd y-axis movement multipler with xFactor and yFactor respectively.
151    *
152    * <p>setFactor
153    *
154    * @param xFactor a double.
155    * @param yFactor a double.
156    */
157   public void setFactor(double xFactor, double yFactor) {
158     this.xFactor = xFactor;
159     this.yFactor = yFactor;
160   }
161 
162   /**
163    * The transformChanged method in the callback class will be called every time the transform is
164    * updated
165    *
166    * <p>setupCallback
167    *
168    * @param c a {@link ffx.ui.behaviors.MouseBehaviorCallback} object.
169    */
170   public void setupCallback(MouseBehaviorCallback c) {
171     callback = c;
172   }
173 
174   /**
175    * transformChanged
176    *
177    * @param transform a {@link org.jogamp.java3d.Transform3D} object.
178    */
179   public void transformChanged(Transform3D transform) {}
180 }