Code

Fix small snapping regression (reported in LP #562205, comment #11)
[inkscape.git] / src / ui / tool / node.cpp
index 865d65386c7fe92d10f9927688f21e6992cc3503..56e0ecff86e91a8caef0e326d5b8f3303310724a 100644 (file)
@@ -106,22 +106,11 @@ void Handle::setVisible(bool v)
 
 void Handle::move(Geom::Point const &new_pos)
 {
-    Handle *other, *towards, *towards_second;
-    Node *node_towards; // node in direction of this handle
-    Node *node_away; // node in the opposite direction
-    if (this == &_parent->_front) {
-        other = &_parent->_back;
-        node_towards = _parent->_next();
-        node_away = _parent->_prev();
-        towards = node_towards ? &node_towards->_back : 0;
-        towards_second = node_towards ? &node_towards->_front : 0;
-    } else {
-        other = &_parent->_front;
-        node_towards = _parent->_prev();
-        node_away = _parent->_next();
-        towards = node_towards ? &node_towards->_front : 0;
-        towards_second = node_towards ? &node_towards->_back : 0;
-    }
+    Handle *other = this->other();
+    Node *node_towards = _parent->nodeToward(this); // node in direction of this handle
+    Node *node_away = _parent->nodeAwayFrom(this); // node in the opposite direction
+    Handle *towards = node_towards ? node_towards->handleAwayFrom(_parent) : NULL;
+    Handle *towards_second = node_towards ? node_towards->handleToward(_parent) : NULL;
 
     if (Geom::are_near(new_pos, _parent->position())) {
         // The handle becomes degenerate. If the segment between it and the node
@@ -225,7 +214,7 @@ char const *Handle::handle_type_to_localized_string(NodeType type)
 
 bool Handle::grabbed(GdkEventMotion *)
 {
-    _saved_other_pos = other().position();
+    _saved_other_pos = other()->position();
     _saved_length = _drag_out ? 0 : length();
     _pm()._handleGrabbed();
     return false;
@@ -235,9 +224,13 @@ void Handle::dragged(Geom::Point &new_pos, GdkEventMotion *event)
 {
     Geom::Point parent_pos = _parent->position();
     Geom::Point origin = _last_drag_origin();
+    SnapManager &sm = _desktop->namedview->snap_manager;
+    bool snap = sm.someSnapperMightSnap();
+
     // with Alt, preserve length
     if (held_alt(*event)) {
         new_pos = parent_pos + Geom::unit_vector(new_pos - parent_pos) * _saved_length;
+        snap = false;
     }
     // with Ctrl, constrain to M_PI/rotationsnapsperpi increments from vertical
     // and the original position.
@@ -246,7 +239,7 @@ void Handle::dragged(Geom::Point &new_pos, GdkEventMotion *event)
         int snaps = 2 * prefs->getIntLimited("/options/rotationsnapsperpi/value", 12, 1, 1000);
 
         // note: if snapping to the original position is only desired in the original
-        // direction of the handle, change 2nd line below to Ray instead of Line
+        // direction of the handle, change to Ray instead of Line
         Geom::Line original_line(parent_pos, origin);
         Geom::Point snap_pos = parent_pos + Geom::constrain_angle(
             Geom::Point(0,0), new_pos - parent_pos, snaps, Geom::Point(1,0));
@@ -257,18 +250,46 @@ void Handle::dragged(Geom::Point &new_pos, GdkEventMotion *event)
         } else {
             new_pos = orig_pos;
         }
+        snap = false;
     }
+
+    std::vector<Inkscape::SnapCandidatePoint> unselected;
+    if (snap) {
+        typedef ControlPointSelection::Set Set;
+        Set &nodes = _parent->_selection.allPoints();
+        for (Set::iterator i = nodes.begin(); i != nodes.end(); ++i) {
+            Node *n = static_cast<Node*>(*i);
+            Inkscape::SnapCandidatePoint p(n->position(), n->_snapSourceType(), n->_snapTargetType());
+            unselected.push_back(p);
+        }
+        sm.setupIgnoreSelection(_desktop, true, &unselected);
+
+        Node *node_away = (this == &_parent->_front ? _parent->_prev() : _parent->_next());
+        if (_parent->type() == NODE_SMOOTH && Node::_is_line_segment(_parent, node_away)) {
+            Inkscape::Snapper::ConstraintLine cl(_parent->position(),
+                _parent->position() - node_away->position());
+            Inkscape::SnappedPoint p;
+            p = sm.constrainedSnap(Inkscape::SnapCandidatePoint(new_pos, SNAPSOURCE_NODE_HANDLE), cl);
+            if (p.getSnapped()) {
+                p.getPoint(new_pos);
+            }
+        } else {
+            sm.freeSnapReturnByRef(new_pos, SNAPSOURCE_NODE_HANDLE);
+        }
+    }
+
     // with Shift, if the node is cusp, rotate the other handle as well
     if (_parent->type() == NODE_CUSP && !_drag_out) {
         if (held_shift(*event)) {
             Geom::Point other_relpos = _saved_other_pos - parent_pos;
             other_relpos *= Geom::Rotate(Geom::angle_between(origin - parent_pos, new_pos - parent_pos));
-            other().setRelativePos(other_relpos);
+            other()->setRelativePos(other_relpos);
         } else {
             // restore the position
-            other().setPosition(_saved_other_pos);
+            other()->setPosition(_saved_other_pos);
         }
     }
+    move(new_pos); // needed for correct update, even though it's redundant
     _pm().update();
 }
 
@@ -300,10 +321,10 @@ bool Handle::clicked(GdkEventButton *event)
     return true;
 }
 
-Handle &Handle::other()
+Handle *Handle::other()
 {
-    if (this == &_parent->_front) return _parent->_back;
-    return _parent->_front;
+    if (this == &_parent->_front) return &_parent->_back;
+    return &_parent->_front;
 }
 
 static double snap_increment_degrees() {
@@ -315,7 +336,7 @@ static double snap_increment_degrees() {
 Glib::ustring Handle::_getTip(unsigned state)
 {
     char const *more;
-    bool can_shift_rotate = _parent->type() == NODE_CUSP && !other().isDegenerate();
+    bool can_shift_rotate = _parent->type() == NODE_CUSP && !other()->isDegenerate();
     if (can_shift_rotate) {
         more = C_("Path handle tip", "more: Shift, Ctrl, Alt");
     } else {
@@ -346,11 +367,11 @@ Glib::ustring Handle::_getTip(unsigned state)
         if (state_held_control(state)) {
             if (state_held_shift(state) && can_shift_rotate) {
                 return format_tip(C_("Path handle tip",
-                    "<b>Ctrl:</b> snap rotation angle to %g° increments, click to retract"),
+                    "<b>Shift+Ctrl:</b> snap rotation angle to %g° increments and rotate both handles"),
                     snap_increment_degrees());
             } else {
                 return format_tip(C_("Path handle tip",
-                    "<b>Shift+Ctrl:</b> snap rotation angle to %g° increments and rotate both handles"),
+                    "<b>Ctrl:</b> snap rotation angle to %g° increments, click to retract"),
                     snap_increment_degrees());
             }
         } else if (state_held_shift(state) && can_shift_rotate) {
@@ -943,7 +964,7 @@ void Node::dragged(Geom::Point &new_pos, GdkEventMotion *event)
                 unselected.push_back(p);
             }
         }
-        sm.setupIgnoreSelection(_desktop, true, &unselected);
+        sm.setupIgnoreSelection(_desktop, false, &unselected);
     }
 
     if (held_control(*event)) {
@@ -974,20 +995,20 @@ void Node::dragged(Geom::Point &new_pos, GdkEventMotion *event)
             // TODO: combine the snap and non-snap branches by modifying snap.h / snap.cpp
             if (snap) {
                 if (line_front) {
-                    fp = sm.constrainedSnap(Inkscape::SnapCandidatePoint(position(),
+                    fp = sm.constrainedSnap(Inkscape::SnapCandidatePoint(new_pos,
                         _snapSourceType()), *line_front);
                 }
                 if (line_back) {
-                    bp = sm.constrainedSnap(Inkscape::SnapCandidatePoint(position(),
+                    bp = sm.constrainedSnap(Inkscape::SnapCandidatePoint(new_pos,
                         _snapSourceType()), *line_back);
                 }
             }
             if (fp.getSnapped() || bp.getSnapped()) {
                 if (fp.isOtherSnapBetter(bp, false)) {
-                    bp.getPoint(new_pos);
-                } else {
-                    fp.getPoint(new_pos);
+                    fp = bp;
                 }
+                fp.getPoint(new_pos);
+                _desktop->snapindicator->set_new_snaptarget(fp);
             } else {
                 boost::optional<Geom::Point> pos;
                 if (line_front) {
@@ -1010,14 +1031,15 @@ void Node::dragged(Geom::Point &new_pos, GdkEventMotion *event)
             if (snap) {
                 Inkscape::Snapper::ConstraintLine line_x(origin, Geom::Point(1, 0));
                 Inkscape::Snapper::ConstraintLine line_y(origin, Geom::Point(0, 1));
-                fp = sm.constrainedSnap(Inkscape::SnapCandidatePoint(position(), _snapSourceType()), line_x);
-                bp = sm.constrainedSnap(Inkscape::SnapCandidatePoint(position(), _snapSourceType()), line_y);
+                fp = sm.constrainedSnap(Inkscape::SnapCandidatePoint(new_pos, _snapSourceType()), line_x);
+                bp = sm.constrainedSnap(Inkscape::SnapCandidatePoint(new_pos, _snapSourceType()), line_y);
             }
             if (fp.getSnapped() || bp.getSnapped()) {
                 if (fp.isOtherSnapBetter(bp, false)) {
                     fp = bp;
                 }
                 fp.getPoint(new_pos);
+                _desktop->snapindicator->set_new_snaptarget(fp);
             } else {
                 Geom::Point origin = _last_drag_origin();
                 Geom::Point delta = new_pos - origin;
@@ -1026,7 +1048,11 @@ void Node::dragged(Geom::Point &new_pos, GdkEventMotion *event)
             }
         }
     } else if (snap) {
-        sm.freeSnapReturnByRef(new_pos, _snapSourceType());
+        Inkscape::SnappedPoint p = sm.freeSnap(Inkscape::SnapCandidatePoint(new_pos, _snapSourceType()));
+        if (p.getSnapped()) {
+            p.getPoint(new_pos);
+            _desktop->snapindicator->set_new_snaptarget(p);
+        }
     }
 
     SelectableControlPoint::dragged(new_pos, event);
@@ -1052,6 +1078,58 @@ Inkscape::SnapTargetType Node::_snapTargetType()
     return SNAPTARGET_NODE_CUSP;
 }
 
+/** @brief Gets the handle that faces the given adjacent node.
+ * Will abort with error if the given node is not adjacent. */
+Handle *Node::handleToward(Node *to)
+{
+    if (_next() == to) {
+        return front();
+    }
+    if (_prev() == to) {
+        return back();
+    }
+    g_error("Node::handleToward(): second node is not adjacent!");
+}
+
+/** @brief Gets the node in the direction of the given handle.
+ * Will abort with error if the handle doesn't belong to this node. */
+Node *Node::nodeToward(Handle *dir)
+{
+    if (front() == dir) {
+        return _next();
+    }
+    if (back() == dir) {
+        return _prev();
+    }
+    g_error("Node::nodeToward(): handle is not a child of this node!");
+}
+
+/** @brief Gets the handle that goes in the direction opposite to the given adjacent node.
+ * Will abort with error if the given node is not adjacent. */
+Handle *Node::handleAwayFrom(Node *to)
+{
+    if (_next() == to) {
+        return back();
+    }
+    if (_prev() == to) {
+        return front();
+    }
+    g_error("Node::handleAwayFrom(): second node is not adjacent!");
+}
+
+/** @brief Gets the node in the direction opposite to the given handle.
+ * Will abort with error if the handle doesn't belong to this node. */
+Node *Node::nodeAwayFrom(Handle *h)
+{
+    if (front() == h) {
+        return _prev();
+    }
+    if (back() == h) {
+        return _next();
+    }
+    g_error("Node::nodeAwayFrom(): handle is not a child of this node!");
+}
+
 Glib::ustring Node::_getTip(unsigned state)
 {
     if (state_held_shift(state)) {
@@ -1076,7 +1154,11 @@ Glib::ustring Node::_getTip(unsigned state)
             "<b>Ctrl:</b> move along axes, click to change node type");
     }
 
-    // assemble tip from node name
+    if (state_held_alt(state)) {
+        return C_("Path node tip", "<b>Alt:</b> sculpt nodes");
+    }
+
+    // No modifiers: assemble tip from node type
     char const *nodetype = node_type_to_localized_string(_type);
     if (_selection.transformHandlesEnabled() && selected()) {
         if (_selection.size() == 1) {
@@ -1149,9 +1231,9 @@ NodeList::NodeList(SubpathList &splist)
     : _list(splist)
     , _closed(false)
 {
-    this->list = this;
-    this->next = this;
-    this->prev = this;
+    this->ln_list = this;
+    this->ln_next = this;
+    this->ln_prev = this;
 }
 
 NodeList::~NodeList()
@@ -1161,13 +1243,13 @@ NodeList::~NodeList()
 
 bool NodeList::empty()
 {
-    return next == this;
+    return ln_next == this;
 }
 
 NodeList::size_type NodeList::size()
 {
     size_type sz = 0;
-    for (ListNode *ln = next; ln != this; ln = ln->next) ++sz;
+    for (ListNode *ln = ln_next; ln != this; ln = ln->ln_next) ++sz;
     return sz;
 }
 
@@ -1198,11 +1280,11 @@ NodeList::iterator NodeList::before(double t, double *fracpart)
 NodeList::iterator NodeList::insert(iterator i, Node *x)
 {
     ListNode *ins = i._node;
-    x->next = ins;
-    x->prev = ins->prev;
-    ins->prev->next = x;
-    ins->prev = x;
-    x->ListNode::list = this;
+    x->ln_next = ins;
+    x->ln_prev = ins->ln_prev;
+    ins->ln_prev->ln_next = x;
+    ins->ln_prev = x;
+    x->ln_list = this;
     return iterator(x);
 }
 
@@ -1218,51 +1300,51 @@ void NodeList::splice(iterator pos, NodeList &list, iterator i)
     splice(pos, list, i, j);
 }
 
-void NodeList::splice(iterator pos, NodeList &list, iterator first, iterator last)
+void NodeList::splice(iterator pos, NodeList &/*list*/, iterator first, iterator last)
 {
     ListNode *ins_beg = first._node, *ins_end = last._node, *at = pos._node;
-    for (ListNode *ln = ins_beg; ln != ins_end; ln = ln->next) {
-        ln->list = this;
+    for (ListNode *ln = ins_beg; ln != ins_end; ln = ln->ln_next) {
+        ln->ln_list = this;
     }
-    ins_beg->prev->next = ins_end;
-    ins_end->prev->next = at;
-    at->prev->next = ins_beg;
+    ins_beg->ln_prev->ln_next = ins_end;
+    ins_end->ln_prev->ln_next = at;
+    at->ln_prev->ln_next = ins_beg;
 
-    ListNode *atprev = at->prev;
-    at->prev = ins_end->prev;
-    ins_end->prev = ins_beg->prev;
-    ins_beg->prev = atprev;
+    ListNode *atprev = at->ln_prev;
+    at->ln_prev = ins_end->ln_prev;
+    ins_end->ln_prev = ins_beg->ln_prev;
+    ins_beg->ln_prev = atprev;
 }
 
 void NodeList::shift(int n)
 {
     // 1. make the list perfectly cyclic
-    next->prev = prev;
-    prev->next = next;
+    ln_next->ln_prev = ln_prev;
+    ln_prev->ln_next = ln_next;
     // 2. find new begin
-    ListNode *new_begin = next;
+    ListNode *new_begin = ln_next;
     if (n > 0) {
-        for (; n > 0; --n) new_begin = new_begin->next;
+        for (; n > 0; --n) new_begin = new_begin->ln_next;
     } else {
-        for (; n < 0; ++n) new_begin = new_begin->prev;
+        for (; n < 0; ++n) new_begin = new_begin->ln_prev;
     }
     // 3. relink begin to list
-    next = new_begin;
-    prev = new_begin->prev;
-    new_begin->prev->next = this;
-    new_begin->prev = this;
+    ln_next = new_begin;
+    ln_prev = new_begin->ln_prev;
+    new_begin->ln_prev->ln_next = this;
+    new_begin->ln_prev = this;
 }
 
 void NodeList::reverse()
 {
-    for (ListNode *ln = next; ln != this; ln = ln->prev) {
-        std::swap(ln->next, ln->prev);
+    for (ListNode *ln = ln_next; ln != this; ln = ln->ln_prev) {
+        std::swap(ln->ln_next, ln->ln_prev);
         Node *node = static_cast<Node*>(ln);
         Geom::Point save_pos = node->front()->position();
         node->front()->setPosition(node->back()->position());
         node->back()->setPosition(save_pos);
     }
-    std::swap(next, prev);
+    std::swap(ln_next, ln_prev);
 }
 
 void NodeList::clear()
@@ -1275,11 +1357,11 @@ NodeList::iterator NodeList::erase(iterator i)
     // some gymnastics are required to ensure that the node is valid when deleted;
     // otherwise the code that updates handle visibility will break
     Node *rm = static_cast<Node*>(i._node);
-    ListNode *rmnext = rm->next, *rmprev = rm->prev;
+    ListNode *rmnext = rm->ln_next, *rmprev = rm->ln_prev;
     ++i;
     delete rm;
-    rmprev->next = rmnext;
-    rmnext->prev = rmprev;
+    rmprev->ln_next = rmnext;
+    rmnext->ln_prev = rmprev;
     return i;
 }
 
@@ -1296,10 +1378,10 @@ void NodeList::kill()
 }
 
 NodeList &NodeList::get(Node *n) {
-    return *(n->list());
+    return n->nodeList();
 }
 NodeList &NodeList::get(iterator const &i) {
-    return *(i._node->list);
+    return *(i._node->ln_list);
 }