Code

Node tool: special case node duplication for endnodes - select new endnode
[inkscape.git] / src / knot-holder-entity.cpp
index 6fcb262475a674e30b1536ed21752070a9cdf184..005ca24c71c1fbe2e1b8bbb8112c5c5401f4ed26 100644 (file)
@@ -1,11 +1,10 @@
-#define __KNOT_HOLDER_ENTITY_C__
-
 /** \file
  * KnotHolderEntity definition.
  *
  * Authors:
  *   Mitsuru Oka <oka326@parkcity.ne.jp>
  *   Maximilian Albert <maximilian.albert@gmail.com>
+ *   Abhishek Sharma
  *
  * Copyright (C) 1999-2001 Lauris Kaplinski
  * Copyright (C) 2000-2001 Ximian, Inc.
@@ -77,7 +76,7 @@ KnotHolderEntity::~KnotHolderEntity()
 void
 KnotHolderEntity::update_knot()
 {
-    Geom::Matrix const i2d(sp_item_i2d_affine(item));
+    Geom::Matrix const i2d(item->i2d_affine());
 
     Geom::Point dp(knot_get() * i2d);
 
@@ -89,43 +88,32 @@ KnotHolderEntity::update_knot()
 Geom::Point
 KnotHolderEntity::snap_knot_position(Geom::Point const &p)
 {
-    Geom::Matrix const i2d (sp_item_i2d_affine(item));
+    Geom::Matrix const i2d (item->i2d_affine());
     Geom::Point s = p * i2d;
 
     SnapManager &m = desktop->namedview->snap_manager;
     m.setup(desktop, true, item);
-
     m.freeSnapReturnByRef(s, Inkscape::SNAPSOURCE_NODE_HANDLE);
+    m.unSetup();
 
     return s * i2d.inverse();
 }
 
 Geom::Point
-KnotHolderEntity::snap_knot_position_constrained(Geom::Point const &p, Inkscape::Snapper::ConstraintLine const &constraint)
+KnotHolderEntity::snap_knot_position_constrained(Geom::Point const &p, Inkscape::Snapper::SnapConstraint const &constraint)
 {
-    Geom::Matrix const i2d (sp_item_i2d_affine(item));
+    Geom::Matrix const i2d (item->i2d_affine());
     Geom::Point s = p * i2d;
 
     SnapManager &m = desktop->namedview->snap_manager;
     m.setup(desktop, true, item);
 
     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
-    if ((prefs->getBool("/options/snapmousepointer/value", false))) { // legacy behavior (pre v0.47)
-        // 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 should be handled by the
-        // smart dynamic guides which are yet to be implemented, making this behavior more clean and
-        // transparent. With the current implementation it leads to unexpected results, and it doesn't
-        // allow accurately controlling what is being snapped to.
-
-        // freeSnap() will try snapping point p. This will not take into account the constraint, which
-        // is therefore to be enforced after snap_knot_position_constrained() has finished
-        m.freeSnapReturnByRef(s, Inkscape::SNAPSOURCE_NODE_HANDLE);
-    } else {
-        // constrainedSnap() will first project the point p onto the constraint line and then try to snap along that line.
-        // This way the constraint is already enforced, no need to worry about that later on
-        Inkscape::Snapper::ConstraintLine transformed_constraint = Inkscape::Snapper::ConstraintLine(constraint.getPoint() * i2d, (constraint.getPoint() + constraint.getDirection()) * i2d - constraint.getPoint() * i2d);
-        m.constrainedSnapReturnByRef(s, Inkscape::SNAPSOURCE_NODE_HANDLE, transformed_constraint);
-    }
+    // constrainedSnap() will first project the point p onto the constraint line and then try to snap along that line.
+    // This way the constraint is already enforced, no need to worry about that later on
+    Inkscape::Snapper::SnapConstraint transformed_constraint = Inkscape::Snapper::SnapConstraint(constraint.getPoint() * i2d, (constraint.getPoint() + constraint.getDirection()) * i2d - constraint.getPoint() * i2d);
+    m.constrainedSnapReturnByRef(s, Inkscape::SNAPSOURCE_NODE_HANDLE, transformed_constraint);
+    m.unSetup();
 
     return s * i2d.inverse();
 }
@@ -135,16 +123,21 @@ KnotHolderEntity::snap_knot_position_constrained(Geom::Point const &p, Inkscape:
 
 /*  TODO: this pattern manipulation is not able to handle general transformation matrices. Only matrices that are the result of a pure scale times a pure rotation. */
 
