1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.elementclass.wheel;
14 import java.awt.geom.Point2D;
15 import java.awt.geom.Rectangle2D;
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;
34 * This handlerer rotates element when it is touched.
38 * KEY_MIN_VALUE optional
39 * KEY_MAX_VALUE optional
46 * @author Toni Kalajainen
48 public class RotatorHandler extends AbstractGrabbable implements HandleMouseEvent, LifeCycle, Heartbeat {
50 private static final long serialVersionUID = -3588513675880482627L;
52 public static final RotatorHandler INSTANCE = new RotatorHandler();
54 public static final double STRAY_DISTANCE = 1000;
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;
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);
70 public RotatorHandler() {
75 protected void onDrag(GrabInfo gi, ICanvasContext ctx) {
77 Point2D origo = getOrigo(e, null);
78 Point2D prevPos = gi.prevPosElement;
79 Point2D p = gi.dragPosElement;
81 // ---- Wheel is held! ----
83 Vector2D p0 = new Vector2D(prevPos.getX(), prevPos.getY());
85 Vector2D p1 = new Vector2D(p.getX(), p.getY());
87 Vector2D f = p1.subtract(p0);
89 if (f.getNormSq()==0) return;
91 // -- We are holding the wheel and we have moved the pointer --
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);
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;
109 double deltaAngle = Vector2D.angle(odp0, odp1);
110 if (M.getZ()<0) deltaAngle *= -1;
112 double deltaAngularVelocity = deltaAngle * 20;
113 setAngVel(e, getAngVel(e)+deltaAngularVelocity);
116 modifyValue(e, ctx, deltaAngle);
120 protected void onGrab(GrabInfo gi, ICanvasContext ctx) {
124 protected void onGrabCancel(GrabInfo gi, ICanvasContext ctx) {
128 protected boolean onGrabCheck(IElement e, ICanvasContext ctx, int pointerId, Point2D pickPos) {
133 protected void onRelease(GrabInfo gi, ICanvasContext ctx) {
137 public void heartbeat(IElement e, long time, long deltaTime, ICanvasContext ctx) {
139 boolean isGrabbed = isGrabbed(e, ctx);
140 boolean isMoving = isMoving(e);
141 boolean isLocked = isLocked(e);
144 // Check if the value has changed in the value source
145 if ((!isGrabbed && !isMoving)||isLocked) {
150 double angVel = getAngVel(e);
151 //System.out.println(angVel);
153 if (angVel==0) return;
155 // motion under tolerance
156 if (Math.abs(angVel)<angVelTolerance) {
158 // Stopped moving, write value
160 ;//fireStoppedMoving();
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();
174 public void onElementActivated(IDiagram d, IElement e) {
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);
188 public void onElementDeactivated(IDiagram d, IElement e) {
192 public void onElementDestroyed(IElement e) {
196 private double getFriction(IElement e) {
197 return e.getHint(KEY_FRICTION);
200 private double getGrabFriction(IElement e) {
201 return e.getHint(KEY_GRAB_FRICTION);
204 private double getFactor(IElement e) {
205 return e.getHint(KEY_FACTOR);
208 private double getValue(IElement e) {
209 return e.getHint(ElementHints.KEY_VALUE);
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);
219 Point2D origo = getOrigo(e, null);
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);
229 public Point2D getOrigo(IElement e, Point2D origo)
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());
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);
245 public int getMinPointers(IElement e) {
246 Integer i = e.getHint(KEY_MIN_POINTERS);
247 if (i==null) return 1;
251 public double getAngVel(IElement e)
253 Double d = e.getHint(KEY_ANG_VEL);
254 if (d==null) return 0.0;
258 public void setAngVel(IElement e, double value)
260 e.setHint(KEY_ANG_VEL, value);
263 public boolean isGrabbed(IElement e, ICanvasContext ctx) {
264 return (getGrabCount(e, ctx)>=getMinPointers(e)) || !isEnabled(e);
267 public boolean isMoving(IElement e) {
268 return getAngVel(e)!=0;
271 public boolean isLocked(IElement e) {
272 Boolean b = e.getHint(ElementHints.KEY_LOCKED);
273 return b==null?false:b;
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);
282 public boolean isMoveable(IElement e) {