--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.g2d.elementclass.wheel;\r
+\r
+import java.awt.geom.Point2D;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import javax.vecmath.Vector2d;\r
+import javax.vecmath.Vector3d;\r
+\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.element.ElementHints;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.InternalSize;\r
+import org.simantics.g2d.element.handler.Stateful;\r
+import org.simantics.g2d.element.handler.HandleMouseEvent;\r
+import org.simantics.g2d.element.handler.Heartbeat;\r
+import org.simantics.g2d.element.handler.LifeCycle;\r
+import org.simantics.g2d.element.handler.Rotate;\r
+import org.simantics.g2d.element.handler.impl.AbstractGrabbable;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+\r
+/**\r
+ * This handlerer rotates element when it is touched.\r
+ * \r
+ * Hints used:\r
+ * KEY_VALUE\r
+ * KEY_MIN_VALUE optional\r
+ * KEY_MAX_VALUE optional\r
+ * KEY_FRICTION\r
+ * KEY_FACTOR\r
+ * KEY_GRAB_FRICTION\r
+ * KEY_MIN_POINTERS\r
+ * KEY_LOCKED\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class RotatorHandler extends AbstractGrabbable implements HandleMouseEvent, LifeCycle, Heartbeat {\r
+\r
+ private static final long serialVersionUID = -3588513675880482627L;\r
+\r
+ public static final RotatorHandler INSTANCE = new RotatorHandler(); \r
+ \r
+ public static final double STRAY_DISTANCE = 1000; \r
+ \r
+ public double initial_friction = 0.20;\r
+ public double initial_grab_friction = 0.99;\r
+ public double initial_factor = 0.025;\r
+ // When ang vel goes under tolerance, the motion is considered stopped\r
+ public double angVelTolerance = 0.5; \r
+ \r
+ /** Angular velocity, canvas specific variable */\r
+ public static final Key KEY_ANG_VEL = new KeyOf(Double.class);\r
+ /** Minimum number of pointers */\r
+ public static final Key KEY_MIN_POINTERS = new KeyOf(Integer.class); \r
+ public static final Key KEY_GRAB_FRICTION = new KeyOf(Double.class); \r
+ public static final Key KEY_FRICTION = new KeyOf(Double.class);\r
+ public static final Key KEY_FACTOR = new KeyOf(Double.class);\r
+\r
+ public RotatorHandler() {\r
+ super(1000.0);\r
+ }\r
+\r
+ @Override\r
+ protected void onDrag(GrabInfo gi, ICanvasContext ctx) {\r
+ IElement e = gi.e;\r
+ Point2D origo = getOrigo(e, null); \r
+ Point2D prevPos = gi.prevPosElement;\r
+ Point2D p = gi.dragPosElement;\r
+ \r
+ // ---- Wheel is held! ---- \r
+ // position vector 0\r
+ Vector2d p0 = new Vector2d(prevPos.getX(), prevPos.getY());\r
+ // position vector 1\r
+ Vector2d p1 = new Vector2d(p.getX(), p.getY());\r
+ // motion vector\r
+ Vector2d f = new Vector2d(p1);\r
+ f.sub(p0);\r
+ // no movement\r
+ if (f.length()==0) return;\r
+ \r
+ // -- We are holding the wheel and we have moved the pointer --\r
+\r
+ // vector from origo to mouse\r
+ Vector2d odp0 = new Vector2d(p0.x - origo.getX(), p0.y - origo.getY());\r
+ Vector2d odp1 = new Vector2d(p1.x - origo.getX(), p1.y - origo.getY());\r
+ // convert motion into tangential and normal vectors \r
+ // normal vector of the motion\r
+ Vector2d fn = new Vector2d(odp0);\r
+ fn.scale( f.dot(odp0) / odp0.lengthSquared() );\r
+ // tangential vector of the motion\r
+ Vector2d ft = new Vector2d(f);\r
+ ft.sub(fn);\r
+ \r
+ // momentum \r
+ Vector3d r = new Vector3d(odp0.x, odp0.y, 0);\r
+ Vector3d F = new Vector3d(ft.x, ft.y, 0);\r
+ Vector3d M = new Vector3d();\r
+ M.cross(r, F);\r
+ if (!isGrabbed(e, ctx)) return;\r
+ \r
+ // Turn the wheel\r
+ double deltaAngle = odp0.angle(odp1);\r
+ if (M.z<0) deltaAngle *= -1;\r
+ \r
+ double deltaAngularVelocity = deltaAngle * 20;\r
+ setAngVel(e, getAngVel(e)+deltaAngularVelocity);\r
+\r
+ deltaAngle *= 0.297;\r
+ modifyValue(e, ctx, deltaAngle); \r
+ }\r
+\r
+ @Override\r
+ protected void onGrab(GrabInfo gi, ICanvasContext ctx) {\r
+ }\r
+\r
+ @Override\r
+ protected void onGrabCancel(GrabInfo gi, ICanvasContext ctx) {\r
+ }\r
+\r
+ @Override\r
+ protected boolean onGrabCheck(IElement e, ICanvasContext ctx, int pointerId, Point2D pickPos) {\r
+ return isEnabled(e);\r
+ }\r
+\r
+ @Override\r
+ protected void onRelease(GrabInfo gi, ICanvasContext ctx) {\r
+ }\r
+\r
+ @Override\r
+ public void heartbeat(IElement e, long time, long deltaTime, ICanvasContext ctx) {\r
+ \r
+ boolean isGrabbed = isGrabbed(e, ctx);\r
+ boolean isMoving = isMoving(e);\r
+ boolean isLocked = isLocked(e);\r
+ \r
+\r
+ // Check if the value has changed in the value source \r
+ if ((!isGrabbed && !isMoving)||isLocked) {\r
+ setAngVel(e, 0.0);\r
+ return;\r
+ }\r
+ \r
+ double angVel = getAngVel(e);\r
+ //System.out.println(angVel);\r
+ // not moving\r
+ if (angVel==0) return;\r
+ \r
+ // motion under tolerance\r
+ if (Math.abs(angVel)<angVelTolerance) {\r
+ setAngVel(e, 0.0);\r
+ // Stopped moving, write value\r
+ if (!isGrabbed)\r
+ ;//fireStoppedMoving();\r
+ \r
+ return;\r
+ }\r
+ \r
+ modifyValue(e, ctx, deltaTime * angVel * getFactor(e)* 0.01);\r
+ double f = (isGrabbed?getGrabFriction(e):getFriction(e)); \r
+ double dt = ((double)deltaTime)/1000;\r
+ angVel *= Math.pow(1-f, dt);\r
+ setAngVel(e, angVel);\r
+ ctx.getContentContext().setDirty(); \r
+ }\r
+\r
+ @Override\r
+ public void onElementActivated(IDiagram d, IElement e) {\r
+ }\r
+\r
+ @Override\r
+ public void onElementCreated(IElement e) {\r
+ e.setHint(KEY_MIN_POINTERS, 1);\r
+ e.setHint(ElementHints.KEY_VALUE, 0.0);\r
+ e.setHint(KEY_FRICTION, initial_friction);\r
+ e.setHint(KEY_FACTOR, initial_factor);\r
+ e.setHint(KEY_GRAB_FRICTION, initial_grab_friction);\r
+ e.setHint(KEY_ANG_VEL, 0.0);\r
+ }\r
+\r
+ @Override\r
+ public void onElementDeactivated(IDiagram d, IElement e) {\r
+ }\r
+\r
+ @Override\r
+ public void onElementDestroyed(IElement e) {\r
+ }\r
+\r
+\r
+ private double getFriction(IElement e) {\r
+ return e.getHint(KEY_FRICTION); \r
+ }\r
+ \r
+ private double getGrabFriction(IElement e) {\r
+ return e.getHint(KEY_GRAB_FRICTION);\r
+ }\r
+ \r
+ private double getFactor(IElement e) {\r
+ return e.getHint(KEY_FACTOR);\r
+ } \r
+ \r
+ private double getValue(IElement e) {\r
+ return e.getHint(ElementHints.KEY_VALUE);\r
+ }\r
+ \r
+ private void setValue(IElement e, ICanvasContext ctx, double value) {\r
+ Double min = e.getHint(ElementHints.KEY_MIN_VALUE);\r
+ Double max = e.getHint(ElementHints.KEY_MAX_VALUE);\r
+ if (min!=null && value<min) value = min;\r
+ if (max!=null && value>max) value = max;\r
+ e.setHint(ElementHints.KEY_VALUE, value);\r
+ \r
+ Point2D origo = getOrigo(e, null);\r
+ \r
+ Rotate r = e.getElementClass().getSingleItem(Rotate.class);\r
+ double angle = r.getAngle(e);\r
+ ///System.out.println(angle);\r
+ double targetAngle = Math.IEEEremainder(value, Math.PI*2);\r
+ double diffrence = targetAngle - angle;\r
+ r.rotate(e, diffrence, origo);\r
+ }\r
+ \r
+ public Point2D getOrigo(IElement e, Point2D origo)\r
+ {\r
+ Rectangle2D size = new Rectangle2D.Double();\r
+ InternalSize b = e.getElementClass().getSingleItem(InternalSize.class);\r
+ b.getBounds(e, size);\r
+ if (origo==null) origo = new Point2D.Double(size.getCenterX(),size.getCenterY());\r
+ return origo;\r
+ }\r
+ \r
+ public synchronized void modifyValue(IElement e, ICanvasContext ctx, double modification) {\r
+ Double position = getValue(e);\r
+ double value = position + modification;\r
+ setValue(e, ctx, value);\r
+ }\r
+ \r
+ \r
+ public int getMinPointers(IElement e) {\r
+ Integer i = e.getHint(KEY_MIN_POINTERS);\r
+ if (i==null) return 1;\r
+ return i;\r
+ }\r
+ \r
+ public double getAngVel(IElement e)\r
+ {\r
+ Double d = e.getHint(KEY_ANG_VEL);\r
+ if (d==null) return 0.0;\r
+ return d;\r
+ }\r
+ \r
+ public void setAngVel(IElement e, double value)\r
+ {\r
+ e.setHint(KEY_ANG_VEL, value); \r
+ }\r
+ \r
+ public boolean isGrabbed(IElement e, ICanvasContext ctx) {\r
+ return (getGrabCount(e, ctx)>=getMinPointers(e)) || !isEnabled(e); \r
+ }\r
+ \r
+ public boolean isMoving(IElement e) {\r
+ return getAngVel(e)!=0;\r
+ }\r
+ \r
+ public boolean isLocked(IElement e) {\r
+ Boolean b = e.getHint(ElementHints.KEY_LOCKED);\r
+ return b==null?false:b;\r
+ }\r
+ \r
+ public boolean isEnabled(IElement e) {\r
+ Stateful enabled = e.getElementClass().getAtMostOneItemOfClass(Stateful.class);\r
+ if (enabled==null) return true;\r
+ return enabled.isEnabled(e);\r
+ }\r
+ \r
+ public boolean isMoveable(IElement e) {\r
+ return true;\r
+ } \r
+\r
+ \r
+ \r
+}\r