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.scenegraph.g2d.snap;
\r
14 import java.awt.geom.Point2D;
\r
15 import java.io.Serializable;
\r
17 import org.simantics.scl.runtime.function.Function1;
\r
20 * @author Tuukka Lehtonen
\r
22 public class GridSnapAdvisor implements ISnapAdvisor, Serializable {
\r
24 private static final long serialVersionUID = 2411311098028229134L;
\r
27 Function1<Object, Boolean> enabled;
\r
29 public GridSnapAdvisor(double resolution) {
\r
30 this.resolution = resolution;
\r
33 public void setEnabledFunction(Function1<Object, Boolean> function) {
\r
34 this.enabled = function;
\r
37 public void setResolution(double resolution) {
\r
38 this.resolution = resolution;
\r
41 public double getResolution() {
\r
46 public void snap(Point2D point) {
\r
47 if (enabled != null && !enabled.apply(null))
\r
50 Math.round(point.getX() / resolution) * resolution,
\r
51 Math.round(point.getY() / resolution) * resolution);
\r
55 public void snap(Point2D point, Point2D[] features) {
\r
56 if (enabled != null && !enabled.apply(null))
\r
58 double dx = features[0].getX();
\r
59 double dy = features[0].getY();
\r
61 Math.round((point.getX()-dx) / resolution) * resolution + dx,
\r
62 Math.round((point.getY()-dy) / resolution) * resolution + dy);
\r
66 public String toString() {
\r
67 return getClass().getSimpleName() + "[resolution=" + resolution + "]";
\r
71 public int hashCode() {
\r
72 long temp = Double.doubleToLongBits(resolution);
\r
73 return (int) (temp ^ (temp >>> 32));
\r
77 public boolean equals(Object obj) {
\r
82 if (getClass() != obj.getClass())
\r
84 GridSnapAdvisor other = (GridSnapAdvisor) obj;
\r
85 return Double.doubleToLongBits(resolution) != Double.doubleToLongBits(other.resolution);
\r