001/* A snap constraint for locatable nodes
002
003 Copyright (c) 1998-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.vergil.toolbox;
029
030import java.awt.geom.Point2D;
031
032import diva.canvas.interactor.PointConstraint;
033
034/**
035 This constraint ensures that a point is a multiple of a constant
036 that defaults to 5.0.
037
038 @version $Id$
039 @since Ptolemy II 2.0
040 @Pt.ProposedRating Red (eal)
041 @Pt.AcceptedRating Red (johnr)
042 @author Edward A. Lee
043 */
044public class SnapConstraint implements PointConstraint {
045    /** Construct a new instance of a snap constraint.
046     */
047    public SnapConstraint() {
048        super();
049        _resolution = _defaultResolution;
050    }
051
052    ///////////////////////////////////////////////////////////////////
053    ////                         public methods                    ////
054
055    /** Modify the specified point to snap to grid using the local
056     *  resolution.
057     *  @param point The point to modify.
058     */
059    @Override
060    public void constrain(Point2D point) {
061        double x = Math.round(point.getX() / _resolution) * _resolution;
062        double y = Math.round(point.getY() / _resolution) * _resolution;
063        point.setLocation(x, y);
064    }
065
066    /** Modify the specified point to snap to grid using the local
067     *  resolution.
068     *  @param point The point to modify (a dimension 2 array).
069     *  @return The constrained point.
070     */
071    public double[] constrain(double[] point) {
072        double[] result = new double[2];
073        result[0] = Math.round(point[0] / _resolution) * _resolution;
074        result[1] = Math.round(point[1] / _resolution) * _resolution;
075        return result;
076    }
077
078    /** Modify the specified point to snap to grid using the local
079     *  resolution.
080     *  @param x The x dimension of the point to modify.
081     *  @param y The y dimension of the point to modify.
082     *  @return The constrained point.
083     */
084    public double[] constrain(double x, double y) {
085        double[] result = new double[2];
086        result[0] = Math.round(x / _resolution) * _resolution;
087        result[1] = Math.round(y / _resolution) * _resolution;
088        return result;
089    }
090
091    /** Modify the specified point to snap to grid using the global
092     *  default resolution.
093     *  @param point The point to modify.
094     *  @return The constrained point.
095     */
096    public static Point2D constrainPoint(Point2D point) {
097        double[] originalPoint = new double[2];
098        originalPoint[0] = point.getX();
099        originalPoint[1] = point.getY();
100
101        double[] result = constrainPoint(originalPoint);
102        return new Point2D.Double(result[0], result[1]);
103    }
104
105    /** Modify the specified point to snap to grid using the global
106     *  default resolution.
107     *  @param point The point to modify (a dimension 2 array).
108     *  @return The constrained point.
109     */
110    public static double[] constrainPoint(double[] point) {
111        return constrainPoint(point[0], point[1]);
112    }
113
114    /** Modify the specified point to snap to grid using the global
115     *  default resolution.
116     *  @param x The x dimension of the point to modify.
117     *  @param y The y dimension of the point to modify.
118     *  @return The constrained point.
119     */
120    public static double[] constrainPoint(double x, double y) {
121        double[] result = new double[2];
122        result[0] = Math.round(x / _defaultResolution) * _defaultResolution;
123        result[1] = Math.round(y / _defaultResolution) * _defaultResolution;
124        return result;
125    }
126
127    /** Return the default resolution.
128     *  @return The global default resolution.
129     */
130    public static double getDefaultResolution() {
131        return _defaultResolution;
132    }
133
134    /** Return the resolution for this instance.
135     *  @return The global default resolution.
136     *  @see #setResolution(double)
137     */
138    public double getResolution() {
139        return _resolution;
140    }
141
142    /** Return true to indicate that this does snap to grid.
143     *  @return True.
144     */
145    @Override
146    public boolean snapped() {
147        return true;
148    }
149
150    /** Set the resolution for this instance.
151     *  @param resolution The new resolution.
152     *  @see #getResolution()
153     */
154    public void setResolution(double resolution) {
155        _resolution = resolution;
156    }
157
158    ///////////////////////////////////////////////////////////////////
159    ////                         private variables                 ////
160
161    /** The default resolution globally. */
162    private static double _defaultResolution = 5.0;
163
164    /** The resolution for this instance. */
165    private double _resolution = 5.0;
166}