From: buliabyak Date: Fri, 12 May 2006 19:28:52 +0000 (+0000) Subject: fix undo bug in nodepath_preserve; fix crash caused by nodepath_cleanup deleting... X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=2c9c6838241516364d68db0d30520e18f70fc70d;p=inkscape.git fix undo bug in nodepath_preserve; fix crash caused by nodepath_cleanup deleting subpaths from under our feet; rename origin to origin_radial for node sides; add x/y origins to sides and remember origins for all nodes and sides upon grabbing; store drag pressure in the knot; sculpt --- diff --git a/src/knot.cpp b/src/knot.cpp index 40246ce01..3ec2799bd 100644 --- a/src/knot.cpp +++ b/src/knot.cpp @@ -195,6 +195,7 @@ static void sp_knot_init(SPKnot *knot) knot->mode = SP_KNOT_MODE_XOR; knot->tip = NULL; knot->_event_handler_id = 0; + knot->pressure = 0; knot->fill[SP_KNOT_STATE_NORMAL] = 0xffffff00; knot->fill[SP_KNOT_STATE_MOUSEOVER] = 0xff0000ff; @@ -318,6 +319,7 @@ static int sp_knot_handler(SPCanvasItem *item, GdkEvent *event, SPKnot *knot) break; case GDK_BUTTON_RELEASE: if (event->button.button == 1) { + knot->pressure = 0; if (transform_escaped) { transform_escaped = false; consumed = TRUE; @@ -359,6 +361,11 @@ static int sp_knot_handler(SPCanvasItem *item, GdkEvent *event, SPKnot *knot) // motion notify coordinates as given (no snapping back to origin) within_tolerance = false; + if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &knot->pressure)) + knot->pressure = CLAMP (knot->pressure, 0, 1); + else + knot->pressure = 0.5; + if (!moved) { g_signal_emit(knot, knot_signals[GRABBED], 0, diff --git a/src/knot.h b/src/knot.h index f948b3dfc..bba49f3e7 100644 --- a/src/knot.h +++ b/src/knot.h @@ -63,6 +63,8 @@ struct SPKnot : GObject { gulong _event_handler_id; + double pressure; /**< The tablet pen pressure when the knot is being dragged. */ + //TODO: all the members above should eventualle become private, accessible via setters/getters inline void setSize (guint i) {size = i;} inline void setShape (guint i) {shape = (SPKnotShapeType) i;} diff --git a/src/nodepath.cpp b/src/nodepath.cpp index a5fd2fab8..9be8b4ff2 100644 --- a/src/nodepath.cpp +++ b/src/nodepath.cpp @@ -7,7 +7,7 @@ * Lauris Kaplinski * bulia byak * - * This code is in public domain + * Portions of this code are in public domain; node sculpting functions written by bulia byak are under GNU GPL */ #ifdef HAVE_CONFIG_H @@ -281,10 +281,10 @@ static void sp_nodepath_cleanup(Inkscape::NodePath::Path *nodepath) { GList *badSubPaths = NULL; - //Check all subpaths to be >=2 nodes + //Check all closed subpaths to be >=1 nodes, all open subpaths to be >= 2 nodes for (GList *l = nodepath->subpaths; l ; l=l->next) { Inkscape::NodePath::SubPath *sp = (Inkscape::NodePath::SubPath *)l->data; - if (sp_nodepath_subpath_get_node_count(sp)<2) + if ((sp_nodepath_subpath_get_node_count(sp)<2 && !sp->closed) || (sp_nodepath_subpath_get_node_count(sp)<1 && sp->closed)) badSubPaths = g_list_append(badSubPaths, sp); } @@ -947,6 +947,157 @@ static void sp_nodepath_selected_nodes_move(Inkscape::NodePath::Path *nodepath, update_object(nodepath); } +/** +Function mapping x (in the range 0..1) to y (in the range 1..0) using a smooth half-bell-like +curve; the parameter alpha determines how blunt (alpha > 1) or sharp (alpha < 1) will be the curve +near x = 0. + */ +double +sculpt_profile (double x, double alpha) +{ + if (x >= 1) + return 0; + return (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5); +} + +double +bezier_length (NR::Point a, NR::Point ah, NR::Point bh, NR::Point b) +{ + // extremely primitive for now, don't have time to look for the real one + double lower = NR::L2(b - a); + double upper = NR::L2(ah - a) + NR::L2(bh - ah) + NR::L2(bh - b); + return (lower + upper)/2; +} + +void +sp_nodepath_move_node_and_handles (Inkscape::NodePath::Node *n, NR::Point delta, NR::Point delta_n, NR::Point delta_p) +{ + n->pos = n->origin + delta; + n->n.pos = n->n.origin + delta_n; + n->p.pos = n->p.origin + delta_p; + sp_node_adjust_handles(n); + sp_node_update_handles(n, false); +} + +/** + * Displace selected nodes and their handles by fractions of delta (from their origins), depending + * on how far they are from the dragged node n. + */ +static void +sp_nodepath_selected_nodes_sculpt(Inkscape::NodePath::Path *nodepath, Inkscape::NodePath::Node *n, NR::Point delta) +{ + g_assert (n); + g_assert (nodepath); + g_assert (n->subpath->nodepath == nodepath); + + double pressure = n->knot->pressure; + if (pressure == 0) + pressure = 0.5; // default + pressure = CLAMP (pressure, 0.2, 0.8); + + // map pressure to alpha = 1/5 ... 5 + double alpha = 1 - 2 * fabs(pressure - 0.5); + if (pressure > 0.5) + alpha = 1/alpha; + + double n_sel_range = 0, p_sel_range = 0; + guint n_nodes = 0, p_nodes = 0; + guint n_sel_nodes = 0, p_sel_nodes = 0; + + // First pass: calculate ranges (TODO: we could cache them, as they don't change while dragging) + { + double n_range = 0, p_range = 0; + bool n_going = true, p_going = true; + Inkscape::NodePath::Node *n_node = n; + Inkscape::NodePath::Node *p_node = n; + do { + // Do one step in both directions from n, until reaching the end of subpath or bumping into each other + if (n_node && n_going) + n_node = n_node->n.other; + if (n_node == NULL) { + n_going = false; + } else { + n_nodes ++; + n_range += bezier_length (n_node->p.other->origin, n_node->p.other->n.origin, n_node->p.origin, n_node->origin); + if (n_node->selected) { + n_sel_nodes ++; + n_sel_range = n_range; + } + if (n_node == p_node) { + n_going = false; + p_going = false; + } + } + if (p_node && p_going) + p_node = p_node->p.other; + if (p_node == NULL) { + p_going = false; + } else { + p_nodes ++; + p_range += bezier_length (p_node->n.other->origin, p_node->n.other->p.origin, p_node->n.origin, p_node->origin); + if (p_node->selected) { + p_sel_nodes ++; + p_sel_range = p_range; + } + if (p_node == n_node) { + n_going = false; + p_going = false; + } + } + } while (n_going || p_going); + } + + // Second pass: actually move nodes + sp_nodepath_move_node_and_handles (n, delta, delta, delta); + { + double n_range = 0, p_range = 0; + bool n_going = true, p_going = true; + Inkscape::NodePath::Node *n_node = n; + Inkscape::NodePath::Node *p_node = n; + do { + // Do one step in both directions from n, until reaching the end of subpath or bumping into each other + if (n_node && n_going) + n_node = n_node->n.other; + if (n_node == NULL) { + n_going = false; + } else { + n_range += bezier_length (n_node->p.other->origin, n_node->p.other->n.origin, n_node->p.origin, n_node->origin); + if (n_node->selected) { + sp_nodepath_move_node_and_handles (n_node, + sculpt_profile (n_range / n_sel_range, alpha) * delta, + sculpt_profile ((n_range + NR::L2(n_node->n.origin - n_node->origin)) / n_sel_range, alpha) * delta, + sculpt_profile ((n_range - NR::L2(n_node->p.origin - n_node->origin)) / n_sel_range, alpha) * delta); + } + if (n_node == p_node) { + n_going = false; + p_going = false; + } + } + if (p_node && p_going) + p_node = p_node->p.other; + if (p_node == NULL) { + p_going = false; + } else { + p_range += bezier_length (p_node->n.other->origin, p_node->n.other->p.origin, p_node->n.origin, p_node->origin); + if (p_node->selected) { + sp_nodepath_move_node_and_handles (p_node, + sculpt_profile (p_range / p_sel_range, alpha) * delta, + sculpt_profile ((p_range - NR::L2(p_node->n.origin - p_node->origin)) / p_sel_range, alpha) * delta, + sculpt_profile ((p_range + NR::L2(p_node->p.origin - p_node->origin)) / p_sel_range, alpha) * delta); + } + if (p_node == n_node) { + n_going = false; + p_going = false; + } + } + } while (n_going || p_going); + } + + // do not update repr here so that node dragging is acceptably fast + update_object(nodepath); +} + + /** * Move node selection to point, adjust its and neighbouring handles, * handle possible snapping, and commit the change with possible undo. @@ -1625,6 +1776,7 @@ void sp_node_selected_join_segment() */ void sp_node_delete_preserve(GList *nodes_to_delete) { + GSList *nodepaths = NULL; while (nodes_to_delete) { Inkscape::NodePath::Node *node = (Inkscape::NodePath::Node*) g_list_first(nodes_to_delete)->data; @@ -1716,9 +1868,6 @@ void sp_node_delete_preserve(GList *nodes_to_delete) sp_nodepath_node_destroy(temp); } - //clean up the nodepath (such as for trivial subpaths) - sp_nodepath_cleanup(nodepath); - sp_nodepath_update_handles(nodepath); // if the entire nodepath is removed, delete the selected object. @@ -1734,8 +1883,13 @@ void sp_node_delete_preserve(GList *nodes_to_delete) return; } - sp_nodepath_update_repr(nodepath); + if (!g_slist_find(nodepaths, nodepath)) + nodepaths = g_slist_prepend (nodepaths, nodepath); + } + for (GSList *i = nodepaths; i; i = i->next) { + Inkscape::NodePath::Path *nodepath = (Inkscape::NodePath::Path *) i->data; + sp_nodepath_update_repr(nodepath); sp_nodepath_update_statusbar(nodepath); } } @@ -2244,6 +2398,24 @@ void sp_nodepath_select_rect(Inkscape::NodePath::Path *nodepath, NR::Rect const } } + +/** +\brief Saves all nodes' and handles' current positions in their origin members +*/ +void +sp_nodepath_remember_origins(Inkscape::NodePath::Path *nodepath) +{ + for (GList *spl = nodepath->subpaths; spl != NULL; spl = spl->next) { + Inkscape::NodePath::SubPath *subpath = (Inkscape::NodePath::SubPath *) spl->data; + for (GList *nl = subpath->nodes; nl != NULL; nl = nl->next) { + Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) nl->data; + n->origin = n->pos; + n->p.origin = n->p.pos; + n->n.origin = n->n.pos; + } + } +} + /** \brief Saves selected nodes in a nodepath into a list containing integer positions of all selected nodes */ @@ -2515,11 +2687,11 @@ static void node_grabbed(SPKnot *knot, guint state, gpointer data) { Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) data; - n->origin = knot->pos; - if (!n->selected) { sp_nodepath_node_select(n, (state & GDK_SHIFT_MASK), FALSE); } + + sp_nodepath_remember_origins (n->subpath->nodepath); } /** @@ -2698,8 +2870,6 @@ node_request(SPKnot *knot, NR::Point *p, guint state, gpointer data) if (an == 0) na = HUGE_VAL; else na = -1/an; if (ap == 0) pa = HUGE_VAL; else pa = -1/ap; - //g_print("an %g ap %g\n", an, ap); - // mouse point relative to the node's original pos pr = (*p) - n->origin; @@ -2734,10 +2904,14 @@ node_request(SPKnot *knot, NR::Point *p, guint state, gpointer data) } } } else { // move freely - sp_nodepath_selected_nodes_move(n->subpath->nodepath, + if (state & GDK_MOD1_MASK) { // sculpt + sp_nodepath_selected_nodes_sculpt(n->subpath->nodepath, n, (*p) - n->origin); + } else { + sp_nodepath_selected_nodes_move(n->subpath->nodepath, (*p)[NR::X] - n->pos[NR::X], (*p)[NR::Y] - n->pos[NR::Y], (state & GDK_SHIFT_MASK) == 0); + } } n->subpath->nodepath->desktop->scroll_to_point(p); @@ -2781,9 +2955,9 @@ static void node_handle_grabbed(SPKnot *knot, guint state, gpointer data) // remember the origin point of the handle if (n->p.knot == knot) { - n->p.origin = n->p.pos - n->pos; + n->p.origin_radial = n->p.pos - n->pos; } else if (n->n.knot == knot) { - n->n.origin = n->n.pos - n->pos; + n->n.origin_radial = n->n.pos - n->pos; } else { g_assert_not_reached(); } @@ -2799,10 +2973,10 @@ static void node_handle_ungrabbed(SPKnot *knot, guint state, gpointer data) // forget origin and set knot position once more (because it can be wrong now due to restrictions) if (n->p.knot == knot) { - n->p.origin.a = 0; + n->p.origin_radial.a = 0; sp_knot_set_position(knot, &n->p.pos, state); } else if (n->n.knot == knot) { - n->n.origin.a = 0; + n->n.origin_radial.a = 0; sp_knot_set_position(knot, &n->n.pos, state); } else { g_assert_not_reached(); @@ -2891,13 +3065,13 @@ static void node_handle_moved(SPKnot *knot, NR::Point *p, guint state, gpointer // The closest PI/snaps angle, starting from zero. double const a_snapped = floor(rnew.a/(M_PI/snaps) + 0.5) * (M_PI/snaps); - if (me->origin.a == HUGE_VAL) { + if (me->origin_radial.a == HUGE_VAL) { // ortho doesn't exist: original handle was zero length. rnew.a = a_snapped; } else { /* The closest PI/2 angle, starting from original angle (i.e. snapping to original, * its opposite and perpendiculars). */ - double const a_ortho = me->origin.a + floor((rnew.a - me->origin.a)/(M_PI/2) + 0.5) * (M_PI/2); + double const a_ortho = me->origin_radial.a + floor((rnew.a - me->origin_radial.a)/(M_PI/2) + 0.5) * (M_PI/2); // Snap to the closest. rnew.a = ( fabs(a_snapped - rnew.a) < fabs(a_ortho - rnew.a) @@ -2908,7 +3082,7 @@ static void node_handle_moved(SPKnot *knot, NR::Point *p, guint state, gpointer if (state & GDK_MOD1_MASK) { // lock handle length - rnew.r = me->origin.r; + rnew.r = me->origin_radial.r; } if (( n->type !=Inkscape::NodePath::NODE_CUSP || (state & GDK_SHIFT_MASK)) diff --git a/src/nodepath.h b/src/nodepath.h index c9315470f..2b6131684 100644 --- a/src/nodepath.h +++ b/src/nodepath.h @@ -185,14 +185,16 @@ class NodeSide{ Node * other; /** Position */ NR::Point pos; +/** Origin (while dragging) in radial notation */ + Radial origin_radial; +/** Origin (while dragging) in x/y notation */ + NR::Point origin; /** Knots are Inkscape's way of providing draggable points. This * Knot is the point on the curve representing the control point in a * bezier curve.*/ SPKnot * knot; /** What kind of rendering? */ SPCanvasItem * line; -/** */ - Radial origin; }; /**