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