diff --git a/src/ui/tool/node.cpp b/src/ui/tool/node.cpp
index 5f792cfc74bb5fc8880ad5ef6d66f91c3bb1d343..e9fa79fb3bb4d3c1b8ac603675245a51e6cd6984 100644 (file)
--- a/src/ui/tool/node.cpp
+++ b/src/ui/tool/node.cpp
#include <boost/utility.hpp>
#include <glib.h>
#include <glib/gi18n.h>
+#include <2geom/bezier-utils.h>
#include <2geom/transforms.h>
-#include "ui/tool/event-utils.h"
-#include "ui/tool/node.h"
+
#include "display/sp-ctrlline.h"
#include "display/sp-canvas.h"
#include "display/sp-canvas-util.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "preferences.h"
+#include "snap.h"
+#include "snap-preferences.h"
#include "sp-metrics.h"
#include "sp-namedview.h"
+#include "ui/tool/control-point-selection.h"
+#include "ui/tool/event-utils.h"
+#include "ui/tool/multi-path-manipulator.h"
+#include "ui/tool/node.h"
+#include "ui/tool/path-manipulator.h"
namespace Inkscape {
-namespace UI {
+namespace UI {
static SelectableControlPoint::ColorSet node_colors = {
{
/**
* @class Handle
- * Represents a control point of a cubic Bezier curve in a path.
+ * @brief Control point of a cubic Bezier curve in a path.
+ *
+ * Handle keeps the node type invariant only for the opposite handle of the same node.
+ * Keeping the invariant on node moves is left to the %Node class.
*/
+Geom::Point Handle::_saved_other_pos(0, 0);
double Handle::_saved_length = 0.0;
bool Handle::_drag_out = false;
_cset = &handle_colors;
_handle_line = sp_canvas_item_new(data.handle_line_group, SP_TYPE_CTRLLINE, NULL);
setVisible(false);
- signal_grabbed.connect(
- sigc::bind_return(
- sigc::hide(
- sigc::mem_fun(*this, &Handle::_grabbedHandler)),
- false));
- signal_dragged.connect(
- sigc::hide<0>(
- sigc::mem_fun(*this, &Handle::_draggedHandler)));
- signal_ungrabbed.connect(
- sigc::hide(sigc::mem_fun(*this, &Handle::_ungrabbedHandler)));
}
Handle::~Handle()
{
- sp_canvas_item_hide(_handle_line);
+ //sp_canvas_item_hide(_handle_line);
gtk_object_destroy(GTK_OBJECT(_handle_line));
}
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
}
}
-void Handle::_grabbedHandler()
+bool Handle::grabbed(GdkEventMotion *)
{
+ _saved_other_pos = other()->position();
_saved_length = _drag_out ? 0 : length();
+ _pm()._handleGrabbed();
+ return false;
}
-void Handle::_draggedHandler(Geom::Point &new_pos, GdkEventMotion *event)
+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.
+ // with Ctrl, constrain to M_PI/rotationsnapsperpi increments from vertical
+ // and the original position.
if (held_control(*event)) {
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
int snaps = 2 * prefs->getIntLimited("/options/rotationsnapsperpi/value", 12, 1, 1000);
- Geom::Point origin = _last_drag_origin();
- Geom::Point rel_origin = origin - parent_pos;
- new_pos = parent_pos + Geom::constrain_angle(Geom::Point(0,0), new_pos - parent_pos, snaps,
- _drag_out ? Geom::Point(1,0) : Geom::unit_vector(rel_origin));
+
+ // note: if snapping to the original position is only desired in the original
+ // 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));
+ Geom::Point orig_pos = original_line.pointAt(original_line.nearestPoint(new_pos));
+
+ if (Geom::distance(snap_pos, new_pos) < Geom::distance(orig_pos, new_pos)) {
+ new_pos = snap_pos;
+ } 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);
+ }
}
- signal_update.emit();
+
+ // 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);
+ } else {
+ // restore the position
+ other()->setPosition(_saved_other_pos);
+ }
+ }
+ move(new_pos); // needed for correct update, even though it's redundant
+ _pm().update();
}
-void Handle::_ungrabbedHandler()
+void Handle::ungrabbed(GdkEventButton *event)
{
// hide the handle if it's less than dragtolerance away from the node
+ // TODO is this actually desired?
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
int drag_tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
-
+
Geom::Point dist = _desktop->d2w(_parent->position()) - _desktop->d2w(position());
if (dist.length() <= drag_tolerance) {
move(_parent->position());
}
+
+ // HACK: If the handle was dragged out, call parent's ungrabbed handler,
+ // so that transform handles reappear
+ if (_drag_out) {
+ _parent->ungrabbed(event);
+ }
_drag_out = false;
+
+ _pm()._handleUngrabbed();
+}
+
+bool Handle::clicked(GdkEventButton *event)
+{
+ _pm()._handleClicked(this, event);
+ return true;
+}
+
+Handle *Handle::other()
+{
+ 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();
+ if (can_shift_rotate) {
+ more = C_("Path handle tip", "more: Shift, Ctrl, Alt");
+ } else {
+ more = C_("Path handle tip", "more: Ctrl, Alt");
+ }
if (state_held_alt(state)) {
if (state_held_control(state)) {
- return format_tip(C_("Path handle tip",
- "<b>Ctrl+Alt</b>: preserve length and snap rotation angle to %f° increments"),
- snap_increment_degrees());
+ if (state_held_shift(state) && can_shift_rotate) {
+ return format_tip(C_("Path handle tip",
+ "<b>Shift+Ctrl+Alt</b>: preserve length and snap rotation angle to %g° "
+ "increments while rotating both handles"),
+ snap_increment_degrees());
+ } else {
+ return format_tip(C_("Path handle tip",
+ "<b>Ctrl+Alt</b>: preserve length and snap rotation angle to %g° increments"),
+ snap_increment_degrees());
+ }
} else {
- return C_("Path handle tip",
- "<b>Alt:</b> preserve handle length while dragging");
+ if (state_held_shift(state) && can_shift_rotate) {
+ return C_("Path handle tip",
+ "<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");
+ }
}
} else {
if (state_held_control(state)) {
- return format_tip(C_("Path handle tip",
- "<b>Ctrl:</b> snap rotation angle to %f° increments, click to retract"),
- snap_increment_degrees());
+ if (state_held_shift(state) && can_shift_rotate) {
+ return format_tip(C_("Path handle tip",
+ "<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>Ctrl:</b> snap rotation angle to %g° increments, click to retract"),
+ snap_increment_degrees());
+ }
+ } else if (state_held_shift(state) && can_shift_rotate) {
+ return C_("Path hande tip",
+ "<b>Shift</b>: rotate both handles by the same angle");
}
}
+
switch (_parent->type()) {
case NODE_AUTO:
- return C_("Path handle tip",
- "<b>Auto node handle:</b> drag to convert to smooth node");
+ return format_tip(C_("Path handle tip",
+ "<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 curve"),
- handle_type_to_localized_string(_parent->type()));
+ return format_tip(C_("Path handle tip",
+ "<b>%s:</b> drag to shape the segment (%s)"),
+ handle_type_to_localized_string(_parent->type()), more);
}
}
-Glib::ustring Handle::_getDragTip(GdkEventMotion *event)
+Glib::ustring Handle::_getDragTip(GdkEventMotion */*event*/)
{
Geom::Point dist = position() - _last_drag_origin();
// report angle in mathematical convention
GString *y = SP_PX_TO_METRIC_STRING(dist[Geom::Y], _desktop->namedview->getDefaultMetric());
GString *len = SP_PX_TO_METRIC_STRING(length(), _desktop->namedview->getDefaultMetric());
Glib::ustring ret = format_tip(C_("Path handle tip",
- "Move by %s, %s; angle %.2f°, length %s"), x->str, y->str, angle, len->str);
+ "Move handle by %s, %s; angle %.2f°, length %s"), x->str, y->str, angle, len->str);
g_string_free(x, TRUE);
g_string_free(y, TRUE);
g_string_free(len, TRUE);
/**
* @class Node
- * Represents a curve endpoint in an editable path.
+ * @brief Curve endpoint in an editable path.
+ *
+ * The method move() keeps node type invariants during translations.
*/
Node::Node(NodeSharedData const &data, Geom::Point const &initial_pos)
, _handles_shown(false)
{
// NOTE we do not set type here, because the handles are still degenerate
- // connect to own grabbed signal - dragging out handles
- signal_grabbed.connect(
- sigc::mem_fun(*this, &Node::_grabbedHandler));
- signal_dragged.connect( sigc::hide<0>(
- sigc::mem_fun(*this, &Node::_draggedHandler)));
}
// NOTE: not using iterators won't make this much quicker because iterators can be 100% inlined.
@@ -384,14 +479,14 @@ void Node::_fixNeighbors(Geom::Point const &old_pos, Geom::Point const &new_pos)
{
/* This method restores handle invariants for neighboring nodes,
* and invariants that are based on positions of those nodes for this one. */
-
+
/* Fix auto handles */
if (_type == NODE_AUTO) _updateAutoHandles();
if (old_pos != new_pos) {
if (_next() && _next()->_type == NODE_AUTO) _next()->_updateAutoHandles();
if (_prev() && _prev()->_type == NODE_AUTO) _prev()->_updateAutoHandles();
}
-
+
/* Fix smooth handles at the ends of linear segments.
* Rotate the appropriate handle to be colinear with the segment.
* If there is a smooth node at the other end of the segment, rotate it too. */
@@ -409,37 +504,38 @@ void Node::_fixNeighbors(Geom::Point const &old_pos, Geom::Point const &new_pos)
if (_type == NODE_SMOOTH && !handle->isDegenerate()) {
handle->setDirection(other->position(), new_pos);
- /*Geom::Point handle_delta = handle->position() - position();
- Geom::Point new_delta = Geom::unit_vector(new_direction) * handle_delta.length();
- handle->setPosition(position() + new_delta);*/
}
// also update the handle on the other end of the segment
if (other->_type == NODE_SMOOTH && !other_handle->isDegenerate()) {
other_handle->setDirection(new_pos, other->position());
- /*
- Geom::Point handle_delta2 = other_handle->position() - other->position();
- Geom::Point new_delta2 = Geom::unit_vector(new_direction) * handle_delta2.length();
- other_handle->setPosition(other->position() + new_delta2);*/
}
}
void Node::_updateAutoHandles()
{
// Recompute the position of automatic handles.
- if (!_prev() || !_next()) {
+ // For endnodes, retract both handles. (It's only possible to create an end auto node
+ // through the XML editor.)
+ if (isEndNode()) {
_front.retract();
_back.retract();
return;
}
- // TODO describe in detail what the code below does
+
+ // Auto nodes automaticaly adjust their handles to give an appearance of smoothness,
+ // no matter what their surroundings are.
Geom::Point vec_next = _next()->position() - position();
Geom::Point vec_prev = _prev()->position() - position();
double len_next = vec_next.length(), len_prev = vec_prev.length();
if (len_next > 0 && len_prev > 0) {
+ // "dir" is an unit vector perpendicular to the bisector of the angle created
+ // by the previous node, this auto node and the next node.
Geom::Point dir = Geom::unit_vector((len_prev / len_next) * vec_next - vec_prev);
+ // Handle lengths are equal to 1/3 of the distance from the adjacent node.
_back.setRelativePos(-dir * (len_prev / 3));
_front.setRelativePos(dir * (len_next / 3));
} else {
+ // If any of the adjacent nodes coincides, retract both handles.
_front.retract();
_back.retract();
}
// 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 (isDegenerate()) {
+ 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
+ if (_front.isDegenerate()) {
+ double dist = Geom::distance(_next()->position(), position());
+ _front.setRelativePos(Geom::unit_vector(-_back.relativePos()) * dist / 3);
+ }
+ if (_back.isDegenerate()) {
+ double dist = Geom::distance(_prev()->position(), position());
+ _back.setRelativePos(Geom::unit_vector(-_front.relativePos()) * dist / 3);
+ }
+ } else if (isDegenerate()) {
_updateAutoHandles();
} else if (_front.isDegenerate()) {
// if the front handle is degenerate and this...next is a line segment,
_back.setDirection(*_next(), *this);
} else if (_prev()) {
Geom::Point dir = direction(_back, *this);
- _front.setRelativePos((_prev()->position() - position()).length() / 3 * dir);
+ _front.setRelativePos(Geom::distance(_prev()->position(), position()) / 3 * dir);
}
} else if (_back.isDegenerate()) {
if (prev_line) {
_front.setDirection(*_prev(), *this);
} else if (_next()) {
Geom::Point dir = direction(_front, *this);
- _back.setRelativePos((_next()->position() - position()).length() / 3 * dir);
+ _back.setRelativePos(Geom::distance(_next()->position(), position()) / 3 * dir);
}
} else {
// both handles are extended. make colinear while keeping length
double len_next = vec_next.length(), len_prev = vec_prev.length();
double len = (len_next + len_prev) / 6; // take 1/3 of average
if (len == 0) return;
-
+
Geom::Point dir = Geom::unit_vector((len_prev / len_next) * vec_next - vec_prev);
_back.setRelativePos(-dir * len);
_front.setRelativePos(dir * len);
updateState();
}
+/** Pick the best type for this node, based on the position of its handles.
+ * This is what assigns types to nodes created using the pen tool. */
void Node::pickBestType()
{
_type = NODE_CUSP;
}
}
+/** Customized event handler to catch scroll events needed for selection grow/shrink. */
+bool Node::_eventHandler(GdkEvent *event)
+{
+ static NodeList::iterator origin;
+ static int dir;
+
+ switch (event->type)
+ {
+ case GDK_SCROLL:
+ if (event->scroll.direction == GDK_SCROLL_UP) {
+ dir = 1;
+ } else if (event->scroll.direction == GDK_SCROLL_DOWN) {
+ dir = -1;
+ } else break;
+ if (held_control(event->scroll)) {
+ _selection.spatialGrow(this, dir);
+ } else {
+ _linearGrow(dir);
+ }
+ return true;
+ default:
+ break;
+ }
+ return ControlPoint::_eventHandler(event);
+}
+
+// TODO Move this to 2Geom!
+static double bezier_length (Geom::Point a0, Geom::Point a1, Geom::Point a2, Geom::Point a3)
+{
+ double lower = Geom::distance(a0, a3);
+ double upper = Geom::distance(a0, a1) + Geom::distance(a1, a2) + Geom::distance(a2, a3);
+
+ if (upper - lower < Geom::EPSILON) return (lower + upper)/2;
+
+ Geom::Point // Casteljau subdivision
+ b0 = a0,
+ c0 = a3,
+ b1 = 0.5*(a0 + a1),
+ t0 = 0.5*(a1 + a2),
+ c1 = 0.5*(a2 + a3),
+ b2 = 0.5*(b1 + t0),
+ c2 = 0.5*(t0 + c1),
+ b3 = 0.5*(b2 + c2); // == c3
+ return bezier_length(b0, b1, b2, b3) + bezier_length(b3, c2, c1, c0);
+}
+
+/** Select or deselect a node in this node's subpath based on its path distance from this node.
+ * @param dir If negative, shrink selection by one node; if positive, grow by one node */
+void Node::_linearGrow(int dir)
+{
+ // Interestingly, we do not need any help from PathManipulator when doing linear grow.
+ // First handle the trivial case of growing over an unselected node.
+ if (!selected() && dir > 0) {
+ _selection.insert(this);
+ return;
+ }
+
+ NodeList::iterator this_iter = NodeList::get_iterator(this);
+ NodeList::iterator fwd = this_iter, rev = this_iter;
+ double distance_back = 0, distance_front = 0;
+
+ // Linear grow is simple. We find the first unselected nodes in each direction
+ // and compare the linear distances to them.
+ if (dir > 0) {
+ if (!selected()) {
+ _selection.insert(this);
+ return;
+ }
+
+ // find first unselected nodes on both sides
+ while (fwd && fwd->selected()) {
+ NodeList::iterator n = fwd.next();
+ distance_front += bezier_length(*fwd, fwd->_front, n->_back, *n);
+ fwd = n;
+ if (fwd == this_iter)
+ // there is no unselected node in this cyclic subpath
+ return;
+ }
+ // do the same for the second direction. Do not check for equality with
+ // this node, because there is at least one unselected node in the subpath,
+ // so we are guaranteed to stop.
+ while (rev && rev->selected()) {
+ NodeList::iterator p = rev.prev();
+ distance_back += bezier_length(*rev, rev->_back, p->_front, *p);
+ rev = p;
+ }
+
+ NodeList::iterator t; // node to select
+ if (fwd && rev) {
+ if (distance_front <= distance_back) t = fwd;
+ else t = rev;
+ } else {
+ if (fwd) t = fwd;
+ if (rev) t = rev;
+ }
+ if (t) _selection.insert(t.ptr());
+
+ // Linear shrink is more complicated. We need to find the farthest selected node.
+ // This means we have to check the entire subpath. We go in the direction in which
+ // the distance we traveled is lower. We do this until we run out of nodes (ends of path)
+ // or the two iterators meet. On the way, we store the last selected node and its distance
+ // in each direction (if any). At the end, we choose the one that is farther and deselect it.
+ } else {
+ // both iterators that store last selected nodes are initially empty
+ NodeList::iterator last_fwd, last_rev;
+ double last_distance_back = 0, last_distance_front = 0;
+
+ while (rev || fwd) {
+ if (fwd && (!rev || distance_front <= distance_back)) {
+ if (fwd->selected()) {
+ last_fwd = fwd;
+ last_distance_front = distance_front;
+ }
+ NodeList::iterator n = fwd.next();
+ if (n) distance_front += bezier_length(*fwd, fwd->_front, n->_back, *n);
+ fwd = n;
+ } else if (rev && (!fwd || distance_front > distance_back)) {
+ if (rev->selected()) {
+ last_rev = rev;
+ last_distance_back = distance_back;
+ }
+ NodeList::iterator p = rev.prev();
+ if (p) distance_back += bezier_length(*rev, rev->_back, p->_front, *p);
+ rev = p;
+ }
+ // Check whether we walked the entire cyclic subpath.
+ // This is initially true because both iterators start from this node,
+ // so this check cannot go in the while condition.
+ // When this happens, we need to check the last node, pointed to by the iterators.
+ if (fwd && fwd == rev) {
+ if (!fwd->selected()) break;
+ NodeList::iterator fwdp = fwd.prev(), revn = rev.next();
+ double df = distance_front + bezier_length(*fwdp, fwdp->_front, fwd->_back, *fwd);
+ double db = distance_back + bezier_length(*revn, revn->_back, rev->_front, *rev);
+ if (df > db) {
+ last_fwd = fwd;
+ last_distance_front = df;
+ } else {
+ last_rev = rev;
+ last_distance_back = db;
+ }
+ break;
+ }
+ }
+
+ NodeList::iterator t;
+ if (last_fwd && last_rev) {
+ if (last_distance_front >= last_distance_back) t = last_fwd;
+ else t = last_rev;
+ } else {
+ if (last_fwd) t = last_fwd;
+ if (last_rev) t = last_rev;
+ }
+ if (t) _selection.erase(t.ptr());
+ }
+}
+
void Node::_setState(State state)
{
// change node size to match type and selection state
SelectableControlPoint::_setState(state);
}
-bool Node::_grabbedHandler(GdkEventMotion *event)
+bool Node::grabbed(GdkEventMotion *event)
{
- // dragging out handles
+ if (SelectableControlPoint::grabbed(event))
+ return true;
+
+ // Dragging out handles with Shift + drag on a node.
if (!held_shift(*event)) return false;
Handle *h;
Geom::Point evp = event_point(*event);
Geom::Point rel_evp = evp - _last_click_event_point();
- // this should work even if dragtolerance is zero and evp coincides with node position
+ // This should work even if dragtolerance is zero and evp coincides with node position.
double angle_next = HUGE_VAL;
double angle_prev = HUGE_VAL;
bool has_degenerate = false;
return true;
}
-void Node::_draggedHandler(Geom::Point &new_pos, GdkEventMotion *event)
+void Node::dragged(Geom::Point &new_pos, GdkEventMotion *event)
{
- if (!held_control(*event)) return;
- if (held_alt(*event)) {
- // with Ctrl+Alt, constrain to handle lines
- // project the new position onto a handle line that is closer
+ // For a note on how snapping is implemented in Inkscape, see snap.h.
+ SnapManager &sm = _desktop->namedview->snap_manager;
+ bool snap = sm.someSnapperMightSnap();
+ std::vector<Inkscape::SnapCandidatePoint> unselected;
+ if (snap) {
+ /* setup
+ * TODO We are doing this every time a snap happens. It should once be done only once
+ * per drag - maybe in the grabbed handler?
+ * TODO Unselected nodes vector must be valid during the snap run, because it is not
+ * copied. Fix this in snap.h and snap.cpp, then the above.
+ * TODO Snapping to unselected segments of selected paths doesn't work yet. */
+
+ // Build the list of unselected nodes.
+ typedef ControlPointSelection::Set Set;
+ Set &nodes = _selection.allPoints();
+ for (Set::iterator i = nodes.begin(); i != nodes.end(); ++i) {
+ if (!(*i)->selected()) {
+ Node *n = static_cast<Node*>(*i);
+ Inkscape::SnapCandidatePoint p(n->position(), n->_snapSourceType(), n->_snapTargetType());
+ unselected.push_back(p);
+ }
+ }
+ sm.setupIgnoreSelection(_desktop, false, &unselected);
+ }
+
+ if (held_control(*event)) {
Geom::Point origin = _last_drag_origin();
- Geom::Line line_front(origin, origin + _front.relativePos());
- Geom::Line line_back(origin, origin + _back.relativePos());
- double dist_front, dist_back;
- dist_front = Geom::distance(new_pos, line_front);
- dist_back = Geom::distance(new_pos, line_back);
- if (dist_front < dist_back) {
- new_pos = Geom::projection(new_pos, line_front);
+ Inkscape::SnappedPoint fp, bp;
+ 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;
+ if (_front.isDegenerate()) {
+ if (_is_line_segment(this, _next()))
+ front_point = _next()->position() - origin;
+ } else {
+ front_point = _front.relativePos();
+ }
+ if (_back.isDegenerate()) {
+ if (_is_line_segment(_prev(), this))
+ back_point = _prev()->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(new_pos,
+ _snapSourceType()), *line_front);
+ }
+ if (line_back) {
+ bp = sm.constrainedSnap(Inkscape::SnapCandidatePoint(new_pos,
+ _snapSourceType()), *line_back);
+ }
+ }
+ if (fp.getSnapped() || bp.getSnapped()) {
+ if (fp.isOtherSnapBetter(bp, false)) {
+ fp = bp;
+ }
+ fp.getPoint(new_pos);
+ _desktop->snapindicator->set_new_snaptarget(fp);
+ } 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;
+ }
+ }
} else {
- new_pos = Geom::projection(new_pos, line_back);
+ // 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(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);
+ if (fp.getTarget() != SNAPTARGET_CONSTRAINT) {
+ _desktop->snapindicator->set_new_snaptarget(fp);
+ }
+ } 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];
+ }
}
- } else {
- // with Ctrl, constrain to axes
- // TODO this probably has to be separated into an AxisConstrainablePoint class
- // TODO maybe add diagonals when the distance from origin is large enough?
- 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];
+ } else if (snap) {
+ 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);
+}
+
+bool Node::clicked(GdkEventButton *event)
+{
+ if(_pm()._nodeClicked(this, event))
+ return true;
+ return SelectableControlPoint::clicked(event);
+}
+
+Inkscape::SnapSourceType Node::_snapSourceType()
+{
+ if (_type == NODE_SMOOTH || _type == NODE_AUTO)
+ return SNAPSOURCE_NODE_SMOOTH;
+ return SNAPSOURCE_NODE_CUSP;
+}
+Inkscape::SnapTargetType Node::_snapTargetType()
+{
+ if (_type == NODE_SMOOTH || _type == NODE_AUTO)
+ return SNAPTARGET_NODE_SMOOTH;
+ 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)) {
- if ((_next() && _front.isDegenerate()) || (_prev() && _back.isDegenerate())) {
- if (state_held_control(state)) {
+ bool can_drag_out = (_next() && _front.isDegenerate()) || (_prev() && _back.isDegenerate());
+ if (can_drag_out) {
+ /*if (state_held_control(state)) {
return format_tip(C_("Path node tip",
"<b>Shift+Ctrl:</b> drag out a handle and snap its angle "
"to %f° increments"), snap_increment_degrees());
- }
+ }*/
return C_("Path node tip",
"<b>Shift:</b> drag out a handle, click to toggle selection");
}
- return C_("Path node statusbar 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");
+ 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");
}
-
- // 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) {
+ return format_tip(C_("Path node tip",
+ "<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);
+ }
return format_tip(C_("Path node tip",
- "<b>%s:</b> drag to shape the path, click to select this node"), 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)
+Glib::ustring Node::_getDragTip(GdkEventMotion */*event*/)
{
Geom::Point dist = position() - _last_drag_origin();
GString *x = SP_PX_TO_METRIC_STRING(dist[Geom::X], _desktop->namedview->getDefaultMetric());
GString *y = SP_PX_TO_METRIC_STRING(dist[Geom::Y], _desktop->namedview->getDefaultMetric());
- Glib::ustring ret = format_tip(C_("Path node statusbar tip", "Move by %s, %s"),
+ Glib::ustring ret = format_tip(C_("Path node tip", "Move node by %s, %s"),
x->str, y->str);
g_string_free(x, TRUE);
g_string_free(y, TRUE);
* It can optionally be cyclic to represent a closed path.
* The list has iterators that act like plain node iterators, but can also be used
* to obtain shared pointers to nodes.
- *
- * @todo Manage geometric representation to improve speed
*/
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()
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;
}
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;
- _list.signal_insert_node.emit(x);
+ 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);
}
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) {
- list._list.signal_remove_node.emit(static_cast<Node*>(ln));
- ln->list = this;
- _list.signal_insert_node.emit(static_cast<Node*>(ln));
+ 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;
-
- ListNode *atprev = at->prev;
- at->prev = ins_end->prev;
- ins_end->prev = ins_beg->prev;
- ins_beg->prev = atprev;
+ 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->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()
NodeList::iterator NodeList::erase(iterator i)
{
- // some acrobatics are required to ensure that the node is valid when deleted;
+ // 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;
- _list.signal_remove_node.emit(rm);
delete rm;
- rmprev->next = rmnext;
- rmnext->prev = rmprev;
+ rmprev->ln_next = rmnext;
+ rmnext->ln_prev = rmprev;
return i;
}
+// TODO this method is very ugly!
+// converting SubpathList to an intrusive list might allow us to get rid of it
void NodeList::kill()
{
for (SubpathList::iterator i = _list.begin(); i != _list.end(); ++i) {
}
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);
}