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 javax.vecmath.Vector2d;
18 import javax.vecmath.Vector3d;
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;
35 * This handlerer rotates element when it is touched.
39 * KEY_MIN_VALUE optional
40 * KEY_MAX_VALUE optional
47 * @author Toni Kalajainen
49 public class RotatorHandler extends AbstractGrabbable implements HandleMouseEvent, LifeCycle, Heartbeat {
51 private static final long serialVersionUID = -3588513675880482627L;
53 public static final RotatorHandler INSTANCE = new RotatorHandler();
55 public static final double STRAY_DISTANCE = 1000;
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;
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);
71 public RotatorHandler() {
76 protected void onDrag(GrabInfo gi, ICanvasContext ctx) {
78 Point2D origo = getOrigo(e, null);
79 Point2D prevPos = gi.prevPosElement;
80 Point2D p = gi.dragPosElement;
82 // ---- Wheel is held! ----
84 Vector2d p0 = new Vector2d(prevPos.getX(), prevPos.getY());
86 Vector2d p1 = new Vector2d(p.getX(), p.getY());
88 Vector2d f = new Vector2d(p1);
91 if (f.length()==0) return;
93 // -- We are holding the wheel and we have moved the pointer --
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);
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();
111 if (!isGrabbed(e, ctx)) return;
114 double deltaAngle = odp0.angle(odp1);
115 if (M.z<0) deltaAngle *= -1;
117 double deltaAngularVelocity = deltaAngle * 20;
118 setAngVel(e, getAngVel(e)+deltaAngularVelocity);
121 modifyValue(e, ctx, deltaAngle);
125 protected void onGrab(GrabInfo gi, ICanvasContext ctx) {
129 protected void onGrabCancel(GrabInfo gi, ICanvasContext ctx) {
133 protected boolean onGrabCheck(IElement e, ICanvasContext ctx, int pointerId, Point2D pickPos) {
138 protected void onRelease(GrabInfo gi, ICanvasContext ctx) {
142 public void heartbeat(IElement e, long time, long deltaTime, ICanvasContext ctx) {
144 boolean isGrabbed = isGrabbed(e, ctx);
145 boolean isMoving = isMoving(e);
146 boolean isLocked = isLocked(e);
149 // Check if the value has changed in the value source
150 if ((!isGrabbed && !isMoving)||isLocked) {
155 double angVel = getAngVel(e);
156 //System.out.println(angVel);
158 if (angVel==0) return;
160 // motion under tolerance
161 if (Math.abs(angVel)<angVelTolerance) {
163 // Stopped moving, write value
165 ;//fireStoppedMoving();
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();
179 public void onElementActivated(IDiagram d, IElement e) {
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);
193 public void onElementDeactivated(IDiagram d, IElement e) {
197 public void onElementDestroyed(IElement e) {
201 private double getFriction(IElement e) {
202 return e.getHint(KEY_FRICTION);
205 private double getGrabFriction(IElement e) {
206 return e.getHint(KEY_GRAB_FRICTION);
209 private double getFactor(IElement e) {
210 return e.getHint(KEY_FACTOR);
213 private double getValue(IElement e) {
214 return e.getHint(ElementHints.KEY_VALUE);
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);
224 Point2D origo = getOrigo(e, null);
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);
234 public Point2D getOrigo(IElement e, Point2D origo)
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());
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);
250 public int getMinPointers(IElement e) {
251 Integer i = e.getHint(KEY_MIN_POINTERS);
252 if (i==null) return 1;
256 public double getAngVel(IElement e)
258 Double d = e.getHint(KEY_ANG_VEL);
259 if (d==null) return 0.0;
263 public void setAngVel(IElement e, double value)
265 e.setHint(KEY_ANG_VEL, value);
268 public boolean isGrabbed(IElement e, ICanvasContext ctx) {
269 return (getGrabCount(e, ctx)>=getMinPointers(e)) || !isEnabled(e);
272 public boolean isMoving(IElement e) {
273 return getAngVel(e)!=0;
276 public boolean isLocked(IElement e) {
277 Boolean b = e.getHint(ElementHints.KEY_LOCKED);
278 return b==null?false:b;
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);
287 public boolean isMoveable(IElement e) {