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