-Geom::Point
-PatternKnotHolderEntityXY::knot_get()
+static gdouble sp_pattern_extract_theta(SPPattern *pat)
 {
-    SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
+    Geom::Matrix transf = pat->patternTransform;
+    return Geom::atan2(transf.xAxis());
+}
 
-    gdouble x = 0;
-    gdouble y = -pattern_height(pat);
+static Geom::Point sp_pattern_extract_scale(SPPattern *pat)
+{
+    Geom::Matrix transf = pat->patternTransform;
+    return Geom::Point( transf.expansionX(), transf.expansionY() );
+}
 
-    Geom::Point delta = Geom::Point(x,y) * pat->patternTransform;
-    return delta;
+static Geom::Point sp_pattern_extract_trans(SPPattern const *pat)
+{
+    return Geom::Point(pat->patternTransform[4], pat->patternTransform[5]);
 }
 
 void
@@ -164,23 +157,32 @@ PatternKnotHolderEntityXY::knot_set(Geom::Point const &p, Geom::Point const &ori
     }
 
     if (state)  {
-        Geom::Point knot_relpos(0, -pattern_height(pat));
-        Geom::Point const q = p_snapped - (knot_relpos * pat->patternTransform);
-        sp_item_adjust_pattern(item, Geom::Matrix(Geom::Translate(q)));
+        Geom::Point const q = p_snapped - sp_pattern_extract_trans(pat);
+        item->adjust_pattern(Geom::Matrix(Geom::Translate(q)));
     }
 
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
 }
 
+Geom::Point
+PatternKnotHolderEntityXY::knot_get()
+{
+    SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
+    return sp_pattern_extract_trans(pat);
+}
+
 Geom::Point
 PatternKnotHolderEntityAngle::knot_get()
 {
     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
 
-    gdouble x = pattern_width(pat);
-    gdouble y = -pattern_height(pat);
-
-    Geom::Point delta = Geom::Point(x,y) * pat->patternTransform;
+    gdouble x = (pattern_width(pat));
+    gdouble y = 0;
+    Geom::Point delta = Geom::Point(x,y);
+    Geom::Point scale = sp_pattern_extract_scale(pat);
+    gdouble theta = sp_pattern_extract_theta(pat);
+    delta = delta * Geom::Matrix(Geom::Scale(scale))*Geom::Matrix(Geom::Rotate(theta));
+    delta = delta + sp_pattern_extract_trans(pat);
     return delta;
 }
 
@@ -192,38 +194,24 @@ PatternKnotHolderEntityAngle::knot_set(Geom::Point const &p, Geom::Point const &
 
     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
 
-    // rotate pattern around XY knot position
-    Geom::Point knot_relpos(pattern_width(pat), -pattern_height(pat));
-    Geom::Point xy_knot_relpos(0, -pattern_height(pat));
-    Geom::Point transform_origin = xy_knot_relpos * pat->patternTransform;
-
-    Geom::Point oldp = (knot_relpos * pat->patternTransform) - transform_origin;
-    Geom::Point newp = p - transform_origin;
-
-    gdouble theta = Geom::angle_between(oldp, newp);
+    // get the angle from pattern 0,0 to the cursor pos
+    Geom::Point delta = p - sp_pattern_extract_trans(pat);
+    gdouble theta = atan2(delta);
 
     if ( state & GDK_CONTROL_MASK ) {
         theta = sp_round(theta, M_PI/snaps);
     }
 
-    Geom::Matrix rot = Geom::Matrix(Geom::Translate(-transform_origin))
-                     * Geom::Rotate(theta)
-                     * Geom::Translate(transform_origin);
-    sp_item_adjust_pattern(item, rot);
+    // get the scale from the current transform so we can keep it.
+    Geom::Point scl = sp_pattern_extract_scale(pat);
+    Geom::Matrix rot = Geom::Matrix(Geom::Scale(scl)) * Geom::Matrix(Geom::Rotate(theta));
+    Geom::Point const t = sp_pattern_extract_trans(pat);
+    rot[4] = t[Geom::X];
+    rot[5] = t[Geom::Y];
+    item->adjust_pattern(rot, true);
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
 }
 
