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