diff --git a/src/ui/tool/node.cpp b/src/ui/tool/node.cpp
index b72da1374b7b7918f2d6e9b82ebe45289679ac82..fea02d399f64c01cbd30bfcee4948736eadaa1b4 100644 (file)
--- a/src/ui/tool/node.cpp
+++ b/src/ui/tool/node.cpp
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
+ // The handle becomes degenerate.
+ // Adjust node type as necessary.
+ if (other->isDegenerate()) {
+ // If both handles become degenerate, convert to parent cusp node
+ _parent->setType(NODE_CUSP, false);
+ } else {
+ // Only 1 handle becomes degenerate
+ switch (_parent->type()) {
+ case NODE_AUTO:
+ case NODE_SYMMETRIC:
+ _parent->setType(NODE_SMOOTH, false);
+ break;
+ default:
+ // do nothing for other node types
+ break;
+ }
+ }
+ // If the segment between the handle and the node
// in its direction becomes linear and there are smooth nodes
// at its ends, make their handles colinear with the segment
- if (towards && towards->isDegenerate()) {
+ if (towards && towards_second->isDegenerate()) {
if (node_towards->type() == NODE_SMOOTH) {
- towards_second->setDirection(*_parent, *node_towards);
+ towards->setDirection(*_parent, *node_towards);
}
if (_parent->type() == NODE_SMOOTH) {
other->setDirection(*node_towards, *_parent);
void Handle::setPosition(Geom::Point const &p)
{
+ Geom::Point old_pos = position();
ControlPoint::setPosition(p);
sp_ctrlline_set_coords(SP_CTRLLINE(_handle_line), _parent->position(), position());
if (Geom::are_near(position(), _parent->position()))
_degenerate = true;
else _degenerate = false;
+
if (_parent->_handles_shown && _parent->visible() && !_degenerate) {
setVisible(true);
} else {
setVisible(false);
}
- // If both handles become degenerate, convert to parent cusp node
- if (_parent->isDegenerate()) {
- _parent->setType(NODE_CUSP, false);
- }
}
void Handle::setLength(double len)
void Handle::retract()
{
- setPosition(_parent->position());
+ move(_parent->position());
}
void Handle::setDirection(Geom::Point const &from, Geom::Point const &to)
}
}
+bool Handle::_eventHandler(GdkEvent *event)
+{
+ switch (event->type)
+ {
+ case GDK_KEY_PRESS:
+ switch (shortcut_key(event->key))
+ {
+ case GDK_s:
+ case GDK_S:
+ if (held_only_shift(event->key) && _parent->_type == NODE_CUSP) {
+ // when Shift+S is pressed when hovering over a handle belonging to a cusp node,
+ // hold this handle in place; otherwise process normally
+ // this handle is guaranteed not to be degenerate
+ other()->move(_parent->position() - (position() - _parent->position()));
+ _parent->setType(NODE_SMOOTH, false);
+ _parent->_pm().update(); // magic triple combo to add undo event
+ _parent->_pm().writeXML();
+ _parent->_pm()._commit(_("Change node type"));
+ return true;
+ }
+ break;
+ default: break;
+ }
+ default: break;
+ }
+
+ return ControlPoint::_eventHandler(event);
+}
+
bool Handle::grabbed(GdkEventMotion *)
{
- _saved_other_pos = other().position();
+ _saved_other_pos = other()->position();
_saved_length = _drag_out ? 0 : length();
_pm()._handleGrabbed();
return false;
{
Geom::Point parent_pos = _parent->position();
Geom::Point origin = _last_drag_origin();
+ SnapManager &sm = _desktop->namedview->snap_manager;
+ bool snap = sm.someSnapperMightSnap();
+ boost::optional<Inkscape::Snapper::SnapConstraint> ctrl_constraint;
+
// 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.
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::Line perp_line(parent_pos, parent_pos + Geom::rot90(origin - parent_pos));
Geom::Point snap_pos = parent_pos + Geom::constrain_angle(
Geom::Point(0,0), new_pos - parent_pos, snaps, Geom::Point(1,0));
Geom::Point orig_pos = original_line.pointAt(original_line.nearestPoint(new_pos));
+ Geom::Point perp_pos = perp_line.pointAt(perp_line.nearestPoint(new_pos));
- if (Geom::distance(snap_pos, new_pos) < Geom::distance(orig_pos, new_pos)) {
- new_pos = snap_pos;
+ Geom::Point result = snap_pos;
+ ctrl_constraint = Inkscape::Snapper::SnapConstraint(parent_pos, parent_pos - snap_pos);
+ if (Geom::distance(orig_pos, new_pos) < Geom::distance(result, new_pos)) {
+ result = orig_pos;
+ ctrl_constraint = Inkscape::Snapper::SnapConstraint(parent_pos, parent_pos - orig_pos);
+ }
+ if (Geom::distance(perp_pos, new_pos) < Geom::distance(result, new_pos)) {
+ result = perp_pos;
+ ctrl_constraint = Inkscape::Snapper::SnapConstraint(parent_pos, parent_pos - perp_pos);
+ }
+ new_pos = result;
+ }
+
+ 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 = _parent->nodeAwayFrom(this);
+ if (_parent->type() == NODE_SMOOTH && Node::_is_line_segment(_parent, node_away)) {
+ Inkscape::Snapper::SnapConstraint cl(_parent->position(),
+ _parent->position() - node_away->position());
+ Inkscape::SnappedPoint p;
+ p = sm.constrainedSnap(Inkscape::SnapCandidatePoint(new_pos, SNAPSOURCE_NODE_HANDLE), cl);
+ new_pos = p.getPoint();
+ } else if (ctrl_constraint) {
+ // NOTE: this is subtly wrong.
+ // We should get all possible constraints and snap along them using
+ // multipleConstrainedSnaps, instead of first snapping to angle and the to objects
+ Inkscape::SnappedPoint p;
+ p = sm.constrainedSnap(Inkscape::SnapCandidatePoint(new_pos, SNAPSOURCE_NODE_HANDLE), *ctrl_constraint);
+ new_pos = p.getPoint();
} else {
- new_pos = orig_pos;
+ sm.freeSnapReturnByRef(new_pos, SNAPSOURCE_NODE_HANDLE);
}
+ sm.unSetup();
}
+
+
// 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();
}
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() {
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 {
} else {
if (state_held_shift(state) && can_shift_rotate) {
return C_("Path handle tip",
- "<b>Shift+Alt:</b> preserve handle length and rotate both handles");
+ "<b>Shift+Alt</b>: preserve handle length and rotate both handles");
} else {
return C_("Path handle tip",
- "<b>Alt:</b> preserve handle length while dragging");
+ "<b>Alt</b>: preserve handle length while dragging");
}
}
} else {
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) {
switch (_parent->type()) {
case NODE_AUTO:
return format_tip(C_("Path handle tip",
- "<b>Auto node handle:</b> drag to convert to smooth node (%s)"), more);
+ "<b>Auto node handle</b>: drag to convert to smooth node (%s)"), more);
default:
return format_tip(C_("Path handle tip",
- "<b>%s:</b> drag to shape the segment (%s)"),
+ "<b>%s</b>: drag to shape the segment (%s)"),
handle_type_to_localized_string(_parent->type()), more);
}
}
if (update_handles) {
switch (type) {
case NODE_CUSP:
- // if the existing type is also NODE_CUSP, retract handles
- if (_type == NODE_CUSP) {
- _front.retract();
- _back.retract();
- }
+ // nothing to do
break;
case NODE_AUTO:
// auto handles make no sense for endnodes
_updateAutoHandles();
break;
case NODE_SMOOTH: {
+ // ignore attempts to make smooth endnodes.
+ if (isEndNode()) return;
// rotate handles to be colinear
// for degenerate nodes set positions like auto handles
bool prev_line = _is_line_segment(_prev(), this);
bool next_line = _is_line_segment(this, _next());
if (_type == NODE_SMOOTH) {
- // for a node that is already smooth and has a degenerate handle,
- // drag out the second handle to 1/3 the length of the linear segment
+ // For a node that is already smooth and has a degenerate handle,
+ // drag out the second handle without changing the direction of the first one.
if (_front.isDegenerate()) {
double dist = Geom::distance(_next()->position(), position());
_front.setRelativePos(Geom::unit_vector(-_back.relativePos()) * dist / 3);
/** Customized event handler to catch scroll events needed for selection grow/shrink. */
bool Node::_eventHandler(GdkEvent *event)
{
- static NodeList::iterator origin;
- static int dir;
+ int dir = 0;
switch (event->type)
{
dir = -1;
} else break;
if (held_control(event->scroll)) {
- _selection.spatialGrow(this, dir);
+ _linearGrow(dir);
} else {
+ _selection.spatialGrow(this, dir);
+ }
+ return true;
+ case GDK_KEY_PRESS:
+ switch (shortcut_key(event->key))
+ {
+ case GDK_Page_Up:
+ dir = 1;
+ break;
+ case GDK_Page_Down:
+ dir = -1;
+ break;
+ default: goto bail_out;
+ }
+
+ if (held_control(event->key)) {
_linearGrow(dir);
+ } else {
+ _selection.spatialGrow(this, dir);
}
return true;
default:
break;
}
+
+ bail_out:
return ControlPoint::_eventHandler(event);
}
{
// For a note on how snapping is implemented in Inkscape, see snap.h.
SnapManager &sm = _desktop->namedview->snap_manager;
- bool snap = sm.someSnapperMightSnap();
+ // even if we won't really snap, we might still call the one of the
+ // constrainedSnap() methods to enforce the constraints, so we need
+ // to setup the snapmanager anyway; this is also required for someSnapperMightSnap()
+ sm.setup(_desktop);
+
+ // do not snap when Shift is pressed
+ bool snap = !held_shift(*event) && sm.someSnapperMightSnap();
+
+ Inkscape::SnappedPoint sp;
std::vector<Inkscape::SnapCandidatePoint> unselected;
if (snap) {
/* setup
unselected.push_back(p);
}
}
+ sm.unSetup();
sm.setupIgnoreSelection(_desktop, true, &unselected);
}
if (held_control(*event)) {
Geom::Point origin = _last_drag_origin();
- Inkscape::SnappedPoint fp, bp;
+ std::vector<Inkscape::Snapper::SnapConstraint> constraints;
if (held_alt(*event)) {
// with Ctrl+Alt, constrain to handle lines
- // project the new position onto a handle line that is closer
- boost::optional<Geom::Point> front_point, back_point;
- boost::optional<Inkscape::Snapper::ConstraintLine> line_front, line_back;
+ // project the new position onto a handle line that is closer;
+ // also snap to perpendiculars of handle lines
+
+ Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+ int snaps = prefs->getIntLimited("/options/rotationsnapsperpi/value", 12, 1, 1000);
+ double min_angle = M_PI / snaps;
+
+ boost::optional<Geom::Point> front_point, back_point, fperp_point, bperp_point;
if (_front.isDegenerate()) {
if (_is_line_segment(this, _next()))
front_point = _next()->position() - origin;
} else {
back_point = _back.relativePos();
}
- if (front_point)
- line_front = Inkscape::Snapper::ConstraintLine(origin, *front_point);
- if (back_point)
- line_back = Inkscape::Snapper::ConstraintLine(origin, *back_point);
-
- // 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(),
- _snapSourceType()), *line_front);
- }
- if (line_back) {
- bp = sm.constrainedSnap(Inkscape::SnapCandidatePoint(position(),
- _snapSourceType()), *line_back);
- }
+ if (front_point) {
+ constraints.push_back(Inkscape::Snapper::SnapConstraint(origin, *front_point));
+ fperp_point = Geom::rot90(*front_point);
}
- if (fp.getSnapped() || bp.getSnapped()) {
- if (fp.isOtherSnapBetter(bp, false)) {
- bp.getPoint(new_pos);
- } else {
- fp.getPoint(new_pos);
- }
- } else {
- boost::optional<Geom::Point> pos;
- if (line_front) {
- pos = line_front->projection(new_pos);
- }
- if (line_back) {
- Geom::Point pos2 = line_back->projection(new_pos);
- if (!pos || (pos && Geom::distance(new_pos, *pos) > Geom::distance(new_pos, pos2)))
- pos = pos2;
- }
- if (pos) {
- new_pos = *pos;
- } else {
- new_pos = origin;
- }
+ if (back_point) {
+ constraints.push_back(Inkscape::Snapper::SnapConstraint(origin, *back_point));
+ bperp_point = Geom::rot90(*back_point);
}
- } else {
- // with Ctrl, constrain to axes
- // TODO combine the two branches
- 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);
+ // perpendiculars only snap when they are further than snap increment away
+ // from the second handle constraint
+ if (fperp_point && (!back_point ||
+ (fabs(Geom::angle_between(*fperp_point, *back_point)) > min_angle &&
+ fabs(Geom::angle_between(*fperp_point, *back_point)) < M_PI - min_angle)))
+ {
+ constraints.push_back(Inkscape::Snapper::SnapConstraint(origin, *fperp_point));
}
- if (fp.getSnapped() || bp.getSnapped()) {
- if (fp.isOtherSnapBetter(bp, false)) {
- fp = bp;
- }
- fp.getPoint(new_pos);
- } else {
- Geom::Point origin = _last_drag_origin();
- Geom::Point delta = new_pos - origin;
- Geom::Dim2 d = (fabs(delta[Geom::X]) < fabs(delta[Geom::Y])) ? Geom::X : Geom::Y;
- new_pos[d] = origin[d];
+ if (bperp_point && (!front_point ||
+ (fabs(Geom::angle_between(*bperp_point, *front_point)) > min_angle &&
+ fabs(Geom::angle_between(*bperp_point, *front_point)) < M_PI - min_angle)))
+ {
+ constraints.push_back(Inkscape::Snapper::SnapConstraint(origin, *bperp_point));
}
+
+ sp = sm.multipleConstrainedSnaps(Inkscape::SnapCandidatePoint(new_pos, _snapSourceType()), constraints, held_shift(*event));
+ } else {
+ // with Ctrl, constrain to axes
+ constraints.push_back(Inkscape::Snapper::SnapConstraint(origin, Geom::Point(1, 0)));
+ constraints.push_back(Inkscape::Snapper::SnapConstraint(origin, Geom::Point(0, 1)));
+ sp = sm.multipleConstrainedSnaps(Inkscape::SnapCandidatePoint(new_pos, _snapSourceType()), constraints, held_shift(*event));
}
+ new_pos = sp.getPoint();
} else if (snap) {
- sm.freeSnapReturnByRef(new_pos, _snapSourceType());
+ sp = sm.freeSnap(Inkscape::SnapCandidatePoint(new_pos, _snapSourceType()));
+ new_pos = sp.getPoint();
}
+ sm.unSetup();
+
SelectableControlPoint::dragged(new_pos, event);
}
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)) {
"to %f° increments"), snap_increment_degrees());
}*/
return C_("Path node tip",
- "<b>Shift:</b> drag out a handle, click to toggle selection");
+ "<b>Shift</b>: drag out a handle, click to toggle selection");
}
- return C_("Path node tip", "<b>Shift:</b> click to toggle selection");
+ return C_("Path node tip", "<b>Shift</b>: click to toggle selection");
}
if (state_held_control(state)) {
if (state_held_alt(state)) {
- return C_("Path node tip", "<b>Ctrl+Alt:</b> move along handle lines, click to delete node");
+ return C_("Path node tip", "<b>Ctrl+Alt</b>: move along handle lines, click to delete node");
}
return C_("Path node tip",
- "<b>Ctrl:</b> move along axes, click to change node type");
+ "<b>Ctrl</b>: move along axes, click to change node type");
+ }
+
+ if (state_held_alt(state)) {
+ return C_("Path node tip", "<b>Alt</b>: sculpt nodes");
}
- // assemble tip from node name
+ // No modifiers: assemble tip from node type
char const *nodetype = node_type_to_localized_string(_type);
if (_selection.transformHandlesEnabled() && selected()) {
if (_selection.size() == 1) {
return format_tip(C_("Path node tip",
- "<b>%s:</b> drag to shape the path (more: Shift, Ctrl, Alt)"), nodetype);
+ "<b>%s</b>: drag to shape the path (more: Shift, Ctrl, Alt)"), nodetype);
}
return format_tip(C_("Path node tip",
- "<b>%s:</b> drag to shape the path, click to toggle scale/rotation handles (more: Shift, Ctrl, Alt)"), nodetype);
+ "<b>%s</b>: drag to shape the path, click to toggle scale/rotation handles (more: Shift, Ctrl, Alt)"), nodetype);
}
return format_tip(C_("Path node tip",
- "<b>%s:</b> drag to shape the path, click to select only this node (more: Shift, Ctrl, Alt)"), nodetype);
+ "<b>%s</b>: drag to shape the path, click to select only this node (more: Shift, Ctrl, Alt)"), nodetype);
}
Glib::ustring Node::_getDragTip(GdkEventMotion */*event*/)
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 :