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.event.MouseEvent;
41  import java.util.Iterator;
42  import org.jogamp.java3d.Behavior;
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  import org.jogamp.java3d.WakeupOnBehaviorPost;
48  import org.jogamp.java3d.WakeupOr;
49  
50  /**
51   * The MouseBehavior class is the Base class for all mouse manipulators.
52   *
53   * @author Michael J. Schnieders
54   */
55  public abstract class MouseBehavior extends Behavior {
56    /**
57     * Constant <code>MANUAL_WAKEUP=0x1</code>
58     *
59     * <p>Set this flag if you want to manually wakeup the behavior.
60     */
61    public static final int MANUAL_WAKEUP = 0x1;
62    /**
63     * Constant <code>INVERT_INPUT=0x2</code>
64     *
65     * <p>Set this flag if you want to invert the inputs. This is useful when the transform for the
66     * view platform is being changed instead of the transform for the object.
67     */
68    public static final int INVERT_INPUT = 0x2;
69  
70    protected WakeupCriterion[] mouseEvents;
71    protected WakeupOr mouseCriterion;
72    protected Behavior poster;
73    protected int id;
74    protected WakeupOnBehaviorPost postCriterion;
75    protected int x, y;
76    protected int xLast, yLast;
77    protected TransformGroup transformGroup;
78    protected Transform3D transformX;
79    protected Transform3D transformY;
80    protected Transform3D currXform;
81    protected boolean buttonPress = false;
82    protected boolean reset;
83    protected boolean invert = false;
84    protected boolean wakeUp = false;
85    protected int flags;
86    protected TransformGroup ViewerTG;
87  
88    /**
89     * Constructor for MouseBehavior.
90     *
91     * @param format a int.
92     * @param VPTG a {@link org.jogamp.java3d.TransformGroup} object.
93     */
94    public MouseBehavior(int format, TransformGroup VPTG) {
95      super();
96      flags = format;
97      ViewerTG = VPTG;
98      currXform = new Transform3D();
99      transformX = new Transform3D();
100     transformY = new Transform3D();
101     reset = true;
102   }
103 
104   /**
105    * Constructor for MouseBehavior.
106    *
107    * @param format a int.
108    * @param VPTG a {@link org.jogamp.java3d.TransformGroup} object.
109    * @param b a {@link org.jogamp.java3d.Behavior} object.
110    * @param i a int.
111    */
112   public MouseBehavior(int format, TransformGroup VPTG, Behavior b, int i) {
113     this(format, VPTG);
114     poster = b;
115     id = i;
116   }
117 
118   /** initialize */
119   public void initialize() {
120     mouseEvents = new WakeupCriterion[3];
121     mouseEvents[0] = new WakeupOnAWTEvent(MouseEvent.MOUSE_DRAGGED);
122     mouseEvents[1] = new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);
123     mouseEvents[2] = new WakeupOnAWTEvent(MouseEvent.MOUSE_RELEASED);
124     mouseCriterion = new WakeupOr(mouseEvents);
125     if (poster == null) {
126       wakeupOn(mouseCriterion);
127     } else {
128       postCriterion = new WakeupOnBehaviorPost(poster, id);
129       wakeupOn(postCriterion);
130     }
131     x = 0;
132     y = 0;
133     xLast = 0;
134     yLast = 0;
135   }
136 
137   /**
138    * processMouseEvent
139    *
140    * @param evt a {@link java.awt.event.MouseEvent} object.
141    */
142   public void processMouseEvent(MouseEvent evt) {
143     if (evt.getID() == MouseEvent.MOUSE_PRESSED) {
144       buttonPress = true;
145     } else if (evt.getID() == MouseEvent.MOUSE_RELEASED) {
146       buttonPress = false;
147       wakeUp = false;
148     }
149   }
150 
151   /** {@inheritDoc} */
152   public abstract void processStimulus(Iterator<WakeupCriterion> criteria);
153 
154   /**
155    * Setter for the field <code>transformGroup</code>.
156    *
157    * @param t a {@link org.jogamp.java3d.TransformGroup} object.
158    */
159   public void setTransformGroup(TransformGroup t) {
160     transformGroup = t;
161     currXform = new Transform3D();
162     transformX = new Transform3D();
163     transformY = new Transform3D();
164     reset = true;
165   }
166 
167   /**
168    * Manually wake up the behavior. If MANUAL_WAKEUP flag was set upon creation, you must wake up
169    * this behavior each time it is handled.
170    */
171   public void wakeup() {
172     wakeUp = true;
173   }
174 }