Code

Node tool: snap while scaling a selection of nodes. Consider this as experimental...
[inkscape.git] / src / snap.cpp
index f41dcf2ab6c93453d832c4cdb4126d7f86a468ee..85d2fd5afbac5bfe129b400bc04214b23813f1fd 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "sp-namedview.h"
 #include "snap.h"
+#include "snap-enums.h"
 #include "snapped-line.h"
 #include "snapped-curve.h"
 
@@ -319,11 +320,12 @@ Geom::Point SnapManager::multipleOfGridPitch(Geom::Point const &t, Geom::Point c
  *  constrainedSnapReturnByRef() is equal in snapping behavior to
  *  constrainedSnap(), but the former returns the snapped point trough the referenced
  *  parameter p. This parameter p initially contains the position of the snap
- *  source and will we overwritten by the target position if snapping has occurred.
+ *  source and will be overwritten by the target position if snapping has occurred.
  *  This makes snapping transparent to the calling code. If this is not desired
  *  because either the calling code must know whether snapping has occurred, or
  *  because the original position should not be touched, then constrainedSnap() should
- *  be called instead.
+ *  be called instead. If there's nothing to snap to or if snapping has been disabled,
+ *  then this method will still apply the constraint (but without snapping)
  *
  *  PS:
  *  1) SnapManager::setup() must have been called before calling this method,
