X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fpencil-context.cpp;h=a873eb6fcb9a84e383c68e006e1ee2040b13a7e9;hb=6302669b796eadd6994ecdc2e70f0119b29194dd;hp=99432ee96e0e3d9e7c0011f838f3347281f6ebd3;hpb=07c523b7778d16faf58d80af7b6c86a1b8c81e00;p=inkscape.git diff --git a/src/pencil-context.cpp b/src/pencil-context.cpp index 99432ee96..a873eb6fc 100644 --- a/src/pencil-context.cpp +++ b/src/pencil-context.cpp @@ -41,7 +41,9 @@ #include "document.h" #include "desktop-style.h" #include "macros.h" +#include "display/sp-canvas.h" #include "display/curve.h" +#include "livarot/Path.h" static void sp_pencil_context_class_init(SPPencilContextClass *klass); static void sp_pencil_context_init(SPPencilContext *pc); @@ -234,6 +236,15 @@ pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &beve return TRUE; } + if (!pc->grab) { + /* Grab mouse, so release will not pass unnoticed */ + pc->grab = SP_CANVAS_ITEM(desktop->acetate); + sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK | + GDK_BUTTON_RELEASE_MASK | + GDK_POINTER_MOTION_MASK ), + NULL, bevent.time); + } + Geom::Point const button_w(bevent.x, bevent.y); /* Find desktop coordinates */ @@ -252,38 +263,38 @@ pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &beve break; default: /* Set first point of sequence */ + SnapManager &m = desktop->namedview->snap_manager; + if (bevent.state & GDK_CONTROL_MASK) { - spdc_create_single_dot(event_context, from_2geom(p), "/tools/freehand/pencil", bevent.state); + m.setup(desktop); + if (!(bevent.state & GDK_SHIFT_MASK)) { + m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE); + } + spdc_create_single_dot(event_context, p, "/tools/freehand/pencil", bevent.state); + m.unSetup(); ret = true; break; } if (anchor) { - p = to_2geom(anchor->dp); + p = anchor->dp; desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path")); } else { - + m.setup(desktop); if (!(bevent.state & GDK_SHIFT_MASK)) { - // This is the first click of a new curve; deselect item so that // this curve is not combined with it (unless it is drawn from its // anchor, which is handled by the sibling branch above) selection->clear(); desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path")); - SnapManager &m = desktop->namedview->snap_manager; - m.setup(desktop); - Inkscape::SnappedPoint const s = m.freeSnap(Inkscape::SnapPreferences::SNAPPOINT_NODE, p); - if (s.getSnapped()) { - s.getPoint(p); - pc->prev_snap_was_succesful = true; - } else { - pc->prev_snap_was_succesful = false; - } + m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE); } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) { desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path")); + m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE); } + m.unSetup(); } pc->sa = anchor; - spdc_set_startpoint(pc, from_2geom(p)); + spdc_set_startpoint(pc, p); ret = TRUE; break; } @@ -296,6 +307,8 @@ pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &beve static gint pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent) { + SPDesktop *const dt = pc->desktop; + if ((mevent.state & GDK_CONTROL_MASK) && (mevent.state & GDK_BUTTON1_MASK)) { // mouse was accidentally moved during Ctrl+click; // ignore the motion and create a single point @@ -303,7 +316,6 @@ pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mev return TRUE; } gint ret = FALSE; - SPDesktop *const dt = pc->desktop; SPEventContext *event_context = SP_EVENT_CONTEXT(pc); if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) { @@ -321,7 +333,7 @@ pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mev } /* Find desktop coordinates */ - Geom::Point p = to_2geom(dt->w2d(Geom::Point(mevent.x, mevent.y))); + Geom::Point p = dt->w2d(Geom::Point(mevent.x, mevent.y)); /* Test whether we hit any anchor. */ SPDrawAnchor *anchor = spdc_test_inside(pc, Geom::Point(mevent.x, mevent.y)); @@ -343,48 +355,41 @@ pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mev case SP_PENCIL_CONTEXT_ADDLINE: /* Set red endpoint */ if (anchor) { - p = to_2geom(anchor->dp); + p = anchor->dp; } else { - Geom::Point ptnr = from_2geom(p); + Geom::Point ptnr(p); spdc_endpoint_snap(pc, ptnr, mevent.state); - p = to_2geom(ptnr); + p = ptnr; } - spdc_set_endpoint(pc, from_2geom(p)); + spdc_set_endpoint(pc, p); ret = TRUE; break; default: /* We may be idle or already freehand */ if ( mevent.state & GDK_BUTTON1_MASK && pc->is_drawing ) { + if (pc->state == SP_PENCIL_CONTEXT_IDLE) { + sp_event_context_discard_delayed_snap_event(event_context); + } pc->state = SP_PENCIL_CONTEXT_FREEHAND; + if ( !pc->sa && !pc->green_anchor ) { /* Create green anchor */ pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, pc->p[0]); } - /** \todo - * fixme: I am not sure whether we want to snap to anchors - * in middle of freehand (Lauris) - */ - SnapManager &m = dt->namedview->snap_manager; - if (anchor) { - p = to_2geom(anchor->dp); - } else if ((mevent.state & GDK_SHIFT_MASK) == 0) { - m.setup(dt); - Inkscape::SnappedPoint const s = m.freeSnap(Inkscape::SnapPreferences::SNAPPOINT_NODE, p); - if (s.getSnapped()) { - s.getPoint(p); - pc->prev_snap_was_succesful = true; - } else { - pc->prev_snap_was_succesful = false; - } + p = anchor->dp; } + if ( pc->npoints != 0) { // buttonpress may have happened before we entered draw context! - if (!(pc->prev_snap_was_succesful && m.snapprefs.getSnapPostponedGlobally())) { - // When snapping is enabled but temporarily on hold because the mouse is moving - // fast, then we don't want to add nodes off-grid - spdc_add_freehand_point(pc, from_2geom(p), mevent.state); - ret = TRUE; - } + if (pc->ps.size() == 0) { + // Only in freehand mode we have to add the first point also to pc->ps (apparently) + // - We cannot add this point in spdc_set_startpoint, because we only need it for freehand + // - We cannot do this in the button press handler because at that point we don't know yet + // wheter we're going into freehand mode or not + pc->ps.push_back(pc->p[0]); + } + spdc_add_freehand_point(pc, p, mevent.state); + ret = TRUE; } if (anchor && !pc->anchor_statusbar) { @@ -406,6 +411,16 @@ pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mev pc->anchor_statusbar = false; } } + + // Show the pre-snap indicator to communicate to the user where we would snap to if he/she were to + // a) press the mousebutton to start a freehand drawing, or + // b) release the mousebutton to finish a freehand drawing + if (!sp_event_context_knot_mouseover(pc)) { + SnapManager &m = dt->namedview->snap_manager; + m.setup(dt); + m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE)); + m.unSetup(); + } break; } return ret; @@ -450,27 +465,34 @@ pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &re spdc_set_endpoint(pc, p); spdc_finish_endpoint(pc); pc->state = SP_PENCIL_CONTEXT_IDLE; + sp_event_context_discard_delayed_snap_event(event_context); ret = TRUE; break; case SP_PENCIL_CONTEXT_FREEHAND: if (revent.state & GDK_MOD1_MASK) { /* sketch mode: interpolate the sketched path and improve the current output path with the new interpolation. don't finish sketch */ - if (anchor) { - p = anchor->dp; - } - pc->ea = anchor; - - dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Sketching: release Alt to finalize current curve")); sketch_interpolate(pc); + if (pc->green_anchor) { + pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor); + } + pc->state = SP_PENCIL_CONTEXT_SKETCH; } else { /* Finish segment now */ /// \todo fixme: Clean up what follows (Lauris) if (anchor) { p = anchor->dp; + } else { + Geom::Point p_end = p; + spdc_endpoint_snap(pc, p_end, revent.state); + if (p_end != p) { + // then we must have snapped! + spdc_add_freehand_point(pc, p_end, revent.state); + } } + pc->ea = anchor; /* Write curves to object */ @@ -484,6 +506,8 @@ pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &re pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor); } pc->state = SP_PENCIL_CONTEXT_IDLE; + // reset sketch mode too + pc->sketch_n = 0; } ret = TRUE; break; @@ -504,7 +528,7 @@ pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &re } static void -pencil_cancel (SPPencilContext *const pc) +pencil_cancel (SPPencilContext *const pc) { if (pc->grab) { /* Release grab now */ @@ -513,8 +537,8 @@ pencil_cancel (SPPencilContext *const pc) } pc->is_drawing = false; - pc->state = SP_PENCIL_CONTEXT_IDLE; + sp_event_context_discard_delayed_snap_event(SP_EVENT_CONTEXT(pc)); pc->red_curve->reset(); sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL); @@ -573,6 +597,14 @@ pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint con ret = true; } break; + case GDK_Alt_L: + case GDK_Alt_R: + case GDK_Meta_L: + case GDK_Meta_R: + if (pc->state == SP_PENCIL_CONTEXT_IDLE) { + pc->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Sketch mode: holding Alt interpolates between sketched paths. Release Alt to finalize.")); + } + break; default: break; } @@ -597,6 +629,8 @@ pencil_handle_key_release(SPPencilContext *const pc, guint const keyval, guint c pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor); } pc->state = SP_PENCIL_CONTEXT_IDLE; + sp_event_context_discard_delayed_snap_event(SP_EVENT_CONTEXT(pc)); + pc->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand sketch")); ret = TRUE; } break; @@ -705,12 +739,13 @@ square(double const x) static void interpolate(SPPencilContext *pc) { - - g_assert( pc->ps.size() > 1 ); + if ( pc->ps.size() <= 1 ) { + return; + } Inkscape::Preferences *prefs = Inkscape::Preferences::get(); double const tol = prefs->getDoubleLimited("/tools/freehand/pencil/tolerance", 10.0, 1.0, 100.0) * 0.4; - double const tolerance_sq = 0.02 * square( pc->desktop->w2d().descrim() * + double const tolerance_sq = 0.02 * square( pc->desktop->w2d().descrim() * tol) * exp(0.2*tol - 2); g_assert(is_zero(pc->req_tangent) @@ -724,7 +759,7 @@ interpolate(SPPencilContext *pc) Geom::Point * b = g_new(Geom::Point, 4*n_points); Geom::Point * points = g_new(Geom::Point, 4*n_points); - for (unsigned int i = 0; i < pc->ps.size(); i++) { + for (unsigned int i = 0; i < pc->ps.size(); i++) { points[i] = pc->ps[i]; } @@ -738,7 +773,7 @@ interpolate(SPPencilContext *pc) { /* Fit and draw and reset state */ pc->green_curve->moveto(b[0]); - for (int c = 0; c < n_segs; c++) { + for (int c = 0; c < n_segs; c++) { pc->green_curve->curveto(b[4*c+1], b[4*c+2], b[4*c+3]); } sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->green_curve); @@ -768,11 +803,13 @@ interpolate(SPPencilContext *pc) static void sketch_interpolate(SPPencilContext *pc) { - g_assert( pc->ps.size() > 1 ); + if ( pc->ps.size() <= 1 ) { + return; + } Inkscape::Preferences *prefs = Inkscape::Preferences::get(); double const tol = prefs->getDoubleLimited("/tools/freehand/pencil/tolerance", 10.0, 1.0, 100.0) * 0.4; - double const tolerance_sq = 0.02 * square( pc->desktop->w2d().descrim() * + double const tolerance_sq = 0.02 * square( pc->desktop->w2d().descrim() * tol) * exp(0.2*tol - 2); bool average_all_sketches = prefs->getBool("/tools/freehand/pencil/average_all_sketches", true); @@ -787,7 +824,7 @@ sketch_interpolate(SPPencilContext *pc) Geom::Point * b = g_new(Geom::Point, 4*n_points); Geom::Point * points = g_new(Geom::Point, 4*n_points); - for (unsigned i = 0; i < pc->ps.size(); i++) { + for (unsigned i = 0; i < pc->ps.size(); i++) { points[i] = pc->ps[i]; } @@ -800,7 +837,7 @@ sketch_interpolate(SPPencilContext *pc) if ( n_segs > 0) { Geom::Path fit(b[0]); - for (int c = 0; c < n_segs; c++) { + for (int c = 0; c < n_segs; c++) { fit.appendNew(b[4*c+1], b[4*c+2], b[4*c+3]); } Geom::Piecewise > fit_pwd2 = fit.toPwSb(); @@ -815,8 +852,18 @@ sketch_interpolate(SPPencilContext *pc) } else { t = 0.5; } + pc->sketch_interpolation = Geom::lerp(t, fit_pwd2, pc->sketch_interpolation); + // simplify path, to eliminate small segments + Path *path = new Path; + path->LoadPathVector(Geom::path_from_piecewise(pc->sketch_interpolation, 0.01)); + path->Simplify(0.5); + Geom::PathVector *pathv = path->MakePathVector(); + pc->sketch_interpolation = (*pathv)[0].toPwSb(); + delete path; + delete pathv; + } else { + pc->sketch_interpolation = fit_pwd2; } - pc->sketch_interpolation = Geom::lerp(fit_pwd2, pc->sketch_interpolation, t); pc->sketch_n++; pc->green_curve->reset(); @@ -855,7 +902,7 @@ fit_and_split(SPPencilContext *pc) || is_unit_vector(pc->req_tangent)); Geom::Point const tHatEnd(0, 0); int const n_segs = Geom::bezier_fit_cubic_full(b, NULL, pc->p, pc->npoints, - pc->req_tangent, tHatEnd, + pc->req_tangent, tHatEnd, tolerance_sq, 1); if ( n_segs > 0 && unsigned(pc->npoints) < G_N_ELEMENTS(pc->p) ) @@ -907,4 +954,4 @@ fit_and_split(SPPencilContext *pc) 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 :