Code

Fix ef spam when adjusting pattern on text - patch from Adonis Papaderos
[inkscape.git] / src / draw-context.cpp
index d2794f0c2b6f1eed9e7444f01df145d15eb004a7..3f33f9499809ff22f9c5410e5cc60742a58e66d1 100644 (file)
@@ -468,7 +468,7 @@ spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection */*sel*/)
  *  \param dc draw context
  *  \param p cursor point (to be changed by snapping)
  *  \param o origin point
- *  \param state  keyboard state to check if ctrl was pressed
+ *  \param state  keyboard state to check if ctrl or shift was pressed
 */
 
 void spdc_endpoint_snap_rotation(SPEventContext const *const ec, Geom::Point &p, Geom::Point const &o,
@@ -476,62 +476,41 @@ void spdc_endpoint_snap_rotation(SPEventContext const *const ec, Geom::Point &p,
 {
     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
     unsigned const snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
-    /* 0 means no snapping. */
-
-    /* mirrored by fabs, so this corresponds to 15 degrees */
-    Geom::Point best; /* best solution */
-    double bn = NR_HUGE; /* best normal */
-    double bdot = 0;
-    Geom::Point v = Geom::Point(0, 1);
-    double const r00 = cos(M_PI / snaps), r01 = sin(M_PI / snaps);
-    double const r10 = -r01, r11 = r00;
-
-    Geom::Point delta = p - o;
-
-    for (unsigned i = 0; i < snaps; i++) {
-        double const ndot = fabs(dot(v,Geom::rot90(delta)));
-        Geom::Point t(r00*v[Geom::X] + r01*v[Geom::Y],
-                    r10*v[Geom::X] + r11*v[Geom::Y]);
-        if (ndot < bn) {
-            /* I think it is better numerically to use the normal, rather than the dot product
-             * to assess solutions, but I haven't proven it. */
-            bn = ndot;
-            best = v;
-            bdot = dot(v, delta);
-        }
-        v = t;
+    SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
+    m.setup(SP_EVENT_CONTEXT_DESKTOP(ec));
+
+    bool snap_enabled = m.snapprefs.getSnapEnabledGlobally();
+    if (state & GDK_SHIFT_MASK) {
+        // SHIFT disables all snapping, except the angular snapping. After all, the user explicitly asked for angular
+        // snapping by pressing CTRL, otherwise we wouldn't have arrived here. But although we temporarily disable
+        // the snapping here, we must still call for a constrained snap in order to apply the constraints (i.e. round
+        // to the nearest angle increment)
+        m.snapprefs.setSnapEnabledGlobally(false);
     }
 
-    if (fabs(bdot) > 0) {
-        p = o + bdot * best;
+    Inkscape::SnappedPoint dummy = m.constrainedAngularSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE), boost::optional<Geom::Point>(), o, snaps);
+    p = dummy.getPoint();
 
-        if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
-                                         //After all, the user explicitly asked for angular snapping by
-                                         //pressing CTRL
-            /* Snap it along best vector */
-            SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
-            m.setup(SP_EVENT_CONTEXT_DESKTOP(ec));
-            Geom::Point pt2g = to_2geom(p);
-            m.constrainedSnapReturnByRef( Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE, Inkscape::Snapper::ConstraintLine(best), false);
-            p = from_2geom(pt2g);
-        }
+    if (state & GDK_SHIFT_MASK) {
+        m.snapprefs.setSnapEnabledGlobally(snap_enabled); // restore the original setting
     }
+
+    m.unSetup();
 }
 
 
 void spdc_endpoint_snap_free(SPEventContext const * const ec, Geom::Point& p, guint const /*state*/)
 {
     SPDesktop *dt = SP_EVENT_CONTEXT_DESKTOP(ec);
-       SnapManager &m = dt->namedview->snap_manager;
-       Inkscape::Selection *selection = sp_desktop_selection (dt);
+    SnapManager &m = dt->namedview->snap_manager;
+    Inkscape::Selection *selection = sp_desktop_selection (dt);
 
-       // selection->singleItem() is the item that is currently being drawn. This item will not be snapped to (to avoid self-snapping)
-       // TODO: Allow snapping to the stationary parts of the item, and only ignore the last segment
+    // selection->singleItem() is the item that is currently being drawn. This item will not be snapped to (to avoid self-snapping)
+    // TODO: Allow snapping to the stationary parts of the item, and only ignore the last segment
 
-       m.setup(dt, true, selection->singleItem());
-    Geom::Point pt2g = to_2geom(p);
-    m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
-    p = from_2geom(pt2g);
+    m.setup(dt, true, selection->singleItem());
+    m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE);
+    m.unSetup();
 }
 
 static SPCurve *
@@ -841,7 +820,7 @@ void spdc_create_single_dot(SPEventContext *ec, Geom::Point const &pt, char cons
     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
     Geom::Matrix const i2d (sp_item_i2d_affine (item));
-    Geom::Point pp = pt * i2d;
+    Geom::Point pp = pt * i2d.inverse();
     double rad = 0.5 * prefs->getDouble(tool_path + "/dot-size", 3.0);
     if (event_state & GDK_MOD1_MASK) {
         /* TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size
@@ -876,4 +855,4 @@ void spdc_create_single_dot(SPEventContext *ec, Geom::Point const &pt, char 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 :