@@ -357,6 +359,8 @@ void SnapManager::constrainedSnapReturnByRef(Geom::Point &p,
  *
  *  PS: SnapManager::setup() must have been called before calling this method,
  *  but only once for a set of points
+ *  PS: If there's nothing to snap to or if snapping has been disabled, then this
+ *  method will still apply the constraint (but without snapping)
  *
  *  \param p Source point to be snapped
  *  \param constraint The direction or line along which snapping must occur
@@ -421,12 +425,14 @@ Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::SnapCandidatePoint
  * and will try to snap the SnapCandidatePoint to all of the provided constraints and see which one fits best
  *  \param p Source point to be snapped
  *  \param constraints List of directions or lines along which snapping must occur
+ *  \param dont_snap If true then we will only apply the constraint, without snapping
  *  \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
  */
 
 
 Inkscape::SnappedPoint SnapManager::multipleConstrainedSnaps(Inkscape::SnapCandidatePoint const &p,
                                                     std::vector<Inkscape::Snapper::SnapConstraint> const &constraints,
+                                                    bool dont_snap,
                                                     Geom::OptRect const &bbox_to_snap) const
 {
 
@@ -438,7 +444,7 @@ Inkscape::SnappedPoint SnapManager::multipleConstrainedSnaps(Inkscape::SnapCandi
     SnappedConstraints sc;
     SnapperList const snappers = getSnappers();
     std::vector<Geom::Point> projections;
-    bool snapping_is_futile = !someSnapperMightSnap();
+    bool snapping_is_futile = !someSnapperMightSnap() || dont_snap;
 
     Inkscape::SnappedPoint result = no_snap;
 
@@ -452,7 +458,7 @@ Inkscape::SnappedPoint SnapManager::multipleConstrainedSnaps(Inkscape::SnapCandi
         projections.push_back(pp);
     }
 
-    if (snap_mouse && p.isSingleHandle()) {
+    if (snap_mouse && p.isSingleHandle() && !dont_snap) {
         // Snapping the mouse pointer instead of the constrained position of the knot allows
         // to snap to things which don't intersect with the constraint line; this is basically
         // then just a freesnap with the constraint applied afterwards
@@ -677,7 +683,7 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed(
     Geom::Point const &transformation,
     Geom::Point const &origin,
     Geom::Dim2 dim,
-    bool uniform) const
+    bool uniform)
 {
     /* We have a list of points, which we are proposing to transform in some way.  We need to see
     ** if any of these points, when transformed, snap to anything.  If they do, we return the
@@ -729,6 +735,12 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed(
         g_warning("Unconstrained rotation is not supported!");
     }
 
+    // We will try to snap a set of points, but we don't want to have a snap indicator displayed
+    // for each of them. That's why it's temporarily disabled here, and re-enabled again after we
+    // have finished calling the freeSnap() and constrainedSnap() methods
+    bool _orig_snapindicator_status = _snapindicator;
+    _snapindicator = false;
+
     std::vector<Inkscape::SnapCandidatePoint>::iterator j = transformed_points.begin();
 
     // std::cout << std::endl;
@@ -797,6 +809,9 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed(
         // std::cout << "dist = " << snapped_point.getSnapDistance() << std::endl;
         snapped_point.setPointerDistance(Geom::L2(pointer - (*i).getPoint()));
 
+        // Allow the snapindicator to be displayed again
+        _snapindicator = _orig_snapindicator_status;
+
         Geom::Point result;
 
         /*Find the transformation that describes where the snapped point has
@@ -886,6 +901,7 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed(
 
         if (snapped_point.getSnapped()) {
             // We snapped; keep track of the best snap
+            // TODO: Compare the transformations instead of the snap points; we should be looking for the closest transformation
             if (best_snapped_point.isOtherSnapBetter(snapped_point, true)) {
                 best_transformation = result;
                 best_snapped_point = snapped_point;
@@ -927,6 +943,15 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed(
     // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
     // These rounding errors might be caused by NRRects, see bug #1584301
     best_snapped_point.setSnapDistance(best_metric < 1e6 ? best_metric : NR_HUGE);
+
+    if (_snapindicator) {
+        if (best_snapped_point.getSnapped()) {
+            _desktop->snapindicator->set_new_snaptarget(best_snapped_point);
+        } else {
+            _desktop->snapindicator->remove_snaptarget();
+        }
+    }
+
     return best_snapped_point;
 }
 
@@ -942,7 +967,7 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed(
 
 Inkscape::SnappedPoint SnapManager::freeSnapTranslate(std::vector<Inkscape::SnapCandidatePoint> const &p,
                                                         Geom::Point const &pointer,
-                                                        Geom::Point const &tr) const
+                                                        Geom::Point const &tr)
 {
     Inkscape::SnappedPoint result = _snapTransformed(p, pointer, false, Geom::Point(0,0), TRANSLATE, tr, Geom::Point(0,0), Geom::X, false);
 
@@ -966,7 +991,7 @@ Inkscape::SnappedPoint SnapManager::freeSnapTranslate(std::vector<Inkscape::Snap
 Inkscape::SnappedPoint SnapManager::constrainedSnapTranslate(std::vector<Inkscape::SnapCandidatePoint> const &p,
                                                                Geom::Point const &pointer,
                                                                Inkscape::Snapper::SnapConstraint const &constraint,
-                                                               Geom::Point const &tr) const
+                                                               Geom::Point const &tr)
 {
     Inkscape::SnappedPoint result = _snapTransformed(p, pointer, true, constraint, TRANSLATE, tr, Geom::Point(0,0), Geom::X, false);
 
@@ -991,7 +1016,7 @@ Inkscape::SnappedPoint SnapManager::constrainedSnapTranslate(std::vector<Inkscap
 Inkscape::SnappedPoint SnapManager::freeSnapScale(std::vector<Inkscape::SnapCandidatePoint> const &p,
                                                   Geom::Point const &pointer,
                                                   Geom::Scale const &s,
-                                                  Geom::Point const &o) const
+                                                  Geom::Point const &o)
 {
     Inkscape::SnappedPoint result = _snapTransformed(p, pointer, false, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false);
 
@@ -1016,7 +1041,7 @@ Inkscape::SnappedPoint SnapManager::freeSnapScale(std::vector<Inkscape::SnapCand
 Inkscape::SnappedPoint SnapManager::constrainedSnapScale(std::vector<Inkscape::SnapCandidatePoint> const &p,
                                                          Geom::Point const &pointer,
                                                          Geom::Scale const &s,
-                                                         Geom::Point const &o) const
+                                                         Geom::Point const &o)
 {
     // When constrained scaling, only uniform scaling is supported.
     Inkscape::SnappedPoint result = _snapTransformed(p, pointer, true, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true);
@@ -1045,7 +1070,7 @@ Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(std::vector<Inkscape:
                                                             Geom::Coord const &s,
                                                             Geom::Point const &o,
                                                             Geom::Dim2 d,
-                                                            bool u) const
+                                                            bool u)
 {
     Inkscape::SnappedPoint result = _snapTransformed(p, pointer, true, Geom::Point(0,0), STRETCH, Geom::Point(s, s), o, d, u);
 
@@ -1073,7 +1098,7 @@ Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(std::vector<Inkscape::Sn
                                                  Inkscape::Snapper::SnapConstraint const &constraint,
                                                  Geom::Point const &s,
                                                  Geom::Point const &o,
-                                                 Geom::Dim2 d) const
+                                                 Geom::Dim2 d)
 {
     // "s" contains skew factor in s[0], and scale factor in s[1]
 
@@ -1108,7 +1133,7 @@ Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(std::vector<Inkscape::Sn
 Inkscape::SnappedPoint SnapManager::constrainedSnapRotate(std::vector<Inkscape::SnapCandidatePoint> const &p,
                                                     Geom::Point const &pointer,
                                                     Geom::Coord const &angle,
-                                                    Geom::Point const &o) const
+                                                    Geom::Point const &o)
 {
     // Snapping the nodes of the bounding box of a selection that is being transformed, will only work if
     // the transformation of the bounding box is equal to the transformation of the individual nodes. This is
@@ -1424,6 +1449,26 @@ void SnapManager::_displaySnapsource(Inkscape::SnapCandidatePoint const &p) cons
     }
 }
 
+void SnapManager::keepClosestPointOnly(std::vector<Inkscape::SnapCandidatePoint> &points, const Geom::Point &reference) const
+{
+    if (points.size() < 2) return;
+
+    Inkscape::SnapCandidatePoint closest_point = Inkscape::SnapCandidatePoint(Geom::Point(NR_HUGE, NR_HUGE), Inkscape::SNAPSOURCE_UNDEFINED, Inkscape::SNAPTARGET_UNDEFINED);
+    Geom::Coord closest_dist = NR_HUGE;
+
+    for(std::vector<Inkscape::SnapCandidatePoint>::const_iterator i = points.begin(); i != points.end(); i++) {
+        Geom::Coord dist = Geom::L2((*i).getPoint() - reference);
+        if (i == points.begin() || dist < closest_dist) {
+            closest_point = *i;
+            closest_dist = dist;
+        }
+    }
+
+    closest_point.setSourceNum(-1);
+    points.clear();
+    points.push_back(closest_point);
+}
+
 /*
   Local Variables:
   mode:c++
@@ -1433,4 +1478,4 @@ void SnapManager::_displaySnapsource(Inkscape::SnapCandidatePoint const &p) cons
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :