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