-Geom::Point
-PatternKnotHolderEntityScale::knot_get()
-{
-    SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
-
-    gdouble x = pattern_width(pat);
-    gdouble y = 0;
-    Geom::Point delta = Geom::Point(x,y) * pat->patternTransform;
-    return delta;
-}
-
 void
 PatternKnotHolderEntityScale::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint state)
 {
@@ -232,32 +220,49 @@ PatternKnotHolderEntityScale::knot_set(Geom::Point const &p, Geom::Point const &
     // FIXME: this snapping should be done together with knowing whether control was pressed. If GDK_CONTROL_MASK, then constrained snapping should be used.
     Geom::Point p_snapped = snap_knot_position(p);
 
-    Geom::Point knot_relpos(pattern_width(pat), 0);
-    Geom::Point xy_knot_relpos(0, -pattern_height(pat));
-    Geom::Point transform_origin = xy_knot_relpos * pat->patternTransform;
+    // get angle from current transform
+    gdouble theta = sp_pattern_extract_theta(pat);
 
-    // do the scaling in pattern coordinate space
-    Geom::Point oldp = knot_relpos - xy_knot_relpos;
-    Geom::Point newp = p_snapped * pat->patternTransform.inverse() - xy_knot_relpos;
-
-    if (Geom::are_near(newp.length(), 0)) return;
-
-    Geom::Scale s(1);
-    if (state & GDK_CONTROL_MASK) {
-        // uniform scaling
-        s = Geom::Scale(oldp * (newp.length() * oldp.length()));
+    // Get the new scale from the position of the knotholder
+    Geom::Point d = p_snapped - sp_pattern_extract_trans(pat);
+    gdouble pat_x = pattern_width(pat);
+    gdouble pat_y = pattern_height(pat);
+    Geom::Scale scl(1);
+    if ( state & GDK_CONTROL_MASK ) {
+        // if ctrl is pressed: use 1:1 scaling
+        gdouble pat_h = hypot(pat_x, pat_y);
+        scl = Geom::Scale(d.length() / pat_h);
     } else {
-        s = Geom::Scale(newp[Geom::X] / oldp[Geom::X], newp[Geom::Y] / oldp[Geom::Y]);
+        d *= Geom::Rotate(-theta);
+        scl = Geom::Scale(d[Geom::X] / pat_x, d[Geom::Y] / pat_y);
     }
 
-    Geom::Matrix scl = Geom::Matrix(Geom::Translate(-xy_knot_relpos))
-                     * s
-                     * Geom::Translate(xy_knot_relpos)
-                     * pat->patternTransform;
-    sp_item_adjust_pattern(item, scl, true);
+    Geom::Matrix rot = (Geom::Matrix)scl * Geom::Rotate(theta);
+
+    Geom::Point const t = sp_pattern_extract_trans(pat);
+    rot[4] = t[Geom::X];
+    rot[5] = t[Geom::Y];
+    item->adjust_pattern(rot, true);
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
 }
 
+
+Geom::Point
+PatternKnotHolderEntityScale::knot_get()
+{
+    SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
+
+    gdouble x = pattern_width(pat);
+    gdouble y = pattern_height(pat);
+    Geom::Point delta = Geom::Point(x,y);
+    Geom::Matrix a = pat->patternTransform;
+    a[4] = 0;
+    a[5] = 0;
+    delta = delta * a;
+    delta = delta + sp_pattern_extract_trans(pat);
+    return delta;
+}
+
 /*
   Local Variables:
   mode:c++
@@ -267,4 +272,4 @@ PatternKnotHolderEntityScale::knot_set(Geom::Point const &p, Geom::Point const &
   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 :