]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/wheel/RotatorHandler.java
Removed javax.vecmath from target definitions.
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / elementclass / wheel / RotatorHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.elementclass.wheel;
13
14 import java.awt.geom.Point2D;
15 import java.awt.geom.Rectangle2D;
16
17 import javax.vecmath.Vector2d;
18 import javax.vecmath.Vector3d;
19
20 import org.simantics.g2d.canvas.ICanvasContext;
21 import org.simantics.g2d.diagram.IDiagram;
22 import org.simantics.g2d.element.ElementHints;
23 import org.simantics.g2d.element.IElement;
24 import org.simantics.g2d.element.handler.InternalSize;
25 import org.simantics.g2d.element.handler.Stateful;
26 import org.simantics.g2d.element.handler.HandleMouseEvent;
27 import org.simantics.g2d.element.handler.Heartbeat;
28 import org.simantics.g2d.element.handler.LifeCycle;
29 import org.simantics.g2d.element.handler.Rotate;
30 import org.simantics.g2d.element.handler.impl.AbstractGrabbable;
31 import org.simantics.utils.datastructures.hints.IHintContext.Key;
32 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
33
34 /**
35  * This handlerer rotates element when it is touched.
36  * 
37  * Hints used:
38  *    KEY_VALUE
39  *    KEY_MIN_VALUE                     optional
40  *    KEY_MAX_VALUE                     optional
41  *    KEY_FRICTION
42  *    KEY_FACTOR
43  *    KEY_GRAB_FRICTION
44  *    KEY_MIN_POINTERS
45  *    KEY_LOCKED
46  * 
47  * @author Toni Kalajainen
48  */
49 public class RotatorHandler extends AbstractGrabbable implements HandleMouseEvent, LifeCycle, Heartbeat {
50
51         private static final long serialVersionUID = -3588513675880482627L;
52
53         public static final RotatorHandler INSTANCE = new RotatorHandler(); 
54         
55         public static final double STRAY_DISTANCE = 1000;       
56         
57         public double initial_friction = 0.20;
58         public double initial_grab_friction = 0.99;
59         public double initial_factor = 0.025;
60         // When ang vel goes under tolerance, the motion is considered stopped
61         public double angVelTolerance = 0.5; 
62         
63         /** Angular velocity, canvas specific variable */
64         public static final Key KEY_ANG_VEL = new KeyOf(Double.class);
65         /** Minimum number of pointers */
66         public static final Key KEY_MIN_POINTERS = new KeyOf(Integer.class);    
67         public static final Key KEY_GRAB_FRICTION = new KeyOf(Double.class);    
68         public static final Key KEY_FRICTION = new KeyOf(Double.class);
69         public static final Key KEY_FACTOR = new KeyOf(Double.class);
70
71         public RotatorHandler() {
72                 super(1000.0);
73         }
74
75         @Override
76         protected void onDrag(GrabInfo gi, ICanvasContext ctx) {
77                 IElement        e       = gi.e;
78                 Point2D origo   = getOrigo(e, null);            
79                 Point2D prevPos = gi.prevPosElement;
80                 Point2D p               = gi.dragPosElement;
81                 
82                 // ---- Wheel is held! ----             
83                 // position vector 0
84                 Vector2d p0 = new Vector2d(prevPos.getX(), prevPos.getY());
85                 // position vector 1
86                 Vector2d p1 = new Vector2d(p.getX(), p.getY());
87                 // motion vector
88                 Vector2d f = new Vector2d(p1);
89                 f.sub(p0);
90                 // no movement
91                 if (f.length()==0) return;
92                 
93                 // -- We are holding the wheel and we have moved the pointer --
94
95                 // vector from origo to mouse
96                 Vector2d odp0 = new Vector2d(p0.x - origo.getX(), p0.y - origo.getY());
97                 Vector2d odp1 = new Vector2d(p1.x - origo.getX(), p1.y - origo.getY());
98                 // convert motion into tangential and normal vectors            
99                 // normal vector of the motion
100                 Vector2d fn = new Vector2d(odp0);
101                 fn.scale( f.dot(odp0) / odp0.lengthSquared() );
102                 // tangential vector of the motion
103                 Vector2d ft = new Vector2d(f);
104                 ft.sub(fn);
105                 
106                 // momentum             
107                 Vector3d r = new Vector3d(odp0.x, odp0.y, 0);
108                 Vector3d F = new Vector3d(ft.x, ft.y, 0);
109                 Vector3d M = new Vector3d();
110                 M.cross(r, F);
111                 if (!isGrabbed(e, ctx)) return;
112                 
113                 // Turn the wheel
114                 double deltaAngle = odp0.angle(odp1);
115                 if (M.z<0) deltaAngle *= -1;
116                 
117                 double deltaAngularVelocity = deltaAngle * 20;
118                 setAngVel(e, getAngVel(e)+deltaAngularVelocity);
119
120                 deltaAngle *= 0.297;
121                 modifyValue(e, ctx, deltaAngle);                                
122         }
123
124         @Override
125         protected void onGrab(GrabInfo gi, ICanvasContext ctx) {
126         }
127
128         @Override
129         protected void onGrabCancel(GrabInfo gi, ICanvasContext ctx) {
130         }
131
132         @Override
133         protected boolean onGrabCheck(IElement e, ICanvasContext ctx, int pointerId, Point2D pickPos) {
134                 return isEnabled(e);
135         }
136
137         @Override
138         protected void onRelease(GrabInfo gi, ICanvasContext ctx) {
139         }
140
141         @Override
142         public void heartbeat(IElement e, long time, long deltaTime, ICanvasContext ctx) {
143                 
144         boolean isGrabbed = isGrabbed(e, ctx);
145         boolean isMoving = isMoving(e);
146         boolean isLocked = isLocked(e);
147                 
148
149         // Check if the value has changed in the value source 
150         if ((!isGrabbed && !isMoving)||isLocked) {
151             setAngVel(e, 0.0);
152             return;
153         }
154                 
155         double angVel = getAngVel(e);
156         //System.out.println(angVel);
157         // not moving
158         if (angVel==0) return;
159         
160         // motion under tolerance
161                 if (Math.abs(angVel)<angVelTolerance) {
162                         setAngVel(e, 0.0);
163                     // Stopped moving, write value
164                     if (!isGrabbed)
165                         ;//fireStoppedMoving();
166                     
167                         return;
168                 }
169                 
170                 modifyValue(e, ctx, deltaTime * angVel * getFactor(e)* 0.01);
171                 double f = (isGrabbed?getGrabFriction(e):getFriction(e));       
172                 double dt = ((double)deltaTime)/1000;
173                 angVel *= Math.pow(1-f, dt);
174                 setAngVel(e, angVel);
175                 ctx.getContentContext().setDirty();             
176         }
177
178         @Override
179         public void onElementActivated(IDiagram d, IElement e) {
180         }
181
182         @Override
183         public void onElementCreated(IElement e) {
184                 e.setHint(KEY_MIN_POINTERS, 1);
185                 e.setHint(ElementHints.KEY_VALUE, 0.0);
186                 e.setHint(KEY_FRICTION, initial_friction);
187                 e.setHint(KEY_FACTOR, initial_factor);
188                 e.setHint(KEY_GRAB_FRICTION, initial_grab_friction);
189                 e.setHint(KEY_ANG_VEL, 0.0);
190         }
191
192         @Override
193         public void onElementDeactivated(IDiagram d, IElement e) {
194         }
195
196         @Override
197         public void onElementDestroyed(IElement e) {
198         }
199
200
201     private double getFriction(IElement e) {
202         return e.getHint(KEY_FRICTION); 
203     }
204     
205     private double getGrabFriction(IElement e) {
206         return e.getHint(KEY_GRAB_FRICTION);
207     }
208     
209     private double getFactor(IElement e) {
210         return e.getHint(KEY_FACTOR);
211     }    
212     
213         private double getValue(IElement e) {
214                 return e.getHint(ElementHints.KEY_VALUE);
215         }
216         
217         private void setValue(IElement e, ICanvasContext ctx, double value) {
218                 Double min = e.getHint(ElementHints.KEY_MIN_VALUE);
219                 Double max = e.getHint(ElementHints.KEY_MAX_VALUE);
220                 if (min!=null && value<min) value = min;
221                 if (max!=null && value>max) value = max;
222                 e.setHint(ElementHints.KEY_VALUE, value);
223                 
224                 Point2D origo = getOrigo(e, null);
225                 
226                 Rotate r = e.getElementClass().getSingleItem(Rotate.class);
227                 double angle = r.getAngle(e);
228                 ///System.out.println(angle);
229                 double targetAngle = Math.IEEEremainder(value, Math.PI*2);
230                 double diffrence = targetAngle - angle;
231                 r.rotate(e, diffrence, origo);
232         }
233         
234         public Point2D getOrigo(IElement e, Point2D origo)
235         {
236                 Rectangle2D size = new Rectangle2D.Double();
237                 InternalSize b = e.getElementClass().getSingleItem(InternalSize.class);
238                 b.getBounds(e, size);
239                 if (origo==null) origo = new Point2D.Double(size.getCenterX(),size.getCenterY());
240                 return origo;
241         }
242         
243         public synchronized void modifyValue(IElement e, ICanvasContext ctx, double modification) {
244                 Double                          position = getValue(e);
245                 double value = position + modification;
246                 setValue(e, ctx, value);
247         }
248         
249         
250         public int getMinPointers(IElement e) {
251                 Integer i = e.getHint(KEY_MIN_POINTERS);
252                 if (i==null) return 1;
253                 return i;
254         }
255         
256         public double getAngVel(IElement e)
257         {
258                 Double d = e.getHint(KEY_ANG_VEL);
259                 if (d==null) return 0.0;
260                 return d;
261         }
262         
263         public void setAngVel(IElement e, double value)
264         {
265                 e.setHint(KEY_ANG_VEL, value);          
266         }
267         
268         public boolean isGrabbed(IElement e, ICanvasContext ctx) {
269             return (getGrabCount(e, ctx)>=getMinPointers(e)) || !isEnabled(e);      
270         }
271         
272         public boolean isMoving(IElement e) {
273             return getAngVel(e)!=0;
274         }
275         
276         public boolean isLocked(IElement e) {
277                 Boolean b = e.getHint(ElementHints.KEY_LOCKED);
278                 return b==null?false:b;
279         }
280         
281         public boolean isEnabled(IElement e) {
282                 Stateful enabled = e.getElementClass().getAtMostOneItemOfClass(Stateful.class);
283                 if (enabled==null) return true;
284                 return enabled.isEnabled(e);
285         }
286         
287         public boolean isMoveable(IElement e) {
288                 return true;
289         }               
290
291         
292         
293 }